blob: c81b7794f7c51c498df53a139b4e2015518beef5 [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000030#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// X86TargetLowering - X86 Implementation of the TargetLowering interface
35namespace {
36 class X86TargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000038 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000039 public:
40 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000042
43 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
44 setShiftAmountType(MVT::i8);
45 setSetCCResultType(MVT::i8);
Chris Lattner009b55b2005-01-19 03:36:30 +000046 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000047
48 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000049 addRegisterClass(MVT::i8, X86::R8RegisterClass);
50 addRegisterClass(MVT::i16, X86::R16RegisterClass);
51 addRegisterClass(MVT::i32, X86::R32RegisterClass);
52 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
53
54 // FIXME: Eliminate these two classes when legalize can handle promotions
55 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000056/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000057
58 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
59 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
60 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i16 , Expand);
61 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
62 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i1 , Expand);
63 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
64 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
65 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000066
67 // We don't support these yet.
68 setOperationAction(ISD::FNEG , MVT::f64 , Expand);
69 setOperationAction(ISD::FABS , MVT::f64 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000070
71 // These should be promoted to a larger select which is supported.
72/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
73 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000074
75 computeRegisterProperties();
Chris Lattner8acb1ba2005-01-07 07:49:41 +000076
77 addLegalFPImmediate(+0.0); // FLD0
78 addLegalFPImmediate(+1.0); // FLD1
79 addLegalFPImmediate(-0.0); // FLD0/FCHS
80 addLegalFPImmediate(-1.0); // FLD1/FCHS
81 }
82
83 /// LowerArguments - This hook must be implemented to indicate how we should
84 /// lower the arguments for the specified function, into the specified DAG.
85 virtual std::vector<SDOperand>
86 LowerArguments(Function &F, SelectionDAG &DAG);
87
88 /// LowerCallTo - This hook lowers an abstract call to a function into an
89 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000090 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +000091 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
92 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000093
94 virtual std::pair<SDOperand, SDOperand>
95 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
96
97 virtual std::pair<SDOperand,SDOperand>
98 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
99 const Type *ArgTy, SelectionDAG &DAG);
100
101 virtual std::pair<SDOperand, SDOperand>
102 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
103 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000104 };
105}
106
107
108std::vector<SDOperand>
109X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
110 std::vector<SDOperand> ArgValues;
111
112 // Add DAG nodes to load the arguments... On entry to a function on the X86,
113 // the stack frame looks like this:
114 //
115 // [ESP] -- return address
116 // [ESP + 4] -- first argument (leftmost lexically)
117 // [ESP + 8] -- second argument, if first argument is four bytes in size
118 // ...
119 //
120 MachineFunction &MF = DAG.getMachineFunction();
121 MachineFrameInfo *MFI = MF.getFrameInfo();
122
123 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000124 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000125 MVT::ValueType ObjectVT = getValueType(I->getType());
126 unsigned ArgIncrement = 4;
127 unsigned ObjSize;
128 switch (ObjectVT) {
129 default: assert(0 && "Unhandled argument type!");
130 case MVT::i1:
131 case MVT::i8: ObjSize = 1; break;
132 case MVT::i16: ObjSize = 2; break;
133 case MVT::i32: ObjSize = 4; break;
134 case MVT::i64: ObjSize = ArgIncrement = 8; break;
135 case MVT::f32: ObjSize = 4; break;
136 case MVT::f64: ObjSize = ArgIncrement = 8; break;
137 }
138 // Create the frame index object for this incoming parameter...
139 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
140
141 // Create the SelectionDAG nodes corresponding to a load from this parameter
142 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
143
144 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
145 // dead loads.
146 SDOperand ArgValue;
147 if (!I->use_empty())
148 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
149 else {
150 if (MVT::isInteger(ObjectVT))
151 ArgValue = DAG.getConstant(0, ObjectVT);
152 else
153 ArgValue = DAG.getConstantFP(0, ObjectVT);
154 }
155 ArgValues.push_back(ArgValue);
156
157 ArgOffset += ArgIncrement; // Move on to the next argument...
158 }
159
160 // If the function takes variable number of arguments, make a frame index for
161 // the start of the first vararg value... for expansion of llvm.va_start.
162 if (F.isVarArg())
163 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000164 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000165 return ArgValues;
166}
167
Chris Lattner5188ad72005-01-08 19:28:19 +0000168std::pair<SDOperand, SDOperand>
169X86TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000170 const Type *RetTy, bool isVarArg,
171 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000172 // Count how many bytes are to be pushed on the stack.
173 unsigned NumBytes = 0;
174
175 if (Args.empty()) {
176 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000177 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
178 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000179 } else {
180 for (unsigned i = 0, e = Args.size(); i != e; ++i)
181 switch (getValueType(Args[i].second)) {
182 default: assert(0 && "Unknown value type!");
183 case MVT::i1:
184 case MVT::i8:
185 case MVT::i16:
186 case MVT::i32:
187 case MVT::f32:
188 NumBytes += 4;
189 break;
190 case MVT::i64:
191 case MVT::f64:
192 NumBytes += 8;
193 break;
194 }
195
Chris Lattner5188ad72005-01-08 19:28:19 +0000196 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
197 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000198
199 // Arguments go on the stack in reverse order, as specified by the ABI.
200 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000201 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
202 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000203 std::vector<SDOperand> Stores;
204
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000205 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
206 unsigned ArgReg;
207 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
208 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
209
210 switch (getValueType(Args[i].second)) {
211 default: assert(0 && "Unexpected ValueType for argument!");
212 case MVT::i1:
213 case MVT::i8:
214 case MVT::i16:
215 // Promote the integer to 32 bits. If the input type is signed use a
216 // sign extend, otherwise use a zero extend.
217 if (Args[i].second->isSigned())
218 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
219 else
220 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
221
222 // FALL THROUGH
223 case MVT::i32:
224 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000225 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
226 Args[i].first, PtrOff));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000227 ArgOffset += 4;
228 break;
229 case MVT::i64:
230 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000231 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
232 Args[i].first, PtrOff));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000233 ArgOffset += 8;
234 break;
235 }
236 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000237 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000238 }
239
240 std::vector<MVT::ValueType> RetVals;
241 MVT::ValueType RetTyVT = getValueType(RetTy);
242 if (RetTyVT != MVT::isVoid)
243 RetVals.push_back(RetTyVT);
244 RetVals.push_back(MVT::Other);
245
Chris Lattner5188ad72005-01-08 19:28:19 +0000246 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000247 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000248 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
249 DAG.getConstant(NumBytes, getPointerTy()));
250 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000251}
252
Chris Lattner14824582005-01-09 00:01:27 +0000253std::pair<SDOperand, SDOperand>
254X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
255 // vastart just returns the address of the VarArgsFrameIndex slot.
256 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
257}
258
259std::pair<SDOperand,SDOperand> X86TargetLowering::
260LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
261 const Type *ArgTy, SelectionDAG &DAG) {
262 MVT::ValueType ArgVT = getValueType(ArgTy);
263 SDOperand Result;
264 if (!isVANext) {
265 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
266 } else {
267 unsigned Amt;
268 if (ArgVT == MVT::i32)
269 Amt = 4;
270 else {
271 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
272 "Other types should have been promoted for varargs!");
273 Amt = 8;
274 }
275 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
276 DAG.getConstant(Amt, VAList.getValueType()));
277 }
278 return std::make_pair(Result, Chain);
279}
280
281
282std::pair<SDOperand, SDOperand> X86TargetLowering::
283LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
284 SelectionDAG &DAG) {
285 SDOperand Result;
286 if (Depth) // Depths > 0 not supported yet!
287 Result = DAG.getConstant(0, getPointerTy());
288 else {
289 if (ReturnAddrIndex == 0) {
290 // Set up a frame object for the return address.
291 MachineFunction &MF = DAG.getMachineFunction();
292 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
293 }
294
295 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
296
297 if (!isFrameAddress)
298 // Just load the return address
299 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
300 else
301 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
302 DAG.getConstant(4, MVT::i32));
303 }
304 return std::make_pair(Result, Chain);
305}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000306
307
Chris Lattner98a8ba02005-01-18 01:06:26 +0000308namespace {
309 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
310 /// SDOperand's instead of register numbers for the leaves of the matched
311 /// tree.
312 struct X86ISelAddressMode {
313 enum {
314 RegBase,
315 FrameIndexBase,
316 } BaseType;
317
318 struct { // This is really a union, discriminated by BaseType!
319 SDOperand Reg;
320 int FrameIndex;
321 } Base;
322
323 unsigned Scale;
324 SDOperand IndexReg;
325 unsigned Disp;
326 GlobalValue *GV;
327
328 X86ISelAddressMode()
329 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
330 }
331 };
332}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000333
334
335namespace {
336 Statistic<>
337 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
338
339 //===--------------------------------------------------------------------===//
340 /// ISel - X86 specific code to select X86 machine instructions for
341 /// SelectionDAG operations.
342 ///
343 class ISel : public SelectionDAGISel {
344 /// ContainsFPCode - Every instruction we select that uses or defines a FP
345 /// register should set this to true.
346 bool ContainsFPCode;
347
348 /// X86Lowering - This object fully describes how to lower LLVM code to an
349 /// X86-specific SelectionDAG.
350 X86TargetLowering X86Lowering;
351
Chris Lattner11333092005-01-11 03:11:44 +0000352 /// RegPressureMap - This keeps an approximate count of the number of
353 /// registers required to evaluate each node in the graph.
354 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000355
356 /// ExprMap - As shared expressions are codegen'd, we keep track of which
357 /// vreg the value is produced in, so we only emit one copy of each compiled
358 /// tree.
359 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000360
361 public:
362 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
363 }
364
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000365 virtual const char *getPassName() const {
366 return "X86 Pattern Instruction Selection";
367 }
368
Chris Lattner11333092005-01-11 03:11:44 +0000369 unsigned getRegPressure(SDOperand O) {
370 return RegPressureMap[O.Val];
371 }
372 unsigned ComputeRegPressure(SDOperand O);
373
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000374 /// InstructionSelectBasicBlock - This callback is invoked by
375 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000376 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000377
Chris Lattner44129b52005-01-25 20:03:11 +0000378 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
379 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000380 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000381 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000382
Chris Lattner30ea1e92005-01-19 07:37:26 +0000383 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000384 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000385 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000386 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
387 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000388 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000389
390 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
391 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
392 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000393 void Select(SDOperand N);
394 };
395}
396
Chris Lattner7dbcb752005-01-12 04:21:28 +0000397/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
398/// when it has created a SelectionDAG for us to codegen.
399void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
400 // While we're doing this, keep track of whether we see any FP code for
401 // FP_REG_KILL insertion.
402 ContainsFPCode = false;
403
404 // Scan the PHI nodes that already are inserted into this basic block. If any
405 // of them is a PHI of a floating point value, we need to insert an
406 // FP_REG_KILL.
407 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
408 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
409 I != E; ++I) {
410 assert(I->getOpcode() == X86::PHI &&
411 "Isn't just PHI nodes?");
412 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
413 X86::RFPRegisterClass) {
414 ContainsFPCode = true;
415 break;
416 }
417 }
418
419 // Compute the RegPressureMap, which is an approximation for the number of
420 // registers required to compute each node.
421 ComputeRegPressure(DAG.getRoot());
422
423 // Codegen the basic block.
424 Select(DAG.getRoot());
425
426 // Finally, look at all of the successors of this block. If any contain a PHI
427 // node of FP type, we need to insert an FP_REG_KILL in this block.
428 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
429 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
430 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
431 I != E && I->getOpcode() == X86::PHI; ++I) {
432 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
433 X86::RFPRegisterClass) {
434 ContainsFPCode = true;
435 break;
436 }
437 }
438
439 // Insert FP_REG_KILL instructions into basic blocks that need them. This
440 // only occurs due to the floating point stackifier not being aggressive
441 // enough to handle arbitrary global stackification.
442 //
443 // Currently we insert an FP_REG_KILL instruction into each block that uses or
444 // defines a floating point virtual register.
445 //
446 // When the global register allocators (like linear scan) finally update live
447 // variable analysis, we can keep floating point values in registers across
448 // basic blocks. This will be a huge win, but we are waiting on the global
449 // allocators before we can do this.
450 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000451 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000452 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
453 ++NumFPKill;
454 }
455
456 // Clear state used for selection.
457 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000458 RegPressureMap.clear();
459}
460
461
Chris Lattner11333092005-01-11 03:11:44 +0000462// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
463// for the number of registers required to compute each node. This is basically
464// computing a generalized form of the Sethi-Ullman number for each node.
465unsigned ISel::ComputeRegPressure(SDOperand O) {
466 SDNode *N = O.Val;
467 unsigned &Result = RegPressureMap[N];
468 if (Result) return Result;
469
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000470 // FIXME: Should operations like CALL (which clobber lots o regs) have a
471 // higher fixed cost??
472
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000473 if (N->getNumOperands() == 0) {
474 Result = 1;
475 } else {
476 unsigned MaxRegUse = 0;
477 unsigned NumExtraMaxRegUsers = 0;
478 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
479 unsigned Regs;
480 if (N->getOperand(i).getOpcode() == ISD::Constant)
481 Regs = 0;
482 else
483 Regs = ComputeRegPressure(N->getOperand(i));
484 if (Regs > MaxRegUse) {
485 MaxRegUse = Regs;
486 NumExtraMaxRegUsers = 0;
487 } else if (Regs == MaxRegUse &&
488 N->getOperand(i).getValueType() != MVT::Other) {
489 ++NumExtraMaxRegUsers;
490 }
Chris Lattner11333092005-01-11 03:11:44 +0000491 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000492
493 if (O.getOpcode() != ISD::TokenFactor)
494 Result = MaxRegUse+NumExtraMaxRegUsers;
495 else
Chris Lattner869e0432005-01-17 23:02:13 +0000496 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000497 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000498
Chris Lattner837caa72005-01-11 23:21:30 +0000499 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000500 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000501}
502
Chris Lattnerbf52d492005-01-20 16:50:16 +0000503/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
504/// The DAG cannot have cycles in it, by definition, so the visited set is not
505/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
506/// reuse, so it prevents exponential cases.
507///
508static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
509 std::set<SDNode*> &Visited) {
510 if (N == Op) return true; // Found it.
511 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000512 if (Node->getNumOperands() == 0 || // Leaf?
513 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000514 if (!Visited.insert(Node).second) return false; // Already visited?
515
516 // Recurse for the first N-1 operands.
517 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
518 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
519 return true;
520
521 // Tail recurse for the last operand.
522 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
523}
524
Chris Lattner98a8ba02005-01-18 01:06:26 +0000525X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
526 X86AddressMode Result;
527
528 // If we need to emit two register operands, emit the one with the highest
529 // register pressure first.
530 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
531 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000532 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000533 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000534 std::set<SDNode*> Visited;
535 EmitBaseThenIndex = true;
536 // If Base ends up pointing to Index, we must emit index first. This is
537 // because of the way we fold loads, we may end up doing bad things with
538 // the folded add.
539 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
540 EmitBaseThenIndex = false;
541 } else {
542 std::set<SDNode*> Visited;
543 EmitBaseThenIndex = false;
544 // If Base ends up pointing to Index, we must emit index first. This is
545 // because of the way we fold loads, we may end up doing bad things with
546 // the folded add.
547 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
548 EmitBaseThenIndex = true;
549 }
550
551 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000552 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
553 Result.IndexReg = SelectExpr(IAM.IndexReg);
554 } else {
555 Result.IndexReg = SelectExpr(IAM.IndexReg);
556 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
557 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000558
Chris Lattner98a8ba02005-01-18 01:06:26 +0000559 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
560 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
561 } else if (IAM.IndexReg.Val) {
562 Result.IndexReg = SelectExpr(IAM.IndexReg);
563 }
564
565 switch (IAM.BaseType) {
566 case X86ISelAddressMode::RegBase:
567 Result.BaseType = X86AddressMode::RegBase;
568 break;
569 case X86ISelAddressMode::FrameIndexBase:
570 Result.BaseType = X86AddressMode::FrameIndexBase;
571 Result.Base.FrameIndex = IAM.Base.FrameIndex;
572 break;
573 default:
574 assert(0 && "Unknown base type!");
575 break;
576 }
577 Result.Scale = IAM.Scale;
578 Result.Disp = IAM.Disp;
579 Result.GV = IAM.GV;
580 return Result;
581}
582
583/// SelectAddress - Pattern match the maximal addressing mode for this node and
584/// emit all of the leaf registers.
585void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
586 X86ISelAddressMode IAM;
587 MatchAddress(N, IAM);
588 AM = SelectAddrExprs(IAM);
589}
590
591/// MatchAddress - Add the specified node to the specified addressing mode,
592/// returning true if it cannot be done. This just pattern matches for the
593/// addressing mode, it does not cause any code to be emitted. For that, use
594/// SelectAddress.
595bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000596 switch (N.getOpcode()) {
597 default: break;
598 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000599 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
600 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000601 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
602 return false;
603 }
604 break;
605 case ISD::GlobalAddress:
606 if (AM.GV == 0) {
607 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
608 return false;
609 }
610 break;
611 case ISD::Constant:
612 AM.Disp += cast<ConstantSDNode>(N)->getValue();
613 return false;
614 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000615 // We might have folded the load into this shift, so don't regen the value
616 // if so.
617 if (ExprMap.count(N)) break;
618
Chris Lattner98a8ba02005-01-18 01:06:26 +0000619 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000620 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
621 unsigned Val = CN->getValue();
622 if (Val == 1 || Val == 2 || Val == 3) {
623 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000624 SDOperand ShVal = N.Val->getOperand(0);
625
626 // Okay, we know that we have a scale by now. However, if the scaled
627 // value is an add of something and a constant, we can fold the
628 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000629 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000630 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000631 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000632 ConstantSDNode *AddVal =
633 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
634 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000635 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000636 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000637 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000638 return false;
639 }
640 }
641 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000642 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000643 // We might have folded the load into this mul, so don't regen the value if
644 // so.
645 if (ExprMap.count(N)) break;
646
Chris Lattner947d5442005-01-11 19:37:02 +0000647 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000648 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
649 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000650 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
651 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
652 AM.Scale = unsigned(CN->getValue())-1;
653
654 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000655 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000656
657 // Okay, we know that we have a scale by now. However, if the scaled
658 // value is an add of something and a constant, we can fold the
659 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000660 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000661 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000662 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000663 ConstantSDNode *AddVal =
664 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
665 AM.Disp += AddVal->getValue() * CN->getValue();
666 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000667 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000668 }
669
670 AM.IndexReg = AM.Base.Reg = Reg;
671 return false;
672 }
673 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000674
675 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000676 // We might have folded the load into this mul, so don't regen the value if
677 // so.
678 if (ExprMap.count(N)) break;
679
Chris Lattner98a8ba02005-01-18 01:06:26 +0000680 X86ISelAddressMode Backup = AM;
681 if (!MatchAddress(N.Val->getOperand(0), AM) &&
682 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000683 return false;
684 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000685 if (!MatchAddress(N.Val->getOperand(1), AM) &&
686 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000687 return false;
688 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000689 break;
690 }
691 }
692
Chris Lattnera95589b2005-01-11 04:40:19 +0000693 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000694 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000695 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000696 if (AM.IndexReg.Val == 0) {
697 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000698 AM.Scale = 1;
699 return false;
700 }
701
702 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000703 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000704 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000705
706 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000707 AM.BaseType = X86ISelAddressMode::RegBase;
708 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000709 return false;
710}
711
712/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
713/// assuming that the temporary registers are in the 8-bit register class.
714///
715/// Tmp1 = setcc1
716/// Tmp2 = setcc2
717/// DestReg = logicalop Tmp1, Tmp2
718///
719static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
720 unsigned SetCC2, unsigned LogicalOp,
721 unsigned DestReg) {
722 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
723 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
724 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
725 BuildMI(BB, SetCC1, 0, Tmp1);
726 BuildMI(BB, SetCC2, 0, Tmp2);
727 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
728}
729
730/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
731/// condition codes match the specified SetCCOpcode. Note that some conditions
732/// require multiple instructions to generate the correct value.
733static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
734 ISD::CondCode SetCCOpcode, bool isFP) {
735 unsigned Opc;
736 if (!isFP) {
737 switch (SetCCOpcode) {
738 default: assert(0 && "Illegal integer SetCC!");
739 case ISD::SETEQ: Opc = X86::SETEr; break;
740 case ISD::SETGT: Opc = X86::SETGr; break;
741 case ISD::SETGE: Opc = X86::SETGEr; break;
742 case ISD::SETLT: Opc = X86::SETLr; break;
743 case ISD::SETLE: Opc = X86::SETLEr; break;
744 case ISD::SETNE: Opc = X86::SETNEr; break;
745 case ISD::SETULT: Opc = X86::SETBr; break;
746 case ISD::SETUGT: Opc = X86::SETAr; break;
747 case ISD::SETULE: Opc = X86::SETBEr; break;
748 case ISD::SETUGE: Opc = X86::SETAEr; break;
749 }
750 } else {
751 // On a floating point condition, the flags are set as follows:
752 // ZF PF CF op
753 // 0 | 0 | 0 | X > Y
754 // 0 | 0 | 1 | X < Y
755 // 1 | 0 | 0 | X == Y
756 // 1 | 1 | 1 | unordered
757 //
758 switch (SetCCOpcode) {
759 default: assert(0 && "Invalid FP setcc!");
760 case ISD::SETUEQ:
761 case ISD::SETEQ:
762 Opc = X86::SETEr; // True if ZF = 1
763 break;
764 case ISD::SETOGT:
765 case ISD::SETGT:
766 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
767 break;
768 case ISD::SETOGE:
769 case ISD::SETGE:
770 Opc = X86::SETAEr; // True if CF = 0
771 break;
772 case ISD::SETULT:
773 case ISD::SETLT:
774 Opc = X86::SETBr; // True if CF = 1
775 break;
776 case ISD::SETULE:
777 case ISD::SETLE:
778 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
779 break;
780 case ISD::SETONE:
781 case ISD::SETNE:
782 Opc = X86::SETNEr; // True if ZF = 0
783 break;
784 case ISD::SETUO:
785 Opc = X86::SETPr; // True if PF = 1
786 break;
787 case ISD::SETO:
788 Opc = X86::SETNPr; // True if PF = 0
789 break;
790 case ISD::SETOEQ: // !PF & ZF
791 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
792 return;
793 case ISD::SETOLT: // !PF & CF
794 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
795 return;
796 case ISD::SETOLE: // !PF & (CF || ZF)
797 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
798 return;
799 case ISD::SETUGT: // PF | (!ZF & !CF)
800 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
801 return;
802 case ISD::SETUGE: // PF | !CF
803 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
804 return;
805 case ISD::SETUNE: // PF | !ZF
806 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
807 return;
808 }
809 }
810 BuildMI(BB, Opc, 0, DestReg);
811}
812
813
814/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
815/// the Dest block if the Cond condition is true. If we cannot fold this
816/// condition into the branch, return true.
817///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000818bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
819 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000820 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
821 // B) using two conditional branches instead of one condbr, two setcc's, and
822 // an or.
823 if ((Cond.getOpcode() == ISD::OR ||
824 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
825 // And and or set the flags for us, so there is no need to emit a TST of the
826 // result. It is only safe to do this if there is only a single use of the
827 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000828 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000829 SelectExpr(Cond);
830 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
831 return false;
832 }
833
834 // Codegen br not C -> JE.
835 if (Cond.getOpcode() == ISD::XOR)
836 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
837 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000838 unsigned CondR;
839 if (getRegPressure(Chain) > getRegPressure(Cond)) {
840 Select(Chain);
841 CondR = SelectExpr(Cond.Val->getOperand(0));
842 } else {
843 CondR = SelectExpr(Cond.Val->getOperand(0));
844 Select(Chain);
845 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000846 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
847 BuildMI(BB, X86::JE, 1).addMBB(Dest);
848 return false;
849 }
850
851 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
852 if (SetCC == 0)
853 return true; // Can only handle simple setcc's so far.
854
855 unsigned Opc;
856
857 // Handle integer conditions first.
858 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
859 switch (SetCC->getCondition()) {
860 default: assert(0 && "Illegal integer SetCC!");
861 case ISD::SETEQ: Opc = X86::JE; break;
862 case ISD::SETGT: Opc = X86::JG; break;
863 case ISD::SETGE: Opc = X86::JGE; break;
864 case ISD::SETLT: Opc = X86::JL; break;
865 case ISD::SETLE: Opc = X86::JLE; break;
866 case ISD::SETNE: Opc = X86::JNE; break;
867 case ISD::SETULT: Opc = X86::JB; break;
868 case ISD::SETUGT: Opc = X86::JA; break;
869 case ISD::SETULE: Opc = X86::JBE; break;
870 case ISD::SETUGE: Opc = X86::JAE; break;
871 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000872 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000873 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000874 BuildMI(BB, Opc, 1).addMBB(Dest);
875 return false;
876 }
877
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000878 unsigned Opc2 = 0; // Second branch if needed.
879
880 // On a floating point condition, the flags are set as follows:
881 // ZF PF CF op
882 // 0 | 0 | 0 | X > Y
883 // 0 | 0 | 1 | X < Y
884 // 1 | 0 | 0 | X == Y
885 // 1 | 1 | 1 | unordered
886 //
887 switch (SetCC->getCondition()) {
888 default: assert(0 && "Invalid FP setcc!");
889 case ISD::SETUEQ:
890 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
891 case ISD::SETOGT:
892 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
893 case ISD::SETOGE:
894 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
895 case ISD::SETULT:
896 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
897 case ISD::SETULE:
898 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
899 case ISD::SETONE:
900 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
901 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
902 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
903 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
904 Opc = X86::JA; // ZF = 0 & CF = 0
905 Opc2 = X86::JP; // PF = 1
906 break;
907 case ISD::SETUGE: // PF = 1 | CF = 0
908 Opc = X86::JAE; // CF = 0
909 Opc2 = X86::JP; // PF = 1
910 break;
911 case ISD::SETUNE: // PF = 1 | ZF = 0
912 Opc = X86::JNE; // ZF = 0
913 Opc2 = X86::JP; // PF = 1
914 break;
915 case ISD::SETOEQ: // PF = 0 & ZF = 1
916 //X86::JNP, X86::JE
917 //X86::AND8rr
918 return true; // FIXME: Emit more efficient code for this branch.
919 case ISD::SETOLT: // PF = 0 & CF = 1
920 //X86::JNP, X86::JB
921 //X86::AND8rr
922 return true; // FIXME: Emit more efficient code for this branch.
923 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
924 //X86::JNP, X86::JBE
925 //X86::AND8rr
926 return true; // FIXME: Emit more efficient code for this branch.
927 }
928
Chris Lattner6c07aee2005-01-11 04:06:27 +0000929 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000930 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000931 BuildMI(BB, Opc, 1).addMBB(Dest);
932 if (Opc2)
933 BuildMI(BB, Opc2, 1).addMBB(Dest);
934 return false;
935}
936
Chris Lattner24aad1b2005-01-10 22:10:13 +0000937/// EmitSelectCC - Emit code into BB that performs a select operation between
938/// the two registers RTrue and RFalse, generating a result into RDest. Return
939/// true if the fold cannot be performed.
940///
941void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
942 unsigned RTrue, unsigned RFalse, unsigned RDest) {
943 enum Condition {
944 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
945 NOT_SET
946 } CondCode = NOT_SET;
947
948 static const unsigned CMOVTAB16[] = {
949 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
950 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
951 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
952 };
953 static const unsigned CMOVTAB32[] = {
954 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
955 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
956 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
957 };
958 static const unsigned CMOVTABFP[] = {
959 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
960 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
961 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
962 };
963
964 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
965 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
966 switch (SetCC->getCondition()) {
967 default: assert(0 && "Unknown integer comparison!");
968 case ISD::SETEQ: CondCode = EQ; break;
969 case ISD::SETGT: CondCode = GT; break;
970 case ISD::SETGE: CondCode = GE; break;
971 case ISD::SETLT: CondCode = LT; break;
972 case ISD::SETLE: CondCode = LE; break;
973 case ISD::SETNE: CondCode = NE; break;
974 case ISD::SETULT: CondCode = B; break;
975 case ISD::SETUGT: CondCode = A; break;
976 case ISD::SETULE: CondCode = BE; break;
977 case ISD::SETUGE: CondCode = AE; break;
978 }
979 } else {
980 // On a floating point condition, the flags are set as follows:
981 // ZF PF CF op
982 // 0 | 0 | 0 | X > Y
983 // 0 | 0 | 1 | X < Y
984 // 1 | 0 | 0 | X == Y
985 // 1 | 1 | 1 | unordered
986 //
987 switch (SetCC->getCondition()) {
988 default: assert(0 && "Unknown FP comparison!");
989 case ISD::SETUEQ:
990 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
991 case ISD::SETOGT:
992 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
993 case ISD::SETOGE:
994 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
995 case ISD::SETULT:
996 case ISD::SETLT: CondCode = B; break; // True if CF = 1
997 case ISD::SETULE:
998 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
999 case ISD::SETONE:
1000 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1001 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1002 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1003 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1004 case ISD::SETUGE: // PF = 1 | CF = 0
1005 case ISD::SETUNE: // PF = 1 | ZF = 0
1006 case ISD::SETOEQ: // PF = 0 & ZF = 1
1007 case ISD::SETOLT: // PF = 0 & CF = 1
1008 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1009 // We cannot emit this comparison as a single cmov.
1010 break;
1011 }
1012 }
1013 }
1014
1015 unsigned Opc = 0;
1016 if (CondCode != NOT_SET) {
1017 switch (SVT) {
1018 default: assert(0 && "Cannot select this type!");
1019 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1020 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001021 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001022 }
1023 }
1024
1025 // Finally, if we weren't able to fold this, just emit the condition and test
1026 // it.
1027 if (CondCode == NOT_SET || Opc == 0) {
1028 // Get the condition into the zero flag.
1029 unsigned CondReg = SelectExpr(Cond);
1030 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1031
1032 switch (SVT) {
1033 default: assert(0 && "Cannot select this type!");
1034 case MVT::i16: Opc = X86::CMOVE16rr; break;
1035 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001036 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001037 }
1038 } else {
1039 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001040 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001041 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001042 }
1043 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1044}
1045
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001046void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001047 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001048 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1049 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001050 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001051 switch (RHS.getValueType()) {
1052 default: break;
1053 case MVT::i1:
1054 case MVT::i8: Opc = X86::CMP8mi; break;
1055 case MVT::i16: Opc = X86::CMP16mi; break;
1056 case MVT::i32: Opc = X86::CMP32mi; break;
1057 }
1058 if (Opc) {
1059 X86AddressMode AM;
1060 EmitFoldedLoad(LHS, AM);
1061 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1062 return;
1063 }
1064 }
1065
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001066 switch (RHS.getValueType()) {
1067 default: break;
1068 case MVT::i1:
1069 case MVT::i8: Opc = X86::CMP8ri; break;
1070 case MVT::i16: Opc = X86::CMP16ri; break;
1071 case MVT::i32: Opc = X86::CMP32ri; break;
1072 }
1073 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001074 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001075 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1076 return;
1077 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001078 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1079 if (CN->isExactlyValue(+0.0) ||
1080 CN->isExactlyValue(-0.0)) {
1081 unsigned Reg = SelectExpr(LHS);
1082 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1083 BuildMI(BB, X86::FNSTSW8r, 0);
1084 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001085 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001086 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001087 }
1088
Chris Lattneref6806c2005-01-12 02:02:48 +00001089 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001090 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001091 switch (RHS.getValueType()) {
1092 default: break;
1093 case MVT::i1:
1094 case MVT::i8: Opc = X86::CMP8mr; break;
1095 case MVT::i16: Opc = X86::CMP16mr; break;
1096 case MVT::i32: Opc = X86::CMP32mr; break;
1097 }
1098 if (Opc) {
1099 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001100 EmitFoldedLoad(LHS, AM);
1101 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001102 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1103 return;
1104 }
1105 }
1106
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001107 switch (LHS.getValueType()) {
1108 default: assert(0 && "Cannot compare this value!");
1109 case MVT::i1:
1110 case MVT::i8: Opc = X86::CMP8rr; break;
1111 case MVT::i16: Opc = X86::CMP16rr; break;
1112 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001113 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001114 }
Chris Lattner11333092005-01-11 03:11:44 +00001115 unsigned Tmp1, Tmp2;
1116 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1117 Tmp1 = SelectExpr(LHS);
1118 Tmp2 = SelectExpr(RHS);
1119 } else {
1120 Tmp2 = SelectExpr(RHS);
1121 Tmp1 = SelectExpr(LHS);
1122 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001123 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1124}
1125
Chris Lattnera5ade062005-01-11 21:19:59 +00001126/// isFoldableLoad - Return true if this is a load instruction that can safely
1127/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001128bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1129 if (Op.getOpcode() == ISD::LOAD) {
1130 // FIXME: currently can't fold constant pool indexes.
1131 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1132 return false;
1133 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1134 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1135 // FIXME: currently can't fold constant pool indexes.
1136 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1137 return false;
1138 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001139 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001140 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001141
1142 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001143 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1144 if (ExprMap.count(Op.getValue(1))) return false;
1145 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001146 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001147
Chris Lattner4ff348b2005-01-17 06:26:58 +00001148 // If there is not just one use of its value, we cannot fold.
1149 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1150
1151 // Finally, we cannot fold the load into the operation if this would induce a
1152 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1153 // operand of the operation we are folding the load into) can possible use the
1154 // chain node defined by the load.
1155 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1156 std::set<SDNode*> Visited;
1157 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1158 return false;
1159 }
1160 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001161}
1162
Chris Lattner4ff348b2005-01-17 06:26:58 +00001163
Chris Lattnera5ade062005-01-11 21:19:59 +00001164/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1165/// and compute the address being loaded into AM.
1166void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1167 SDOperand Chain = Op.getOperand(0);
1168 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001169
Chris Lattnera5ade062005-01-11 21:19:59 +00001170 if (getRegPressure(Chain) > getRegPressure(Address)) {
1171 Select(Chain);
1172 SelectAddress(Address, AM);
1173 } else {
1174 SelectAddress(Address, AM);
1175 Select(Chain);
1176 }
1177
1178 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001179 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1180 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001181 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001182 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001183}
1184
Chris Lattner30ea1e92005-01-19 07:37:26 +00001185// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1186// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1187// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1188// return true.
1189bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001190 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1191 // good!
1192 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1193 std::swap(Op1, Op2); // Op1 is the SHL now.
1194 } else {
1195 return false; // No match
1196 }
1197
1198 SDOperand ShlVal = Op1.getOperand(0);
1199 SDOperand ShlAmt = Op1.getOperand(1);
1200 SDOperand ShrVal = Op2.getOperand(0);
1201 SDOperand ShrAmt = Op2.getOperand(1);
1202
Chris Lattner30ea1e92005-01-19 07:37:26 +00001203 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1204
Chris Lattner85716372005-01-19 06:18:43 +00001205 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1206 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1207 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001208 if (SubCST->getValue() == RegSize) {
1209 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001210 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001211 if (ShrVal == ShlVal) {
1212 unsigned Reg, ShAmt;
1213 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1214 Reg = SelectExpr(ShrVal);
1215 ShAmt = SelectExpr(ShrAmt);
1216 } else {
1217 ShAmt = SelectExpr(ShrAmt);
1218 Reg = SelectExpr(ShrVal);
1219 }
1220 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1221 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1222 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1223 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1224 return true;
1225 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001226 unsigned AReg, BReg;
1227 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001228 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001229 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001230 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001231 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001232 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001233 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001234 unsigned ShAmt = SelectExpr(ShrAmt);
1235 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1236 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1237 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001238 return true;
1239 }
1240 }
1241
Chris Lattner4053b1e2005-01-19 08:07:05 +00001242 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1243 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1244 if (SubCST->getValue() == RegSize) {
1245 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1246 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1247 if (ShrVal == ShlVal) {
1248 unsigned Reg, ShAmt;
1249 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1250 Reg = SelectExpr(ShrVal);
1251 ShAmt = SelectExpr(ShlAmt);
1252 } else {
1253 ShAmt = SelectExpr(ShlAmt);
1254 Reg = SelectExpr(ShrVal);
1255 }
1256 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1257 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1258 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1259 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1260 return true;
1261 } else if (RegSize != 8) {
1262 unsigned AReg, BReg;
1263 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001264 AReg = SelectExpr(ShlVal);
1265 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001266 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001267 BReg = SelectExpr(ShrVal);
1268 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001269 }
1270 unsigned ShAmt = SelectExpr(ShlAmt);
1271 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1272 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1273 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1274 return true;
1275 }
1276 }
Chris Lattner85716372005-01-19 06:18:43 +00001277
Chris Lattner4053b1e2005-01-19 08:07:05 +00001278 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1279 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1280 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1281 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1282 // (A >> 5) | (A << 27) --> ROR A, 5
1283 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1284 if (ShrVal == ShlVal) {
1285 unsigned Reg = SelectExpr(ShrVal);
1286 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1287 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1288 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1289 return true;
1290 } else if (RegSize != 8) {
1291 unsigned AReg, BReg;
1292 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001293 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001294 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001295 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001296 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001297 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001298 }
1299 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1300 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1301 .addImm(ShrCst->getValue());
1302 return true;
1303 }
1304 }
1305
Chris Lattner85716372005-01-19 06:18:43 +00001306 return false;
1307}
1308
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001309unsigned ISel::SelectExpr(SDOperand N) {
1310 unsigned Result;
1311 unsigned Tmp1, Tmp2, Tmp3;
1312 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001313 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001314 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001315
Chris Lattner7f2afac2005-01-14 22:37:41 +00001316 if (Node->getOpcode() == ISD::CopyFromReg) {
1317 // FIXME: Handle copy from physregs!
1318
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001319 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001320 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001321 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001322
1323 unsigned &Reg = ExprMap[N];
1324 if (Reg) return Reg;
1325
Chris Lattnerb38a7492005-04-02 04:01:14 +00001326 switch (N.getOpcode()) {
1327 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001328 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001329 MakeReg(N.getValueType()) : 1;
1330 break;
1331 case ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001332 // If this is a call instruction, make sure to prepare ALL of the result
1333 // values as well as the chain.
Chris Lattnerb38a7492005-04-02 04:01:14 +00001334 if (Node->getNumValues() == 1)
1335 Reg = Result = 1; // Void call, just a chain.
1336 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001337 Result = MakeReg(Node->getValueType(0));
1338 ExprMap[N.getValue(0)] = Result;
Chris Lattnerb38a7492005-04-02 04:01:14 +00001339 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001340 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnerb38a7492005-04-02 04:01:14 +00001341 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001342 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00001343 break;
1344 case ISD::ADD_PARTS:
1345 case ISD::SUB_PARTS:
1346 case ISD::SHL_PARTS:
1347 case ISD::SRL_PARTS:
1348 case ISD::SRA_PARTS:
1349 Result = MakeReg(Node->getValueType(0));
1350 ExprMap[N.getValue(0)] = Result;
1351 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1352 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1353 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001354 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001355
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001356 switch (N.getOpcode()) {
1357 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001358 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001359 assert(0 && "Node not handled!\n");
1360 case ISD::FrameIndex:
1361 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1362 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1363 return Result;
1364 case ISD::ConstantPool:
1365 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1366 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1367 return Result;
1368 case ISD::ConstantFP:
1369 ContainsFPCode = true;
1370 Tmp1 = Result; // Intermediate Register
1371 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1372 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1373 Tmp1 = MakeReg(MVT::f64);
1374
1375 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1376 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1377 BuildMI(BB, X86::FLD0, 0, Tmp1);
1378 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1379 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1380 BuildMI(BB, X86::FLD1, 0, Tmp1);
1381 else
1382 assert(0 && "Unexpected constant!");
1383 if (Tmp1 != Result)
1384 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1385 return Result;
1386 case ISD::Constant:
1387 switch (N.getValueType()) {
1388 default: assert(0 && "Cannot use constants of this type!");
1389 case MVT::i1:
1390 case MVT::i8: Opc = X86::MOV8ri; break;
1391 case MVT::i16: Opc = X86::MOV16ri; break;
1392 case MVT::i32: Opc = X86::MOV32ri; break;
1393 }
1394 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1395 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001396 case ISD::UNDEF:
1397 if (Node->getValueType(0) == MVT::f64) {
1398 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1399 BuildMI(BB, X86::FLD0, 0, Result);
1400 } else {
1401 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1402 }
1403 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001404 case ISD::GlobalAddress: {
1405 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1406 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1407 return Result;
1408 }
1409 case ISD::ExternalSymbol: {
1410 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1411 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1412 return Result;
1413 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001414 case ISD::ZERO_EXTEND: {
1415 int DestIs16 = N.getValueType() == MVT::i16;
1416 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001417
1418 // FIXME: This hack is here for zero extension casts from bool to i8. This
1419 // would not be needed if bools were promoted by Legalize.
1420 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001421 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001422 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1423 return Result;
1424 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001425
Chris Lattner4ff348b2005-01-17 06:26:58 +00001426 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001427 static const unsigned Opc[3] = {
1428 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1429 };
1430
1431 X86AddressMode AM;
1432 EmitFoldedLoad(N.getOperand(0), AM);
1433 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1434
1435 return Result;
1436 }
1437
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001438 static const unsigned Opc[3] = {
1439 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1440 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001441 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001442 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1443 return Result;
1444 }
1445 case ISD::SIGN_EXTEND: {
1446 int DestIs16 = N.getValueType() == MVT::i16;
1447 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1448
Chris Lattner590d8002005-01-09 18:52:44 +00001449 // FIXME: Legalize should promote bools to i8!
1450 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1451 "Sign extend from bool not implemented!");
1452
Chris Lattner4ff348b2005-01-17 06:26:58 +00001453 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001454 static const unsigned Opc[3] = {
1455 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1456 };
1457
1458 X86AddressMode AM;
1459 EmitFoldedLoad(N.getOperand(0), AM);
1460 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1461 return Result;
1462 }
1463
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001464 static const unsigned Opc[3] = {
1465 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1466 };
1467 Tmp1 = SelectExpr(N.getOperand(0));
1468 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1469 return Result;
1470 }
1471 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001472 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001473 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001474 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001475 switch (N.getValueType()) {
1476 default: assert(0 && "Unknown truncate!");
1477 case MVT::i1:
1478 case MVT::i8: Opc = X86::MOV8rm; break;
1479 case MVT::i16: Opc = X86::MOV16rm; break;
1480 }
1481 X86AddressMode AM;
1482 EmitFoldedLoad(N.getOperand(0), AM);
1483 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1484 return Result;
1485 }
1486
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001487 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1488 // a move out of AX or AL.
1489 switch (N.getOperand(0).getValueType()) {
1490 default: assert(0 && "Unknown truncate!");
1491 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1492 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1493 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1494 }
1495 Tmp1 = SelectExpr(N.getOperand(0));
1496 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1497
1498 switch (N.getValueType()) {
1499 default: assert(0 && "Unknown truncate!");
1500 case MVT::i1:
1501 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1502 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1503 }
1504 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1505 return Result;
1506
Chris Lattner590d8002005-01-09 18:52:44 +00001507 case ISD::SINT_TO_FP:
1508 case ISD::UINT_TO_FP: {
1509 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001510 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001511
1512 // Promote the integer to a type supported by FLD. We do this because there
1513 // are no unsigned FLD instructions, so we must promote an unsigned value to
1514 // a larger signed value, then use FLD on the larger value.
1515 //
1516 MVT::ValueType PromoteType = MVT::Other;
1517 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1518 unsigned PromoteOpcode = 0;
1519 unsigned RealDestReg = Result;
1520 switch (SrcTy) {
1521 case MVT::i1:
1522 case MVT::i8:
1523 // We don't have the facilities for directly loading byte sized data from
1524 // memory (even signed). Promote it to 16 bits.
1525 PromoteType = MVT::i16;
1526 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1527 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1528 break;
1529 case MVT::i16:
1530 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1531 PromoteType = MVT::i32;
1532 PromoteOpcode = X86::MOVZX32rr16;
1533 }
1534 break;
1535 default:
1536 // Don't fild into the real destination.
1537 if (Node->getOpcode() == ISD::UINT_TO_FP)
1538 Result = MakeReg(Node->getValueType(0));
1539 break;
1540 }
1541
1542 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1543
1544 if (PromoteType != MVT::Other) {
1545 Tmp2 = MakeReg(PromoteType);
1546 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1547 SrcTy = PromoteType;
1548 Tmp1 = Tmp2;
1549 }
1550
1551 // Spill the integer to memory and reload it from there.
1552 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1553 MachineFunction *F = BB->getParent();
1554 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1555
1556 switch (SrcTy) {
1557 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001558 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001559 // FIXME: this won't work for cast [u]long to FP
1560 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1561 FrameIdx).addReg(Tmp1);
1562 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1563 FrameIdx, 4).addReg(Tmp1+1);
1564 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1565 break;
1566 case MVT::i32:
1567 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1568 FrameIdx).addReg(Tmp1);
1569 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1570 break;
1571 case MVT::i16:
1572 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1573 FrameIdx).addReg(Tmp1);
1574 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1575 break;
1576 default: break; // No promotion required.
1577 }
1578
Chris Lattner085c9952005-01-12 04:00:00 +00001579 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001580 // If this is a cast from uint -> double, we need to be careful when if
1581 // the "sign" bit is set. If so, we don't want to make a negative number,
1582 // we want to make a positive number. Emit code to add an offset if the
1583 // sign bit is set.
1584
1585 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1586 unsigned IsNeg = MakeReg(MVT::i32);
1587 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1588
1589 // Create a CP value that has the offset in one word and 0 in the other.
1590 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1591 0x4f80000000000000ULL);
1592 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1593 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1594 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1595
1596 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1597 // We need special handling for unsigned 64-bit integer sources. If the
1598 // input number has the "sign bit" set, then we loaded it incorrectly as a
1599 // negative 64-bit number. In this case, add an offset value.
1600
1601 // Emit a test instruction to see if the dynamic input value was signed.
1602 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1603
1604 // If the sign bit is set, get a pointer to an offset, otherwise get a
1605 // pointer to a zero.
1606 MachineConstantPool *CP = F->getConstantPool();
1607 unsigned Zero = MakeReg(MVT::i32);
1608 Constant *Null = Constant::getNullValue(Type::UIntTy);
1609 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1610 CP->getConstantPoolIndex(Null));
1611 unsigned Offset = MakeReg(MVT::i32);
1612 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1613
1614 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1615 CP->getConstantPoolIndex(OffsetCst));
1616 unsigned Addr = MakeReg(MVT::i32);
1617 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1618
1619 // Load the constant for an add. FIXME: this could make an 'fadd' that
1620 // reads directly from memory, but we don't support these yet.
1621 unsigned ConstReg = MakeReg(MVT::f64);
1622 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1623
1624 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1625 }
1626 return RealDestReg;
1627 }
1628 case ISD::FP_TO_SINT:
1629 case ISD::FP_TO_UINT: {
1630 // FIXME: Most of this grunt work should be done by legalize!
1631 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1632
1633 // Change the floating point control register to use "round towards zero"
1634 // mode when truncating to an integer value.
1635 //
1636 MachineFunction *F = BB->getParent();
1637 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1638 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1639
1640 // Load the old value of the high byte of the control word...
1641 unsigned HighPartOfCW = MakeReg(MVT::i8);
1642 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1643 CWFrameIdx, 1);
1644
1645 // Set the high part to be round to zero...
1646 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1647 CWFrameIdx, 1).addImm(12);
1648
1649 // Reload the modified control word now...
1650 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1651
1652 // Restore the memory image of control word to original value
1653 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1654 CWFrameIdx, 1).addReg(HighPartOfCW);
1655
1656 // We don't have the facilities for directly storing byte sized data to
1657 // memory. Promote it to 16 bits. We also must promote unsigned values to
1658 // larger classes because we only have signed FP stores.
1659 MVT::ValueType StoreClass = Node->getValueType(0);
1660 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1661 switch (StoreClass) {
1662 case MVT::i8: StoreClass = MVT::i16; break;
1663 case MVT::i16: StoreClass = MVT::i32; break;
1664 case MVT::i32: StoreClass = MVT::i64; break;
1665 // The following treatment of cLong may not be perfectly right,
1666 // but it survives chains of casts of the form
1667 // double->ulong->double.
1668 case MVT::i64: StoreClass = MVT::i64; break;
1669 default: assert(0 && "Unknown store class!");
1670 }
1671
1672 // Spill the integer to memory and reload it from there.
1673 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1674 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1675
1676 switch (StoreClass) {
1677 default: assert(0 && "Unknown store class!");
1678 case MVT::i16:
1679 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1680 break;
1681 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001682 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001683 break;
1684 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001685 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001686 break;
1687 }
1688
1689 switch (Node->getValueType(0)) {
1690 default:
1691 assert(0 && "Unknown integer type!");
1692 case MVT::i64:
1693 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001694 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001695 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1696 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1697 case MVT::i32:
1698 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1699 break;
1700 case MVT::i16:
1701 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1702 break;
1703 case MVT::i8:
1704 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1705 break;
1706 }
1707
1708 // Reload the original control word now.
1709 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1710 return Result;
1711 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001712 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001713 Op0 = N.getOperand(0);
1714 Op1 = N.getOperand(1);
1715
Chris Lattner44129b52005-01-25 20:03:11 +00001716 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001717 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001718 goto FoldAdd;
1719 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001720
Chris Lattner44129b52005-01-25 20:03:11 +00001721 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001722 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001723 switch (N.getValueType()) {
1724 default: assert(0 && "Cannot add this type!");
1725 case MVT::i1:
1726 case MVT::i8: Opc = X86::ADD8rm; break;
1727 case MVT::i16: Opc = X86::ADD16rm; break;
1728 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001729 case MVT::f64:
1730 // For F64, handle promoted load operations (from F32) as well!
1731 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1732 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001733 }
1734 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001735 EmitFoldedLoad(Op1, AM);
1736 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001737 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1738 return Result;
1739 }
1740
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001741 // See if we can codegen this as an LEA to fold operations together.
1742 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001743 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001744 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001745 MatchAddress(N, AM);
1746 ExprMap[N] = Result;
1747
1748 // If this is not just an add, emit the LEA. For a simple add (like
1749 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1750 // leave this as LEA, then peephole it to 'ADD' after two address elim
1751 // happens.
1752 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1753 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1754 X86AddressMode XAM = SelectAddrExprs(AM);
1755 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1756 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001757 }
1758 }
Chris Lattner11333092005-01-11 03:11:44 +00001759
Chris Lattnera5ade062005-01-11 21:19:59 +00001760 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001761 Opc = 0;
1762 if (CN->getValue() == 1) { // add X, 1 -> inc X
1763 switch (N.getValueType()) {
1764 default: assert(0 && "Cannot integer add this type!");
1765 case MVT::i8: Opc = X86::INC8r; break;
1766 case MVT::i16: Opc = X86::INC16r; break;
1767 case MVT::i32: Opc = X86::INC32r; break;
1768 }
1769 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1770 switch (N.getValueType()) {
1771 default: assert(0 && "Cannot integer add this type!");
1772 case MVT::i8: Opc = X86::DEC8r; break;
1773 case MVT::i16: Opc = X86::DEC16r; break;
1774 case MVT::i32: Opc = X86::DEC32r; break;
1775 }
1776 }
1777
1778 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001779 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001780 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1781 return Result;
1782 }
1783
1784 switch (N.getValueType()) {
1785 default: assert(0 && "Cannot add this type!");
1786 case MVT::i8: Opc = X86::ADD8ri; break;
1787 case MVT::i16: Opc = X86::ADD16ri; break;
1788 case MVT::i32: Opc = X86::ADD32ri; break;
1789 }
1790 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001791 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001792 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1793 return Result;
1794 }
1795 }
1796
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001797 switch (N.getValueType()) {
1798 default: assert(0 && "Cannot add this type!");
1799 case MVT::i8: Opc = X86::ADD8rr; break;
1800 case MVT::i16: Opc = X86::ADD16rr; break;
1801 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001802 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001803 }
Chris Lattner11333092005-01-11 03:11:44 +00001804
Chris Lattnera5ade062005-01-11 21:19:59 +00001805 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1806 Tmp1 = SelectExpr(Op0);
1807 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001808 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001809 Tmp2 = SelectExpr(Op1);
1810 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001811 }
1812
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001813 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1814 return Result;
1815 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001816 case ISD::MUL:
1817 case ISD::AND:
1818 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001819 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001820 static const unsigned SUBTab[] = {
1821 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1822 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1823 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1824 };
1825 static const unsigned MULTab[] = {
1826 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1827 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1828 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1829 };
1830 static const unsigned ANDTab[] = {
1831 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1832 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1833 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1834 };
1835 static const unsigned ORTab[] = {
1836 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1837 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1838 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1839 };
1840 static const unsigned XORTab[] = {
1841 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1842 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1843 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1844 };
1845
1846 Op0 = Node->getOperand(0);
1847 Op1 = Node->getOperand(1);
1848
Chris Lattner30ea1e92005-01-19 07:37:26 +00001849 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1850 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001851 return Result;
1852
1853 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001854 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1855 if (CN->isNullValue()) { // 0 - N -> neg N
1856 switch (N.getValueType()) {
1857 default: assert(0 && "Cannot sub this type!");
1858 case MVT::i1:
1859 case MVT::i8: Opc = X86::NEG8r; break;
1860 case MVT::i16: Opc = X86::NEG16r; break;
1861 case MVT::i32: Opc = X86::NEG32r; break;
1862 }
1863 Tmp1 = SelectExpr(N.getOperand(1));
1864 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1865 return Result;
1866 }
1867
Chris Lattnera5ade062005-01-11 21:19:59 +00001868 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1869 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001870 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001871 switch (N.getValueType()) {
1872 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001873 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001874 case MVT::i8: Opc = X86::NOT8r; break;
1875 case MVT::i16: Opc = X86::NOT16r; break;
1876 case MVT::i32: Opc = X86::NOT32r; break;
1877 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001878 if (Opc) {
1879 Tmp1 = SelectExpr(Op0);
1880 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1881 return Result;
1882 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001883 }
1884
Chris Lattner2a4e5082005-01-17 06:48:02 +00001885 // Fold common multiplies into LEA instructions.
1886 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1887 switch ((int)CN->getValue()) {
1888 default: break;
1889 case 3:
1890 case 5:
1891 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001892 // Remove N from exprmap so SelectAddress doesn't get confused.
1893 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001894 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001895 SelectAddress(N, AM);
1896 // Restore it to the map.
1897 ExprMap[N] = Result;
1898 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1899 return Result;
1900 }
1901 }
1902
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001903 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001904 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001905 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001906 case MVT::i8: Opc = 0; break;
1907 case MVT::i16: Opc = 1; break;
1908 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001909 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001910 switch (Node->getOpcode()) {
1911 default: assert(0 && "Unreachable!");
1912 case ISD::SUB: Opc = SUBTab[Opc]; break;
1913 case ISD::MUL: Opc = MULTab[Opc]; break;
1914 case ISD::AND: Opc = ANDTab[Opc]; break;
1915 case ISD::OR: Opc = ORTab[Opc]; break;
1916 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001917 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001918 if (Opc) { // Can't fold MUL:i8 R, imm
1919 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001920 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1921 return Result;
1922 }
1923 }
Chris Lattner11333092005-01-11 03:11:44 +00001924
Chris Lattner44129b52005-01-25 20:03:11 +00001925 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00001926 if (Node->getOpcode() != ISD::SUB) {
1927 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001928 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001929 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00001930 // For FP, emit 'reverse' subract, with a memory operand.
1931 if (N.getValueType() == MVT::f64) {
1932 if (Op0.getOpcode() == ISD::EXTLOAD)
1933 Opc = X86::FSUBR32m;
1934 else
1935 Opc = X86::FSUBR64m;
1936
Chris Lattnera5ade062005-01-11 21:19:59 +00001937 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001938 EmitFoldedLoad(Op0, AM);
1939 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001940 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1941 return Result;
1942 }
1943 }
1944
Chris Lattner44129b52005-01-25 20:03:11 +00001945 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001946 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001947 switch (N.getValueType()) {
1948 default: assert(0 && "Cannot operate on this type!");
1949 case MVT::i1:
1950 case MVT::i8: Opc = 5; break;
1951 case MVT::i16: Opc = 6; break;
1952 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001953 // For F64, handle promoted load operations (from F32) as well!
1954 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001955 }
1956 switch (Node->getOpcode()) {
1957 default: assert(0 && "Unreachable!");
1958 case ISD::SUB: Opc = SUBTab[Opc]; break;
1959 case ISD::MUL: Opc = MULTab[Opc]; break;
1960 case ISD::AND: Opc = ANDTab[Opc]; break;
1961 case ISD::OR: Opc = ORTab[Opc]; break;
1962 case ISD::XOR: Opc = XORTab[Opc]; break;
1963 }
1964
1965 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001966 EmitFoldedLoad(Op1, AM);
1967 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001968 if (Opc) {
1969 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1970 } else {
1971 assert(Node->getOpcode() == ISD::MUL &&
1972 N.getValueType() == MVT::i8 && "Unexpected situation!");
1973 // Must use the MUL instruction, which forces use of AL.
1974 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1975 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1976 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1977 }
1978 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001979 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001980
1981 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1982 Tmp1 = SelectExpr(Op0);
1983 Tmp2 = SelectExpr(Op1);
1984 } else {
1985 Tmp2 = SelectExpr(Op1);
1986 Tmp1 = SelectExpr(Op0);
1987 }
1988
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001989 switch (N.getValueType()) {
1990 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001991 case MVT::i1:
1992 case MVT::i8: Opc = 10; break;
1993 case MVT::i16: Opc = 11; break;
1994 case MVT::i32: Opc = 12; break;
1995 case MVT::f32: Opc = 13; break;
1996 case MVT::f64: Opc = 14; break;
1997 }
1998 switch (Node->getOpcode()) {
1999 default: assert(0 && "Unreachable!");
2000 case ISD::SUB: Opc = SUBTab[Opc]; break;
2001 case ISD::MUL: Opc = MULTab[Opc]; break;
2002 case ISD::AND: Opc = ANDTab[Opc]; break;
2003 case ISD::OR: Opc = ORTab[Opc]; break;
2004 case ISD::XOR: Opc = XORTab[Opc]; break;
2005 }
2006 if (Opc) {
2007 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2008 } else {
2009 assert(Node->getOpcode() == ISD::MUL &&
2010 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002011 // Must use the MUL instruction, which forces use of AL.
2012 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2013 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2014 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002015 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002016 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002017 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002018 case ISD::ADD_PARTS:
2019 case ISD::SUB_PARTS: {
2020 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2021 "Not an i64 add/sub!");
2022 // Emit all of the operands.
2023 std::vector<unsigned> InVals;
2024 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2025 InVals.push_back(SelectExpr(N.getOperand(i)));
2026 if (N.getOpcode() == ISD::ADD_PARTS) {
2027 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2028 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2029 } else {
2030 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2031 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2032 }
2033 return Result+N.ResNo;
2034 }
2035
Chris Lattnerb38a7492005-04-02 04:01:14 +00002036 case ISD::SHL_PARTS:
2037 case ISD::SRA_PARTS:
2038 case ISD::SRL_PARTS: {
2039 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2040 "Not an i64 shift!");
2041 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2042 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2043 unsigned TmpReg = MakeReg(MVT::i32);
2044 if (N.getOpcode() == ISD::SRA_PARTS) {
2045 // If this is a SHR of a Long, then we need to do funny sign extension
2046 // stuff. TmpReg gets the value to use as the high-part if we are
2047 // shifting more than 32 bits.
2048 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2049 } else {
2050 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2051 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2052 }
2053
2054 // Initialize CL with the shift amount.
2055 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2056 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2057
2058 unsigned TmpReg2 = MakeReg(MVT::i32);
2059 unsigned TmpReg3 = MakeReg(MVT::i32);
2060 if (N.getOpcode() == ISD::SHL_PARTS) {
2061 // TmpReg2 = shld inHi, inLo
2062 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2063 .addReg(ShiftOpLo);
2064 // TmpReg3 = shl inLo, CL
2065 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
2066
2067 // Set the flags to indicate whether the shift was by more than 32 bits.
2068 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
2069
2070 // DestHi = (>32) ? TmpReg3 : TmpReg2;
2071 BuildMI(BB, X86::CMOVNE32rr, 2,
2072 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2073 // DestLo = (>32) ? TmpReg : TmpReg3;
2074 BuildMI(BB, X86::CMOVNE32rr, 2,
2075 Result).addReg(TmpReg3).addReg(TmpReg);
2076 } else {
2077 // TmpReg2 = shrd inLo, inHi
2078 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2079 .addReg(ShiftOpHi);
2080 // TmpReg3 = s[ah]r inHi, CL
2081 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
2082 : X86::SHR32rCL, 1, TmpReg3)
2083 .addReg(ShiftOpHi);
2084
2085 // Set the flags to indicate whether the shift was by more than 32 bits.
2086 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
2087
2088 // DestLo = (>32) ? TmpReg3 : TmpReg2;
2089 BuildMI(BB, X86::CMOVNE32rr, 2,
2090 Result).addReg(TmpReg2).addReg(TmpReg3);
2091
2092 // DestHi = (>32) ? TmpReg : TmpReg3;
2093 BuildMI(BB, X86::CMOVNE32rr, 2,
2094 Result+1).addReg(TmpReg3).addReg(TmpReg);
2095 }
2096 return Result+N.ResNo;
2097 }
2098
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002099 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002100 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2101 Tmp2 = SelectExpr(N.getOperand(1));
2102 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002103 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002104 Tmp3 = SelectExpr(N.getOperand(2));
2105 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002106 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002107 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2108 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002109
2110 case ISD::SDIV:
2111 case ISD::UDIV:
2112 case ISD::SREM:
2113 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002114 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2115 "We don't support this operator!");
2116
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002117 if (N.getOpcode() == ISD::SDIV)
Chris Lattner3576c842005-01-25 20:35:10 +00002118
2119 // We can fold loads into FpDIVs, but not really into any others.
2120 if (N.getValueType() == MVT::f64) {
2121 // Check for reversed and unreversed DIV.
2122 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2123 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2124 Opc = X86::FDIVR32m;
2125 else
2126 Opc = X86::FDIVR64m;
2127 X86AddressMode AM;
2128 EmitFoldedLoad(N.getOperand(0), AM);
2129 Tmp1 = SelectExpr(N.getOperand(1));
2130 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2131 return Result;
2132 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2133 N.getOperand(1).getOpcode() == ISD::LOAD) {
2134 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2135 Opc = X86::FDIV32m;
2136 else
2137 Opc = X86::FDIV64m;
2138 X86AddressMode AM;
2139 EmitFoldedLoad(N.getOperand(1), AM);
2140 Tmp1 = SelectExpr(N.getOperand(0));
2141 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2142 return Result;
2143 }
2144 }
2145
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002146 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2147 // FIXME: These special cases should be handled by the lowering impl!
2148 unsigned RHS = CN->getValue();
2149 bool isNeg = false;
2150 if ((int)RHS < 0) {
2151 isNeg = true;
2152 RHS = -RHS;
2153 }
2154 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2155 unsigned Log = log2(RHS);
2156 unsigned TmpReg = MakeReg(N.getValueType());
2157 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2158 switch (N.getValueType()) {
2159 default: assert("Unknown type to signed divide!");
2160 case MVT::i8:
2161 SAROpc = X86::SAR8ri;
2162 SHROpc = X86::SHR8ri;
2163 ADDOpc = X86::ADD8rr;
2164 NEGOpc = X86::NEG8r;
2165 break;
2166 case MVT::i16:
2167 SAROpc = X86::SAR16ri;
2168 SHROpc = X86::SHR16ri;
2169 ADDOpc = X86::ADD16rr;
2170 NEGOpc = X86::NEG16r;
2171 break;
2172 case MVT::i32:
2173 SAROpc = X86::SAR32ri;
2174 SHROpc = X86::SHR32ri;
2175 ADDOpc = X86::ADD32rr;
2176 NEGOpc = X86::NEG32r;
2177 break;
2178 }
Chris Lattner11333092005-01-11 03:11:44 +00002179 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002180 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2181 unsigned TmpReg2 = MakeReg(N.getValueType());
2182 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2183 unsigned TmpReg3 = MakeReg(N.getValueType());
2184 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
2185
2186 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2187 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2188 if (isNeg)
2189 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2190 return Result;
2191 }
2192 }
2193
Chris Lattner11333092005-01-11 03:11:44 +00002194 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2195 Tmp1 = SelectExpr(N.getOperand(0));
2196 Tmp2 = SelectExpr(N.getOperand(1));
2197 } else {
2198 Tmp2 = SelectExpr(N.getOperand(1));
2199 Tmp1 = SelectExpr(N.getOperand(0));
2200 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002201
2202 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2203 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2204 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2205 switch (N.getValueType()) {
2206 default: assert(0 && "Cannot sdiv this type!");
2207 case MVT::i8:
2208 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2209 LoReg = X86::AL;
2210 HiReg = X86::AH;
2211 MovOpcode = X86::MOV8rr;
2212 ClrOpcode = X86::MOV8ri;
2213 SExtOpcode = X86::CBW;
2214 break;
2215 case MVT::i16:
2216 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2217 LoReg = X86::AX;
2218 HiReg = X86::DX;
2219 MovOpcode = X86::MOV16rr;
2220 ClrOpcode = X86::MOV16ri;
2221 SExtOpcode = X86::CWD;
2222 break;
2223 case MVT::i32:
2224 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002225 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002226 HiReg = X86::EDX;
2227 MovOpcode = X86::MOV32rr;
2228 ClrOpcode = X86::MOV32ri;
2229 SExtOpcode = X86::CDQ;
2230 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002231 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002232 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002233 return Result;
2234 }
2235
2236 // Set up the low part.
2237 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2238
2239 if (isSigned) {
2240 // Sign extend the low part into the high part.
2241 BuildMI(BB, SExtOpcode, 0);
2242 } else {
2243 // Zero out the high part, effectively zero extending the input.
2244 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2245 }
2246
2247 // Emit the DIV/IDIV instruction.
2248 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
2249
2250 // Get the result of the divide or rem.
2251 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2252 return Result;
2253 }
2254
2255 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002256 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002257 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2258 switch (N.getValueType()) {
2259 default: assert(0 && "Cannot shift this type!");
2260 case MVT::i8: Opc = X86::ADD8rr; break;
2261 case MVT::i16: Opc = X86::ADD16rr; break;
2262 case MVT::i32: Opc = X86::ADD32rr; break;
2263 }
2264 Tmp1 = SelectExpr(N.getOperand(0));
2265 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2266 return Result;
2267 }
2268
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002269 switch (N.getValueType()) {
2270 default: assert(0 && "Cannot shift this type!");
2271 case MVT::i8: Opc = X86::SHL8ri; break;
2272 case MVT::i16: Opc = X86::SHL16ri; break;
2273 case MVT::i32: Opc = X86::SHL32ri; break;
2274 }
Chris Lattner11333092005-01-11 03:11:44 +00002275 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002276 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2277 return Result;
2278 }
Chris Lattner11333092005-01-11 03:11:44 +00002279
2280 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2281 Tmp1 = SelectExpr(N.getOperand(0));
2282 Tmp2 = SelectExpr(N.getOperand(1));
2283 } else {
2284 Tmp2 = SelectExpr(N.getOperand(1));
2285 Tmp1 = SelectExpr(N.getOperand(0));
2286 }
2287
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002288 switch (N.getValueType()) {
2289 default: assert(0 && "Cannot shift this type!");
2290 case MVT::i8 : Opc = X86::SHL8rCL; break;
2291 case MVT::i16: Opc = X86::SHL16rCL; break;
2292 case MVT::i32: Opc = X86::SHL32rCL; break;
2293 }
2294 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2295 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2296 return Result;
2297 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002298 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2299 switch (N.getValueType()) {
2300 default: assert(0 && "Cannot shift this type!");
2301 case MVT::i8: Opc = X86::SHR8ri; break;
2302 case MVT::i16: Opc = X86::SHR16ri; break;
2303 case MVT::i32: Opc = X86::SHR32ri; break;
2304 }
Chris Lattner11333092005-01-11 03:11:44 +00002305 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002306 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2307 return Result;
2308 }
Chris Lattner11333092005-01-11 03:11:44 +00002309
2310 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2311 Tmp1 = SelectExpr(N.getOperand(0));
2312 Tmp2 = SelectExpr(N.getOperand(1));
2313 } else {
2314 Tmp2 = SelectExpr(N.getOperand(1));
2315 Tmp1 = SelectExpr(N.getOperand(0));
2316 }
2317
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002318 switch (N.getValueType()) {
2319 default: assert(0 && "Cannot shift this type!");
2320 case MVT::i8 : Opc = X86::SHR8rCL; break;
2321 case MVT::i16: Opc = X86::SHR16rCL; break;
2322 case MVT::i32: Opc = X86::SHR32rCL; break;
2323 }
2324 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2325 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2326 return Result;
2327 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002328 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2329 switch (N.getValueType()) {
2330 default: assert(0 && "Cannot shift this type!");
2331 case MVT::i8: Opc = X86::SAR8ri; break;
2332 case MVT::i16: Opc = X86::SAR16ri; break;
2333 case MVT::i32: Opc = X86::SAR32ri; break;
2334 }
Chris Lattner11333092005-01-11 03:11:44 +00002335 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002336 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2337 return Result;
2338 }
Chris Lattner11333092005-01-11 03:11:44 +00002339
2340 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2341 Tmp1 = SelectExpr(N.getOperand(0));
2342 Tmp2 = SelectExpr(N.getOperand(1));
2343 } else {
2344 Tmp2 = SelectExpr(N.getOperand(1));
2345 Tmp1 = SelectExpr(N.getOperand(0));
2346 }
2347
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002348 switch (N.getValueType()) {
2349 default: assert(0 && "Cannot shift this type!");
2350 case MVT::i8 : Opc = X86::SAR8rCL; break;
2351 case MVT::i16: Opc = X86::SAR16rCL; break;
2352 case MVT::i32: Opc = X86::SAR32rCL; break;
2353 }
2354 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2355 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2356 return Result;
2357
2358 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002359 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002360 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2361 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2362 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002363 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002364 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002365 if (Result != 1) { // Generate the token
2366 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2367 assert(0 && "Load already emitted!?");
2368 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002369 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2370
Chris Lattner5188ad72005-01-08 19:28:19 +00002371 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002372 default: assert(0 && "Cannot load this type!");
2373 case MVT::i1:
2374 case MVT::i8: Opc = X86::MOV8rm; break;
2375 case MVT::i16: Opc = X86::MOV16rm; break;
2376 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002377 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2378 }
Chris Lattner11333092005-01-11 03:11:44 +00002379
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002380 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002381 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002382 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2383 } else {
2384 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002385
2386 SDOperand Chain = N.getOperand(0);
2387 SDOperand Address = N.getOperand(1);
2388 if (getRegPressure(Chain) > getRegPressure(Address)) {
2389 Select(Chain);
2390 SelectAddress(Address, AM);
2391 } else {
2392 SelectAddress(Address, AM);
2393 Select(Chain);
2394 }
2395
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002396 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2397 }
2398 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002399
2400 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2401 case ISD::ZEXTLOAD: {
2402 // Make sure we generate both values.
2403 if (Result != 1)
2404 ExprMap[N.getValue(1)] = 1; // Generate the token
2405 else
2406 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2407
Chris Lattnerda2ce112005-01-16 07:34:08 +00002408 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2409 if (Node->getValueType(0) == MVT::f64) {
2410 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2411 "Bad EXTLOAD!");
2412 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2413 CP->getIndex());
2414 return Result;
2415 }
2416
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002417 X86AddressMode AM;
2418 if (getRegPressure(Node->getOperand(0)) >
2419 getRegPressure(Node->getOperand(1))) {
2420 Select(Node->getOperand(0)); // chain
2421 SelectAddress(Node->getOperand(1), AM);
2422 } else {
2423 SelectAddress(Node->getOperand(1), AM);
2424 Select(Node->getOperand(0)); // chain
2425 }
2426
2427 switch (Node->getValueType(0)) {
2428 default: assert(0 && "Unknown type to sign extend to.");
2429 case MVT::f64:
2430 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2431 "Bad EXTLOAD!");
2432 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2433 break;
2434 case MVT::i32:
2435 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2436 default:
2437 assert(0 && "Bad zero extend!");
2438 case MVT::i1:
2439 case MVT::i8:
2440 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2441 break;
2442 case MVT::i16:
2443 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2444 break;
2445 }
2446 break;
2447 case MVT::i16:
2448 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2449 "Bad zero extend!");
2450 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2451 break;
2452 case MVT::i8:
2453 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2454 "Bad zero extend!");
2455 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2456 break;
2457 }
2458 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002459 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002460 case ISD::SEXTLOAD: {
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
2467 X86AddressMode AM;
2468 if (getRegPressure(Node->getOperand(0)) >
2469 getRegPressure(Node->getOperand(1))) {
2470 Select(Node->getOperand(0)); // chain
2471 SelectAddress(Node->getOperand(1), AM);
2472 } else {
2473 SelectAddress(Node->getOperand(1), AM);
2474 Select(Node->getOperand(0)); // chain
2475 }
2476
2477 switch (Node->getValueType(0)) {
2478 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2479 default: assert(0 && "Unknown type to sign extend to.");
2480 case MVT::i32:
2481 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2482 default:
2483 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2484 case MVT::i8:
2485 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2486 break;
2487 case MVT::i16:
2488 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2489 break;
2490 }
2491 break;
2492 case MVT::i16:
2493 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2494 "Cannot sign extend from bool!");
2495 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2496 break;
2497 }
2498 return Result;
2499 }
2500
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002501 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002502 // Generate both result values.
2503 if (Result != 1)
2504 ExprMap[N.getValue(1)] = 1; // Generate the token
2505 else
2506 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2507
2508 // FIXME: We are currently ignoring the requested alignment for handling
2509 // greater than the stack alignment. This will need to be revisited at some
2510 // point. Align = N.getOperand(2);
2511
2512 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2513 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2514 std::cerr << "Cannot allocate stack object with greater alignment than"
2515 << " the stack alignment yet!";
2516 abort();
2517 }
2518
2519 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002520 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002521 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2522 .addImm(CN->getValue());
2523 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002524 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2525 Select(N.getOperand(0));
2526 Tmp1 = SelectExpr(N.getOperand(1));
2527 } else {
2528 Tmp1 = SelectExpr(N.getOperand(1));
2529 Select(N.getOperand(0));
2530 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002531
2532 // Subtract size from stack pointer, thereby allocating some space.
2533 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2534 }
2535
2536 // Put a pointer to the space into the result register, by copying the stack
2537 // pointer.
2538 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2539 return Result;
2540
2541 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002542 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002543 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002544
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002545 if (GlobalAddressSDNode *GASD =
2546 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002547 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002548 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2549 } else if (ExternalSymbolSDNode *ESSDN =
2550 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002551 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002552 BuildMI(BB, X86::CALLpcrel32,
2553 1).addExternalSymbol(ESSDN->getSymbol(), true);
2554 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002555 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2556 Select(N.getOperand(0));
2557 Tmp1 = SelectExpr(N.getOperand(1));
2558 } else {
2559 Tmp1 = SelectExpr(N.getOperand(1));
2560 Select(N.getOperand(0));
2561 }
2562
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002563 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2564 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002565 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002566 default: assert(0 && "Unknown value type for call result!");
2567 case MVT::Other: return 1;
2568 case MVT::i1:
2569 case MVT::i8:
2570 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2571 break;
2572 case MVT::i16:
2573 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2574 break;
2575 case MVT::i32:
2576 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002577 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002578 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2579 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002580 case MVT::f64: // Floating-point return values live in %ST(0)
2581 ContainsFPCode = true;
2582 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2583 break;
2584 }
2585 return Result+N.ResNo;
2586 }
2587
2588 return 0;
2589}
2590
Chris Lattnere10269b2005-01-17 19:25:26 +00002591/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2592/// load/op/store instruction. If successful return true.
2593bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2594 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2595 SDOperand Chain = Node->getOperand(0);
2596 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002597 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002598
2599 // The chain has to be a load, the stored value must be an integer binary
2600 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002601 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002602 MVT::isFloatingPoint(StVal.getValueType()))
2603 return false;
2604
Chris Lattner5c659812005-01-17 22:10:42 +00002605 // Token chain must either be a factor node or the load to fold.
2606 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2607 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002608
Chris Lattner5c659812005-01-17 22:10:42 +00002609 SDOperand TheLoad;
2610
2611 // Check to see if there is a load from the same pointer that we're storing
2612 // to in either operand of the binop.
2613 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2614 StVal.getOperand(0).getOperand(1) == StPtr)
2615 TheLoad = StVal.getOperand(0);
2616 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2617 StVal.getOperand(1).getOperand(1) == StPtr)
2618 TheLoad = StVal.getOperand(1);
2619 else
2620 return false; // No matching load operand.
2621
2622 // We can only fold the load if there are no intervening side-effecting
2623 // operations. This means that the store uses the load as its token chain, or
2624 // there are only token factor nodes in between the store and load.
2625 if (Chain != TheLoad.getValue(1)) {
2626 // Okay, the other option is that we have a store referring to (possibly
2627 // nested) token factor nodes. For now, just try peeking through one level
2628 // of token factors to see if this is the case.
2629 bool ChainOk = false;
2630 if (Chain.getOpcode() == ISD::TokenFactor) {
2631 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2632 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2633 ChainOk = true;
2634 break;
2635 }
2636 }
2637
2638 if (!ChainOk) return false;
2639 }
2640
2641 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002642 return false;
2643
2644 // Make sure that one of the operands of the binop is the load, and that the
2645 // load folds into the binop.
2646 if (((StVal.getOperand(0) != TheLoad ||
2647 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2648 (StVal.getOperand(1) != TheLoad ||
2649 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2650 return false;
2651
2652 // Finally, check to see if this is one of the ops we can handle!
2653 static const unsigned ADDTAB[] = {
2654 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2655 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2656 };
2657 static const unsigned SUBTAB[] = {
2658 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2659 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2660 };
2661 static const unsigned ANDTAB[] = {
2662 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2663 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2664 };
2665 static const unsigned ORTAB[] = {
2666 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2667 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2668 };
2669 static const unsigned XORTAB[] = {
2670 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2671 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2672 };
2673 static const unsigned SHLTAB[] = {
2674 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2675 /*Have to put the reg in CL*/0, 0, 0,
2676 };
2677 static const unsigned SARTAB[] = {
2678 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2679 /*Have to put the reg in CL*/0, 0, 0,
2680 };
2681 static const unsigned SHRTAB[] = {
2682 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2683 /*Have to put the reg in CL*/0, 0, 0,
2684 };
2685
2686 const unsigned *TabPtr = 0;
2687 switch (StVal.getOpcode()) {
2688 default:
2689 std::cerr << "CANNOT [mem] op= val: ";
2690 StVal.Val->dump(); std::cerr << "\n";
2691 case ISD::MUL:
2692 case ISD::SDIV:
2693 case ISD::UDIV:
2694 case ISD::SREM:
2695 case ISD::UREM: return false;
2696
2697 case ISD::ADD: TabPtr = ADDTAB; break;
2698 case ISD::SUB: TabPtr = SUBTAB; break;
2699 case ISD::AND: TabPtr = ANDTAB; break;
2700 case ISD:: OR: TabPtr = ORTAB; break;
2701 case ISD::XOR: TabPtr = XORTAB; break;
2702 case ISD::SHL: TabPtr = SHLTAB; break;
2703 case ISD::SRA: TabPtr = SARTAB; break;
2704 case ISD::SRL: TabPtr = SHRTAB; break;
2705 }
2706
2707 // Handle: [mem] op= CST
2708 SDOperand Op0 = StVal.getOperand(0);
2709 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002710 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002711 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2712 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2713 default: break;
2714 case MVT::i1:
2715 case MVT::i8: Opc = TabPtr[0]; break;
2716 case MVT::i16: Opc = TabPtr[1]; break;
2717 case MVT::i32: Opc = TabPtr[2]; break;
2718 }
2719
2720 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002721 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2722 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002723 Select(Chain);
2724
Chris Lattnere10269b2005-01-17 19:25:26 +00002725 X86AddressMode AM;
2726 if (getRegPressure(TheLoad.getOperand(0)) >
2727 getRegPressure(TheLoad.getOperand(1))) {
2728 Select(TheLoad.getOperand(0));
2729 SelectAddress(TheLoad.getOperand(1), AM);
2730 } else {
2731 SelectAddress(TheLoad.getOperand(1), AM);
2732 Select(TheLoad.getOperand(0));
2733 }
Chris Lattner5c659812005-01-17 22:10:42 +00002734
2735 if (StVal.getOpcode() == ISD::ADD) {
2736 if (CN->getValue() == 1) {
2737 switch (Op0.getValueType()) {
2738 default: break;
2739 case MVT::i8:
2740 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2741 return true;
2742 case MVT::i16: Opc = TabPtr[1];
2743 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2744 return true;
2745 case MVT::i32: Opc = TabPtr[2];
2746 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2747 return true;
2748 }
2749 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2750 switch (Op0.getValueType()) {
2751 default: break;
2752 case MVT::i8:
2753 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2754 return true;
2755 case MVT::i16: Opc = TabPtr[1];
2756 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2757 return true;
2758 case MVT::i32: Opc = TabPtr[2];
2759 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2760 return true;
2761 }
2762 }
2763 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002764
2765 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2766 return true;
2767 }
2768 }
2769
2770 // If we have [mem] = V op [mem], try to turn it into:
2771 // [mem] = [mem] op V.
2772 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2773 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2774 StVal.getOpcode() != ISD::SRL)
2775 std::swap(Op0, Op1);
2776
2777 if (Op0 != TheLoad) return false;
2778
2779 switch (Op0.getValueType()) {
2780 default: return false;
2781 case MVT::i1:
2782 case MVT::i8: Opc = TabPtr[3]; break;
2783 case MVT::i16: Opc = TabPtr[4]; break;
2784 case MVT::i32: Opc = TabPtr[5]; break;
2785 }
Chris Lattner5c659812005-01-17 22:10:42 +00002786
Chris Lattnerb422aea2005-01-18 17:35:28 +00002787 // Table entry doesn't exist?
2788 if (Opc == 0) return false;
2789
Chris Lattner4a108662005-01-18 03:51:59 +00002790 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2791 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002792 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002793 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002794
Chris Lattnere10269b2005-01-17 19:25:26 +00002795 X86AddressMode AM;
2796 SelectAddress(TheLoad.getOperand(1), AM);
2797 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002798 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002799 return true;
2800}
2801
2802
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002803void ISel::Select(SDOperand N) {
2804 unsigned Tmp1, Tmp2, Opc;
2805
Nate Begeman85fdeb22005-03-24 04:39:54 +00002806 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002807 return; // Already selected.
2808
Chris Lattner989de032005-01-11 06:14:36 +00002809 SDNode *Node = N.Val;
2810
2811 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002812 default:
Chris Lattner989de032005-01-11 06:14:36 +00002813 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002814 assert(0 && "Node not handled yet!");
2815 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002816 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002817 if (Node->getNumOperands() == 2) {
2818 bool OneFirst =
2819 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2820 Select(Node->getOperand(OneFirst));
2821 Select(Node->getOperand(!OneFirst));
2822 } else {
2823 std::vector<std::pair<unsigned, unsigned> > OpsP;
2824 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2825 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2826 std::sort(OpsP.begin(), OpsP.end());
2827 std::reverse(OpsP.begin(), OpsP.end());
2828 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2829 Select(Node->getOperand(OpsP[i].second));
2830 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002831 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002832 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002833 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2834 Select(N.getOperand(0));
2835 Tmp1 = SelectExpr(N.getOperand(1));
2836 } else {
2837 Tmp1 = SelectExpr(N.getOperand(1));
2838 Select(N.getOperand(0));
2839 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002840 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002841
2842 if (Tmp1 != Tmp2) {
2843 switch (N.getOperand(1).getValueType()) {
2844 default: assert(0 && "Invalid type for operation!");
2845 case MVT::i1:
2846 case MVT::i8: Opc = X86::MOV8rr; break;
2847 case MVT::i16: Opc = X86::MOV16rr; break;
2848 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002849 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002850 }
2851 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2852 }
2853 return;
2854 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002855 switch (N.getNumOperands()) {
2856 default:
2857 assert(0 && "Unknown return instruction!");
2858 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002859 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2860 N.getOperand(2).getValueType() == MVT::i32 &&
2861 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002862 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2863 Tmp1 = SelectExpr(N.getOperand(1));
2864 Tmp2 = SelectExpr(N.getOperand(2));
2865 } else {
2866 Tmp2 = SelectExpr(N.getOperand(2));
2867 Tmp1 = SelectExpr(N.getOperand(1));
2868 }
2869 Select(N.getOperand(0));
2870
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002871 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2872 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2873 // Declare that EAX & EDX are live on exit.
2874 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2875 .addReg(X86::ESP);
2876 break;
2877 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002878 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2879 Select(N.getOperand(0));
2880 Tmp1 = SelectExpr(N.getOperand(1));
2881 } else {
2882 Tmp1 = SelectExpr(N.getOperand(1));
2883 Select(N.getOperand(0));
2884 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002885 switch (N.getOperand(1).getValueType()) {
2886 default: assert(0 && "All other types should have been promoted!!");
2887 case MVT::f64:
2888 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2889 // Declare that top-of-stack is live on exit
2890 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2891 break;
2892 case MVT::i32:
2893 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2894 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2895 break;
2896 }
2897 break;
2898 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002899 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002900 break;
2901 }
2902 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2903 return;
2904 case ISD::BR: {
2905 Select(N.getOperand(0));
2906 MachineBasicBlock *Dest =
2907 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2908 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2909 return;
2910 }
2911
2912 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002913 MachineBasicBlock *Dest =
2914 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002915
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002916 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2917 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002918 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2919 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2920 Select(N.getOperand(0));
2921 Tmp1 = SelectExpr(N.getOperand(1));
2922 } else {
2923 Tmp1 = SelectExpr(N.getOperand(1));
2924 Select(N.getOperand(0));
2925 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002926 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2927 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2928 }
Chris Lattner11333092005-01-11 03:11:44 +00002929
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002930 return;
2931 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002932
Chris Lattner4df0de92005-01-17 00:00:33 +00002933 case ISD::LOAD:
2934 // If this load could be folded into the only using instruction, and if it
2935 // is safe to emit the instruction here, try to do so now.
2936 if (Node->hasNUsesOfValue(1, 0)) {
2937 SDOperand TheVal = N.getValue(0);
2938 SDNode *User = 0;
2939 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2940 assert(UI != Node->use_end() && "Didn't find use!");
2941 SDNode *UN = *UI;
2942 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2943 if (UN->getOperand(i) == TheVal) {
2944 User = UN;
2945 goto FoundIt;
2946 }
2947 }
2948 FoundIt:
2949 // Only handle unary operators right now.
2950 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00002951 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002952 SelectExpr(SDOperand(User, 0));
2953 return;
2954 }
2955 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002956 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002957 SelectExpr(N);
2958 return;
2959
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002960 case ISD::EXTLOAD:
2961 case ISD::SEXTLOAD:
2962 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002963 case ISD::CALL:
2964 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002965 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002966 SelectExpr(N);
2967 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002968
2969 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2970 // On X86, we can represent all types except for Bool and Float natively.
2971 X86AddressMode AM;
2972 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002973 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2974 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2975 && "Unsupported TRUNCSTORE for this target!");
2976
2977 if (StoredTy == MVT::i16) {
2978 // FIXME: This is here just to allow testing. X86 doesn't really have a
2979 // TRUNCSTORE i16 operation, but this is required for targets that do not
2980 // have 16-bit integer registers. We occasionally disable 16-bit integer
2981 // registers to test the promotion code.
2982 Select(N.getOperand(0));
2983 Tmp1 = SelectExpr(N.getOperand(1));
2984 SelectAddress(N.getOperand(2), AM);
2985
2986 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2987 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2988 return;
2989 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002990
2991 // Store of constant bool?
2992 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2993 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2994 Select(N.getOperand(0));
2995 SelectAddress(N.getOperand(2), AM);
2996 } else {
2997 SelectAddress(N.getOperand(2), AM);
2998 Select(N.getOperand(0));
2999 }
3000 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3001 return;
3002 }
3003
3004 switch (StoredTy) {
3005 default: assert(0 && "Cannot truncstore this type!");
3006 case MVT::i1: Opc = X86::MOV8mr; break;
3007 case MVT::f32: Opc = X86::FST32m; break;
3008 }
3009
3010 std::vector<std::pair<unsigned, unsigned> > RP;
3011 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3012 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3013 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3014 std::sort(RP.begin(), RP.end());
3015
Chris Lattner572dd082005-02-23 05:57:21 +00003016 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003017 for (unsigned i = 0; i != 3; ++i)
3018 switch (RP[2-i].second) {
3019 default: assert(0 && "Unknown operand number!");
3020 case 0: Select(N.getOperand(0)); break;
3021 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3022 case 2: SelectAddress(N.getOperand(2), AM); break;
3023 }
3024
3025 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3026 return;
3027 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003028 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003029 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003030
3031 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3032 Opc = 0;
3033 switch (CN->getValueType(0)) {
3034 default: assert(0 && "Invalid type for operation!");
3035 case MVT::i1:
3036 case MVT::i8: Opc = X86::MOV8mi; break;
3037 case MVT::i16: Opc = X86::MOV16mi; break;
3038 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003039 case MVT::f64: break;
3040 }
3041 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003042 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3043 Select(N.getOperand(0));
3044 SelectAddress(N.getOperand(2), AM);
3045 } else {
3046 SelectAddress(N.getOperand(2), AM);
3047 Select(N.getOperand(0));
3048 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003049 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3050 return;
3051 }
3052 }
Chris Lattner837caa72005-01-11 23:21:30 +00003053
3054 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003055 if (TryToFoldLoadOpStore(Node))
3056 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003057
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003058 switch (N.getOperand(1).getValueType()) {
3059 default: assert(0 && "Cannot store this type!");
3060 case MVT::i1:
3061 case MVT::i8: Opc = X86::MOV8mr; break;
3062 case MVT::i16: Opc = X86::MOV16mr; break;
3063 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003064 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003065 }
Chris Lattner11333092005-01-11 03:11:44 +00003066
3067 std::vector<std::pair<unsigned, unsigned> > RP;
3068 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3069 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3070 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3071 std::sort(RP.begin(), RP.end());
3072
Chris Lattner572dd082005-02-23 05:57:21 +00003073 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003074 for (unsigned i = 0; i != 3; ++i)
3075 switch (RP[2-i].second) {
3076 default: assert(0 && "Unknown operand number!");
3077 case 0: Select(N.getOperand(0)); break;
3078 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003079 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003080 }
3081
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003082 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3083 return;
3084 }
3085 case ISD::ADJCALLSTACKDOWN:
3086 case ISD::ADJCALLSTACKUP:
3087 Select(N.getOperand(0));
3088 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
3089
3090 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3091 X86::ADJCALLSTACKUP;
3092 BuildMI(BB, Opc, 1).addImm(Tmp1);
3093 return;
Chris Lattner989de032005-01-11 06:14:36 +00003094 case ISD::MEMSET: {
3095 Select(N.getOperand(0)); // Select the chain.
3096 unsigned Align =
3097 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3098 if (Align == 0) Align = 1;
3099
3100 // Turn the byte code into # iterations
3101 unsigned CountReg;
3102 unsigned Opcode;
3103 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3104 unsigned Val = ValC->getValue() & 255;
3105
3106 // If the value is a constant, then we can potentially use larger sets.
3107 switch (Align & 3) {
3108 case 2: // WORD aligned
3109 CountReg = MakeReg(MVT::i32);
3110 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3111 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3112 } else {
3113 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3114 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3115 }
3116 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3117 Opcode = X86::REP_STOSW;
3118 break;
3119 case 0: // DWORD aligned
3120 CountReg = MakeReg(MVT::i32);
3121 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3122 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3123 } else {
3124 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3125 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3126 }
3127 Val = (Val << 8) | Val;
3128 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3129 Opcode = X86::REP_STOSD;
3130 break;
3131 default: // BYTE aligned
3132 CountReg = SelectExpr(Node->getOperand(3));
3133 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3134 Opcode = X86::REP_STOSB;
3135 break;
3136 }
3137 } else {
3138 // If it's not a constant value we are storing, just fall back. We could
3139 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3140 unsigned ValReg = SelectExpr(Node->getOperand(2));
3141 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3142 CountReg = SelectExpr(Node->getOperand(3));
3143 Opcode = X86::REP_STOSB;
3144 }
3145
3146 // No matter what the alignment is, we put the source in ESI, the
3147 // destination in EDI, and the count in ECX.
3148 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3149 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3150 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3151 BuildMI(BB, Opcode, 0);
3152 return;
3153 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003154 case ISD::MEMCPY:
3155 Select(N.getOperand(0)); // Select the chain.
3156 unsigned Align =
3157 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3158 if (Align == 0) Align = 1;
3159
3160 // Turn the byte code into # iterations
3161 unsigned CountReg;
3162 unsigned Opcode;
3163 switch (Align & 3) {
3164 case 2: // WORD aligned
3165 CountReg = MakeReg(MVT::i32);
3166 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3167 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3168 } else {
3169 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3170 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3171 }
3172 Opcode = X86::REP_MOVSW;
3173 break;
3174 case 0: // DWORD aligned
3175 CountReg = MakeReg(MVT::i32);
3176 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3177 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3178 } else {
3179 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3180 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3181 }
3182 Opcode = X86::REP_MOVSD;
3183 break;
3184 default: // BYTE aligned
3185 CountReg = SelectExpr(Node->getOperand(3));
3186 Opcode = X86::REP_MOVSB;
3187 break;
3188 }
3189
3190 // No matter what the alignment is, we put the source in ESI, the
3191 // destination in EDI, and the count in ECX.
3192 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3193 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3194 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3195 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3196 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3197 BuildMI(BB, Opcode, 0);
3198 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003199 }
3200 assert(0 && "Should not be reached!");
3201}
3202
3203
3204/// createX86PatternInstructionSelector - This pass converts an LLVM function
3205/// into a machine code representation using pattern matching and a machine
3206/// description file.
3207///
3208FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
3209 return new ISel(TM);
3210}