blob: 322d1299499b182c0a48613192e8520f2a17f41e [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000030#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// X86TargetLowering - X86 Implementation of the TargetLowering interface
35namespace {
36 class X86TargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000038 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000039 public:
40 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000042
43 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
44 setShiftAmountType(MVT::i8);
45 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +000046 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +000047 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000048
49 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000050 addRegisterClass(MVT::i8, X86::R8RegisterClass);
51 addRegisterClass(MVT::i16, X86::R16RegisterClass);
52 addRegisterClass(MVT::i32, X86::R32RegisterClass);
53 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
54
55 // FIXME: Eliminate these two classes when legalize can handle promotions
56 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000057/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000058
59 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
60 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
61 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i16 , Expand);
62 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
63 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i1 , Expand);
64 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
65 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
66 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000067
Chris Lattnerda2ce112005-01-16 07:34:08 +000068 // These should be promoted to a larger select which is supported.
69/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
70 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000071
72 computeRegisterProperties();
Chris Lattner8acb1ba2005-01-07 07:49:41 +000073
74 addLegalFPImmediate(+0.0); // FLD0
75 addLegalFPImmediate(+1.0); // FLD1
76 addLegalFPImmediate(-0.0); // FLD0/FCHS
77 addLegalFPImmediate(-1.0); // FLD1/FCHS
78 }
79
80 /// LowerArguments - This hook must be implemented to indicate how we should
81 /// lower the arguments for the specified function, into the specified DAG.
82 virtual std::vector<SDOperand>
83 LowerArguments(Function &F, SelectionDAG &DAG);
84
85 /// LowerCallTo - This hook lowers an abstract call to a function into an
86 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000087 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +000088 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
89 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000090
91 virtual std::pair<SDOperand, SDOperand>
92 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
93
94 virtual std::pair<SDOperand,SDOperand>
95 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
96 const Type *ArgTy, SelectionDAG &DAG);
97
98 virtual std::pair<SDOperand, SDOperand>
99 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
100 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000101 };
102}
103
104
105std::vector<SDOperand>
106X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
107 std::vector<SDOperand> ArgValues;
108
109 // Add DAG nodes to load the arguments... On entry to a function on the X86,
110 // the stack frame looks like this:
111 //
112 // [ESP] -- return address
113 // [ESP + 4] -- first argument (leftmost lexically)
114 // [ESP + 8] -- second argument, if first argument is four bytes in size
115 // ...
116 //
117 MachineFunction &MF = DAG.getMachineFunction();
118 MachineFrameInfo *MFI = MF.getFrameInfo();
119
120 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000121 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000122 MVT::ValueType ObjectVT = getValueType(I->getType());
123 unsigned ArgIncrement = 4;
124 unsigned ObjSize;
125 switch (ObjectVT) {
126 default: assert(0 && "Unhandled argument type!");
127 case MVT::i1:
128 case MVT::i8: ObjSize = 1; break;
129 case MVT::i16: ObjSize = 2; break;
130 case MVT::i32: ObjSize = 4; break;
131 case MVT::i64: ObjSize = ArgIncrement = 8; break;
132 case MVT::f32: ObjSize = 4; break;
133 case MVT::f64: ObjSize = ArgIncrement = 8; break;
134 }
135 // Create the frame index object for this incoming parameter...
136 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
137
138 // Create the SelectionDAG nodes corresponding to a load from this parameter
139 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
140
141 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
142 // dead loads.
143 SDOperand ArgValue;
144 if (!I->use_empty())
145 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
146 else {
147 if (MVT::isInteger(ObjectVT))
148 ArgValue = DAG.getConstant(0, ObjectVT);
149 else
150 ArgValue = DAG.getConstantFP(0, ObjectVT);
151 }
152 ArgValues.push_back(ArgValue);
153
154 ArgOffset += ArgIncrement; // Move on to the next argument...
155 }
156
157 // If the function takes variable number of arguments, make a frame index for
158 // the start of the first vararg value... for expansion of llvm.va_start.
159 if (F.isVarArg())
160 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000161 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000162 return ArgValues;
163}
164
Chris Lattner5188ad72005-01-08 19:28:19 +0000165std::pair<SDOperand, SDOperand>
166X86TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000167 const Type *RetTy, bool isVarArg,
168 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000169 // Count how many bytes are to be pushed on the stack.
170 unsigned NumBytes = 0;
171
172 if (Args.empty()) {
173 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000174 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
175 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000176 } else {
177 for (unsigned i = 0, e = Args.size(); i != e; ++i)
178 switch (getValueType(Args[i].second)) {
179 default: assert(0 && "Unknown value type!");
180 case MVT::i1:
181 case MVT::i8:
182 case MVT::i16:
183 case MVT::i32:
184 case MVT::f32:
185 NumBytes += 4;
186 break;
187 case MVT::i64:
188 case MVT::f64:
189 NumBytes += 8;
190 break;
191 }
192
Chris Lattner5188ad72005-01-08 19:28:19 +0000193 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
194 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000195
196 // Arguments go on the stack in reverse order, as specified by the ABI.
197 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000198 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
199 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000200 std::vector<SDOperand> Stores;
201
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000202 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
203 unsigned ArgReg;
204 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
205 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
206
207 switch (getValueType(Args[i].second)) {
208 default: assert(0 && "Unexpected ValueType for argument!");
209 case MVT::i1:
210 case MVT::i8:
211 case MVT::i16:
212 // Promote the integer to 32 bits. If the input type is signed use a
213 // sign extend, otherwise use a zero extend.
214 if (Args[i].second->isSigned())
215 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
216 else
217 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
218
219 // FALL THROUGH
220 case MVT::i32:
221 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000222 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
223 Args[i].first, PtrOff));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000224 ArgOffset += 4;
225 break;
226 case MVT::i64:
227 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000228 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
229 Args[i].first, PtrOff));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000230 ArgOffset += 8;
231 break;
232 }
233 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000234 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000235 }
236
237 std::vector<MVT::ValueType> RetVals;
238 MVT::ValueType RetTyVT = getValueType(RetTy);
239 if (RetTyVT != MVT::isVoid)
240 RetVals.push_back(RetTyVT);
241 RetVals.push_back(MVT::Other);
242
Chris Lattner5188ad72005-01-08 19:28:19 +0000243 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000244 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000245 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
246 DAG.getConstant(NumBytes, getPointerTy()));
247 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000248}
249
Chris Lattner14824582005-01-09 00:01:27 +0000250std::pair<SDOperand, SDOperand>
251X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
252 // vastart just returns the address of the VarArgsFrameIndex slot.
253 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
254}
255
256std::pair<SDOperand,SDOperand> X86TargetLowering::
257LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
258 const Type *ArgTy, SelectionDAG &DAG) {
259 MVT::ValueType ArgVT = getValueType(ArgTy);
260 SDOperand Result;
261 if (!isVANext) {
262 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
263 } else {
264 unsigned Amt;
265 if (ArgVT == MVT::i32)
266 Amt = 4;
267 else {
268 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
269 "Other types should have been promoted for varargs!");
270 Amt = 8;
271 }
272 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
273 DAG.getConstant(Amt, VAList.getValueType()));
274 }
275 return std::make_pair(Result, Chain);
276}
277
278
279std::pair<SDOperand, SDOperand> X86TargetLowering::
280LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
281 SelectionDAG &DAG) {
282 SDOperand Result;
283 if (Depth) // Depths > 0 not supported yet!
284 Result = DAG.getConstant(0, getPointerTy());
285 else {
286 if (ReturnAddrIndex == 0) {
287 // Set up a frame object for the return address.
288 MachineFunction &MF = DAG.getMachineFunction();
289 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
290 }
291
292 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
293
294 if (!isFrameAddress)
295 // Just load the return address
296 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
297 else
298 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
299 DAG.getConstant(4, MVT::i32));
300 }
301 return std::make_pair(Result, Chain);
302}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000303
304
Chris Lattner98a8ba02005-01-18 01:06:26 +0000305namespace {
306 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
307 /// SDOperand's instead of register numbers for the leaves of the matched
308 /// tree.
309 struct X86ISelAddressMode {
310 enum {
311 RegBase,
312 FrameIndexBase,
313 } BaseType;
314
315 struct { // This is really a union, discriminated by BaseType!
316 SDOperand Reg;
317 int FrameIndex;
318 } Base;
319
320 unsigned Scale;
321 SDOperand IndexReg;
322 unsigned Disp;
323 GlobalValue *GV;
324
325 X86ISelAddressMode()
326 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
327 }
328 };
329}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000330
331
332namespace {
333 Statistic<>
334 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
335
336 //===--------------------------------------------------------------------===//
337 /// ISel - X86 specific code to select X86 machine instructions for
338 /// SelectionDAG operations.
339 ///
340 class ISel : public SelectionDAGISel {
341 /// ContainsFPCode - Every instruction we select that uses or defines a FP
342 /// register should set this to true.
343 bool ContainsFPCode;
344
345 /// X86Lowering - This object fully describes how to lower LLVM code to an
346 /// X86-specific SelectionDAG.
347 X86TargetLowering X86Lowering;
348
Chris Lattner11333092005-01-11 03:11:44 +0000349 /// RegPressureMap - This keeps an approximate count of the number of
350 /// registers required to evaluate each node in the graph.
351 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000352
353 /// ExprMap - As shared expressions are codegen'd, we keep track of which
354 /// vreg the value is produced in, so we only emit one copy of each compiled
355 /// tree.
356 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000357
358 public:
359 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
360 }
361
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000362 virtual const char *getPassName() const {
363 return "X86 Pattern Instruction Selection";
364 }
365
Chris Lattner11333092005-01-11 03:11:44 +0000366 unsigned getRegPressure(SDOperand O) {
367 return RegPressureMap[O.Val];
368 }
369 unsigned ComputeRegPressure(SDOperand O);
370
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000371 /// InstructionSelectBasicBlock - This callback is invoked by
372 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000373 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000374
Chris Lattner44129b52005-01-25 20:03:11 +0000375 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
376 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000377 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000378 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000379
Chris Lattner30ea1e92005-01-19 07:37:26 +0000380 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000381 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000382 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000383 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
384 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000385 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000386
387 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
388 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
389 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000390 void Select(SDOperand N);
391 };
392}
393
Chris Lattner7dbcb752005-01-12 04:21:28 +0000394/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
395/// when it has created a SelectionDAG for us to codegen.
396void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
397 // While we're doing this, keep track of whether we see any FP code for
398 // FP_REG_KILL insertion.
399 ContainsFPCode = false;
400
401 // Scan the PHI nodes that already are inserted into this basic block. If any
402 // of them is a PHI of a floating point value, we need to insert an
403 // FP_REG_KILL.
404 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
405 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
406 I != E; ++I) {
407 assert(I->getOpcode() == X86::PHI &&
408 "Isn't just PHI nodes?");
409 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
410 X86::RFPRegisterClass) {
411 ContainsFPCode = true;
412 break;
413 }
414 }
415
416 // Compute the RegPressureMap, which is an approximation for the number of
417 // registers required to compute each node.
418 ComputeRegPressure(DAG.getRoot());
419
420 // Codegen the basic block.
421 Select(DAG.getRoot());
422
423 // Finally, look at all of the successors of this block. If any contain a PHI
424 // node of FP type, we need to insert an FP_REG_KILL in this block.
425 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
426 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
427 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
428 I != E && I->getOpcode() == X86::PHI; ++I) {
429 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
430 X86::RFPRegisterClass) {
431 ContainsFPCode = true;
432 break;
433 }
434 }
435
436 // Insert FP_REG_KILL instructions into basic blocks that need them. This
437 // only occurs due to the floating point stackifier not being aggressive
438 // enough to handle arbitrary global stackification.
439 //
440 // Currently we insert an FP_REG_KILL instruction into each block that uses or
441 // defines a floating point virtual register.
442 //
443 // When the global register allocators (like linear scan) finally update live
444 // variable analysis, we can keep floating point values in registers across
445 // basic blocks. This will be a huge win, but we are waiting on the global
446 // allocators before we can do this.
447 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000448 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000449 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
450 ++NumFPKill;
451 }
452
453 // Clear state used for selection.
454 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000455 RegPressureMap.clear();
456}
457
458
Chris Lattner11333092005-01-11 03:11:44 +0000459// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
460// for the number of registers required to compute each node. This is basically
461// computing a generalized form of the Sethi-Ullman number for each node.
462unsigned ISel::ComputeRegPressure(SDOperand O) {
463 SDNode *N = O.Val;
464 unsigned &Result = RegPressureMap[N];
465 if (Result) return Result;
466
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000467 // FIXME: Should operations like CALL (which clobber lots o regs) have a
468 // higher fixed cost??
469
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000470 if (N->getNumOperands() == 0) {
471 Result = 1;
472 } else {
473 unsigned MaxRegUse = 0;
474 unsigned NumExtraMaxRegUsers = 0;
475 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
476 unsigned Regs;
477 if (N->getOperand(i).getOpcode() == ISD::Constant)
478 Regs = 0;
479 else
480 Regs = ComputeRegPressure(N->getOperand(i));
481 if (Regs > MaxRegUse) {
482 MaxRegUse = Regs;
483 NumExtraMaxRegUsers = 0;
484 } else if (Regs == MaxRegUse &&
485 N->getOperand(i).getValueType() != MVT::Other) {
486 ++NumExtraMaxRegUsers;
487 }
Chris Lattner11333092005-01-11 03:11:44 +0000488 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000489
490 if (O.getOpcode() != ISD::TokenFactor)
491 Result = MaxRegUse+NumExtraMaxRegUsers;
492 else
Chris Lattner869e0432005-01-17 23:02:13 +0000493 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000494 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000495
Chris Lattner837caa72005-01-11 23:21:30 +0000496 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000497 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000498}
499
Chris Lattnerbf52d492005-01-20 16:50:16 +0000500/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
501/// The DAG cannot have cycles in it, by definition, so the visited set is not
502/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
503/// reuse, so it prevents exponential cases.
504///
505static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
506 std::set<SDNode*> &Visited) {
507 if (N == Op) return true; // Found it.
508 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000509 if (Node->getNumOperands() == 0 || // Leaf?
510 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000511 if (!Visited.insert(Node).second) return false; // Already visited?
512
513 // Recurse for the first N-1 operands.
514 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
515 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
516 return true;
517
518 // Tail recurse for the last operand.
519 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
520}
521
Chris Lattner98a8ba02005-01-18 01:06:26 +0000522X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
523 X86AddressMode Result;
524
525 // If we need to emit two register operands, emit the one with the highest
526 // register pressure first.
527 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
528 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000529 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000530 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000531 std::set<SDNode*> Visited;
532 EmitBaseThenIndex = true;
533 // If Base ends up pointing to Index, we must emit index first. This is
534 // because of the way we fold loads, we may end up doing bad things with
535 // the folded add.
536 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
537 EmitBaseThenIndex = false;
538 } else {
539 std::set<SDNode*> Visited;
540 EmitBaseThenIndex = false;
541 // If Base ends up pointing to Index, we must emit index first. This is
542 // because of the way we fold loads, we may end up doing bad things with
543 // the folded add.
544 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
545 EmitBaseThenIndex = true;
546 }
547
548 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000549 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
550 Result.IndexReg = SelectExpr(IAM.IndexReg);
551 } else {
552 Result.IndexReg = SelectExpr(IAM.IndexReg);
553 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
554 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000555
Chris Lattner98a8ba02005-01-18 01:06:26 +0000556 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
557 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
558 } else if (IAM.IndexReg.Val) {
559 Result.IndexReg = SelectExpr(IAM.IndexReg);
560 }
561
562 switch (IAM.BaseType) {
563 case X86ISelAddressMode::RegBase:
564 Result.BaseType = X86AddressMode::RegBase;
565 break;
566 case X86ISelAddressMode::FrameIndexBase:
567 Result.BaseType = X86AddressMode::FrameIndexBase;
568 Result.Base.FrameIndex = IAM.Base.FrameIndex;
569 break;
570 default:
571 assert(0 && "Unknown base type!");
572 break;
573 }
574 Result.Scale = IAM.Scale;
575 Result.Disp = IAM.Disp;
576 Result.GV = IAM.GV;
577 return Result;
578}
579
580/// SelectAddress - Pattern match the maximal addressing mode for this node and
581/// emit all of the leaf registers.
582void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
583 X86ISelAddressMode IAM;
584 MatchAddress(N, IAM);
585 AM = SelectAddrExprs(IAM);
586}
587
588/// MatchAddress - Add the specified node to the specified addressing mode,
589/// returning true if it cannot be done. This just pattern matches for the
590/// addressing mode, it does not cause any code to be emitted. For that, use
591/// SelectAddress.
592bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000593 switch (N.getOpcode()) {
594 default: break;
595 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000596 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
597 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000598 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
599 return false;
600 }
601 break;
602 case ISD::GlobalAddress:
603 if (AM.GV == 0) {
604 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
605 return false;
606 }
607 break;
608 case ISD::Constant:
609 AM.Disp += cast<ConstantSDNode>(N)->getValue();
610 return false;
611 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000612 // We might have folded the load into this shift, so don't regen the value
613 // if so.
614 if (ExprMap.count(N)) break;
615
Chris Lattner98a8ba02005-01-18 01:06:26 +0000616 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000617 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
618 unsigned Val = CN->getValue();
619 if (Val == 1 || Val == 2 || Val == 3) {
620 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000621 SDOperand ShVal = N.Val->getOperand(0);
622
623 // Okay, we know that we have a scale by now. However, if the scaled
624 // value is an add of something and a constant, we can fold the
625 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000626 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000627 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000628 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000629 ConstantSDNode *AddVal =
630 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
631 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000632 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000633 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000634 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000635 return false;
636 }
637 }
638 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000639 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000640 // We might have folded the load into this mul, so don't regen the value if
641 // so.
642 if (ExprMap.count(N)) break;
643
Chris Lattner947d5442005-01-11 19:37:02 +0000644 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000645 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
646 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000647 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
648 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
649 AM.Scale = unsigned(CN->getValue())-1;
650
651 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000652 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000653
654 // Okay, we know that we have a scale by now. However, if the scaled
655 // value is an add of something and a constant, we can fold the
656 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000657 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000658 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000659 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000660 ConstantSDNode *AddVal =
661 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
662 AM.Disp += AddVal->getValue() * CN->getValue();
663 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000664 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000665 }
666
667 AM.IndexReg = AM.Base.Reg = Reg;
668 return false;
669 }
670 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000671
672 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000673 // We might have folded the load into this mul, so don't regen the value if
674 // so.
675 if (ExprMap.count(N)) break;
676
Chris Lattner98a8ba02005-01-18 01:06:26 +0000677 X86ISelAddressMode Backup = AM;
678 if (!MatchAddress(N.Val->getOperand(0), AM) &&
679 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000680 return false;
681 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000682 if (!MatchAddress(N.Val->getOperand(1), AM) &&
683 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000684 return false;
685 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000686 break;
687 }
688 }
689
Chris Lattnera95589b2005-01-11 04:40:19 +0000690 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000691 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000692 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000693 if (AM.IndexReg.Val == 0) {
694 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000695 AM.Scale = 1;
696 return false;
697 }
698
699 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000700 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000701 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000702
703 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000704 AM.BaseType = X86ISelAddressMode::RegBase;
705 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000706 return false;
707}
708
709/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
710/// assuming that the temporary registers are in the 8-bit register class.
711///
712/// Tmp1 = setcc1
713/// Tmp2 = setcc2
714/// DestReg = logicalop Tmp1, Tmp2
715///
716static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
717 unsigned SetCC2, unsigned LogicalOp,
718 unsigned DestReg) {
719 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
720 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
721 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
722 BuildMI(BB, SetCC1, 0, Tmp1);
723 BuildMI(BB, SetCC2, 0, Tmp2);
724 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
725}
726
727/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
728/// condition codes match the specified SetCCOpcode. Note that some conditions
729/// require multiple instructions to generate the correct value.
730static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
731 ISD::CondCode SetCCOpcode, bool isFP) {
732 unsigned Opc;
733 if (!isFP) {
734 switch (SetCCOpcode) {
735 default: assert(0 && "Illegal integer SetCC!");
736 case ISD::SETEQ: Opc = X86::SETEr; break;
737 case ISD::SETGT: Opc = X86::SETGr; break;
738 case ISD::SETGE: Opc = X86::SETGEr; break;
739 case ISD::SETLT: Opc = X86::SETLr; break;
740 case ISD::SETLE: Opc = X86::SETLEr; break;
741 case ISD::SETNE: Opc = X86::SETNEr; break;
742 case ISD::SETULT: Opc = X86::SETBr; break;
743 case ISD::SETUGT: Opc = X86::SETAr; break;
744 case ISD::SETULE: Opc = X86::SETBEr; break;
745 case ISD::SETUGE: Opc = X86::SETAEr; break;
746 }
747 } else {
748 // On a floating point condition, the flags are set as follows:
749 // ZF PF CF op
750 // 0 | 0 | 0 | X > Y
751 // 0 | 0 | 1 | X < Y
752 // 1 | 0 | 0 | X == Y
753 // 1 | 1 | 1 | unordered
754 //
755 switch (SetCCOpcode) {
756 default: assert(0 && "Invalid FP setcc!");
757 case ISD::SETUEQ:
758 case ISD::SETEQ:
759 Opc = X86::SETEr; // True if ZF = 1
760 break;
761 case ISD::SETOGT:
762 case ISD::SETGT:
763 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
764 break;
765 case ISD::SETOGE:
766 case ISD::SETGE:
767 Opc = X86::SETAEr; // True if CF = 0
768 break;
769 case ISD::SETULT:
770 case ISD::SETLT:
771 Opc = X86::SETBr; // True if CF = 1
772 break;
773 case ISD::SETULE:
774 case ISD::SETLE:
775 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
776 break;
777 case ISD::SETONE:
778 case ISD::SETNE:
779 Opc = X86::SETNEr; // True if ZF = 0
780 break;
781 case ISD::SETUO:
782 Opc = X86::SETPr; // True if PF = 1
783 break;
784 case ISD::SETO:
785 Opc = X86::SETNPr; // True if PF = 0
786 break;
787 case ISD::SETOEQ: // !PF & ZF
788 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
789 return;
790 case ISD::SETOLT: // !PF & CF
791 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
792 return;
793 case ISD::SETOLE: // !PF & (CF || ZF)
794 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
795 return;
796 case ISD::SETUGT: // PF | (!ZF & !CF)
797 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
798 return;
799 case ISD::SETUGE: // PF | !CF
800 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
801 return;
802 case ISD::SETUNE: // PF | !ZF
803 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
804 return;
805 }
806 }
807 BuildMI(BB, Opc, 0, DestReg);
808}
809
810
811/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
812/// the Dest block if the Cond condition is true. If we cannot fold this
813/// condition into the branch, return true.
814///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000815bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
816 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000817 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
818 // B) using two conditional branches instead of one condbr, two setcc's, and
819 // an or.
820 if ((Cond.getOpcode() == ISD::OR ||
821 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
822 // And and or set the flags for us, so there is no need to emit a TST of the
823 // result. It is only safe to do this if there is only a single use of the
824 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000825 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000826 SelectExpr(Cond);
827 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
828 return false;
829 }
830
831 // Codegen br not C -> JE.
832 if (Cond.getOpcode() == ISD::XOR)
833 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
834 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000835 unsigned CondR;
836 if (getRegPressure(Chain) > getRegPressure(Cond)) {
837 Select(Chain);
838 CondR = SelectExpr(Cond.Val->getOperand(0));
839 } else {
840 CondR = SelectExpr(Cond.Val->getOperand(0));
841 Select(Chain);
842 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000843 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
844 BuildMI(BB, X86::JE, 1).addMBB(Dest);
845 return false;
846 }
847
848 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
849 if (SetCC == 0)
850 return true; // Can only handle simple setcc's so far.
851
852 unsigned Opc;
853
854 // Handle integer conditions first.
855 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
856 switch (SetCC->getCondition()) {
857 default: assert(0 && "Illegal integer SetCC!");
858 case ISD::SETEQ: Opc = X86::JE; break;
859 case ISD::SETGT: Opc = X86::JG; break;
860 case ISD::SETGE: Opc = X86::JGE; break;
861 case ISD::SETLT: Opc = X86::JL; break;
862 case ISD::SETLE: Opc = X86::JLE; break;
863 case ISD::SETNE: Opc = X86::JNE; break;
864 case ISD::SETULT: Opc = X86::JB; break;
865 case ISD::SETUGT: Opc = X86::JA; break;
866 case ISD::SETULE: Opc = X86::JBE; break;
867 case ISD::SETUGE: Opc = X86::JAE; break;
868 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000869 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000870 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000871 BuildMI(BB, Opc, 1).addMBB(Dest);
872 return false;
873 }
874
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000875 unsigned Opc2 = 0; // Second branch if needed.
876
877 // On a floating point condition, the flags are set as follows:
878 // ZF PF CF op
879 // 0 | 0 | 0 | X > Y
880 // 0 | 0 | 1 | X < Y
881 // 1 | 0 | 0 | X == Y
882 // 1 | 1 | 1 | unordered
883 //
884 switch (SetCC->getCondition()) {
885 default: assert(0 && "Invalid FP setcc!");
886 case ISD::SETUEQ:
887 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
888 case ISD::SETOGT:
889 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
890 case ISD::SETOGE:
891 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
892 case ISD::SETULT:
893 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
894 case ISD::SETULE:
895 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
896 case ISD::SETONE:
897 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
898 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
899 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
900 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
901 Opc = X86::JA; // ZF = 0 & CF = 0
902 Opc2 = X86::JP; // PF = 1
903 break;
904 case ISD::SETUGE: // PF = 1 | CF = 0
905 Opc = X86::JAE; // CF = 0
906 Opc2 = X86::JP; // PF = 1
907 break;
908 case ISD::SETUNE: // PF = 1 | ZF = 0
909 Opc = X86::JNE; // ZF = 0
910 Opc2 = X86::JP; // PF = 1
911 break;
912 case ISD::SETOEQ: // PF = 0 & ZF = 1
913 //X86::JNP, X86::JE
914 //X86::AND8rr
915 return true; // FIXME: Emit more efficient code for this branch.
916 case ISD::SETOLT: // PF = 0 & CF = 1
917 //X86::JNP, X86::JB
918 //X86::AND8rr
919 return true; // FIXME: Emit more efficient code for this branch.
920 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
921 //X86::JNP, X86::JBE
922 //X86::AND8rr
923 return true; // FIXME: Emit more efficient code for this branch.
924 }
925
Chris Lattner6c07aee2005-01-11 04:06:27 +0000926 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000927 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000928 BuildMI(BB, Opc, 1).addMBB(Dest);
929 if (Opc2)
930 BuildMI(BB, Opc2, 1).addMBB(Dest);
931 return false;
932}
933
Chris Lattner24aad1b2005-01-10 22:10:13 +0000934/// EmitSelectCC - Emit code into BB that performs a select operation between
935/// the two registers RTrue and RFalse, generating a result into RDest. Return
936/// true if the fold cannot be performed.
937///
938void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
939 unsigned RTrue, unsigned RFalse, unsigned RDest) {
940 enum Condition {
941 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
942 NOT_SET
943 } CondCode = NOT_SET;
944
945 static const unsigned CMOVTAB16[] = {
946 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
947 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
948 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
949 };
950 static const unsigned CMOVTAB32[] = {
951 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
952 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
953 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
954 };
955 static const unsigned CMOVTABFP[] = {
956 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
957 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
958 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
959 };
960
961 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
962 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
963 switch (SetCC->getCondition()) {
964 default: assert(0 && "Unknown integer comparison!");
965 case ISD::SETEQ: CondCode = EQ; break;
966 case ISD::SETGT: CondCode = GT; break;
967 case ISD::SETGE: CondCode = GE; break;
968 case ISD::SETLT: CondCode = LT; break;
969 case ISD::SETLE: CondCode = LE; break;
970 case ISD::SETNE: CondCode = NE; break;
971 case ISD::SETULT: CondCode = B; break;
972 case ISD::SETUGT: CondCode = A; break;
973 case ISD::SETULE: CondCode = BE; break;
974 case ISD::SETUGE: CondCode = AE; break;
975 }
976 } else {
977 // On a floating point condition, the flags are set as follows:
978 // ZF PF CF op
979 // 0 | 0 | 0 | X > Y
980 // 0 | 0 | 1 | X < Y
981 // 1 | 0 | 0 | X == Y
982 // 1 | 1 | 1 | unordered
983 //
984 switch (SetCC->getCondition()) {
985 default: assert(0 && "Unknown FP comparison!");
986 case ISD::SETUEQ:
987 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
988 case ISD::SETOGT:
989 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
990 case ISD::SETOGE:
991 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
992 case ISD::SETULT:
993 case ISD::SETLT: CondCode = B; break; // True if CF = 1
994 case ISD::SETULE:
995 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
996 case ISD::SETONE:
997 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
998 case ISD::SETUO: CondCode = P; break; // True if PF = 1
999 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1000 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1001 case ISD::SETUGE: // PF = 1 | CF = 0
1002 case ISD::SETUNE: // PF = 1 | ZF = 0
1003 case ISD::SETOEQ: // PF = 0 & ZF = 1
1004 case ISD::SETOLT: // PF = 0 & CF = 1
1005 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1006 // We cannot emit this comparison as a single cmov.
1007 break;
1008 }
1009 }
1010 }
1011
1012 unsigned Opc = 0;
1013 if (CondCode != NOT_SET) {
1014 switch (SVT) {
1015 default: assert(0 && "Cannot select this type!");
1016 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1017 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001018 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001019 }
1020 }
1021
1022 // Finally, if we weren't able to fold this, just emit the condition and test
1023 // it.
1024 if (CondCode == NOT_SET || Opc == 0) {
1025 // Get the condition into the zero flag.
1026 unsigned CondReg = SelectExpr(Cond);
1027 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1028
1029 switch (SVT) {
1030 default: assert(0 && "Cannot select this type!");
1031 case MVT::i16: Opc = X86::CMOVE16rr; break;
1032 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001033 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001034 }
1035 } else {
1036 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001037 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001038 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001039 }
1040 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1041}
1042
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001043void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001044 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001045 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1046 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001047 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001048 switch (RHS.getValueType()) {
1049 default: break;
1050 case MVT::i1:
1051 case MVT::i8: Opc = X86::CMP8mi; break;
1052 case MVT::i16: Opc = X86::CMP16mi; break;
1053 case MVT::i32: Opc = X86::CMP32mi; break;
1054 }
1055 if (Opc) {
1056 X86AddressMode AM;
1057 EmitFoldedLoad(LHS, AM);
1058 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1059 return;
1060 }
1061 }
1062
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001063 switch (RHS.getValueType()) {
1064 default: break;
1065 case MVT::i1:
1066 case MVT::i8: Opc = X86::CMP8ri; break;
1067 case MVT::i16: Opc = X86::CMP16ri; break;
1068 case MVT::i32: Opc = X86::CMP32ri; break;
1069 }
1070 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001071 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001072 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1073 return;
1074 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001075 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1076 if (CN->isExactlyValue(+0.0) ||
1077 CN->isExactlyValue(-0.0)) {
1078 unsigned Reg = SelectExpr(LHS);
1079 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1080 BuildMI(BB, X86::FNSTSW8r, 0);
1081 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001082 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001083 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001084 }
1085
Chris Lattneref6806c2005-01-12 02:02:48 +00001086 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001087 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001088 switch (RHS.getValueType()) {
1089 default: break;
1090 case MVT::i1:
1091 case MVT::i8: Opc = X86::CMP8mr; break;
1092 case MVT::i16: Opc = X86::CMP16mr; break;
1093 case MVT::i32: Opc = X86::CMP32mr; break;
1094 }
1095 if (Opc) {
1096 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001097 EmitFoldedLoad(LHS, AM);
1098 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001099 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1100 return;
1101 }
1102 }
1103
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001104 switch (LHS.getValueType()) {
1105 default: assert(0 && "Cannot compare this value!");
1106 case MVT::i1:
1107 case MVT::i8: Opc = X86::CMP8rr; break;
1108 case MVT::i16: Opc = X86::CMP16rr; break;
1109 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001110 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001111 }
Chris Lattner11333092005-01-11 03:11:44 +00001112 unsigned Tmp1, Tmp2;
1113 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1114 Tmp1 = SelectExpr(LHS);
1115 Tmp2 = SelectExpr(RHS);
1116 } else {
1117 Tmp2 = SelectExpr(RHS);
1118 Tmp1 = SelectExpr(LHS);
1119 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001120 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1121}
1122
Chris Lattnera5ade062005-01-11 21:19:59 +00001123/// isFoldableLoad - Return true if this is a load instruction that can safely
1124/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001125bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1126 if (Op.getOpcode() == ISD::LOAD) {
1127 // FIXME: currently can't fold constant pool indexes.
1128 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1129 return false;
1130 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1131 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1132 // FIXME: currently can't fold constant pool indexes.
1133 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1134 return false;
1135 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001136 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001137 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001138
1139 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001140 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1141 if (ExprMap.count(Op.getValue(1))) return false;
1142 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001143 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001144
Chris Lattner4ff348b2005-01-17 06:26:58 +00001145 // If there is not just one use of its value, we cannot fold.
1146 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1147
1148 // Finally, we cannot fold the load into the operation if this would induce a
1149 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1150 // operand of the operation we are folding the load into) can possible use the
1151 // chain node defined by the load.
1152 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1153 std::set<SDNode*> Visited;
1154 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1155 return false;
1156 }
1157 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001158}
1159
Chris Lattner4ff348b2005-01-17 06:26:58 +00001160
Chris Lattnera5ade062005-01-11 21:19:59 +00001161/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1162/// and compute the address being loaded into AM.
1163void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1164 SDOperand Chain = Op.getOperand(0);
1165 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001166
Chris Lattnera5ade062005-01-11 21:19:59 +00001167 if (getRegPressure(Chain) > getRegPressure(Address)) {
1168 Select(Chain);
1169 SelectAddress(Address, AM);
1170 } else {
1171 SelectAddress(Address, AM);
1172 Select(Chain);
1173 }
1174
1175 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001176 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1177 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001178 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001179 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001180}
1181
Chris Lattner30ea1e92005-01-19 07:37:26 +00001182// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1183// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1184// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1185// return true.
1186bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001187 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1188 // good!
1189 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1190 std::swap(Op1, Op2); // Op1 is the SHL now.
1191 } else {
1192 return false; // No match
1193 }
1194
1195 SDOperand ShlVal = Op1.getOperand(0);
1196 SDOperand ShlAmt = Op1.getOperand(1);
1197 SDOperand ShrVal = Op2.getOperand(0);
1198 SDOperand ShrAmt = Op2.getOperand(1);
1199
Chris Lattner30ea1e92005-01-19 07:37:26 +00001200 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1201
Chris Lattner85716372005-01-19 06:18:43 +00001202 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1203 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1204 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001205 if (SubCST->getValue() == RegSize) {
1206 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001207 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001208 if (ShrVal == ShlVal) {
1209 unsigned Reg, ShAmt;
1210 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1211 Reg = SelectExpr(ShrVal);
1212 ShAmt = SelectExpr(ShrAmt);
1213 } else {
1214 ShAmt = SelectExpr(ShrAmt);
1215 Reg = SelectExpr(ShrVal);
1216 }
1217 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1218 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1219 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1220 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1221 return true;
1222 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001223 unsigned AReg, BReg;
1224 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001225 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001226 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001227 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001228 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001229 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001230 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001231 unsigned ShAmt = SelectExpr(ShrAmt);
1232 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1233 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1234 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001235 return true;
1236 }
1237 }
1238
Chris Lattner4053b1e2005-01-19 08:07:05 +00001239 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1240 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1241 if (SubCST->getValue() == RegSize) {
1242 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1243 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1244 if (ShrVal == ShlVal) {
1245 unsigned Reg, ShAmt;
1246 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1247 Reg = SelectExpr(ShrVal);
1248 ShAmt = SelectExpr(ShlAmt);
1249 } else {
1250 ShAmt = SelectExpr(ShlAmt);
1251 Reg = SelectExpr(ShrVal);
1252 }
1253 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1254 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1255 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1256 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1257 return true;
1258 } else if (RegSize != 8) {
1259 unsigned AReg, BReg;
1260 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001261 AReg = SelectExpr(ShlVal);
1262 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001263 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001264 BReg = SelectExpr(ShrVal);
1265 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001266 }
1267 unsigned ShAmt = SelectExpr(ShlAmt);
1268 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1269 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1270 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1271 return true;
1272 }
1273 }
Chris Lattner85716372005-01-19 06:18:43 +00001274
Chris Lattner4053b1e2005-01-19 08:07:05 +00001275 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1276 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1277 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1278 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1279 // (A >> 5) | (A << 27) --> ROR A, 5
1280 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1281 if (ShrVal == ShlVal) {
1282 unsigned Reg = SelectExpr(ShrVal);
1283 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1284 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1285 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1286 return true;
1287 } else if (RegSize != 8) {
1288 unsigned AReg, BReg;
1289 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001290 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001291 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001292 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001293 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001294 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001295 }
1296 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1297 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1298 .addImm(ShrCst->getValue());
1299 return true;
1300 }
1301 }
1302
Chris Lattner85716372005-01-19 06:18:43 +00001303 return false;
1304}
1305
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001306unsigned ISel::SelectExpr(SDOperand N) {
1307 unsigned Result;
1308 unsigned Tmp1, Tmp2, Tmp3;
1309 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001310 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001311 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001312
Chris Lattner7f2afac2005-01-14 22:37:41 +00001313 if (Node->getOpcode() == ISD::CopyFromReg) {
1314 // FIXME: Handle copy from physregs!
1315
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001316 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001317 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001318 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001319
1320 unsigned &Reg = ExprMap[N];
1321 if (Reg) return Reg;
1322
Chris Lattnerb38a7492005-04-02 04:01:14 +00001323 switch (N.getOpcode()) {
1324 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001325 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001326 MakeReg(N.getValueType()) : 1;
1327 break;
1328 case ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001329 // If this is a call instruction, make sure to prepare ALL of the result
1330 // values as well as the chain.
Chris Lattnerb38a7492005-04-02 04:01:14 +00001331 if (Node->getNumValues() == 1)
1332 Reg = Result = 1; // Void call, just a chain.
1333 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001334 Result = MakeReg(Node->getValueType(0));
1335 ExprMap[N.getValue(0)] = Result;
Chris Lattnerb38a7492005-04-02 04:01:14 +00001336 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001337 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnerb38a7492005-04-02 04:01:14 +00001338 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001339 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00001340 break;
1341 case ISD::ADD_PARTS:
1342 case ISD::SUB_PARTS:
1343 case ISD::SHL_PARTS:
1344 case ISD::SRL_PARTS:
1345 case ISD::SRA_PARTS:
1346 Result = MakeReg(Node->getValueType(0));
1347 ExprMap[N.getValue(0)] = Result;
1348 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1349 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1350 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001351 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001352
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001353 switch (N.getOpcode()) {
1354 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001355 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001356 assert(0 && "Node not handled!\n");
1357 case ISD::FrameIndex:
1358 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1359 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1360 return Result;
1361 case ISD::ConstantPool:
1362 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1363 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1364 return Result;
1365 case ISD::ConstantFP:
1366 ContainsFPCode = true;
1367 Tmp1 = Result; // Intermediate Register
1368 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1369 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1370 Tmp1 = MakeReg(MVT::f64);
1371
1372 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1373 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1374 BuildMI(BB, X86::FLD0, 0, Tmp1);
1375 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1376 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1377 BuildMI(BB, X86::FLD1, 0, Tmp1);
1378 else
1379 assert(0 && "Unexpected constant!");
1380 if (Tmp1 != Result)
1381 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1382 return Result;
1383 case ISD::Constant:
1384 switch (N.getValueType()) {
1385 default: assert(0 && "Cannot use constants of this type!");
1386 case MVT::i1:
1387 case MVT::i8: Opc = X86::MOV8ri; break;
1388 case MVT::i16: Opc = X86::MOV16ri; break;
1389 case MVT::i32: Opc = X86::MOV32ri; break;
1390 }
1391 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1392 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001393 case ISD::UNDEF:
1394 if (Node->getValueType(0) == MVT::f64) {
1395 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1396 BuildMI(BB, X86::FLD0, 0, Result);
1397 } else {
1398 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1399 }
1400 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001401 case ISD::GlobalAddress: {
1402 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1403 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1404 return Result;
1405 }
1406 case ISD::ExternalSymbol: {
1407 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1408 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1409 return Result;
1410 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001411 case ISD::ZERO_EXTEND: {
1412 int DestIs16 = N.getValueType() == MVT::i16;
1413 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001414
1415 // FIXME: This hack is here for zero extension casts from bool to i8. This
1416 // would not be needed if bools were promoted by Legalize.
1417 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001418 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001419 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1420 return Result;
1421 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001422
Chris Lattner4ff348b2005-01-17 06:26:58 +00001423 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001424 static const unsigned Opc[3] = {
1425 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1426 };
1427
1428 X86AddressMode AM;
1429 EmitFoldedLoad(N.getOperand(0), AM);
1430 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1431
1432 return Result;
1433 }
1434
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001435 static const unsigned Opc[3] = {
1436 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1437 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001438 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001439 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1440 return Result;
1441 }
1442 case ISD::SIGN_EXTEND: {
1443 int DestIs16 = N.getValueType() == MVT::i16;
1444 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1445
Chris Lattner590d8002005-01-09 18:52:44 +00001446 // FIXME: Legalize should promote bools to i8!
1447 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1448 "Sign extend from bool not implemented!");
1449
Chris Lattner4ff348b2005-01-17 06:26:58 +00001450 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001451 static const unsigned Opc[3] = {
1452 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1453 };
1454
1455 X86AddressMode AM;
1456 EmitFoldedLoad(N.getOperand(0), AM);
1457 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1458 return Result;
1459 }
1460
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001461 static const unsigned Opc[3] = {
1462 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1463 };
1464 Tmp1 = SelectExpr(N.getOperand(0));
1465 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1466 return Result;
1467 }
1468 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001469 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001470 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001471 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001472 switch (N.getValueType()) {
1473 default: assert(0 && "Unknown truncate!");
1474 case MVT::i1:
1475 case MVT::i8: Opc = X86::MOV8rm; break;
1476 case MVT::i16: Opc = X86::MOV16rm; break;
1477 }
1478 X86AddressMode AM;
1479 EmitFoldedLoad(N.getOperand(0), AM);
1480 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1481 return Result;
1482 }
1483
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001484 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1485 // a move out of AX or AL.
1486 switch (N.getOperand(0).getValueType()) {
1487 default: assert(0 && "Unknown truncate!");
1488 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1489 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1490 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1491 }
1492 Tmp1 = SelectExpr(N.getOperand(0));
1493 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1494
1495 switch (N.getValueType()) {
1496 default: assert(0 && "Unknown truncate!");
1497 case MVT::i1:
1498 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1499 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1500 }
1501 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1502 return Result;
1503
Chris Lattner590d8002005-01-09 18:52:44 +00001504 case ISD::SINT_TO_FP:
1505 case ISD::UINT_TO_FP: {
1506 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001507 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001508
1509 // Promote the integer to a type supported by FLD. We do this because there
1510 // are no unsigned FLD instructions, so we must promote an unsigned value to
1511 // a larger signed value, then use FLD on the larger value.
1512 //
1513 MVT::ValueType PromoteType = MVT::Other;
1514 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1515 unsigned PromoteOpcode = 0;
1516 unsigned RealDestReg = Result;
1517 switch (SrcTy) {
1518 case MVT::i1:
1519 case MVT::i8:
1520 // We don't have the facilities for directly loading byte sized data from
1521 // memory (even signed). Promote it to 16 bits.
1522 PromoteType = MVT::i16;
1523 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1524 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1525 break;
1526 case MVT::i16:
1527 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1528 PromoteType = MVT::i32;
1529 PromoteOpcode = X86::MOVZX32rr16;
1530 }
1531 break;
1532 default:
1533 // Don't fild into the real destination.
1534 if (Node->getOpcode() == ISD::UINT_TO_FP)
1535 Result = MakeReg(Node->getValueType(0));
1536 break;
1537 }
1538
1539 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1540
1541 if (PromoteType != MVT::Other) {
1542 Tmp2 = MakeReg(PromoteType);
1543 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1544 SrcTy = PromoteType;
1545 Tmp1 = Tmp2;
1546 }
1547
1548 // Spill the integer to memory and reload it from there.
1549 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1550 MachineFunction *F = BB->getParent();
1551 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1552
1553 switch (SrcTy) {
1554 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001555 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001556 // FIXME: this won't work for cast [u]long to FP
1557 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1558 FrameIdx).addReg(Tmp1);
1559 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1560 FrameIdx, 4).addReg(Tmp1+1);
1561 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1562 break;
1563 case MVT::i32:
1564 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1565 FrameIdx).addReg(Tmp1);
1566 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1567 break;
1568 case MVT::i16:
1569 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1570 FrameIdx).addReg(Tmp1);
1571 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1572 break;
1573 default: break; // No promotion required.
1574 }
1575
Chris Lattner085c9952005-01-12 04:00:00 +00001576 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001577 // If this is a cast from uint -> double, we need to be careful when if
1578 // the "sign" bit is set. If so, we don't want to make a negative number,
1579 // we want to make a positive number. Emit code to add an offset if the
1580 // sign bit is set.
1581
1582 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1583 unsigned IsNeg = MakeReg(MVT::i32);
1584 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1585
1586 // Create a CP value that has the offset in one word and 0 in the other.
1587 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1588 0x4f80000000000000ULL);
1589 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1590 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1591 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1592
1593 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1594 // We need special handling for unsigned 64-bit integer sources. If the
1595 // input number has the "sign bit" set, then we loaded it incorrectly as a
1596 // negative 64-bit number. In this case, add an offset value.
1597
1598 // Emit a test instruction to see if the dynamic input value was signed.
1599 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1600
1601 // If the sign bit is set, get a pointer to an offset, otherwise get a
1602 // pointer to a zero.
1603 MachineConstantPool *CP = F->getConstantPool();
1604 unsigned Zero = MakeReg(MVT::i32);
1605 Constant *Null = Constant::getNullValue(Type::UIntTy);
1606 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1607 CP->getConstantPoolIndex(Null));
1608 unsigned Offset = MakeReg(MVT::i32);
1609 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1610
1611 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1612 CP->getConstantPoolIndex(OffsetCst));
1613 unsigned Addr = MakeReg(MVT::i32);
1614 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1615
1616 // Load the constant for an add. FIXME: this could make an 'fadd' that
1617 // reads directly from memory, but we don't support these yet.
1618 unsigned ConstReg = MakeReg(MVT::f64);
1619 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1620
1621 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1622 }
1623 return RealDestReg;
1624 }
1625 case ISD::FP_TO_SINT:
1626 case ISD::FP_TO_UINT: {
1627 // FIXME: Most of this grunt work should be done by legalize!
1628 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1629
1630 // Change the floating point control register to use "round towards zero"
1631 // mode when truncating to an integer value.
1632 //
1633 MachineFunction *F = BB->getParent();
1634 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1635 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1636
1637 // Load the old value of the high byte of the control word...
1638 unsigned HighPartOfCW = MakeReg(MVT::i8);
1639 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1640 CWFrameIdx, 1);
1641
1642 // Set the high part to be round to zero...
1643 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1644 CWFrameIdx, 1).addImm(12);
1645
1646 // Reload the modified control word now...
1647 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1648
1649 // Restore the memory image of control word to original value
1650 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1651 CWFrameIdx, 1).addReg(HighPartOfCW);
1652
1653 // We don't have the facilities for directly storing byte sized data to
1654 // memory. Promote it to 16 bits. We also must promote unsigned values to
1655 // larger classes because we only have signed FP stores.
1656 MVT::ValueType StoreClass = Node->getValueType(0);
1657 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1658 switch (StoreClass) {
1659 case MVT::i8: StoreClass = MVT::i16; break;
1660 case MVT::i16: StoreClass = MVT::i32; break;
1661 case MVT::i32: StoreClass = MVT::i64; break;
1662 // The following treatment of cLong may not be perfectly right,
1663 // but it survives chains of casts of the form
1664 // double->ulong->double.
1665 case MVT::i64: StoreClass = MVT::i64; break;
1666 default: assert(0 && "Unknown store class!");
1667 }
1668
1669 // Spill the integer to memory and reload it from there.
1670 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1671 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1672
1673 switch (StoreClass) {
1674 default: assert(0 && "Unknown store class!");
1675 case MVT::i16:
1676 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1677 break;
1678 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001679 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001680 break;
1681 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001682 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001683 break;
1684 }
1685
1686 switch (Node->getValueType(0)) {
1687 default:
1688 assert(0 && "Unknown integer type!");
1689 case MVT::i64:
1690 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001691 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001692 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1693 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1694 case MVT::i32:
1695 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1696 break;
1697 case MVT::i16:
1698 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1699 break;
1700 case MVT::i8:
1701 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1702 break;
1703 }
1704
1705 // Reload the original control word now.
1706 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1707 return Result;
1708 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001709 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001710 Op0 = N.getOperand(0);
1711 Op1 = N.getOperand(1);
1712
Chris Lattner44129b52005-01-25 20:03:11 +00001713 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001714 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001715 goto FoldAdd;
1716 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001717
Chris Lattner44129b52005-01-25 20:03:11 +00001718 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001719 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001720 switch (N.getValueType()) {
1721 default: assert(0 && "Cannot add this type!");
1722 case MVT::i1:
1723 case MVT::i8: Opc = X86::ADD8rm; break;
1724 case MVT::i16: Opc = X86::ADD16rm; break;
1725 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001726 case MVT::f64:
1727 // For F64, handle promoted load operations (from F32) as well!
1728 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1729 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001730 }
1731 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001732 EmitFoldedLoad(Op1, AM);
1733 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001734 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1735 return Result;
1736 }
1737
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001738 // See if we can codegen this as an LEA to fold operations together.
1739 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001740 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001741 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001742 MatchAddress(N, AM);
1743 ExprMap[N] = Result;
1744
1745 // If this is not just an add, emit the LEA. For a simple add (like
1746 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1747 // leave this as LEA, then peephole it to 'ADD' after two address elim
1748 // happens.
1749 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1750 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1751 X86AddressMode XAM = SelectAddrExprs(AM);
1752 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1753 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001754 }
1755 }
Chris Lattner11333092005-01-11 03:11:44 +00001756
Chris Lattnera5ade062005-01-11 21:19:59 +00001757 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001758 Opc = 0;
1759 if (CN->getValue() == 1) { // add X, 1 -> inc X
1760 switch (N.getValueType()) {
1761 default: assert(0 && "Cannot integer add this type!");
1762 case MVT::i8: Opc = X86::INC8r; break;
1763 case MVT::i16: Opc = X86::INC16r; break;
1764 case MVT::i32: Opc = X86::INC32r; break;
1765 }
1766 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1767 switch (N.getValueType()) {
1768 default: assert(0 && "Cannot integer add this type!");
1769 case MVT::i8: Opc = X86::DEC8r; break;
1770 case MVT::i16: Opc = X86::DEC16r; break;
1771 case MVT::i32: Opc = X86::DEC32r; break;
1772 }
1773 }
1774
1775 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001776 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001777 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1778 return Result;
1779 }
1780
1781 switch (N.getValueType()) {
1782 default: assert(0 && "Cannot add this type!");
1783 case MVT::i8: Opc = X86::ADD8ri; break;
1784 case MVT::i16: Opc = X86::ADD16ri; break;
1785 case MVT::i32: Opc = X86::ADD32ri; break;
1786 }
1787 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001788 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001789 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1790 return Result;
1791 }
1792 }
1793
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001794 switch (N.getValueType()) {
1795 default: assert(0 && "Cannot add this type!");
1796 case MVT::i8: Opc = X86::ADD8rr; break;
1797 case MVT::i16: Opc = X86::ADD16rr; break;
1798 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001799 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001800 }
Chris Lattner11333092005-01-11 03:11:44 +00001801
Chris Lattnera5ade062005-01-11 21:19:59 +00001802 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1803 Tmp1 = SelectExpr(Op0);
1804 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001805 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001806 Tmp2 = SelectExpr(Op1);
1807 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001808 }
1809
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001810 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1811 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001812
1813 case ISD::FABS:
1814 Tmp1 = SelectExpr(Node->getOperand(0));
1815 BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1);
1816 return Result;
1817 case ISD::FNEG:
1818 Tmp1 = SelectExpr(Node->getOperand(0));
1819 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1820 return Result;
1821
Chris Lattner8db0af12005-04-06 04:21:07 +00001822 case ISD::MULHU:
1823 switch (N.getValueType()) {
1824 default: assert(0 && "Unsupported VT!");
1825 case MVT::i8: Tmp2 = X86::MUL8r; break;
1826 case MVT::i16: Tmp2 = X86::MUL16r; break;
1827 case MVT::i32: Tmp2 = X86::MUL32r; break;
1828 }
1829 // FALL THROUGH
1830 case ISD::MULHS: {
1831 unsigned MovOpc, LowReg, HiReg;
1832 switch (N.getValueType()) {
1833 default: assert(0 && "Unsupported VT!");
1834 case MVT::i8:
1835 MovOpc = X86::MOV8rr;
1836 LowReg = X86::AL;
1837 HiReg = X86::AH;
1838 Opc = X86::IMUL8r;
1839 break;
1840 case MVT::i16:
1841 MovOpc = X86::MOV16rr;
1842 LowReg = X86::AX;
1843 HiReg = X86::DX;
1844 Opc = X86::IMUL16r;
1845 break;
1846 case MVT::i32:
1847 MovOpc = X86::MOV32rr;
1848 LowReg = X86::EAX;
1849 HiReg = X86::EDX;
1850 Opc = X86::IMUL32r;
1851 break;
1852 }
1853 if (Node->getOpcode() != ISD::MULHS)
1854 Opc = Tmp2; // Get the MULHU opcode.
1855
1856 Op0 = Node->getOperand(0);
1857 Op1 = Node->getOperand(1);
1858 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1859 Tmp1 = SelectExpr(Op0);
1860 Tmp2 = SelectExpr(Op1);
1861 } else {
1862 Tmp2 = SelectExpr(Op1);
1863 Tmp1 = SelectExpr(Op0);
1864 }
1865
1866 // FIXME: Implement folding of loads into the memory operands here!
1867 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1868 BuildMI(BB, Opc, 1).addReg(Tmp2);
1869 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1870 return Result;
1871 }
1872
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001873 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001874 case ISD::MUL:
1875 case ISD::AND:
1876 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001877 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001878 static const unsigned SUBTab[] = {
1879 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1880 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1881 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1882 };
1883 static const unsigned MULTab[] = {
1884 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1885 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1886 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1887 };
1888 static const unsigned ANDTab[] = {
1889 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1890 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1891 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1892 };
1893 static const unsigned ORTab[] = {
1894 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1895 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1896 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1897 };
1898 static const unsigned XORTab[] = {
1899 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1900 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1901 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1902 };
1903
1904 Op0 = Node->getOperand(0);
1905 Op1 = Node->getOperand(1);
1906
Chris Lattner30ea1e92005-01-19 07:37:26 +00001907 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1908 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001909 return Result;
1910
1911 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001912 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1913 if (CN->isNullValue()) { // 0 - N -> neg N
1914 switch (N.getValueType()) {
1915 default: assert(0 && "Cannot sub this type!");
1916 case MVT::i1:
1917 case MVT::i8: Opc = X86::NEG8r; break;
1918 case MVT::i16: Opc = X86::NEG16r; break;
1919 case MVT::i32: Opc = X86::NEG32r; break;
1920 }
1921 Tmp1 = SelectExpr(N.getOperand(1));
1922 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1923 return Result;
1924 }
1925
Chris Lattnera5ade062005-01-11 21:19:59 +00001926 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1927 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001928 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001929 switch (N.getValueType()) {
1930 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001931 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001932 case MVT::i8: Opc = X86::NOT8r; break;
1933 case MVT::i16: Opc = X86::NOT16r; break;
1934 case MVT::i32: Opc = X86::NOT32r; break;
1935 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001936 if (Opc) {
1937 Tmp1 = SelectExpr(Op0);
1938 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1939 return Result;
1940 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001941 }
1942
Chris Lattner2a4e5082005-01-17 06:48:02 +00001943 // Fold common multiplies into LEA instructions.
1944 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1945 switch ((int)CN->getValue()) {
1946 default: break;
1947 case 3:
1948 case 5:
1949 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001950 // Remove N from exprmap so SelectAddress doesn't get confused.
1951 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001952 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001953 SelectAddress(N, AM);
1954 // Restore it to the map.
1955 ExprMap[N] = Result;
1956 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1957 return Result;
1958 }
1959 }
1960
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001961 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001962 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001963 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001964 case MVT::i8: Opc = 0; break;
1965 case MVT::i16: Opc = 1; break;
1966 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001967 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001968 switch (Node->getOpcode()) {
1969 default: assert(0 && "Unreachable!");
1970 case ISD::SUB: Opc = SUBTab[Opc]; break;
1971 case ISD::MUL: Opc = MULTab[Opc]; break;
1972 case ISD::AND: Opc = ANDTab[Opc]; break;
1973 case ISD::OR: Opc = ORTab[Opc]; break;
1974 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001975 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001976 if (Opc) { // Can't fold MUL:i8 R, imm
1977 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001978 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1979 return Result;
1980 }
1981 }
Chris Lattner11333092005-01-11 03:11:44 +00001982
Chris Lattner44129b52005-01-25 20:03:11 +00001983 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00001984 if (Node->getOpcode() != ISD::SUB) {
1985 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001986 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001987 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00001988 // For FP, emit 'reverse' subract, with a memory operand.
1989 if (N.getValueType() == MVT::f64) {
1990 if (Op0.getOpcode() == ISD::EXTLOAD)
1991 Opc = X86::FSUBR32m;
1992 else
1993 Opc = X86::FSUBR64m;
1994
Chris Lattnera5ade062005-01-11 21:19:59 +00001995 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001996 EmitFoldedLoad(Op0, AM);
1997 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001998 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1999 return Result;
2000 }
2001 }
2002
Chris Lattner44129b52005-01-25 20:03:11 +00002003 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002004 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002005 switch (N.getValueType()) {
2006 default: assert(0 && "Cannot operate on this type!");
2007 case MVT::i1:
2008 case MVT::i8: Opc = 5; break;
2009 case MVT::i16: Opc = 6; break;
2010 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002011 // For F64, handle promoted load operations (from F32) as well!
2012 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002013 }
2014 switch (Node->getOpcode()) {
2015 default: assert(0 && "Unreachable!");
2016 case ISD::SUB: Opc = SUBTab[Opc]; break;
2017 case ISD::MUL: Opc = MULTab[Opc]; break;
2018 case ISD::AND: Opc = ANDTab[Opc]; break;
2019 case ISD::OR: Opc = ORTab[Opc]; break;
2020 case ISD::XOR: Opc = XORTab[Opc]; break;
2021 }
2022
2023 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002024 EmitFoldedLoad(Op1, AM);
2025 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002026 if (Opc) {
2027 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2028 } else {
2029 assert(Node->getOpcode() == ISD::MUL &&
2030 N.getValueType() == MVT::i8 && "Unexpected situation!");
2031 // Must use the MUL instruction, which forces use of AL.
2032 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2033 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2034 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2035 }
2036 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002037 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002038
2039 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2040 Tmp1 = SelectExpr(Op0);
2041 Tmp2 = SelectExpr(Op1);
2042 } else {
2043 Tmp2 = SelectExpr(Op1);
2044 Tmp1 = SelectExpr(Op0);
2045 }
2046
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002047 switch (N.getValueType()) {
2048 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002049 case MVT::i1:
2050 case MVT::i8: Opc = 10; break;
2051 case MVT::i16: Opc = 11; break;
2052 case MVT::i32: Opc = 12; break;
2053 case MVT::f32: Opc = 13; break;
2054 case MVT::f64: Opc = 14; break;
2055 }
2056 switch (Node->getOpcode()) {
2057 default: assert(0 && "Unreachable!");
2058 case ISD::SUB: Opc = SUBTab[Opc]; break;
2059 case ISD::MUL: Opc = MULTab[Opc]; break;
2060 case ISD::AND: Opc = ANDTab[Opc]; break;
2061 case ISD::OR: Opc = ORTab[Opc]; break;
2062 case ISD::XOR: Opc = XORTab[Opc]; break;
2063 }
2064 if (Opc) {
2065 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2066 } else {
2067 assert(Node->getOpcode() == ISD::MUL &&
2068 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002069 // Must use the MUL instruction, which forces use of AL.
2070 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2071 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2072 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002073 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002074 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002075 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002076 case ISD::ADD_PARTS:
2077 case ISD::SUB_PARTS: {
2078 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2079 "Not an i64 add/sub!");
2080 // Emit all of the operands.
2081 std::vector<unsigned> InVals;
2082 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2083 InVals.push_back(SelectExpr(N.getOperand(i)));
2084 if (N.getOpcode() == ISD::ADD_PARTS) {
2085 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2086 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2087 } else {
2088 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2089 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2090 }
2091 return Result+N.ResNo;
2092 }
2093
Chris Lattnerb38a7492005-04-02 04:01:14 +00002094 case ISD::SHL_PARTS:
2095 case ISD::SRA_PARTS:
2096 case ISD::SRL_PARTS: {
2097 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2098 "Not an i64 shift!");
2099 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2100 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2101 unsigned TmpReg = MakeReg(MVT::i32);
2102 if (N.getOpcode() == ISD::SRA_PARTS) {
2103 // If this is a SHR of a Long, then we need to do funny sign extension
2104 // stuff. TmpReg gets the value to use as the high-part if we are
2105 // shifting more than 32 bits.
2106 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2107 } else {
2108 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2109 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2110 }
2111
2112 // Initialize CL with the shift amount.
2113 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2114 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2115
2116 unsigned TmpReg2 = MakeReg(MVT::i32);
2117 unsigned TmpReg3 = MakeReg(MVT::i32);
2118 if (N.getOpcode() == ISD::SHL_PARTS) {
2119 // TmpReg2 = shld inHi, inLo
2120 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2121 .addReg(ShiftOpLo);
2122 // TmpReg3 = shl inLo, CL
2123 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
2124
2125 // Set the flags to indicate whether the shift was by more than 32 bits.
2126 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
2127
2128 // DestHi = (>32) ? TmpReg3 : TmpReg2;
2129 BuildMI(BB, X86::CMOVNE32rr, 2,
2130 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2131 // DestLo = (>32) ? TmpReg : TmpReg3;
2132 BuildMI(BB, X86::CMOVNE32rr, 2,
2133 Result).addReg(TmpReg3).addReg(TmpReg);
2134 } else {
2135 // TmpReg2 = shrd inLo, inHi
2136 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2137 .addReg(ShiftOpHi);
2138 // TmpReg3 = s[ah]r inHi, CL
2139 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
2140 : X86::SHR32rCL, 1, TmpReg3)
2141 .addReg(ShiftOpHi);
2142
2143 // Set the flags to indicate whether the shift was by more than 32 bits.
2144 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
2145
2146 // DestLo = (>32) ? TmpReg3 : TmpReg2;
2147 BuildMI(BB, X86::CMOVNE32rr, 2,
2148 Result).addReg(TmpReg2).addReg(TmpReg3);
2149
2150 // DestHi = (>32) ? TmpReg : TmpReg3;
2151 BuildMI(BB, X86::CMOVNE32rr, 2,
2152 Result+1).addReg(TmpReg3).addReg(TmpReg);
2153 }
2154 return Result+N.ResNo;
2155 }
2156
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002157 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002158 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2159 Tmp2 = SelectExpr(N.getOperand(1));
2160 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002161 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002162 Tmp3 = SelectExpr(N.getOperand(2));
2163 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002164 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002165 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2166 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002167
2168 case ISD::SDIV:
2169 case ISD::UDIV:
2170 case ISD::SREM:
2171 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002172 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2173 "We don't support this operator!");
2174
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002175 if (N.getOpcode() == ISD::SDIV)
Chris Lattner3576c842005-01-25 20:35:10 +00002176
2177 // We can fold loads into FpDIVs, but not really into any others.
2178 if (N.getValueType() == MVT::f64) {
2179 // Check for reversed and unreversed DIV.
2180 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2181 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2182 Opc = X86::FDIVR32m;
2183 else
2184 Opc = X86::FDIVR64m;
2185 X86AddressMode AM;
2186 EmitFoldedLoad(N.getOperand(0), AM);
2187 Tmp1 = SelectExpr(N.getOperand(1));
2188 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2189 return Result;
2190 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2191 N.getOperand(1).getOpcode() == ISD::LOAD) {
2192 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2193 Opc = X86::FDIV32m;
2194 else
2195 Opc = X86::FDIV64m;
2196 X86AddressMode AM;
2197 EmitFoldedLoad(N.getOperand(1), AM);
2198 Tmp1 = SelectExpr(N.getOperand(0));
2199 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2200 return Result;
2201 }
2202 }
2203
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002204 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2205 // FIXME: These special cases should be handled by the lowering impl!
2206 unsigned RHS = CN->getValue();
2207 bool isNeg = false;
2208 if ((int)RHS < 0) {
2209 isNeg = true;
2210 RHS = -RHS;
2211 }
2212 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2213 unsigned Log = log2(RHS);
2214 unsigned TmpReg = MakeReg(N.getValueType());
2215 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2216 switch (N.getValueType()) {
2217 default: assert("Unknown type to signed divide!");
2218 case MVT::i8:
2219 SAROpc = X86::SAR8ri;
2220 SHROpc = X86::SHR8ri;
2221 ADDOpc = X86::ADD8rr;
2222 NEGOpc = X86::NEG8r;
2223 break;
2224 case MVT::i16:
2225 SAROpc = X86::SAR16ri;
2226 SHROpc = X86::SHR16ri;
2227 ADDOpc = X86::ADD16rr;
2228 NEGOpc = X86::NEG16r;
2229 break;
2230 case MVT::i32:
2231 SAROpc = X86::SAR32ri;
2232 SHROpc = X86::SHR32ri;
2233 ADDOpc = X86::ADD32rr;
2234 NEGOpc = X86::NEG32r;
2235 break;
2236 }
Chris Lattner11333092005-01-11 03:11:44 +00002237 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002238 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2239 unsigned TmpReg2 = MakeReg(N.getValueType());
2240 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2241 unsigned TmpReg3 = MakeReg(N.getValueType());
2242 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
2243
2244 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2245 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2246 if (isNeg)
2247 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2248 return Result;
2249 }
2250 }
2251
Chris Lattner11333092005-01-11 03:11:44 +00002252 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2253 Tmp1 = SelectExpr(N.getOperand(0));
2254 Tmp2 = SelectExpr(N.getOperand(1));
2255 } else {
2256 Tmp2 = SelectExpr(N.getOperand(1));
2257 Tmp1 = SelectExpr(N.getOperand(0));
2258 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002259
2260 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2261 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2262 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2263 switch (N.getValueType()) {
2264 default: assert(0 && "Cannot sdiv this type!");
2265 case MVT::i8:
2266 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2267 LoReg = X86::AL;
2268 HiReg = X86::AH;
2269 MovOpcode = X86::MOV8rr;
2270 ClrOpcode = X86::MOV8ri;
2271 SExtOpcode = X86::CBW;
2272 break;
2273 case MVT::i16:
2274 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2275 LoReg = X86::AX;
2276 HiReg = X86::DX;
2277 MovOpcode = X86::MOV16rr;
2278 ClrOpcode = X86::MOV16ri;
2279 SExtOpcode = X86::CWD;
2280 break;
2281 case MVT::i32:
2282 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002283 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002284 HiReg = X86::EDX;
2285 MovOpcode = X86::MOV32rr;
2286 ClrOpcode = X86::MOV32ri;
2287 SExtOpcode = X86::CDQ;
2288 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002289 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002290 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002291 return Result;
2292 }
2293
2294 // Set up the low part.
2295 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2296
2297 if (isSigned) {
2298 // Sign extend the low part into the high part.
2299 BuildMI(BB, SExtOpcode, 0);
2300 } else {
2301 // Zero out the high part, effectively zero extending the input.
2302 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2303 }
2304
2305 // Emit the DIV/IDIV instruction.
2306 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
2307
2308 // Get the result of the divide or rem.
2309 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2310 return Result;
2311 }
2312
2313 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002314 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002315 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2316 switch (N.getValueType()) {
2317 default: assert(0 && "Cannot shift this type!");
2318 case MVT::i8: Opc = X86::ADD8rr; break;
2319 case MVT::i16: Opc = X86::ADD16rr; break;
2320 case MVT::i32: Opc = X86::ADD32rr; break;
2321 }
2322 Tmp1 = SelectExpr(N.getOperand(0));
2323 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2324 return Result;
2325 }
2326
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002327 switch (N.getValueType()) {
2328 default: assert(0 && "Cannot shift this type!");
2329 case MVT::i8: Opc = X86::SHL8ri; break;
2330 case MVT::i16: Opc = X86::SHL16ri; break;
2331 case MVT::i32: Opc = X86::SHL32ri; break;
2332 }
Chris Lattner11333092005-01-11 03:11:44 +00002333 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002334 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2335 return Result;
2336 }
Chris Lattner11333092005-01-11 03:11:44 +00002337
2338 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2339 Tmp1 = SelectExpr(N.getOperand(0));
2340 Tmp2 = SelectExpr(N.getOperand(1));
2341 } else {
2342 Tmp2 = SelectExpr(N.getOperand(1));
2343 Tmp1 = SelectExpr(N.getOperand(0));
2344 }
2345
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002346 switch (N.getValueType()) {
2347 default: assert(0 && "Cannot shift this type!");
2348 case MVT::i8 : Opc = X86::SHL8rCL; break;
2349 case MVT::i16: Opc = X86::SHL16rCL; break;
2350 case MVT::i32: Opc = X86::SHL32rCL; break;
2351 }
2352 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2353 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2354 return Result;
2355 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002356 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2357 switch (N.getValueType()) {
2358 default: assert(0 && "Cannot shift this type!");
2359 case MVT::i8: Opc = X86::SHR8ri; break;
2360 case MVT::i16: Opc = X86::SHR16ri; break;
2361 case MVT::i32: Opc = X86::SHR32ri; break;
2362 }
Chris Lattner11333092005-01-11 03:11:44 +00002363 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002364 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2365 return Result;
2366 }
Chris Lattner11333092005-01-11 03:11:44 +00002367
2368 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2369 Tmp1 = SelectExpr(N.getOperand(0));
2370 Tmp2 = SelectExpr(N.getOperand(1));
2371 } else {
2372 Tmp2 = SelectExpr(N.getOperand(1));
2373 Tmp1 = SelectExpr(N.getOperand(0));
2374 }
2375
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002376 switch (N.getValueType()) {
2377 default: assert(0 && "Cannot shift this type!");
2378 case MVT::i8 : Opc = X86::SHR8rCL; break;
2379 case MVT::i16: Opc = X86::SHR16rCL; break;
2380 case MVT::i32: Opc = X86::SHR32rCL; break;
2381 }
2382 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2383 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2384 return Result;
2385 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002386 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2387 switch (N.getValueType()) {
2388 default: assert(0 && "Cannot shift this type!");
2389 case MVT::i8: Opc = X86::SAR8ri; break;
2390 case MVT::i16: Opc = X86::SAR16ri; break;
2391 case MVT::i32: Opc = X86::SAR32ri; break;
2392 }
Chris Lattner11333092005-01-11 03:11:44 +00002393 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002394 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2395 return Result;
2396 }
Chris Lattner11333092005-01-11 03:11:44 +00002397
2398 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2399 Tmp1 = SelectExpr(N.getOperand(0));
2400 Tmp2 = SelectExpr(N.getOperand(1));
2401 } else {
2402 Tmp2 = SelectExpr(N.getOperand(1));
2403 Tmp1 = SelectExpr(N.getOperand(0));
2404 }
2405
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002406 switch (N.getValueType()) {
2407 default: assert(0 && "Cannot shift this type!");
2408 case MVT::i8 : Opc = X86::SAR8rCL; break;
2409 case MVT::i16: Opc = X86::SAR16rCL; break;
2410 case MVT::i32: Opc = X86::SAR32rCL; break;
2411 }
2412 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2413 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2414 return Result;
2415
2416 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002417 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002418 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2419 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2420 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002421 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002422 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002423 if (Result != 1) { // Generate the token
2424 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2425 assert(0 && "Load already emitted!?");
2426 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002427 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2428
Chris Lattner5188ad72005-01-08 19:28:19 +00002429 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002430 default: assert(0 && "Cannot load this type!");
2431 case MVT::i1:
2432 case MVT::i8: Opc = X86::MOV8rm; break;
2433 case MVT::i16: Opc = X86::MOV16rm; break;
2434 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002435 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2436 }
Chris Lattner11333092005-01-11 03:11:44 +00002437
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002438 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002439 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002440 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2441 } else {
2442 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002443
2444 SDOperand Chain = N.getOperand(0);
2445 SDOperand Address = N.getOperand(1);
2446 if (getRegPressure(Chain) > getRegPressure(Address)) {
2447 Select(Chain);
2448 SelectAddress(Address, AM);
2449 } else {
2450 SelectAddress(Address, AM);
2451 Select(Chain);
2452 }
2453
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002454 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2455 }
2456 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002457
2458 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2459 case ISD::ZEXTLOAD: {
2460 // Make sure we generate both values.
2461 if (Result != 1)
2462 ExprMap[N.getValue(1)] = 1; // Generate the token
2463 else
2464 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2465
Chris Lattnerda2ce112005-01-16 07:34:08 +00002466 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2467 if (Node->getValueType(0) == MVT::f64) {
2468 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2469 "Bad EXTLOAD!");
2470 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2471 CP->getIndex());
2472 return Result;
2473 }
2474
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002475 X86AddressMode AM;
2476 if (getRegPressure(Node->getOperand(0)) >
2477 getRegPressure(Node->getOperand(1))) {
2478 Select(Node->getOperand(0)); // chain
2479 SelectAddress(Node->getOperand(1), AM);
2480 } else {
2481 SelectAddress(Node->getOperand(1), AM);
2482 Select(Node->getOperand(0)); // chain
2483 }
2484
2485 switch (Node->getValueType(0)) {
2486 default: assert(0 && "Unknown type to sign extend to.");
2487 case MVT::f64:
2488 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2489 "Bad EXTLOAD!");
2490 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2491 break;
2492 case MVT::i32:
2493 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2494 default:
2495 assert(0 && "Bad zero extend!");
2496 case MVT::i1:
2497 case MVT::i8:
2498 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2499 break;
2500 case MVT::i16:
2501 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2502 break;
2503 }
2504 break;
2505 case MVT::i16:
2506 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2507 "Bad zero extend!");
2508 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2509 break;
2510 case MVT::i8:
2511 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2512 "Bad zero extend!");
2513 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2514 break;
2515 }
2516 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002517 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002518 case ISD::SEXTLOAD: {
2519 // Make sure we generate both values.
2520 if (Result != 1)
2521 ExprMap[N.getValue(1)] = 1; // Generate the token
2522 else
2523 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2524
2525 X86AddressMode AM;
2526 if (getRegPressure(Node->getOperand(0)) >
2527 getRegPressure(Node->getOperand(1))) {
2528 Select(Node->getOperand(0)); // chain
2529 SelectAddress(Node->getOperand(1), AM);
2530 } else {
2531 SelectAddress(Node->getOperand(1), AM);
2532 Select(Node->getOperand(0)); // chain
2533 }
2534
2535 switch (Node->getValueType(0)) {
2536 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2537 default: assert(0 && "Unknown type to sign extend to.");
2538 case MVT::i32:
2539 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2540 default:
2541 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2542 case MVT::i8:
2543 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2544 break;
2545 case MVT::i16:
2546 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2547 break;
2548 }
2549 break;
2550 case MVT::i16:
2551 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2552 "Cannot sign extend from bool!");
2553 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2554 break;
2555 }
2556 return Result;
2557 }
2558
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002559 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002560 // Generate both result values.
2561 if (Result != 1)
2562 ExprMap[N.getValue(1)] = 1; // Generate the token
2563 else
2564 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2565
2566 // FIXME: We are currently ignoring the requested alignment for handling
2567 // greater than the stack alignment. This will need to be revisited at some
2568 // point. Align = N.getOperand(2);
2569
2570 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2571 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2572 std::cerr << "Cannot allocate stack object with greater alignment than"
2573 << " the stack alignment yet!";
2574 abort();
2575 }
2576
2577 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002578 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002579 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2580 .addImm(CN->getValue());
2581 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002582 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2583 Select(N.getOperand(0));
2584 Tmp1 = SelectExpr(N.getOperand(1));
2585 } else {
2586 Tmp1 = SelectExpr(N.getOperand(1));
2587 Select(N.getOperand(0));
2588 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002589
2590 // Subtract size from stack pointer, thereby allocating some space.
2591 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2592 }
2593
2594 // Put a pointer to the space into the result register, by copying the stack
2595 // pointer.
2596 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2597 return Result;
2598
2599 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002600 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002601 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002602
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002603 if (GlobalAddressSDNode *GASD =
2604 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002605 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002606 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2607 } else if (ExternalSymbolSDNode *ESSDN =
2608 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002609 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002610 BuildMI(BB, X86::CALLpcrel32,
2611 1).addExternalSymbol(ESSDN->getSymbol(), true);
2612 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002613 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2614 Select(N.getOperand(0));
2615 Tmp1 = SelectExpr(N.getOperand(1));
2616 } else {
2617 Tmp1 = SelectExpr(N.getOperand(1));
2618 Select(N.getOperand(0));
2619 }
2620
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002621 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2622 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002623 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002624 default: assert(0 && "Unknown value type for call result!");
2625 case MVT::Other: return 1;
2626 case MVT::i1:
2627 case MVT::i8:
2628 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2629 break;
2630 case MVT::i16:
2631 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2632 break;
2633 case MVT::i32:
2634 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002635 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002636 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2637 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002638 case MVT::f64: // Floating-point return values live in %ST(0)
2639 ContainsFPCode = true;
2640 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2641 break;
2642 }
2643 return Result+N.ResNo;
2644 }
2645
2646 return 0;
2647}
2648
Chris Lattnere10269b2005-01-17 19:25:26 +00002649/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2650/// load/op/store instruction. If successful return true.
2651bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2652 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2653 SDOperand Chain = Node->getOperand(0);
2654 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002655 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002656
2657 // The chain has to be a load, the stored value must be an integer binary
2658 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002659 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002660 MVT::isFloatingPoint(StVal.getValueType()))
2661 return false;
2662
Chris Lattner5c659812005-01-17 22:10:42 +00002663 // Token chain must either be a factor node or the load to fold.
2664 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2665 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002666
Chris Lattner5c659812005-01-17 22:10:42 +00002667 SDOperand TheLoad;
2668
2669 // Check to see if there is a load from the same pointer that we're storing
2670 // to in either operand of the binop.
2671 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2672 StVal.getOperand(0).getOperand(1) == StPtr)
2673 TheLoad = StVal.getOperand(0);
2674 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2675 StVal.getOperand(1).getOperand(1) == StPtr)
2676 TheLoad = StVal.getOperand(1);
2677 else
2678 return false; // No matching load operand.
2679
2680 // We can only fold the load if there are no intervening side-effecting
2681 // operations. This means that the store uses the load as its token chain, or
2682 // there are only token factor nodes in between the store and load.
2683 if (Chain != TheLoad.getValue(1)) {
2684 // Okay, the other option is that we have a store referring to (possibly
2685 // nested) token factor nodes. For now, just try peeking through one level
2686 // of token factors to see if this is the case.
2687 bool ChainOk = false;
2688 if (Chain.getOpcode() == ISD::TokenFactor) {
2689 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2690 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2691 ChainOk = true;
2692 break;
2693 }
2694 }
2695
2696 if (!ChainOk) return false;
2697 }
2698
2699 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002700 return false;
2701
2702 // Make sure that one of the operands of the binop is the load, and that the
2703 // load folds into the binop.
2704 if (((StVal.getOperand(0) != TheLoad ||
2705 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2706 (StVal.getOperand(1) != TheLoad ||
2707 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2708 return false;
2709
2710 // Finally, check to see if this is one of the ops we can handle!
2711 static const unsigned ADDTAB[] = {
2712 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2713 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2714 };
2715 static const unsigned SUBTAB[] = {
2716 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2717 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2718 };
2719 static const unsigned ANDTAB[] = {
2720 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2721 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2722 };
2723 static const unsigned ORTAB[] = {
2724 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2725 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2726 };
2727 static const unsigned XORTAB[] = {
2728 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2729 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2730 };
2731 static const unsigned SHLTAB[] = {
2732 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2733 /*Have to put the reg in CL*/0, 0, 0,
2734 };
2735 static const unsigned SARTAB[] = {
2736 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2737 /*Have to put the reg in CL*/0, 0, 0,
2738 };
2739 static const unsigned SHRTAB[] = {
2740 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2741 /*Have to put the reg in CL*/0, 0, 0,
2742 };
2743
2744 const unsigned *TabPtr = 0;
2745 switch (StVal.getOpcode()) {
2746 default:
2747 std::cerr << "CANNOT [mem] op= val: ";
2748 StVal.Val->dump(); std::cerr << "\n";
2749 case ISD::MUL:
2750 case ISD::SDIV:
2751 case ISD::UDIV:
2752 case ISD::SREM:
2753 case ISD::UREM: return false;
2754
2755 case ISD::ADD: TabPtr = ADDTAB; break;
2756 case ISD::SUB: TabPtr = SUBTAB; break;
2757 case ISD::AND: TabPtr = ANDTAB; break;
2758 case ISD:: OR: TabPtr = ORTAB; break;
2759 case ISD::XOR: TabPtr = XORTAB; break;
2760 case ISD::SHL: TabPtr = SHLTAB; break;
2761 case ISD::SRA: TabPtr = SARTAB; break;
2762 case ISD::SRL: TabPtr = SHRTAB; break;
2763 }
2764
2765 // Handle: [mem] op= CST
2766 SDOperand Op0 = StVal.getOperand(0);
2767 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002768 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002769 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2770 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2771 default: break;
2772 case MVT::i1:
2773 case MVT::i8: Opc = TabPtr[0]; break;
2774 case MVT::i16: Opc = TabPtr[1]; break;
2775 case MVT::i32: Opc = TabPtr[2]; break;
2776 }
2777
2778 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002779 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2780 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002781 Select(Chain);
2782
Chris Lattnere10269b2005-01-17 19:25:26 +00002783 X86AddressMode AM;
2784 if (getRegPressure(TheLoad.getOperand(0)) >
2785 getRegPressure(TheLoad.getOperand(1))) {
2786 Select(TheLoad.getOperand(0));
2787 SelectAddress(TheLoad.getOperand(1), AM);
2788 } else {
2789 SelectAddress(TheLoad.getOperand(1), AM);
2790 Select(TheLoad.getOperand(0));
2791 }
Chris Lattner5c659812005-01-17 22:10:42 +00002792
2793 if (StVal.getOpcode() == ISD::ADD) {
2794 if (CN->getValue() == 1) {
2795 switch (Op0.getValueType()) {
2796 default: break;
2797 case MVT::i8:
2798 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2799 return true;
2800 case MVT::i16: Opc = TabPtr[1];
2801 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2802 return true;
2803 case MVT::i32: Opc = TabPtr[2];
2804 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2805 return true;
2806 }
2807 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2808 switch (Op0.getValueType()) {
2809 default: break;
2810 case MVT::i8:
2811 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2812 return true;
2813 case MVT::i16: Opc = TabPtr[1];
2814 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2815 return true;
2816 case MVT::i32: Opc = TabPtr[2];
2817 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2818 return true;
2819 }
2820 }
2821 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002822
2823 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2824 return true;
2825 }
2826 }
2827
2828 // If we have [mem] = V op [mem], try to turn it into:
2829 // [mem] = [mem] op V.
2830 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2831 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2832 StVal.getOpcode() != ISD::SRL)
2833 std::swap(Op0, Op1);
2834
2835 if (Op0 != TheLoad) return false;
2836
2837 switch (Op0.getValueType()) {
2838 default: return false;
2839 case MVT::i1:
2840 case MVT::i8: Opc = TabPtr[3]; break;
2841 case MVT::i16: Opc = TabPtr[4]; break;
2842 case MVT::i32: Opc = TabPtr[5]; break;
2843 }
Chris Lattner5c659812005-01-17 22:10:42 +00002844
Chris Lattnerb422aea2005-01-18 17:35:28 +00002845 // Table entry doesn't exist?
2846 if (Opc == 0) return false;
2847
Chris Lattner4a108662005-01-18 03:51:59 +00002848 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2849 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002850 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002851 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002852
Chris Lattnere10269b2005-01-17 19:25:26 +00002853 X86AddressMode AM;
2854 SelectAddress(TheLoad.getOperand(1), AM);
2855 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002856 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002857 return true;
2858}
2859
2860
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002861void ISel::Select(SDOperand N) {
2862 unsigned Tmp1, Tmp2, Opc;
2863
Nate Begeman85fdeb22005-03-24 04:39:54 +00002864 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002865 return; // Already selected.
2866
Chris Lattner989de032005-01-11 06:14:36 +00002867 SDNode *Node = N.Val;
2868
2869 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002870 default:
Chris Lattner989de032005-01-11 06:14:36 +00002871 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002872 assert(0 && "Node not handled yet!");
2873 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002874 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002875 if (Node->getNumOperands() == 2) {
2876 bool OneFirst =
2877 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2878 Select(Node->getOperand(OneFirst));
2879 Select(Node->getOperand(!OneFirst));
2880 } else {
2881 std::vector<std::pair<unsigned, unsigned> > OpsP;
2882 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2883 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2884 std::sort(OpsP.begin(), OpsP.end());
2885 std::reverse(OpsP.begin(), OpsP.end());
2886 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2887 Select(Node->getOperand(OpsP[i].second));
2888 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002889 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002890 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002891 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2892 Select(N.getOperand(0));
2893 Tmp1 = SelectExpr(N.getOperand(1));
2894 } else {
2895 Tmp1 = SelectExpr(N.getOperand(1));
2896 Select(N.getOperand(0));
2897 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002898 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002899
2900 if (Tmp1 != Tmp2) {
2901 switch (N.getOperand(1).getValueType()) {
2902 default: assert(0 && "Invalid type for operation!");
2903 case MVT::i1:
2904 case MVT::i8: Opc = X86::MOV8rr; break;
2905 case MVT::i16: Opc = X86::MOV16rr; break;
2906 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002907 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002908 }
2909 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2910 }
2911 return;
2912 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002913 switch (N.getNumOperands()) {
2914 default:
2915 assert(0 && "Unknown return instruction!");
2916 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002917 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2918 N.getOperand(2).getValueType() == MVT::i32 &&
2919 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002920 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2921 Tmp1 = SelectExpr(N.getOperand(1));
2922 Tmp2 = SelectExpr(N.getOperand(2));
2923 } else {
2924 Tmp2 = SelectExpr(N.getOperand(2));
2925 Tmp1 = SelectExpr(N.getOperand(1));
2926 }
2927 Select(N.getOperand(0));
2928
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002929 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2930 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2931 // Declare that EAX & EDX are live on exit.
2932 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2933 .addReg(X86::ESP);
2934 break;
2935 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002936 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2937 Select(N.getOperand(0));
2938 Tmp1 = SelectExpr(N.getOperand(1));
2939 } else {
2940 Tmp1 = SelectExpr(N.getOperand(1));
2941 Select(N.getOperand(0));
2942 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002943 switch (N.getOperand(1).getValueType()) {
2944 default: assert(0 && "All other types should have been promoted!!");
2945 case MVT::f64:
2946 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2947 // Declare that top-of-stack is live on exit
2948 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2949 break;
2950 case MVT::i32:
2951 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2952 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2953 break;
2954 }
2955 break;
2956 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002957 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002958 break;
2959 }
2960 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2961 return;
2962 case ISD::BR: {
2963 Select(N.getOperand(0));
2964 MachineBasicBlock *Dest =
2965 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2966 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2967 return;
2968 }
2969
2970 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002971 MachineBasicBlock *Dest =
2972 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002973
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002974 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2975 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002976 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2977 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2978 Select(N.getOperand(0));
2979 Tmp1 = SelectExpr(N.getOperand(1));
2980 } else {
2981 Tmp1 = SelectExpr(N.getOperand(1));
2982 Select(N.getOperand(0));
2983 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002984 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2985 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2986 }
Chris Lattner11333092005-01-11 03:11:44 +00002987
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002988 return;
2989 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002990
Chris Lattner4df0de92005-01-17 00:00:33 +00002991 case ISD::LOAD:
2992 // If this load could be folded into the only using instruction, and if it
2993 // is safe to emit the instruction here, try to do so now.
2994 if (Node->hasNUsesOfValue(1, 0)) {
2995 SDOperand TheVal = N.getValue(0);
2996 SDNode *User = 0;
2997 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2998 assert(UI != Node->use_end() && "Didn't find use!");
2999 SDNode *UN = *UI;
3000 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3001 if (UN->getOperand(i) == TheVal) {
3002 User = UN;
3003 goto FoundIt;
3004 }
3005 }
3006 FoundIt:
3007 // Only handle unary operators right now.
3008 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003009 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003010 SelectExpr(SDOperand(User, 0));
3011 return;
3012 }
3013 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003014 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003015 SelectExpr(N);
3016 return;
3017
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003018 case ISD::EXTLOAD:
3019 case ISD::SEXTLOAD:
3020 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003021 case ISD::CALL:
3022 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003023 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003024 SelectExpr(N);
3025 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003026
3027 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3028 // On X86, we can represent all types except for Bool and Float natively.
3029 X86AddressMode AM;
3030 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003031 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3032 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3033 && "Unsupported TRUNCSTORE for this target!");
3034
3035 if (StoredTy == MVT::i16) {
3036 // FIXME: This is here just to allow testing. X86 doesn't really have a
3037 // TRUNCSTORE i16 operation, but this is required for targets that do not
3038 // have 16-bit integer registers. We occasionally disable 16-bit integer
3039 // registers to test the promotion code.
3040 Select(N.getOperand(0));
3041 Tmp1 = SelectExpr(N.getOperand(1));
3042 SelectAddress(N.getOperand(2), AM);
3043
3044 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3045 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3046 return;
3047 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003048
3049 // Store of constant bool?
3050 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3051 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3052 Select(N.getOperand(0));
3053 SelectAddress(N.getOperand(2), AM);
3054 } else {
3055 SelectAddress(N.getOperand(2), AM);
3056 Select(N.getOperand(0));
3057 }
3058 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3059 return;
3060 }
3061
3062 switch (StoredTy) {
3063 default: assert(0 && "Cannot truncstore this type!");
3064 case MVT::i1: Opc = X86::MOV8mr; break;
3065 case MVT::f32: Opc = X86::FST32m; break;
3066 }
3067
3068 std::vector<std::pair<unsigned, unsigned> > RP;
3069 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3070 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3071 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3072 std::sort(RP.begin(), RP.end());
3073
Chris Lattner572dd082005-02-23 05:57:21 +00003074 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003075 for (unsigned i = 0; i != 3; ++i)
3076 switch (RP[2-i].second) {
3077 default: assert(0 && "Unknown operand number!");
3078 case 0: Select(N.getOperand(0)); break;
3079 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3080 case 2: SelectAddress(N.getOperand(2), AM); break;
3081 }
3082
3083 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3084 return;
3085 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003086 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003087 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003088
3089 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3090 Opc = 0;
3091 switch (CN->getValueType(0)) {
3092 default: assert(0 && "Invalid type for operation!");
3093 case MVT::i1:
3094 case MVT::i8: Opc = X86::MOV8mi; break;
3095 case MVT::i16: Opc = X86::MOV16mi; break;
3096 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003097 case MVT::f64: break;
3098 }
3099 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003100 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3101 Select(N.getOperand(0));
3102 SelectAddress(N.getOperand(2), AM);
3103 } else {
3104 SelectAddress(N.getOperand(2), AM);
3105 Select(N.getOperand(0));
3106 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003107 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3108 return;
3109 }
3110 }
Chris Lattner837caa72005-01-11 23:21:30 +00003111
3112 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003113 if (TryToFoldLoadOpStore(Node))
3114 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003115
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003116 switch (N.getOperand(1).getValueType()) {
3117 default: assert(0 && "Cannot store this type!");
3118 case MVT::i1:
3119 case MVT::i8: Opc = X86::MOV8mr; break;
3120 case MVT::i16: Opc = X86::MOV16mr; break;
3121 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003122 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003123 }
Chris Lattner11333092005-01-11 03:11:44 +00003124
3125 std::vector<std::pair<unsigned, unsigned> > RP;
3126 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3127 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3128 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3129 std::sort(RP.begin(), RP.end());
3130
Chris Lattner572dd082005-02-23 05:57:21 +00003131 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003132 for (unsigned i = 0; i != 3; ++i)
3133 switch (RP[2-i].second) {
3134 default: assert(0 && "Unknown operand number!");
3135 case 0: Select(N.getOperand(0)); break;
3136 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003137 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003138 }
3139
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003140 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3141 return;
3142 }
3143 case ISD::ADJCALLSTACKDOWN:
3144 case ISD::ADJCALLSTACKUP:
3145 Select(N.getOperand(0));
3146 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
3147
3148 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3149 X86::ADJCALLSTACKUP;
3150 BuildMI(BB, Opc, 1).addImm(Tmp1);
3151 return;
Chris Lattner989de032005-01-11 06:14:36 +00003152 case ISD::MEMSET: {
3153 Select(N.getOperand(0)); // Select the chain.
3154 unsigned Align =
3155 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3156 if (Align == 0) Align = 1;
3157
3158 // Turn the byte code into # iterations
3159 unsigned CountReg;
3160 unsigned Opcode;
3161 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3162 unsigned Val = ValC->getValue() & 255;
3163
3164 // If the value is a constant, then we can potentially use larger sets.
3165 switch (Align & 3) {
3166 case 2: // WORD aligned
3167 CountReg = MakeReg(MVT::i32);
3168 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3169 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3170 } else {
3171 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3172 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3173 }
3174 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3175 Opcode = X86::REP_STOSW;
3176 break;
3177 case 0: // DWORD aligned
3178 CountReg = MakeReg(MVT::i32);
3179 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3180 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3181 } else {
3182 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3183 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3184 }
3185 Val = (Val << 8) | Val;
3186 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3187 Opcode = X86::REP_STOSD;
3188 break;
3189 default: // BYTE aligned
3190 CountReg = SelectExpr(Node->getOperand(3));
3191 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3192 Opcode = X86::REP_STOSB;
3193 break;
3194 }
3195 } else {
3196 // If it's not a constant value we are storing, just fall back. We could
3197 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3198 unsigned ValReg = SelectExpr(Node->getOperand(2));
3199 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3200 CountReg = SelectExpr(Node->getOperand(3));
3201 Opcode = X86::REP_STOSB;
3202 }
3203
3204 // No matter what the alignment is, we put the source in ESI, the
3205 // destination in EDI, and the count in ECX.
3206 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3207 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3208 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3209 BuildMI(BB, Opcode, 0);
3210 return;
3211 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003212 case ISD::MEMCPY:
3213 Select(N.getOperand(0)); // Select the chain.
3214 unsigned Align =
3215 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3216 if (Align == 0) Align = 1;
3217
3218 // Turn the byte code into # iterations
3219 unsigned CountReg;
3220 unsigned Opcode;
3221 switch (Align & 3) {
3222 case 2: // WORD aligned
3223 CountReg = MakeReg(MVT::i32);
3224 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3225 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3226 } else {
3227 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3228 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3229 }
3230 Opcode = X86::REP_MOVSW;
3231 break;
3232 case 0: // DWORD aligned
3233 CountReg = MakeReg(MVT::i32);
3234 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3235 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3236 } else {
3237 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3238 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3239 }
3240 Opcode = X86::REP_MOVSD;
3241 break;
3242 default: // BYTE aligned
3243 CountReg = SelectExpr(Node->getOperand(3));
3244 Opcode = X86::REP_MOVSB;
3245 break;
3246 }
3247
3248 // No matter what the alignment is, we put the source in ESI, the
3249 // destination in EDI, and the count in ECX.
3250 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3251 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3252 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3253 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3254 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3255 BuildMI(BB, Opcode, 0);
3256 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003257 }
3258 assert(0 && "Should not be reached!");
3259}
3260
3261
3262/// createX86PatternInstructionSelector - This pass converts an LLVM function
3263/// into a machine code representation using pattern matching and a machine
3264/// description file.
3265///
3266FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
3267 return new ISel(TM);
3268}