blob: 61b7e597e1aba7a2bd1488c821671d88095f5c5e [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 Lattner6659bd72005-04-07 19:41:46 +000046 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +000047 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000048
49 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000050 addRegisterClass(MVT::i8, X86::R8RegisterClass);
51 addRegisterClass(MVT::i16, X86::R16RegisterClass);
52 addRegisterClass(MVT::i32, X86::R32RegisterClass);
53 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
54
55 // FIXME: Eliminate these two classes when legalize can handle promotions
56 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000057/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000058
Chris Lattnerda4d4692005-04-09 03:22:37 +000059 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000060 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
61 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
62 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i16 , Expand);
63 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
64 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i1 , Expand);
65 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
66 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
67 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000068
Chris Lattnerda2ce112005-01-16 07:34:08 +000069 // These should be promoted to a larger select which is supported.
70/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
71 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000072
73 computeRegisterProperties();
Chris Lattner8acb1ba2005-01-07 07:49:41 +000074
75 addLegalFPImmediate(+0.0); // FLD0
76 addLegalFPImmediate(+1.0); // FLD1
77 addLegalFPImmediate(-0.0); // FLD0/FCHS
78 addLegalFPImmediate(-1.0); // FLD1/FCHS
79 }
80
81 /// LowerArguments - This hook must be implemented to indicate how we should
82 /// lower the arguments for the specified function, into the specified DAG.
83 virtual std::vector<SDOperand>
84 LowerArguments(Function &F, SelectionDAG &DAG);
85
86 /// LowerCallTo - This hook lowers an abstract call to a function into an
87 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000088 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +000089 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
90 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000091
92 virtual std::pair<SDOperand, SDOperand>
93 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
94
95 virtual std::pair<SDOperand,SDOperand>
96 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
97 const Type *ArgTy, SelectionDAG &DAG);
98
99 virtual std::pair<SDOperand, SDOperand>
100 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
101 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000102 };
103}
104
105
106std::vector<SDOperand>
107X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
108 std::vector<SDOperand> ArgValues;
109
110 // Add DAG nodes to load the arguments... On entry to a function on the X86,
111 // the stack frame looks like this:
112 //
113 // [ESP] -- return address
114 // [ESP + 4] -- first argument (leftmost lexically)
115 // [ESP + 8] -- second argument, if first argument is four bytes in size
116 // ...
117 //
118 MachineFunction &MF = DAG.getMachineFunction();
119 MachineFrameInfo *MFI = MF.getFrameInfo();
120
121 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000122 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000123 MVT::ValueType ObjectVT = getValueType(I->getType());
124 unsigned ArgIncrement = 4;
125 unsigned ObjSize;
126 switch (ObjectVT) {
127 default: assert(0 && "Unhandled argument type!");
128 case MVT::i1:
129 case MVT::i8: ObjSize = 1; break;
130 case MVT::i16: ObjSize = 2; break;
131 case MVT::i32: ObjSize = 4; break;
132 case MVT::i64: ObjSize = ArgIncrement = 8; break;
133 case MVT::f32: ObjSize = 4; break;
134 case MVT::f64: ObjSize = ArgIncrement = 8; break;
135 }
136 // Create the frame index object for this incoming parameter...
137 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
138
139 // Create the SelectionDAG nodes corresponding to a load from this parameter
140 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
141
142 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
143 // dead loads.
144 SDOperand ArgValue;
145 if (!I->use_empty())
146 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
147 else {
148 if (MVT::isInteger(ObjectVT))
149 ArgValue = DAG.getConstant(0, ObjectVT);
150 else
151 ArgValue = DAG.getConstantFP(0, ObjectVT);
152 }
153 ArgValues.push_back(ArgValue);
154
155 ArgOffset += ArgIncrement; // Move on to the next argument...
156 }
157
158 // If the function takes variable number of arguments, make a frame index for
159 // the start of the first vararg value... for expansion of llvm.va_start.
160 if (F.isVarArg())
161 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000162 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000163 return ArgValues;
164}
165
Chris Lattner5188ad72005-01-08 19:28:19 +0000166std::pair<SDOperand, SDOperand>
167X86TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000168 const Type *RetTy, bool isVarArg,
169 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000170 // Count how many bytes are to be pushed on the stack.
171 unsigned NumBytes = 0;
172
173 if (Args.empty()) {
174 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000175 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
176 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000177 } else {
178 for (unsigned i = 0, e = Args.size(); i != e; ++i)
179 switch (getValueType(Args[i].second)) {
180 default: assert(0 && "Unknown value type!");
181 case MVT::i1:
182 case MVT::i8:
183 case MVT::i16:
184 case MVT::i32:
185 case MVT::f32:
186 NumBytes += 4;
187 break;
188 case MVT::i64:
189 case MVT::f64:
190 NumBytes += 8;
191 break;
192 }
193
Chris Lattner5188ad72005-01-08 19:28:19 +0000194 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
195 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000196
197 // Arguments go on the stack in reverse order, as specified by the ABI.
198 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000199 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
200 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000201 std::vector<SDOperand> Stores;
202
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000203 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
204 unsigned ArgReg;
205 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
206 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
207
208 switch (getValueType(Args[i].second)) {
209 default: assert(0 && "Unexpected ValueType for argument!");
210 case MVT::i1:
211 case MVT::i8:
212 case MVT::i16:
213 // Promote the integer to 32 bits. If the input type is signed use a
214 // sign extend, otherwise use a zero extend.
215 if (Args[i].second->isSigned())
216 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
217 else
218 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
219
220 // FALL THROUGH
221 case MVT::i32:
222 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000223 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
224 Args[i].first, PtrOff));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000225 ArgOffset += 4;
226 break;
227 case MVT::i64:
228 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000229 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
230 Args[i].first, PtrOff));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000231 ArgOffset += 8;
232 break;
233 }
234 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000235 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000236 }
237
238 std::vector<MVT::ValueType> RetVals;
239 MVT::ValueType RetTyVT = getValueType(RetTy);
240 if (RetTyVT != MVT::isVoid)
241 RetVals.push_back(RetTyVT);
242 RetVals.push_back(MVT::Other);
243
Chris Lattner5188ad72005-01-08 19:28:19 +0000244 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000245 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000246 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
247 DAG.getConstant(NumBytes, getPointerTy()));
248 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000249}
250
Chris Lattner14824582005-01-09 00:01:27 +0000251std::pair<SDOperand, SDOperand>
252X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
253 // vastart just returns the address of the VarArgsFrameIndex slot.
254 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
255}
256
257std::pair<SDOperand,SDOperand> X86TargetLowering::
258LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
259 const Type *ArgTy, SelectionDAG &DAG) {
260 MVT::ValueType ArgVT = getValueType(ArgTy);
261 SDOperand Result;
262 if (!isVANext) {
263 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
264 } else {
265 unsigned Amt;
266 if (ArgVT == MVT::i32)
267 Amt = 4;
268 else {
269 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
270 "Other types should have been promoted for varargs!");
271 Amt = 8;
272 }
273 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
274 DAG.getConstant(Amt, VAList.getValueType()));
275 }
276 return std::make_pair(Result, Chain);
277}
278
279
280std::pair<SDOperand, SDOperand> X86TargetLowering::
281LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
282 SelectionDAG &DAG) {
283 SDOperand Result;
284 if (Depth) // Depths > 0 not supported yet!
285 Result = DAG.getConstant(0, getPointerTy());
286 else {
287 if (ReturnAddrIndex == 0) {
288 // Set up a frame object for the return address.
289 MachineFunction &MF = DAG.getMachineFunction();
290 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
291 }
292
293 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
294
295 if (!isFrameAddress)
296 // Just load the return address
297 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
298 else
299 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
300 DAG.getConstant(4, MVT::i32));
301 }
302 return std::make_pair(Result, Chain);
303}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000304
305
Chris Lattner98a8ba02005-01-18 01:06:26 +0000306namespace {
307 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
308 /// SDOperand's instead of register numbers for the leaves of the matched
309 /// tree.
310 struct X86ISelAddressMode {
311 enum {
312 RegBase,
313 FrameIndexBase,
314 } BaseType;
315
316 struct { // This is really a union, discriminated by BaseType!
317 SDOperand Reg;
318 int FrameIndex;
319 } Base;
320
321 unsigned Scale;
322 SDOperand IndexReg;
323 unsigned Disp;
324 GlobalValue *GV;
325
326 X86ISelAddressMode()
327 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
328 }
329 };
330}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000331
332
333namespace {
334 Statistic<>
335 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
336
337 //===--------------------------------------------------------------------===//
338 /// ISel - X86 specific code to select X86 machine instructions for
339 /// SelectionDAG operations.
340 ///
341 class ISel : public SelectionDAGISel {
342 /// ContainsFPCode - Every instruction we select that uses or defines a FP
343 /// register should set this to true.
344 bool ContainsFPCode;
345
346 /// X86Lowering - This object fully describes how to lower LLVM code to an
347 /// X86-specific SelectionDAG.
348 X86TargetLowering X86Lowering;
349
Chris Lattner11333092005-01-11 03:11:44 +0000350 /// RegPressureMap - This keeps an approximate count of the number of
351 /// registers required to evaluate each node in the graph.
352 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000353
354 /// ExprMap - As shared expressions are codegen'd, we keep track of which
355 /// vreg the value is produced in, so we only emit one copy of each compiled
356 /// tree.
357 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000358
359 public:
360 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
361 }
362
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000363 virtual const char *getPassName() const {
364 return "X86 Pattern Instruction Selection";
365 }
366
Chris Lattner11333092005-01-11 03:11:44 +0000367 unsigned getRegPressure(SDOperand O) {
368 return RegPressureMap[O.Val];
369 }
370 unsigned ComputeRegPressure(SDOperand O);
371
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000372 /// InstructionSelectBasicBlock - This callback is invoked by
373 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000374 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000375
Chris Lattner44129b52005-01-25 20:03:11 +0000376 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
377 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000378 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000379 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000380
Chris Lattner30ea1e92005-01-19 07:37:26 +0000381 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000382 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000383 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000384 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
385 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000386 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000387
388 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
389 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
390 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000391 void Select(SDOperand N);
392 };
393}
394
Chris Lattner7dbcb752005-01-12 04:21:28 +0000395/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
396/// when it has created a SelectionDAG for us to codegen.
397void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
398 // While we're doing this, keep track of whether we see any FP code for
399 // FP_REG_KILL insertion.
400 ContainsFPCode = false;
401
402 // Scan the PHI nodes that already are inserted into this basic block. If any
403 // of them is a PHI of a floating point value, we need to insert an
404 // FP_REG_KILL.
405 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
406 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
407 I != E; ++I) {
408 assert(I->getOpcode() == X86::PHI &&
409 "Isn't just PHI nodes?");
410 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
411 X86::RFPRegisterClass) {
412 ContainsFPCode = true;
413 break;
414 }
415 }
416
417 // Compute the RegPressureMap, which is an approximation for the number of
418 // registers required to compute each node.
419 ComputeRegPressure(DAG.getRoot());
420
421 // Codegen the basic block.
422 Select(DAG.getRoot());
423
424 // Finally, look at all of the successors of this block. If any contain a PHI
425 // node of FP type, we need to insert an FP_REG_KILL in this block.
426 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
427 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
428 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
429 I != E && I->getOpcode() == X86::PHI; ++I) {
430 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
431 X86::RFPRegisterClass) {
432 ContainsFPCode = true;
433 break;
434 }
435 }
436
437 // Insert FP_REG_KILL instructions into basic blocks that need them. This
438 // only occurs due to the floating point stackifier not being aggressive
439 // enough to handle arbitrary global stackification.
440 //
441 // Currently we insert an FP_REG_KILL instruction into each block that uses or
442 // defines a floating point virtual register.
443 //
444 // When the global register allocators (like linear scan) finally update live
445 // variable analysis, we can keep floating point values in registers across
446 // basic blocks. This will be a huge win, but we are waiting on the global
447 // allocators before we can do this.
448 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000449 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000450 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
451 ++NumFPKill;
452 }
453
454 // Clear state used for selection.
455 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000456 RegPressureMap.clear();
457}
458
459
Chris Lattner11333092005-01-11 03:11:44 +0000460// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
461// for the number of registers required to compute each node. This is basically
462// computing a generalized form of the Sethi-Ullman number for each node.
463unsigned ISel::ComputeRegPressure(SDOperand O) {
464 SDNode *N = O.Val;
465 unsigned &Result = RegPressureMap[N];
466 if (Result) return Result;
467
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000468 // FIXME: Should operations like CALL (which clobber lots o regs) have a
469 // higher fixed cost??
470
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000471 if (N->getNumOperands() == 0) {
472 Result = 1;
473 } else {
474 unsigned MaxRegUse = 0;
475 unsigned NumExtraMaxRegUsers = 0;
476 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
477 unsigned Regs;
478 if (N->getOperand(i).getOpcode() == ISD::Constant)
479 Regs = 0;
480 else
481 Regs = ComputeRegPressure(N->getOperand(i));
482 if (Regs > MaxRegUse) {
483 MaxRegUse = Regs;
484 NumExtraMaxRegUsers = 0;
485 } else if (Regs == MaxRegUse &&
486 N->getOperand(i).getValueType() != MVT::Other) {
487 ++NumExtraMaxRegUsers;
488 }
Chris Lattner11333092005-01-11 03:11:44 +0000489 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000490
491 if (O.getOpcode() != ISD::TokenFactor)
492 Result = MaxRegUse+NumExtraMaxRegUsers;
493 else
Chris Lattner869e0432005-01-17 23:02:13 +0000494 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000495 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000496
Chris Lattner837caa72005-01-11 23:21:30 +0000497 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000498 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000499}
500
Chris Lattnerbf52d492005-01-20 16:50:16 +0000501/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
502/// The DAG cannot have cycles in it, by definition, so the visited set is not
503/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
504/// reuse, so it prevents exponential cases.
505///
506static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
507 std::set<SDNode*> &Visited) {
508 if (N == Op) return true; // Found it.
509 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000510 if (Node->getNumOperands() == 0 || // Leaf?
511 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000512 if (!Visited.insert(Node).second) return false; // Already visited?
513
514 // Recurse for the first N-1 operands.
515 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
516 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
517 return true;
518
519 // Tail recurse for the last operand.
520 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
521}
522
Chris Lattner98a8ba02005-01-18 01:06:26 +0000523X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
524 X86AddressMode Result;
525
526 // If we need to emit two register operands, emit the one with the highest
527 // register pressure first.
528 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
529 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000530 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000531 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000532 std::set<SDNode*> Visited;
533 EmitBaseThenIndex = true;
534 // If Base ends up pointing to Index, we must emit index first. This is
535 // because of the way we fold loads, we may end up doing bad things with
536 // the folded add.
537 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
538 EmitBaseThenIndex = false;
539 } else {
540 std::set<SDNode*> Visited;
541 EmitBaseThenIndex = false;
542 // If Base ends up pointing to Index, we must emit index first. This is
543 // because of the way we fold loads, we may end up doing bad things with
544 // the folded add.
545 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
546 EmitBaseThenIndex = true;
547 }
548
549 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000550 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
551 Result.IndexReg = SelectExpr(IAM.IndexReg);
552 } else {
553 Result.IndexReg = SelectExpr(IAM.IndexReg);
554 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
555 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000556
Chris Lattner98a8ba02005-01-18 01:06:26 +0000557 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
558 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
559 } else if (IAM.IndexReg.Val) {
560 Result.IndexReg = SelectExpr(IAM.IndexReg);
561 }
562
563 switch (IAM.BaseType) {
564 case X86ISelAddressMode::RegBase:
565 Result.BaseType = X86AddressMode::RegBase;
566 break;
567 case X86ISelAddressMode::FrameIndexBase:
568 Result.BaseType = X86AddressMode::FrameIndexBase;
569 Result.Base.FrameIndex = IAM.Base.FrameIndex;
570 break;
571 default:
572 assert(0 && "Unknown base type!");
573 break;
574 }
575 Result.Scale = IAM.Scale;
576 Result.Disp = IAM.Disp;
577 Result.GV = IAM.GV;
578 return Result;
579}
580
581/// SelectAddress - Pattern match the maximal addressing mode for this node and
582/// emit all of the leaf registers.
583void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
584 X86ISelAddressMode IAM;
585 MatchAddress(N, IAM);
586 AM = SelectAddrExprs(IAM);
587}
588
589/// MatchAddress - Add the specified node to the specified addressing mode,
590/// returning true if it cannot be done. This just pattern matches for the
591/// addressing mode, it does not cause any code to be emitted. For that, use
592/// SelectAddress.
593bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000594 switch (N.getOpcode()) {
595 default: break;
596 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000597 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
598 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000599 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
600 return false;
601 }
602 break;
603 case ISD::GlobalAddress:
604 if (AM.GV == 0) {
605 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
606 return false;
607 }
608 break;
609 case ISD::Constant:
610 AM.Disp += cast<ConstantSDNode>(N)->getValue();
611 return false;
612 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000613 // We might have folded the load into this shift, so don't regen the value
614 // if so.
615 if (ExprMap.count(N)) break;
616
Chris Lattner98a8ba02005-01-18 01:06:26 +0000617 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000618 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
619 unsigned Val = CN->getValue();
620 if (Val == 1 || Val == 2 || Val == 3) {
621 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000622 SDOperand ShVal = N.Val->getOperand(0);
623
624 // Okay, we know that we have a scale by now. However, if the scaled
625 // value is an add of something and a constant, we can fold the
626 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000627 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000628 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000629 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000630 ConstantSDNode *AddVal =
631 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
632 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000633 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000634 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000635 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000636 return false;
637 }
638 }
639 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000640 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000641 // We might have folded the load into this mul, so don't regen the value if
642 // so.
643 if (ExprMap.count(N)) break;
644
Chris Lattner947d5442005-01-11 19:37:02 +0000645 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000646 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
647 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000648 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
649 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
650 AM.Scale = unsigned(CN->getValue())-1;
651
652 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000653 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000654
655 // Okay, we know that we have a scale by now. However, if the scaled
656 // value is an add of something and a constant, we can fold the
657 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000658 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000659 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000660 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000661 ConstantSDNode *AddVal =
662 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
663 AM.Disp += AddVal->getValue() * CN->getValue();
664 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000665 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000666 }
667
668 AM.IndexReg = AM.Base.Reg = Reg;
669 return false;
670 }
671 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000672
673 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000674 // We might have folded the load into this mul, so don't regen the value if
675 // so.
676 if (ExprMap.count(N)) break;
677
Chris Lattner98a8ba02005-01-18 01:06:26 +0000678 X86ISelAddressMode Backup = AM;
679 if (!MatchAddress(N.Val->getOperand(0), AM) &&
680 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000681 return false;
682 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000683 if (!MatchAddress(N.Val->getOperand(1), AM) &&
684 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000685 return false;
686 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000687 break;
688 }
689 }
690
Chris Lattnera95589b2005-01-11 04:40:19 +0000691 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000692 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000693 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000694 if (AM.IndexReg.Val == 0) {
695 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000696 AM.Scale = 1;
697 return false;
698 }
699
700 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000701 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000702 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000703
704 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000705 AM.BaseType = X86ISelAddressMode::RegBase;
706 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000707 return false;
708}
709
710/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
711/// assuming that the temporary registers are in the 8-bit register class.
712///
713/// Tmp1 = setcc1
714/// Tmp2 = setcc2
715/// DestReg = logicalop Tmp1, Tmp2
716///
717static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
718 unsigned SetCC2, unsigned LogicalOp,
719 unsigned DestReg) {
720 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
721 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
722 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
723 BuildMI(BB, SetCC1, 0, Tmp1);
724 BuildMI(BB, SetCC2, 0, Tmp2);
725 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
726}
727
728/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
729/// condition codes match the specified SetCCOpcode. Note that some conditions
730/// require multiple instructions to generate the correct value.
731static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
732 ISD::CondCode SetCCOpcode, bool isFP) {
733 unsigned Opc;
734 if (!isFP) {
735 switch (SetCCOpcode) {
736 default: assert(0 && "Illegal integer SetCC!");
737 case ISD::SETEQ: Opc = X86::SETEr; break;
738 case ISD::SETGT: Opc = X86::SETGr; break;
739 case ISD::SETGE: Opc = X86::SETGEr; break;
740 case ISD::SETLT: Opc = X86::SETLr; break;
741 case ISD::SETLE: Opc = X86::SETLEr; break;
742 case ISD::SETNE: Opc = X86::SETNEr; break;
743 case ISD::SETULT: Opc = X86::SETBr; break;
744 case ISD::SETUGT: Opc = X86::SETAr; break;
745 case ISD::SETULE: Opc = X86::SETBEr; break;
746 case ISD::SETUGE: Opc = X86::SETAEr; break;
747 }
748 } else {
749 // On a floating point condition, the flags are set as follows:
750 // ZF PF CF op
751 // 0 | 0 | 0 | X > Y
752 // 0 | 0 | 1 | X < Y
753 // 1 | 0 | 0 | X == Y
754 // 1 | 1 | 1 | unordered
755 //
756 switch (SetCCOpcode) {
757 default: assert(0 && "Invalid FP setcc!");
758 case ISD::SETUEQ:
759 case ISD::SETEQ:
760 Opc = X86::SETEr; // True if ZF = 1
761 break;
762 case ISD::SETOGT:
763 case ISD::SETGT:
764 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
765 break;
766 case ISD::SETOGE:
767 case ISD::SETGE:
768 Opc = X86::SETAEr; // True if CF = 0
769 break;
770 case ISD::SETULT:
771 case ISD::SETLT:
772 Opc = X86::SETBr; // True if CF = 1
773 break;
774 case ISD::SETULE:
775 case ISD::SETLE:
776 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
777 break;
778 case ISD::SETONE:
779 case ISD::SETNE:
780 Opc = X86::SETNEr; // True if ZF = 0
781 break;
782 case ISD::SETUO:
783 Opc = X86::SETPr; // True if PF = 1
784 break;
785 case ISD::SETO:
786 Opc = X86::SETNPr; // True if PF = 0
787 break;
788 case ISD::SETOEQ: // !PF & ZF
789 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
790 return;
791 case ISD::SETOLT: // !PF & CF
792 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
793 return;
794 case ISD::SETOLE: // !PF & (CF || ZF)
795 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
796 return;
797 case ISD::SETUGT: // PF | (!ZF & !CF)
798 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
799 return;
800 case ISD::SETUGE: // PF | !CF
801 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
802 return;
803 case ISD::SETUNE: // PF | !ZF
804 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
805 return;
806 }
807 }
808 BuildMI(BB, Opc, 0, DestReg);
809}
810
811
812/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
813/// the Dest block if the Cond condition is true. If we cannot fold this
814/// condition into the branch, return true.
815///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000816bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
817 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000818 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
819 // B) using two conditional branches instead of one condbr, two setcc's, and
820 // an or.
821 if ((Cond.getOpcode() == ISD::OR ||
822 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
823 // And and or set the flags for us, so there is no need to emit a TST of the
824 // result. It is only safe to do this if there is only a single use of the
825 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000826 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000827 SelectExpr(Cond);
828 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
829 return false;
830 }
831
832 // Codegen br not C -> JE.
833 if (Cond.getOpcode() == ISD::XOR)
834 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
835 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000836 unsigned CondR;
837 if (getRegPressure(Chain) > getRegPressure(Cond)) {
838 Select(Chain);
839 CondR = SelectExpr(Cond.Val->getOperand(0));
840 } else {
841 CondR = SelectExpr(Cond.Val->getOperand(0));
842 Select(Chain);
843 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000844 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
845 BuildMI(BB, X86::JE, 1).addMBB(Dest);
846 return false;
847 }
848
849 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
850 if (SetCC == 0)
851 return true; // Can only handle simple setcc's so far.
852
853 unsigned Opc;
854
855 // Handle integer conditions first.
856 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
857 switch (SetCC->getCondition()) {
858 default: assert(0 && "Illegal integer SetCC!");
859 case ISD::SETEQ: Opc = X86::JE; break;
860 case ISD::SETGT: Opc = X86::JG; break;
861 case ISD::SETGE: Opc = X86::JGE; break;
862 case ISD::SETLT: Opc = X86::JL; break;
863 case ISD::SETLE: Opc = X86::JLE; break;
864 case ISD::SETNE: Opc = X86::JNE; break;
865 case ISD::SETULT: Opc = X86::JB; break;
866 case ISD::SETUGT: Opc = X86::JA; break;
867 case ISD::SETULE: Opc = X86::JBE; break;
868 case ISD::SETUGE: Opc = X86::JAE; break;
869 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000870 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000871 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000872 BuildMI(BB, Opc, 1).addMBB(Dest);
873 return false;
874 }
875
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000876 unsigned Opc2 = 0; // Second branch if needed.
877
878 // On a floating point condition, the flags are set as follows:
879 // ZF PF CF op
880 // 0 | 0 | 0 | X > Y
881 // 0 | 0 | 1 | X < Y
882 // 1 | 0 | 0 | X == Y
883 // 1 | 1 | 1 | unordered
884 //
885 switch (SetCC->getCondition()) {
886 default: assert(0 && "Invalid FP setcc!");
887 case ISD::SETUEQ:
888 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
889 case ISD::SETOGT:
890 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
891 case ISD::SETOGE:
892 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
893 case ISD::SETULT:
894 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
895 case ISD::SETULE:
896 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
897 case ISD::SETONE:
898 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
899 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
900 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
901 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
902 Opc = X86::JA; // ZF = 0 & CF = 0
903 Opc2 = X86::JP; // PF = 1
904 break;
905 case ISD::SETUGE: // PF = 1 | CF = 0
906 Opc = X86::JAE; // CF = 0
907 Opc2 = X86::JP; // PF = 1
908 break;
909 case ISD::SETUNE: // PF = 1 | ZF = 0
910 Opc = X86::JNE; // ZF = 0
911 Opc2 = X86::JP; // PF = 1
912 break;
913 case ISD::SETOEQ: // PF = 0 & ZF = 1
914 //X86::JNP, X86::JE
915 //X86::AND8rr
916 return true; // FIXME: Emit more efficient code for this branch.
917 case ISD::SETOLT: // PF = 0 & CF = 1
918 //X86::JNP, X86::JB
919 //X86::AND8rr
920 return true; // FIXME: Emit more efficient code for this branch.
921 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
922 //X86::JNP, X86::JBE
923 //X86::AND8rr
924 return true; // FIXME: Emit more efficient code for this branch.
925 }
926
Chris Lattner6c07aee2005-01-11 04:06:27 +0000927 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000928 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000929 BuildMI(BB, Opc, 1).addMBB(Dest);
930 if (Opc2)
931 BuildMI(BB, Opc2, 1).addMBB(Dest);
932 return false;
933}
934
Chris Lattner24aad1b2005-01-10 22:10:13 +0000935/// EmitSelectCC - Emit code into BB that performs a select operation between
936/// the two registers RTrue and RFalse, generating a result into RDest. Return
937/// true if the fold cannot be performed.
938///
939void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
940 unsigned RTrue, unsigned RFalse, unsigned RDest) {
941 enum Condition {
942 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
943 NOT_SET
944 } CondCode = NOT_SET;
945
946 static const unsigned CMOVTAB16[] = {
947 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
948 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
949 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
950 };
951 static const unsigned CMOVTAB32[] = {
952 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
953 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
954 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
955 };
956 static const unsigned CMOVTABFP[] = {
957 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
958 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
959 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
960 };
961
962 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
963 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
964 switch (SetCC->getCondition()) {
965 default: assert(0 && "Unknown integer comparison!");
966 case ISD::SETEQ: CondCode = EQ; break;
967 case ISD::SETGT: CondCode = GT; break;
968 case ISD::SETGE: CondCode = GE; break;
969 case ISD::SETLT: CondCode = LT; break;
970 case ISD::SETLE: CondCode = LE; break;
971 case ISD::SETNE: CondCode = NE; break;
972 case ISD::SETULT: CondCode = B; break;
973 case ISD::SETUGT: CondCode = A; break;
974 case ISD::SETULE: CondCode = BE; break;
975 case ISD::SETUGE: CondCode = AE; break;
976 }
977 } else {
978 // On a floating point condition, the flags are set as follows:
979 // ZF PF CF op
980 // 0 | 0 | 0 | X > Y
981 // 0 | 0 | 1 | X < Y
982 // 1 | 0 | 0 | X == Y
983 // 1 | 1 | 1 | unordered
984 //
985 switch (SetCC->getCondition()) {
986 default: assert(0 && "Unknown FP comparison!");
987 case ISD::SETUEQ:
988 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
989 case ISD::SETOGT:
990 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
991 case ISD::SETOGE:
992 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
993 case ISD::SETULT:
994 case ISD::SETLT: CondCode = B; break; // True if CF = 1
995 case ISD::SETULE:
996 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
997 case ISD::SETONE:
998 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
999 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1000 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1001 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1002 case ISD::SETUGE: // PF = 1 | CF = 0
1003 case ISD::SETUNE: // PF = 1 | ZF = 0
1004 case ISD::SETOEQ: // PF = 0 & ZF = 1
1005 case ISD::SETOLT: // PF = 0 & CF = 1
1006 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1007 // We cannot emit this comparison as a single cmov.
1008 break;
1009 }
1010 }
1011 }
1012
1013 unsigned Opc = 0;
1014 if (CondCode != NOT_SET) {
1015 switch (SVT) {
1016 default: assert(0 && "Cannot select this type!");
1017 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1018 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001019 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001020 }
1021 }
1022
1023 // Finally, if we weren't able to fold this, just emit the condition and test
1024 // it.
1025 if (CondCode == NOT_SET || Opc == 0) {
1026 // Get the condition into the zero flag.
1027 unsigned CondReg = SelectExpr(Cond);
1028 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1029
1030 switch (SVT) {
1031 default: assert(0 && "Cannot select this type!");
1032 case MVT::i16: Opc = X86::CMOVE16rr; break;
1033 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001034 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001035 }
1036 } else {
1037 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001038 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001039 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001040 }
1041 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1042}
1043
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001044void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001045 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001046 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1047 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001048 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001049 switch (RHS.getValueType()) {
1050 default: break;
1051 case MVT::i1:
1052 case MVT::i8: Opc = X86::CMP8mi; break;
1053 case MVT::i16: Opc = X86::CMP16mi; break;
1054 case MVT::i32: Opc = X86::CMP32mi; break;
1055 }
1056 if (Opc) {
1057 X86AddressMode AM;
1058 EmitFoldedLoad(LHS, AM);
1059 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1060 return;
1061 }
1062 }
1063
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001064 switch (RHS.getValueType()) {
1065 default: break;
1066 case MVT::i1:
1067 case MVT::i8: Opc = X86::CMP8ri; break;
1068 case MVT::i16: Opc = X86::CMP16ri; break;
1069 case MVT::i32: Opc = X86::CMP32ri; break;
1070 }
1071 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001072 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001073 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1074 return;
1075 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001076 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1077 if (CN->isExactlyValue(+0.0) ||
1078 CN->isExactlyValue(-0.0)) {
1079 unsigned Reg = SelectExpr(LHS);
1080 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1081 BuildMI(BB, X86::FNSTSW8r, 0);
1082 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001083 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001084 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001085 }
1086
Chris Lattneref6806c2005-01-12 02:02:48 +00001087 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001088 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001089 switch (RHS.getValueType()) {
1090 default: break;
1091 case MVT::i1:
1092 case MVT::i8: Opc = X86::CMP8mr; break;
1093 case MVT::i16: Opc = X86::CMP16mr; break;
1094 case MVT::i32: Opc = X86::CMP32mr; break;
1095 }
1096 if (Opc) {
1097 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001098 EmitFoldedLoad(LHS, AM);
1099 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001100 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1101 return;
1102 }
1103 }
1104
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001105 switch (LHS.getValueType()) {
1106 default: assert(0 && "Cannot compare this value!");
1107 case MVT::i1:
1108 case MVT::i8: Opc = X86::CMP8rr; break;
1109 case MVT::i16: Opc = X86::CMP16rr; break;
1110 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001111 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001112 }
Chris Lattner11333092005-01-11 03:11:44 +00001113 unsigned Tmp1, Tmp2;
1114 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1115 Tmp1 = SelectExpr(LHS);
1116 Tmp2 = SelectExpr(RHS);
1117 } else {
1118 Tmp2 = SelectExpr(RHS);
1119 Tmp1 = SelectExpr(LHS);
1120 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001121 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1122}
1123
Chris Lattnera5ade062005-01-11 21:19:59 +00001124/// isFoldableLoad - Return true if this is a load instruction that can safely
1125/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001126bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1127 if (Op.getOpcode() == ISD::LOAD) {
1128 // FIXME: currently can't fold constant pool indexes.
1129 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1130 return false;
1131 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1132 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1133 // FIXME: currently can't fold constant pool indexes.
1134 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1135 return false;
1136 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001137 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001138 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001139
1140 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001141 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1142 if (ExprMap.count(Op.getValue(1))) return false;
1143 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001144 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001145
Chris Lattner4ff348b2005-01-17 06:26:58 +00001146 // If there is not just one use of its value, we cannot fold.
1147 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1148
1149 // Finally, we cannot fold the load into the operation if this would induce a
1150 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1151 // operand of the operation we are folding the load into) can possible use the
1152 // chain node defined by the load.
1153 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1154 std::set<SDNode*> Visited;
1155 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1156 return false;
1157 }
1158 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001159}
1160
Chris Lattner4ff348b2005-01-17 06:26:58 +00001161
Chris Lattnera5ade062005-01-11 21:19:59 +00001162/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1163/// and compute the address being loaded into AM.
1164void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1165 SDOperand Chain = Op.getOperand(0);
1166 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001167
Chris Lattnera5ade062005-01-11 21:19:59 +00001168 if (getRegPressure(Chain) > getRegPressure(Address)) {
1169 Select(Chain);
1170 SelectAddress(Address, AM);
1171 } else {
1172 SelectAddress(Address, AM);
1173 Select(Chain);
1174 }
1175
1176 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001177 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1178 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001179 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001180 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001181}
1182
Chris Lattner30ea1e92005-01-19 07:37:26 +00001183// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1184// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1185// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1186// return true.
1187bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001188 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1189 // good!
1190 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1191 std::swap(Op1, Op2); // Op1 is the SHL now.
1192 } else {
1193 return false; // No match
1194 }
1195
1196 SDOperand ShlVal = Op1.getOperand(0);
1197 SDOperand ShlAmt = Op1.getOperand(1);
1198 SDOperand ShrVal = Op2.getOperand(0);
1199 SDOperand ShrAmt = Op2.getOperand(1);
1200
Chris Lattner30ea1e92005-01-19 07:37:26 +00001201 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1202
Chris Lattner85716372005-01-19 06:18:43 +00001203 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1204 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1205 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001206 if (SubCST->getValue() == RegSize) {
1207 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001208 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001209 if (ShrVal == ShlVal) {
1210 unsigned Reg, ShAmt;
1211 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1212 Reg = SelectExpr(ShrVal);
1213 ShAmt = SelectExpr(ShrAmt);
1214 } else {
1215 ShAmt = SelectExpr(ShrAmt);
1216 Reg = SelectExpr(ShrVal);
1217 }
1218 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1219 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1220 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1221 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1222 return true;
1223 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001224 unsigned AReg, BReg;
1225 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001226 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001227 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001228 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001229 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001230 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001231 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001232 unsigned ShAmt = SelectExpr(ShrAmt);
1233 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1234 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1235 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001236 return true;
1237 }
1238 }
1239
Chris Lattner4053b1e2005-01-19 08:07:05 +00001240 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1241 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1242 if (SubCST->getValue() == RegSize) {
1243 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1244 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1245 if (ShrVal == ShlVal) {
1246 unsigned Reg, ShAmt;
1247 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1248 Reg = SelectExpr(ShrVal);
1249 ShAmt = SelectExpr(ShlAmt);
1250 } else {
1251 ShAmt = SelectExpr(ShlAmt);
1252 Reg = SelectExpr(ShrVal);
1253 }
1254 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1255 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1256 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1257 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1258 return true;
1259 } else if (RegSize != 8) {
1260 unsigned AReg, BReg;
1261 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001262 AReg = SelectExpr(ShlVal);
1263 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001264 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001265 BReg = SelectExpr(ShrVal);
1266 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001267 }
1268 unsigned ShAmt = SelectExpr(ShlAmt);
1269 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1270 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1271 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1272 return true;
1273 }
1274 }
Chris Lattner85716372005-01-19 06:18:43 +00001275
Chris Lattner4053b1e2005-01-19 08:07:05 +00001276 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1277 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1278 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1279 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1280 // (A >> 5) | (A << 27) --> ROR A, 5
1281 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1282 if (ShrVal == ShlVal) {
1283 unsigned Reg = SelectExpr(ShrVal);
1284 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1285 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1286 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1287 return true;
1288 } else if (RegSize != 8) {
1289 unsigned AReg, BReg;
1290 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001291 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001292 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001293 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001294 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001295 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001296 }
1297 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1298 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1299 .addImm(ShrCst->getValue());
1300 return true;
1301 }
1302 }
1303
Chris Lattner85716372005-01-19 06:18:43 +00001304 return false;
1305}
1306
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001307unsigned ISel::SelectExpr(SDOperand N) {
1308 unsigned Result;
1309 unsigned Tmp1, Tmp2, Tmp3;
1310 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001311 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001312 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001313
Chris Lattner7f2afac2005-01-14 22:37:41 +00001314 if (Node->getOpcode() == ISD::CopyFromReg) {
1315 // FIXME: Handle copy from physregs!
1316
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001317 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001318 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001319 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001320
1321 unsigned &Reg = ExprMap[N];
1322 if (Reg) return Reg;
1323
Chris Lattnerb38a7492005-04-02 04:01:14 +00001324 switch (N.getOpcode()) {
1325 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001326 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001327 MakeReg(N.getValueType()) : 1;
1328 break;
1329 case ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001330 // If this is a call instruction, make sure to prepare ALL of the result
1331 // values as well as the chain.
Chris Lattnerb38a7492005-04-02 04:01:14 +00001332 if (Node->getNumValues() == 1)
1333 Reg = Result = 1; // Void call, just a chain.
1334 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001335 Result = MakeReg(Node->getValueType(0));
1336 ExprMap[N.getValue(0)] = Result;
Chris Lattnerb38a7492005-04-02 04:01:14 +00001337 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001338 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnerb38a7492005-04-02 04:01:14 +00001339 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001340 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00001341 break;
1342 case ISD::ADD_PARTS:
1343 case ISD::SUB_PARTS:
1344 case ISD::SHL_PARTS:
1345 case ISD::SRL_PARTS:
1346 case ISD::SRA_PARTS:
1347 Result = MakeReg(Node->getValueType(0));
1348 ExprMap[N.getValue(0)] = Result;
1349 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1350 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1351 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001352 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001353
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001354 switch (N.getOpcode()) {
1355 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001356 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001357 assert(0 && "Node not handled!\n");
1358 case ISD::FrameIndex:
1359 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1360 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1361 return Result;
1362 case ISD::ConstantPool:
1363 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1364 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1365 return Result;
1366 case ISD::ConstantFP:
1367 ContainsFPCode = true;
1368 Tmp1 = Result; // Intermediate Register
1369 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1370 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1371 Tmp1 = MakeReg(MVT::f64);
1372
1373 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1374 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1375 BuildMI(BB, X86::FLD0, 0, Tmp1);
1376 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1377 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1378 BuildMI(BB, X86::FLD1, 0, Tmp1);
1379 else
1380 assert(0 && "Unexpected constant!");
1381 if (Tmp1 != Result)
1382 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1383 return Result;
1384 case ISD::Constant:
1385 switch (N.getValueType()) {
1386 default: assert(0 && "Cannot use constants of this type!");
1387 case MVT::i1:
1388 case MVT::i8: Opc = X86::MOV8ri; break;
1389 case MVT::i16: Opc = X86::MOV16ri; break;
1390 case MVT::i32: Opc = X86::MOV32ri; break;
1391 }
1392 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1393 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001394 case ISD::UNDEF:
1395 if (Node->getValueType(0) == MVT::f64) {
1396 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1397 BuildMI(BB, X86::FLD0, 0, Result);
1398 } else {
1399 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1400 }
1401 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001402 case ISD::GlobalAddress: {
1403 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1404 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1405 return Result;
1406 }
1407 case ISD::ExternalSymbol: {
1408 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1409 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1410 return Result;
1411 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001412 case ISD::ZERO_EXTEND: {
1413 int DestIs16 = N.getValueType() == MVT::i16;
1414 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001415
1416 // FIXME: This hack is here for zero extension casts from bool to i8. This
1417 // would not be needed if bools were promoted by Legalize.
1418 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001419 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001420 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1421 return Result;
1422 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001423
Chris Lattner4ff348b2005-01-17 06:26:58 +00001424 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001425 static const unsigned Opc[3] = {
1426 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1427 };
1428
1429 X86AddressMode AM;
1430 EmitFoldedLoad(N.getOperand(0), AM);
1431 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1432
1433 return Result;
1434 }
1435
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001436 static const unsigned Opc[3] = {
1437 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1438 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001439 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001440 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1441 return Result;
1442 }
1443 case ISD::SIGN_EXTEND: {
1444 int DestIs16 = N.getValueType() == MVT::i16;
1445 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1446
Chris Lattner590d8002005-01-09 18:52:44 +00001447 // FIXME: Legalize should promote bools to i8!
1448 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1449 "Sign extend from bool not implemented!");
1450
Chris Lattner4ff348b2005-01-17 06:26:58 +00001451 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001452 static const unsigned Opc[3] = {
1453 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1454 };
1455
1456 X86AddressMode AM;
1457 EmitFoldedLoad(N.getOperand(0), AM);
1458 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1459 return Result;
1460 }
1461
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001462 static const unsigned Opc[3] = {
1463 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1464 };
1465 Tmp1 = SelectExpr(N.getOperand(0));
1466 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1467 return Result;
1468 }
1469 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001470 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001471 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001472 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001473 switch (N.getValueType()) {
1474 default: assert(0 && "Unknown truncate!");
1475 case MVT::i1:
1476 case MVT::i8: Opc = X86::MOV8rm; break;
1477 case MVT::i16: Opc = X86::MOV16rm; break;
1478 }
1479 X86AddressMode AM;
1480 EmitFoldedLoad(N.getOperand(0), AM);
1481 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1482 return Result;
1483 }
1484
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001485 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1486 // a move out of AX or AL.
1487 switch (N.getOperand(0).getValueType()) {
1488 default: assert(0 && "Unknown truncate!");
1489 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1490 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1491 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1492 }
1493 Tmp1 = SelectExpr(N.getOperand(0));
1494 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1495
1496 switch (N.getValueType()) {
1497 default: assert(0 && "Unknown truncate!");
1498 case MVT::i1:
1499 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1500 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1501 }
1502 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1503 return Result;
1504
Chris Lattner590d8002005-01-09 18:52:44 +00001505 case ISD::SINT_TO_FP:
1506 case ISD::UINT_TO_FP: {
1507 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001508 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001509
1510 // Promote the integer to a type supported by FLD. We do this because there
1511 // are no unsigned FLD instructions, so we must promote an unsigned value to
1512 // a larger signed value, then use FLD on the larger value.
1513 //
1514 MVT::ValueType PromoteType = MVT::Other;
1515 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1516 unsigned PromoteOpcode = 0;
1517 unsigned RealDestReg = Result;
1518 switch (SrcTy) {
1519 case MVT::i1:
1520 case MVT::i8:
1521 // We don't have the facilities for directly loading byte sized data from
1522 // memory (even signed). Promote it to 16 bits.
1523 PromoteType = MVT::i16;
1524 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1525 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1526 break;
1527 case MVT::i16:
1528 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1529 PromoteType = MVT::i32;
1530 PromoteOpcode = X86::MOVZX32rr16;
1531 }
1532 break;
1533 default:
1534 // Don't fild into the real destination.
1535 if (Node->getOpcode() == ISD::UINT_TO_FP)
1536 Result = MakeReg(Node->getValueType(0));
1537 break;
1538 }
1539
1540 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1541
1542 if (PromoteType != MVT::Other) {
1543 Tmp2 = MakeReg(PromoteType);
1544 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1545 SrcTy = PromoteType;
1546 Tmp1 = Tmp2;
1547 }
1548
1549 // Spill the integer to memory and reload it from there.
1550 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1551 MachineFunction *F = BB->getParent();
1552 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1553
1554 switch (SrcTy) {
1555 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001556 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001557 // FIXME: this won't work for cast [u]long to FP
1558 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1559 FrameIdx).addReg(Tmp1);
1560 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1561 FrameIdx, 4).addReg(Tmp1+1);
1562 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1563 break;
1564 case MVT::i32:
1565 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1566 FrameIdx).addReg(Tmp1);
1567 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1568 break;
1569 case MVT::i16:
1570 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1571 FrameIdx).addReg(Tmp1);
1572 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1573 break;
1574 default: break; // No promotion required.
1575 }
1576
Chris Lattner085c9952005-01-12 04:00:00 +00001577 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001578 // If this is a cast from uint -> double, we need to be careful when if
1579 // the "sign" bit is set. If so, we don't want to make a negative number,
1580 // we want to make a positive number. Emit code to add an offset if the
1581 // sign bit is set.
1582
1583 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1584 unsigned IsNeg = MakeReg(MVT::i32);
1585 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1586
1587 // Create a CP value that has the offset in one word and 0 in the other.
1588 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1589 0x4f80000000000000ULL);
1590 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1591 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1592 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1593
1594 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1595 // We need special handling for unsigned 64-bit integer sources. If the
1596 // input number has the "sign bit" set, then we loaded it incorrectly as a
1597 // negative 64-bit number. In this case, add an offset value.
1598
1599 // Emit a test instruction to see if the dynamic input value was signed.
1600 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1601
1602 // If the sign bit is set, get a pointer to an offset, otherwise get a
1603 // pointer to a zero.
1604 MachineConstantPool *CP = F->getConstantPool();
1605 unsigned Zero = MakeReg(MVT::i32);
1606 Constant *Null = Constant::getNullValue(Type::UIntTy);
1607 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1608 CP->getConstantPoolIndex(Null));
1609 unsigned Offset = MakeReg(MVT::i32);
1610 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1611
1612 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1613 CP->getConstantPoolIndex(OffsetCst));
1614 unsigned Addr = MakeReg(MVT::i32);
1615 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1616
1617 // Load the constant for an add. FIXME: this could make an 'fadd' that
1618 // reads directly from memory, but we don't support these yet.
1619 unsigned ConstReg = MakeReg(MVT::f64);
1620 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1621
1622 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1623 }
1624 return RealDestReg;
1625 }
1626 case ISD::FP_TO_SINT:
1627 case ISD::FP_TO_UINT: {
1628 // FIXME: Most of this grunt work should be done by legalize!
1629 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1630
1631 // Change the floating point control register to use "round towards zero"
1632 // mode when truncating to an integer value.
1633 //
1634 MachineFunction *F = BB->getParent();
1635 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1636 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1637
1638 // Load the old value of the high byte of the control word...
1639 unsigned HighPartOfCW = MakeReg(MVT::i8);
1640 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1641 CWFrameIdx, 1);
1642
1643 // Set the high part to be round to zero...
1644 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1645 CWFrameIdx, 1).addImm(12);
1646
1647 // Reload the modified control word now...
1648 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1649
1650 // Restore the memory image of control word to original value
1651 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1652 CWFrameIdx, 1).addReg(HighPartOfCW);
1653
1654 // We don't have the facilities for directly storing byte sized data to
1655 // memory. Promote it to 16 bits. We also must promote unsigned values to
1656 // larger classes because we only have signed FP stores.
1657 MVT::ValueType StoreClass = Node->getValueType(0);
1658 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1659 switch (StoreClass) {
1660 case MVT::i8: StoreClass = MVT::i16; break;
1661 case MVT::i16: StoreClass = MVT::i32; break;
1662 case MVT::i32: StoreClass = MVT::i64; break;
1663 // The following treatment of cLong may not be perfectly right,
1664 // but it survives chains of casts of the form
1665 // double->ulong->double.
1666 case MVT::i64: StoreClass = MVT::i64; break;
1667 default: assert(0 && "Unknown store class!");
1668 }
1669
1670 // Spill the integer to memory and reload it from there.
1671 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1672 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1673
1674 switch (StoreClass) {
1675 default: assert(0 && "Unknown store class!");
1676 case MVT::i16:
1677 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1678 break;
1679 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001680 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001681 break;
1682 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001683 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001684 break;
1685 }
1686
1687 switch (Node->getValueType(0)) {
1688 default:
1689 assert(0 && "Unknown integer type!");
1690 case MVT::i64:
1691 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001692 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001693 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1694 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1695 case MVT::i32:
1696 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1697 break;
1698 case MVT::i16:
1699 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1700 break;
1701 case MVT::i8:
1702 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1703 break;
1704 }
1705
1706 // Reload the original control word now.
1707 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1708 return Result;
1709 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001710 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001711 Op0 = N.getOperand(0);
1712 Op1 = N.getOperand(1);
1713
Chris Lattner44129b52005-01-25 20:03:11 +00001714 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001715 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001716 goto FoldAdd;
1717 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001718
Chris Lattner44129b52005-01-25 20:03:11 +00001719 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001720 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001721 switch (N.getValueType()) {
1722 default: assert(0 && "Cannot add this type!");
1723 case MVT::i1:
1724 case MVT::i8: Opc = X86::ADD8rm; break;
1725 case MVT::i16: Opc = X86::ADD16rm; break;
1726 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001727 case MVT::f64:
1728 // For F64, handle promoted load operations (from F32) as well!
1729 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1730 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001731 }
1732 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001733 EmitFoldedLoad(Op1, AM);
1734 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001735 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1736 return Result;
1737 }
1738
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001739 // See if we can codegen this as an LEA to fold operations together.
1740 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001741 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001742 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001743 MatchAddress(N, AM);
1744 ExprMap[N] = Result;
1745
1746 // If this is not just an add, emit the LEA. For a simple add (like
1747 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1748 // leave this as LEA, then peephole it to 'ADD' after two address elim
1749 // happens.
1750 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1751 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1752 X86AddressMode XAM = SelectAddrExprs(AM);
1753 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1754 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001755 }
1756 }
Chris Lattner11333092005-01-11 03:11:44 +00001757
Chris Lattnera5ade062005-01-11 21:19:59 +00001758 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001759 Opc = 0;
1760 if (CN->getValue() == 1) { // add X, 1 -> inc X
1761 switch (N.getValueType()) {
1762 default: assert(0 && "Cannot integer add this type!");
1763 case MVT::i8: Opc = X86::INC8r; break;
1764 case MVT::i16: Opc = X86::INC16r; break;
1765 case MVT::i32: Opc = X86::INC32r; break;
1766 }
1767 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1768 switch (N.getValueType()) {
1769 default: assert(0 && "Cannot integer add this type!");
1770 case MVT::i8: Opc = X86::DEC8r; break;
1771 case MVT::i16: Opc = X86::DEC16r; break;
1772 case MVT::i32: Opc = X86::DEC32r; break;
1773 }
1774 }
1775
1776 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001777 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001778 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1779 return Result;
1780 }
1781
1782 switch (N.getValueType()) {
1783 default: assert(0 && "Cannot add this type!");
1784 case MVT::i8: Opc = X86::ADD8ri; break;
1785 case MVT::i16: Opc = X86::ADD16ri; break;
1786 case MVT::i32: Opc = X86::ADD32ri; break;
1787 }
1788 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001789 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001790 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1791 return Result;
1792 }
1793 }
1794
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001795 switch (N.getValueType()) {
1796 default: assert(0 && "Cannot add this type!");
1797 case MVT::i8: Opc = X86::ADD8rr; break;
1798 case MVT::i16: Opc = X86::ADD16rr; break;
1799 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001800 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001801 }
Chris Lattner11333092005-01-11 03:11:44 +00001802
Chris Lattnera5ade062005-01-11 21:19:59 +00001803 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1804 Tmp1 = SelectExpr(Op0);
1805 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001806 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001807 Tmp2 = SelectExpr(Op1);
1808 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001809 }
1810
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001811 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1812 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001813
1814 case ISD::FABS:
1815 Tmp1 = SelectExpr(Node->getOperand(0));
1816 BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1);
1817 return Result;
1818 case ISD::FNEG:
1819 Tmp1 = SelectExpr(Node->getOperand(0));
1820 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1821 return Result;
1822
Chris Lattner8db0af12005-04-06 04:21:07 +00001823 case ISD::MULHU:
1824 switch (N.getValueType()) {
1825 default: assert(0 && "Unsupported VT!");
1826 case MVT::i8: Tmp2 = X86::MUL8r; break;
1827 case MVT::i16: Tmp2 = X86::MUL16r; break;
1828 case MVT::i32: Tmp2 = X86::MUL32r; break;
1829 }
1830 // FALL THROUGH
1831 case ISD::MULHS: {
1832 unsigned MovOpc, LowReg, HiReg;
1833 switch (N.getValueType()) {
1834 default: assert(0 && "Unsupported VT!");
1835 case MVT::i8:
1836 MovOpc = X86::MOV8rr;
1837 LowReg = X86::AL;
1838 HiReg = X86::AH;
1839 Opc = X86::IMUL8r;
1840 break;
1841 case MVT::i16:
1842 MovOpc = X86::MOV16rr;
1843 LowReg = X86::AX;
1844 HiReg = X86::DX;
1845 Opc = X86::IMUL16r;
1846 break;
1847 case MVT::i32:
1848 MovOpc = X86::MOV32rr;
1849 LowReg = X86::EAX;
1850 HiReg = X86::EDX;
1851 Opc = X86::IMUL32r;
1852 break;
1853 }
1854 if (Node->getOpcode() != ISD::MULHS)
1855 Opc = Tmp2; // Get the MULHU opcode.
1856
1857 Op0 = Node->getOperand(0);
1858 Op1 = Node->getOperand(1);
1859 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1860 Tmp1 = SelectExpr(Op0);
1861 Tmp2 = SelectExpr(Op1);
1862 } else {
1863 Tmp2 = SelectExpr(Op1);
1864 Tmp1 = SelectExpr(Op0);
1865 }
1866
1867 // FIXME: Implement folding of loads into the memory operands here!
1868 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1869 BuildMI(BB, Opc, 1).addReg(Tmp2);
1870 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1871 return Result;
1872 }
1873
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001874 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001875 case ISD::MUL:
1876 case ISD::AND:
1877 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001878 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001879 static const unsigned SUBTab[] = {
1880 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1881 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1882 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1883 };
1884 static const unsigned MULTab[] = {
1885 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1886 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1887 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1888 };
1889 static const unsigned ANDTab[] = {
1890 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1891 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1892 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1893 };
1894 static const unsigned ORTab[] = {
1895 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1896 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1897 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1898 };
1899 static const unsigned XORTab[] = {
1900 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1901 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1902 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1903 };
1904
1905 Op0 = Node->getOperand(0);
1906 Op1 = Node->getOperand(1);
1907
Chris Lattner30ea1e92005-01-19 07:37:26 +00001908 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1909 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001910 return Result;
1911
1912 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001913 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1914 if (CN->isNullValue()) { // 0 - N -> neg N
1915 switch (N.getValueType()) {
1916 default: assert(0 && "Cannot sub this type!");
1917 case MVT::i1:
1918 case MVT::i8: Opc = X86::NEG8r; break;
1919 case MVT::i16: Opc = X86::NEG16r; break;
1920 case MVT::i32: Opc = X86::NEG32r; break;
1921 }
1922 Tmp1 = SelectExpr(N.getOperand(1));
1923 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1924 return Result;
1925 }
1926
Chris Lattnera5ade062005-01-11 21:19:59 +00001927 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1928 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001929 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001930 switch (N.getValueType()) {
1931 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001932 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001933 case MVT::i8: Opc = X86::NOT8r; break;
1934 case MVT::i16: Opc = X86::NOT16r; break;
1935 case MVT::i32: Opc = X86::NOT32r; break;
1936 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001937 if (Opc) {
1938 Tmp1 = SelectExpr(Op0);
1939 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1940 return Result;
1941 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001942 }
1943
Chris Lattner2a4e5082005-01-17 06:48:02 +00001944 // Fold common multiplies into LEA instructions.
1945 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1946 switch ((int)CN->getValue()) {
1947 default: break;
1948 case 3:
1949 case 5:
1950 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001951 // Remove N from exprmap so SelectAddress doesn't get confused.
1952 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001953 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001954 SelectAddress(N, AM);
1955 // Restore it to the map.
1956 ExprMap[N] = Result;
1957 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1958 return Result;
1959 }
1960 }
1961
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001962 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001963 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001964 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001965 case MVT::i8: Opc = 0; break;
1966 case MVT::i16: Opc = 1; break;
1967 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001968 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001969 switch (Node->getOpcode()) {
1970 default: assert(0 && "Unreachable!");
1971 case ISD::SUB: Opc = SUBTab[Opc]; break;
1972 case ISD::MUL: Opc = MULTab[Opc]; break;
1973 case ISD::AND: Opc = ANDTab[Opc]; break;
1974 case ISD::OR: Opc = ORTab[Opc]; break;
1975 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001976 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001977 if (Opc) { // Can't fold MUL:i8 R, imm
1978 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001979 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1980 return Result;
1981 }
1982 }
Chris Lattner11333092005-01-11 03:11:44 +00001983
Chris Lattner44129b52005-01-25 20:03:11 +00001984 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00001985 if (Node->getOpcode() != ISD::SUB) {
1986 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001987 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001988 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00001989 // For FP, emit 'reverse' subract, with a memory operand.
1990 if (N.getValueType() == MVT::f64) {
1991 if (Op0.getOpcode() == ISD::EXTLOAD)
1992 Opc = X86::FSUBR32m;
1993 else
1994 Opc = X86::FSUBR64m;
1995
Chris Lattnera5ade062005-01-11 21:19:59 +00001996 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001997 EmitFoldedLoad(Op0, AM);
1998 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001999 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2000 return Result;
2001 }
2002 }
2003
Chris Lattner44129b52005-01-25 20:03:11 +00002004 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002005 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002006 switch (N.getValueType()) {
2007 default: assert(0 && "Cannot operate on this type!");
2008 case MVT::i1:
2009 case MVT::i8: Opc = 5; break;
2010 case MVT::i16: Opc = 6; break;
2011 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002012 // For F64, handle promoted load operations (from F32) as well!
2013 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002014 }
2015 switch (Node->getOpcode()) {
2016 default: assert(0 && "Unreachable!");
2017 case ISD::SUB: Opc = SUBTab[Opc]; break;
2018 case ISD::MUL: Opc = MULTab[Opc]; break;
2019 case ISD::AND: Opc = ANDTab[Opc]; break;
2020 case ISD::OR: Opc = ORTab[Opc]; break;
2021 case ISD::XOR: Opc = XORTab[Opc]; break;
2022 }
2023
2024 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002025 EmitFoldedLoad(Op1, AM);
2026 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002027 if (Opc) {
2028 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2029 } else {
2030 assert(Node->getOpcode() == ISD::MUL &&
2031 N.getValueType() == MVT::i8 && "Unexpected situation!");
2032 // Must use the MUL instruction, which forces use of AL.
2033 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2034 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2035 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2036 }
2037 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002038 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002039
2040 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2041 Tmp1 = SelectExpr(Op0);
2042 Tmp2 = SelectExpr(Op1);
2043 } else {
2044 Tmp2 = SelectExpr(Op1);
2045 Tmp1 = SelectExpr(Op0);
2046 }
2047
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002048 switch (N.getValueType()) {
2049 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002050 case MVT::i1:
2051 case MVT::i8: Opc = 10; break;
2052 case MVT::i16: Opc = 11; break;
2053 case MVT::i32: Opc = 12; break;
2054 case MVT::f32: Opc = 13; break;
2055 case MVT::f64: Opc = 14; break;
2056 }
2057 switch (Node->getOpcode()) {
2058 default: assert(0 && "Unreachable!");
2059 case ISD::SUB: Opc = SUBTab[Opc]; break;
2060 case ISD::MUL: Opc = MULTab[Opc]; break;
2061 case ISD::AND: Opc = ANDTab[Opc]; break;
2062 case ISD::OR: Opc = ORTab[Opc]; break;
2063 case ISD::XOR: Opc = XORTab[Opc]; break;
2064 }
2065 if (Opc) {
2066 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2067 } else {
2068 assert(Node->getOpcode() == ISD::MUL &&
2069 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002070 // Must use the MUL instruction, which forces use of AL.
2071 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2072 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2073 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002074 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002075 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002076 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002077 case ISD::ADD_PARTS:
2078 case ISD::SUB_PARTS: {
2079 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2080 "Not an i64 add/sub!");
2081 // Emit all of the operands.
2082 std::vector<unsigned> InVals;
2083 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2084 InVals.push_back(SelectExpr(N.getOperand(i)));
2085 if (N.getOpcode() == ISD::ADD_PARTS) {
2086 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2087 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2088 } else {
2089 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2090 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2091 }
2092 return Result+N.ResNo;
2093 }
2094
Chris Lattnerb38a7492005-04-02 04:01:14 +00002095 case ISD::SHL_PARTS:
2096 case ISD::SRA_PARTS:
2097 case ISD::SRL_PARTS: {
2098 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2099 "Not an i64 shift!");
2100 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2101 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2102 unsigned TmpReg = MakeReg(MVT::i32);
2103 if (N.getOpcode() == ISD::SRA_PARTS) {
2104 // If this is a SHR of a Long, then we need to do funny sign extension
2105 // stuff. TmpReg gets the value to use as the high-part if we are
2106 // shifting more than 32 bits.
2107 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2108 } else {
2109 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2110 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2111 }
2112
2113 // Initialize CL with the shift amount.
2114 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2115 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2116
2117 unsigned TmpReg2 = MakeReg(MVT::i32);
2118 unsigned TmpReg3 = MakeReg(MVT::i32);
2119 if (N.getOpcode() == ISD::SHL_PARTS) {
2120 // TmpReg2 = shld inHi, inLo
2121 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2122 .addReg(ShiftOpLo);
2123 // TmpReg3 = shl inLo, CL
2124 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
2125
2126 // Set the flags to indicate whether the shift was by more than 32 bits.
2127 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
2128
2129 // DestHi = (>32) ? TmpReg3 : TmpReg2;
2130 BuildMI(BB, X86::CMOVNE32rr, 2,
2131 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2132 // DestLo = (>32) ? TmpReg : TmpReg3;
2133 BuildMI(BB, X86::CMOVNE32rr, 2,
2134 Result).addReg(TmpReg3).addReg(TmpReg);
2135 } else {
2136 // TmpReg2 = shrd inLo, inHi
2137 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2138 .addReg(ShiftOpHi);
2139 // TmpReg3 = s[ah]r inHi, CL
2140 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
2141 : X86::SHR32rCL, 1, TmpReg3)
2142 .addReg(ShiftOpHi);
2143
2144 // Set the flags to indicate whether the shift was by more than 32 bits.
2145 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
2146
2147 // DestLo = (>32) ? TmpReg3 : TmpReg2;
2148 BuildMI(BB, X86::CMOVNE32rr, 2,
2149 Result).addReg(TmpReg2).addReg(TmpReg3);
2150
2151 // DestHi = (>32) ? TmpReg : TmpReg3;
2152 BuildMI(BB, X86::CMOVNE32rr, 2,
2153 Result+1).addReg(TmpReg3).addReg(TmpReg);
2154 }
2155 return Result+N.ResNo;
2156 }
2157
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002158 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002159 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2160 Tmp2 = SelectExpr(N.getOperand(1));
2161 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002162 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002163 Tmp3 = SelectExpr(N.getOperand(2));
2164 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002165 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002166 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2167 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002168
2169 case ISD::SDIV:
2170 case ISD::UDIV:
2171 case ISD::SREM:
2172 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002173 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2174 "We don't support this operator!");
2175
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002176 if (N.getOpcode() == ISD::SDIV)
Chris Lattner3576c842005-01-25 20:35:10 +00002177
2178 // We can fold loads into FpDIVs, but not really into any others.
2179 if (N.getValueType() == MVT::f64) {
2180 // Check for reversed and unreversed DIV.
2181 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2182 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2183 Opc = X86::FDIVR32m;
2184 else
2185 Opc = X86::FDIVR64m;
2186 X86AddressMode AM;
2187 EmitFoldedLoad(N.getOperand(0), AM);
2188 Tmp1 = SelectExpr(N.getOperand(1));
2189 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2190 return Result;
2191 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2192 N.getOperand(1).getOpcode() == ISD::LOAD) {
2193 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2194 Opc = X86::FDIV32m;
2195 else
2196 Opc = X86::FDIV64m;
2197 X86AddressMode AM;
2198 EmitFoldedLoad(N.getOperand(1), AM);
2199 Tmp1 = SelectExpr(N.getOperand(0));
2200 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2201 return Result;
2202 }
2203 }
2204
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002205 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2206 // FIXME: These special cases should be handled by the lowering impl!
2207 unsigned RHS = CN->getValue();
2208 bool isNeg = false;
2209 if ((int)RHS < 0) {
2210 isNeg = true;
2211 RHS = -RHS;
2212 }
2213 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2214 unsigned Log = log2(RHS);
2215 unsigned TmpReg = MakeReg(N.getValueType());
2216 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2217 switch (N.getValueType()) {
2218 default: assert("Unknown type to signed divide!");
2219 case MVT::i8:
2220 SAROpc = X86::SAR8ri;
2221 SHROpc = X86::SHR8ri;
2222 ADDOpc = X86::ADD8rr;
2223 NEGOpc = X86::NEG8r;
2224 break;
2225 case MVT::i16:
2226 SAROpc = X86::SAR16ri;
2227 SHROpc = X86::SHR16ri;
2228 ADDOpc = X86::ADD16rr;
2229 NEGOpc = X86::NEG16r;
2230 break;
2231 case MVT::i32:
2232 SAROpc = X86::SAR32ri;
2233 SHROpc = X86::SHR32ri;
2234 ADDOpc = X86::ADD32rr;
2235 NEGOpc = X86::NEG32r;
2236 break;
2237 }
Chris Lattner11333092005-01-11 03:11:44 +00002238 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002239 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2240 unsigned TmpReg2 = MakeReg(N.getValueType());
2241 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2242 unsigned TmpReg3 = MakeReg(N.getValueType());
2243 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
2244
2245 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2246 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2247 if (isNeg)
2248 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2249 return Result;
2250 }
2251 }
2252
Chris Lattner11333092005-01-11 03:11:44 +00002253 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2254 Tmp1 = SelectExpr(N.getOperand(0));
2255 Tmp2 = SelectExpr(N.getOperand(1));
2256 } else {
2257 Tmp2 = SelectExpr(N.getOperand(1));
2258 Tmp1 = SelectExpr(N.getOperand(0));
2259 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002260
2261 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2262 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2263 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2264 switch (N.getValueType()) {
2265 default: assert(0 && "Cannot sdiv this type!");
2266 case MVT::i8:
2267 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2268 LoReg = X86::AL;
2269 HiReg = X86::AH;
2270 MovOpcode = X86::MOV8rr;
2271 ClrOpcode = X86::MOV8ri;
2272 SExtOpcode = X86::CBW;
2273 break;
2274 case MVT::i16:
2275 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2276 LoReg = X86::AX;
2277 HiReg = X86::DX;
2278 MovOpcode = X86::MOV16rr;
2279 ClrOpcode = X86::MOV16ri;
2280 SExtOpcode = X86::CWD;
2281 break;
2282 case MVT::i32:
2283 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002284 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002285 HiReg = X86::EDX;
2286 MovOpcode = X86::MOV32rr;
2287 ClrOpcode = X86::MOV32ri;
2288 SExtOpcode = X86::CDQ;
2289 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002290 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002291 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002292 return Result;
2293 }
2294
2295 // Set up the low part.
2296 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2297
2298 if (isSigned) {
2299 // Sign extend the low part into the high part.
2300 BuildMI(BB, SExtOpcode, 0);
2301 } else {
2302 // Zero out the high part, effectively zero extending the input.
2303 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2304 }
2305
2306 // Emit the DIV/IDIV instruction.
2307 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
2308
2309 // Get the result of the divide or rem.
2310 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2311 return Result;
2312 }
2313
2314 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002315 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002316 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2317 switch (N.getValueType()) {
2318 default: assert(0 && "Cannot shift this type!");
2319 case MVT::i8: Opc = X86::ADD8rr; break;
2320 case MVT::i16: Opc = X86::ADD16rr; break;
2321 case MVT::i32: Opc = X86::ADD32rr; break;
2322 }
2323 Tmp1 = SelectExpr(N.getOperand(0));
2324 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2325 return Result;
2326 }
2327
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002328 switch (N.getValueType()) {
2329 default: assert(0 && "Cannot shift this type!");
2330 case MVT::i8: Opc = X86::SHL8ri; break;
2331 case MVT::i16: Opc = X86::SHL16ri; break;
2332 case MVT::i32: Opc = X86::SHL32ri; break;
2333 }
Chris Lattner11333092005-01-11 03:11:44 +00002334 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002335 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2336 return Result;
2337 }
Chris Lattner11333092005-01-11 03:11:44 +00002338
2339 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2340 Tmp1 = SelectExpr(N.getOperand(0));
2341 Tmp2 = SelectExpr(N.getOperand(1));
2342 } else {
2343 Tmp2 = SelectExpr(N.getOperand(1));
2344 Tmp1 = SelectExpr(N.getOperand(0));
2345 }
2346
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002347 switch (N.getValueType()) {
2348 default: assert(0 && "Cannot shift this type!");
2349 case MVT::i8 : Opc = X86::SHL8rCL; break;
2350 case MVT::i16: Opc = X86::SHL16rCL; break;
2351 case MVT::i32: Opc = X86::SHL32rCL; break;
2352 }
2353 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2354 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2355 return Result;
2356 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002357 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2358 switch (N.getValueType()) {
2359 default: assert(0 && "Cannot shift this type!");
2360 case MVT::i8: Opc = X86::SHR8ri; break;
2361 case MVT::i16: Opc = X86::SHR16ri; break;
2362 case MVT::i32: Opc = X86::SHR32ri; break;
2363 }
Chris Lattner11333092005-01-11 03:11:44 +00002364 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002365 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2366 return Result;
2367 }
Chris Lattner11333092005-01-11 03:11:44 +00002368
2369 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2370 Tmp1 = SelectExpr(N.getOperand(0));
2371 Tmp2 = SelectExpr(N.getOperand(1));
2372 } else {
2373 Tmp2 = SelectExpr(N.getOperand(1));
2374 Tmp1 = SelectExpr(N.getOperand(0));
2375 }
2376
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002377 switch (N.getValueType()) {
2378 default: assert(0 && "Cannot shift this type!");
2379 case MVT::i8 : Opc = X86::SHR8rCL; break;
2380 case MVT::i16: Opc = X86::SHR16rCL; break;
2381 case MVT::i32: Opc = X86::SHR32rCL; break;
2382 }
2383 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2384 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2385 return Result;
2386 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002387 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2388 switch (N.getValueType()) {
2389 default: assert(0 && "Cannot shift this type!");
2390 case MVT::i8: Opc = X86::SAR8ri; break;
2391 case MVT::i16: Opc = X86::SAR16ri; break;
2392 case MVT::i32: Opc = X86::SAR32ri; break;
2393 }
Chris Lattner11333092005-01-11 03:11:44 +00002394 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002395 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2396 return Result;
2397 }
Chris Lattner11333092005-01-11 03:11:44 +00002398
2399 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2400 Tmp1 = SelectExpr(N.getOperand(0));
2401 Tmp2 = SelectExpr(N.getOperand(1));
2402 } else {
2403 Tmp2 = SelectExpr(N.getOperand(1));
2404 Tmp1 = SelectExpr(N.getOperand(0));
2405 }
2406
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002407 switch (N.getValueType()) {
2408 default: assert(0 && "Cannot shift this type!");
2409 case MVT::i8 : Opc = X86::SAR8rCL; break;
2410 case MVT::i16: Opc = X86::SAR16rCL; break;
2411 case MVT::i32: Opc = X86::SAR32rCL; break;
2412 }
2413 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2414 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2415 return Result;
2416
2417 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002418 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002419 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2420 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2421 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002422 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002423 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002424 if (Result != 1) { // Generate the token
2425 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2426 assert(0 && "Load already emitted!?");
2427 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002428 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2429
Chris Lattner5188ad72005-01-08 19:28:19 +00002430 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002431 default: assert(0 && "Cannot load this type!");
2432 case MVT::i1:
2433 case MVT::i8: Opc = X86::MOV8rm; break;
2434 case MVT::i16: Opc = X86::MOV16rm; break;
2435 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002436 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2437 }
Chris Lattner11333092005-01-11 03:11:44 +00002438
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002439 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002440 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002441 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2442 } else {
2443 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002444
2445 SDOperand Chain = N.getOperand(0);
2446 SDOperand Address = N.getOperand(1);
2447 if (getRegPressure(Chain) > getRegPressure(Address)) {
2448 Select(Chain);
2449 SelectAddress(Address, AM);
2450 } else {
2451 SelectAddress(Address, AM);
2452 Select(Chain);
2453 }
2454
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002455 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2456 }
2457 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002458
2459 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2460 case ISD::ZEXTLOAD: {
2461 // Make sure we generate both values.
2462 if (Result != 1)
2463 ExprMap[N.getValue(1)] = 1; // Generate the token
2464 else
2465 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2466
Chris Lattnerda2ce112005-01-16 07:34:08 +00002467 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2468 if (Node->getValueType(0) == MVT::f64) {
2469 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2470 "Bad EXTLOAD!");
2471 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2472 CP->getIndex());
2473 return Result;
2474 }
2475
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002476 X86AddressMode AM;
2477 if (getRegPressure(Node->getOperand(0)) >
2478 getRegPressure(Node->getOperand(1))) {
2479 Select(Node->getOperand(0)); // chain
2480 SelectAddress(Node->getOperand(1), AM);
2481 } else {
2482 SelectAddress(Node->getOperand(1), AM);
2483 Select(Node->getOperand(0)); // chain
2484 }
2485
2486 switch (Node->getValueType(0)) {
2487 default: assert(0 && "Unknown type to sign extend to.");
2488 case MVT::f64:
2489 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2490 "Bad EXTLOAD!");
2491 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2492 break;
2493 case MVT::i32:
2494 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2495 default:
2496 assert(0 && "Bad zero extend!");
2497 case MVT::i1:
2498 case MVT::i8:
2499 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2500 break;
2501 case MVT::i16:
2502 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2503 break;
2504 }
2505 break;
2506 case MVT::i16:
2507 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2508 "Bad zero extend!");
2509 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2510 break;
2511 case MVT::i8:
2512 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2513 "Bad zero extend!");
2514 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2515 break;
2516 }
2517 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002518 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002519 case ISD::SEXTLOAD: {
2520 // Make sure we generate both values.
2521 if (Result != 1)
2522 ExprMap[N.getValue(1)] = 1; // Generate the token
2523 else
2524 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2525
2526 X86AddressMode AM;
2527 if (getRegPressure(Node->getOperand(0)) >
2528 getRegPressure(Node->getOperand(1))) {
2529 Select(Node->getOperand(0)); // chain
2530 SelectAddress(Node->getOperand(1), AM);
2531 } else {
2532 SelectAddress(Node->getOperand(1), AM);
2533 Select(Node->getOperand(0)); // chain
2534 }
2535
2536 switch (Node->getValueType(0)) {
2537 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2538 default: assert(0 && "Unknown type to sign extend to.");
2539 case MVT::i32:
2540 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2541 default:
2542 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2543 case MVT::i8:
2544 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2545 break;
2546 case MVT::i16:
2547 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2548 break;
2549 }
2550 break;
2551 case MVT::i16:
2552 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2553 "Cannot sign extend from bool!");
2554 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2555 break;
2556 }
2557 return Result;
2558 }
2559
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002560 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002561 // Generate both result values.
2562 if (Result != 1)
2563 ExprMap[N.getValue(1)] = 1; // Generate the token
2564 else
2565 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2566
2567 // FIXME: We are currently ignoring the requested alignment for handling
2568 // greater than the stack alignment. This will need to be revisited at some
2569 // point. Align = N.getOperand(2);
2570
2571 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2572 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2573 std::cerr << "Cannot allocate stack object with greater alignment than"
2574 << " the stack alignment yet!";
2575 abort();
2576 }
2577
2578 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002579 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002580 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2581 .addImm(CN->getValue());
2582 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002583 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2584 Select(N.getOperand(0));
2585 Tmp1 = SelectExpr(N.getOperand(1));
2586 } else {
2587 Tmp1 = SelectExpr(N.getOperand(1));
2588 Select(N.getOperand(0));
2589 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002590
2591 // Subtract size from stack pointer, thereby allocating some space.
2592 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2593 }
2594
2595 // Put a pointer to the space into the result register, by copying the stack
2596 // pointer.
2597 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2598 return Result;
2599
2600 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002601 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002602 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002603
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002604 if (GlobalAddressSDNode *GASD =
2605 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002606 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002607 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2608 } else if (ExternalSymbolSDNode *ESSDN =
2609 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002610 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002611 BuildMI(BB, X86::CALLpcrel32,
2612 1).addExternalSymbol(ESSDN->getSymbol(), true);
2613 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002614 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2615 Select(N.getOperand(0));
2616 Tmp1 = SelectExpr(N.getOperand(1));
2617 } else {
2618 Tmp1 = SelectExpr(N.getOperand(1));
2619 Select(N.getOperand(0));
2620 }
2621
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002622 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2623 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002624 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002625 default: assert(0 && "Unknown value type for call result!");
2626 case MVT::Other: return 1;
2627 case MVT::i1:
2628 case MVT::i8:
2629 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2630 break;
2631 case MVT::i16:
2632 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2633 break;
2634 case MVT::i32:
2635 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002636 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002637 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2638 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002639 case MVT::f64: // Floating-point return values live in %ST(0)
2640 ContainsFPCode = true;
2641 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2642 break;
2643 }
2644 return Result+N.ResNo;
2645 }
2646
2647 return 0;
2648}
2649
Chris Lattnere10269b2005-01-17 19:25:26 +00002650/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2651/// load/op/store instruction. If successful return true.
2652bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2653 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2654 SDOperand Chain = Node->getOperand(0);
2655 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002656 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002657
2658 // The chain has to be a load, the stored value must be an integer binary
2659 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002660 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002661 MVT::isFloatingPoint(StVal.getValueType()))
2662 return false;
2663
Chris Lattner5c659812005-01-17 22:10:42 +00002664 // Token chain must either be a factor node or the load to fold.
2665 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2666 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002667
Chris Lattner5c659812005-01-17 22:10:42 +00002668 SDOperand TheLoad;
2669
2670 // Check to see if there is a load from the same pointer that we're storing
2671 // to in either operand of the binop.
2672 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2673 StVal.getOperand(0).getOperand(1) == StPtr)
2674 TheLoad = StVal.getOperand(0);
2675 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2676 StVal.getOperand(1).getOperand(1) == StPtr)
2677 TheLoad = StVal.getOperand(1);
2678 else
2679 return false; // No matching load operand.
2680
2681 // We can only fold the load if there are no intervening side-effecting
2682 // operations. This means that the store uses the load as its token chain, or
2683 // there are only token factor nodes in between the store and load.
2684 if (Chain != TheLoad.getValue(1)) {
2685 // Okay, the other option is that we have a store referring to (possibly
2686 // nested) token factor nodes. For now, just try peeking through one level
2687 // of token factors to see if this is the case.
2688 bool ChainOk = false;
2689 if (Chain.getOpcode() == ISD::TokenFactor) {
2690 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2691 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2692 ChainOk = true;
2693 break;
2694 }
2695 }
2696
2697 if (!ChainOk) return false;
2698 }
2699
2700 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002701 return false;
2702
2703 // Make sure that one of the operands of the binop is the load, and that the
2704 // load folds into the binop.
2705 if (((StVal.getOperand(0) != TheLoad ||
2706 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2707 (StVal.getOperand(1) != TheLoad ||
2708 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2709 return false;
2710
2711 // Finally, check to see if this is one of the ops we can handle!
2712 static const unsigned ADDTAB[] = {
2713 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2714 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2715 };
2716 static const unsigned SUBTAB[] = {
2717 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2718 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2719 };
2720 static const unsigned ANDTAB[] = {
2721 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2722 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2723 };
2724 static const unsigned ORTAB[] = {
2725 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2726 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2727 };
2728 static const unsigned XORTAB[] = {
2729 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2730 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2731 };
2732 static const unsigned SHLTAB[] = {
2733 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2734 /*Have to put the reg in CL*/0, 0, 0,
2735 };
2736 static const unsigned SARTAB[] = {
2737 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2738 /*Have to put the reg in CL*/0, 0, 0,
2739 };
2740 static const unsigned SHRTAB[] = {
2741 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2742 /*Have to put the reg in CL*/0, 0, 0,
2743 };
2744
2745 const unsigned *TabPtr = 0;
2746 switch (StVal.getOpcode()) {
2747 default:
2748 std::cerr << "CANNOT [mem] op= val: ";
2749 StVal.Val->dump(); std::cerr << "\n";
2750 case ISD::MUL:
2751 case ISD::SDIV:
2752 case ISD::UDIV:
2753 case ISD::SREM:
2754 case ISD::UREM: return false;
2755
2756 case ISD::ADD: TabPtr = ADDTAB; break;
2757 case ISD::SUB: TabPtr = SUBTAB; break;
2758 case ISD::AND: TabPtr = ANDTAB; break;
2759 case ISD:: OR: TabPtr = ORTAB; break;
2760 case ISD::XOR: TabPtr = XORTAB; break;
2761 case ISD::SHL: TabPtr = SHLTAB; break;
2762 case ISD::SRA: TabPtr = SARTAB; break;
2763 case ISD::SRL: TabPtr = SHRTAB; break;
2764 }
2765
2766 // Handle: [mem] op= CST
2767 SDOperand Op0 = StVal.getOperand(0);
2768 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002769 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002770 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2771 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2772 default: break;
2773 case MVT::i1:
2774 case MVT::i8: Opc = TabPtr[0]; break;
2775 case MVT::i16: Opc = TabPtr[1]; break;
2776 case MVT::i32: Opc = TabPtr[2]; break;
2777 }
2778
2779 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002780 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2781 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002782 Select(Chain);
2783
Chris Lattnere10269b2005-01-17 19:25:26 +00002784 X86AddressMode AM;
2785 if (getRegPressure(TheLoad.getOperand(0)) >
2786 getRegPressure(TheLoad.getOperand(1))) {
2787 Select(TheLoad.getOperand(0));
2788 SelectAddress(TheLoad.getOperand(1), AM);
2789 } else {
2790 SelectAddress(TheLoad.getOperand(1), AM);
2791 Select(TheLoad.getOperand(0));
2792 }
Chris Lattner5c659812005-01-17 22:10:42 +00002793
2794 if (StVal.getOpcode() == ISD::ADD) {
2795 if (CN->getValue() == 1) {
2796 switch (Op0.getValueType()) {
2797 default: break;
2798 case MVT::i8:
2799 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2800 return true;
2801 case MVT::i16: Opc = TabPtr[1];
2802 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2803 return true;
2804 case MVT::i32: Opc = TabPtr[2];
2805 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2806 return true;
2807 }
2808 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2809 switch (Op0.getValueType()) {
2810 default: break;
2811 case MVT::i8:
2812 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2813 return true;
2814 case MVT::i16: Opc = TabPtr[1];
2815 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2816 return true;
2817 case MVT::i32: Opc = TabPtr[2];
2818 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2819 return true;
2820 }
2821 }
2822 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002823
2824 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2825 return true;
2826 }
2827 }
2828
2829 // If we have [mem] = V op [mem], try to turn it into:
2830 // [mem] = [mem] op V.
2831 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2832 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2833 StVal.getOpcode() != ISD::SRL)
2834 std::swap(Op0, Op1);
2835
2836 if (Op0 != TheLoad) return false;
2837
2838 switch (Op0.getValueType()) {
2839 default: return false;
2840 case MVT::i1:
2841 case MVT::i8: Opc = TabPtr[3]; break;
2842 case MVT::i16: Opc = TabPtr[4]; break;
2843 case MVT::i32: Opc = TabPtr[5]; break;
2844 }
Chris Lattner5c659812005-01-17 22:10:42 +00002845
Chris Lattnerb422aea2005-01-18 17:35:28 +00002846 // Table entry doesn't exist?
2847 if (Opc == 0) return false;
2848
Chris Lattner4a108662005-01-18 03:51:59 +00002849 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2850 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002851 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002852 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002853
Chris Lattnere10269b2005-01-17 19:25:26 +00002854 X86AddressMode AM;
2855 SelectAddress(TheLoad.getOperand(1), AM);
2856 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002857 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002858 return true;
2859}
2860
2861
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002862void ISel::Select(SDOperand N) {
2863 unsigned Tmp1, Tmp2, Opc;
2864
Nate Begeman85fdeb22005-03-24 04:39:54 +00002865 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002866 return; // Already selected.
2867
Chris Lattner989de032005-01-11 06:14:36 +00002868 SDNode *Node = N.Val;
2869
2870 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002871 default:
Chris Lattner989de032005-01-11 06:14:36 +00002872 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002873 assert(0 && "Node not handled yet!");
2874 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002875 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002876 if (Node->getNumOperands() == 2) {
2877 bool OneFirst =
2878 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2879 Select(Node->getOperand(OneFirst));
2880 Select(Node->getOperand(!OneFirst));
2881 } else {
2882 std::vector<std::pair<unsigned, unsigned> > OpsP;
2883 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2884 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2885 std::sort(OpsP.begin(), OpsP.end());
2886 std::reverse(OpsP.begin(), OpsP.end());
2887 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2888 Select(Node->getOperand(OpsP[i].second));
2889 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002890 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002891 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002892 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2893 Select(N.getOperand(0));
2894 Tmp1 = SelectExpr(N.getOperand(1));
2895 } else {
2896 Tmp1 = SelectExpr(N.getOperand(1));
2897 Select(N.getOperand(0));
2898 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002899 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002900
2901 if (Tmp1 != Tmp2) {
2902 switch (N.getOperand(1).getValueType()) {
2903 default: assert(0 && "Invalid type for operation!");
2904 case MVT::i1:
2905 case MVT::i8: Opc = X86::MOV8rr; break;
2906 case MVT::i16: Opc = X86::MOV16rr; break;
2907 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002908 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002909 }
2910 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2911 }
2912 return;
2913 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002914 switch (N.getNumOperands()) {
2915 default:
2916 assert(0 && "Unknown return instruction!");
2917 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002918 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2919 N.getOperand(2).getValueType() == MVT::i32 &&
2920 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002921 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2922 Tmp1 = SelectExpr(N.getOperand(1));
2923 Tmp2 = SelectExpr(N.getOperand(2));
2924 } else {
2925 Tmp2 = SelectExpr(N.getOperand(2));
2926 Tmp1 = SelectExpr(N.getOperand(1));
2927 }
2928 Select(N.getOperand(0));
2929
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002930 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2931 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2932 // Declare that EAX & EDX are live on exit.
2933 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2934 .addReg(X86::ESP);
2935 break;
2936 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002937 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2938 Select(N.getOperand(0));
2939 Tmp1 = SelectExpr(N.getOperand(1));
2940 } else {
2941 Tmp1 = SelectExpr(N.getOperand(1));
2942 Select(N.getOperand(0));
2943 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002944 switch (N.getOperand(1).getValueType()) {
2945 default: assert(0 && "All other types should have been promoted!!");
2946 case MVT::f64:
2947 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2948 // Declare that top-of-stack is live on exit
2949 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2950 break;
2951 case MVT::i32:
2952 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2953 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2954 break;
2955 }
2956 break;
2957 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002958 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002959 break;
2960 }
2961 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2962 return;
2963 case ISD::BR: {
2964 Select(N.getOperand(0));
2965 MachineBasicBlock *Dest =
2966 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2967 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2968 return;
2969 }
2970
2971 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002972 MachineBasicBlock *Dest =
2973 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002974
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002975 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2976 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002977 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2978 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2979 Select(N.getOperand(0));
2980 Tmp1 = SelectExpr(N.getOperand(1));
2981 } else {
2982 Tmp1 = SelectExpr(N.getOperand(1));
2983 Select(N.getOperand(0));
2984 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002985 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2986 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2987 }
Chris Lattner11333092005-01-11 03:11:44 +00002988
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002989 return;
2990 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002991
Chris Lattner4df0de92005-01-17 00:00:33 +00002992 case ISD::LOAD:
2993 // If this load could be folded into the only using instruction, and if it
2994 // is safe to emit the instruction here, try to do so now.
2995 if (Node->hasNUsesOfValue(1, 0)) {
2996 SDOperand TheVal = N.getValue(0);
2997 SDNode *User = 0;
2998 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2999 assert(UI != Node->use_end() && "Didn't find use!");
3000 SDNode *UN = *UI;
3001 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3002 if (UN->getOperand(i) == TheVal) {
3003 User = UN;
3004 goto FoundIt;
3005 }
3006 }
3007 FoundIt:
3008 // Only handle unary operators right now.
3009 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003010 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003011 SelectExpr(SDOperand(User, 0));
3012 return;
3013 }
3014 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003015 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003016 SelectExpr(N);
3017 return;
3018
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003019 case ISD::EXTLOAD:
3020 case ISD::SEXTLOAD:
3021 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003022 case ISD::CALL:
3023 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003024 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003025 SelectExpr(N);
3026 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003027
3028 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3029 // On X86, we can represent all types except for Bool and Float natively.
3030 X86AddressMode AM;
3031 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003032 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3033 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3034 && "Unsupported TRUNCSTORE for this target!");
3035
3036 if (StoredTy == MVT::i16) {
3037 // FIXME: This is here just to allow testing. X86 doesn't really have a
3038 // TRUNCSTORE i16 operation, but this is required for targets that do not
3039 // have 16-bit integer registers. We occasionally disable 16-bit integer
3040 // registers to test the promotion code.
3041 Select(N.getOperand(0));
3042 Tmp1 = SelectExpr(N.getOperand(1));
3043 SelectAddress(N.getOperand(2), AM);
3044
3045 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3046 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3047 return;
3048 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003049
3050 // Store of constant bool?
3051 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3052 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3053 Select(N.getOperand(0));
3054 SelectAddress(N.getOperand(2), AM);
3055 } else {
3056 SelectAddress(N.getOperand(2), AM);
3057 Select(N.getOperand(0));
3058 }
3059 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3060 return;
3061 }
3062
3063 switch (StoredTy) {
3064 default: assert(0 && "Cannot truncstore this type!");
3065 case MVT::i1: Opc = X86::MOV8mr; break;
3066 case MVT::f32: Opc = X86::FST32m; break;
3067 }
3068
3069 std::vector<std::pair<unsigned, unsigned> > RP;
3070 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3071 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3072 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3073 std::sort(RP.begin(), RP.end());
3074
Chris Lattner572dd082005-02-23 05:57:21 +00003075 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003076 for (unsigned i = 0; i != 3; ++i)
3077 switch (RP[2-i].second) {
3078 default: assert(0 && "Unknown operand number!");
3079 case 0: Select(N.getOperand(0)); break;
3080 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3081 case 2: SelectAddress(N.getOperand(2), AM); break;
3082 }
3083
3084 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3085 return;
3086 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003087 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003088 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003089
3090 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3091 Opc = 0;
3092 switch (CN->getValueType(0)) {
3093 default: assert(0 && "Invalid type for operation!");
3094 case MVT::i1:
3095 case MVT::i8: Opc = X86::MOV8mi; break;
3096 case MVT::i16: Opc = X86::MOV16mi; break;
3097 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003098 case MVT::f64: break;
3099 }
3100 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003101 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3102 Select(N.getOperand(0));
3103 SelectAddress(N.getOperand(2), AM);
3104 } else {
3105 SelectAddress(N.getOperand(2), AM);
3106 Select(N.getOperand(0));
3107 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003108 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3109 return;
3110 }
3111 }
Chris Lattner837caa72005-01-11 23:21:30 +00003112
3113 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003114 if (TryToFoldLoadOpStore(Node))
3115 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003116
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003117 switch (N.getOperand(1).getValueType()) {
3118 default: assert(0 && "Cannot store this type!");
3119 case MVT::i1:
3120 case MVT::i8: Opc = X86::MOV8mr; break;
3121 case MVT::i16: Opc = X86::MOV16mr; break;
3122 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003123 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003124 }
Chris Lattner11333092005-01-11 03:11:44 +00003125
3126 std::vector<std::pair<unsigned, unsigned> > RP;
3127 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3128 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3129 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3130 std::sort(RP.begin(), RP.end());
3131
Chris Lattner572dd082005-02-23 05:57:21 +00003132 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003133 for (unsigned i = 0; i != 3; ++i)
3134 switch (RP[2-i].second) {
3135 default: assert(0 && "Unknown operand number!");
3136 case 0: Select(N.getOperand(0)); break;
3137 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003138 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003139 }
3140
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003141 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3142 return;
3143 }
3144 case ISD::ADJCALLSTACKDOWN:
3145 case ISD::ADJCALLSTACKUP:
3146 Select(N.getOperand(0));
3147 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
3148
3149 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3150 X86::ADJCALLSTACKUP;
3151 BuildMI(BB, Opc, 1).addImm(Tmp1);
3152 return;
Chris Lattner989de032005-01-11 06:14:36 +00003153 case ISD::MEMSET: {
3154 Select(N.getOperand(0)); // Select the chain.
3155 unsigned Align =
3156 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3157 if (Align == 0) Align = 1;
3158
3159 // Turn the byte code into # iterations
3160 unsigned CountReg;
3161 unsigned Opcode;
3162 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3163 unsigned Val = ValC->getValue() & 255;
3164
3165 // If the value is a constant, then we can potentially use larger sets.
3166 switch (Align & 3) {
3167 case 2: // WORD aligned
3168 CountReg = MakeReg(MVT::i32);
3169 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3170 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3171 } else {
3172 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3173 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3174 }
3175 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3176 Opcode = X86::REP_STOSW;
3177 break;
3178 case 0: // DWORD aligned
3179 CountReg = MakeReg(MVT::i32);
3180 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3181 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3182 } else {
3183 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3184 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3185 }
3186 Val = (Val << 8) | Val;
3187 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3188 Opcode = X86::REP_STOSD;
3189 break;
3190 default: // BYTE aligned
3191 CountReg = SelectExpr(Node->getOperand(3));
3192 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3193 Opcode = X86::REP_STOSB;
3194 break;
3195 }
3196 } else {
3197 // If it's not a constant value we are storing, just fall back. We could
3198 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3199 unsigned ValReg = SelectExpr(Node->getOperand(2));
3200 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3201 CountReg = SelectExpr(Node->getOperand(3));
3202 Opcode = X86::REP_STOSB;
3203 }
3204
3205 // No matter what the alignment is, we put the source in ESI, the
3206 // destination in EDI, and the count in ECX.
3207 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3208 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3209 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3210 BuildMI(BB, Opcode, 0);
3211 return;
3212 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003213 case ISD::MEMCPY:
3214 Select(N.getOperand(0)); // Select the chain.
3215 unsigned Align =
3216 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3217 if (Align == 0) Align = 1;
3218
3219 // Turn the byte code into # iterations
3220 unsigned CountReg;
3221 unsigned Opcode;
3222 switch (Align & 3) {
3223 case 2: // WORD aligned
3224 CountReg = MakeReg(MVT::i32);
3225 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3226 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3227 } else {
3228 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3229 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3230 }
3231 Opcode = X86::REP_MOVSW;
3232 break;
3233 case 0: // DWORD aligned
3234 CountReg = MakeReg(MVT::i32);
3235 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3236 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3237 } else {
3238 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3239 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3240 }
3241 Opcode = X86::REP_MOVSD;
3242 break;
3243 default: // BYTE aligned
3244 CountReg = SelectExpr(Node->getOperand(3));
3245 Opcode = X86::REP_MOVSB;
3246 break;
3247 }
3248
3249 // No matter what the alignment is, we put the source in ESI, the
3250 // destination in EDI, and the count in ECX.
3251 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3252 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3253 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3254 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3255 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3256 BuildMI(BB, Opcode, 0);
3257 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003258 }
3259 assert(0 && "Should not be reached!");
3260}
3261
3262
3263/// createX86PatternInstructionSelector - This pass converts an LLVM function
3264/// into a machine code representation using pattern matching and a machine
3265/// description file.
3266///
3267FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
3268 return new ISel(TM);
3269}