blob: 0b64354954b7d76eaf4eabe39fb90fbdbc415241 [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);
Chris Lattner009b55b2005-01-19 03:36:30 +000046 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000047
48 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000049 addRegisterClass(MVT::i8, X86::R8RegisterClass);
50 addRegisterClass(MVT::i16, X86::R16RegisterClass);
51 addRegisterClass(MVT::i32, X86::R32RegisterClass);
52 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
53
54 // FIXME: Eliminate these two classes when legalize can handle promotions
55 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000056/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000057
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 Lattnerb62e1e22005-01-21 19:46:38 +0000199 std::vector<SDOperand> Stores;
200
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000201 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
202 unsigned ArgReg;
203 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
204 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
205
206 switch (getValueType(Args[i].second)) {
207 default: assert(0 && "Unexpected ValueType for argument!");
208 case MVT::i1:
209 case MVT::i8:
210 case MVT::i16:
211 // Promote the integer to 32 bits. If the input type is signed use a
212 // sign extend, otherwise use a zero extend.
213 if (Args[i].second->isSigned())
214 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
215 else
216 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
217
218 // FALL THROUGH
219 case MVT::i32:
220 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000221 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
222 Args[i].first, PtrOff));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000223 ArgOffset += 4;
224 break;
225 case MVT::i64:
226 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000227 Stores.push_back(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 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000233 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000234 }
235
236 std::vector<MVT::ValueType> RetVals;
237 MVT::ValueType RetTyVT = getValueType(RetTy);
238 if (RetTyVT != MVT::isVoid)
239 RetVals.push_back(RetTyVT);
240 RetVals.push_back(MVT::Other);
241
Chris Lattner5188ad72005-01-08 19:28:19 +0000242 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000243 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000244 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
245 DAG.getConstant(NumBytes, getPointerTy()));
246 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000247}
248
Chris Lattner14824582005-01-09 00:01:27 +0000249std::pair<SDOperand, SDOperand>
250X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
251 // vastart just returns the address of the VarArgsFrameIndex slot.
252 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
253}
254
255std::pair<SDOperand,SDOperand> X86TargetLowering::
256LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
257 const Type *ArgTy, SelectionDAG &DAG) {
258 MVT::ValueType ArgVT = getValueType(ArgTy);
259 SDOperand Result;
260 if (!isVANext) {
261 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
262 } else {
263 unsigned Amt;
264 if (ArgVT == MVT::i32)
265 Amt = 4;
266 else {
267 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
268 "Other types should have been promoted for varargs!");
269 Amt = 8;
270 }
271 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
272 DAG.getConstant(Amt, VAList.getValueType()));
273 }
274 return std::make_pair(Result, Chain);
275}
276
277
278std::pair<SDOperand, SDOperand> X86TargetLowering::
279LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
280 SelectionDAG &DAG) {
281 SDOperand Result;
282 if (Depth) // Depths > 0 not supported yet!
283 Result = DAG.getConstant(0, getPointerTy());
284 else {
285 if (ReturnAddrIndex == 0) {
286 // Set up a frame object for the return address.
287 MachineFunction &MF = DAG.getMachineFunction();
288 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
289 }
290
291 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
292
293 if (!isFrameAddress)
294 // Just load the return address
295 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
296 else
297 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
298 DAG.getConstant(4, MVT::i32));
299 }
300 return std::make_pair(Result, Chain);
301}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000302
303
Chris Lattner98a8ba02005-01-18 01:06:26 +0000304namespace {
305 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
306 /// SDOperand's instead of register numbers for the leaves of the matched
307 /// tree.
308 struct X86ISelAddressMode {
309 enum {
310 RegBase,
311 FrameIndexBase,
312 } BaseType;
313
314 struct { // This is really a union, discriminated by BaseType!
315 SDOperand Reg;
316 int FrameIndex;
317 } Base;
318
319 unsigned Scale;
320 SDOperand IndexReg;
321 unsigned Disp;
322 GlobalValue *GV;
323
324 X86ISelAddressMode()
325 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
326 }
327 };
328}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000329
330
331namespace {
332 Statistic<>
333 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
334
335 //===--------------------------------------------------------------------===//
336 /// ISel - X86 specific code to select X86 machine instructions for
337 /// SelectionDAG operations.
338 ///
339 class ISel : public SelectionDAGISel {
340 /// ContainsFPCode - Every instruction we select that uses or defines a FP
341 /// register should set this to true.
342 bool ContainsFPCode;
343
344 /// X86Lowering - This object fully describes how to lower LLVM code to an
345 /// X86-specific SelectionDAG.
346 X86TargetLowering X86Lowering;
347
Chris Lattner11333092005-01-11 03:11:44 +0000348 /// RegPressureMap - This keeps an approximate count of the number of
349 /// registers required to evaluate each node in the graph.
350 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000351
352 /// ExprMap - As shared expressions are codegen'd, we keep track of which
353 /// vreg the value is produced in, so we only emit one copy of each compiled
354 /// tree.
355 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000356
357 public:
358 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
359 }
360
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000361 virtual const char *getPassName() const {
362 return "X86 Pattern Instruction Selection";
363 }
364
Chris Lattner11333092005-01-11 03:11:44 +0000365 unsigned getRegPressure(SDOperand O) {
366 return RegPressureMap[O.Val];
367 }
368 unsigned ComputeRegPressure(SDOperand O);
369
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000370 /// InstructionSelectBasicBlock - This callback is invoked by
371 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000372 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000373
Chris Lattner4ff348b2005-01-17 06:26:58 +0000374 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp);
Chris Lattnera5ade062005-01-11 21:19:59 +0000375 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000376 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000377
Chris Lattner30ea1e92005-01-19 07:37:26 +0000378 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000379 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000380 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000381 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
382 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000383 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000384
385 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
386 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
387 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000388 void Select(SDOperand N);
389 };
390}
391
Chris Lattner7dbcb752005-01-12 04:21:28 +0000392/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
393/// when it has created a SelectionDAG for us to codegen.
394void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
395 // While we're doing this, keep track of whether we see any FP code for
396 // FP_REG_KILL insertion.
397 ContainsFPCode = false;
398
399 // Scan the PHI nodes that already are inserted into this basic block. If any
400 // of them is a PHI of a floating point value, we need to insert an
401 // FP_REG_KILL.
402 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
403 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
404 I != E; ++I) {
405 assert(I->getOpcode() == X86::PHI &&
406 "Isn't just PHI nodes?");
407 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
408 X86::RFPRegisterClass) {
409 ContainsFPCode = true;
410 break;
411 }
412 }
413
414 // Compute the RegPressureMap, which is an approximation for the number of
415 // registers required to compute each node.
416 ComputeRegPressure(DAG.getRoot());
417
418 // Codegen the basic block.
419 Select(DAG.getRoot());
420
421 // Finally, look at all of the successors of this block. If any contain a PHI
422 // node of FP type, we need to insert an FP_REG_KILL in this block.
423 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
424 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
425 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
426 I != E && I->getOpcode() == X86::PHI; ++I) {
427 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
428 X86::RFPRegisterClass) {
429 ContainsFPCode = true;
430 break;
431 }
432 }
433
434 // Insert FP_REG_KILL instructions into basic blocks that need them. This
435 // only occurs due to the floating point stackifier not being aggressive
436 // enough to handle arbitrary global stackification.
437 //
438 // Currently we insert an FP_REG_KILL instruction into each block that uses or
439 // defines a floating point virtual register.
440 //
441 // When the global register allocators (like linear scan) finally update live
442 // variable analysis, we can keep floating point values in registers across
443 // basic blocks. This will be a huge win, but we are waiting on the global
444 // allocators before we can do this.
445 //
446 if (ContainsFPCode && BB->succ_size()) {
447 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
448 ++NumFPKill;
449 }
450
451 // Clear state used for selection.
452 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000453 RegPressureMap.clear();
454}
455
456
Chris Lattner11333092005-01-11 03:11:44 +0000457// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
458// for the number of registers required to compute each node. This is basically
459// computing a generalized form of the Sethi-Ullman number for each node.
460unsigned ISel::ComputeRegPressure(SDOperand O) {
461 SDNode *N = O.Val;
462 unsigned &Result = RegPressureMap[N];
463 if (Result) return Result;
464
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000465 // FIXME: Should operations like CALL (which clobber lots o regs) have a
466 // higher fixed cost??
467
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000468 if (N->getNumOperands() == 0) {
469 Result = 1;
470 } else {
471 unsigned MaxRegUse = 0;
472 unsigned NumExtraMaxRegUsers = 0;
473 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
474 unsigned Regs;
475 if (N->getOperand(i).getOpcode() == ISD::Constant)
476 Regs = 0;
477 else
478 Regs = ComputeRegPressure(N->getOperand(i));
479 if (Regs > MaxRegUse) {
480 MaxRegUse = Regs;
481 NumExtraMaxRegUsers = 0;
482 } else if (Regs == MaxRegUse &&
483 N->getOperand(i).getValueType() != MVT::Other) {
484 ++NumExtraMaxRegUsers;
485 }
Chris Lattner11333092005-01-11 03:11:44 +0000486 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000487
488 if (O.getOpcode() != ISD::TokenFactor)
489 Result = MaxRegUse+NumExtraMaxRegUsers;
490 else
Chris Lattner869e0432005-01-17 23:02:13 +0000491 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000492 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000493
Chris Lattner837caa72005-01-11 23:21:30 +0000494 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000495 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000496}
497
Chris Lattnerbf52d492005-01-20 16:50:16 +0000498/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
499/// The DAG cannot have cycles in it, by definition, so the visited set is not
500/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
501/// reuse, so it prevents exponential cases.
502///
503static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
504 std::set<SDNode*> &Visited) {
505 if (N == Op) return true; // Found it.
506 SDNode *Node = N.Val;
507 if (Node->getNumOperands() == 0) return false; // Leaf?
508 if (!Visited.insert(Node).second) return false; // Already visited?
509
510 // Recurse for the first N-1 operands.
511 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
512 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
513 return true;
514
515 // Tail recurse for the last operand.
516 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
517}
518
Chris Lattner98a8ba02005-01-18 01:06:26 +0000519X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
520 X86AddressMode Result;
521
522 // If we need to emit two register operands, emit the one with the highest
523 // register pressure first.
524 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
525 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000526 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000527 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000528 std::set<SDNode*> Visited;
529 EmitBaseThenIndex = true;
530 // If Base ends up pointing to Index, we must emit index first. This is
531 // because of the way we fold loads, we may end up doing bad things with
532 // the folded add.
533 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
534 EmitBaseThenIndex = false;
535 } else {
536 std::set<SDNode*> Visited;
537 EmitBaseThenIndex = false;
538 // If Base ends up pointing to Index, we must emit index first. This is
539 // because of the way we fold loads, we may end up doing bad things with
540 // the folded add.
541 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
542 EmitBaseThenIndex = true;
543 }
544
545 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000546 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
547 Result.IndexReg = SelectExpr(IAM.IndexReg);
548 } else {
549 Result.IndexReg = SelectExpr(IAM.IndexReg);
550 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
551 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000552
Chris Lattner98a8ba02005-01-18 01:06:26 +0000553 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
554 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
555 } else if (IAM.IndexReg.Val) {
556 Result.IndexReg = SelectExpr(IAM.IndexReg);
557 }
558
559 switch (IAM.BaseType) {
560 case X86ISelAddressMode::RegBase:
561 Result.BaseType = X86AddressMode::RegBase;
562 break;
563 case X86ISelAddressMode::FrameIndexBase:
564 Result.BaseType = X86AddressMode::FrameIndexBase;
565 Result.Base.FrameIndex = IAM.Base.FrameIndex;
566 break;
567 default:
568 assert(0 && "Unknown base type!");
569 break;
570 }
571 Result.Scale = IAM.Scale;
572 Result.Disp = IAM.Disp;
573 Result.GV = IAM.GV;
574 return Result;
575}
576
577/// SelectAddress - Pattern match the maximal addressing mode for this node and
578/// emit all of the leaf registers.
579void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
580 X86ISelAddressMode IAM;
581 MatchAddress(N, IAM);
582 AM = SelectAddrExprs(IAM);
583}
584
585/// MatchAddress - Add the specified node to the specified addressing mode,
586/// returning true if it cannot be done. This just pattern matches for the
587/// addressing mode, it does not cause any code to be emitted. For that, use
588/// SelectAddress.
589bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000590 switch (N.getOpcode()) {
591 default: break;
592 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000593 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
594 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000595 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
596 return false;
597 }
598 break;
599 case ISD::GlobalAddress:
600 if (AM.GV == 0) {
601 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
602 return false;
603 }
604 break;
605 case ISD::Constant:
606 AM.Disp += cast<ConstantSDNode>(N)->getValue();
607 return false;
608 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000609 // We might have folded the load into this shift, so don't regen the value
610 // if so.
611 if (ExprMap.count(N)) break;
612
Chris Lattner98a8ba02005-01-18 01:06:26 +0000613 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000614 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
615 unsigned Val = CN->getValue();
616 if (Val == 1 || Val == 2 || Val == 3) {
617 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000618 SDOperand ShVal = N.Val->getOperand(0);
619
620 // Okay, we know that we have a scale by now. However, if the scaled
621 // value is an add of something and a constant, we can fold the
622 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000623 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000624 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000625 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000626 ConstantSDNode *AddVal =
627 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
628 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000629 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000630 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000631 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000632 return false;
633 }
634 }
635 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000636 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000637 // We might have folded the load into this mul, so don't regen the value if
638 // so.
639 if (ExprMap.count(N)) break;
640
Chris Lattner947d5442005-01-11 19:37:02 +0000641 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000642 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
643 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000644 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
645 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
646 AM.Scale = unsigned(CN->getValue())-1;
647
648 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000649 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000650
651 // Okay, we know that we have a scale by now. However, if the scaled
652 // value is an add of something and a constant, we can fold the
653 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000654 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000655 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000656 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000657 ConstantSDNode *AddVal =
658 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
659 AM.Disp += AddVal->getValue() * CN->getValue();
660 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000661 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000662 }
663
664 AM.IndexReg = AM.Base.Reg = Reg;
665 return false;
666 }
667 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000668
669 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000670 // We might have folded the load into this mul, so don't regen the value if
671 // so.
672 if (ExprMap.count(N)) break;
673
Chris Lattner98a8ba02005-01-18 01:06:26 +0000674 X86ISelAddressMode Backup = AM;
675 if (!MatchAddress(N.Val->getOperand(0), AM) &&
676 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000677 return false;
678 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000679 if (!MatchAddress(N.Val->getOperand(1), AM) &&
680 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000681 return false;
682 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000683 break;
684 }
685 }
686
Chris Lattnera95589b2005-01-11 04:40:19 +0000687 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000688 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000689 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000690 if (AM.IndexReg.Val == 0) {
691 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000692 AM.Scale = 1;
693 return false;
694 }
695
696 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000697 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000698 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000699
700 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000701 AM.BaseType = X86ISelAddressMode::RegBase;
702 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000703 return false;
704}
705
706/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
707/// assuming that the temporary registers are in the 8-bit register class.
708///
709/// Tmp1 = setcc1
710/// Tmp2 = setcc2
711/// DestReg = logicalop Tmp1, Tmp2
712///
713static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
714 unsigned SetCC2, unsigned LogicalOp,
715 unsigned DestReg) {
716 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
717 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
718 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
719 BuildMI(BB, SetCC1, 0, Tmp1);
720 BuildMI(BB, SetCC2, 0, Tmp2);
721 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
722}
723
724/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
725/// condition codes match the specified SetCCOpcode. Note that some conditions
726/// require multiple instructions to generate the correct value.
727static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
728 ISD::CondCode SetCCOpcode, bool isFP) {
729 unsigned Opc;
730 if (!isFP) {
731 switch (SetCCOpcode) {
732 default: assert(0 && "Illegal integer SetCC!");
733 case ISD::SETEQ: Opc = X86::SETEr; break;
734 case ISD::SETGT: Opc = X86::SETGr; break;
735 case ISD::SETGE: Opc = X86::SETGEr; break;
736 case ISD::SETLT: Opc = X86::SETLr; break;
737 case ISD::SETLE: Opc = X86::SETLEr; break;
738 case ISD::SETNE: Opc = X86::SETNEr; break;
739 case ISD::SETULT: Opc = X86::SETBr; break;
740 case ISD::SETUGT: Opc = X86::SETAr; break;
741 case ISD::SETULE: Opc = X86::SETBEr; break;
742 case ISD::SETUGE: Opc = X86::SETAEr; break;
743 }
744 } else {
745 // On a floating point condition, the flags are set as follows:
746 // ZF PF CF op
747 // 0 | 0 | 0 | X > Y
748 // 0 | 0 | 1 | X < Y
749 // 1 | 0 | 0 | X == Y
750 // 1 | 1 | 1 | unordered
751 //
752 switch (SetCCOpcode) {
753 default: assert(0 && "Invalid FP setcc!");
754 case ISD::SETUEQ:
755 case ISD::SETEQ:
756 Opc = X86::SETEr; // True if ZF = 1
757 break;
758 case ISD::SETOGT:
759 case ISD::SETGT:
760 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
761 break;
762 case ISD::SETOGE:
763 case ISD::SETGE:
764 Opc = X86::SETAEr; // True if CF = 0
765 break;
766 case ISD::SETULT:
767 case ISD::SETLT:
768 Opc = X86::SETBr; // True if CF = 1
769 break;
770 case ISD::SETULE:
771 case ISD::SETLE:
772 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
773 break;
774 case ISD::SETONE:
775 case ISD::SETNE:
776 Opc = X86::SETNEr; // True if ZF = 0
777 break;
778 case ISD::SETUO:
779 Opc = X86::SETPr; // True if PF = 1
780 break;
781 case ISD::SETO:
782 Opc = X86::SETNPr; // True if PF = 0
783 break;
784 case ISD::SETOEQ: // !PF & ZF
785 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
786 return;
787 case ISD::SETOLT: // !PF & CF
788 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
789 return;
790 case ISD::SETOLE: // !PF & (CF || ZF)
791 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
792 return;
793 case ISD::SETUGT: // PF | (!ZF & !CF)
794 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
795 return;
796 case ISD::SETUGE: // PF | !CF
797 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
798 return;
799 case ISD::SETUNE: // PF | !ZF
800 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
801 return;
802 }
803 }
804 BuildMI(BB, Opc, 0, DestReg);
805}
806
807
808/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
809/// the Dest block if the Cond condition is true. If we cannot fold this
810/// condition into the branch, return true.
811///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000812bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
813 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000814 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
815 // B) using two conditional branches instead of one condbr, two setcc's, and
816 // an or.
817 if ((Cond.getOpcode() == ISD::OR ||
818 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
819 // And and or set the flags for us, so there is no need to emit a TST of the
820 // result. It is only safe to do this if there is only a single use of the
821 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000822 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000823 SelectExpr(Cond);
824 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
825 return false;
826 }
827
828 // Codegen br not C -> JE.
829 if (Cond.getOpcode() == ISD::XOR)
830 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
831 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000832 unsigned CondR;
833 if (getRegPressure(Chain) > getRegPressure(Cond)) {
834 Select(Chain);
835 CondR = SelectExpr(Cond.Val->getOperand(0));
836 } else {
837 CondR = SelectExpr(Cond.Val->getOperand(0));
838 Select(Chain);
839 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000840 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
841 BuildMI(BB, X86::JE, 1).addMBB(Dest);
842 return false;
843 }
844
845 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
846 if (SetCC == 0)
847 return true; // Can only handle simple setcc's so far.
848
849 unsigned Opc;
850
851 // Handle integer conditions first.
852 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
853 switch (SetCC->getCondition()) {
854 default: assert(0 && "Illegal integer SetCC!");
855 case ISD::SETEQ: Opc = X86::JE; break;
856 case ISD::SETGT: Opc = X86::JG; break;
857 case ISD::SETGE: Opc = X86::JGE; break;
858 case ISD::SETLT: Opc = X86::JL; break;
859 case ISD::SETLE: Opc = X86::JLE; break;
860 case ISD::SETNE: Opc = X86::JNE; break;
861 case ISD::SETULT: Opc = X86::JB; break;
862 case ISD::SETUGT: Opc = X86::JA; break;
863 case ISD::SETULE: Opc = X86::JBE; break;
864 case ISD::SETUGE: Opc = X86::JAE; break;
865 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000866 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000867 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000868 BuildMI(BB, Opc, 1).addMBB(Dest);
869 return false;
870 }
871
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000872 unsigned Opc2 = 0; // Second branch if needed.
873
874 // On a floating point condition, the flags are set as follows:
875 // ZF PF CF op
876 // 0 | 0 | 0 | X > Y
877 // 0 | 0 | 1 | X < Y
878 // 1 | 0 | 0 | X == Y
879 // 1 | 1 | 1 | unordered
880 //
881 switch (SetCC->getCondition()) {
882 default: assert(0 && "Invalid FP setcc!");
883 case ISD::SETUEQ:
884 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
885 case ISD::SETOGT:
886 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
887 case ISD::SETOGE:
888 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
889 case ISD::SETULT:
890 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
891 case ISD::SETULE:
892 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
893 case ISD::SETONE:
894 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
895 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
896 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
897 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
898 Opc = X86::JA; // ZF = 0 & CF = 0
899 Opc2 = X86::JP; // PF = 1
900 break;
901 case ISD::SETUGE: // PF = 1 | CF = 0
902 Opc = X86::JAE; // CF = 0
903 Opc2 = X86::JP; // PF = 1
904 break;
905 case ISD::SETUNE: // PF = 1 | ZF = 0
906 Opc = X86::JNE; // ZF = 0
907 Opc2 = X86::JP; // PF = 1
908 break;
909 case ISD::SETOEQ: // PF = 0 & ZF = 1
910 //X86::JNP, X86::JE
911 //X86::AND8rr
912 return true; // FIXME: Emit more efficient code for this branch.
913 case ISD::SETOLT: // PF = 0 & CF = 1
914 //X86::JNP, X86::JB
915 //X86::AND8rr
916 return true; // FIXME: Emit more efficient code for this branch.
917 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
918 //X86::JNP, X86::JBE
919 //X86::AND8rr
920 return true; // FIXME: Emit more efficient code for this branch.
921 }
922
Chris Lattner6c07aee2005-01-11 04:06:27 +0000923 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000924 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000925 BuildMI(BB, Opc, 1).addMBB(Dest);
926 if (Opc2)
927 BuildMI(BB, Opc2, 1).addMBB(Dest);
928 return false;
929}
930
Chris Lattner24aad1b2005-01-10 22:10:13 +0000931/// EmitSelectCC - Emit code into BB that performs a select operation between
932/// the two registers RTrue and RFalse, generating a result into RDest. Return
933/// true if the fold cannot be performed.
934///
935void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
936 unsigned RTrue, unsigned RFalse, unsigned RDest) {
937 enum Condition {
938 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
939 NOT_SET
940 } CondCode = NOT_SET;
941
942 static const unsigned CMOVTAB16[] = {
943 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
944 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
945 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
946 };
947 static const unsigned CMOVTAB32[] = {
948 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
949 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
950 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
951 };
952 static const unsigned CMOVTABFP[] = {
953 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
954 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
955 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
956 };
957
958 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
959 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
960 switch (SetCC->getCondition()) {
961 default: assert(0 && "Unknown integer comparison!");
962 case ISD::SETEQ: CondCode = EQ; break;
963 case ISD::SETGT: CondCode = GT; break;
964 case ISD::SETGE: CondCode = GE; break;
965 case ISD::SETLT: CondCode = LT; break;
966 case ISD::SETLE: CondCode = LE; break;
967 case ISD::SETNE: CondCode = NE; break;
968 case ISD::SETULT: CondCode = B; break;
969 case ISD::SETUGT: CondCode = A; break;
970 case ISD::SETULE: CondCode = BE; break;
971 case ISD::SETUGE: CondCode = AE; break;
972 }
973 } else {
974 // On a floating point condition, the flags are set as follows:
975 // ZF PF CF op
976 // 0 | 0 | 0 | X > Y
977 // 0 | 0 | 1 | X < Y
978 // 1 | 0 | 0 | X == Y
979 // 1 | 1 | 1 | unordered
980 //
981 switch (SetCC->getCondition()) {
982 default: assert(0 && "Unknown FP comparison!");
983 case ISD::SETUEQ:
984 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
985 case ISD::SETOGT:
986 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
987 case ISD::SETOGE:
988 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
989 case ISD::SETULT:
990 case ISD::SETLT: CondCode = B; break; // True if CF = 1
991 case ISD::SETULE:
992 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
993 case ISD::SETONE:
994 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
995 case ISD::SETUO: CondCode = P; break; // True if PF = 1
996 case ISD::SETO: CondCode = NP; break; // True if PF = 0
997 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
998 case ISD::SETUGE: // PF = 1 | CF = 0
999 case ISD::SETUNE: // PF = 1 | ZF = 0
1000 case ISD::SETOEQ: // PF = 0 & ZF = 1
1001 case ISD::SETOLT: // PF = 0 & CF = 1
1002 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1003 // We cannot emit this comparison as a single cmov.
1004 break;
1005 }
1006 }
1007 }
1008
1009 unsigned Opc = 0;
1010 if (CondCode != NOT_SET) {
1011 switch (SVT) {
1012 default: assert(0 && "Cannot select this type!");
1013 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1014 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001015 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001016 }
1017 }
1018
1019 // Finally, if we weren't able to fold this, just emit the condition and test
1020 // it.
1021 if (CondCode == NOT_SET || Opc == 0) {
1022 // Get the condition into the zero flag.
1023 unsigned CondReg = SelectExpr(Cond);
1024 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1025
1026 switch (SVT) {
1027 default: assert(0 && "Cannot select this type!");
1028 case MVT::i16: Opc = X86::CMOVE16rr; break;
1029 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001030 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001031 }
1032 } else {
1033 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001034 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001035 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001036 }
1037 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1038}
1039
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001040void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001041 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001042 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1043 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001044 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001045 switch (RHS.getValueType()) {
1046 default: break;
1047 case MVT::i1:
1048 case MVT::i8: Opc = X86::CMP8mi; break;
1049 case MVT::i16: Opc = X86::CMP16mi; break;
1050 case MVT::i32: Opc = X86::CMP32mi; break;
1051 }
1052 if (Opc) {
1053 X86AddressMode AM;
1054 EmitFoldedLoad(LHS, AM);
1055 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1056 return;
1057 }
1058 }
1059
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001060 switch (RHS.getValueType()) {
1061 default: break;
1062 case MVT::i1:
1063 case MVT::i8: Opc = X86::CMP8ri; break;
1064 case MVT::i16: Opc = X86::CMP16ri; break;
1065 case MVT::i32: Opc = X86::CMP32ri; break;
1066 }
1067 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001068 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001069 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1070 return;
1071 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001072 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1073 if (CN->isExactlyValue(+0.0) ||
1074 CN->isExactlyValue(-0.0)) {
1075 unsigned Reg = SelectExpr(LHS);
1076 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1077 BuildMI(BB, X86::FNSTSW8r, 0);
1078 BuildMI(BB, X86::SAHF, 1);
1079 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001080 }
1081
Chris Lattneref6806c2005-01-12 02:02:48 +00001082 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001083 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001084 switch (RHS.getValueType()) {
1085 default: break;
1086 case MVT::i1:
1087 case MVT::i8: Opc = X86::CMP8mr; break;
1088 case MVT::i16: Opc = X86::CMP16mr; break;
1089 case MVT::i32: Opc = X86::CMP32mr; break;
1090 }
1091 if (Opc) {
1092 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001093 EmitFoldedLoad(LHS, AM);
1094 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001095 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1096 return;
1097 }
1098 }
1099
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001100 switch (LHS.getValueType()) {
1101 default: assert(0 && "Cannot compare this value!");
1102 case MVT::i1:
1103 case MVT::i8: Opc = X86::CMP8rr; break;
1104 case MVT::i16: Opc = X86::CMP16rr; break;
1105 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001106 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001107 }
Chris Lattner11333092005-01-11 03:11:44 +00001108 unsigned Tmp1, Tmp2;
1109 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1110 Tmp1 = SelectExpr(LHS);
1111 Tmp2 = SelectExpr(RHS);
1112 } else {
1113 Tmp2 = SelectExpr(RHS);
1114 Tmp1 = SelectExpr(LHS);
1115 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001116 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1117}
1118
Chris Lattnera5ade062005-01-11 21:19:59 +00001119/// isFoldableLoad - Return true if this is a load instruction that can safely
1120/// be folded into an operation that uses it.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001121bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001122 if (Op.getOpcode() != ISD::LOAD ||
1123 // FIXME: currently can't fold constant pool indexes.
1124 isa<ConstantPoolSDNode>(Op.getOperand(1)))
1125 return false;
1126
1127 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001128 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1129 if (ExprMap.count(Op.getValue(1))) return false;
1130 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001131 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001132
Chris Lattner4ff348b2005-01-17 06:26:58 +00001133 // If there is not just one use of its value, we cannot fold.
1134 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1135
1136 // Finally, we cannot fold the load into the operation if this would induce a
1137 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1138 // operand of the operation we are folding the load into) can possible use the
1139 // chain node defined by the load.
1140 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1141 std::set<SDNode*> Visited;
1142 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1143 return false;
1144 }
1145 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001146}
1147
Chris Lattner4ff348b2005-01-17 06:26:58 +00001148
Chris Lattnera5ade062005-01-11 21:19:59 +00001149/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1150/// and compute the address being loaded into AM.
1151void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1152 SDOperand Chain = Op.getOperand(0);
1153 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001154
Chris Lattnera5ade062005-01-11 21:19:59 +00001155 if (getRegPressure(Chain) > getRegPressure(Address)) {
1156 Select(Chain);
1157 SelectAddress(Address, AM);
1158 } else {
1159 SelectAddress(Address, AM);
1160 Select(Chain);
1161 }
1162
1163 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001164 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1165 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001166 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001167 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001168}
1169
Chris Lattner30ea1e92005-01-19 07:37:26 +00001170// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1171// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1172// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1173// return true.
1174bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001175 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1176 // good!
1177 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1178 std::swap(Op1, Op2); // Op1 is the SHL now.
1179 } else {
1180 return false; // No match
1181 }
1182
1183 SDOperand ShlVal = Op1.getOperand(0);
1184 SDOperand ShlAmt = Op1.getOperand(1);
1185 SDOperand ShrVal = Op2.getOperand(0);
1186 SDOperand ShrAmt = Op2.getOperand(1);
1187
Chris Lattner30ea1e92005-01-19 07:37:26 +00001188 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1189
Chris Lattner85716372005-01-19 06:18:43 +00001190 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1191 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1192 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001193 if (SubCST->getValue() == RegSize) {
1194 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001195 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001196 if (ShrVal == ShlVal) {
1197 unsigned Reg, ShAmt;
1198 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1199 Reg = SelectExpr(ShrVal);
1200 ShAmt = SelectExpr(ShrAmt);
1201 } else {
1202 ShAmt = SelectExpr(ShrAmt);
1203 Reg = SelectExpr(ShrVal);
1204 }
1205 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1206 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1207 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1208 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1209 return true;
1210 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001211 unsigned AReg, BReg;
1212 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001213 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001214 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001215 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001216 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001217 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001218 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001219 unsigned ShAmt = SelectExpr(ShrAmt);
1220 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1221 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1222 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001223 return true;
1224 }
1225 }
1226
Chris Lattner4053b1e2005-01-19 08:07:05 +00001227 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1228 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1229 if (SubCST->getValue() == RegSize) {
1230 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1231 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1232 if (ShrVal == ShlVal) {
1233 unsigned Reg, ShAmt;
1234 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1235 Reg = SelectExpr(ShrVal);
1236 ShAmt = SelectExpr(ShlAmt);
1237 } else {
1238 ShAmt = SelectExpr(ShlAmt);
1239 Reg = SelectExpr(ShrVal);
1240 }
1241 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1242 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1243 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1244 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1245 return true;
1246 } else if (RegSize != 8) {
1247 unsigned AReg, BReg;
1248 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001249 AReg = SelectExpr(ShlVal);
1250 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001251 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001252 BReg = SelectExpr(ShrVal);
1253 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001254 }
1255 unsigned ShAmt = SelectExpr(ShlAmt);
1256 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1257 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1258 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1259 return true;
1260 }
1261 }
Chris Lattner85716372005-01-19 06:18:43 +00001262
Chris Lattner4053b1e2005-01-19 08:07:05 +00001263 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1264 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1265 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1266 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1267 // (A >> 5) | (A << 27) --> ROR A, 5
1268 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1269 if (ShrVal == ShlVal) {
1270 unsigned Reg = SelectExpr(ShrVal);
1271 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1272 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1273 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1274 return true;
1275 } else if (RegSize != 8) {
1276 unsigned AReg, BReg;
1277 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001278 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001279 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001280 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001281 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001282 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001283 }
1284 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1285 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1286 .addImm(ShrCst->getValue());
1287 return true;
1288 }
1289 }
1290
Chris Lattner85716372005-01-19 06:18:43 +00001291 return false;
1292}
1293
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001294unsigned ISel::SelectExpr(SDOperand N) {
1295 unsigned Result;
1296 unsigned Tmp1, Tmp2, Tmp3;
1297 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001298 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001299 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001300
Chris Lattner7f2afac2005-01-14 22:37:41 +00001301 if (Node->getOpcode() == ISD::CopyFromReg) {
1302 // FIXME: Handle copy from physregs!
1303
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001304 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001305 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001306 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001307
1308 unsigned &Reg = ExprMap[N];
1309 if (Reg) return Reg;
1310
Chris Lattner19ad0622005-01-20 18:53:00 +00001311 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
1312 N.getOpcode() != ISD::SUB_PARTS)
Chris Lattnera5ade062005-01-11 21:19:59 +00001313 Reg = Result = (N.getValueType() != MVT::Other) ?
1314 MakeReg(N.getValueType()) : 1;
1315 else {
1316 // If this is a call instruction, make sure to prepare ALL of the result
1317 // values as well as the chain.
Chris Lattner19ad0622005-01-20 18:53:00 +00001318 if (N.getOpcode() == ISD::CALL) {
1319 if (Node->getNumValues() == 1)
1320 Reg = Result = 1; // Void call, just a chain.
1321 else {
1322 Result = MakeReg(Node->getValueType(0));
1323 ExprMap[N.getValue(0)] = Result;
1324 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1325 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1326 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
1327 }
1328 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001329 Result = MakeReg(Node->getValueType(0));
1330 ExprMap[N.getValue(0)] = Result;
Chris Lattner19ad0622005-01-20 18:53:00 +00001331 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001332 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001333 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001334 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001335
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001336 switch (N.getOpcode()) {
1337 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001338 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001339 assert(0 && "Node not handled!\n");
1340 case ISD::FrameIndex:
1341 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1342 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1343 return Result;
1344 case ISD::ConstantPool:
1345 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1346 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1347 return Result;
1348 case ISD::ConstantFP:
1349 ContainsFPCode = true;
1350 Tmp1 = Result; // Intermediate Register
1351 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1352 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1353 Tmp1 = MakeReg(MVT::f64);
1354
1355 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1356 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1357 BuildMI(BB, X86::FLD0, 0, Tmp1);
1358 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1359 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1360 BuildMI(BB, X86::FLD1, 0, Tmp1);
1361 else
1362 assert(0 && "Unexpected constant!");
1363 if (Tmp1 != Result)
1364 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1365 return Result;
1366 case ISD::Constant:
1367 switch (N.getValueType()) {
1368 default: assert(0 && "Cannot use constants of this type!");
1369 case MVT::i1:
1370 case MVT::i8: Opc = X86::MOV8ri; break;
1371 case MVT::i16: Opc = X86::MOV16ri; break;
1372 case MVT::i32: Opc = X86::MOV32ri; break;
1373 }
1374 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1375 return Result;
1376 case ISD::GlobalAddress: {
1377 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1378 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1379 return Result;
1380 }
1381 case ISD::ExternalSymbol: {
1382 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1383 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1384 return Result;
1385 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001386 case ISD::ZERO_EXTEND: {
1387 int DestIs16 = N.getValueType() == MVT::i16;
1388 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001389
1390 // FIXME: This hack is here for zero extension casts from bool to i8. This
1391 // would not be needed if bools were promoted by Legalize.
1392 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001393 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001394 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1395 return Result;
1396 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001397
Chris Lattner4ff348b2005-01-17 06:26:58 +00001398 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001399 static const unsigned Opc[3] = {
1400 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1401 };
1402
1403 X86AddressMode AM;
1404 EmitFoldedLoad(N.getOperand(0), AM);
1405 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1406
1407 return Result;
1408 }
1409
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001410 static const unsigned Opc[3] = {
1411 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1412 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001413 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001414 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1415 return Result;
1416 }
1417 case ISD::SIGN_EXTEND: {
1418 int DestIs16 = N.getValueType() == MVT::i16;
1419 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1420
Chris Lattner590d8002005-01-09 18:52:44 +00001421 // FIXME: Legalize should promote bools to i8!
1422 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1423 "Sign extend from bool not implemented!");
1424
Chris Lattner4ff348b2005-01-17 06:26:58 +00001425 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001426 static const unsigned Opc[3] = {
1427 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1428 };
1429
1430 X86AddressMode AM;
1431 EmitFoldedLoad(N.getOperand(0), AM);
1432 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1433 return Result;
1434 }
1435
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001436 static const unsigned Opc[3] = {
1437 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1438 };
1439 Tmp1 = SelectExpr(N.getOperand(0));
1440 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1441 return Result;
1442 }
1443 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001444 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001445 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001446 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001447 switch (N.getValueType()) {
1448 default: assert(0 && "Unknown truncate!");
1449 case MVT::i1:
1450 case MVT::i8: Opc = X86::MOV8rm; break;
1451 case MVT::i16: Opc = X86::MOV16rm; break;
1452 }
1453 X86AddressMode AM;
1454 EmitFoldedLoad(N.getOperand(0), AM);
1455 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1456 return Result;
1457 }
1458
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001459 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1460 // a move out of AX or AL.
1461 switch (N.getOperand(0).getValueType()) {
1462 default: assert(0 && "Unknown truncate!");
1463 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1464 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1465 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1466 }
1467 Tmp1 = SelectExpr(N.getOperand(0));
1468 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1469
1470 switch (N.getValueType()) {
1471 default: assert(0 && "Unknown truncate!");
1472 case MVT::i1:
1473 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1474 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1475 }
1476 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1477 return Result;
1478
Chris Lattner590d8002005-01-09 18:52:44 +00001479 case ISD::SINT_TO_FP:
1480 case ISD::UINT_TO_FP: {
1481 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001482 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001483
1484 // Promote the integer to a type supported by FLD. We do this because there
1485 // are no unsigned FLD instructions, so we must promote an unsigned value to
1486 // a larger signed value, then use FLD on the larger value.
1487 //
1488 MVT::ValueType PromoteType = MVT::Other;
1489 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1490 unsigned PromoteOpcode = 0;
1491 unsigned RealDestReg = Result;
1492 switch (SrcTy) {
1493 case MVT::i1:
1494 case MVT::i8:
1495 // We don't have the facilities for directly loading byte sized data from
1496 // memory (even signed). Promote it to 16 bits.
1497 PromoteType = MVT::i16;
1498 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1499 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1500 break;
1501 case MVT::i16:
1502 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1503 PromoteType = MVT::i32;
1504 PromoteOpcode = X86::MOVZX32rr16;
1505 }
1506 break;
1507 default:
1508 // Don't fild into the real destination.
1509 if (Node->getOpcode() == ISD::UINT_TO_FP)
1510 Result = MakeReg(Node->getValueType(0));
1511 break;
1512 }
1513
1514 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1515
1516 if (PromoteType != MVT::Other) {
1517 Tmp2 = MakeReg(PromoteType);
1518 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1519 SrcTy = PromoteType;
1520 Tmp1 = Tmp2;
1521 }
1522
1523 // Spill the integer to memory and reload it from there.
1524 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1525 MachineFunction *F = BB->getParent();
1526 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1527
1528 switch (SrcTy) {
1529 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001530 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001531 // FIXME: this won't work for cast [u]long to FP
1532 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1533 FrameIdx).addReg(Tmp1);
1534 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1535 FrameIdx, 4).addReg(Tmp1+1);
1536 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1537 break;
1538 case MVT::i32:
1539 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1540 FrameIdx).addReg(Tmp1);
1541 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1542 break;
1543 case MVT::i16:
1544 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1545 FrameIdx).addReg(Tmp1);
1546 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1547 break;
1548 default: break; // No promotion required.
1549 }
1550
Chris Lattner085c9952005-01-12 04:00:00 +00001551 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001552 // If this is a cast from uint -> double, we need to be careful when if
1553 // the "sign" bit is set. If so, we don't want to make a negative number,
1554 // we want to make a positive number. Emit code to add an offset if the
1555 // sign bit is set.
1556
1557 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1558 unsigned IsNeg = MakeReg(MVT::i32);
1559 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1560
1561 // Create a CP value that has the offset in one word and 0 in the other.
1562 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1563 0x4f80000000000000ULL);
1564 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1565 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1566 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1567
1568 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1569 // We need special handling for unsigned 64-bit integer sources. If the
1570 // input number has the "sign bit" set, then we loaded it incorrectly as a
1571 // negative 64-bit number. In this case, add an offset value.
1572
1573 // Emit a test instruction to see if the dynamic input value was signed.
1574 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1575
1576 // If the sign bit is set, get a pointer to an offset, otherwise get a
1577 // pointer to a zero.
1578 MachineConstantPool *CP = F->getConstantPool();
1579 unsigned Zero = MakeReg(MVT::i32);
1580 Constant *Null = Constant::getNullValue(Type::UIntTy);
1581 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1582 CP->getConstantPoolIndex(Null));
1583 unsigned Offset = MakeReg(MVT::i32);
1584 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1585
1586 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1587 CP->getConstantPoolIndex(OffsetCst));
1588 unsigned Addr = MakeReg(MVT::i32);
1589 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1590
1591 // Load the constant for an add. FIXME: this could make an 'fadd' that
1592 // reads directly from memory, but we don't support these yet.
1593 unsigned ConstReg = MakeReg(MVT::f64);
1594 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1595
1596 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1597 }
1598 return RealDestReg;
1599 }
1600 case ISD::FP_TO_SINT:
1601 case ISD::FP_TO_UINT: {
1602 // FIXME: Most of this grunt work should be done by legalize!
1603 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1604
1605 // Change the floating point control register to use "round towards zero"
1606 // mode when truncating to an integer value.
1607 //
1608 MachineFunction *F = BB->getParent();
1609 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1610 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1611
1612 // Load the old value of the high byte of the control word...
1613 unsigned HighPartOfCW = MakeReg(MVT::i8);
1614 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1615 CWFrameIdx, 1);
1616
1617 // Set the high part to be round to zero...
1618 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1619 CWFrameIdx, 1).addImm(12);
1620
1621 // Reload the modified control word now...
1622 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1623
1624 // Restore the memory image of control word to original value
1625 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1626 CWFrameIdx, 1).addReg(HighPartOfCW);
1627
1628 // We don't have the facilities for directly storing byte sized data to
1629 // memory. Promote it to 16 bits. We also must promote unsigned values to
1630 // larger classes because we only have signed FP stores.
1631 MVT::ValueType StoreClass = Node->getValueType(0);
1632 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1633 switch (StoreClass) {
1634 case MVT::i8: StoreClass = MVT::i16; break;
1635 case MVT::i16: StoreClass = MVT::i32; break;
1636 case MVT::i32: StoreClass = MVT::i64; break;
1637 // The following treatment of cLong may not be perfectly right,
1638 // but it survives chains of casts of the form
1639 // double->ulong->double.
1640 case MVT::i64: StoreClass = MVT::i64; break;
1641 default: assert(0 && "Unknown store class!");
1642 }
1643
1644 // Spill the integer to memory and reload it from there.
1645 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1646 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1647
1648 switch (StoreClass) {
1649 default: assert(0 && "Unknown store class!");
1650 case MVT::i16:
1651 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1652 break;
1653 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001654 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001655 break;
1656 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001657 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001658 break;
1659 }
1660
1661 switch (Node->getValueType(0)) {
1662 default:
1663 assert(0 && "Unknown integer type!");
1664 case MVT::i64:
1665 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001666 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001667 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1668 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1669 case MVT::i32:
1670 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1671 break;
1672 case MVT::i16:
1673 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1674 break;
1675 case MVT::i8:
1676 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1677 break;
1678 }
1679
1680 // Reload the original control word now.
1681 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1682 return Result;
1683 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001684 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001685 Op0 = N.getOperand(0);
1686 Op1 = N.getOperand(1);
1687
Chris Lattner4ff348b2005-01-17 06:26:58 +00001688 if (isFoldableLoad(Op0, Op1)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001689 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001690 goto FoldAdd;
1691 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001692
Chris Lattner4ff348b2005-01-17 06:26:58 +00001693 if (isFoldableLoad(Op1, Op0)) {
1694 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001695 switch (N.getValueType()) {
1696 default: assert(0 && "Cannot add this type!");
1697 case MVT::i1:
1698 case MVT::i8: Opc = X86::ADD8rm; break;
1699 case MVT::i16: Opc = X86::ADD16rm; break;
1700 case MVT::i32: Opc = X86::ADD32rm; break;
1701 case MVT::f32: Opc = X86::FADD32m; break;
1702 case MVT::f64: Opc = X86::FADD64m; break;
1703 }
1704 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001705 EmitFoldedLoad(Op1, AM);
1706 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001707 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1708 return Result;
1709 }
1710
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001711 // See if we can codegen this as an LEA to fold operations together.
1712 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001713 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001714 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001715 MatchAddress(N, AM);
1716 ExprMap[N] = Result;
1717
1718 // If this is not just an add, emit the LEA. For a simple add (like
1719 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1720 // leave this as LEA, then peephole it to 'ADD' after two address elim
1721 // happens.
1722 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1723 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1724 X86AddressMode XAM = SelectAddrExprs(AM);
1725 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1726 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001727 }
1728 }
Chris Lattner11333092005-01-11 03:11:44 +00001729
Chris Lattnera5ade062005-01-11 21:19:59 +00001730 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001731 Opc = 0;
1732 if (CN->getValue() == 1) { // add X, 1 -> inc X
1733 switch (N.getValueType()) {
1734 default: assert(0 && "Cannot integer add this type!");
1735 case MVT::i8: Opc = X86::INC8r; break;
1736 case MVT::i16: Opc = X86::INC16r; break;
1737 case MVT::i32: Opc = X86::INC32r; break;
1738 }
1739 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1740 switch (N.getValueType()) {
1741 default: assert(0 && "Cannot integer add this type!");
1742 case MVT::i8: Opc = X86::DEC8r; break;
1743 case MVT::i16: Opc = X86::DEC16r; break;
1744 case MVT::i32: Opc = X86::DEC32r; break;
1745 }
1746 }
1747
1748 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001749 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001750 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1751 return Result;
1752 }
1753
1754 switch (N.getValueType()) {
1755 default: assert(0 && "Cannot add this type!");
1756 case MVT::i8: Opc = X86::ADD8ri; break;
1757 case MVT::i16: Opc = X86::ADD16ri; break;
1758 case MVT::i32: Opc = X86::ADD32ri; break;
1759 }
1760 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001761 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001762 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1763 return Result;
1764 }
1765 }
1766
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001767 switch (N.getValueType()) {
1768 default: assert(0 && "Cannot add this type!");
1769 case MVT::i8: Opc = X86::ADD8rr; break;
1770 case MVT::i16: Opc = X86::ADD16rr; break;
1771 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001772 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001773 }
Chris Lattner11333092005-01-11 03:11:44 +00001774
Chris Lattnera5ade062005-01-11 21:19:59 +00001775 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1776 Tmp1 = SelectExpr(Op0);
1777 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001778 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001779 Tmp2 = SelectExpr(Op1);
1780 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001781 }
1782
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001783 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1784 return Result;
1785 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001786 case ISD::MUL:
1787 case ISD::AND:
1788 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001789 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001790 static const unsigned SUBTab[] = {
1791 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1792 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1793 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1794 };
1795 static const unsigned MULTab[] = {
1796 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1797 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1798 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1799 };
1800 static const unsigned ANDTab[] = {
1801 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1802 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1803 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1804 };
1805 static const unsigned ORTab[] = {
1806 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1807 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1808 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1809 };
1810 static const unsigned XORTab[] = {
1811 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1812 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1813 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1814 };
1815
1816 Op0 = Node->getOperand(0);
1817 Op1 = Node->getOperand(1);
1818
Chris Lattner30ea1e92005-01-19 07:37:26 +00001819 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1820 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001821 return Result;
1822
1823 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001824 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1825 if (CN->isNullValue()) { // 0 - N -> neg N
1826 switch (N.getValueType()) {
1827 default: assert(0 && "Cannot sub this type!");
1828 case MVT::i1:
1829 case MVT::i8: Opc = X86::NEG8r; break;
1830 case MVT::i16: Opc = X86::NEG16r; break;
1831 case MVT::i32: Opc = X86::NEG32r; break;
1832 }
1833 Tmp1 = SelectExpr(N.getOperand(1));
1834 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1835 return Result;
1836 }
1837
Chris Lattnera5ade062005-01-11 21:19:59 +00001838 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1839 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001840 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001841 switch (N.getValueType()) {
1842 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001843 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001844 case MVT::i8: Opc = X86::NOT8r; break;
1845 case MVT::i16: Opc = X86::NOT16r; break;
1846 case MVT::i32: Opc = X86::NOT32r; break;
1847 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001848 if (Opc) {
1849 Tmp1 = SelectExpr(Op0);
1850 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1851 return Result;
1852 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001853 }
1854
Chris Lattner2a4e5082005-01-17 06:48:02 +00001855 // Fold common multiplies into LEA instructions.
1856 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1857 switch ((int)CN->getValue()) {
1858 default: break;
1859 case 3:
1860 case 5:
1861 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001862 // Remove N from exprmap so SelectAddress doesn't get confused.
1863 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001864 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001865 SelectAddress(N, AM);
1866 // Restore it to the map.
1867 ExprMap[N] = Result;
1868 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1869 return Result;
1870 }
1871 }
1872
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001873 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001874 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001875 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001876 case MVT::i8: Opc = 0; break;
1877 case MVT::i16: Opc = 1; break;
1878 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001879 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001880 switch (Node->getOpcode()) {
1881 default: assert(0 && "Unreachable!");
1882 case ISD::SUB: Opc = SUBTab[Opc]; break;
1883 case ISD::MUL: Opc = MULTab[Opc]; break;
1884 case ISD::AND: Opc = ANDTab[Opc]; break;
1885 case ISD::OR: Opc = ORTab[Opc]; break;
1886 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001887 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001888 if (Opc) { // Can't fold MUL:i8 R, imm
1889 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001890 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1891 return Result;
1892 }
1893 }
Chris Lattner11333092005-01-11 03:11:44 +00001894
Chris Lattner4ff348b2005-01-17 06:26:58 +00001895 if (isFoldableLoad(Op0, Op1))
Chris Lattnera5ade062005-01-11 21:19:59 +00001896 if (Node->getOpcode() != ISD::SUB) {
1897 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001898 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001899 } else {
1900 // Emit 'reverse' subract, with a memory operand.
1901 switch (N.getValueType()) {
1902 default: Opc = 0; break;
1903 case MVT::f32: Opc = X86::FSUBR32m; break;
1904 case MVT::f64: Opc = X86::FSUBR64m; break;
1905 }
1906 if (Opc) {
1907 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001908 EmitFoldedLoad(Op0, AM);
1909 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001910 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1911 return Result;
1912 }
1913 }
1914
Chris Lattner4ff348b2005-01-17 06:26:58 +00001915 if (isFoldableLoad(Op1, Op0)) {
1916 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001917 switch (N.getValueType()) {
1918 default: assert(0 && "Cannot operate on this type!");
1919 case MVT::i1:
1920 case MVT::i8: Opc = 5; break;
1921 case MVT::i16: Opc = 6; break;
1922 case MVT::i32: Opc = 7; break;
1923 case MVT::f32: Opc = 8; break;
1924 case MVT::f64: Opc = 9; break;
1925 }
1926 switch (Node->getOpcode()) {
1927 default: assert(0 && "Unreachable!");
1928 case ISD::SUB: Opc = SUBTab[Opc]; break;
1929 case ISD::MUL: Opc = MULTab[Opc]; break;
1930 case ISD::AND: Opc = ANDTab[Opc]; break;
1931 case ISD::OR: Opc = ORTab[Opc]; break;
1932 case ISD::XOR: Opc = XORTab[Opc]; break;
1933 }
1934
1935 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001936 EmitFoldedLoad(Op1, AM);
1937 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001938 if (Opc) {
1939 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1940 } else {
1941 assert(Node->getOpcode() == ISD::MUL &&
1942 N.getValueType() == MVT::i8 && "Unexpected situation!");
1943 // Must use the MUL instruction, which forces use of AL.
1944 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1945 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1946 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1947 }
1948 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001949 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001950
1951 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1952 Tmp1 = SelectExpr(Op0);
1953 Tmp2 = SelectExpr(Op1);
1954 } else {
1955 Tmp2 = SelectExpr(Op1);
1956 Tmp1 = SelectExpr(Op0);
1957 }
1958
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001959 switch (N.getValueType()) {
1960 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001961 case MVT::i1:
1962 case MVT::i8: Opc = 10; break;
1963 case MVT::i16: Opc = 11; break;
1964 case MVT::i32: Opc = 12; break;
1965 case MVT::f32: Opc = 13; break;
1966 case MVT::f64: Opc = 14; break;
1967 }
1968 switch (Node->getOpcode()) {
1969 default: assert(0 && "Unreachable!");
1970 case ISD::SUB: Opc = SUBTab[Opc]; break;
1971 case ISD::MUL: Opc = MULTab[Opc]; break;
1972 case ISD::AND: Opc = ANDTab[Opc]; break;
1973 case ISD::OR: Opc = ORTab[Opc]; break;
1974 case ISD::XOR: Opc = XORTab[Opc]; break;
1975 }
1976 if (Opc) {
1977 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1978 } else {
1979 assert(Node->getOpcode() == ISD::MUL &&
1980 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001981 // Must use the MUL instruction, which forces use of AL.
1982 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1983 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1984 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001985 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001986 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001987 }
Chris Lattner19ad0622005-01-20 18:53:00 +00001988 case ISD::ADD_PARTS:
1989 case ISD::SUB_PARTS: {
1990 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1991 "Not an i64 add/sub!");
1992 // Emit all of the operands.
1993 std::vector<unsigned> InVals;
1994 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1995 InVals.push_back(SelectExpr(N.getOperand(i)));
1996 if (N.getOpcode() == ISD::ADD_PARTS) {
1997 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1998 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1999 } else {
2000 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2001 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2002 }
2003 return Result+N.ResNo;
2004 }
2005
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002006 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002007 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2008 Tmp2 = SelectExpr(N.getOperand(1));
2009 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002010 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002011 Tmp3 = SelectExpr(N.getOperand(2));
2012 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002013 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002014 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2015 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002016
2017 case ISD::SDIV:
2018 case ISD::UDIV:
2019 case ISD::SREM:
2020 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002021 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2022 "We don't support this operator!");
2023
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002024 if (N.getOpcode() == ISD::SDIV)
2025 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2026 // FIXME: These special cases should be handled by the lowering impl!
2027 unsigned RHS = CN->getValue();
2028 bool isNeg = false;
2029 if ((int)RHS < 0) {
2030 isNeg = true;
2031 RHS = -RHS;
2032 }
2033 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2034 unsigned Log = log2(RHS);
2035 unsigned TmpReg = MakeReg(N.getValueType());
2036 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2037 switch (N.getValueType()) {
2038 default: assert("Unknown type to signed divide!");
2039 case MVT::i8:
2040 SAROpc = X86::SAR8ri;
2041 SHROpc = X86::SHR8ri;
2042 ADDOpc = X86::ADD8rr;
2043 NEGOpc = X86::NEG8r;
2044 break;
2045 case MVT::i16:
2046 SAROpc = X86::SAR16ri;
2047 SHROpc = X86::SHR16ri;
2048 ADDOpc = X86::ADD16rr;
2049 NEGOpc = X86::NEG16r;
2050 break;
2051 case MVT::i32:
2052 SAROpc = X86::SAR32ri;
2053 SHROpc = X86::SHR32ri;
2054 ADDOpc = X86::ADD32rr;
2055 NEGOpc = X86::NEG32r;
2056 break;
2057 }
Chris Lattner11333092005-01-11 03:11:44 +00002058 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002059 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2060 unsigned TmpReg2 = MakeReg(N.getValueType());
2061 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2062 unsigned TmpReg3 = MakeReg(N.getValueType());
2063 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
2064
2065 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2066 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2067 if (isNeg)
2068 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2069 return Result;
2070 }
2071 }
2072
Chris Lattner11333092005-01-11 03:11:44 +00002073 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2074 Tmp1 = SelectExpr(N.getOperand(0));
2075 Tmp2 = SelectExpr(N.getOperand(1));
2076 } else {
2077 Tmp2 = SelectExpr(N.getOperand(1));
2078 Tmp1 = SelectExpr(N.getOperand(0));
2079 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002080
2081 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2082 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2083 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2084 switch (N.getValueType()) {
2085 default: assert(0 && "Cannot sdiv this type!");
2086 case MVT::i8:
2087 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2088 LoReg = X86::AL;
2089 HiReg = X86::AH;
2090 MovOpcode = X86::MOV8rr;
2091 ClrOpcode = X86::MOV8ri;
2092 SExtOpcode = X86::CBW;
2093 break;
2094 case MVT::i16:
2095 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2096 LoReg = X86::AX;
2097 HiReg = X86::DX;
2098 MovOpcode = X86::MOV16rr;
2099 ClrOpcode = X86::MOV16ri;
2100 SExtOpcode = X86::CWD;
2101 break;
2102 case MVT::i32:
2103 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002104 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002105 HiReg = X86::EDX;
2106 MovOpcode = X86::MOV32rr;
2107 ClrOpcode = X86::MOV32ri;
2108 SExtOpcode = X86::CDQ;
2109 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002110 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002111 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002112 return Result;
2113 }
2114
2115 // Set up the low part.
2116 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2117
2118 if (isSigned) {
2119 // Sign extend the low part into the high part.
2120 BuildMI(BB, SExtOpcode, 0);
2121 } else {
2122 // Zero out the high part, effectively zero extending the input.
2123 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2124 }
2125
2126 // Emit the DIV/IDIV instruction.
2127 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
2128
2129 // Get the result of the divide or rem.
2130 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2131 return Result;
2132 }
2133
2134 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002135 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002136 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2137 switch (N.getValueType()) {
2138 default: assert(0 && "Cannot shift this type!");
2139 case MVT::i8: Opc = X86::ADD8rr; break;
2140 case MVT::i16: Opc = X86::ADD16rr; break;
2141 case MVT::i32: Opc = X86::ADD32rr; break;
2142 }
2143 Tmp1 = SelectExpr(N.getOperand(0));
2144 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2145 return Result;
2146 }
2147
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002148 switch (N.getValueType()) {
2149 default: assert(0 && "Cannot shift this type!");
2150 case MVT::i8: Opc = X86::SHL8ri; break;
2151 case MVT::i16: Opc = X86::SHL16ri; break;
2152 case MVT::i32: Opc = X86::SHL32ri; break;
2153 }
Chris Lattner11333092005-01-11 03:11:44 +00002154 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002155 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2156 return Result;
2157 }
Chris Lattner11333092005-01-11 03:11:44 +00002158
2159 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2160 Tmp1 = SelectExpr(N.getOperand(0));
2161 Tmp2 = SelectExpr(N.getOperand(1));
2162 } else {
2163 Tmp2 = SelectExpr(N.getOperand(1));
2164 Tmp1 = SelectExpr(N.getOperand(0));
2165 }
2166
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002167 switch (N.getValueType()) {
2168 default: assert(0 && "Cannot shift this type!");
2169 case MVT::i8 : Opc = X86::SHL8rCL; break;
2170 case MVT::i16: Opc = X86::SHL16rCL; break;
2171 case MVT::i32: Opc = X86::SHL32rCL; break;
2172 }
2173 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2174 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2175 return Result;
2176 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002177 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2178 switch (N.getValueType()) {
2179 default: assert(0 && "Cannot shift this type!");
2180 case MVT::i8: Opc = X86::SHR8ri; break;
2181 case MVT::i16: Opc = X86::SHR16ri; break;
2182 case MVT::i32: Opc = X86::SHR32ri; break;
2183 }
Chris Lattner11333092005-01-11 03:11:44 +00002184 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002185 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2186 return Result;
2187 }
Chris Lattner11333092005-01-11 03:11:44 +00002188
2189 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2190 Tmp1 = SelectExpr(N.getOperand(0));
2191 Tmp2 = SelectExpr(N.getOperand(1));
2192 } else {
2193 Tmp2 = SelectExpr(N.getOperand(1));
2194 Tmp1 = SelectExpr(N.getOperand(0));
2195 }
2196
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002197 switch (N.getValueType()) {
2198 default: assert(0 && "Cannot shift this type!");
2199 case MVT::i8 : Opc = X86::SHR8rCL; break;
2200 case MVT::i16: Opc = X86::SHR16rCL; break;
2201 case MVT::i32: Opc = X86::SHR32rCL; break;
2202 }
2203 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2204 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2205 return Result;
2206 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002207 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2208 switch (N.getValueType()) {
2209 default: assert(0 && "Cannot shift this type!");
2210 case MVT::i8: Opc = X86::SAR8ri; break;
2211 case MVT::i16: Opc = X86::SAR16ri; break;
2212 case MVT::i32: Opc = X86::SAR32ri; break;
2213 }
Chris Lattner11333092005-01-11 03:11:44 +00002214 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002215 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2216 return Result;
2217 }
Chris Lattner11333092005-01-11 03:11:44 +00002218
2219 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2220 Tmp1 = SelectExpr(N.getOperand(0));
2221 Tmp2 = SelectExpr(N.getOperand(1));
2222 } else {
2223 Tmp2 = SelectExpr(N.getOperand(1));
2224 Tmp1 = SelectExpr(N.getOperand(0));
2225 }
2226
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002227 switch (N.getValueType()) {
2228 default: assert(0 && "Cannot shift this type!");
2229 case MVT::i8 : Opc = X86::SAR8rCL; break;
2230 case MVT::i16: Opc = X86::SAR16rCL; break;
2231 case MVT::i32: Opc = X86::SAR32rCL; break;
2232 }
2233 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2234 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2235 return Result;
2236
2237 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002238 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002239 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2240 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2241 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002242 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002243 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002244 if (Result != 1) { // Generate the token
2245 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2246 assert(0 && "Load already emitted!?");
2247 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002248 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2249
Chris Lattner5188ad72005-01-08 19:28:19 +00002250 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002251 default: assert(0 && "Cannot load this type!");
2252 case MVT::i1:
2253 case MVT::i8: Opc = X86::MOV8rm; break;
2254 case MVT::i16: Opc = X86::MOV16rm; break;
2255 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002256 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2257 }
Chris Lattner11333092005-01-11 03:11:44 +00002258
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002259 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002260 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002261 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2262 } else {
2263 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002264
2265 SDOperand Chain = N.getOperand(0);
2266 SDOperand Address = N.getOperand(1);
2267 if (getRegPressure(Chain) > getRegPressure(Address)) {
2268 Select(Chain);
2269 SelectAddress(Address, AM);
2270 } else {
2271 SelectAddress(Address, AM);
2272 Select(Chain);
2273 }
2274
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002275 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2276 }
2277 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002278
2279 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2280 case ISD::ZEXTLOAD: {
2281 // Make sure we generate both values.
2282 if (Result != 1)
2283 ExprMap[N.getValue(1)] = 1; // Generate the token
2284 else
2285 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2286
Chris Lattnerda2ce112005-01-16 07:34:08 +00002287 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2288 if (Node->getValueType(0) == MVT::f64) {
2289 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2290 "Bad EXTLOAD!");
2291 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2292 CP->getIndex());
2293 return Result;
2294 }
2295
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002296 X86AddressMode AM;
2297 if (getRegPressure(Node->getOperand(0)) >
2298 getRegPressure(Node->getOperand(1))) {
2299 Select(Node->getOperand(0)); // chain
2300 SelectAddress(Node->getOperand(1), AM);
2301 } else {
2302 SelectAddress(Node->getOperand(1), AM);
2303 Select(Node->getOperand(0)); // chain
2304 }
2305
2306 switch (Node->getValueType(0)) {
2307 default: assert(0 && "Unknown type to sign extend to.");
2308 case MVT::f64:
2309 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2310 "Bad EXTLOAD!");
2311 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2312 break;
2313 case MVT::i32:
2314 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2315 default:
2316 assert(0 && "Bad zero extend!");
2317 case MVT::i1:
2318 case MVT::i8:
2319 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2320 break;
2321 case MVT::i16:
2322 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2323 break;
2324 }
2325 break;
2326 case MVT::i16:
2327 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2328 "Bad zero extend!");
2329 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2330 break;
2331 case MVT::i8:
2332 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2333 "Bad zero extend!");
2334 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2335 break;
2336 }
2337 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002338 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002339 case ISD::SEXTLOAD: {
2340 // Make sure we generate both values.
2341 if (Result != 1)
2342 ExprMap[N.getValue(1)] = 1; // Generate the token
2343 else
2344 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2345
2346 X86AddressMode AM;
2347 if (getRegPressure(Node->getOperand(0)) >
2348 getRegPressure(Node->getOperand(1))) {
2349 Select(Node->getOperand(0)); // chain
2350 SelectAddress(Node->getOperand(1), AM);
2351 } else {
2352 SelectAddress(Node->getOperand(1), AM);
2353 Select(Node->getOperand(0)); // chain
2354 }
2355
2356 switch (Node->getValueType(0)) {
2357 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2358 default: assert(0 && "Unknown type to sign extend to.");
2359 case MVT::i32:
2360 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2361 default:
2362 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2363 case MVT::i8:
2364 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2365 break;
2366 case MVT::i16:
2367 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2368 break;
2369 }
2370 break;
2371 case MVT::i16:
2372 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2373 "Cannot sign extend from bool!");
2374 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2375 break;
2376 }
2377 return Result;
2378 }
2379
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002380 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002381 // Generate both result values.
2382 if (Result != 1)
2383 ExprMap[N.getValue(1)] = 1; // Generate the token
2384 else
2385 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2386
2387 // FIXME: We are currently ignoring the requested alignment for handling
2388 // greater than the stack alignment. This will need to be revisited at some
2389 // point. Align = N.getOperand(2);
2390
2391 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2392 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2393 std::cerr << "Cannot allocate stack object with greater alignment than"
2394 << " the stack alignment yet!";
2395 abort();
2396 }
2397
2398 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002399 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002400 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2401 .addImm(CN->getValue());
2402 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002403 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2404 Select(N.getOperand(0));
2405 Tmp1 = SelectExpr(N.getOperand(1));
2406 } else {
2407 Tmp1 = SelectExpr(N.getOperand(1));
2408 Select(N.getOperand(0));
2409 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002410
2411 // Subtract size from stack pointer, thereby allocating some space.
2412 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2413 }
2414
2415 // Put a pointer to the space into the result register, by copying the stack
2416 // pointer.
2417 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2418 return Result;
2419
2420 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002421 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002422 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002423
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002424 if (GlobalAddressSDNode *GASD =
2425 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002426 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002427 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2428 } else if (ExternalSymbolSDNode *ESSDN =
2429 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002430 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002431 BuildMI(BB, X86::CALLpcrel32,
2432 1).addExternalSymbol(ESSDN->getSymbol(), true);
2433 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002434 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2435 Select(N.getOperand(0));
2436 Tmp1 = SelectExpr(N.getOperand(1));
2437 } else {
2438 Tmp1 = SelectExpr(N.getOperand(1));
2439 Select(N.getOperand(0));
2440 }
2441
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002442 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2443 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002444 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002445 default: assert(0 && "Unknown value type for call result!");
2446 case MVT::Other: return 1;
2447 case MVT::i1:
2448 case MVT::i8:
2449 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2450 break;
2451 case MVT::i16:
2452 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2453 break;
2454 case MVT::i32:
2455 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002456 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002457 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2458 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002459 case MVT::f64: // Floating-point return values live in %ST(0)
2460 ContainsFPCode = true;
2461 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2462 break;
2463 }
2464 return Result+N.ResNo;
2465 }
2466
2467 return 0;
2468}
2469
Chris Lattnere10269b2005-01-17 19:25:26 +00002470/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2471/// load/op/store instruction. If successful return true.
2472bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2473 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2474 SDOperand Chain = Node->getOperand(0);
2475 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002476 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002477
2478 // The chain has to be a load, the stored value must be an integer binary
2479 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002480 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002481 MVT::isFloatingPoint(StVal.getValueType()))
2482 return false;
2483
Chris Lattner5c659812005-01-17 22:10:42 +00002484 // Token chain must either be a factor node or the load to fold.
2485 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2486 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002487
Chris Lattner5c659812005-01-17 22:10:42 +00002488 SDOperand TheLoad;
2489
2490 // Check to see if there is a load from the same pointer that we're storing
2491 // to in either operand of the binop.
2492 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2493 StVal.getOperand(0).getOperand(1) == StPtr)
2494 TheLoad = StVal.getOperand(0);
2495 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2496 StVal.getOperand(1).getOperand(1) == StPtr)
2497 TheLoad = StVal.getOperand(1);
2498 else
2499 return false; // No matching load operand.
2500
2501 // We can only fold the load if there are no intervening side-effecting
2502 // operations. This means that the store uses the load as its token chain, or
2503 // there are only token factor nodes in between the store and load.
2504 if (Chain != TheLoad.getValue(1)) {
2505 // Okay, the other option is that we have a store referring to (possibly
2506 // nested) token factor nodes. For now, just try peeking through one level
2507 // of token factors to see if this is the case.
2508 bool ChainOk = false;
2509 if (Chain.getOpcode() == ISD::TokenFactor) {
2510 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2511 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2512 ChainOk = true;
2513 break;
2514 }
2515 }
2516
2517 if (!ChainOk) return false;
2518 }
2519
2520 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002521 return false;
2522
2523 // Make sure that one of the operands of the binop is the load, and that the
2524 // load folds into the binop.
2525 if (((StVal.getOperand(0) != TheLoad ||
2526 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2527 (StVal.getOperand(1) != TheLoad ||
2528 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2529 return false;
2530
2531 // Finally, check to see if this is one of the ops we can handle!
2532 static const unsigned ADDTAB[] = {
2533 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2534 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2535 };
2536 static const unsigned SUBTAB[] = {
2537 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2538 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2539 };
2540 static const unsigned ANDTAB[] = {
2541 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2542 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2543 };
2544 static const unsigned ORTAB[] = {
2545 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2546 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2547 };
2548 static const unsigned XORTAB[] = {
2549 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2550 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2551 };
2552 static const unsigned SHLTAB[] = {
2553 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2554 /*Have to put the reg in CL*/0, 0, 0,
2555 };
2556 static const unsigned SARTAB[] = {
2557 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2558 /*Have to put the reg in CL*/0, 0, 0,
2559 };
2560 static const unsigned SHRTAB[] = {
2561 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2562 /*Have to put the reg in CL*/0, 0, 0,
2563 };
2564
2565 const unsigned *TabPtr = 0;
2566 switch (StVal.getOpcode()) {
2567 default:
2568 std::cerr << "CANNOT [mem] op= val: ";
2569 StVal.Val->dump(); std::cerr << "\n";
2570 case ISD::MUL:
2571 case ISD::SDIV:
2572 case ISD::UDIV:
2573 case ISD::SREM:
2574 case ISD::UREM: return false;
2575
2576 case ISD::ADD: TabPtr = ADDTAB; break;
2577 case ISD::SUB: TabPtr = SUBTAB; break;
2578 case ISD::AND: TabPtr = ANDTAB; break;
2579 case ISD:: OR: TabPtr = ORTAB; break;
2580 case ISD::XOR: TabPtr = XORTAB; break;
2581 case ISD::SHL: TabPtr = SHLTAB; break;
2582 case ISD::SRA: TabPtr = SARTAB; break;
2583 case ISD::SRL: TabPtr = SHRTAB; break;
2584 }
2585
2586 // Handle: [mem] op= CST
2587 SDOperand Op0 = StVal.getOperand(0);
2588 SDOperand Op1 = StVal.getOperand(1);
2589 unsigned Opc;
2590 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2591 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2592 default: break;
2593 case MVT::i1:
2594 case MVT::i8: Opc = TabPtr[0]; break;
2595 case MVT::i16: Opc = TabPtr[1]; break;
2596 case MVT::i32: Opc = TabPtr[2]; break;
2597 }
2598
2599 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002600 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2601 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002602 Select(Chain);
2603
Chris Lattnere10269b2005-01-17 19:25:26 +00002604 X86AddressMode AM;
2605 if (getRegPressure(TheLoad.getOperand(0)) >
2606 getRegPressure(TheLoad.getOperand(1))) {
2607 Select(TheLoad.getOperand(0));
2608 SelectAddress(TheLoad.getOperand(1), AM);
2609 } else {
2610 SelectAddress(TheLoad.getOperand(1), AM);
2611 Select(TheLoad.getOperand(0));
2612 }
Chris Lattner5c659812005-01-17 22:10:42 +00002613
2614 if (StVal.getOpcode() == ISD::ADD) {
2615 if (CN->getValue() == 1) {
2616 switch (Op0.getValueType()) {
2617 default: break;
2618 case MVT::i8:
2619 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2620 return true;
2621 case MVT::i16: Opc = TabPtr[1];
2622 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2623 return true;
2624 case MVT::i32: Opc = TabPtr[2];
2625 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2626 return true;
2627 }
2628 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2629 switch (Op0.getValueType()) {
2630 default: break;
2631 case MVT::i8:
2632 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2633 return true;
2634 case MVT::i16: Opc = TabPtr[1];
2635 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2636 return true;
2637 case MVT::i32: Opc = TabPtr[2];
2638 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2639 return true;
2640 }
2641 }
2642 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002643
2644 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2645 return true;
2646 }
2647 }
2648
2649 // If we have [mem] = V op [mem], try to turn it into:
2650 // [mem] = [mem] op V.
2651 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2652 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2653 StVal.getOpcode() != ISD::SRL)
2654 std::swap(Op0, Op1);
2655
2656 if (Op0 != TheLoad) return false;
2657
2658 switch (Op0.getValueType()) {
2659 default: return false;
2660 case MVT::i1:
2661 case MVT::i8: Opc = TabPtr[3]; break;
2662 case MVT::i16: Opc = TabPtr[4]; break;
2663 case MVT::i32: Opc = TabPtr[5]; break;
2664 }
Chris Lattner5c659812005-01-17 22:10:42 +00002665
Chris Lattnerb422aea2005-01-18 17:35:28 +00002666 // Table entry doesn't exist?
2667 if (Opc == 0) return false;
2668
Chris Lattner4a108662005-01-18 03:51:59 +00002669 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2670 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002671 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002672 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002673
Chris Lattnere10269b2005-01-17 19:25:26 +00002674 X86AddressMode AM;
2675 SelectAddress(TheLoad.getOperand(1), AM);
2676 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002677 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002678 return true;
2679}
2680
2681
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002682void ISel::Select(SDOperand N) {
2683 unsigned Tmp1, Tmp2, Opc;
2684
2685 // FIXME: Disable for our current expansion model!
Chris Lattner4a108662005-01-18 03:51:59 +00002686 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002687 return; // Already selected.
2688
Chris Lattner989de032005-01-11 06:14:36 +00002689 SDNode *Node = N.Val;
2690
2691 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002692 default:
Chris Lattner989de032005-01-11 06:14:36 +00002693 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002694 assert(0 && "Node not handled yet!");
2695 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002696 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002697 if (Node->getNumOperands() == 2) {
2698 bool OneFirst =
2699 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2700 Select(Node->getOperand(OneFirst));
2701 Select(Node->getOperand(!OneFirst));
2702 } else {
2703 std::vector<std::pair<unsigned, unsigned> > OpsP;
2704 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2705 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2706 std::sort(OpsP.begin(), OpsP.end());
2707 std::reverse(OpsP.begin(), OpsP.end());
2708 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2709 Select(Node->getOperand(OpsP[i].second));
2710 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002711 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002712 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002713 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2714 Select(N.getOperand(0));
2715 Tmp1 = SelectExpr(N.getOperand(1));
2716 } else {
2717 Tmp1 = SelectExpr(N.getOperand(1));
2718 Select(N.getOperand(0));
2719 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002720 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002721
2722 if (Tmp1 != Tmp2) {
2723 switch (N.getOperand(1).getValueType()) {
2724 default: assert(0 && "Invalid type for operation!");
2725 case MVT::i1:
2726 case MVT::i8: Opc = X86::MOV8rr; break;
2727 case MVT::i16: Opc = X86::MOV16rr; break;
2728 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002729 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002730 }
2731 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2732 }
2733 return;
2734 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002735 switch (N.getNumOperands()) {
2736 default:
2737 assert(0 && "Unknown return instruction!");
2738 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002739 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2740 N.getOperand(2).getValueType() == MVT::i32 &&
2741 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002742 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2743 Tmp1 = SelectExpr(N.getOperand(1));
2744 Tmp2 = SelectExpr(N.getOperand(2));
2745 } else {
2746 Tmp2 = SelectExpr(N.getOperand(2));
2747 Tmp1 = SelectExpr(N.getOperand(1));
2748 }
2749 Select(N.getOperand(0));
2750
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002751 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2752 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2753 // Declare that EAX & EDX are live on exit.
2754 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2755 .addReg(X86::ESP);
2756 break;
2757 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002758 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2759 Select(N.getOperand(0));
2760 Tmp1 = SelectExpr(N.getOperand(1));
2761 } else {
2762 Tmp1 = SelectExpr(N.getOperand(1));
2763 Select(N.getOperand(0));
2764 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002765 switch (N.getOperand(1).getValueType()) {
2766 default: assert(0 && "All other types should have been promoted!!");
2767 case MVT::f64:
2768 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2769 // Declare that top-of-stack is live on exit
2770 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2771 break;
2772 case MVT::i32:
2773 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2774 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2775 break;
2776 }
2777 break;
2778 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002779 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002780 break;
2781 }
2782 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2783 return;
2784 case ISD::BR: {
2785 Select(N.getOperand(0));
2786 MachineBasicBlock *Dest =
2787 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2788 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2789 return;
2790 }
2791
2792 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002793 MachineBasicBlock *Dest =
2794 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002795
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002796 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2797 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002798 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2799 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2800 Select(N.getOperand(0));
2801 Tmp1 = SelectExpr(N.getOperand(1));
2802 } else {
2803 Tmp1 = SelectExpr(N.getOperand(1));
2804 Select(N.getOperand(0));
2805 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002806 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2807 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2808 }
Chris Lattner11333092005-01-11 03:11:44 +00002809
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002810 return;
2811 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002812
Chris Lattner4df0de92005-01-17 00:00:33 +00002813 case ISD::LOAD:
2814 // If this load could be folded into the only using instruction, and if it
2815 // is safe to emit the instruction here, try to do so now.
2816 if (Node->hasNUsesOfValue(1, 0)) {
2817 SDOperand TheVal = N.getValue(0);
2818 SDNode *User = 0;
2819 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2820 assert(UI != Node->use_end() && "Didn't find use!");
2821 SDNode *UN = *UI;
2822 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2823 if (UN->getOperand(i) == TheVal) {
2824 User = UN;
2825 goto FoundIt;
2826 }
2827 }
2828 FoundIt:
2829 // Only handle unary operators right now.
2830 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00002831 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002832 SelectExpr(SDOperand(User, 0));
2833 return;
2834 }
2835 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002836 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002837 SelectExpr(N);
2838 return;
2839
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002840 case ISD::EXTLOAD:
2841 case ISD::SEXTLOAD:
2842 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002843 case ISD::CALL:
2844 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002845 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002846 SelectExpr(N);
2847 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002848
2849 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2850 // On X86, we can represent all types except for Bool and Float natively.
2851 X86AddressMode AM;
2852 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002853 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2854 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2855 && "Unsupported TRUNCSTORE for this target!");
2856
2857 if (StoredTy == MVT::i16) {
2858 // FIXME: This is here just to allow testing. X86 doesn't really have a
2859 // TRUNCSTORE i16 operation, but this is required for targets that do not
2860 // have 16-bit integer registers. We occasionally disable 16-bit integer
2861 // registers to test the promotion code.
2862 Select(N.getOperand(0));
2863 Tmp1 = SelectExpr(N.getOperand(1));
2864 SelectAddress(N.getOperand(2), AM);
2865
2866 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2867 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2868 return;
2869 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002870
2871 // Store of constant bool?
2872 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2873 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2874 Select(N.getOperand(0));
2875 SelectAddress(N.getOperand(2), AM);
2876 } else {
2877 SelectAddress(N.getOperand(2), AM);
2878 Select(N.getOperand(0));
2879 }
2880 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2881 return;
2882 }
2883
2884 switch (StoredTy) {
2885 default: assert(0 && "Cannot truncstore this type!");
2886 case MVT::i1: Opc = X86::MOV8mr; break;
2887 case MVT::f32: Opc = X86::FST32m; break;
2888 }
2889
2890 std::vector<std::pair<unsigned, unsigned> > RP;
2891 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2892 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2893 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2894 std::sort(RP.begin(), RP.end());
2895
2896 for (unsigned i = 0; i != 3; ++i)
2897 switch (RP[2-i].second) {
2898 default: assert(0 && "Unknown operand number!");
2899 case 0: Select(N.getOperand(0)); break;
2900 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2901 case 2: SelectAddress(N.getOperand(2), AM); break;
2902 }
2903
2904 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2905 return;
2906 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002907 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002908 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002909
2910 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2911 Opc = 0;
2912 switch (CN->getValueType(0)) {
2913 default: assert(0 && "Invalid type for operation!");
2914 case MVT::i1:
2915 case MVT::i8: Opc = X86::MOV8mi; break;
2916 case MVT::i16: Opc = X86::MOV16mi; break;
2917 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002918 case MVT::f64: break;
2919 }
2920 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002921 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2922 Select(N.getOperand(0));
2923 SelectAddress(N.getOperand(2), AM);
2924 } else {
2925 SelectAddress(N.getOperand(2), AM);
2926 Select(N.getOperand(0));
2927 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002928 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2929 return;
2930 }
2931 }
Chris Lattner837caa72005-01-11 23:21:30 +00002932
2933 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00002934 if (TryToFoldLoadOpStore(Node))
2935 return;
Chris Lattner837caa72005-01-11 23:21:30 +00002936
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002937 switch (N.getOperand(1).getValueType()) {
2938 default: assert(0 && "Cannot store this type!");
2939 case MVT::i1:
2940 case MVT::i8: Opc = X86::MOV8mr; break;
2941 case MVT::i16: Opc = X86::MOV16mr; break;
2942 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002943 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002944 }
Chris Lattner11333092005-01-11 03:11:44 +00002945
2946 std::vector<std::pair<unsigned, unsigned> > RP;
2947 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2948 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2949 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2950 std::sort(RP.begin(), RP.end());
2951
2952 for (unsigned i = 0; i != 3; ++i)
2953 switch (RP[2-i].second) {
2954 default: assert(0 && "Unknown operand number!");
2955 case 0: Select(N.getOperand(0)); break;
2956 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002957 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002958 }
2959
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002960 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2961 return;
2962 }
2963 case ISD::ADJCALLSTACKDOWN:
2964 case ISD::ADJCALLSTACKUP:
2965 Select(N.getOperand(0));
2966 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2967
2968 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2969 X86::ADJCALLSTACKUP;
2970 BuildMI(BB, Opc, 1).addImm(Tmp1);
2971 return;
Chris Lattner989de032005-01-11 06:14:36 +00002972 case ISD::MEMSET: {
2973 Select(N.getOperand(0)); // Select the chain.
2974 unsigned Align =
2975 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2976 if (Align == 0) Align = 1;
2977
2978 // Turn the byte code into # iterations
2979 unsigned CountReg;
2980 unsigned Opcode;
2981 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2982 unsigned Val = ValC->getValue() & 255;
2983
2984 // If the value is a constant, then we can potentially use larger sets.
2985 switch (Align & 3) {
2986 case 2: // WORD aligned
2987 CountReg = MakeReg(MVT::i32);
2988 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2989 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2990 } else {
2991 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2992 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2993 }
2994 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2995 Opcode = X86::REP_STOSW;
2996 break;
2997 case 0: // DWORD aligned
2998 CountReg = MakeReg(MVT::i32);
2999 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3000 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3001 } else {
3002 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3003 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3004 }
3005 Val = (Val << 8) | Val;
3006 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3007 Opcode = X86::REP_STOSD;
3008 break;
3009 default: // BYTE aligned
3010 CountReg = SelectExpr(Node->getOperand(3));
3011 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3012 Opcode = X86::REP_STOSB;
3013 break;
3014 }
3015 } else {
3016 // If it's not a constant value we are storing, just fall back. We could
3017 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3018 unsigned ValReg = SelectExpr(Node->getOperand(2));
3019 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3020 CountReg = SelectExpr(Node->getOperand(3));
3021 Opcode = X86::REP_STOSB;
3022 }
3023
3024 // No matter what the alignment is, we put the source in ESI, the
3025 // destination in EDI, and the count in ECX.
3026 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3027 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3028 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3029 BuildMI(BB, Opcode, 0);
3030 return;
3031 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003032 case ISD::MEMCPY:
3033 Select(N.getOperand(0)); // Select the chain.
3034 unsigned Align =
3035 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3036 if (Align == 0) Align = 1;
3037
3038 // Turn the byte code into # iterations
3039 unsigned CountReg;
3040 unsigned Opcode;
3041 switch (Align & 3) {
3042 case 2: // WORD aligned
3043 CountReg = MakeReg(MVT::i32);
3044 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3045 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3046 } else {
3047 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3048 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3049 }
3050 Opcode = X86::REP_MOVSW;
3051 break;
3052 case 0: // DWORD aligned
3053 CountReg = MakeReg(MVT::i32);
3054 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3055 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3056 } else {
3057 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3058 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3059 }
3060 Opcode = X86::REP_MOVSD;
3061 break;
3062 default: // BYTE aligned
3063 CountReg = SelectExpr(Node->getOperand(3));
3064 Opcode = X86::REP_MOVSB;
3065 break;
3066 }
3067
3068 // No matter what the alignment is, we put the source in ESI, the
3069 // destination in EDI, and the count in ECX.
3070 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3071 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3072 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3073 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3074 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3075 BuildMI(BB, Opcode, 0);
3076 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003077 }
3078 assert(0 && "Should not be reached!");
3079}
3080
3081
3082/// createX86PatternInstructionSelector - This pass converts an LLVM function
3083/// into a machine code representation using pattern matching and a machine
3084/// description file.
3085///
3086FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
3087 return new ISel(TM);
3088}