blob: 47aa527c7ac78d368b2efef6e9db134a9cef46dd [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000030#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// X86TargetLowering - X86 Implementation of the TargetLowering interface
35namespace {
36 class X86TargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000038 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000039 public:
40 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000042
43 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
44 setShiftAmountType(MVT::i8);
45 setSetCCResultType(MVT::i8);
46
47 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000048 addRegisterClass(MVT::i8, X86::R8RegisterClass);
49 addRegisterClass(MVT::i16, X86::R16RegisterClass);
50 addRegisterClass(MVT::i32, X86::R32RegisterClass);
51 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
52
53 // FIXME: Eliminate these two classes when legalize can handle promotions
54 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000055/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
56/**/ //addRegisterClass(MVT::f32, X86::RFPRegisterClass);
57
58 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
59 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
60 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i16 , Expand);
61 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
62 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i1 , Expand);
63 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
64 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
65 setOperationAction(ISD::SREM , MVT::f64 , Expand);
66
67 // These should be promoted to a larger select which is supported.
68/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
69 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000070
71 computeRegisterProperties();
Chris Lattner8acb1ba2005-01-07 07:49:41 +000072
73 addLegalFPImmediate(+0.0); // FLD0
74 addLegalFPImmediate(+1.0); // FLD1
75 addLegalFPImmediate(-0.0); // FLD0/FCHS
76 addLegalFPImmediate(-1.0); // FLD1/FCHS
77 }
78
79 /// LowerArguments - This hook must be implemented to indicate how we should
80 /// lower the arguments for the specified function, into the specified DAG.
81 virtual std::vector<SDOperand>
82 LowerArguments(Function &F, SelectionDAG &DAG);
83
84 /// LowerCallTo - This hook lowers an abstract call to a function into an
85 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000086 virtual std::pair<SDOperand, SDOperand>
87 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
88 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000089
90 virtual std::pair<SDOperand, SDOperand>
91 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
92
93 virtual std::pair<SDOperand,SDOperand>
94 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
95 const Type *ArgTy, SelectionDAG &DAG);
96
97 virtual std::pair<SDOperand, SDOperand>
98 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
99 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000100 };
101}
102
103
104std::vector<SDOperand>
105X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
106 std::vector<SDOperand> ArgValues;
107
108 // Add DAG nodes to load the arguments... On entry to a function on the X86,
109 // the stack frame looks like this:
110 //
111 // [ESP] -- return address
112 // [ESP + 4] -- first argument (leftmost lexically)
113 // [ESP + 8] -- second argument, if first argument is four bytes in size
114 // ...
115 //
116 MachineFunction &MF = DAG.getMachineFunction();
117 MachineFrameInfo *MFI = MF.getFrameInfo();
118
119 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
120 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
121 MVT::ValueType ObjectVT = getValueType(I->getType());
122 unsigned ArgIncrement = 4;
123 unsigned ObjSize;
124 switch (ObjectVT) {
125 default: assert(0 && "Unhandled argument type!");
126 case MVT::i1:
127 case MVT::i8: ObjSize = 1; break;
128 case MVT::i16: ObjSize = 2; break;
129 case MVT::i32: ObjSize = 4; break;
130 case MVT::i64: ObjSize = ArgIncrement = 8; break;
131 case MVT::f32: ObjSize = 4; break;
132 case MVT::f64: ObjSize = ArgIncrement = 8; break;
133 }
134 // Create the frame index object for this incoming parameter...
135 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
136
137 // Create the SelectionDAG nodes corresponding to a load from this parameter
138 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
139
140 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
141 // dead loads.
142 SDOperand ArgValue;
143 if (!I->use_empty())
144 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
145 else {
146 if (MVT::isInteger(ObjectVT))
147 ArgValue = DAG.getConstant(0, ObjectVT);
148 else
149 ArgValue = DAG.getConstantFP(0, ObjectVT);
150 }
151 ArgValues.push_back(ArgValue);
152
153 ArgOffset += ArgIncrement; // Move on to the next argument...
154 }
155
156 // If the function takes variable number of arguments, make a frame index for
157 // the start of the first vararg value... for expansion of llvm.va_start.
158 if (F.isVarArg())
159 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000160 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000161 return ArgValues;
162}
163
Chris Lattner5188ad72005-01-08 19:28:19 +0000164std::pair<SDOperand, SDOperand>
165X86TargetLowering::LowerCallTo(SDOperand Chain,
166 const Type *RetTy, SDOperand Callee,
167 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000168 // Count how many bytes are to be pushed on the stack.
169 unsigned NumBytes = 0;
170
171 if (Args.empty()) {
172 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000173 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
174 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000175 } else {
176 for (unsigned i = 0, e = Args.size(); i != e; ++i)
177 switch (getValueType(Args[i].second)) {
178 default: assert(0 && "Unknown value type!");
179 case MVT::i1:
180 case MVT::i8:
181 case MVT::i16:
182 case MVT::i32:
183 case MVT::f32:
184 NumBytes += 4;
185 break;
186 case MVT::i64:
187 case MVT::f64:
188 NumBytes += 8;
189 break;
190 }
191
Chris Lattner5188ad72005-01-08 19:28:19 +0000192 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
193 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000194
195 // Arguments go on the stack in reverse order, as specified by the ABI.
196 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000197 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
198 DAG.getEntryNode());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000199 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
200 unsigned ArgReg;
201 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
202 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
203
204 switch (getValueType(Args[i].second)) {
205 default: assert(0 && "Unexpected ValueType for argument!");
206 case MVT::i1:
207 case MVT::i8:
208 case MVT::i16:
209 // Promote the integer to 32 bits. If the input type is signed use a
210 // sign extend, otherwise use a zero extend.
211 if (Args[i].second->isSigned())
212 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
213 else
214 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
215
216 // FALL THROUGH
217 case MVT::i32:
218 case MVT::f32:
219 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000220 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
221 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000222 ArgOffset += 4;
223 break;
224 case MVT::i64:
225 case MVT::f64:
226 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000227 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
228 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000229 ArgOffset += 8;
230 break;
231 }
232 }
233 }
234
235 std::vector<MVT::ValueType> RetVals;
236 MVT::ValueType RetTyVT = getValueType(RetTy);
237 if (RetTyVT != MVT::isVoid)
238 RetVals.push_back(RetTyVT);
239 RetVals.push_back(MVT::Other);
240
Chris Lattner5188ad72005-01-08 19:28:19 +0000241 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000242 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000243 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
244 DAG.getConstant(NumBytes, getPointerTy()));
245 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000246}
247
Chris Lattner14824582005-01-09 00:01:27 +0000248std::pair<SDOperand, SDOperand>
249X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
250 // vastart just returns the address of the VarArgsFrameIndex slot.
251 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
252}
253
254std::pair<SDOperand,SDOperand> X86TargetLowering::
255LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
256 const Type *ArgTy, SelectionDAG &DAG) {
257 MVT::ValueType ArgVT = getValueType(ArgTy);
258 SDOperand Result;
259 if (!isVANext) {
260 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
261 } else {
262 unsigned Amt;
263 if (ArgVT == MVT::i32)
264 Amt = 4;
265 else {
266 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
267 "Other types should have been promoted for varargs!");
268 Amt = 8;
269 }
270 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
271 DAG.getConstant(Amt, VAList.getValueType()));
272 }
273 return std::make_pair(Result, Chain);
274}
275
276
277std::pair<SDOperand, SDOperand> X86TargetLowering::
278LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
279 SelectionDAG &DAG) {
280 SDOperand Result;
281 if (Depth) // Depths > 0 not supported yet!
282 Result = DAG.getConstant(0, getPointerTy());
283 else {
284 if (ReturnAddrIndex == 0) {
285 // Set up a frame object for the return address.
286 MachineFunction &MF = DAG.getMachineFunction();
287 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
288 }
289
290 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
291
292 if (!isFrameAddress)
293 // Just load the return address
294 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
295 else
296 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
297 DAG.getConstant(4, MVT::i32));
298 }
299 return std::make_pair(Result, Chain);
300}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000301
302
Chris Lattner98a8ba02005-01-18 01:06:26 +0000303namespace {
304 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
305 /// SDOperand's instead of register numbers for the leaves of the matched
306 /// tree.
307 struct X86ISelAddressMode {
308 enum {
309 RegBase,
310 FrameIndexBase,
311 } BaseType;
312
313 struct { // This is really a union, discriminated by BaseType!
314 SDOperand Reg;
315 int FrameIndex;
316 } Base;
317
318 unsigned Scale;
319 SDOperand IndexReg;
320 unsigned Disp;
321 GlobalValue *GV;
322
323 X86ISelAddressMode()
324 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
325 }
326 };
327}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000328
329
330namespace {
331 Statistic<>
332 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
333
334 //===--------------------------------------------------------------------===//
335 /// ISel - X86 specific code to select X86 machine instructions for
336 /// SelectionDAG operations.
337 ///
338 class ISel : public SelectionDAGISel {
339 /// ContainsFPCode - Every instruction we select that uses or defines a FP
340 /// register should set this to true.
341 bool ContainsFPCode;
342
343 /// X86Lowering - This object fully describes how to lower LLVM code to an
344 /// X86-specific SelectionDAG.
345 X86TargetLowering X86Lowering;
346
Chris Lattner11333092005-01-11 03:11:44 +0000347 /// RegPressureMap - This keeps an approximate count of the number of
348 /// registers required to evaluate each node in the graph.
349 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000350
351 /// ExprMap - As shared expressions are codegen'd, we keep track of which
352 /// vreg the value is produced in, so we only emit one copy of each compiled
353 /// tree.
354 std::map<SDOperand, unsigned> ExprMap;
355 std::set<SDOperand> LoweredTokens;
356
357 public:
358 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
359 }
360
Chris Lattner11333092005-01-11 03:11:44 +0000361 unsigned getRegPressure(SDOperand O) {
362 return RegPressureMap[O.Val];
363 }
364 unsigned ComputeRegPressure(SDOperand O);
365
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000366 /// InstructionSelectBasicBlock - This callback is invoked by
367 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000368 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000369
Chris Lattner4ff348b2005-01-17 06:26:58 +0000370 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp);
Chris Lattnera5ade062005-01-11 21:19:59 +0000371 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000372 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000373
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000374 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000375 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000376 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
377 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000378 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000379
380 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
381 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
382 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000383 void Select(SDOperand N);
384 };
385}
386
Chris Lattner7dbcb752005-01-12 04:21:28 +0000387/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
388/// when it has created a SelectionDAG for us to codegen.
389void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
390 // While we're doing this, keep track of whether we see any FP code for
391 // FP_REG_KILL insertion.
392 ContainsFPCode = false;
393
394 // Scan the PHI nodes that already are inserted into this basic block. If any
395 // of them is a PHI of a floating point value, we need to insert an
396 // FP_REG_KILL.
397 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
398 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
399 I != E; ++I) {
400 assert(I->getOpcode() == X86::PHI &&
401 "Isn't just PHI nodes?");
402 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
403 X86::RFPRegisterClass) {
404 ContainsFPCode = true;
405 break;
406 }
407 }
408
409 // Compute the RegPressureMap, which is an approximation for the number of
410 // registers required to compute each node.
411 ComputeRegPressure(DAG.getRoot());
412
413 // Codegen the basic block.
414 Select(DAG.getRoot());
415
416 // Finally, look at all of the successors of this block. If any contain a PHI
417 // node of FP type, we need to insert an FP_REG_KILL in this block.
418 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
419 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
420 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
421 I != E && I->getOpcode() == X86::PHI; ++I) {
422 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
423 X86::RFPRegisterClass) {
424 ContainsFPCode = true;
425 break;
426 }
427 }
428
429 // Insert FP_REG_KILL instructions into basic blocks that need them. This
430 // only occurs due to the floating point stackifier not being aggressive
431 // enough to handle arbitrary global stackification.
432 //
433 // Currently we insert an FP_REG_KILL instruction into each block that uses or
434 // defines a floating point virtual register.
435 //
436 // When the global register allocators (like linear scan) finally update live
437 // variable analysis, we can keep floating point values in registers across
438 // basic blocks. This will be a huge win, but we are waiting on the global
439 // allocators before we can do this.
440 //
441 if (ContainsFPCode && BB->succ_size()) {
442 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
443 ++NumFPKill;
444 }
445
446 // Clear state used for selection.
447 ExprMap.clear();
448 LoweredTokens.clear();
449 RegPressureMap.clear();
450}
451
452
Chris Lattner11333092005-01-11 03:11:44 +0000453// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
454// for the number of registers required to compute each node. This is basically
455// computing a generalized form of the Sethi-Ullman number for each node.
456unsigned ISel::ComputeRegPressure(SDOperand O) {
457 SDNode *N = O.Val;
458 unsigned &Result = RegPressureMap[N];
459 if (Result) return Result;
460
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000461 // FIXME: Should operations like CALL (which clobber lots o regs) have a
462 // higher fixed cost??
463
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000464 if (N->getNumOperands() == 0) {
465 Result = 1;
466 } else {
467 unsigned MaxRegUse = 0;
468 unsigned NumExtraMaxRegUsers = 0;
469 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
470 unsigned Regs;
471 if (N->getOperand(i).getOpcode() == ISD::Constant)
472 Regs = 0;
473 else
474 Regs = ComputeRegPressure(N->getOperand(i));
475 if (Regs > MaxRegUse) {
476 MaxRegUse = Regs;
477 NumExtraMaxRegUsers = 0;
478 } else if (Regs == MaxRegUse &&
479 N->getOperand(i).getValueType() != MVT::Other) {
480 ++NumExtraMaxRegUsers;
481 }
Chris Lattner11333092005-01-11 03:11:44 +0000482 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000483
484 if (O.getOpcode() != ISD::TokenFactor)
485 Result = MaxRegUse+NumExtraMaxRegUsers;
486 else
Chris Lattner869e0432005-01-17 23:02:13 +0000487 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000488 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000489
Chris Lattner837caa72005-01-11 23:21:30 +0000490 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000491 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000492}
493
Chris Lattner98a8ba02005-01-18 01:06:26 +0000494X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
495 X86AddressMode Result;
496
497 // If we need to emit two register operands, emit the one with the highest
498 // register pressure first.
499 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
500 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
501 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
502 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
503 Result.IndexReg = SelectExpr(IAM.IndexReg);
504 } else {
505 Result.IndexReg = SelectExpr(IAM.IndexReg);
506 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
507 }
508 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
509 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
510 } else if (IAM.IndexReg.Val) {
511 Result.IndexReg = SelectExpr(IAM.IndexReg);
512 }
513
514 switch (IAM.BaseType) {
515 case X86ISelAddressMode::RegBase:
516 Result.BaseType = X86AddressMode::RegBase;
517 break;
518 case X86ISelAddressMode::FrameIndexBase:
519 Result.BaseType = X86AddressMode::FrameIndexBase;
520 Result.Base.FrameIndex = IAM.Base.FrameIndex;
521 break;
522 default:
523 assert(0 && "Unknown base type!");
524 break;
525 }
526 Result.Scale = IAM.Scale;
527 Result.Disp = IAM.Disp;
528 Result.GV = IAM.GV;
529 return Result;
530}
531
532/// SelectAddress - Pattern match the maximal addressing mode for this node and
533/// emit all of the leaf registers.
534void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
535 X86ISelAddressMode IAM;
536 MatchAddress(N, IAM);
537 AM = SelectAddrExprs(IAM);
538}
539
540/// MatchAddress - Add the specified node to the specified addressing mode,
541/// returning true if it cannot be done. This just pattern matches for the
542/// addressing mode, it does not cause any code to be emitted. For that, use
543/// SelectAddress.
544bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000545 switch (N.getOpcode()) {
546 default: break;
547 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000548 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
549 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000550 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
551 return false;
552 }
553 break;
554 case ISD::GlobalAddress:
555 if (AM.GV == 0) {
556 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
557 return false;
558 }
559 break;
560 case ISD::Constant:
561 AM.Disp += cast<ConstantSDNode>(N)->getValue();
562 return false;
563 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000564 // We might have folded the load into this shift, so don't regen the value
565 // if so.
566 if (ExprMap.count(N)) break;
567
Chris Lattner98a8ba02005-01-18 01:06:26 +0000568 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000569 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
570 unsigned Val = CN->getValue();
571 if (Val == 1 || Val == 2 || Val == 3) {
572 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000573 SDOperand ShVal = N.Val->getOperand(0);
574
575 // Okay, we know that we have a scale by now. However, if the scaled
576 // value is an add of something and a constant, we can fold the
577 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000578 if (ShVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(ShVal) &&
Chris Lattner51a26342005-01-11 06:36:20 +0000579 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000580 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000581 ConstantSDNode *AddVal =
582 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
583 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000584 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000585 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000586 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000587 return false;
588 }
589 }
590 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000591 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000592 // We might have folded the load into this mul, so don't regen the value if
593 // so.
594 if (ExprMap.count(N)) break;
595
Chris Lattner947d5442005-01-11 19:37:02 +0000596 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000597 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
598 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000599 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
600 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
601 AM.Scale = unsigned(CN->getValue())-1;
602
603 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000604 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000605
606 // Okay, we know that we have a scale by now. However, if the scaled
607 // value is an add of something and a constant, we can fold the
608 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000609 if (MulVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(MulVal) &&
Chris Lattner947d5442005-01-11 19:37:02 +0000610 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000611 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000612 ConstantSDNode *AddVal =
613 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
614 AM.Disp += AddVal->getValue() * CN->getValue();
615 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000616 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000617 }
618
619 AM.IndexReg = AM.Base.Reg = Reg;
620 return false;
621 }
622 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000623
624 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000625 // We might have folded the load into this mul, so don't regen the value if
626 // so.
627 if (ExprMap.count(N)) break;
628
Chris Lattner98a8ba02005-01-18 01:06:26 +0000629 X86ISelAddressMode Backup = AM;
630 if (!MatchAddress(N.Val->getOperand(0), AM) &&
631 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000632 return false;
633 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000634 if (!MatchAddress(N.Val->getOperand(1), AM) &&
635 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000636 return false;
637 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000638 break;
639 }
640 }
641
Chris Lattnera95589b2005-01-11 04:40:19 +0000642 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000643 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000644 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000645 if (AM.IndexReg.Val == 0) {
646 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000647 AM.Scale = 1;
648 return false;
649 }
650
651 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000652 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000653 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000654
655 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000656 AM.BaseType = X86ISelAddressMode::RegBase;
657 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000658 return false;
659}
660
661/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
662/// assuming that the temporary registers are in the 8-bit register class.
663///
664/// Tmp1 = setcc1
665/// Tmp2 = setcc2
666/// DestReg = logicalop Tmp1, Tmp2
667///
668static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
669 unsigned SetCC2, unsigned LogicalOp,
670 unsigned DestReg) {
671 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
672 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
673 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
674 BuildMI(BB, SetCC1, 0, Tmp1);
675 BuildMI(BB, SetCC2, 0, Tmp2);
676 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
677}
678
679/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
680/// condition codes match the specified SetCCOpcode. Note that some conditions
681/// require multiple instructions to generate the correct value.
682static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
683 ISD::CondCode SetCCOpcode, bool isFP) {
684 unsigned Opc;
685 if (!isFP) {
686 switch (SetCCOpcode) {
687 default: assert(0 && "Illegal integer SetCC!");
688 case ISD::SETEQ: Opc = X86::SETEr; break;
689 case ISD::SETGT: Opc = X86::SETGr; break;
690 case ISD::SETGE: Opc = X86::SETGEr; break;
691 case ISD::SETLT: Opc = X86::SETLr; break;
692 case ISD::SETLE: Opc = X86::SETLEr; break;
693 case ISD::SETNE: Opc = X86::SETNEr; break;
694 case ISD::SETULT: Opc = X86::SETBr; break;
695 case ISD::SETUGT: Opc = X86::SETAr; break;
696 case ISD::SETULE: Opc = X86::SETBEr; break;
697 case ISD::SETUGE: Opc = X86::SETAEr; break;
698 }
699 } else {
700 // On a floating point condition, the flags are set as follows:
701 // ZF PF CF op
702 // 0 | 0 | 0 | X > Y
703 // 0 | 0 | 1 | X < Y
704 // 1 | 0 | 0 | X == Y
705 // 1 | 1 | 1 | unordered
706 //
707 switch (SetCCOpcode) {
708 default: assert(0 && "Invalid FP setcc!");
709 case ISD::SETUEQ:
710 case ISD::SETEQ:
711 Opc = X86::SETEr; // True if ZF = 1
712 break;
713 case ISD::SETOGT:
714 case ISD::SETGT:
715 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
716 break;
717 case ISD::SETOGE:
718 case ISD::SETGE:
719 Opc = X86::SETAEr; // True if CF = 0
720 break;
721 case ISD::SETULT:
722 case ISD::SETLT:
723 Opc = X86::SETBr; // True if CF = 1
724 break;
725 case ISD::SETULE:
726 case ISD::SETLE:
727 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
728 break;
729 case ISD::SETONE:
730 case ISD::SETNE:
731 Opc = X86::SETNEr; // True if ZF = 0
732 break;
733 case ISD::SETUO:
734 Opc = X86::SETPr; // True if PF = 1
735 break;
736 case ISD::SETO:
737 Opc = X86::SETNPr; // True if PF = 0
738 break;
739 case ISD::SETOEQ: // !PF & ZF
740 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
741 return;
742 case ISD::SETOLT: // !PF & CF
743 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
744 return;
745 case ISD::SETOLE: // !PF & (CF || ZF)
746 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
747 return;
748 case ISD::SETUGT: // PF | (!ZF & !CF)
749 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
750 return;
751 case ISD::SETUGE: // PF | !CF
752 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
753 return;
754 case ISD::SETUNE: // PF | !ZF
755 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
756 return;
757 }
758 }
759 BuildMI(BB, Opc, 0, DestReg);
760}
761
762
763/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
764/// the Dest block if the Cond condition is true. If we cannot fold this
765/// condition into the branch, return true.
766///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000767bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
768 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000769 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
770 // B) using two conditional branches instead of one condbr, two setcc's, and
771 // an or.
772 if ((Cond.getOpcode() == ISD::OR ||
773 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
774 // And and or set the flags for us, so there is no need to emit a TST of the
775 // result. It is only safe to do this if there is only a single use of the
776 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000777 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000778 SelectExpr(Cond);
779 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
780 return false;
781 }
782
783 // Codegen br not C -> JE.
784 if (Cond.getOpcode() == ISD::XOR)
785 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
786 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000787 unsigned CondR;
788 if (getRegPressure(Chain) > getRegPressure(Cond)) {
789 Select(Chain);
790 CondR = SelectExpr(Cond.Val->getOperand(0));
791 } else {
792 CondR = SelectExpr(Cond.Val->getOperand(0));
793 Select(Chain);
794 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000795 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
796 BuildMI(BB, X86::JE, 1).addMBB(Dest);
797 return false;
798 }
799
800 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
801 if (SetCC == 0)
802 return true; // Can only handle simple setcc's so far.
803
804 unsigned Opc;
805
806 // Handle integer conditions first.
807 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
808 switch (SetCC->getCondition()) {
809 default: assert(0 && "Illegal integer SetCC!");
810 case ISD::SETEQ: Opc = X86::JE; break;
811 case ISD::SETGT: Opc = X86::JG; break;
812 case ISD::SETGE: Opc = X86::JGE; break;
813 case ISD::SETLT: Opc = X86::JL; break;
814 case ISD::SETLE: Opc = X86::JLE; break;
815 case ISD::SETNE: Opc = X86::JNE; break;
816 case ISD::SETULT: Opc = X86::JB; break;
817 case ISD::SETUGT: Opc = X86::JA; break;
818 case ISD::SETULE: Opc = X86::JBE; break;
819 case ISD::SETUGE: Opc = X86::JAE; break;
820 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000821 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000822 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000823 BuildMI(BB, Opc, 1).addMBB(Dest);
824 return false;
825 }
826
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000827 unsigned Opc2 = 0; // Second branch if needed.
828
829 // On a floating point condition, the flags are set as follows:
830 // ZF PF CF op
831 // 0 | 0 | 0 | X > Y
832 // 0 | 0 | 1 | X < Y
833 // 1 | 0 | 0 | X == Y
834 // 1 | 1 | 1 | unordered
835 //
836 switch (SetCC->getCondition()) {
837 default: assert(0 && "Invalid FP setcc!");
838 case ISD::SETUEQ:
839 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
840 case ISD::SETOGT:
841 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
842 case ISD::SETOGE:
843 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
844 case ISD::SETULT:
845 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
846 case ISD::SETULE:
847 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
848 case ISD::SETONE:
849 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
850 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
851 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
852 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
853 Opc = X86::JA; // ZF = 0 & CF = 0
854 Opc2 = X86::JP; // PF = 1
855 break;
856 case ISD::SETUGE: // PF = 1 | CF = 0
857 Opc = X86::JAE; // CF = 0
858 Opc2 = X86::JP; // PF = 1
859 break;
860 case ISD::SETUNE: // PF = 1 | ZF = 0
861 Opc = X86::JNE; // ZF = 0
862 Opc2 = X86::JP; // PF = 1
863 break;
864 case ISD::SETOEQ: // PF = 0 & ZF = 1
865 //X86::JNP, X86::JE
866 //X86::AND8rr
867 return true; // FIXME: Emit more efficient code for this branch.
868 case ISD::SETOLT: // PF = 0 & CF = 1
869 //X86::JNP, X86::JB
870 //X86::AND8rr
871 return true; // FIXME: Emit more efficient code for this branch.
872 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
873 //X86::JNP, X86::JBE
874 //X86::AND8rr
875 return true; // FIXME: Emit more efficient code for this branch.
876 }
877
Chris Lattner6c07aee2005-01-11 04:06:27 +0000878 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000879 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000880 BuildMI(BB, Opc, 1).addMBB(Dest);
881 if (Opc2)
882 BuildMI(BB, Opc2, 1).addMBB(Dest);
883 return false;
884}
885
Chris Lattner24aad1b2005-01-10 22:10:13 +0000886/// EmitSelectCC - Emit code into BB that performs a select operation between
887/// the two registers RTrue and RFalse, generating a result into RDest. Return
888/// true if the fold cannot be performed.
889///
890void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
891 unsigned RTrue, unsigned RFalse, unsigned RDest) {
892 enum Condition {
893 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
894 NOT_SET
895 } CondCode = NOT_SET;
896
897 static const unsigned CMOVTAB16[] = {
898 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
899 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
900 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
901 };
902 static const unsigned CMOVTAB32[] = {
903 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
904 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
905 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
906 };
907 static const unsigned CMOVTABFP[] = {
908 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
909 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
910 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
911 };
912
913 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
914 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
915 switch (SetCC->getCondition()) {
916 default: assert(0 && "Unknown integer comparison!");
917 case ISD::SETEQ: CondCode = EQ; break;
918 case ISD::SETGT: CondCode = GT; break;
919 case ISD::SETGE: CondCode = GE; break;
920 case ISD::SETLT: CondCode = LT; break;
921 case ISD::SETLE: CondCode = LE; break;
922 case ISD::SETNE: CondCode = NE; break;
923 case ISD::SETULT: CondCode = B; break;
924 case ISD::SETUGT: CondCode = A; break;
925 case ISD::SETULE: CondCode = BE; break;
926 case ISD::SETUGE: CondCode = AE; break;
927 }
928 } else {
929 // On a floating point condition, the flags are set as follows:
930 // ZF PF CF op
931 // 0 | 0 | 0 | X > Y
932 // 0 | 0 | 1 | X < Y
933 // 1 | 0 | 0 | X == Y
934 // 1 | 1 | 1 | unordered
935 //
936 switch (SetCC->getCondition()) {
937 default: assert(0 && "Unknown FP comparison!");
938 case ISD::SETUEQ:
939 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
940 case ISD::SETOGT:
941 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
942 case ISD::SETOGE:
943 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
944 case ISD::SETULT:
945 case ISD::SETLT: CondCode = B; break; // True if CF = 1
946 case ISD::SETULE:
947 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
948 case ISD::SETONE:
949 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
950 case ISD::SETUO: CondCode = P; break; // True if PF = 1
951 case ISD::SETO: CondCode = NP; break; // True if PF = 0
952 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
953 case ISD::SETUGE: // PF = 1 | CF = 0
954 case ISD::SETUNE: // PF = 1 | ZF = 0
955 case ISD::SETOEQ: // PF = 0 & ZF = 1
956 case ISD::SETOLT: // PF = 0 & CF = 1
957 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
958 // We cannot emit this comparison as a single cmov.
959 break;
960 }
961 }
962 }
963
964 unsigned Opc = 0;
965 if (CondCode != NOT_SET) {
966 switch (SVT) {
967 default: assert(0 && "Cannot select this type!");
968 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
969 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
970 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000971 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000972 }
973 }
974
975 // Finally, if we weren't able to fold this, just emit the condition and test
976 // it.
977 if (CondCode == NOT_SET || Opc == 0) {
978 // Get the condition into the zero flag.
979 unsigned CondReg = SelectExpr(Cond);
980 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
981
982 switch (SVT) {
983 default: assert(0 && "Cannot select this type!");
984 case MVT::i16: Opc = X86::CMOVE16rr; break;
985 case MVT::i32: Opc = X86::CMOVE32rr; break;
986 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000987 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000988 }
989 } else {
990 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000991 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000992 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000993 }
994 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
995}
996
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000997void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +0000998 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000999 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1000 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001001 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001002 switch (RHS.getValueType()) {
1003 default: break;
1004 case MVT::i1:
1005 case MVT::i8: Opc = X86::CMP8mi; break;
1006 case MVT::i16: Opc = X86::CMP16mi; break;
1007 case MVT::i32: Opc = X86::CMP32mi; break;
1008 }
1009 if (Opc) {
1010 X86AddressMode AM;
1011 EmitFoldedLoad(LHS, AM);
1012 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1013 return;
1014 }
1015 }
1016
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001017 switch (RHS.getValueType()) {
1018 default: break;
1019 case MVT::i1:
1020 case MVT::i8: Opc = X86::CMP8ri; break;
1021 case MVT::i16: Opc = X86::CMP16ri; break;
1022 case MVT::i32: Opc = X86::CMP32ri; break;
1023 }
1024 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001025 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001026 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1027 return;
1028 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001029 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1030 if (CN->isExactlyValue(+0.0) ||
1031 CN->isExactlyValue(-0.0)) {
1032 unsigned Reg = SelectExpr(LHS);
1033 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1034 BuildMI(BB, X86::FNSTSW8r, 0);
1035 BuildMI(BB, X86::SAHF, 1);
1036 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001037 }
1038
Chris Lattneref6806c2005-01-12 02:02:48 +00001039 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001040 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001041 switch (RHS.getValueType()) {
1042 default: break;
1043 case MVT::i1:
1044 case MVT::i8: Opc = X86::CMP8mr; break;
1045 case MVT::i16: Opc = X86::CMP16mr; break;
1046 case MVT::i32: Opc = X86::CMP32mr; break;
1047 }
1048 if (Opc) {
1049 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001050 EmitFoldedLoad(LHS, AM);
1051 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001052 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1053 return;
1054 }
1055 }
1056
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001057 switch (LHS.getValueType()) {
1058 default: assert(0 && "Cannot compare this value!");
1059 case MVT::i1:
1060 case MVT::i8: Opc = X86::CMP8rr; break;
1061 case MVT::i16: Opc = X86::CMP16rr; break;
1062 case MVT::i32: Opc = X86::CMP32rr; break;
1063 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001064 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001065 }
Chris Lattner11333092005-01-11 03:11:44 +00001066 unsigned Tmp1, Tmp2;
1067 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1068 Tmp1 = SelectExpr(LHS);
1069 Tmp2 = SelectExpr(RHS);
1070 } else {
1071 Tmp2 = SelectExpr(RHS);
1072 Tmp1 = SelectExpr(LHS);
1073 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001074 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1075}
1076
Chris Lattner4ff348b2005-01-17 06:26:58 +00001077/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1078/// The DAG cannot have cycles in it, by definition, so the visited set is not
1079/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1080/// reuse, so it prevents exponential cases.
1081///
1082static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1083 std::set<SDNode*> &Visited) {
1084 if (N == Op) return true; // Found it.
1085 SDNode *Node = N.Val;
1086 if (Node->getNumOperands() == 0) return false; // Leaf?
1087 if (!Visited.insert(Node).second) return false; // Already visited?
1088
1089 // Recurse for the first N-1 operands.
1090 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1091 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1092 return true;
1093
1094 // Tail recurse for the last operand.
1095 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1096}
1097
Chris Lattnera5ade062005-01-11 21:19:59 +00001098/// isFoldableLoad - Return true if this is a load instruction that can safely
1099/// be folded into an operation that uses it.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001100bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001101 if (Op.getOpcode() != ISD::LOAD ||
1102 // FIXME: currently can't fold constant pool indexes.
1103 isa<ConstantPoolSDNode>(Op.getOperand(1)))
1104 return false;
1105
1106 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001107 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1108 if (ExprMap.count(Op.getValue(1))) return false;
1109 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
1110 assert(!LoweredTokens.count(Op.getValue(1)) &&
1111 "Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001112
Chris Lattner4ff348b2005-01-17 06:26:58 +00001113 // If there is not just one use of its value, we cannot fold.
1114 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1115
1116 // Finally, we cannot fold the load into the operation if this would induce a
1117 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1118 // operand of the operation we are folding the load into) can possible use the
1119 // chain node defined by the load.
1120 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1121 std::set<SDNode*> Visited;
1122 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1123 return false;
1124 }
1125 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001126}
1127
Chris Lattner4ff348b2005-01-17 06:26:58 +00001128
Chris Lattnera5ade062005-01-11 21:19:59 +00001129/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1130/// and compute the address being loaded into AM.
1131void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1132 SDOperand Chain = Op.getOperand(0);
1133 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001134
Chris Lattnera5ade062005-01-11 21:19:59 +00001135 if (getRegPressure(Chain) > getRegPressure(Address)) {
1136 Select(Chain);
1137 SelectAddress(Address, AM);
1138 } else {
1139 SelectAddress(Address, AM);
1140 Select(Chain);
1141 }
1142
1143 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001144 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1145 "Load emitted more than once?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001146 ExprMap[SDOperand(Op.Val, 1)] = 1;
Chris Lattner636e79a2005-01-13 05:53:16 +00001147 if (!LoweredTokens.insert(Op.getValue(1)).second)
1148 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001149}
1150
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001151unsigned ISel::SelectExpr(SDOperand N) {
1152 unsigned Result;
1153 unsigned Tmp1, Tmp2, Tmp3;
1154 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001155 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001156 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001157
Chris Lattner7f2afac2005-01-14 22:37:41 +00001158 if (Node->getOpcode() == ISD::CopyFromReg) {
1159 // FIXME: Handle copy from physregs!
1160
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001161 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001162 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001163 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001164
1165 unsigned &Reg = ExprMap[N];
1166 if (Reg) return Reg;
1167
1168 if (N.getOpcode() != ISD::CALL)
1169 Reg = Result = (N.getValueType() != MVT::Other) ?
1170 MakeReg(N.getValueType()) : 1;
1171 else {
1172 // If this is a call instruction, make sure to prepare ALL of the result
1173 // values as well as the chain.
1174 if (Node->getNumValues() == 1)
1175 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001176 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001177 Result = MakeReg(Node->getValueType(0));
1178 ExprMap[N.getValue(0)] = Result;
1179 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1180 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1181 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001182 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001183 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001184
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001185 switch (N.getOpcode()) {
1186 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001187 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001188 assert(0 && "Node not handled!\n");
1189 case ISD::FrameIndex:
1190 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1191 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1192 return Result;
1193 case ISD::ConstantPool:
1194 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1195 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1196 return Result;
1197 case ISD::ConstantFP:
1198 ContainsFPCode = true;
1199 Tmp1 = Result; // Intermediate Register
1200 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1201 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1202 Tmp1 = MakeReg(MVT::f64);
1203
1204 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1205 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1206 BuildMI(BB, X86::FLD0, 0, Tmp1);
1207 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1208 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1209 BuildMI(BB, X86::FLD1, 0, Tmp1);
1210 else
1211 assert(0 && "Unexpected constant!");
1212 if (Tmp1 != Result)
1213 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1214 return Result;
1215 case ISD::Constant:
1216 switch (N.getValueType()) {
1217 default: assert(0 && "Cannot use constants of this type!");
1218 case MVT::i1:
1219 case MVT::i8: Opc = X86::MOV8ri; break;
1220 case MVT::i16: Opc = X86::MOV16ri; break;
1221 case MVT::i32: Opc = X86::MOV32ri; break;
1222 }
1223 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1224 return Result;
1225 case ISD::GlobalAddress: {
1226 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1227 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1228 return Result;
1229 }
1230 case ISD::ExternalSymbol: {
1231 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1232 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1233 return Result;
1234 }
1235 case ISD::FP_EXTEND:
1236 Tmp1 = SelectExpr(N.getOperand(0));
1237 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001238 return Result;
1239 case ISD::ZERO_EXTEND: {
1240 int DestIs16 = N.getValueType() == MVT::i16;
1241 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001242
1243 // FIXME: This hack is here for zero extension casts from bool to i8. This
1244 // would not be needed if bools were promoted by Legalize.
1245 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001246 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001247 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1248 return Result;
1249 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001250
Chris Lattner4ff348b2005-01-17 06:26:58 +00001251 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001252 static const unsigned Opc[3] = {
1253 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1254 };
1255
1256 X86AddressMode AM;
1257 EmitFoldedLoad(N.getOperand(0), AM);
1258 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1259
1260 return Result;
1261 }
1262
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001263 static const unsigned Opc[3] = {
1264 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1265 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001266 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001267 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1268 return Result;
1269 }
1270 case ISD::SIGN_EXTEND: {
1271 int DestIs16 = N.getValueType() == MVT::i16;
1272 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1273
Chris Lattner590d8002005-01-09 18:52:44 +00001274 // FIXME: Legalize should promote bools to i8!
1275 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1276 "Sign extend from bool not implemented!");
1277
Chris Lattner4ff348b2005-01-17 06:26:58 +00001278 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001279 static const unsigned Opc[3] = {
1280 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1281 };
1282
1283 X86AddressMode AM;
1284 EmitFoldedLoad(N.getOperand(0), AM);
1285 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1286 return Result;
1287 }
1288
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001289 static const unsigned Opc[3] = {
1290 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1291 };
1292 Tmp1 = SelectExpr(N.getOperand(0));
1293 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1294 return Result;
1295 }
1296 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001297 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001298 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001299 switch (N.getValueType()) {
1300 default: assert(0 && "Unknown truncate!");
1301 case MVT::i1:
1302 case MVT::i8: Opc = X86::MOV8rm; break;
1303 case MVT::i16: Opc = X86::MOV16rm; break;
1304 }
1305 X86AddressMode AM;
1306 EmitFoldedLoad(N.getOperand(0), AM);
1307 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1308 return Result;
1309 }
1310
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001311 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1312 // a move out of AX or AL.
1313 switch (N.getOperand(0).getValueType()) {
1314 default: assert(0 && "Unknown truncate!");
1315 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1316 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1317 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1318 }
1319 Tmp1 = SelectExpr(N.getOperand(0));
1320 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1321
1322 switch (N.getValueType()) {
1323 default: assert(0 && "Unknown truncate!");
1324 case MVT::i1:
1325 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1326 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1327 }
1328 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1329 return Result;
1330
1331 case ISD::FP_ROUND:
1332 // Truncate from double to float by storing to memory as float,
1333 // then reading it back into a register.
1334
1335 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001336 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001337 Tmp1 = TLI.getTargetData().getFloatAlignment();
1338 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1339
1340 // Codegen the input.
1341 Tmp1 = SelectExpr(N.getOperand(0));
1342
1343 // Emit the store, then the reload.
1344 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1345 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001346 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001347
1348 case ISD::SINT_TO_FP:
1349 case ISD::UINT_TO_FP: {
1350 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001351 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001352
1353 // Promote the integer to a type supported by FLD. We do this because there
1354 // are no unsigned FLD instructions, so we must promote an unsigned value to
1355 // a larger signed value, then use FLD on the larger value.
1356 //
1357 MVT::ValueType PromoteType = MVT::Other;
1358 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1359 unsigned PromoteOpcode = 0;
1360 unsigned RealDestReg = Result;
1361 switch (SrcTy) {
1362 case MVT::i1:
1363 case MVT::i8:
1364 // We don't have the facilities for directly loading byte sized data from
1365 // memory (even signed). Promote it to 16 bits.
1366 PromoteType = MVT::i16;
1367 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1368 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1369 break;
1370 case MVT::i16:
1371 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1372 PromoteType = MVT::i32;
1373 PromoteOpcode = X86::MOVZX32rr16;
1374 }
1375 break;
1376 default:
1377 // Don't fild into the real destination.
1378 if (Node->getOpcode() == ISD::UINT_TO_FP)
1379 Result = MakeReg(Node->getValueType(0));
1380 break;
1381 }
1382
1383 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1384
1385 if (PromoteType != MVT::Other) {
1386 Tmp2 = MakeReg(PromoteType);
1387 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1388 SrcTy = PromoteType;
1389 Tmp1 = Tmp2;
1390 }
1391
1392 // Spill the integer to memory and reload it from there.
1393 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1394 MachineFunction *F = BB->getParent();
1395 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1396
1397 switch (SrcTy) {
1398 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001399 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001400 // FIXME: this won't work for cast [u]long to FP
1401 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1402 FrameIdx).addReg(Tmp1);
1403 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1404 FrameIdx, 4).addReg(Tmp1+1);
1405 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1406 break;
1407 case MVT::i32:
1408 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1409 FrameIdx).addReg(Tmp1);
1410 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1411 break;
1412 case MVT::i16:
1413 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1414 FrameIdx).addReg(Tmp1);
1415 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1416 break;
1417 default: break; // No promotion required.
1418 }
1419
Chris Lattner085c9952005-01-12 04:00:00 +00001420 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001421 // If this is a cast from uint -> double, we need to be careful when if
1422 // the "sign" bit is set. If so, we don't want to make a negative number,
1423 // we want to make a positive number. Emit code to add an offset if the
1424 // sign bit is set.
1425
1426 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1427 unsigned IsNeg = MakeReg(MVT::i32);
1428 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1429
1430 // Create a CP value that has the offset in one word and 0 in the other.
1431 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1432 0x4f80000000000000ULL);
1433 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1434 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1435 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1436
1437 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1438 // We need special handling for unsigned 64-bit integer sources. If the
1439 // input number has the "sign bit" set, then we loaded it incorrectly as a
1440 // negative 64-bit number. In this case, add an offset value.
1441
1442 // Emit a test instruction to see if the dynamic input value was signed.
1443 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1444
1445 // If the sign bit is set, get a pointer to an offset, otherwise get a
1446 // pointer to a zero.
1447 MachineConstantPool *CP = F->getConstantPool();
1448 unsigned Zero = MakeReg(MVT::i32);
1449 Constant *Null = Constant::getNullValue(Type::UIntTy);
1450 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1451 CP->getConstantPoolIndex(Null));
1452 unsigned Offset = MakeReg(MVT::i32);
1453 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1454
1455 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1456 CP->getConstantPoolIndex(OffsetCst));
1457 unsigned Addr = MakeReg(MVT::i32);
1458 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1459
1460 // Load the constant for an add. FIXME: this could make an 'fadd' that
1461 // reads directly from memory, but we don't support these yet.
1462 unsigned ConstReg = MakeReg(MVT::f64);
1463 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1464
1465 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1466 }
1467 return RealDestReg;
1468 }
1469 case ISD::FP_TO_SINT:
1470 case ISD::FP_TO_UINT: {
1471 // FIXME: Most of this grunt work should be done by legalize!
1472 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1473
1474 // Change the floating point control register to use "round towards zero"
1475 // mode when truncating to an integer value.
1476 //
1477 MachineFunction *F = BB->getParent();
1478 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1479 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1480
1481 // Load the old value of the high byte of the control word...
1482 unsigned HighPartOfCW = MakeReg(MVT::i8);
1483 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1484 CWFrameIdx, 1);
1485
1486 // Set the high part to be round to zero...
1487 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1488 CWFrameIdx, 1).addImm(12);
1489
1490 // Reload the modified control word now...
1491 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1492
1493 // Restore the memory image of control word to original value
1494 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1495 CWFrameIdx, 1).addReg(HighPartOfCW);
1496
1497 // We don't have the facilities for directly storing byte sized data to
1498 // memory. Promote it to 16 bits. We also must promote unsigned values to
1499 // larger classes because we only have signed FP stores.
1500 MVT::ValueType StoreClass = Node->getValueType(0);
1501 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1502 switch (StoreClass) {
1503 case MVT::i8: StoreClass = MVT::i16; break;
1504 case MVT::i16: StoreClass = MVT::i32; break;
1505 case MVT::i32: StoreClass = MVT::i64; break;
1506 // The following treatment of cLong may not be perfectly right,
1507 // but it survives chains of casts of the form
1508 // double->ulong->double.
1509 case MVT::i64: StoreClass = MVT::i64; break;
1510 default: assert(0 && "Unknown store class!");
1511 }
1512
1513 // Spill the integer to memory and reload it from there.
1514 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1515 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1516
1517 switch (StoreClass) {
1518 default: assert(0 && "Unknown store class!");
1519 case MVT::i16:
1520 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1521 break;
1522 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001523 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001524 break;
1525 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001526 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001527 break;
1528 }
1529
1530 switch (Node->getValueType(0)) {
1531 default:
1532 assert(0 && "Unknown integer type!");
1533 case MVT::i64:
1534 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001535 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001536 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1537 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1538 case MVT::i32:
1539 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1540 break;
1541 case MVT::i16:
1542 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1543 break;
1544 case MVT::i8:
1545 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1546 break;
1547 }
1548
1549 // Reload the original control word now.
1550 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1551 return Result;
1552 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001553 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001554 Op0 = N.getOperand(0);
1555 Op1 = N.getOperand(1);
1556
Chris Lattner4ff348b2005-01-17 06:26:58 +00001557 if (isFoldableLoad(Op0, Op1)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001558 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001559 goto FoldAdd;
1560 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001561
Chris Lattner4ff348b2005-01-17 06:26:58 +00001562 if (isFoldableLoad(Op1, Op0)) {
1563 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001564 switch (N.getValueType()) {
1565 default: assert(0 && "Cannot add this type!");
1566 case MVT::i1:
1567 case MVT::i8: Opc = X86::ADD8rm; break;
1568 case MVT::i16: Opc = X86::ADD16rm; break;
1569 case MVT::i32: Opc = X86::ADD32rm; break;
1570 case MVT::f32: Opc = X86::FADD32m; break;
1571 case MVT::f64: Opc = X86::FADD64m; break;
1572 }
1573 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001574 EmitFoldedLoad(Op1, AM);
1575 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001576 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1577 return Result;
1578 }
1579
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001580 // See if we can codegen this as an LEA to fold operations together.
1581 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001582 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001583 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001584 MatchAddress(N, AM);
1585 ExprMap[N] = Result;
1586
1587 // If this is not just an add, emit the LEA. For a simple add (like
1588 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1589 // leave this as LEA, then peephole it to 'ADD' after two address elim
1590 // happens.
1591 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1592 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1593 X86AddressMode XAM = SelectAddrExprs(AM);
1594 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1595 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001596 }
1597 }
Chris Lattner11333092005-01-11 03:11:44 +00001598
Chris Lattnera5ade062005-01-11 21:19:59 +00001599 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001600 Opc = 0;
1601 if (CN->getValue() == 1) { // add X, 1 -> inc X
1602 switch (N.getValueType()) {
1603 default: assert(0 && "Cannot integer add this type!");
1604 case MVT::i8: Opc = X86::INC8r; break;
1605 case MVT::i16: Opc = X86::INC16r; break;
1606 case MVT::i32: Opc = X86::INC32r; break;
1607 }
1608 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1609 switch (N.getValueType()) {
1610 default: assert(0 && "Cannot integer add this type!");
1611 case MVT::i8: Opc = X86::DEC8r; break;
1612 case MVT::i16: Opc = X86::DEC16r; break;
1613 case MVT::i32: Opc = X86::DEC32r; break;
1614 }
1615 }
1616
1617 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001618 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001619 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1620 return Result;
1621 }
1622
1623 switch (N.getValueType()) {
1624 default: assert(0 && "Cannot add this type!");
1625 case MVT::i8: Opc = X86::ADD8ri; break;
1626 case MVT::i16: Opc = X86::ADD16ri; break;
1627 case MVT::i32: Opc = X86::ADD32ri; break;
1628 }
1629 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001630 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001631 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1632 return Result;
1633 }
1634 }
1635
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001636 switch (N.getValueType()) {
1637 default: assert(0 && "Cannot add this type!");
1638 case MVT::i8: Opc = X86::ADD8rr; break;
1639 case MVT::i16: Opc = X86::ADD16rr; break;
1640 case MVT::i32: Opc = X86::ADD32rr; break;
1641 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001642 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001643 }
Chris Lattner11333092005-01-11 03:11:44 +00001644
Chris Lattnera5ade062005-01-11 21:19:59 +00001645 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1646 Tmp1 = SelectExpr(Op0);
1647 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001648 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001649 Tmp2 = SelectExpr(Op1);
1650 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001651 }
1652
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001653 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1654 return Result;
1655 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001656 case ISD::MUL:
1657 case ISD::AND:
1658 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001659 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001660 static const unsigned SUBTab[] = {
1661 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1662 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1663 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1664 };
1665 static const unsigned MULTab[] = {
1666 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1667 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1668 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1669 };
1670 static const unsigned ANDTab[] = {
1671 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1672 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1673 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1674 };
1675 static const unsigned ORTab[] = {
1676 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1677 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1678 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1679 };
1680 static const unsigned XORTab[] = {
1681 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1682 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1683 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1684 };
1685
1686 Op0 = Node->getOperand(0);
1687 Op1 = Node->getOperand(1);
1688
1689 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001690 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1691 if (CN->isNullValue()) { // 0 - N -> neg N
1692 switch (N.getValueType()) {
1693 default: assert(0 && "Cannot sub this type!");
1694 case MVT::i1:
1695 case MVT::i8: Opc = X86::NEG8r; break;
1696 case MVT::i16: Opc = X86::NEG16r; break;
1697 case MVT::i32: Opc = X86::NEG32r; break;
1698 }
1699 Tmp1 = SelectExpr(N.getOperand(1));
1700 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1701 return Result;
1702 }
1703
Chris Lattnera5ade062005-01-11 21:19:59 +00001704 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1705 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001706 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001707 switch (N.getValueType()) {
1708 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001709 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001710 case MVT::i8: Opc = X86::NOT8r; break;
1711 case MVT::i16: Opc = X86::NOT16r; break;
1712 case MVT::i32: Opc = X86::NOT32r; break;
1713 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001714 if (Opc) {
1715 Tmp1 = SelectExpr(Op0);
1716 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1717 return Result;
1718 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001719 }
1720
Chris Lattner2a4e5082005-01-17 06:48:02 +00001721 // Fold common multiplies into LEA instructions.
1722 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1723 switch ((int)CN->getValue()) {
1724 default: break;
1725 case 3:
1726 case 5:
1727 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001728 // Remove N from exprmap so SelectAddress doesn't get confused.
1729 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001730 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001731 SelectAddress(N, AM);
1732 // Restore it to the map.
1733 ExprMap[N] = Result;
1734 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1735 return Result;
1736 }
1737 }
1738
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001739 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001740 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001741 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001742 case MVT::i8: Opc = 0; break;
1743 case MVT::i16: Opc = 1; break;
1744 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001745 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001746 switch (Node->getOpcode()) {
1747 default: assert(0 && "Unreachable!");
1748 case ISD::SUB: Opc = SUBTab[Opc]; break;
1749 case ISD::MUL: Opc = MULTab[Opc]; break;
1750 case ISD::AND: Opc = ANDTab[Opc]; break;
1751 case ISD::OR: Opc = ORTab[Opc]; break;
1752 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001753 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001754 if (Opc) { // Can't fold MUL:i8 R, imm
1755 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001756 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1757 return Result;
1758 }
1759 }
Chris Lattner11333092005-01-11 03:11:44 +00001760
Chris Lattner4ff348b2005-01-17 06:26:58 +00001761 if (isFoldableLoad(Op0, Op1))
Chris Lattnera5ade062005-01-11 21:19:59 +00001762 if (Node->getOpcode() != ISD::SUB) {
1763 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001764 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001765 } else {
1766 // Emit 'reverse' subract, with a memory operand.
1767 switch (N.getValueType()) {
1768 default: Opc = 0; break;
1769 case MVT::f32: Opc = X86::FSUBR32m; break;
1770 case MVT::f64: Opc = X86::FSUBR64m; break;
1771 }
1772 if (Opc) {
1773 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001774 EmitFoldedLoad(Op0, AM);
1775 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001776 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1777 return Result;
1778 }
1779 }
1780
Chris Lattner4ff348b2005-01-17 06:26:58 +00001781 if (isFoldableLoad(Op1, Op0)) {
1782 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001783 switch (N.getValueType()) {
1784 default: assert(0 && "Cannot operate on this type!");
1785 case MVT::i1:
1786 case MVT::i8: Opc = 5; break;
1787 case MVT::i16: Opc = 6; break;
1788 case MVT::i32: Opc = 7; break;
1789 case MVT::f32: Opc = 8; break;
1790 case MVT::f64: Opc = 9; break;
1791 }
1792 switch (Node->getOpcode()) {
1793 default: assert(0 && "Unreachable!");
1794 case ISD::SUB: Opc = SUBTab[Opc]; break;
1795 case ISD::MUL: Opc = MULTab[Opc]; break;
1796 case ISD::AND: Opc = ANDTab[Opc]; break;
1797 case ISD::OR: Opc = ORTab[Opc]; break;
1798 case ISD::XOR: Opc = XORTab[Opc]; break;
1799 }
1800
1801 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001802 EmitFoldedLoad(Op1, AM);
1803 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001804 if (Opc) {
1805 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1806 } else {
1807 assert(Node->getOpcode() == ISD::MUL &&
1808 N.getValueType() == MVT::i8 && "Unexpected situation!");
1809 // Must use the MUL instruction, which forces use of AL.
1810 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1811 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1812 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1813 }
1814 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001815 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001816
1817 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1818 Tmp1 = SelectExpr(Op0);
1819 Tmp2 = SelectExpr(Op1);
1820 } else {
1821 Tmp2 = SelectExpr(Op1);
1822 Tmp1 = SelectExpr(Op0);
1823 }
1824
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001825 switch (N.getValueType()) {
1826 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001827 case MVT::i1:
1828 case MVT::i8: Opc = 10; break;
1829 case MVT::i16: Opc = 11; break;
1830 case MVT::i32: Opc = 12; break;
1831 case MVT::f32: Opc = 13; break;
1832 case MVT::f64: Opc = 14; break;
1833 }
1834 switch (Node->getOpcode()) {
1835 default: assert(0 && "Unreachable!");
1836 case ISD::SUB: Opc = SUBTab[Opc]; break;
1837 case ISD::MUL: Opc = MULTab[Opc]; break;
1838 case ISD::AND: Opc = ANDTab[Opc]; break;
1839 case ISD::OR: Opc = ORTab[Opc]; break;
1840 case ISD::XOR: Opc = XORTab[Opc]; break;
1841 }
1842 if (Opc) {
1843 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1844 } else {
1845 assert(Node->getOpcode() == ISD::MUL &&
1846 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001847 // Must use the MUL instruction, which forces use of AL.
1848 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1849 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1850 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001851 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001852 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001853 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001854 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001855 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1856 Tmp2 = SelectExpr(N.getOperand(1));
1857 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001858 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001859 Tmp3 = SelectExpr(N.getOperand(2));
1860 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001861 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00001862 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
1863 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001864
1865 case ISD::SDIV:
1866 case ISD::UDIV:
1867 case ISD::SREM:
1868 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001869 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
1870 "We don't support this operator!");
1871
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001872 if (N.getOpcode() == ISD::SDIV)
1873 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1874 // FIXME: These special cases should be handled by the lowering impl!
1875 unsigned RHS = CN->getValue();
1876 bool isNeg = false;
1877 if ((int)RHS < 0) {
1878 isNeg = true;
1879 RHS = -RHS;
1880 }
1881 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1882 unsigned Log = log2(RHS);
1883 unsigned TmpReg = MakeReg(N.getValueType());
1884 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1885 switch (N.getValueType()) {
1886 default: assert("Unknown type to signed divide!");
1887 case MVT::i8:
1888 SAROpc = X86::SAR8ri;
1889 SHROpc = X86::SHR8ri;
1890 ADDOpc = X86::ADD8rr;
1891 NEGOpc = X86::NEG8r;
1892 break;
1893 case MVT::i16:
1894 SAROpc = X86::SAR16ri;
1895 SHROpc = X86::SHR16ri;
1896 ADDOpc = X86::ADD16rr;
1897 NEGOpc = X86::NEG16r;
1898 break;
1899 case MVT::i32:
1900 SAROpc = X86::SAR32ri;
1901 SHROpc = X86::SHR32ri;
1902 ADDOpc = X86::ADD32rr;
1903 NEGOpc = X86::NEG32r;
1904 break;
1905 }
Chris Lattner11333092005-01-11 03:11:44 +00001906 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001907 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1908 unsigned TmpReg2 = MakeReg(N.getValueType());
1909 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1910 unsigned TmpReg3 = MakeReg(N.getValueType());
1911 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1912
1913 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1914 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1915 if (isNeg)
1916 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1917 return Result;
1918 }
1919 }
1920
Chris Lattner11333092005-01-11 03:11:44 +00001921 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1922 Tmp1 = SelectExpr(N.getOperand(0));
1923 Tmp2 = SelectExpr(N.getOperand(1));
1924 } else {
1925 Tmp2 = SelectExpr(N.getOperand(1));
1926 Tmp1 = SelectExpr(N.getOperand(0));
1927 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001928
1929 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1930 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1931 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1932 switch (N.getValueType()) {
1933 default: assert(0 && "Cannot sdiv this type!");
1934 case MVT::i8:
1935 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1936 LoReg = X86::AL;
1937 HiReg = X86::AH;
1938 MovOpcode = X86::MOV8rr;
1939 ClrOpcode = X86::MOV8ri;
1940 SExtOpcode = X86::CBW;
1941 break;
1942 case MVT::i16:
1943 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1944 LoReg = X86::AX;
1945 HiReg = X86::DX;
1946 MovOpcode = X86::MOV16rr;
1947 ClrOpcode = X86::MOV16ri;
1948 SExtOpcode = X86::CWD;
1949 break;
1950 case MVT::i32:
1951 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001952 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001953 HiReg = X86::EDX;
1954 MovOpcode = X86::MOV32rr;
1955 ClrOpcode = X86::MOV32ri;
1956 SExtOpcode = X86::CDQ;
1957 break;
1958 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1959 case MVT::f32:
1960 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001961 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001962 return Result;
1963 }
1964
1965 // Set up the low part.
1966 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1967
1968 if (isSigned) {
1969 // Sign extend the low part into the high part.
1970 BuildMI(BB, SExtOpcode, 0);
1971 } else {
1972 // Zero out the high part, effectively zero extending the input.
1973 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1974 }
1975
1976 // Emit the DIV/IDIV instruction.
1977 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1978
1979 // Get the result of the divide or rem.
1980 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1981 return Result;
1982 }
1983
1984 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001985 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001986 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1987 switch (N.getValueType()) {
1988 default: assert(0 && "Cannot shift this type!");
1989 case MVT::i8: Opc = X86::ADD8rr; break;
1990 case MVT::i16: Opc = X86::ADD16rr; break;
1991 case MVT::i32: Opc = X86::ADD32rr; break;
1992 }
1993 Tmp1 = SelectExpr(N.getOperand(0));
1994 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1995 return Result;
1996 }
1997
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001998 switch (N.getValueType()) {
1999 default: assert(0 && "Cannot shift this type!");
2000 case MVT::i8: Opc = X86::SHL8ri; break;
2001 case MVT::i16: Opc = X86::SHL16ri; break;
2002 case MVT::i32: Opc = X86::SHL32ri; break;
2003 }
Chris Lattner11333092005-01-11 03:11:44 +00002004 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002005 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2006 return Result;
2007 }
Chris Lattner11333092005-01-11 03:11:44 +00002008
2009 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2010 Tmp1 = SelectExpr(N.getOperand(0));
2011 Tmp2 = SelectExpr(N.getOperand(1));
2012 } else {
2013 Tmp2 = SelectExpr(N.getOperand(1));
2014 Tmp1 = SelectExpr(N.getOperand(0));
2015 }
2016
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002017 switch (N.getValueType()) {
2018 default: assert(0 && "Cannot shift this type!");
2019 case MVT::i8 : Opc = X86::SHL8rCL; break;
2020 case MVT::i16: Opc = X86::SHL16rCL; break;
2021 case MVT::i32: Opc = X86::SHL32rCL; break;
2022 }
2023 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2024 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2025 return Result;
2026 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002027 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2028 switch (N.getValueType()) {
2029 default: assert(0 && "Cannot shift this type!");
2030 case MVT::i8: Opc = X86::SHR8ri; break;
2031 case MVT::i16: Opc = X86::SHR16ri; break;
2032 case MVT::i32: Opc = X86::SHR32ri; break;
2033 }
Chris Lattner11333092005-01-11 03:11:44 +00002034 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002035 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2036 return Result;
2037 }
Chris Lattner11333092005-01-11 03:11:44 +00002038
2039 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2040 Tmp1 = SelectExpr(N.getOperand(0));
2041 Tmp2 = SelectExpr(N.getOperand(1));
2042 } else {
2043 Tmp2 = SelectExpr(N.getOperand(1));
2044 Tmp1 = SelectExpr(N.getOperand(0));
2045 }
2046
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002047 switch (N.getValueType()) {
2048 default: assert(0 && "Cannot shift this type!");
2049 case MVT::i8 : Opc = X86::SHR8rCL; break;
2050 case MVT::i16: Opc = X86::SHR16rCL; break;
2051 case MVT::i32: Opc = X86::SHR32rCL; break;
2052 }
2053 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2054 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2055 return Result;
2056 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002057 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2058 switch (N.getValueType()) {
2059 default: assert(0 && "Cannot shift this type!");
2060 case MVT::i8: Opc = X86::SAR8ri; break;
2061 case MVT::i16: Opc = X86::SAR16ri; break;
2062 case MVT::i32: Opc = X86::SAR32ri; break;
2063 }
Chris Lattner11333092005-01-11 03:11:44 +00002064 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002065 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2066 return Result;
2067 }
Chris Lattner11333092005-01-11 03:11:44 +00002068
2069 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2070 Tmp1 = SelectExpr(N.getOperand(0));
2071 Tmp2 = SelectExpr(N.getOperand(1));
2072 } else {
2073 Tmp2 = SelectExpr(N.getOperand(1));
2074 Tmp1 = SelectExpr(N.getOperand(0));
2075 }
2076
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002077 switch (N.getValueType()) {
2078 default: assert(0 && "Cannot shift this type!");
2079 case MVT::i8 : Opc = X86::SAR8rCL; break;
2080 case MVT::i16: Opc = X86::SAR16rCL; break;
2081 case MVT::i32: Opc = X86::SAR32rCL; break;
2082 }
2083 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2084 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2085 return Result;
2086
2087 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002088 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002089 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2090 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2091 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002092 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002093 // Make sure we generate both values.
2094 if (Result != 1)
2095 ExprMap[N.getValue(1)] = 1; // Generate the token
2096 else
2097 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2098
Chris Lattner5188ad72005-01-08 19:28:19 +00002099 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002100 default: assert(0 && "Cannot load this type!");
2101 case MVT::i1:
2102 case MVT::i8: Opc = X86::MOV8rm; break;
2103 case MVT::i16: Opc = X86::MOV16rm; break;
2104 case MVT::i32: Opc = X86::MOV32rm; break;
2105 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
2106 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2107 }
Chris Lattner11333092005-01-11 03:11:44 +00002108
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002109 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002110 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002111 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2112 } else {
2113 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002114
2115 SDOperand Chain = N.getOperand(0);
2116 SDOperand Address = N.getOperand(1);
2117 if (getRegPressure(Chain) > getRegPressure(Address)) {
2118 Select(Chain);
2119 SelectAddress(Address, AM);
2120 } else {
2121 SelectAddress(Address, AM);
2122 Select(Chain);
2123 }
2124
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002125 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2126 }
2127 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002128
2129 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2130 case ISD::ZEXTLOAD: {
2131 // Make sure we generate both values.
2132 if (Result != 1)
2133 ExprMap[N.getValue(1)] = 1; // Generate the token
2134 else
2135 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2136
Chris Lattnerda2ce112005-01-16 07:34:08 +00002137 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2138 if (Node->getValueType(0) == MVT::f64) {
2139 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2140 "Bad EXTLOAD!");
2141 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2142 CP->getIndex());
2143 return Result;
2144 }
2145
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002146 X86AddressMode AM;
2147 if (getRegPressure(Node->getOperand(0)) >
2148 getRegPressure(Node->getOperand(1))) {
2149 Select(Node->getOperand(0)); // chain
2150 SelectAddress(Node->getOperand(1), AM);
2151 } else {
2152 SelectAddress(Node->getOperand(1), AM);
2153 Select(Node->getOperand(0)); // chain
2154 }
2155
2156 switch (Node->getValueType(0)) {
2157 default: assert(0 && "Unknown type to sign extend to.");
2158 case MVT::f64:
2159 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2160 "Bad EXTLOAD!");
2161 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2162 break;
2163 case MVT::i32:
2164 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2165 default:
2166 assert(0 && "Bad zero extend!");
2167 case MVT::i1:
2168 case MVT::i8:
2169 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2170 break;
2171 case MVT::i16:
2172 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2173 break;
2174 }
2175 break;
2176 case MVT::i16:
2177 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2178 "Bad zero extend!");
2179 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2180 break;
2181 case MVT::i8:
2182 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2183 "Bad zero extend!");
2184 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2185 break;
2186 }
2187 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002188 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002189 case ISD::SEXTLOAD: {
2190 // Make sure we generate both values.
2191 if (Result != 1)
2192 ExprMap[N.getValue(1)] = 1; // Generate the token
2193 else
2194 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2195
2196 X86AddressMode AM;
2197 if (getRegPressure(Node->getOperand(0)) >
2198 getRegPressure(Node->getOperand(1))) {
2199 Select(Node->getOperand(0)); // chain
2200 SelectAddress(Node->getOperand(1), AM);
2201 } else {
2202 SelectAddress(Node->getOperand(1), AM);
2203 Select(Node->getOperand(0)); // chain
2204 }
2205
2206 switch (Node->getValueType(0)) {
2207 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2208 default: assert(0 && "Unknown type to sign extend to.");
2209 case MVT::i32:
2210 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2211 default:
2212 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2213 case MVT::i8:
2214 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2215 break;
2216 case MVT::i16:
2217 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2218 break;
2219 }
2220 break;
2221 case MVT::i16:
2222 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2223 "Cannot sign extend from bool!");
2224 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2225 break;
2226 }
2227 return Result;
2228 }
2229
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002230 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002231 // Generate both result values.
2232 if (Result != 1)
2233 ExprMap[N.getValue(1)] = 1; // Generate the token
2234 else
2235 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2236
2237 // FIXME: We are currently ignoring the requested alignment for handling
2238 // greater than the stack alignment. This will need to be revisited at some
2239 // point. Align = N.getOperand(2);
2240
2241 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2242 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2243 std::cerr << "Cannot allocate stack object with greater alignment than"
2244 << " the stack alignment yet!";
2245 abort();
2246 }
2247
2248 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002249 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002250 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2251 .addImm(CN->getValue());
2252 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002253 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2254 Select(N.getOperand(0));
2255 Tmp1 = SelectExpr(N.getOperand(1));
2256 } else {
2257 Tmp1 = SelectExpr(N.getOperand(1));
2258 Select(N.getOperand(0));
2259 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002260
2261 // Subtract size from stack pointer, thereby allocating some space.
2262 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2263 }
2264
2265 // Put a pointer to the space into the result register, by copying the stack
2266 // pointer.
2267 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2268 return Result;
2269
2270 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002271 // The chain for this call is now lowered.
2272 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
2273
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002274 if (GlobalAddressSDNode *GASD =
2275 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002276 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002277 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2278 } else if (ExternalSymbolSDNode *ESSDN =
2279 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002280 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002281 BuildMI(BB, X86::CALLpcrel32,
2282 1).addExternalSymbol(ESSDN->getSymbol(), true);
2283 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002284 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2285 Select(N.getOperand(0));
2286 Tmp1 = SelectExpr(N.getOperand(1));
2287 } else {
2288 Tmp1 = SelectExpr(N.getOperand(1));
2289 Select(N.getOperand(0));
2290 }
2291
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002292 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2293 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002294 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002295 default: assert(0 && "Unknown value type for call result!");
2296 case MVT::Other: return 1;
2297 case MVT::i1:
2298 case MVT::i8:
2299 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2300 break;
2301 case MVT::i16:
2302 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2303 break;
2304 case MVT::i32:
2305 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002306 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002307 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2308 break;
2309 case MVT::f32:
2310 case MVT::f64: // Floating-point return values live in %ST(0)
2311 ContainsFPCode = true;
2312 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2313 break;
2314 }
2315 return Result+N.ResNo;
2316 }
2317
2318 return 0;
2319}
2320
Chris Lattnere10269b2005-01-17 19:25:26 +00002321/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2322/// load/op/store instruction. If successful return true.
2323bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2324 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2325 SDOperand Chain = Node->getOperand(0);
2326 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002327 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002328
2329 // The chain has to be a load, the stored value must be an integer binary
2330 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002331 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002332 MVT::isFloatingPoint(StVal.getValueType()))
2333 return false;
2334
Chris Lattner5c659812005-01-17 22:10:42 +00002335 // Token chain must either be a factor node or the load to fold.
2336 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2337 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002338
Chris Lattner5c659812005-01-17 22:10:42 +00002339 SDOperand TheLoad;
2340
2341 // Check to see if there is a load from the same pointer that we're storing
2342 // to in either operand of the binop.
2343 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2344 StVal.getOperand(0).getOperand(1) == StPtr)
2345 TheLoad = StVal.getOperand(0);
2346 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2347 StVal.getOperand(1).getOperand(1) == StPtr)
2348 TheLoad = StVal.getOperand(1);
2349 else
2350 return false; // No matching load operand.
2351
2352 // We can only fold the load if there are no intervening side-effecting
2353 // operations. This means that the store uses the load as its token chain, or
2354 // there are only token factor nodes in between the store and load.
2355 if (Chain != TheLoad.getValue(1)) {
2356 // Okay, the other option is that we have a store referring to (possibly
2357 // nested) token factor nodes. For now, just try peeking through one level
2358 // of token factors to see if this is the case.
2359 bool ChainOk = false;
2360 if (Chain.getOpcode() == ISD::TokenFactor) {
2361 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2362 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2363 ChainOk = true;
2364 break;
2365 }
2366 }
2367
2368 if (!ChainOk) return false;
2369 }
2370
2371 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002372 return false;
2373
2374 // Make sure that one of the operands of the binop is the load, and that the
2375 // load folds into the binop.
2376 if (((StVal.getOperand(0) != TheLoad ||
2377 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2378 (StVal.getOperand(1) != TheLoad ||
2379 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2380 return false;
2381
2382 // Finally, check to see if this is one of the ops we can handle!
2383 static const unsigned ADDTAB[] = {
2384 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2385 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2386 };
2387 static const unsigned SUBTAB[] = {
2388 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2389 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2390 };
2391 static const unsigned ANDTAB[] = {
2392 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2393 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2394 };
2395 static const unsigned ORTAB[] = {
2396 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2397 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2398 };
2399 static const unsigned XORTAB[] = {
2400 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2401 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2402 };
2403 static const unsigned SHLTAB[] = {
2404 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2405 /*Have to put the reg in CL*/0, 0, 0,
2406 };
2407 static const unsigned SARTAB[] = {
2408 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2409 /*Have to put the reg in CL*/0, 0, 0,
2410 };
2411 static const unsigned SHRTAB[] = {
2412 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2413 /*Have to put the reg in CL*/0, 0, 0,
2414 };
2415
2416 const unsigned *TabPtr = 0;
2417 switch (StVal.getOpcode()) {
2418 default:
2419 std::cerr << "CANNOT [mem] op= val: ";
2420 StVal.Val->dump(); std::cerr << "\n";
2421 case ISD::MUL:
2422 case ISD::SDIV:
2423 case ISD::UDIV:
2424 case ISD::SREM:
2425 case ISD::UREM: return false;
2426
2427 case ISD::ADD: TabPtr = ADDTAB; break;
2428 case ISD::SUB: TabPtr = SUBTAB; break;
2429 case ISD::AND: TabPtr = ANDTAB; break;
2430 case ISD:: OR: TabPtr = ORTAB; break;
2431 case ISD::XOR: TabPtr = XORTAB; break;
2432 case ISD::SHL: TabPtr = SHLTAB; break;
2433 case ISD::SRA: TabPtr = SARTAB; break;
2434 case ISD::SRL: TabPtr = SHRTAB; break;
2435 }
2436
2437 // Handle: [mem] op= CST
2438 SDOperand Op0 = StVal.getOperand(0);
2439 SDOperand Op1 = StVal.getOperand(1);
2440 unsigned Opc;
2441 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2442 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2443 default: break;
2444 case MVT::i1:
2445 case MVT::i8: Opc = TabPtr[0]; break;
2446 case MVT::i16: Opc = TabPtr[1]; break;
2447 case MVT::i32: Opc = TabPtr[2]; break;
2448 }
2449
2450 if (Opc) {
Chris Lattner5c659812005-01-17 22:10:42 +00002451 LoweredTokens.insert(TheLoad.getValue(1));
2452 Select(Chain);
2453
Chris Lattnere10269b2005-01-17 19:25:26 +00002454 X86AddressMode AM;
2455 if (getRegPressure(TheLoad.getOperand(0)) >
2456 getRegPressure(TheLoad.getOperand(1))) {
2457 Select(TheLoad.getOperand(0));
2458 SelectAddress(TheLoad.getOperand(1), AM);
2459 } else {
2460 SelectAddress(TheLoad.getOperand(1), AM);
2461 Select(TheLoad.getOperand(0));
2462 }
Chris Lattner5c659812005-01-17 22:10:42 +00002463
2464 if (StVal.getOpcode() == ISD::ADD) {
2465 if (CN->getValue() == 1) {
2466 switch (Op0.getValueType()) {
2467 default: break;
2468 case MVT::i8:
2469 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2470 return true;
2471 case MVT::i16: Opc = TabPtr[1];
2472 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2473 return true;
2474 case MVT::i32: Opc = TabPtr[2];
2475 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2476 return true;
2477 }
2478 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2479 switch (Op0.getValueType()) {
2480 default: break;
2481 case MVT::i8:
2482 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2483 return true;
2484 case MVT::i16: Opc = TabPtr[1];
2485 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2486 return true;
2487 case MVT::i32: Opc = TabPtr[2];
2488 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2489 return true;
2490 }
2491 }
2492 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002493
2494 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2495 return true;
2496 }
2497 }
2498
2499 // If we have [mem] = V op [mem], try to turn it into:
2500 // [mem] = [mem] op V.
2501 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2502 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2503 StVal.getOpcode() != ISD::SRL)
2504 std::swap(Op0, Op1);
2505
2506 if (Op0 != TheLoad) return false;
2507
2508 switch (Op0.getValueType()) {
2509 default: return false;
2510 case MVT::i1:
2511 case MVT::i8: Opc = TabPtr[3]; break;
2512 case MVT::i16: Opc = TabPtr[4]; break;
2513 case MVT::i32: Opc = TabPtr[5]; break;
2514 }
Chris Lattner5c659812005-01-17 22:10:42 +00002515
2516 LoweredTokens.insert(TheLoad.getValue(1));
2517 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002518 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002519
Chris Lattnere10269b2005-01-17 19:25:26 +00002520 X86AddressMode AM;
2521 SelectAddress(TheLoad.getOperand(1), AM);
2522 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002523 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002524 return true;
2525}
2526
2527
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002528void ISel::Select(SDOperand N) {
2529 unsigned Tmp1, Tmp2, Opc;
2530
2531 // FIXME: Disable for our current expansion model!
2532 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
2533 return; // Already selected.
2534
Chris Lattner989de032005-01-11 06:14:36 +00002535 SDNode *Node = N.Val;
2536
2537 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002538 default:
Chris Lattner989de032005-01-11 06:14:36 +00002539 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002540 assert(0 && "Node not handled yet!");
2541 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002542 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002543 if (Node->getNumOperands() == 2) {
2544 bool OneFirst =
2545 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2546 Select(Node->getOperand(OneFirst));
2547 Select(Node->getOperand(!OneFirst));
2548 } else {
2549 std::vector<std::pair<unsigned, unsigned> > OpsP;
2550 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2551 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2552 std::sort(OpsP.begin(), OpsP.end());
2553 std::reverse(OpsP.begin(), OpsP.end());
2554 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2555 Select(Node->getOperand(OpsP[i].second));
2556 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002557 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002558 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002559 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2560 Select(N.getOperand(0));
2561 Tmp1 = SelectExpr(N.getOperand(1));
2562 } else {
2563 Tmp1 = SelectExpr(N.getOperand(1));
2564 Select(N.getOperand(0));
2565 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002566 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002567
2568 if (Tmp1 != Tmp2) {
2569 switch (N.getOperand(1).getValueType()) {
2570 default: assert(0 && "Invalid type for operation!");
2571 case MVT::i1:
2572 case MVT::i8: Opc = X86::MOV8rr; break;
2573 case MVT::i16: Opc = X86::MOV16rr; break;
2574 case MVT::i32: Opc = X86::MOV32rr; break;
2575 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002576 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002577 }
2578 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2579 }
2580 return;
2581 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002582 switch (N.getNumOperands()) {
2583 default:
2584 assert(0 && "Unknown return instruction!");
2585 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002586 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2587 N.getOperand(2).getValueType() == MVT::i32 &&
2588 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002589 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2590 Tmp1 = SelectExpr(N.getOperand(1));
2591 Tmp2 = SelectExpr(N.getOperand(2));
2592 } else {
2593 Tmp2 = SelectExpr(N.getOperand(2));
2594 Tmp1 = SelectExpr(N.getOperand(1));
2595 }
2596 Select(N.getOperand(0));
2597
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002598 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2599 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2600 // Declare that EAX & EDX are live on exit.
2601 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2602 .addReg(X86::ESP);
2603 break;
2604 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002605 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2606 Select(N.getOperand(0));
2607 Tmp1 = SelectExpr(N.getOperand(1));
2608 } else {
2609 Tmp1 = SelectExpr(N.getOperand(1));
2610 Select(N.getOperand(0));
2611 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002612 switch (N.getOperand(1).getValueType()) {
2613 default: assert(0 && "All other types should have been promoted!!");
2614 case MVT::f64:
2615 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2616 // Declare that top-of-stack is live on exit
2617 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2618 break;
2619 case MVT::i32:
2620 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2621 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2622 break;
2623 }
2624 break;
2625 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002626 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002627 break;
2628 }
2629 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2630 return;
2631 case ISD::BR: {
2632 Select(N.getOperand(0));
2633 MachineBasicBlock *Dest =
2634 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2635 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2636 return;
2637 }
2638
2639 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002640 MachineBasicBlock *Dest =
2641 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002642
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002643 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2644 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002645 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2646 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2647 Select(N.getOperand(0));
2648 Tmp1 = SelectExpr(N.getOperand(1));
2649 } else {
2650 Tmp1 = SelectExpr(N.getOperand(1));
2651 Select(N.getOperand(0));
2652 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002653 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2654 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2655 }
Chris Lattner11333092005-01-11 03:11:44 +00002656
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002657 return;
2658 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002659
Chris Lattner4df0de92005-01-17 00:00:33 +00002660 case ISD::LOAD:
2661 // If this load could be folded into the only using instruction, and if it
2662 // is safe to emit the instruction here, try to do so now.
2663 if (Node->hasNUsesOfValue(1, 0)) {
2664 SDOperand TheVal = N.getValue(0);
2665 SDNode *User = 0;
2666 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2667 assert(UI != Node->use_end() && "Didn't find use!");
2668 SDNode *UN = *UI;
2669 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2670 if (UN->getOperand(i) == TheVal) {
2671 User = UN;
2672 goto FoundIt;
2673 }
2674 }
2675 FoundIt:
2676 // Only handle unary operators right now.
2677 if (User->getNumOperands() == 1) {
2678 LoweredTokens.erase(N);
2679 SelectExpr(SDOperand(User, 0));
2680 return;
2681 }
2682 }
2683 SelectExpr(N);
2684 return;
2685
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002686 case ISD::EXTLOAD:
2687 case ISD::SEXTLOAD:
2688 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002689 case ISD::CALL:
2690 case ISD::DYNAMIC_STACKALLOC:
2691 SelectExpr(N);
2692 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002693
2694 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2695 // On X86, we can represent all types except for Bool and Float natively.
2696 X86AddressMode AM;
2697 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002698 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2699 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2700 && "Unsupported TRUNCSTORE for this target!");
2701
2702 if (StoredTy == MVT::i16) {
2703 // FIXME: This is here just to allow testing. X86 doesn't really have a
2704 // TRUNCSTORE i16 operation, but this is required for targets that do not
2705 // have 16-bit integer registers. We occasionally disable 16-bit integer
2706 // registers to test the promotion code.
2707 Select(N.getOperand(0));
2708 Tmp1 = SelectExpr(N.getOperand(1));
2709 SelectAddress(N.getOperand(2), AM);
2710
2711 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2712 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2713 return;
2714 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002715
2716 // Store of constant bool?
2717 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2718 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2719 Select(N.getOperand(0));
2720 SelectAddress(N.getOperand(2), AM);
2721 } else {
2722 SelectAddress(N.getOperand(2), AM);
2723 Select(N.getOperand(0));
2724 }
2725 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2726 return;
2727 }
2728
2729 switch (StoredTy) {
2730 default: assert(0 && "Cannot truncstore this type!");
2731 case MVT::i1: Opc = X86::MOV8mr; break;
2732 case MVT::f32: Opc = X86::FST32m; break;
2733 }
2734
2735 std::vector<std::pair<unsigned, unsigned> > RP;
2736 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2737 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2738 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2739 std::sort(RP.begin(), RP.end());
2740
2741 for (unsigned i = 0; i != 3; ++i)
2742 switch (RP[2-i].second) {
2743 default: assert(0 && "Unknown operand number!");
2744 case 0: Select(N.getOperand(0)); break;
2745 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2746 case 2: SelectAddress(N.getOperand(2), AM); break;
2747 }
2748
2749 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2750 return;
2751 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002752 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002753 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002754
2755 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2756 Opc = 0;
2757 switch (CN->getValueType(0)) {
2758 default: assert(0 && "Invalid type for operation!");
2759 case MVT::i1:
2760 case MVT::i8: Opc = X86::MOV8mi; break;
2761 case MVT::i16: Opc = X86::MOV16mi; break;
2762 case MVT::i32: Opc = X86::MOV32mi; break;
2763 case MVT::f32:
2764 case MVT::f64: break;
2765 }
2766 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002767 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2768 Select(N.getOperand(0));
2769 SelectAddress(N.getOperand(2), AM);
2770 } else {
2771 SelectAddress(N.getOperand(2), AM);
2772 Select(N.getOperand(0));
2773 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002774 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2775 return;
2776 }
2777 }
Chris Lattner837caa72005-01-11 23:21:30 +00002778
2779 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00002780 if (TryToFoldLoadOpStore(Node))
2781 return;
Chris Lattner837caa72005-01-11 23:21:30 +00002782
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002783 switch (N.getOperand(1).getValueType()) {
2784 default: assert(0 && "Cannot store this type!");
2785 case MVT::i1:
2786 case MVT::i8: Opc = X86::MOV8mr; break;
2787 case MVT::i16: Opc = X86::MOV16mr; break;
2788 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002789 case MVT::f32: Opc = X86::FST32m; break;
2790 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002791 }
Chris Lattner11333092005-01-11 03:11:44 +00002792
2793 std::vector<std::pair<unsigned, unsigned> > RP;
2794 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2795 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2796 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2797 std::sort(RP.begin(), RP.end());
2798
2799 for (unsigned i = 0; i != 3; ++i)
2800 switch (RP[2-i].second) {
2801 default: assert(0 && "Unknown operand number!");
2802 case 0: Select(N.getOperand(0)); break;
2803 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002804 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002805 }
2806
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002807 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2808 return;
2809 }
2810 case ISD::ADJCALLSTACKDOWN:
2811 case ISD::ADJCALLSTACKUP:
2812 Select(N.getOperand(0));
2813 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2814
2815 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2816 X86::ADJCALLSTACKUP;
2817 BuildMI(BB, Opc, 1).addImm(Tmp1);
2818 return;
Chris Lattner989de032005-01-11 06:14:36 +00002819 case ISD::MEMSET: {
2820 Select(N.getOperand(0)); // Select the chain.
2821 unsigned Align =
2822 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2823 if (Align == 0) Align = 1;
2824
2825 // Turn the byte code into # iterations
2826 unsigned CountReg;
2827 unsigned Opcode;
2828 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2829 unsigned Val = ValC->getValue() & 255;
2830
2831 // If the value is a constant, then we can potentially use larger sets.
2832 switch (Align & 3) {
2833 case 2: // WORD aligned
2834 CountReg = MakeReg(MVT::i32);
2835 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2836 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2837 } else {
2838 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2839 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2840 }
2841 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2842 Opcode = X86::REP_STOSW;
2843 break;
2844 case 0: // DWORD aligned
2845 CountReg = MakeReg(MVT::i32);
2846 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2847 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2848 } else {
2849 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2850 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2851 }
2852 Val = (Val << 8) | Val;
2853 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2854 Opcode = X86::REP_STOSD;
2855 break;
2856 default: // BYTE aligned
2857 CountReg = SelectExpr(Node->getOperand(3));
2858 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2859 Opcode = X86::REP_STOSB;
2860 break;
2861 }
2862 } else {
2863 // If it's not a constant value we are storing, just fall back. We could
2864 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2865 unsigned ValReg = SelectExpr(Node->getOperand(2));
2866 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2867 CountReg = SelectExpr(Node->getOperand(3));
2868 Opcode = X86::REP_STOSB;
2869 }
2870
2871 // No matter what the alignment is, we put the source in ESI, the
2872 // destination in EDI, and the count in ECX.
2873 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2874 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2875 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2876 BuildMI(BB, Opcode, 0);
2877 return;
2878 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002879 case ISD::MEMCPY:
2880 Select(N.getOperand(0)); // Select the chain.
2881 unsigned Align =
2882 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2883 if (Align == 0) Align = 1;
2884
2885 // Turn the byte code into # iterations
2886 unsigned CountReg;
2887 unsigned Opcode;
2888 switch (Align & 3) {
2889 case 2: // WORD aligned
2890 CountReg = MakeReg(MVT::i32);
2891 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2892 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2893 } else {
2894 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2895 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2896 }
2897 Opcode = X86::REP_MOVSW;
2898 break;
2899 case 0: // DWORD aligned
2900 CountReg = MakeReg(MVT::i32);
2901 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2902 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2903 } else {
2904 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2905 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2906 }
2907 Opcode = X86::REP_MOVSD;
2908 break;
2909 default: // BYTE aligned
2910 CountReg = SelectExpr(Node->getOperand(3));
2911 Opcode = X86::REP_MOVSB;
2912 break;
2913 }
2914
2915 // No matter what the alignment is, we put the source in ESI, the
2916 // destination in EDI, and the count in ECX.
2917 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2918 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2919 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2920 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2921 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2922 BuildMI(BB, Opcode, 0);
2923 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002924 }
2925 assert(0 && "Should not be reached!");
2926}
2927
2928
2929/// createX86PatternInstructionSelector - This pass converts an LLVM function
2930/// into a machine code representation using pattern matching and a machine
2931/// description file.
2932///
2933FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2934 return new ISel(TM);
2935}