blob: 63432defb851f96e01dc4a54365fc86ba2841e25 [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000030#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// X86TargetLowering - X86 Implementation of the TargetLowering interface
35namespace {
36 class X86TargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000038 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000039 public:
40 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000042
43 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
44 setShiftAmountType(MVT::i8);
45 setSetCCResultType(MVT::i8);
46
47 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000048 addRegisterClass(MVT::i8, X86::R8RegisterClass);
49 addRegisterClass(MVT::i16, X86::R16RegisterClass);
50 addRegisterClass(MVT::i32, X86::R32RegisterClass);
51 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
52
53 // FIXME: Eliminate these two classes when legalize can handle promotions
54 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000055/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
56/**/ //addRegisterClass(MVT::f32, X86::RFPRegisterClass);
57
58 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
59 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
60 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i16 , Expand);
61 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
62 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i1 , Expand);
63 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
64 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
65 setOperationAction(ISD::SREM , MVT::f64 , Expand);
66
67 // These should be promoted to a larger select which is supported.
68/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
69 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000070
71 computeRegisterProperties();
Chris Lattner8acb1ba2005-01-07 07:49:41 +000072
73 addLegalFPImmediate(+0.0); // FLD0
74 addLegalFPImmediate(+1.0); // FLD1
75 addLegalFPImmediate(-0.0); // FLD0/FCHS
76 addLegalFPImmediate(-1.0); // FLD1/FCHS
77 }
78
79 /// LowerArguments - This hook must be implemented to indicate how we should
80 /// lower the arguments for the specified function, into the specified DAG.
81 virtual std::vector<SDOperand>
82 LowerArguments(Function &F, SelectionDAG &DAG);
83
84 /// LowerCallTo - This hook lowers an abstract call to a function into an
85 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000086 virtual std::pair<SDOperand, SDOperand>
87 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
88 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000089
90 virtual std::pair<SDOperand, SDOperand>
91 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
92
93 virtual std::pair<SDOperand,SDOperand>
94 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
95 const Type *ArgTy, SelectionDAG &DAG);
96
97 virtual std::pair<SDOperand, SDOperand>
98 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
99 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000100 };
101}
102
103
104std::vector<SDOperand>
105X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
106 std::vector<SDOperand> ArgValues;
107
108 // Add DAG nodes to load the arguments... On entry to a function on the X86,
109 // the stack frame looks like this:
110 //
111 // [ESP] -- return address
112 // [ESP + 4] -- first argument (leftmost lexically)
113 // [ESP + 8] -- second argument, if first argument is four bytes in size
114 // ...
115 //
116 MachineFunction &MF = DAG.getMachineFunction();
117 MachineFrameInfo *MFI = MF.getFrameInfo();
118
119 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
120 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
121 MVT::ValueType ObjectVT = getValueType(I->getType());
122 unsigned ArgIncrement = 4;
123 unsigned ObjSize;
124 switch (ObjectVT) {
125 default: assert(0 && "Unhandled argument type!");
126 case MVT::i1:
127 case MVT::i8: ObjSize = 1; break;
128 case MVT::i16: ObjSize = 2; break;
129 case MVT::i32: ObjSize = 4; break;
130 case MVT::i64: ObjSize = ArgIncrement = 8; break;
131 case MVT::f32: ObjSize = 4; break;
132 case MVT::f64: ObjSize = ArgIncrement = 8; break;
133 }
134 // Create the frame index object for this incoming parameter...
135 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
136
137 // Create the SelectionDAG nodes corresponding to a load from this parameter
138 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
139
140 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
141 // dead loads.
142 SDOperand ArgValue;
143 if (!I->use_empty())
144 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
145 else {
146 if (MVT::isInteger(ObjectVT))
147 ArgValue = DAG.getConstant(0, ObjectVT);
148 else
149 ArgValue = DAG.getConstantFP(0, ObjectVT);
150 }
151 ArgValues.push_back(ArgValue);
152
153 ArgOffset += ArgIncrement; // Move on to the next argument...
154 }
155
156 // If the function takes variable number of arguments, make a frame index for
157 // the start of the first vararg value... for expansion of llvm.va_start.
158 if (F.isVarArg())
159 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000160 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000161 return ArgValues;
162}
163
Chris Lattner5188ad72005-01-08 19:28:19 +0000164std::pair<SDOperand, SDOperand>
165X86TargetLowering::LowerCallTo(SDOperand Chain,
166 const Type *RetTy, SDOperand Callee,
167 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000168 // Count how many bytes are to be pushed on the stack.
169 unsigned NumBytes = 0;
170
171 if (Args.empty()) {
172 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000173 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
174 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000175 } else {
176 for (unsigned i = 0, e = Args.size(); i != e; ++i)
177 switch (getValueType(Args[i].second)) {
178 default: assert(0 && "Unknown value type!");
179 case MVT::i1:
180 case MVT::i8:
181 case MVT::i16:
182 case MVT::i32:
183 case MVT::f32:
184 NumBytes += 4;
185 break;
186 case MVT::i64:
187 case MVT::f64:
188 NumBytes += 8;
189 break;
190 }
191
Chris Lattner5188ad72005-01-08 19:28:19 +0000192 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
193 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000194
195 // Arguments go on the stack in reverse order, as specified by the ABI.
196 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000197 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
198 DAG.getEntryNode());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000199 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
200 unsigned ArgReg;
201 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
202 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
203
204 switch (getValueType(Args[i].second)) {
205 default: assert(0 && "Unexpected ValueType for argument!");
206 case MVT::i1:
207 case MVT::i8:
208 case MVT::i16:
209 // Promote the integer to 32 bits. If the input type is signed use a
210 // sign extend, otherwise use a zero extend.
211 if (Args[i].second->isSigned())
212 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
213 else
214 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
215
216 // FALL THROUGH
217 case MVT::i32:
218 case MVT::f32:
219 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000220 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
221 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000222 ArgOffset += 4;
223 break;
224 case MVT::i64:
225 case MVT::f64:
226 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000227 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
228 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000229 ArgOffset += 8;
230 break;
231 }
232 }
233 }
234
235 std::vector<MVT::ValueType> RetVals;
236 MVT::ValueType RetTyVT = getValueType(RetTy);
237 if (RetTyVT != MVT::isVoid)
238 RetVals.push_back(RetTyVT);
239 RetVals.push_back(MVT::Other);
240
Chris Lattner5188ad72005-01-08 19:28:19 +0000241 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000242 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000243 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
244 DAG.getConstant(NumBytes, getPointerTy()));
245 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000246}
247
Chris Lattner14824582005-01-09 00:01:27 +0000248std::pair<SDOperand, SDOperand>
249X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
250 // vastart just returns the address of the VarArgsFrameIndex slot.
251 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
252}
253
254std::pair<SDOperand,SDOperand> X86TargetLowering::
255LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
256 const Type *ArgTy, SelectionDAG &DAG) {
257 MVT::ValueType ArgVT = getValueType(ArgTy);
258 SDOperand Result;
259 if (!isVANext) {
260 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
261 } else {
262 unsigned Amt;
263 if (ArgVT == MVT::i32)
264 Amt = 4;
265 else {
266 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
267 "Other types should have been promoted for varargs!");
268 Amt = 8;
269 }
270 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
271 DAG.getConstant(Amt, VAList.getValueType()));
272 }
273 return std::make_pair(Result, Chain);
274}
275
276
277std::pair<SDOperand, SDOperand> X86TargetLowering::
278LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
279 SelectionDAG &DAG) {
280 SDOperand Result;
281 if (Depth) // Depths > 0 not supported yet!
282 Result = DAG.getConstant(0, getPointerTy());
283 else {
284 if (ReturnAddrIndex == 0) {
285 // Set up a frame object for the return address.
286 MachineFunction &MF = DAG.getMachineFunction();
287 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
288 }
289
290 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
291
292 if (!isFrameAddress)
293 // Just load the return address
294 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
295 else
296 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
297 DAG.getConstant(4, MVT::i32));
298 }
299 return std::make_pair(Result, Chain);
300}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000301
302
Chris Lattner98a8ba02005-01-18 01:06:26 +0000303namespace {
304 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
305 /// SDOperand's instead of register numbers for the leaves of the matched
306 /// tree.
307 struct X86ISelAddressMode {
308 enum {
309 RegBase,
310 FrameIndexBase,
311 } BaseType;
312
313 struct { // This is really a union, discriminated by BaseType!
314 SDOperand Reg;
315 int FrameIndex;
316 } Base;
317
318 unsigned Scale;
319 SDOperand IndexReg;
320 unsigned Disp;
321 GlobalValue *GV;
322
323 X86ISelAddressMode()
324 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
325 }
326 };
327}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000328
329
330namespace {
331 Statistic<>
332 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
333
334 //===--------------------------------------------------------------------===//
335 /// ISel - X86 specific code to select X86 machine instructions for
336 /// SelectionDAG operations.
337 ///
338 class ISel : public SelectionDAGISel {
339 /// ContainsFPCode - Every instruction we select that uses or defines a FP
340 /// register should set this to true.
341 bool ContainsFPCode;
342
343 /// X86Lowering - This object fully describes how to lower LLVM code to an
344 /// X86-specific SelectionDAG.
345 X86TargetLowering X86Lowering;
346
Chris Lattner11333092005-01-11 03:11:44 +0000347 /// RegPressureMap - This keeps an approximate count of the number of
348 /// registers required to evaluate each node in the graph.
349 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000350
351 /// ExprMap - As shared expressions are codegen'd, we keep track of which
352 /// vreg the value is produced in, so we only emit one copy of each compiled
353 /// tree.
354 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000355
356 public:
357 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
358 }
359
Chris Lattner11333092005-01-11 03:11:44 +0000360 unsigned getRegPressure(SDOperand O) {
361 return RegPressureMap[O.Val];
362 }
363 unsigned ComputeRegPressure(SDOperand O);
364
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000365 /// InstructionSelectBasicBlock - This callback is invoked by
366 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000367 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000368
Chris Lattner4ff348b2005-01-17 06:26:58 +0000369 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp);
Chris Lattnera5ade062005-01-11 21:19:59 +0000370 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000371 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000372
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000373 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000374 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000375 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
376 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000377 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000378
379 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
380 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
381 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000382 void Select(SDOperand N);
383 };
384}
385
Chris Lattner7dbcb752005-01-12 04:21:28 +0000386/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
387/// when it has created a SelectionDAG for us to codegen.
388void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
389 // While we're doing this, keep track of whether we see any FP code for
390 // FP_REG_KILL insertion.
391 ContainsFPCode = false;
392
393 // Scan the PHI nodes that already are inserted into this basic block. If any
394 // of them is a PHI of a floating point value, we need to insert an
395 // FP_REG_KILL.
396 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
397 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
398 I != E; ++I) {
399 assert(I->getOpcode() == X86::PHI &&
400 "Isn't just PHI nodes?");
401 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
402 X86::RFPRegisterClass) {
403 ContainsFPCode = true;
404 break;
405 }
406 }
407
408 // Compute the RegPressureMap, which is an approximation for the number of
409 // registers required to compute each node.
410 ComputeRegPressure(DAG.getRoot());
411
412 // Codegen the basic block.
413 Select(DAG.getRoot());
414
415 // Finally, look at all of the successors of this block. If any contain a PHI
416 // node of FP type, we need to insert an FP_REG_KILL in this block.
417 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
418 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
419 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
420 I != E && I->getOpcode() == X86::PHI; ++I) {
421 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
422 X86::RFPRegisterClass) {
423 ContainsFPCode = true;
424 break;
425 }
426 }
427
428 // Insert FP_REG_KILL instructions into basic blocks that need them. This
429 // only occurs due to the floating point stackifier not being aggressive
430 // enough to handle arbitrary global stackification.
431 //
432 // Currently we insert an FP_REG_KILL instruction into each block that uses or
433 // defines a floating point virtual register.
434 //
435 // When the global register allocators (like linear scan) finally update live
436 // variable analysis, we can keep floating point values in registers across
437 // basic blocks. This will be a huge win, but we are waiting on the global
438 // allocators before we can do this.
439 //
440 if (ContainsFPCode && BB->succ_size()) {
441 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
442 ++NumFPKill;
443 }
444
445 // Clear state used for selection.
446 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000447 RegPressureMap.clear();
448}
449
450
Chris Lattner11333092005-01-11 03:11:44 +0000451// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
452// for the number of registers required to compute each node. This is basically
453// computing a generalized form of the Sethi-Ullman number for each node.
454unsigned ISel::ComputeRegPressure(SDOperand O) {
455 SDNode *N = O.Val;
456 unsigned &Result = RegPressureMap[N];
457 if (Result) return Result;
458
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000459 // FIXME: Should operations like CALL (which clobber lots o regs) have a
460 // higher fixed cost??
461
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000462 if (N->getNumOperands() == 0) {
463 Result = 1;
464 } else {
465 unsigned MaxRegUse = 0;
466 unsigned NumExtraMaxRegUsers = 0;
467 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
468 unsigned Regs;
469 if (N->getOperand(i).getOpcode() == ISD::Constant)
470 Regs = 0;
471 else
472 Regs = ComputeRegPressure(N->getOperand(i));
473 if (Regs > MaxRegUse) {
474 MaxRegUse = Regs;
475 NumExtraMaxRegUsers = 0;
476 } else if (Regs == MaxRegUse &&
477 N->getOperand(i).getValueType() != MVT::Other) {
478 ++NumExtraMaxRegUsers;
479 }
Chris Lattner11333092005-01-11 03:11:44 +0000480 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000481
482 if (O.getOpcode() != ISD::TokenFactor)
483 Result = MaxRegUse+NumExtraMaxRegUsers;
484 else
Chris Lattner869e0432005-01-17 23:02:13 +0000485 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000486 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000487
Chris Lattner837caa72005-01-11 23:21:30 +0000488 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000489 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000490}
491
Chris Lattner98a8ba02005-01-18 01:06:26 +0000492X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
493 X86AddressMode Result;
494
495 // If we need to emit two register operands, emit the one with the highest
496 // register pressure first.
497 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
498 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
499 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
500 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
501 Result.IndexReg = SelectExpr(IAM.IndexReg);
502 } else {
503 Result.IndexReg = SelectExpr(IAM.IndexReg);
504 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
505 }
506 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
507 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
508 } else if (IAM.IndexReg.Val) {
509 Result.IndexReg = SelectExpr(IAM.IndexReg);
510 }
511
512 switch (IAM.BaseType) {
513 case X86ISelAddressMode::RegBase:
514 Result.BaseType = X86AddressMode::RegBase;
515 break;
516 case X86ISelAddressMode::FrameIndexBase:
517 Result.BaseType = X86AddressMode::FrameIndexBase;
518 Result.Base.FrameIndex = IAM.Base.FrameIndex;
519 break;
520 default:
521 assert(0 && "Unknown base type!");
522 break;
523 }
524 Result.Scale = IAM.Scale;
525 Result.Disp = IAM.Disp;
526 Result.GV = IAM.GV;
527 return Result;
528}
529
530/// SelectAddress - Pattern match the maximal addressing mode for this node and
531/// emit all of the leaf registers.
532void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
533 X86ISelAddressMode IAM;
534 MatchAddress(N, IAM);
535 AM = SelectAddrExprs(IAM);
536}
537
538/// MatchAddress - Add the specified node to the specified addressing mode,
539/// returning true if it cannot be done. This just pattern matches for the
540/// addressing mode, it does not cause any code to be emitted. For that, use
541/// SelectAddress.
542bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000543 switch (N.getOpcode()) {
544 default: break;
545 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000546 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
547 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000548 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
549 return false;
550 }
551 break;
552 case ISD::GlobalAddress:
553 if (AM.GV == 0) {
554 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
555 return false;
556 }
557 break;
558 case ISD::Constant:
559 AM.Disp += cast<ConstantSDNode>(N)->getValue();
560 return false;
561 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000562 // We might have folded the load into this shift, so don't regen the value
563 // if so.
564 if (ExprMap.count(N)) break;
565
Chris Lattner98a8ba02005-01-18 01:06:26 +0000566 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000567 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
568 unsigned Val = CN->getValue();
569 if (Val == 1 || Val == 2 || Val == 3) {
570 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000571 SDOperand ShVal = N.Val->getOperand(0);
572
573 // Okay, we know that we have a scale by now. However, if the scaled
574 // value is an add of something and a constant, we can fold the
575 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000576 if (ShVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(ShVal) &&
Chris Lattner51a26342005-01-11 06:36:20 +0000577 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000578 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000579 ConstantSDNode *AddVal =
580 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
581 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000582 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000583 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000584 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000585 return false;
586 }
587 }
588 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000589 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000590 // We might have folded the load into this mul, so don't regen the value if
591 // so.
592 if (ExprMap.count(N)) break;
593
Chris Lattner947d5442005-01-11 19:37:02 +0000594 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000595 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
596 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000597 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
598 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
599 AM.Scale = unsigned(CN->getValue())-1;
600
601 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000602 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000603
604 // Okay, we know that we have a scale by now. However, if the scaled
605 // value is an add of something and a constant, we can fold the
606 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000607 if (MulVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(MulVal) &&
Chris Lattner947d5442005-01-11 19:37:02 +0000608 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000609 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000610 ConstantSDNode *AddVal =
611 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
612 AM.Disp += AddVal->getValue() * CN->getValue();
613 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000614 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000615 }
616
617 AM.IndexReg = AM.Base.Reg = Reg;
618 return false;
619 }
620 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000621
622 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000623 // We might have folded the load into this mul, so don't regen the value if
624 // so.
625 if (ExprMap.count(N)) break;
626
Chris Lattner98a8ba02005-01-18 01:06:26 +0000627 X86ISelAddressMode Backup = AM;
628 if (!MatchAddress(N.Val->getOperand(0), AM) &&
629 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000630 return false;
631 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000632 if (!MatchAddress(N.Val->getOperand(1), AM) &&
633 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000634 return false;
635 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000636 break;
637 }
638 }
639
Chris Lattnera95589b2005-01-11 04:40:19 +0000640 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000641 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000642 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000643 if (AM.IndexReg.Val == 0) {
644 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000645 AM.Scale = 1;
646 return false;
647 }
648
649 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000650 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000651 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000652
653 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000654 AM.BaseType = X86ISelAddressMode::RegBase;
655 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000656 return false;
657}
658
659/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
660/// assuming that the temporary registers are in the 8-bit register class.
661///
662/// Tmp1 = setcc1
663/// Tmp2 = setcc2
664/// DestReg = logicalop Tmp1, Tmp2
665///
666static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
667 unsigned SetCC2, unsigned LogicalOp,
668 unsigned DestReg) {
669 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
670 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
671 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
672 BuildMI(BB, SetCC1, 0, Tmp1);
673 BuildMI(BB, SetCC2, 0, Tmp2);
674 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
675}
676
677/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
678/// condition codes match the specified SetCCOpcode. Note that some conditions
679/// require multiple instructions to generate the correct value.
680static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
681 ISD::CondCode SetCCOpcode, bool isFP) {
682 unsigned Opc;
683 if (!isFP) {
684 switch (SetCCOpcode) {
685 default: assert(0 && "Illegal integer SetCC!");
686 case ISD::SETEQ: Opc = X86::SETEr; break;
687 case ISD::SETGT: Opc = X86::SETGr; break;
688 case ISD::SETGE: Opc = X86::SETGEr; break;
689 case ISD::SETLT: Opc = X86::SETLr; break;
690 case ISD::SETLE: Opc = X86::SETLEr; break;
691 case ISD::SETNE: Opc = X86::SETNEr; break;
692 case ISD::SETULT: Opc = X86::SETBr; break;
693 case ISD::SETUGT: Opc = X86::SETAr; break;
694 case ISD::SETULE: Opc = X86::SETBEr; break;
695 case ISD::SETUGE: Opc = X86::SETAEr; break;
696 }
697 } else {
698 // On a floating point condition, the flags are set as follows:
699 // ZF PF CF op
700 // 0 | 0 | 0 | X > Y
701 // 0 | 0 | 1 | X < Y
702 // 1 | 0 | 0 | X == Y
703 // 1 | 1 | 1 | unordered
704 //
705 switch (SetCCOpcode) {
706 default: assert(0 && "Invalid FP setcc!");
707 case ISD::SETUEQ:
708 case ISD::SETEQ:
709 Opc = X86::SETEr; // True if ZF = 1
710 break;
711 case ISD::SETOGT:
712 case ISD::SETGT:
713 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
714 break;
715 case ISD::SETOGE:
716 case ISD::SETGE:
717 Opc = X86::SETAEr; // True if CF = 0
718 break;
719 case ISD::SETULT:
720 case ISD::SETLT:
721 Opc = X86::SETBr; // True if CF = 1
722 break;
723 case ISD::SETULE:
724 case ISD::SETLE:
725 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
726 break;
727 case ISD::SETONE:
728 case ISD::SETNE:
729 Opc = X86::SETNEr; // True if ZF = 0
730 break;
731 case ISD::SETUO:
732 Opc = X86::SETPr; // True if PF = 1
733 break;
734 case ISD::SETO:
735 Opc = X86::SETNPr; // True if PF = 0
736 break;
737 case ISD::SETOEQ: // !PF & ZF
738 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
739 return;
740 case ISD::SETOLT: // !PF & CF
741 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
742 return;
743 case ISD::SETOLE: // !PF & (CF || ZF)
744 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
745 return;
746 case ISD::SETUGT: // PF | (!ZF & !CF)
747 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
748 return;
749 case ISD::SETUGE: // PF | !CF
750 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
751 return;
752 case ISD::SETUNE: // PF | !ZF
753 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
754 return;
755 }
756 }
757 BuildMI(BB, Opc, 0, DestReg);
758}
759
760
761/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
762/// the Dest block if the Cond condition is true. If we cannot fold this
763/// condition into the branch, return true.
764///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000765bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
766 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000767 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
768 // B) using two conditional branches instead of one condbr, two setcc's, and
769 // an or.
770 if ((Cond.getOpcode() == ISD::OR ||
771 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
772 // And and or set the flags for us, so there is no need to emit a TST of the
773 // result. It is only safe to do this if there is only a single use of the
774 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000775 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000776 SelectExpr(Cond);
777 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
778 return false;
779 }
780
781 // Codegen br not C -> JE.
782 if (Cond.getOpcode() == ISD::XOR)
783 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
784 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000785 unsigned CondR;
786 if (getRegPressure(Chain) > getRegPressure(Cond)) {
787 Select(Chain);
788 CondR = SelectExpr(Cond.Val->getOperand(0));
789 } else {
790 CondR = SelectExpr(Cond.Val->getOperand(0));
791 Select(Chain);
792 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000793 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
794 BuildMI(BB, X86::JE, 1).addMBB(Dest);
795 return false;
796 }
797
798 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
799 if (SetCC == 0)
800 return true; // Can only handle simple setcc's so far.
801
802 unsigned Opc;
803
804 // Handle integer conditions first.
805 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
806 switch (SetCC->getCondition()) {
807 default: assert(0 && "Illegal integer SetCC!");
808 case ISD::SETEQ: Opc = X86::JE; break;
809 case ISD::SETGT: Opc = X86::JG; break;
810 case ISD::SETGE: Opc = X86::JGE; break;
811 case ISD::SETLT: Opc = X86::JL; break;
812 case ISD::SETLE: Opc = X86::JLE; break;
813 case ISD::SETNE: Opc = X86::JNE; break;
814 case ISD::SETULT: Opc = X86::JB; break;
815 case ISD::SETUGT: Opc = X86::JA; break;
816 case ISD::SETULE: Opc = X86::JBE; break;
817 case ISD::SETUGE: Opc = X86::JAE; break;
818 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000819 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000820 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000821 BuildMI(BB, Opc, 1).addMBB(Dest);
822 return false;
823 }
824
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000825 unsigned Opc2 = 0; // Second branch if needed.
826
827 // On a floating point condition, the flags are set as follows:
828 // ZF PF CF op
829 // 0 | 0 | 0 | X > Y
830 // 0 | 0 | 1 | X < Y
831 // 1 | 0 | 0 | X == Y
832 // 1 | 1 | 1 | unordered
833 //
834 switch (SetCC->getCondition()) {
835 default: assert(0 && "Invalid FP setcc!");
836 case ISD::SETUEQ:
837 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
838 case ISD::SETOGT:
839 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
840 case ISD::SETOGE:
841 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
842 case ISD::SETULT:
843 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
844 case ISD::SETULE:
845 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
846 case ISD::SETONE:
847 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
848 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
849 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
850 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
851 Opc = X86::JA; // ZF = 0 & CF = 0
852 Opc2 = X86::JP; // PF = 1
853 break;
854 case ISD::SETUGE: // PF = 1 | CF = 0
855 Opc = X86::JAE; // CF = 0
856 Opc2 = X86::JP; // PF = 1
857 break;
858 case ISD::SETUNE: // PF = 1 | ZF = 0
859 Opc = X86::JNE; // ZF = 0
860 Opc2 = X86::JP; // PF = 1
861 break;
862 case ISD::SETOEQ: // PF = 0 & ZF = 1
863 //X86::JNP, X86::JE
864 //X86::AND8rr
865 return true; // FIXME: Emit more efficient code for this branch.
866 case ISD::SETOLT: // PF = 0 & CF = 1
867 //X86::JNP, X86::JB
868 //X86::AND8rr
869 return true; // FIXME: Emit more efficient code for this branch.
870 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
871 //X86::JNP, X86::JBE
872 //X86::AND8rr
873 return true; // FIXME: Emit more efficient code for this branch.
874 }
875
Chris Lattner6c07aee2005-01-11 04:06:27 +0000876 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000877 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000878 BuildMI(BB, Opc, 1).addMBB(Dest);
879 if (Opc2)
880 BuildMI(BB, Opc2, 1).addMBB(Dest);
881 return false;
882}
883
Chris Lattner24aad1b2005-01-10 22:10:13 +0000884/// EmitSelectCC - Emit code into BB that performs a select operation between
885/// the two registers RTrue and RFalse, generating a result into RDest. Return
886/// true if the fold cannot be performed.
887///
888void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
889 unsigned RTrue, unsigned RFalse, unsigned RDest) {
890 enum Condition {
891 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
892 NOT_SET
893 } CondCode = NOT_SET;
894
895 static const unsigned CMOVTAB16[] = {
896 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
897 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
898 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
899 };
900 static const unsigned CMOVTAB32[] = {
901 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
902 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
903 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
904 };
905 static const unsigned CMOVTABFP[] = {
906 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
907 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
908 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
909 };
910
911 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
912 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
913 switch (SetCC->getCondition()) {
914 default: assert(0 && "Unknown integer comparison!");
915 case ISD::SETEQ: CondCode = EQ; break;
916 case ISD::SETGT: CondCode = GT; break;
917 case ISD::SETGE: CondCode = GE; break;
918 case ISD::SETLT: CondCode = LT; break;
919 case ISD::SETLE: CondCode = LE; break;
920 case ISD::SETNE: CondCode = NE; break;
921 case ISD::SETULT: CondCode = B; break;
922 case ISD::SETUGT: CondCode = A; break;
923 case ISD::SETULE: CondCode = BE; break;
924 case ISD::SETUGE: CondCode = AE; break;
925 }
926 } else {
927 // On a floating point condition, the flags are set as follows:
928 // ZF PF CF op
929 // 0 | 0 | 0 | X > Y
930 // 0 | 0 | 1 | X < Y
931 // 1 | 0 | 0 | X == Y
932 // 1 | 1 | 1 | unordered
933 //
934 switch (SetCC->getCondition()) {
935 default: assert(0 && "Unknown FP comparison!");
936 case ISD::SETUEQ:
937 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
938 case ISD::SETOGT:
939 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
940 case ISD::SETOGE:
941 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
942 case ISD::SETULT:
943 case ISD::SETLT: CondCode = B; break; // True if CF = 1
944 case ISD::SETULE:
945 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
946 case ISD::SETONE:
947 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
948 case ISD::SETUO: CondCode = P; break; // True if PF = 1
949 case ISD::SETO: CondCode = NP; break; // True if PF = 0
950 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
951 case ISD::SETUGE: // PF = 1 | CF = 0
952 case ISD::SETUNE: // PF = 1 | ZF = 0
953 case ISD::SETOEQ: // PF = 0 & ZF = 1
954 case ISD::SETOLT: // PF = 0 & CF = 1
955 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
956 // We cannot emit this comparison as a single cmov.
957 break;
958 }
959 }
960 }
961
962 unsigned Opc = 0;
963 if (CondCode != NOT_SET) {
964 switch (SVT) {
965 default: assert(0 && "Cannot select this type!");
966 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
967 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
968 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000969 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000970 }
971 }
972
973 // Finally, if we weren't able to fold this, just emit the condition and test
974 // it.
975 if (CondCode == NOT_SET || Opc == 0) {
976 // Get the condition into the zero flag.
977 unsigned CondReg = SelectExpr(Cond);
978 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
979
980 switch (SVT) {
981 default: assert(0 && "Cannot select this type!");
982 case MVT::i16: Opc = X86::CMOVE16rr; break;
983 case MVT::i32: Opc = X86::CMOVE32rr; break;
984 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000985 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000986 }
987 } else {
988 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000989 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000990 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000991 }
992 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
993}
994
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000995void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +0000996 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000997 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
998 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +0000999 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001000 switch (RHS.getValueType()) {
1001 default: break;
1002 case MVT::i1:
1003 case MVT::i8: Opc = X86::CMP8mi; break;
1004 case MVT::i16: Opc = X86::CMP16mi; break;
1005 case MVT::i32: Opc = X86::CMP32mi; break;
1006 }
1007 if (Opc) {
1008 X86AddressMode AM;
1009 EmitFoldedLoad(LHS, AM);
1010 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1011 return;
1012 }
1013 }
1014
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001015 switch (RHS.getValueType()) {
1016 default: break;
1017 case MVT::i1:
1018 case MVT::i8: Opc = X86::CMP8ri; break;
1019 case MVT::i16: Opc = X86::CMP16ri; break;
1020 case MVT::i32: Opc = X86::CMP32ri; break;
1021 }
1022 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001023 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001024 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1025 return;
1026 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001027 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1028 if (CN->isExactlyValue(+0.0) ||
1029 CN->isExactlyValue(-0.0)) {
1030 unsigned Reg = SelectExpr(LHS);
1031 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1032 BuildMI(BB, X86::FNSTSW8r, 0);
1033 BuildMI(BB, X86::SAHF, 1);
1034 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001035 }
1036
Chris Lattneref6806c2005-01-12 02:02:48 +00001037 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001038 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001039 switch (RHS.getValueType()) {
1040 default: break;
1041 case MVT::i1:
1042 case MVT::i8: Opc = X86::CMP8mr; break;
1043 case MVT::i16: Opc = X86::CMP16mr; break;
1044 case MVT::i32: Opc = X86::CMP32mr; break;
1045 }
1046 if (Opc) {
1047 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001048 EmitFoldedLoad(LHS, AM);
1049 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001050 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1051 return;
1052 }
1053 }
1054
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001055 switch (LHS.getValueType()) {
1056 default: assert(0 && "Cannot compare this value!");
1057 case MVT::i1:
1058 case MVT::i8: Opc = X86::CMP8rr; break;
1059 case MVT::i16: Opc = X86::CMP16rr; break;
1060 case MVT::i32: Opc = X86::CMP32rr; break;
1061 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001062 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001063 }
Chris Lattner11333092005-01-11 03:11:44 +00001064 unsigned Tmp1, Tmp2;
1065 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1066 Tmp1 = SelectExpr(LHS);
1067 Tmp2 = SelectExpr(RHS);
1068 } else {
1069 Tmp2 = SelectExpr(RHS);
1070 Tmp1 = SelectExpr(LHS);
1071 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001072 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1073}
1074
Chris Lattner4ff348b2005-01-17 06:26:58 +00001075/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1076/// The DAG cannot have cycles in it, by definition, so the visited set is not
1077/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1078/// reuse, so it prevents exponential cases.
1079///
1080static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1081 std::set<SDNode*> &Visited) {
1082 if (N == Op) return true; // Found it.
1083 SDNode *Node = N.Val;
1084 if (Node->getNumOperands() == 0) return false; // Leaf?
1085 if (!Visited.insert(Node).second) return false; // Already visited?
1086
1087 // Recurse for the first N-1 operands.
1088 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1089 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1090 return true;
1091
1092 // Tail recurse for the last operand.
1093 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1094}
1095
Chris Lattnera5ade062005-01-11 21:19:59 +00001096/// isFoldableLoad - Return true if this is a load instruction that can safely
1097/// be folded into an operation that uses it.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001098bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001099 if (Op.getOpcode() != ISD::LOAD ||
1100 // FIXME: currently can't fold constant pool indexes.
1101 isa<ConstantPoolSDNode>(Op.getOperand(1)))
1102 return false;
1103
1104 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001105 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1106 if (ExprMap.count(Op.getValue(1))) return false;
1107 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001108 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001109
Chris Lattner4ff348b2005-01-17 06:26:58 +00001110 // If there is not just one use of its value, we cannot fold.
1111 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1112
1113 // Finally, we cannot fold the load into the operation if this would induce a
1114 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1115 // operand of the operation we are folding the load into) can possible use the
1116 // chain node defined by the load.
1117 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1118 std::set<SDNode*> Visited;
1119 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1120 return false;
1121 }
1122 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001123}
1124
Chris Lattner4ff348b2005-01-17 06:26:58 +00001125
Chris Lattnera5ade062005-01-11 21:19:59 +00001126/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1127/// and compute the address being loaded into AM.
1128void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1129 SDOperand Chain = Op.getOperand(0);
1130 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001131
Chris Lattnera5ade062005-01-11 21:19:59 +00001132 if (getRegPressure(Chain) > getRegPressure(Address)) {
1133 Select(Chain);
1134 SelectAddress(Address, AM);
1135 } else {
1136 SelectAddress(Address, AM);
1137 Select(Chain);
1138 }
1139
1140 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001141 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1142 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001143 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001144 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001145}
1146
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001147unsigned ISel::SelectExpr(SDOperand N) {
1148 unsigned Result;
1149 unsigned Tmp1, Tmp2, Tmp3;
1150 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001151 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001152 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001153
Chris Lattner7f2afac2005-01-14 22:37:41 +00001154 if (Node->getOpcode() == ISD::CopyFromReg) {
1155 // FIXME: Handle copy from physregs!
1156
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001157 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001158 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001159 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001160
1161 unsigned &Reg = ExprMap[N];
1162 if (Reg) return Reg;
1163
1164 if (N.getOpcode() != ISD::CALL)
1165 Reg = Result = (N.getValueType() != MVT::Other) ?
1166 MakeReg(N.getValueType()) : 1;
1167 else {
1168 // If this is a call instruction, make sure to prepare ALL of the result
1169 // values as well as the chain.
1170 if (Node->getNumValues() == 1)
1171 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001172 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001173 Result = MakeReg(Node->getValueType(0));
1174 ExprMap[N.getValue(0)] = Result;
1175 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1176 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1177 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001178 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001179 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001180
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001181 switch (N.getOpcode()) {
1182 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001183 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001184 assert(0 && "Node not handled!\n");
1185 case ISD::FrameIndex:
1186 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1187 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1188 return Result;
1189 case ISD::ConstantPool:
1190 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1191 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1192 return Result;
1193 case ISD::ConstantFP:
1194 ContainsFPCode = true;
1195 Tmp1 = Result; // Intermediate Register
1196 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1197 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1198 Tmp1 = MakeReg(MVT::f64);
1199
1200 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1201 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1202 BuildMI(BB, X86::FLD0, 0, Tmp1);
1203 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1204 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1205 BuildMI(BB, X86::FLD1, 0, Tmp1);
1206 else
1207 assert(0 && "Unexpected constant!");
1208 if (Tmp1 != Result)
1209 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1210 return Result;
1211 case ISD::Constant:
1212 switch (N.getValueType()) {
1213 default: assert(0 && "Cannot use constants of this type!");
1214 case MVT::i1:
1215 case MVT::i8: Opc = X86::MOV8ri; break;
1216 case MVT::i16: Opc = X86::MOV16ri; break;
1217 case MVT::i32: Opc = X86::MOV32ri; break;
1218 }
1219 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1220 return Result;
1221 case ISD::GlobalAddress: {
1222 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1223 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1224 return Result;
1225 }
1226 case ISD::ExternalSymbol: {
1227 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1228 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1229 return Result;
1230 }
1231 case ISD::FP_EXTEND:
1232 Tmp1 = SelectExpr(N.getOperand(0));
1233 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001234 return Result;
1235 case ISD::ZERO_EXTEND: {
1236 int DestIs16 = N.getValueType() == MVT::i16;
1237 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001238
1239 // FIXME: This hack is here for zero extension casts from bool to i8. This
1240 // would not be needed if bools were promoted by Legalize.
1241 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001242 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001243 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1244 return Result;
1245 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001246
Chris Lattner4ff348b2005-01-17 06:26:58 +00001247 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001248 static const unsigned Opc[3] = {
1249 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1250 };
1251
1252 X86AddressMode AM;
1253 EmitFoldedLoad(N.getOperand(0), AM);
1254 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1255
1256 return Result;
1257 }
1258
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001259 static const unsigned Opc[3] = {
1260 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1261 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001262 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001263 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1264 return Result;
1265 }
1266 case ISD::SIGN_EXTEND: {
1267 int DestIs16 = N.getValueType() == MVT::i16;
1268 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1269
Chris Lattner590d8002005-01-09 18:52:44 +00001270 // FIXME: Legalize should promote bools to i8!
1271 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1272 "Sign extend from bool not implemented!");
1273
Chris Lattner4ff348b2005-01-17 06:26:58 +00001274 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001275 static const unsigned Opc[3] = {
1276 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1277 };
1278
1279 X86AddressMode AM;
1280 EmitFoldedLoad(N.getOperand(0), AM);
1281 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1282 return Result;
1283 }
1284
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001285 static const unsigned Opc[3] = {
1286 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1287 };
1288 Tmp1 = SelectExpr(N.getOperand(0));
1289 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1290 return Result;
1291 }
1292 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001293 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001294 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001295 switch (N.getValueType()) {
1296 default: assert(0 && "Unknown truncate!");
1297 case MVT::i1:
1298 case MVT::i8: Opc = X86::MOV8rm; break;
1299 case MVT::i16: Opc = X86::MOV16rm; break;
1300 }
1301 X86AddressMode AM;
1302 EmitFoldedLoad(N.getOperand(0), AM);
1303 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1304 return Result;
1305 }
1306
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001307 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1308 // a move out of AX or AL.
1309 switch (N.getOperand(0).getValueType()) {
1310 default: assert(0 && "Unknown truncate!");
1311 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1312 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1313 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1314 }
1315 Tmp1 = SelectExpr(N.getOperand(0));
1316 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1317
1318 switch (N.getValueType()) {
1319 default: assert(0 && "Unknown truncate!");
1320 case MVT::i1:
1321 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1322 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1323 }
1324 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1325 return Result;
1326
1327 case ISD::FP_ROUND:
1328 // Truncate from double to float by storing to memory as float,
1329 // then reading it back into a register.
1330
1331 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001332 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001333 Tmp1 = TLI.getTargetData().getFloatAlignment();
1334 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1335
1336 // Codegen the input.
1337 Tmp1 = SelectExpr(N.getOperand(0));
1338
1339 // Emit the store, then the reload.
1340 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1341 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001342 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001343
1344 case ISD::SINT_TO_FP:
1345 case ISD::UINT_TO_FP: {
1346 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001347 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001348
1349 // Promote the integer to a type supported by FLD. We do this because there
1350 // are no unsigned FLD instructions, so we must promote an unsigned value to
1351 // a larger signed value, then use FLD on the larger value.
1352 //
1353 MVT::ValueType PromoteType = MVT::Other;
1354 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1355 unsigned PromoteOpcode = 0;
1356 unsigned RealDestReg = Result;
1357 switch (SrcTy) {
1358 case MVT::i1:
1359 case MVT::i8:
1360 // We don't have the facilities for directly loading byte sized data from
1361 // memory (even signed). Promote it to 16 bits.
1362 PromoteType = MVT::i16;
1363 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1364 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1365 break;
1366 case MVT::i16:
1367 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1368 PromoteType = MVT::i32;
1369 PromoteOpcode = X86::MOVZX32rr16;
1370 }
1371 break;
1372 default:
1373 // Don't fild into the real destination.
1374 if (Node->getOpcode() == ISD::UINT_TO_FP)
1375 Result = MakeReg(Node->getValueType(0));
1376 break;
1377 }
1378
1379 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1380
1381 if (PromoteType != MVT::Other) {
1382 Tmp2 = MakeReg(PromoteType);
1383 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1384 SrcTy = PromoteType;
1385 Tmp1 = Tmp2;
1386 }
1387
1388 // Spill the integer to memory and reload it from there.
1389 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1390 MachineFunction *F = BB->getParent();
1391 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1392
1393 switch (SrcTy) {
1394 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001395 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001396 // FIXME: this won't work for cast [u]long to FP
1397 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1398 FrameIdx).addReg(Tmp1);
1399 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1400 FrameIdx, 4).addReg(Tmp1+1);
1401 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1402 break;
1403 case MVT::i32:
1404 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1405 FrameIdx).addReg(Tmp1);
1406 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1407 break;
1408 case MVT::i16:
1409 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1410 FrameIdx).addReg(Tmp1);
1411 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1412 break;
1413 default: break; // No promotion required.
1414 }
1415
Chris Lattner085c9952005-01-12 04:00:00 +00001416 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001417 // If this is a cast from uint -> double, we need to be careful when if
1418 // the "sign" bit is set. If so, we don't want to make a negative number,
1419 // we want to make a positive number. Emit code to add an offset if the
1420 // sign bit is set.
1421
1422 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1423 unsigned IsNeg = MakeReg(MVT::i32);
1424 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1425
1426 // Create a CP value that has the offset in one word and 0 in the other.
1427 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1428 0x4f80000000000000ULL);
1429 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1430 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1431 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1432
1433 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1434 // We need special handling for unsigned 64-bit integer sources. If the
1435 // input number has the "sign bit" set, then we loaded it incorrectly as a
1436 // negative 64-bit number. In this case, add an offset value.
1437
1438 // Emit a test instruction to see if the dynamic input value was signed.
1439 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1440
1441 // If the sign bit is set, get a pointer to an offset, otherwise get a
1442 // pointer to a zero.
1443 MachineConstantPool *CP = F->getConstantPool();
1444 unsigned Zero = MakeReg(MVT::i32);
1445 Constant *Null = Constant::getNullValue(Type::UIntTy);
1446 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1447 CP->getConstantPoolIndex(Null));
1448 unsigned Offset = MakeReg(MVT::i32);
1449 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1450
1451 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1452 CP->getConstantPoolIndex(OffsetCst));
1453 unsigned Addr = MakeReg(MVT::i32);
1454 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1455
1456 // Load the constant for an add. FIXME: this could make an 'fadd' that
1457 // reads directly from memory, but we don't support these yet.
1458 unsigned ConstReg = MakeReg(MVT::f64);
1459 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1460
1461 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1462 }
1463 return RealDestReg;
1464 }
1465 case ISD::FP_TO_SINT:
1466 case ISD::FP_TO_UINT: {
1467 // FIXME: Most of this grunt work should be done by legalize!
1468 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1469
1470 // Change the floating point control register to use "round towards zero"
1471 // mode when truncating to an integer value.
1472 //
1473 MachineFunction *F = BB->getParent();
1474 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1475 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1476
1477 // Load the old value of the high byte of the control word...
1478 unsigned HighPartOfCW = MakeReg(MVT::i8);
1479 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1480 CWFrameIdx, 1);
1481
1482 // Set the high part to be round to zero...
1483 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1484 CWFrameIdx, 1).addImm(12);
1485
1486 // Reload the modified control word now...
1487 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1488
1489 // Restore the memory image of control word to original value
1490 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1491 CWFrameIdx, 1).addReg(HighPartOfCW);
1492
1493 // We don't have the facilities for directly storing byte sized data to
1494 // memory. Promote it to 16 bits. We also must promote unsigned values to
1495 // larger classes because we only have signed FP stores.
1496 MVT::ValueType StoreClass = Node->getValueType(0);
1497 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1498 switch (StoreClass) {
1499 case MVT::i8: StoreClass = MVT::i16; break;
1500 case MVT::i16: StoreClass = MVT::i32; break;
1501 case MVT::i32: StoreClass = MVT::i64; break;
1502 // The following treatment of cLong may not be perfectly right,
1503 // but it survives chains of casts of the form
1504 // double->ulong->double.
1505 case MVT::i64: StoreClass = MVT::i64; break;
1506 default: assert(0 && "Unknown store class!");
1507 }
1508
1509 // Spill the integer to memory and reload it from there.
1510 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1511 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1512
1513 switch (StoreClass) {
1514 default: assert(0 && "Unknown store class!");
1515 case MVT::i16:
1516 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1517 break;
1518 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001519 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001520 break;
1521 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001522 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001523 break;
1524 }
1525
1526 switch (Node->getValueType(0)) {
1527 default:
1528 assert(0 && "Unknown integer type!");
1529 case MVT::i64:
1530 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001531 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001532 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1533 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1534 case MVT::i32:
1535 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1536 break;
1537 case MVT::i16:
1538 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1539 break;
1540 case MVT::i8:
1541 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1542 break;
1543 }
1544
1545 // Reload the original control word now.
1546 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1547 return Result;
1548 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001549 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001550 Op0 = N.getOperand(0);
1551 Op1 = N.getOperand(1);
1552
Chris Lattner4ff348b2005-01-17 06:26:58 +00001553 if (isFoldableLoad(Op0, Op1)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001554 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001555 goto FoldAdd;
1556 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001557
Chris Lattner4ff348b2005-01-17 06:26:58 +00001558 if (isFoldableLoad(Op1, Op0)) {
1559 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001560 switch (N.getValueType()) {
1561 default: assert(0 && "Cannot add this type!");
1562 case MVT::i1:
1563 case MVT::i8: Opc = X86::ADD8rm; break;
1564 case MVT::i16: Opc = X86::ADD16rm; break;
1565 case MVT::i32: Opc = X86::ADD32rm; break;
1566 case MVT::f32: Opc = X86::FADD32m; break;
1567 case MVT::f64: Opc = X86::FADD64m; break;
1568 }
1569 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001570 EmitFoldedLoad(Op1, AM);
1571 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001572 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1573 return Result;
1574 }
1575
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001576 // See if we can codegen this as an LEA to fold operations together.
1577 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001578 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001579 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001580 MatchAddress(N, AM);
1581 ExprMap[N] = Result;
1582
1583 // If this is not just an add, emit the LEA. For a simple add (like
1584 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1585 // leave this as LEA, then peephole it to 'ADD' after two address elim
1586 // happens.
1587 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1588 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1589 X86AddressMode XAM = SelectAddrExprs(AM);
1590 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1591 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001592 }
1593 }
Chris Lattner11333092005-01-11 03:11:44 +00001594
Chris Lattnera5ade062005-01-11 21:19:59 +00001595 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001596 Opc = 0;
1597 if (CN->getValue() == 1) { // add X, 1 -> inc X
1598 switch (N.getValueType()) {
1599 default: assert(0 && "Cannot integer add this type!");
1600 case MVT::i8: Opc = X86::INC8r; break;
1601 case MVT::i16: Opc = X86::INC16r; break;
1602 case MVT::i32: Opc = X86::INC32r; break;
1603 }
1604 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1605 switch (N.getValueType()) {
1606 default: assert(0 && "Cannot integer add this type!");
1607 case MVT::i8: Opc = X86::DEC8r; break;
1608 case MVT::i16: Opc = X86::DEC16r; break;
1609 case MVT::i32: Opc = X86::DEC32r; break;
1610 }
1611 }
1612
1613 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001614 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001615 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1616 return Result;
1617 }
1618
1619 switch (N.getValueType()) {
1620 default: assert(0 && "Cannot add this type!");
1621 case MVT::i8: Opc = X86::ADD8ri; break;
1622 case MVT::i16: Opc = X86::ADD16ri; break;
1623 case MVT::i32: Opc = X86::ADD32ri; break;
1624 }
1625 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001626 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001627 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1628 return Result;
1629 }
1630 }
1631
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001632 switch (N.getValueType()) {
1633 default: assert(0 && "Cannot add this type!");
1634 case MVT::i8: Opc = X86::ADD8rr; break;
1635 case MVT::i16: Opc = X86::ADD16rr; break;
1636 case MVT::i32: Opc = X86::ADD32rr; break;
1637 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001638 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001639 }
Chris Lattner11333092005-01-11 03:11:44 +00001640
Chris Lattnera5ade062005-01-11 21:19:59 +00001641 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1642 Tmp1 = SelectExpr(Op0);
1643 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001644 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001645 Tmp2 = SelectExpr(Op1);
1646 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001647 }
1648
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001649 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1650 return Result;
1651 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001652 case ISD::MUL:
1653 case ISD::AND:
1654 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001655 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001656 static const unsigned SUBTab[] = {
1657 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1658 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1659 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1660 };
1661 static const unsigned MULTab[] = {
1662 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1663 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1664 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1665 };
1666 static const unsigned ANDTab[] = {
1667 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1668 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1669 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1670 };
1671 static const unsigned ORTab[] = {
1672 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1673 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1674 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1675 };
1676 static const unsigned XORTab[] = {
1677 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1678 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1679 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1680 };
1681
1682 Op0 = Node->getOperand(0);
1683 Op1 = Node->getOperand(1);
1684
1685 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001686 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1687 if (CN->isNullValue()) { // 0 - N -> neg N
1688 switch (N.getValueType()) {
1689 default: assert(0 && "Cannot sub this type!");
1690 case MVT::i1:
1691 case MVT::i8: Opc = X86::NEG8r; break;
1692 case MVT::i16: Opc = X86::NEG16r; break;
1693 case MVT::i32: Opc = X86::NEG32r; break;
1694 }
1695 Tmp1 = SelectExpr(N.getOperand(1));
1696 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1697 return Result;
1698 }
1699
Chris Lattnera5ade062005-01-11 21:19:59 +00001700 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1701 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001702 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001703 switch (N.getValueType()) {
1704 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001705 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001706 case MVT::i8: Opc = X86::NOT8r; break;
1707 case MVT::i16: Opc = X86::NOT16r; break;
1708 case MVT::i32: Opc = X86::NOT32r; break;
1709 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001710 if (Opc) {
1711 Tmp1 = SelectExpr(Op0);
1712 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1713 return Result;
1714 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001715 }
1716
Chris Lattner2a4e5082005-01-17 06:48:02 +00001717 // Fold common multiplies into LEA instructions.
1718 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1719 switch ((int)CN->getValue()) {
1720 default: break;
1721 case 3:
1722 case 5:
1723 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001724 // Remove N from exprmap so SelectAddress doesn't get confused.
1725 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001726 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001727 SelectAddress(N, AM);
1728 // Restore it to the map.
1729 ExprMap[N] = Result;
1730 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1731 return Result;
1732 }
1733 }
1734
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001735 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001736 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001737 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001738 case MVT::i8: Opc = 0; break;
1739 case MVT::i16: Opc = 1; break;
1740 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001741 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001742 switch (Node->getOpcode()) {
1743 default: assert(0 && "Unreachable!");
1744 case ISD::SUB: Opc = SUBTab[Opc]; break;
1745 case ISD::MUL: Opc = MULTab[Opc]; break;
1746 case ISD::AND: Opc = ANDTab[Opc]; break;
1747 case ISD::OR: Opc = ORTab[Opc]; break;
1748 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001749 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001750 if (Opc) { // Can't fold MUL:i8 R, imm
1751 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001752 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1753 return Result;
1754 }
1755 }
Chris Lattner11333092005-01-11 03:11:44 +00001756
Chris Lattner4ff348b2005-01-17 06:26:58 +00001757 if (isFoldableLoad(Op0, Op1))
Chris Lattnera5ade062005-01-11 21:19:59 +00001758 if (Node->getOpcode() != ISD::SUB) {
1759 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001760 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001761 } else {
1762 // Emit 'reverse' subract, with a memory operand.
1763 switch (N.getValueType()) {
1764 default: Opc = 0; break;
1765 case MVT::f32: Opc = X86::FSUBR32m; break;
1766 case MVT::f64: Opc = X86::FSUBR64m; break;
1767 }
1768 if (Opc) {
1769 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001770 EmitFoldedLoad(Op0, AM);
1771 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001772 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1773 return Result;
1774 }
1775 }
1776
Chris Lattner4ff348b2005-01-17 06:26:58 +00001777 if (isFoldableLoad(Op1, Op0)) {
1778 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001779 switch (N.getValueType()) {
1780 default: assert(0 && "Cannot operate on this type!");
1781 case MVT::i1:
1782 case MVT::i8: Opc = 5; break;
1783 case MVT::i16: Opc = 6; break;
1784 case MVT::i32: Opc = 7; break;
1785 case MVT::f32: Opc = 8; break;
1786 case MVT::f64: Opc = 9; break;
1787 }
1788 switch (Node->getOpcode()) {
1789 default: assert(0 && "Unreachable!");
1790 case ISD::SUB: Opc = SUBTab[Opc]; break;
1791 case ISD::MUL: Opc = MULTab[Opc]; break;
1792 case ISD::AND: Opc = ANDTab[Opc]; break;
1793 case ISD::OR: Opc = ORTab[Opc]; break;
1794 case ISD::XOR: Opc = XORTab[Opc]; break;
1795 }
1796
1797 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001798 EmitFoldedLoad(Op1, AM);
1799 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001800 if (Opc) {
1801 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1802 } else {
1803 assert(Node->getOpcode() == ISD::MUL &&
1804 N.getValueType() == MVT::i8 && "Unexpected situation!");
1805 // Must use the MUL instruction, which forces use of AL.
1806 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1807 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1808 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1809 }
1810 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001811 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001812
1813 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1814 Tmp1 = SelectExpr(Op0);
1815 Tmp2 = SelectExpr(Op1);
1816 } else {
1817 Tmp2 = SelectExpr(Op1);
1818 Tmp1 = SelectExpr(Op0);
1819 }
1820
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001821 switch (N.getValueType()) {
1822 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001823 case MVT::i1:
1824 case MVT::i8: Opc = 10; break;
1825 case MVT::i16: Opc = 11; break;
1826 case MVT::i32: Opc = 12; break;
1827 case MVT::f32: Opc = 13; break;
1828 case MVT::f64: Opc = 14; break;
1829 }
1830 switch (Node->getOpcode()) {
1831 default: assert(0 && "Unreachable!");
1832 case ISD::SUB: Opc = SUBTab[Opc]; break;
1833 case ISD::MUL: Opc = MULTab[Opc]; break;
1834 case ISD::AND: Opc = ANDTab[Opc]; break;
1835 case ISD::OR: Opc = ORTab[Opc]; break;
1836 case ISD::XOR: Opc = XORTab[Opc]; break;
1837 }
1838 if (Opc) {
1839 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1840 } else {
1841 assert(Node->getOpcode() == ISD::MUL &&
1842 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001843 // Must use the MUL instruction, which forces use of AL.
1844 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1845 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1846 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001847 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001848 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001849 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001850 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001851 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1852 Tmp2 = SelectExpr(N.getOperand(1));
1853 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001854 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001855 Tmp3 = SelectExpr(N.getOperand(2));
1856 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001857 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00001858 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
1859 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001860
1861 case ISD::SDIV:
1862 case ISD::UDIV:
1863 case ISD::SREM:
1864 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001865 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
1866 "We don't support this operator!");
1867
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001868 if (N.getOpcode() == ISD::SDIV)
1869 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1870 // FIXME: These special cases should be handled by the lowering impl!
1871 unsigned RHS = CN->getValue();
1872 bool isNeg = false;
1873 if ((int)RHS < 0) {
1874 isNeg = true;
1875 RHS = -RHS;
1876 }
1877 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1878 unsigned Log = log2(RHS);
1879 unsigned TmpReg = MakeReg(N.getValueType());
1880 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1881 switch (N.getValueType()) {
1882 default: assert("Unknown type to signed divide!");
1883 case MVT::i8:
1884 SAROpc = X86::SAR8ri;
1885 SHROpc = X86::SHR8ri;
1886 ADDOpc = X86::ADD8rr;
1887 NEGOpc = X86::NEG8r;
1888 break;
1889 case MVT::i16:
1890 SAROpc = X86::SAR16ri;
1891 SHROpc = X86::SHR16ri;
1892 ADDOpc = X86::ADD16rr;
1893 NEGOpc = X86::NEG16r;
1894 break;
1895 case MVT::i32:
1896 SAROpc = X86::SAR32ri;
1897 SHROpc = X86::SHR32ri;
1898 ADDOpc = X86::ADD32rr;
1899 NEGOpc = X86::NEG32r;
1900 break;
1901 }
Chris Lattner11333092005-01-11 03:11:44 +00001902 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001903 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1904 unsigned TmpReg2 = MakeReg(N.getValueType());
1905 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1906 unsigned TmpReg3 = MakeReg(N.getValueType());
1907 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1908
1909 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1910 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1911 if (isNeg)
1912 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1913 return Result;
1914 }
1915 }
1916
Chris Lattner11333092005-01-11 03:11:44 +00001917 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1918 Tmp1 = SelectExpr(N.getOperand(0));
1919 Tmp2 = SelectExpr(N.getOperand(1));
1920 } else {
1921 Tmp2 = SelectExpr(N.getOperand(1));
1922 Tmp1 = SelectExpr(N.getOperand(0));
1923 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001924
1925 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1926 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1927 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1928 switch (N.getValueType()) {
1929 default: assert(0 && "Cannot sdiv this type!");
1930 case MVT::i8:
1931 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1932 LoReg = X86::AL;
1933 HiReg = X86::AH;
1934 MovOpcode = X86::MOV8rr;
1935 ClrOpcode = X86::MOV8ri;
1936 SExtOpcode = X86::CBW;
1937 break;
1938 case MVT::i16:
1939 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1940 LoReg = X86::AX;
1941 HiReg = X86::DX;
1942 MovOpcode = X86::MOV16rr;
1943 ClrOpcode = X86::MOV16ri;
1944 SExtOpcode = X86::CWD;
1945 break;
1946 case MVT::i32:
1947 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001948 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001949 HiReg = X86::EDX;
1950 MovOpcode = X86::MOV32rr;
1951 ClrOpcode = X86::MOV32ri;
1952 SExtOpcode = X86::CDQ;
1953 break;
1954 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1955 case MVT::f32:
1956 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001957 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001958 return Result;
1959 }
1960
1961 // Set up the low part.
1962 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1963
1964 if (isSigned) {
1965 // Sign extend the low part into the high part.
1966 BuildMI(BB, SExtOpcode, 0);
1967 } else {
1968 // Zero out the high part, effectively zero extending the input.
1969 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1970 }
1971
1972 // Emit the DIV/IDIV instruction.
1973 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1974
1975 // Get the result of the divide or rem.
1976 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1977 return Result;
1978 }
1979
1980 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001981 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001982 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1983 switch (N.getValueType()) {
1984 default: assert(0 && "Cannot shift this type!");
1985 case MVT::i8: Opc = X86::ADD8rr; break;
1986 case MVT::i16: Opc = X86::ADD16rr; break;
1987 case MVT::i32: Opc = X86::ADD32rr; break;
1988 }
1989 Tmp1 = SelectExpr(N.getOperand(0));
1990 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1991 return Result;
1992 }
1993
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001994 switch (N.getValueType()) {
1995 default: assert(0 && "Cannot shift this type!");
1996 case MVT::i8: Opc = X86::SHL8ri; break;
1997 case MVT::i16: Opc = X86::SHL16ri; break;
1998 case MVT::i32: Opc = X86::SHL32ri; break;
1999 }
Chris Lattner11333092005-01-11 03:11:44 +00002000 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002001 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2002 return Result;
2003 }
Chris Lattner11333092005-01-11 03:11:44 +00002004
2005 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2006 Tmp1 = SelectExpr(N.getOperand(0));
2007 Tmp2 = SelectExpr(N.getOperand(1));
2008 } else {
2009 Tmp2 = SelectExpr(N.getOperand(1));
2010 Tmp1 = SelectExpr(N.getOperand(0));
2011 }
2012
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002013 switch (N.getValueType()) {
2014 default: assert(0 && "Cannot shift this type!");
2015 case MVT::i8 : Opc = X86::SHL8rCL; break;
2016 case MVT::i16: Opc = X86::SHL16rCL; break;
2017 case MVT::i32: Opc = X86::SHL32rCL; break;
2018 }
2019 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2020 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2021 return Result;
2022 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002023 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2024 switch (N.getValueType()) {
2025 default: assert(0 && "Cannot shift this type!");
2026 case MVT::i8: Opc = X86::SHR8ri; break;
2027 case MVT::i16: Opc = X86::SHR16ri; break;
2028 case MVT::i32: Opc = X86::SHR32ri; break;
2029 }
Chris Lattner11333092005-01-11 03:11:44 +00002030 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002031 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2032 return Result;
2033 }
Chris Lattner11333092005-01-11 03:11:44 +00002034
2035 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2036 Tmp1 = SelectExpr(N.getOperand(0));
2037 Tmp2 = SelectExpr(N.getOperand(1));
2038 } else {
2039 Tmp2 = SelectExpr(N.getOperand(1));
2040 Tmp1 = SelectExpr(N.getOperand(0));
2041 }
2042
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002043 switch (N.getValueType()) {
2044 default: assert(0 && "Cannot shift this type!");
2045 case MVT::i8 : Opc = X86::SHR8rCL; break;
2046 case MVT::i16: Opc = X86::SHR16rCL; break;
2047 case MVT::i32: Opc = X86::SHR32rCL; break;
2048 }
2049 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2050 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2051 return Result;
2052 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002053 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2054 switch (N.getValueType()) {
2055 default: assert(0 && "Cannot shift this type!");
2056 case MVT::i8: Opc = X86::SAR8ri; break;
2057 case MVT::i16: Opc = X86::SAR16ri; break;
2058 case MVT::i32: Opc = X86::SAR32ri; break;
2059 }
Chris Lattner11333092005-01-11 03:11:44 +00002060 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002061 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2062 return Result;
2063 }
Chris Lattner11333092005-01-11 03:11:44 +00002064
2065 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2066 Tmp1 = SelectExpr(N.getOperand(0));
2067 Tmp2 = SelectExpr(N.getOperand(1));
2068 } else {
2069 Tmp2 = SelectExpr(N.getOperand(1));
2070 Tmp1 = SelectExpr(N.getOperand(0));
2071 }
2072
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002073 switch (N.getValueType()) {
2074 default: assert(0 && "Cannot shift this type!");
2075 case MVT::i8 : Opc = X86::SAR8rCL; break;
2076 case MVT::i16: Opc = X86::SAR16rCL; break;
2077 case MVT::i32: Opc = X86::SAR32rCL; break;
2078 }
2079 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2080 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2081 return Result;
2082
2083 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002084 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002085 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2086 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2087 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002088 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002089 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002090 if (Result != 1) { // Generate the token
2091 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2092 assert(0 && "Load already emitted!?");
2093 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002094 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2095
Chris Lattner5188ad72005-01-08 19:28:19 +00002096 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002097 default: assert(0 && "Cannot load this type!");
2098 case MVT::i1:
2099 case MVT::i8: Opc = X86::MOV8rm; break;
2100 case MVT::i16: Opc = X86::MOV16rm; break;
2101 case MVT::i32: Opc = X86::MOV32rm; break;
2102 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
2103 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2104 }
Chris Lattner11333092005-01-11 03:11:44 +00002105
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002106 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002107 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002108 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2109 } else {
2110 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002111
2112 SDOperand Chain = N.getOperand(0);
2113 SDOperand Address = N.getOperand(1);
2114 if (getRegPressure(Chain) > getRegPressure(Address)) {
2115 Select(Chain);
2116 SelectAddress(Address, AM);
2117 } else {
2118 SelectAddress(Address, AM);
2119 Select(Chain);
2120 }
2121
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002122 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2123 }
2124 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002125
2126 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2127 case ISD::ZEXTLOAD: {
2128 // Make sure we generate both values.
2129 if (Result != 1)
2130 ExprMap[N.getValue(1)] = 1; // Generate the token
2131 else
2132 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2133
Chris Lattnerda2ce112005-01-16 07:34:08 +00002134 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2135 if (Node->getValueType(0) == MVT::f64) {
2136 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2137 "Bad EXTLOAD!");
2138 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2139 CP->getIndex());
2140 return Result;
2141 }
2142
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002143 X86AddressMode AM;
2144 if (getRegPressure(Node->getOperand(0)) >
2145 getRegPressure(Node->getOperand(1))) {
2146 Select(Node->getOperand(0)); // chain
2147 SelectAddress(Node->getOperand(1), AM);
2148 } else {
2149 SelectAddress(Node->getOperand(1), AM);
2150 Select(Node->getOperand(0)); // chain
2151 }
2152
2153 switch (Node->getValueType(0)) {
2154 default: assert(0 && "Unknown type to sign extend to.");
2155 case MVT::f64:
2156 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2157 "Bad EXTLOAD!");
2158 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2159 break;
2160 case MVT::i32:
2161 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2162 default:
2163 assert(0 && "Bad zero extend!");
2164 case MVT::i1:
2165 case MVT::i8:
2166 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2167 break;
2168 case MVT::i16:
2169 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2170 break;
2171 }
2172 break;
2173 case MVT::i16:
2174 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2175 "Bad zero extend!");
2176 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2177 break;
2178 case MVT::i8:
2179 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2180 "Bad zero extend!");
2181 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2182 break;
2183 }
2184 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002185 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002186 case ISD::SEXTLOAD: {
2187 // Make sure we generate both values.
2188 if (Result != 1)
2189 ExprMap[N.getValue(1)] = 1; // Generate the token
2190 else
2191 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2192
2193 X86AddressMode AM;
2194 if (getRegPressure(Node->getOperand(0)) >
2195 getRegPressure(Node->getOperand(1))) {
2196 Select(Node->getOperand(0)); // chain
2197 SelectAddress(Node->getOperand(1), AM);
2198 } else {
2199 SelectAddress(Node->getOperand(1), AM);
2200 Select(Node->getOperand(0)); // chain
2201 }
2202
2203 switch (Node->getValueType(0)) {
2204 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2205 default: assert(0 && "Unknown type to sign extend to.");
2206 case MVT::i32:
2207 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2208 default:
2209 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2210 case MVT::i8:
2211 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2212 break;
2213 case MVT::i16:
2214 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2215 break;
2216 }
2217 break;
2218 case MVT::i16:
2219 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2220 "Cannot sign extend from bool!");
2221 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2222 break;
2223 }
2224 return Result;
2225 }
2226
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002227 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002228 // Generate both result values.
2229 if (Result != 1)
2230 ExprMap[N.getValue(1)] = 1; // Generate the token
2231 else
2232 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2233
2234 // FIXME: We are currently ignoring the requested alignment for handling
2235 // greater than the stack alignment. This will need to be revisited at some
2236 // point. Align = N.getOperand(2);
2237
2238 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2239 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2240 std::cerr << "Cannot allocate stack object with greater alignment than"
2241 << " the stack alignment yet!";
2242 abort();
2243 }
2244
2245 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002246 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002247 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2248 .addImm(CN->getValue());
2249 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002250 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2251 Select(N.getOperand(0));
2252 Tmp1 = SelectExpr(N.getOperand(1));
2253 } else {
2254 Tmp1 = SelectExpr(N.getOperand(1));
2255 Select(N.getOperand(0));
2256 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002257
2258 // Subtract size from stack pointer, thereby allocating some space.
2259 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2260 }
2261
2262 // Put a pointer to the space into the result register, by copying the stack
2263 // pointer.
2264 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2265 return Result;
2266
2267 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002268 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002269 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002270
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002271 if (GlobalAddressSDNode *GASD =
2272 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002273 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002274 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2275 } else if (ExternalSymbolSDNode *ESSDN =
2276 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002277 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002278 BuildMI(BB, X86::CALLpcrel32,
2279 1).addExternalSymbol(ESSDN->getSymbol(), true);
2280 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002281 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2282 Select(N.getOperand(0));
2283 Tmp1 = SelectExpr(N.getOperand(1));
2284 } else {
2285 Tmp1 = SelectExpr(N.getOperand(1));
2286 Select(N.getOperand(0));
2287 }
2288
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002289 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2290 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002291 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002292 default: assert(0 && "Unknown value type for call result!");
2293 case MVT::Other: return 1;
2294 case MVT::i1:
2295 case MVT::i8:
2296 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2297 break;
2298 case MVT::i16:
2299 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2300 break;
2301 case MVT::i32:
2302 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002303 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002304 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2305 break;
2306 case MVT::f32:
2307 case MVT::f64: // Floating-point return values live in %ST(0)
2308 ContainsFPCode = true;
2309 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2310 break;
2311 }
2312 return Result+N.ResNo;
2313 }
2314
2315 return 0;
2316}
2317
Chris Lattnere10269b2005-01-17 19:25:26 +00002318/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2319/// load/op/store instruction. If successful return true.
2320bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2321 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2322 SDOperand Chain = Node->getOperand(0);
2323 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002324 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002325
2326 // The chain has to be a load, the stored value must be an integer binary
2327 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002328 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002329 MVT::isFloatingPoint(StVal.getValueType()))
2330 return false;
2331
Chris Lattner5c659812005-01-17 22:10:42 +00002332 // Token chain must either be a factor node or the load to fold.
2333 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2334 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002335
Chris Lattner5c659812005-01-17 22:10:42 +00002336 SDOperand TheLoad;
2337
2338 // Check to see if there is a load from the same pointer that we're storing
2339 // to in either operand of the binop.
2340 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2341 StVal.getOperand(0).getOperand(1) == StPtr)
2342 TheLoad = StVal.getOperand(0);
2343 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2344 StVal.getOperand(1).getOperand(1) == StPtr)
2345 TheLoad = StVal.getOperand(1);
2346 else
2347 return false; // No matching load operand.
2348
2349 // We can only fold the load if there are no intervening side-effecting
2350 // operations. This means that the store uses the load as its token chain, or
2351 // there are only token factor nodes in between the store and load.
2352 if (Chain != TheLoad.getValue(1)) {
2353 // Okay, the other option is that we have a store referring to (possibly
2354 // nested) token factor nodes. For now, just try peeking through one level
2355 // of token factors to see if this is the case.
2356 bool ChainOk = false;
2357 if (Chain.getOpcode() == ISD::TokenFactor) {
2358 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2359 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2360 ChainOk = true;
2361 break;
2362 }
2363 }
2364
2365 if (!ChainOk) return false;
2366 }
2367
2368 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002369 return false;
2370
2371 // Make sure that one of the operands of the binop is the load, and that the
2372 // load folds into the binop.
2373 if (((StVal.getOperand(0) != TheLoad ||
2374 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2375 (StVal.getOperand(1) != TheLoad ||
2376 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2377 return false;
2378
2379 // Finally, check to see if this is one of the ops we can handle!
2380 static const unsigned ADDTAB[] = {
2381 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2382 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2383 };
2384 static const unsigned SUBTAB[] = {
2385 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2386 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2387 };
2388 static const unsigned ANDTAB[] = {
2389 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2390 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2391 };
2392 static const unsigned ORTAB[] = {
2393 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2394 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2395 };
2396 static const unsigned XORTAB[] = {
2397 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2398 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2399 };
2400 static const unsigned SHLTAB[] = {
2401 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2402 /*Have to put the reg in CL*/0, 0, 0,
2403 };
2404 static const unsigned SARTAB[] = {
2405 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2406 /*Have to put the reg in CL*/0, 0, 0,
2407 };
2408 static const unsigned SHRTAB[] = {
2409 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2410 /*Have to put the reg in CL*/0, 0, 0,
2411 };
2412
2413 const unsigned *TabPtr = 0;
2414 switch (StVal.getOpcode()) {
2415 default:
2416 std::cerr << "CANNOT [mem] op= val: ";
2417 StVal.Val->dump(); std::cerr << "\n";
2418 case ISD::MUL:
2419 case ISD::SDIV:
2420 case ISD::UDIV:
2421 case ISD::SREM:
2422 case ISD::UREM: return false;
2423
2424 case ISD::ADD: TabPtr = ADDTAB; break;
2425 case ISD::SUB: TabPtr = SUBTAB; break;
2426 case ISD::AND: TabPtr = ANDTAB; break;
2427 case ISD:: OR: TabPtr = ORTAB; break;
2428 case ISD::XOR: TabPtr = XORTAB; break;
2429 case ISD::SHL: TabPtr = SHLTAB; break;
2430 case ISD::SRA: TabPtr = SARTAB; break;
2431 case ISD::SRL: TabPtr = SHRTAB; break;
2432 }
2433
2434 // Handle: [mem] op= CST
2435 SDOperand Op0 = StVal.getOperand(0);
2436 SDOperand Op1 = StVal.getOperand(1);
2437 unsigned Opc;
2438 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2439 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2440 default: break;
2441 case MVT::i1:
2442 case MVT::i8: Opc = TabPtr[0]; break;
2443 case MVT::i16: Opc = TabPtr[1]; break;
2444 case MVT::i32: Opc = TabPtr[2]; break;
2445 }
2446
2447 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002448 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2449 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002450 Select(Chain);
2451
Chris Lattnere10269b2005-01-17 19:25:26 +00002452 X86AddressMode AM;
2453 if (getRegPressure(TheLoad.getOperand(0)) >
2454 getRegPressure(TheLoad.getOperand(1))) {
2455 Select(TheLoad.getOperand(0));
2456 SelectAddress(TheLoad.getOperand(1), AM);
2457 } else {
2458 SelectAddress(TheLoad.getOperand(1), AM);
2459 Select(TheLoad.getOperand(0));
2460 }
Chris Lattner5c659812005-01-17 22:10:42 +00002461
2462 if (StVal.getOpcode() == ISD::ADD) {
2463 if (CN->getValue() == 1) {
2464 switch (Op0.getValueType()) {
2465 default: break;
2466 case MVT::i8:
2467 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2468 return true;
2469 case MVT::i16: Opc = TabPtr[1];
2470 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2471 return true;
2472 case MVT::i32: Opc = TabPtr[2];
2473 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2474 return true;
2475 }
2476 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2477 switch (Op0.getValueType()) {
2478 default: break;
2479 case MVT::i8:
2480 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2481 return true;
2482 case MVT::i16: Opc = TabPtr[1];
2483 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2484 return true;
2485 case MVT::i32: Opc = TabPtr[2];
2486 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2487 return true;
2488 }
2489 }
2490 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002491
2492 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2493 return true;
2494 }
2495 }
2496
2497 // If we have [mem] = V op [mem], try to turn it into:
2498 // [mem] = [mem] op V.
2499 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2500 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2501 StVal.getOpcode() != ISD::SRL)
2502 std::swap(Op0, Op1);
2503
2504 if (Op0 != TheLoad) return false;
2505
2506 switch (Op0.getValueType()) {
2507 default: return false;
2508 case MVT::i1:
2509 case MVT::i8: Opc = TabPtr[3]; break;
2510 case MVT::i16: Opc = TabPtr[4]; break;
2511 case MVT::i32: Opc = TabPtr[5]; break;
2512 }
Chris Lattner5c659812005-01-17 22:10:42 +00002513
Chris Lattner4a108662005-01-18 03:51:59 +00002514 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2515 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002516 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002517 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002518
Chris Lattnere10269b2005-01-17 19:25:26 +00002519 X86AddressMode AM;
2520 SelectAddress(TheLoad.getOperand(1), AM);
2521 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002522 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002523 return true;
2524}
2525
2526
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002527void ISel::Select(SDOperand N) {
2528 unsigned Tmp1, Tmp2, Opc;
2529
2530 // FIXME: Disable for our current expansion model!
Chris Lattner4a108662005-01-18 03:51:59 +00002531 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002532 return; // Already selected.
2533
Chris Lattner989de032005-01-11 06:14:36 +00002534 SDNode *Node = N.Val;
2535
2536 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002537 default:
Chris Lattner989de032005-01-11 06:14:36 +00002538 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002539 assert(0 && "Node not handled yet!");
2540 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002541 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002542 if (Node->getNumOperands() == 2) {
2543 bool OneFirst =
2544 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2545 Select(Node->getOperand(OneFirst));
2546 Select(Node->getOperand(!OneFirst));
2547 } else {
2548 std::vector<std::pair<unsigned, unsigned> > OpsP;
2549 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2550 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2551 std::sort(OpsP.begin(), OpsP.end());
2552 std::reverse(OpsP.begin(), OpsP.end());
2553 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2554 Select(Node->getOperand(OpsP[i].second));
2555 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002556 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002557 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002558 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2559 Select(N.getOperand(0));
2560 Tmp1 = SelectExpr(N.getOperand(1));
2561 } else {
2562 Tmp1 = SelectExpr(N.getOperand(1));
2563 Select(N.getOperand(0));
2564 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002565 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002566
2567 if (Tmp1 != Tmp2) {
2568 switch (N.getOperand(1).getValueType()) {
2569 default: assert(0 && "Invalid type for operation!");
2570 case MVT::i1:
2571 case MVT::i8: Opc = X86::MOV8rr; break;
2572 case MVT::i16: Opc = X86::MOV16rr; break;
2573 case MVT::i32: Opc = X86::MOV32rr; break;
2574 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002575 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002576 }
2577 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2578 }
2579 return;
2580 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002581 switch (N.getNumOperands()) {
2582 default:
2583 assert(0 && "Unknown return instruction!");
2584 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002585 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2586 N.getOperand(2).getValueType() == MVT::i32 &&
2587 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002588 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2589 Tmp1 = SelectExpr(N.getOperand(1));
2590 Tmp2 = SelectExpr(N.getOperand(2));
2591 } else {
2592 Tmp2 = SelectExpr(N.getOperand(2));
2593 Tmp1 = SelectExpr(N.getOperand(1));
2594 }
2595 Select(N.getOperand(0));
2596
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002597 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2598 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2599 // Declare that EAX & EDX are live on exit.
2600 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2601 .addReg(X86::ESP);
2602 break;
2603 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002604 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2605 Select(N.getOperand(0));
2606 Tmp1 = SelectExpr(N.getOperand(1));
2607 } else {
2608 Tmp1 = SelectExpr(N.getOperand(1));
2609 Select(N.getOperand(0));
2610 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002611 switch (N.getOperand(1).getValueType()) {
2612 default: assert(0 && "All other types should have been promoted!!");
2613 case MVT::f64:
2614 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2615 // Declare that top-of-stack is live on exit
2616 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2617 break;
2618 case MVT::i32:
2619 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2620 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2621 break;
2622 }
2623 break;
2624 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002625 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002626 break;
2627 }
2628 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2629 return;
2630 case ISD::BR: {
2631 Select(N.getOperand(0));
2632 MachineBasicBlock *Dest =
2633 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2634 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2635 return;
2636 }
2637
2638 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002639 MachineBasicBlock *Dest =
2640 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002641
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002642 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2643 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002644 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2645 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2646 Select(N.getOperand(0));
2647 Tmp1 = SelectExpr(N.getOperand(1));
2648 } else {
2649 Tmp1 = SelectExpr(N.getOperand(1));
2650 Select(N.getOperand(0));
2651 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002652 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2653 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2654 }
Chris Lattner11333092005-01-11 03:11:44 +00002655
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002656 return;
2657 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002658
Chris Lattner4df0de92005-01-17 00:00:33 +00002659 case ISD::LOAD:
2660 // If this load could be folded into the only using instruction, and if it
2661 // is safe to emit the instruction here, try to do so now.
2662 if (Node->hasNUsesOfValue(1, 0)) {
2663 SDOperand TheVal = N.getValue(0);
2664 SDNode *User = 0;
2665 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2666 assert(UI != Node->use_end() && "Didn't find use!");
2667 SDNode *UN = *UI;
2668 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2669 if (UN->getOperand(i) == TheVal) {
2670 User = UN;
2671 goto FoundIt;
2672 }
2673 }
2674 FoundIt:
2675 // Only handle unary operators right now.
2676 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00002677 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002678 SelectExpr(SDOperand(User, 0));
2679 return;
2680 }
2681 }
2682 SelectExpr(N);
2683 return;
2684
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002685 case ISD::EXTLOAD:
2686 case ISD::SEXTLOAD:
2687 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002688 case ISD::CALL:
2689 case ISD::DYNAMIC_STACKALLOC:
2690 SelectExpr(N);
2691 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002692
2693 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2694 // On X86, we can represent all types except for Bool and Float natively.
2695 X86AddressMode AM;
2696 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002697 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2698 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2699 && "Unsupported TRUNCSTORE for this target!");
2700
2701 if (StoredTy == MVT::i16) {
2702 // FIXME: This is here just to allow testing. X86 doesn't really have a
2703 // TRUNCSTORE i16 operation, but this is required for targets that do not
2704 // have 16-bit integer registers. We occasionally disable 16-bit integer
2705 // registers to test the promotion code.
2706 Select(N.getOperand(0));
2707 Tmp1 = SelectExpr(N.getOperand(1));
2708 SelectAddress(N.getOperand(2), AM);
2709
2710 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2711 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2712 return;
2713 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002714
2715 // Store of constant bool?
2716 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2717 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2718 Select(N.getOperand(0));
2719 SelectAddress(N.getOperand(2), AM);
2720 } else {
2721 SelectAddress(N.getOperand(2), AM);
2722 Select(N.getOperand(0));
2723 }
2724 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2725 return;
2726 }
2727
2728 switch (StoredTy) {
2729 default: assert(0 && "Cannot truncstore this type!");
2730 case MVT::i1: Opc = X86::MOV8mr; break;
2731 case MVT::f32: Opc = X86::FST32m; break;
2732 }
2733
2734 std::vector<std::pair<unsigned, unsigned> > RP;
2735 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2736 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2737 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2738 std::sort(RP.begin(), RP.end());
2739
2740 for (unsigned i = 0; i != 3; ++i)
2741 switch (RP[2-i].second) {
2742 default: assert(0 && "Unknown operand number!");
2743 case 0: Select(N.getOperand(0)); break;
2744 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2745 case 2: SelectAddress(N.getOperand(2), AM); break;
2746 }
2747
2748 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2749 return;
2750 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002751 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002752 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002753
2754 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2755 Opc = 0;
2756 switch (CN->getValueType(0)) {
2757 default: assert(0 && "Invalid type for operation!");
2758 case MVT::i1:
2759 case MVT::i8: Opc = X86::MOV8mi; break;
2760 case MVT::i16: Opc = X86::MOV16mi; break;
2761 case MVT::i32: Opc = X86::MOV32mi; break;
2762 case MVT::f32:
2763 case MVT::f64: break;
2764 }
2765 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002766 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2767 Select(N.getOperand(0));
2768 SelectAddress(N.getOperand(2), AM);
2769 } else {
2770 SelectAddress(N.getOperand(2), AM);
2771 Select(N.getOperand(0));
2772 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002773 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2774 return;
2775 }
2776 }
Chris Lattner837caa72005-01-11 23:21:30 +00002777
2778 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00002779 if (TryToFoldLoadOpStore(Node))
2780 return;
Chris Lattner837caa72005-01-11 23:21:30 +00002781
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002782 switch (N.getOperand(1).getValueType()) {
2783 default: assert(0 && "Cannot store this type!");
2784 case MVT::i1:
2785 case MVT::i8: Opc = X86::MOV8mr; break;
2786 case MVT::i16: Opc = X86::MOV16mr; break;
2787 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002788 case MVT::f32: Opc = X86::FST32m; break;
2789 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002790 }
Chris Lattner11333092005-01-11 03:11:44 +00002791
2792 std::vector<std::pair<unsigned, unsigned> > RP;
2793 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2794 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2795 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2796 std::sort(RP.begin(), RP.end());
2797
2798 for (unsigned i = 0; i != 3; ++i)
2799 switch (RP[2-i].second) {
2800 default: assert(0 && "Unknown operand number!");
2801 case 0: Select(N.getOperand(0)); break;
2802 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002803 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002804 }
2805
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002806 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2807 return;
2808 }
2809 case ISD::ADJCALLSTACKDOWN:
2810 case ISD::ADJCALLSTACKUP:
2811 Select(N.getOperand(0));
2812 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2813
2814 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2815 X86::ADJCALLSTACKUP;
2816 BuildMI(BB, Opc, 1).addImm(Tmp1);
2817 return;
Chris Lattner989de032005-01-11 06:14:36 +00002818 case ISD::MEMSET: {
2819 Select(N.getOperand(0)); // Select the chain.
2820 unsigned Align =
2821 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2822 if (Align == 0) Align = 1;
2823
2824 // Turn the byte code into # iterations
2825 unsigned CountReg;
2826 unsigned Opcode;
2827 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2828 unsigned Val = ValC->getValue() & 255;
2829
2830 // If the value is a constant, then we can potentially use larger sets.
2831 switch (Align & 3) {
2832 case 2: // WORD aligned
2833 CountReg = MakeReg(MVT::i32);
2834 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2835 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2836 } else {
2837 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2838 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2839 }
2840 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2841 Opcode = X86::REP_STOSW;
2842 break;
2843 case 0: // DWORD aligned
2844 CountReg = MakeReg(MVT::i32);
2845 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2846 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2847 } else {
2848 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2849 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2850 }
2851 Val = (Val << 8) | Val;
2852 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2853 Opcode = X86::REP_STOSD;
2854 break;
2855 default: // BYTE aligned
2856 CountReg = SelectExpr(Node->getOperand(3));
2857 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2858 Opcode = X86::REP_STOSB;
2859 break;
2860 }
2861 } else {
2862 // If it's not a constant value we are storing, just fall back. We could
2863 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2864 unsigned ValReg = SelectExpr(Node->getOperand(2));
2865 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2866 CountReg = SelectExpr(Node->getOperand(3));
2867 Opcode = X86::REP_STOSB;
2868 }
2869
2870 // No matter what the alignment is, we put the source in ESI, the
2871 // destination in EDI, and the count in ECX.
2872 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2873 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2874 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2875 BuildMI(BB, Opcode, 0);
2876 return;
2877 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002878 case ISD::MEMCPY:
2879 Select(N.getOperand(0)); // Select the chain.
2880 unsigned Align =
2881 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2882 if (Align == 0) Align = 1;
2883
2884 // Turn the byte code into # iterations
2885 unsigned CountReg;
2886 unsigned Opcode;
2887 switch (Align & 3) {
2888 case 2: // WORD aligned
2889 CountReg = MakeReg(MVT::i32);
2890 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2891 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2892 } else {
2893 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2894 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2895 }
2896 Opcode = X86::REP_MOVSW;
2897 break;
2898 case 0: // DWORD aligned
2899 CountReg = MakeReg(MVT::i32);
2900 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2901 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2902 } else {
2903 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2904 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2905 }
2906 Opcode = X86::REP_MOVSD;
2907 break;
2908 default: // BYTE aligned
2909 CountReg = SelectExpr(Node->getOperand(3));
2910 Opcode = X86::REP_MOVSB;
2911 break;
2912 }
2913
2914 // No matter what the alignment is, we put the source in ESI, the
2915 // destination in EDI, and the count in ECX.
2916 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2917 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2918 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2919 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2920 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2921 BuildMI(BB, Opcode, 0);
2922 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002923 }
2924 assert(0 && "Should not be reached!");
2925}
2926
2927
2928/// createX86PatternInstructionSelector - This pass converts an LLVM function
2929/// into a machine code representation using pattern matching and a machine
2930/// description file.
2931///
2932FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2933 return new ISel(TM);
2934}