blob: 901077b24ad4fdecf1d1ca7d8a88f269158025eb [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000030#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// X86TargetLowering - X86 Implementation of the TargetLowering interface
35namespace {
36 class X86TargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000038 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000039 public:
40 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000042
43 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
44 setShiftAmountType(MVT::i8);
45 setSetCCResultType(MVT::i8);
Chris Lattner009b55b2005-01-19 03:36:30 +000046 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000047
48 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000049 addRegisterClass(MVT::i8, X86::R8RegisterClass);
50 addRegisterClass(MVT::i16, X86::R16RegisterClass);
51 addRegisterClass(MVT::i32, X86::R32RegisterClass);
52 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
53
54 // FIXME: Eliminate these two classes when legalize can handle promotions
55 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000056/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000057
58 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
59 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
60 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i16 , Expand);
61 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
62 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i1 , Expand);
63 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
64 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
65 setOperationAction(ISD::SREM , MVT::f64 , Expand);
66
67 // These should be promoted to a larger select which is supported.
68/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
69 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000070
71 computeRegisterProperties();
Chris Lattner8acb1ba2005-01-07 07:49:41 +000072
73 addLegalFPImmediate(+0.0); // FLD0
74 addLegalFPImmediate(+1.0); // FLD1
75 addLegalFPImmediate(-0.0); // FLD0/FCHS
76 addLegalFPImmediate(-1.0); // FLD1/FCHS
77 }
78
79 /// LowerArguments - This hook must be implemented to indicate how we should
80 /// lower the arguments for the specified function, into the specified DAG.
81 virtual std::vector<SDOperand>
82 LowerArguments(Function &F, SelectionDAG &DAG);
83
84 /// LowerCallTo - This hook lowers an abstract call to a function into an
85 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000086 virtual std::pair<SDOperand, SDOperand>
87 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
88 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000089
90 virtual std::pair<SDOperand, SDOperand>
91 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
92
93 virtual std::pair<SDOperand,SDOperand>
94 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
95 const Type *ArgTy, SelectionDAG &DAG);
96
97 virtual std::pair<SDOperand, SDOperand>
98 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
99 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000100 };
101}
102
103
104std::vector<SDOperand>
105X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
106 std::vector<SDOperand> ArgValues;
107
108 // Add DAG nodes to load the arguments... On entry to a function on the X86,
109 // the stack frame looks like this:
110 //
111 // [ESP] -- return address
112 // [ESP + 4] -- first argument (leftmost lexically)
113 // [ESP + 8] -- second argument, if first argument is four bytes in size
114 // ...
115 //
116 MachineFunction &MF = DAG.getMachineFunction();
117 MachineFrameInfo *MFI = MF.getFrameInfo();
118
119 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
120 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
121 MVT::ValueType ObjectVT = getValueType(I->getType());
122 unsigned ArgIncrement = 4;
123 unsigned ObjSize;
124 switch (ObjectVT) {
125 default: assert(0 && "Unhandled argument type!");
126 case MVT::i1:
127 case MVT::i8: ObjSize = 1; break;
128 case MVT::i16: ObjSize = 2; break;
129 case MVT::i32: ObjSize = 4; break;
130 case MVT::i64: ObjSize = ArgIncrement = 8; break;
131 case MVT::f32: ObjSize = 4; break;
132 case MVT::f64: ObjSize = ArgIncrement = 8; break;
133 }
134 // Create the frame index object for this incoming parameter...
135 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
136
137 // Create the SelectionDAG nodes corresponding to a load from this parameter
138 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
139
140 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
141 // dead loads.
142 SDOperand ArgValue;
143 if (!I->use_empty())
144 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
145 else {
146 if (MVT::isInteger(ObjectVT))
147 ArgValue = DAG.getConstant(0, ObjectVT);
148 else
149 ArgValue = DAG.getConstantFP(0, ObjectVT);
150 }
151 ArgValues.push_back(ArgValue);
152
153 ArgOffset += ArgIncrement; // Move on to the next argument...
154 }
155
156 // If the function takes variable number of arguments, make a frame index for
157 // the start of the first vararg value... for expansion of llvm.va_start.
158 if (F.isVarArg())
159 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000160 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000161 return ArgValues;
162}
163
Chris Lattner5188ad72005-01-08 19:28:19 +0000164std::pair<SDOperand, SDOperand>
165X86TargetLowering::LowerCallTo(SDOperand Chain,
166 const Type *RetTy, SDOperand Callee,
167 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000168 // Count how many bytes are to be pushed on the stack.
169 unsigned NumBytes = 0;
170
171 if (Args.empty()) {
172 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000173 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
174 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000175 } else {
176 for (unsigned i = 0, e = Args.size(); i != e; ++i)
177 switch (getValueType(Args[i].second)) {
178 default: assert(0 && "Unknown value type!");
179 case MVT::i1:
180 case MVT::i8:
181 case MVT::i16:
182 case MVT::i32:
183 case MVT::f32:
184 NumBytes += 4;
185 break;
186 case MVT::i64:
187 case MVT::f64:
188 NumBytes += 8;
189 break;
190 }
191
Chris Lattner5188ad72005-01-08 19:28:19 +0000192 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
193 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000194
195 // Arguments go on the stack in reverse order, as specified by the ABI.
196 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000197 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
198 DAG.getEntryNode());
Chris 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 Lattner30ea1e92005-01-19 07:37:26 +0000373 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000374 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000375 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000376 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
377 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000378 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000379
380 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
381 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
382 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000383 void Select(SDOperand N);
384 };
385}
386
Chris Lattner7dbcb752005-01-12 04:21:28 +0000387/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
388/// when it has created a SelectionDAG for us to codegen.
389void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
390 // While we're doing this, keep track of whether we see any FP code for
391 // FP_REG_KILL insertion.
392 ContainsFPCode = false;
393
394 // Scan the PHI nodes that already are inserted into this basic block. If any
395 // of them is a PHI of a floating point value, we need to insert an
396 // FP_REG_KILL.
397 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
398 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
399 I != E; ++I) {
400 assert(I->getOpcode() == X86::PHI &&
401 "Isn't just PHI nodes?");
402 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
403 X86::RFPRegisterClass) {
404 ContainsFPCode = true;
405 break;
406 }
407 }
408
409 // Compute the RegPressureMap, which is an approximation for the number of
410 // registers required to compute each node.
411 ComputeRegPressure(DAG.getRoot());
412
413 // Codegen the basic block.
414 Select(DAG.getRoot());
415
416 // Finally, look at all of the successors of this block. If any contain a PHI
417 // node of FP type, we need to insert an FP_REG_KILL in this block.
418 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
419 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
420 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
421 I != E && I->getOpcode() == X86::PHI; ++I) {
422 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
423 X86::RFPRegisterClass) {
424 ContainsFPCode = true;
425 break;
426 }
427 }
428
429 // Insert FP_REG_KILL instructions into basic blocks that need them. This
430 // only occurs due to the floating point stackifier not being aggressive
431 // enough to handle arbitrary global stackification.
432 //
433 // Currently we insert an FP_REG_KILL instruction into each block that uses or
434 // defines a floating point virtual register.
435 //
436 // When the global register allocators (like linear scan) finally update live
437 // variable analysis, we can keep floating point values in registers across
438 // basic blocks. This will be a huge win, but we are waiting on the global
439 // allocators before we can do this.
440 //
441 if (ContainsFPCode && BB->succ_size()) {
442 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
443 ++NumFPKill;
444 }
445
446 // Clear state used for selection.
447 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000448 RegPressureMap.clear();
449}
450
451
Chris Lattner11333092005-01-11 03:11:44 +0000452// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
453// for the number of registers required to compute each node. This is basically
454// computing a generalized form of the Sethi-Ullman number for each node.
455unsigned ISel::ComputeRegPressure(SDOperand O) {
456 SDNode *N = O.Val;
457 unsigned &Result = RegPressureMap[N];
458 if (Result) return Result;
459
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000460 // FIXME: Should operations like CALL (which clobber lots o regs) have a
461 // higher fixed cost??
462
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000463 if (N->getNumOperands() == 0) {
464 Result = 1;
465 } else {
466 unsigned MaxRegUse = 0;
467 unsigned NumExtraMaxRegUsers = 0;
468 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
469 unsigned Regs;
470 if (N->getOperand(i).getOpcode() == ISD::Constant)
471 Regs = 0;
472 else
473 Regs = ComputeRegPressure(N->getOperand(i));
474 if (Regs > MaxRegUse) {
475 MaxRegUse = Regs;
476 NumExtraMaxRegUsers = 0;
477 } else if (Regs == MaxRegUse &&
478 N->getOperand(i).getValueType() != MVT::Other) {
479 ++NumExtraMaxRegUsers;
480 }
Chris Lattner11333092005-01-11 03:11:44 +0000481 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000482
483 if (O.getOpcode() != ISD::TokenFactor)
484 Result = MaxRegUse+NumExtraMaxRegUsers;
485 else
Chris Lattner869e0432005-01-17 23:02:13 +0000486 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000487 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000488
Chris Lattner837caa72005-01-11 23:21:30 +0000489 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000490 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000491}
492
Chris Lattner98a8ba02005-01-18 01:06:26 +0000493X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
494 X86AddressMode Result;
495
496 // If we need to emit two register operands, emit the one with the highest
497 // register pressure first.
498 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
499 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
500 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
501 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
502 Result.IndexReg = SelectExpr(IAM.IndexReg);
503 } else {
504 Result.IndexReg = SelectExpr(IAM.IndexReg);
505 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
506 }
507 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
508 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
509 } else if (IAM.IndexReg.Val) {
510 Result.IndexReg = SelectExpr(IAM.IndexReg);
511 }
512
513 switch (IAM.BaseType) {
514 case X86ISelAddressMode::RegBase:
515 Result.BaseType = X86AddressMode::RegBase;
516 break;
517 case X86ISelAddressMode::FrameIndexBase:
518 Result.BaseType = X86AddressMode::FrameIndexBase;
519 Result.Base.FrameIndex = IAM.Base.FrameIndex;
520 break;
521 default:
522 assert(0 && "Unknown base type!");
523 break;
524 }
525 Result.Scale = IAM.Scale;
526 Result.Disp = IAM.Disp;
527 Result.GV = IAM.GV;
528 return Result;
529}
530
531/// SelectAddress - Pattern match the maximal addressing mode for this node and
532/// emit all of the leaf registers.
533void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
534 X86ISelAddressMode IAM;
535 MatchAddress(N, IAM);
536 AM = SelectAddrExprs(IAM);
537}
538
539/// MatchAddress - Add the specified node to the specified addressing mode,
540/// returning true if it cannot be done. This just pattern matches for the
541/// addressing mode, it does not cause any code to be emitted. For that, use
542/// SelectAddress.
543bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000544 switch (N.getOpcode()) {
545 default: break;
546 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000547 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
548 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000549 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
550 return false;
551 }
552 break;
553 case ISD::GlobalAddress:
554 if (AM.GV == 0) {
555 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
556 return false;
557 }
558 break;
559 case ISD::Constant:
560 AM.Disp += cast<ConstantSDNode>(N)->getValue();
561 return false;
562 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000563 // We might have folded the load into this shift, so don't regen the value
564 // if so.
565 if (ExprMap.count(N)) break;
566
Chris Lattner98a8ba02005-01-18 01:06:26 +0000567 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000568 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
569 unsigned Val = CN->getValue();
570 if (Val == 1 || Val == 2 || Val == 3) {
571 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000572 SDOperand ShVal = N.Val->getOperand(0);
573
574 // Okay, we know that we have a scale by now. However, if the scaled
575 // value is an add of something and a constant, we can fold the
576 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000577 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000578 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000579 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000580 ConstantSDNode *AddVal =
581 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
582 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000583 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000584 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000585 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000586 return false;
587 }
588 }
589 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000590 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000591 // We might have folded the load into this mul, so don't regen the value if
592 // so.
593 if (ExprMap.count(N)) break;
594
Chris Lattner947d5442005-01-11 19:37:02 +0000595 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000596 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
597 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000598 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
599 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
600 AM.Scale = unsigned(CN->getValue())-1;
601
602 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000603 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000604
605 // Okay, we know that we have a scale by now. However, if the scaled
606 // value is an add of something and a constant, we can fold the
607 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000608 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000609 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000610 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000611 ConstantSDNode *AddVal =
612 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
613 AM.Disp += AddVal->getValue() * CN->getValue();
614 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000615 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000616 }
617
618 AM.IndexReg = AM.Base.Reg = Reg;
619 return false;
620 }
621 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000622
623 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000624 // We might have folded the load into this mul, so don't regen the value if
625 // so.
626 if (ExprMap.count(N)) break;
627
Chris Lattner98a8ba02005-01-18 01:06:26 +0000628 X86ISelAddressMode Backup = AM;
629 if (!MatchAddress(N.Val->getOperand(0), AM) &&
630 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000631 return false;
632 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000633 if (!MatchAddress(N.Val->getOperand(1), AM) &&
634 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000635 return false;
636 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000637 break;
638 }
639 }
640
Chris Lattnera95589b2005-01-11 04:40:19 +0000641 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000642 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000643 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000644 if (AM.IndexReg.Val == 0) {
645 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000646 AM.Scale = 1;
647 return false;
648 }
649
650 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000651 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000652 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000653
654 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000655 AM.BaseType = X86ISelAddressMode::RegBase;
656 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000657 return false;
658}
659
660/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
661/// assuming that the temporary registers are in the 8-bit register class.
662///
663/// Tmp1 = setcc1
664/// Tmp2 = setcc2
665/// DestReg = logicalop Tmp1, Tmp2
666///
667static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
668 unsigned SetCC2, unsigned LogicalOp,
669 unsigned DestReg) {
670 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
671 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
672 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
673 BuildMI(BB, SetCC1, 0, Tmp1);
674 BuildMI(BB, SetCC2, 0, Tmp2);
675 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
676}
677
678/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
679/// condition codes match the specified SetCCOpcode. Note that some conditions
680/// require multiple instructions to generate the correct value.
681static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
682 ISD::CondCode SetCCOpcode, bool isFP) {
683 unsigned Opc;
684 if (!isFP) {
685 switch (SetCCOpcode) {
686 default: assert(0 && "Illegal integer SetCC!");
687 case ISD::SETEQ: Opc = X86::SETEr; break;
688 case ISD::SETGT: Opc = X86::SETGr; break;
689 case ISD::SETGE: Opc = X86::SETGEr; break;
690 case ISD::SETLT: Opc = X86::SETLr; break;
691 case ISD::SETLE: Opc = X86::SETLEr; break;
692 case ISD::SETNE: Opc = X86::SETNEr; break;
693 case ISD::SETULT: Opc = X86::SETBr; break;
694 case ISD::SETUGT: Opc = X86::SETAr; break;
695 case ISD::SETULE: Opc = X86::SETBEr; break;
696 case ISD::SETUGE: Opc = X86::SETAEr; break;
697 }
698 } else {
699 // On a floating point condition, the flags are set as follows:
700 // ZF PF CF op
701 // 0 | 0 | 0 | X > Y
702 // 0 | 0 | 1 | X < Y
703 // 1 | 0 | 0 | X == Y
704 // 1 | 1 | 1 | unordered
705 //
706 switch (SetCCOpcode) {
707 default: assert(0 && "Invalid FP setcc!");
708 case ISD::SETUEQ:
709 case ISD::SETEQ:
710 Opc = X86::SETEr; // True if ZF = 1
711 break;
712 case ISD::SETOGT:
713 case ISD::SETGT:
714 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
715 break;
716 case ISD::SETOGE:
717 case ISD::SETGE:
718 Opc = X86::SETAEr; // True if CF = 0
719 break;
720 case ISD::SETULT:
721 case ISD::SETLT:
722 Opc = X86::SETBr; // True if CF = 1
723 break;
724 case ISD::SETULE:
725 case ISD::SETLE:
726 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
727 break;
728 case ISD::SETONE:
729 case ISD::SETNE:
730 Opc = X86::SETNEr; // True if ZF = 0
731 break;
732 case ISD::SETUO:
733 Opc = X86::SETPr; // True if PF = 1
734 break;
735 case ISD::SETO:
736 Opc = X86::SETNPr; // True if PF = 0
737 break;
738 case ISD::SETOEQ: // !PF & ZF
739 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
740 return;
741 case ISD::SETOLT: // !PF & CF
742 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
743 return;
744 case ISD::SETOLE: // !PF & (CF || ZF)
745 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
746 return;
747 case ISD::SETUGT: // PF | (!ZF & !CF)
748 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
749 return;
750 case ISD::SETUGE: // PF | !CF
751 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
752 return;
753 case ISD::SETUNE: // PF | !ZF
754 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
755 return;
756 }
757 }
758 BuildMI(BB, Opc, 0, DestReg);
759}
760
761
762/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
763/// the Dest block if the Cond condition is true. If we cannot fold this
764/// condition into the branch, return true.
765///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000766bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
767 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000768 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
769 // B) using two conditional branches instead of one condbr, two setcc's, and
770 // an or.
771 if ((Cond.getOpcode() == ISD::OR ||
772 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
773 // And and or set the flags for us, so there is no need to emit a TST of the
774 // result. It is only safe to do this if there is only a single use of the
775 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000776 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000777 SelectExpr(Cond);
778 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
779 return false;
780 }
781
782 // Codegen br not C -> JE.
783 if (Cond.getOpcode() == ISD::XOR)
784 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
785 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000786 unsigned CondR;
787 if (getRegPressure(Chain) > getRegPressure(Cond)) {
788 Select(Chain);
789 CondR = SelectExpr(Cond.Val->getOperand(0));
790 } else {
791 CondR = SelectExpr(Cond.Val->getOperand(0));
792 Select(Chain);
793 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000794 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
795 BuildMI(BB, X86::JE, 1).addMBB(Dest);
796 return false;
797 }
798
799 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
800 if (SetCC == 0)
801 return true; // Can only handle simple setcc's so far.
802
803 unsigned Opc;
804
805 // Handle integer conditions first.
806 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
807 switch (SetCC->getCondition()) {
808 default: assert(0 && "Illegal integer SetCC!");
809 case ISD::SETEQ: Opc = X86::JE; break;
810 case ISD::SETGT: Opc = X86::JG; break;
811 case ISD::SETGE: Opc = X86::JGE; break;
812 case ISD::SETLT: Opc = X86::JL; break;
813 case ISD::SETLE: Opc = X86::JLE; break;
814 case ISD::SETNE: Opc = X86::JNE; break;
815 case ISD::SETULT: Opc = X86::JB; break;
816 case ISD::SETUGT: Opc = X86::JA; break;
817 case ISD::SETULE: Opc = X86::JBE; break;
818 case ISD::SETUGE: Opc = X86::JAE; break;
819 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000820 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000821 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000822 BuildMI(BB, Opc, 1).addMBB(Dest);
823 return false;
824 }
825
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000826 unsigned Opc2 = 0; // Second branch if needed.
827
828 // On a floating point condition, the flags are set as follows:
829 // ZF PF CF op
830 // 0 | 0 | 0 | X > Y
831 // 0 | 0 | 1 | X < Y
832 // 1 | 0 | 0 | X == Y
833 // 1 | 1 | 1 | unordered
834 //
835 switch (SetCC->getCondition()) {
836 default: assert(0 && "Invalid FP setcc!");
837 case ISD::SETUEQ:
838 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
839 case ISD::SETOGT:
840 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
841 case ISD::SETOGE:
842 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
843 case ISD::SETULT:
844 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
845 case ISD::SETULE:
846 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
847 case ISD::SETONE:
848 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
849 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
850 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
851 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
852 Opc = X86::JA; // ZF = 0 & CF = 0
853 Opc2 = X86::JP; // PF = 1
854 break;
855 case ISD::SETUGE: // PF = 1 | CF = 0
856 Opc = X86::JAE; // CF = 0
857 Opc2 = X86::JP; // PF = 1
858 break;
859 case ISD::SETUNE: // PF = 1 | ZF = 0
860 Opc = X86::JNE; // ZF = 0
861 Opc2 = X86::JP; // PF = 1
862 break;
863 case ISD::SETOEQ: // PF = 0 & ZF = 1
864 //X86::JNP, X86::JE
865 //X86::AND8rr
866 return true; // FIXME: Emit more efficient code for this branch.
867 case ISD::SETOLT: // PF = 0 & CF = 1
868 //X86::JNP, X86::JB
869 //X86::AND8rr
870 return true; // FIXME: Emit more efficient code for this branch.
871 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
872 //X86::JNP, X86::JBE
873 //X86::AND8rr
874 return true; // FIXME: Emit more efficient code for this branch.
875 }
876
Chris Lattner6c07aee2005-01-11 04:06:27 +0000877 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000878 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000879 BuildMI(BB, Opc, 1).addMBB(Dest);
880 if (Opc2)
881 BuildMI(BB, Opc2, 1).addMBB(Dest);
882 return false;
883}
884
Chris Lattner24aad1b2005-01-10 22:10:13 +0000885/// EmitSelectCC - Emit code into BB that performs a select operation between
886/// the two registers RTrue and RFalse, generating a result into RDest. Return
887/// true if the fold cannot be performed.
888///
889void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
890 unsigned RTrue, unsigned RFalse, unsigned RDest) {
891 enum Condition {
892 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
893 NOT_SET
894 } CondCode = NOT_SET;
895
896 static const unsigned CMOVTAB16[] = {
897 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
898 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
899 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
900 };
901 static const unsigned CMOVTAB32[] = {
902 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
903 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
904 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
905 };
906 static const unsigned CMOVTABFP[] = {
907 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
908 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
909 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
910 };
911
912 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
913 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
914 switch (SetCC->getCondition()) {
915 default: assert(0 && "Unknown integer comparison!");
916 case ISD::SETEQ: CondCode = EQ; break;
917 case ISD::SETGT: CondCode = GT; break;
918 case ISD::SETGE: CondCode = GE; break;
919 case ISD::SETLT: CondCode = LT; break;
920 case ISD::SETLE: CondCode = LE; break;
921 case ISD::SETNE: CondCode = NE; break;
922 case ISD::SETULT: CondCode = B; break;
923 case ISD::SETUGT: CondCode = A; break;
924 case ISD::SETULE: CondCode = BE; break;
925 case ISD::SETUGE: CondCode = AE; break;
926 }
927 } else {
928 // On a floating point condition, the flags are set as follows:
929 // ZF PF CF op
930 // 0 | 0 | 0 | X > Y
931 // 0 | 0 | 1 | X < Y
932 // 1 | 0 | 0 | X == Y
933 // 1 | 1 | 1 | unordered
934 //
935 switch (SetCC->getCondition()) {
936 default: assert(0 && "Unknown FP comparison!");
937 case ISD::SETUEQ:
938 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
939 case ISD::SETOGT:
940 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
941 case ISD::SETOGE:
942 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
943 case ISD::SETULT:
944 case ISD::SETLT: CondCode = B; break; // True if CF = 1
945 case ISD::SETULE:
946 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
947 case ISD::SETONE:
948 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
949 case ISD::SETUO: CondCode = P; break; // True if PF = 1
950 case ISD::SETO: CondCode = NP; break; // True if PF = 0
951 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
952 case ISD::SETUGE: // PF = 1 | CF = 0
953 case ISD::SETUNE: // PF = 1 | ZF = 0
954 case ISD::SETOEQ: // PF = 0 & ZF = 1
955 case ISD::SETOLT: // PF = 0 & CF = 1
956 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
957 // We cannot emit this comparison as a single cmov.
958 break;
959 }
960 }
961 }
962
963 unsigned Opc = 0;
964 if (CondCode != NOT_SET) {
965 switch (SVT) {
966 default: assert(0 && "Cannot select this type!");
967 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
968 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
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;
Chris Lattneref7ba072005-01-11 03:50:45 +0000984 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000985 }
986 } else {
987 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000988 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000989 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000990 }
991 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
992}
993
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000994void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +0000995 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000996 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
997 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +0000998 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +0000999 switch (RHS.getValueType()) {
1000 default: break;
1001 case MVT::i1:
1002 case MVT::i8: Opc = X86::CMP8mi; break;
1003 case MVT::i16: Opc = X86::CMP16mi; break;
1004 case MVT::i32: Opc = X86::CMP32mi; break;
1005 }
1006 if (Opc) {
1007 X86AddressMode AM;
1008 EmitFoldedLoad(LHS, AM);
1009 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1010 return;
1011 }
1012 }
1013
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001014 switch (RHS.getValueType()) {
1015 default: break;
1016 case MVT::i1:
1017 case MVT::i8: Opc = X86::CMP8ri; break;
1018 case MVT::i16: Opc = X86::CMP16ri; break;
1019 case MVT::i32: Opc = X86::CMP32ri; break;
1020 }
1021 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001022 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001023 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1024 return;
1025 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001026 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1027 if (CN->isExactlyValue(+0.0) ||
1028 CN->isExactlyValue(-0.0)) {
1029 unsigned Reg = SelectExpr(LHS);
1030 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1031 BuildMI(BB, X86::FNSTSW8r, 0);
1032 BuildMI(BB, X86::SAHF, 1);
1033 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001034 }
1035
Chris Lattneref6806c2005-01-12 02:02:48 +00001036 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001037 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001038 switch (RHS.getValueType()) {
1039 default: break;
1040 case MVT::i1:
1041 case MVT::i8: Opc = X86::CMP8mr; break;
1042 case MVT::i16: Opc = X86::CMP16mr; break;
1043 case MVT::i32: Opc = X86::CMP32mr; break;
1044 }
1045 if (Opc) {
1046 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001047 EmitFoldedLoad(LHS, AM);
1048 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001049 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1050 return;
1051 }
1052 }
1053
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001054 switch (LHS.getValueType()) {
1055 default: assert(0 && "Cannot compare this value!");
1056 case MVT::i1:
1057 case MVT::i8: Opc = X86::CMP8rr; break;
1058 case MVT::i16: Opc = X86::CMP16rr; break;
1059 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001060 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001061 }
Chris Lattner11333092005-01-11 03:11:44 +00001062 unsigned Tmp1, Tmp2;
1063 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1064 Tmp1 = SelectExpr(LHS);
1065 Tmp2 = SelectExpr(RHS);
1066 } else {
1067 Tmp2 = SelectExpr(RHS);
1068 Tmp1 = SelectExpr(LHS);
1069 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001070 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1071}
1072
Chris Lattner4ff348b2005-01-17 06:26:58 +00001073/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1074/// The DAG cannot have cycles in it, by definition, so the visited set is not
1075/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1076/// reuse, so it prevents exponential cases.
1077///
1078static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1079 std::set<SDNode*> &Visited) {
1080 if (N == Op) return true; // Found it.
1081 SDNode *Node = N.Val;
1082 if (Node->getNumOperands() == 0) return false; // Leaf?
1083 if (!Visited.insert(Node).second) return false; // Already visited?
1084
1085 // Recurse for the first N-1 operands.
1086 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1087 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1088 return true;
1089
1090 // Tail recurse for the last operand.
1091 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1092}
1093
Chris Lattnera5ade062005-01-11 21:19:59 +00001094/// isFoldableLoad - Return true if this is a load instruction that can safely
1095/// be folded into an operation that uses it.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001096bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001097 if (Op.getOpcode() != ISD::LOAD ||
1098 // FIXME: currently can't fold constant pool indexes.
1099 isa<ConstantPoolSDNode>(Op.getOperand(1)))
1100 return false;
1101
1102 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001103 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1104 if (ExprMap.count(Op.getValue(1))) return false;
1105 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001106 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001107
Chris Lattner4ff348b2005-01-17 06:26:58 +00001108 // If there is not just one use of its value, we cannot fold.
1109 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1110
1111 // Finally, we cannot fold the load into the operation if this would induce a
1112 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1113 // operand of the operation we are folding the load into) can possible use the
1114 // chain node defined by the load.
1115 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1116 std::set<SDNode*> Visited;
1117 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1118 return false;
1119 }
1120 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001121}
1122
Chris Lattner4ff348b2005-01-17 06:26:58 +00001123
Chris Lattnera5ade062005-01-11 21:19:59 +00001124/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1125/// and compute the address being loaded into AM.
1126void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1127 SDOperand Chain = Op.getOperand(0);
1128 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001129
Chris Lattnera5ade062005-01-11 21:19:59 +00001130 if (getRegPressure(Chain) > getRegPressure(Address)) {
1131 Select(Chain);
1132 SelectAddress(Address, AM);
1133 } else {
1134 SelectAddress(Address, AM);
1135 Select(Chain);
1136 }
1137
1138 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001139 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1140 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001141 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001142 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001143}
1144
Chris Lattner30ea1e92005-01-19 07:37:26 +00001145// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1146// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1147// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1148// return true.
1149bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001150 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1151 // good!
1152 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1153 std::swap(Op1, Op2); // Op1 is the SHL now.
1154 } else {
1155 return false; // No match
1156 }
1157
1158 SDOperand ShlVal = Op1.getOperand(0);
1159 SDOperand ShlAmt = Op1.getOperand(1);
1160 SDOperand ShrVal = Op2.getOperand(0);
1161 SDOperand ShrAmt = Op2.getOperand(1);
1162
Chris Lattner30ea1e92005-01-19 07:37:26 +00001163 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1164
Chris Lattner85716372005-01-19 06:18:43 +00001165 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1166 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1167 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001168 if (SubCST->getValue() == RegSize) {
1169 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001170 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001171 if (ShrVal == ShlVal) {
1172 unsigned Reg, ShAmt;
1173 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1174 Reg = SelectExpr(ShrVal);
1175 ShAmt = SelectExpr(ShrAmt);
1176 } else {
1177 ShAmt = SelectExpr(ShrAmt);
1178 Reg = SelectExpr(ShrVal);
1179 }
1180 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1181 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1182 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1183 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1184 return true;
1185 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001186 unsigned AReg, BReg;
1187 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001188 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001189 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001190 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001191 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001192 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001193 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001194 unsigned ShAmt = SelectExpr(ShrAmt);
1195 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1196 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1197 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001198 return true;
1199 }
1200 }
1201
Chris Lattner4053b1e2005-01-19 08:07:05 +00001202 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1203 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1204 if (SubCST->getValue() == RegSize) {
1205 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1206 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1207 if (ShrVal == ShlVal) {
1208 unsigned Reg, ShAmt;
1209 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1210 Reg = SelectExpr(ShrVal);
1211 ShAmt = SelectExpr(ShlAmt);
1212 } else {
1213 ShAmt = SelectExpr(ShlAmt);
1214 Reg = SelectExpr(ShrVal);
1215 }
1216 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1217 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1218 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1219 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1220 return true;
1221 } else if (RegSize != 8) {
1222 unsigned AReg, BReg;
1223 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001224 AReg = SelectExpr(ShlVal);
1225 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001226 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001227 BReg = SelectExpr(ShrVal);
1228 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001229 }
1230 unsigned ShAmt = SelectExpr(ShlAmt);
1231 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1232 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1233 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1234 return true;
1235 }
1236 }
Chris Lattner85716372005-01-19 06:18:43 +00001237
Chris Lattner4053b1e2005-01-19 08:07:05 +00001238 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1239 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1240 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1241 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1242 // (A >> 5) | (A << 27) --> ROR A, 5
1243 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1244 if (ShrVal == ShlVal) {
1245 unsigned Reg = SelectExpr(ShrVal);
1246 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1247 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1248 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1249 return true;
1250 } else if (RegSize != 8) {
1251 unsigned AReg, BReg;
1252 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001253 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001254 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001255 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001256 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001257 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001258 }
1259 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1260 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1261 .addImm(ShrCst->getValue());
1262 return true;
1263 }
1264 }
1265
Chris Lattner85716372005-01-19 06:18:43 +00001266 return false;
1267}
1268
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001269unsigned ISel::SelectExpr(SDOperand N) {
1270 unsigned Result;
1271 unsigned Tmp1, Tmp2, Tmp3;
1272 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001273 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001274 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001275
Chris Lattner7f2afac2005-01-14 22:37:41 +00001276 if (Node->getOpcode() == ISD::CopyFromReg) {
1277 // FIXME: Handle copy from physregs!
1278
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001279 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001280 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001281 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001282
1283 unsigned &Reg = ExprMap[N];
1284 if (Reg) return Reg;
1285
1286 if (N.getOpcode() != ISD::CALL)
1287 Reg = Result = (N.getValueType() != MVT::Other) ?
1288 MakeReg(N.getValueType()) : 1;
1289 else {
1290 // If this is a call instruction, make sure to prepare ALL of the result
1291 // values as well as the chain.
1292 if (Node->getNumValues() == 1)
1293 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001294 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001295 Result = MakeReg(Node->getValueType(0));
1296 ExprMap[N.getValue(0)] = Result;
1297 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1298 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1299 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001300 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001301 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001302
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001303 switch (N.getOpcode()) {
1304 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001305 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001306 assert(0 && "Node not handled!\n");
1307 case ISD::FrameIndex:
1308 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1309 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1310 return Result;
1311 case ISD::ConstantPool:
1312 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1313 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1314 return Result;
1315 case ISD::ConstantFP:
1316 ContainsFPCode = true;
1317 Tmp1 = Result; // Intermediate Register
1318 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1319 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1320 Tmp1 = MakeReg(MVT::f64);
1321
1322 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1323 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1324 BuildMI(BB, X86::FLD0, 0, Tmp1);
1325 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1326 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1327 BuildMI(BB, X86::FLD1, 0, Tmp1);
1328 else
1329 assert(0 && "Unexpected constant!");
1330 if (Tmp1 != Result)
1331 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1332 return Result;
1333 case ISD::Constant:
1334 switch (N.getValueType()) {
1335 default: assert(0 && "Cannot use constants of this type!");
1336 case MVT::i1:
1337 case MVT::i8: Opc = X86::MOV8ri; break;
1338 case MVT::i16: Opc = X86::MOV16ri; break;
1339 case MVT::i32: Opc = X86::MOV32ri; break;
1340 }
1341 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1342 return Result;
1343 case ISD::GlobalAddress: {
1344 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1345 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1346 return Result;
1347 }
1348 case ISD::ExternalSymbol: {
1349 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1350 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1351 return Result;
1352 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001353 case ISD::ZERO_EXTEND: {
1354 int DestIs16 = N.getValueType() == MVT::i16;
1355 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001356
1357 // FIXME: This hack is here for zero extension casts from bool to i8. This
1358 // would not be needed if bools were promoted by Legalize.
1359 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001360 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001361 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1362 return Result;
1363 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001364
Chris Lattner4ff348b2005-01-17 06:26:58 +00001365 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001366 static const unsigned Opc[3] = {
1367 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1368 };
1369
1370 X86AddressMode AM;
1371 EmitFoldedLoad(N.getOperand(0), AM);
1372 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1373
1374 return Result;
1375 }
1376
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001377 static const unsigned Opc[3] = {
1378 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1379 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001380 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001381 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1382 return Result;
1383 }
1384 case ISD::SIGN_EXTEND: {
1385 int DestIs16 = N.getValueType() == MVT::i16;
1386 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1387
Chris Lattner590d8002005-01-09 18:52:44 +00001388 // FIXME: Legalize should promote bools to i8!
1389 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1390 "Sign extend from bool not implemented!");
1391
Chris Lattner4ff348b2005-01-17 06:26:58 +00001392 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001393 static const unsigned Opc[3] = {
1394 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1395 };
1396
1397 X86AddressMode AM;
1398 EmitFoldedLoad(N.getOperand(0), AM);
1399 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1400 return Result;
1401 }
1402
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001403 static const unsigned Opc[3] = {
1404 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1405 };
1406 Tmp1 = SelectExpr(N.getOperand(0));
1407 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1408 return Result;
1409 }
1410 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001411 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001412 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001413 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001414 switch (N.getValueType()) {
1415 default: assert(0 && "Unknown truncate!");
1416 case MVT::i1:
1417 case MVT::i8: Opc = X86::MOV8rm; break;
1418 case MVT::i16: Opc = X86::MOV16rm; break;
1419 }
1420 X86AddressMode AM;
1421 EmitFoldedLoad(N.getOperand(0), AM);
1422 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1423 return Result;
1424 }
1425
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001426 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1427 // a move out of AX or AL.
1428 switch (N.getOperand(0).getValueType()) {
1429 default: assert(0 && "Unknown truncate!");
1430 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1431 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1432 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1433 }
1434 Tmp1 = SelectExpr(N.getOperand(0));
1435 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1436
1437 switch (N.getValueType()) {
1438 default: assert(0 && "Unknown truncate!");
1439 case MVT::i1:
1440 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1441 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1442 }
1443 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1444 return Result;
1445
Chris Lattner590d8002005-01-09 18:52:44 +00001446 case ISD::SINT_TO_FP:
1447 case ISD::UINT_TO_FP: {
1448 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001449 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001450
1451 // Promote the integer to a type supported by FLD. We do this because there
1452 // are no unsigned FLD instructions, so we must promote an unsigned value to
1453 // a larger signed value, then use FLD on the larger value.
1454 //
1455 MVT::ValueType PromoteType = MVT::Other;
1456 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1457 unsigned PromoteOpcode = 0;
1458 unsigned RealDestReg = Result;
1459 switch (SrcTy) {
1460 case MVT::i1:
1461 case MVT::i8:
1462 // We don't have the facilities for directly loading byte sized data from
1463 // memory (even signed). Promote it to 16 bits.
1464 PromoteType = MVT::i16;
1465 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1466 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1467 break;
1468 case MVT::i16:
1469 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1470 PromoteType = MVT::i32;
1471 PromoteOpcode = X86::MOVZX32rr16;
1472 }
1473 break;
1474 default:
1475 // Don't fild into the real destination.
1476 if (Node->getOpcode() == ISD::UINT_TO_FP)
1477 Result = MakeReg(Node->getValueType(0));
1478 break;
1479 }
1480
1481 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1482
1483 if (PromoteType != MVT::Other) {
1484 Tmp2 = MakeReg(PromoteType);
1485 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1486 SrcTy = PromoteType;
1487 Tmp1 = Tmp2;
1488 }
1489
1490 // Spill the integer to memory and reload it from there.
1491 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1492 MachineFunction *F = BB->getParent();
1493 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1494
1495 switch (SrcTy) {
1496 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001497 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001498 // FIXME: this won't work for cast [u]long to FP
1499 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1500 FrameIdx).addReg(Tmp1);
1501 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1502 FrameIdx, 4).addReg(Tmp1+1);
1503 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1504 break;
1505 case MVT::i32:
1506 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1507 FrameIdx).addReg(Tmp1);
1508 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1509 break;
1510 case MVT::i16:
1511 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1512 FrameIdx).addReg(Tmp1);
1513 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1514 break;
1515 default: break; // No promotion required.
1516 }
1517
Chris Lattner085c9952005-01-12 04:00:00 +00001518 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001519 // If this is a cast from uint -> double, we need to be careful when if
1520 // the "sign" bit is set. If so, we don't want to make a negative number,
1521 // we want to make a positive number. Emit code to add an offset if the
1522 // sign bit is set.
1523
1524 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1525 unsigned IsNeg = MakeReg(MVT::i32);
1526 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1527
1528 // Create a CP value that has the offset in one word and 0 in the other.
1529 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1530 0x4f80000000000000ULL);
1531 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1532 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1533 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1534
1535 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1536 // We need special handling for unsigned 64-bit integer sources. If the
1537 // input number has the "sign bit" set, then we loaded it incorrectly as a
1538 // negative 64-bit number. In this case, add an offset value.
1539
1540 // Emit a test instruction to see if the dynamic input value was signed.
1541 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1542
1543 // If the sign bit is set, get a pointer to an offset, otherwise get a
1544 // pointer to a zero.
1545 MachineConstantPool *CP = F->getConstantPool();
1546 unsigned Zero = MakeReg(MVT::i32);
1547 Constant *Null = Constant::getNullValue(Type::UIntTy);
1548 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1549 CP->getConstantPoolIndex(Null));
1550 unsigned Offset = MakeReg(MVT::i32);
1551 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1552
1553 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1554 CP->getConstantPoolIndex(OffsetCst));
1555 unsigned Addr = MakeReg(MVT::i32);
1556 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1557
1558 // Load the constant for an add. FIXME: this could make an 'fadd' that
1559 // reads directly from memory, but we don't support these yet.
1560 unsigned ConstReg = MakeReg(MVT::f64);
1561 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1562
1563 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1564 }
1565 return RealDestReg;
1566 }
1567 case ISD::FP_TO_SINT:
1568 case ISD::FP_TO_UINT: {
1569 // FIXME: Most of this grunt work should be done by legalize!
1570 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1571
1572 // Change the floating point control register to use "round towards zero"
1573 // mode when truncating to an integer value.
1574 //
1575 MachineFunction *F = BB->getParent();
1576 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1577 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1578
1579 // Load the old value of the high byte of the control word...
1580 unsigned HighPartOfCW = MakeReg(MVT::i8);
1581 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1582 CWFrameIdx, 1);
1583
1584 // Set the high part to be round to zero...
1585 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1586 CWFrameIdx, 1).addImm(12);
1587
1588 // Reload the modified control word now...
1589 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1590
1591 // Restore the memory image of control word to original value
1592 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1593 CWFrameIdx, 1).addReg(HighPartOfCW);
1594
1595 // We don't have the facilities for directly storing byte sized data to
1596 // memory. Promote it to 16 bits. We also must promote unsigned values to
1597 // larger classes because we only have signed FP stores.
1598 MVT::ValueType StoreClass = Node->getValueType(0);
1599 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1600 switch (StoreClass) {
1601 case MVT::i8: StoreClass = MVT::i16; break;
1602 case MVT::i16: StoreClass = MVT::i32; break;
1603 case MVT::i32: StoreClass = MVT::i64; break;
1604 // The following treatment of cLong may not be perfectly right,
1605 // but it survives chains of casts of the form
1606 // double->ulong->double.
1607 case MVT::i64: StoreClass = MVT::i64; break;
1608 default: assert(0 && "Unknown store class!");
1609 }
1610
1611 // Spill the integer to memory and reload it from there.
1612 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1613 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1614
1615 switch (StoreClass) {
1616 default: assert(0 && "Unknown store class!");
1617 case MVT::i16:
1618 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1619 break;
1620 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001621 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001622 break;
1623 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001624 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001625 break;
1626 }
1627
1628 switch (Node->getValueType(0)) {
1629 default:
1630 assert(0 && "Unknown integer type!");
1631 case MVT::i64:
1632 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001633 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001634 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1635 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1636 case MVT::i32:
1637 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1638 break;
1639 case MVT::i16:
1640 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1641 break;
1642 case MVT::i8:
1643 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1644 break;
1645 }
1646
1647 // Reload the original control word now.
1648 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1649 return Result;
1650 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001651 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001652 Op0 = N.getOperand(0);
1653 Op1 = N.getOperand(1);
1654
Chris Lattner4ff348b2005-01-17 06:26:58 +00001655 if (isFoldableLoad(Op0, Op1)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001656 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001657 goto FoldAdd;
1658 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001659
Chris Lattner4ff348b2005-01-17 06:26:58 +00001660 if (isFoldableLoad(Op1, Op0)) {
1661 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001662 switch (N.getValueType()) {
1663 default: assert(0 && "Cannot add this type!");
1664 case MVT::i1:
1665 case MVT::i8: Opc = X86::ADD8rm; break;
1666 case MVT::i16: Opc = X86::ADD16rm; break;
1667 case MVT::i32: Opc = X86::ADD32rm; break;
1668 case MVT::f32: Opc = X86::FADD32m; break;
1669 case MVT::f64: Opc = X86::FADD64m; break;
1670 }
1671 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001672 EmitFoldedLoad(Op1, AM);
1673 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001674 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1675 return Result;
1676 }
1677
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001678 // See if we can codegen this as an LEA to fold operations together.
1679 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001680 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001681 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001682 MatchAddress(N, AM);
1683 ExprMap[N] = Result;
1684
1685 // If this is not just an add, emit the LEA. For a simple add (like
1686 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1687 // leave this as LEA, then peephole it to 'ADD' after two address elim
1688 // happens.
1689 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1690 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1691 X86AddressMode XAM = SelectAddrExprs(AM);
1692 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1693 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001694 }
1695 }
Chris Lattner11333092005-01-11 03:11:44 +00001696
Chris Lattnera5ade062005-01-11 21:19:59 +00001697 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001698 Opc = 0;
1699 if (CN->getValue() == 1) { // add X, 1 -> inc X
1700 switch (N.getValueType()) {
1701 default: assert(0 && "Cannot integer add this type!");
1702 case MVT::i8: Opc = X86::INC8r; break;
1703 case MVT::i16: Opc = X86::INC16r; break;
1704 case MVT::i32: Opc = X86::INC32r; break;
1705 }
1706 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1707 switch (N.getValueType()) {
1708 default: assert(0 && "Cannot integer add this type!");
1709 case MVT::i8: Opc = X86::DEC8r; break;
1710 case MVT::i16: Opc = X86::DEC16r; break;
1711 case MVT::i32: Opc = X86::DEC32r; break;
1712 }
1713 }
1714
1715 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001716 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001717 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1718 return Result;
1719 }
1720
1721 switch (N.getValueType()) {
1722 default: assert(0 && "Cannot add this type!");
1723 case MVT::i8: Opc = X86::ADD8ri; break;
1724 case MVT::i16: Opc = X86::ADD16ri; break;
1725 case MVT::i32: Opc = X86::ADD32ri; break;
1726 }
1727 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001728 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001729 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1730 return Result;
1731 }
1732 }
1733
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001734 switch (N.getValueType()) {
1735 default: assert(0 && "Cannot add this type!");
1736 case MVT::i8: Opc = X86::ADD8rr; break;
1737 case MVT::i16: Opc = X86::ADD16rr; break;
1738 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001739 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001740 }
Chris Lattner11333092005-01-11 03:11:44 +00001741
Chris Lattnera5ade062005-01-11 21:19:59 +00001742 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1743 Tmp1 = SelectExpr(Op0);
1744 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001745 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001746 Tmp2 = SelectExpr(Op1);
1747 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001748 }
1749
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001750 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1751 return Result;
1752 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001753 case ISD::MUL:
1754 case ISD::AND:
1755 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001756 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001757 static const unsigned SUBTab[] = {
1758 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1759 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1760 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1761 };
1762 static const unsigned MULTab[] = {
1763 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1764 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1765 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1766 };
1767 static const unsigned ANDTab[] = {
1768 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1769 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1770 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1771 };
1772 static const unsigned ORTab[] = {
1773 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1774 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1775 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1776 };
1777 static const unsigned XORTab[] = {
1778 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1779 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1780 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1781 };
1782
1783 Op0 = Node->getOperand(0);
1784 Op1 = Node->getOperand(1);
1785
Chris Lattner30ea1e92005-01-19 07:37:26 +00001786 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1787 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001788 return Result;
1789
1790 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001791 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1792 if (CN->isNullValue()) { // 0 - N -> neg N
1793 switch (N.getValueType()) {
1794 default: assert(0 && "Cannot sub this type!");
1795 case MVT::i1:
1796 case MVT::i8: Opc = X86::NEG8r; break;
1797 case MVT::i16: Opc = X86::NEG16r; break;
1798 case MVT::i32: Opc = X86::NEG32r; break;
1799 }
1800 Tmp1 = SelectExpr(N.getOperand(1));
1801 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1802 return Result;
1803 }
1804
Chris Lattnera5ade062005-01-11 21:19:59 +00001805 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1806 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001807 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001808 switch (N.getValueType()) {
1809 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001810 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001811 case MVT::i8: Opc = X86::NOT8r; break;
1812 case MVT::i16: Opc = X86::NOT16r; break;
1813 case MVT::i32: Opc = X86::NOT32r; break;
1814 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001815 if (Opc) {
1816 Tmp1 = SelectExpr(Op0);
1817 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1818 return Result;
1819 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001820 }
1821
Chris Lattner2a4e5082005-01-17 06:48:02 +00001822 // Fold common multiplies into LEA instructions.
1823 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1824 switch ((int)CN->getValue()) {
1825 default: break;
1826 case 3:
1827 case 5:
1828 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001829 // Remove N from exprmap so SelectAddress doesn't get confused.
1830 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001831 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001832 SelectAddress(N, AM);
1833 // Restore it to the map.
1834 ExprMap[N] = Result;
1835 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1836 return Result;
1837 }
1838 }
1839
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001840 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001841 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001842 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001843 case MVT::i8: Opc = 0; break;
1844 case MVT::i16: Opc = 1; break;
1845 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001846 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001847 switch (Node->getOpcode()) {
1848 default: assert(0 && "Unreachable!");
1849 case ISD::SUB: Opc = SUBTab[Opc]; break;
1850 case ISD::MUL: Opc = MULTab[Opc]; break;
1851 case ISD::AND: Opc = ANDTab[Opc]; break;
1852 case ISD::OR: Opc = ORTab[Opc]; break;
1853 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001854 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001855 if (Opc) { // Can't fold MUL:i8 R, imm
1856 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001857 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1858 return Result;
1859 }
1860 }
Chris Lattner11333092005-01-11 03:11:44 +00001861
Chris Lattner4ff348b2005-01-17 06:26:58 +00001862 if (isFoldableLoad(Op0, Op1))
Chris Lattnera5ade062005-01-11 21:19:59 +00001863 if (Node->getOpcode() != ISD::SUB) {
1864 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001865 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001866 } else {
1867 // Emit 'reverse' subract, with a memory operand.
1868 switch (N.getValueType()) {
1869 default: Opc = 0; break;
1870 case MVT::f32: Opc = X86::FSUBR32m; break;
1871 case MVT::f64: Opc = X86::FSUBR64m; break;
1872 }
1873 if (Opc) {
1874 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001875 EmitFoldedLoad(Op0, AM);
1876 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001877 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1878 return Result;
1879 }
1880 }
1881
Chris Lattner4ff348b2005-01-17 06:26:58 +00001882 if (isFoldableLoad(Op1, Op0)) {
1883 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001884 switch (N.getValueType()) {
1885 default: assert(0 && "Cannot operate on this type!");
1886 case MVT::i1:
1887 case MVT::i8: Opc = 5; break;
1888 case MVT::i16: Opc = 6; break;
1889 case MVT::i32: Opc = 7; break;
1890 case MVT::f32: Opc = 8; break;
1891 case MVT::f64: Opc = 9; break;
1892 }
1893 switch (Node->getOpcode()) {
1894 default: assert(0 && "Unreachable!");
1895 case ISD::SUB: Opc = SUBTab[Opc]; break;
1896 case ISD::MUL: Opc = MULTab[Opc]; break;
1897 case ISD::AND: Opc = ANDTab[Opc]; break;
1898 case ISD::OR: Opc = ORTab[Opc]; break;
1899 case ISD::XOR: Opc = XORTab[Opc]; break;
1900 }
1901
1902 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001903 EmitFoldedLoad(Op1, AM);
1904 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001905 if (Opc) {
1906 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1907 } else {
1908 assert(Node->getOpcode() == ISD::MUL &&
1909 N.getValueType() == MVT::i8 && "Unexpected situation!");
1910 // Must use the MUL instruction, which forces use of AL.
1911 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1912 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1913 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1914 }
1915 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001916 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001917
1918 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1919 Tmp1 = SelectExpr(Op0);
1920 Tmp2 = SelectExpr(Op1);
1921 } else {
1922 Tmp2 = SelectExpr(Op1);
1923 Tmp1 = SelectExpr(Op0);
1924 }
1925
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001926 switch (N.getValueType()) {
1927 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001928 case MVT::i1:
1929 case MVT::i8: Opc = 10; break;
1930 case MVT::i16: Opc = 11; break;
1931 case MVT::i32: Opc = 12; break;
1932 case MVT::f32: Opc = 13; break;
1933 case MVT::f64: Opc = 14; break;
1934 }
1935 switch (Node->getOpcode()) {
1936 default: assert(0 && "Unreachable!");
1937 case ISD::SUB: Opc = SUBTab[Opc]; break;
1938 case ISD::MUL: Opc = MULTab[Opc]; break;
1939 case ISD::AND: Opc = ANDTab[Opc]; break;
1940 case ISD::OR: Opc = ORTab[Opc]; break;
1941 case ISD::XOR: Opc = XORTab[Opc]; break;
1942 }
1943 if (Opc) {
1944 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1945 } else {
1946 assert(Node->getOpcode() == ISD::MUL &&
1947 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001948 // Must use the MUL instruction, which forces use of AL.
1949 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1950 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1951 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001952 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001953 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001954 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001955 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001956 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1957 Tmp2 = SelectExpr(N.getOperand(1));
1958 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001959 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001960 Tmp3 = SelectExpr(N.getOperand(2));
1961 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001962 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00001963 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
1964 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001965
1966 case ISD::SDIV:
1967 case ISD::UDIV:
1968 case ISD::SREM:
1969 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001970 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
1971 "We don't support this operator!");
1972
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001973 if (N.getOpcode() == ISD::SDIV)
1974 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1975 // FIXME: These special cases should be handled by the lowering impl!
1976 unsigned RHS = CN->getValue();
1977 bool isNeg = false;
1978 if ((int)RHS < 0) {
1979 isNeg = true;
1980 RHS = -RHS;
1981 }
1982 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1983 unsigned Log = log2(RHS);
1984 unsigned TmpReg = MakeReg(N.getValueType());
1985 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1986 switch (N.getValueType()) {
1987 default: assert("Unknown type to signed divide!");
1988 case MVT::i8:
1989 SAROpc = X86::SAR8ri;
1990 SHROpc = X86::SHR8ri;
1991 ADDOpc = X86::ADD8rr;
1992 NEGOpc = X86::NEG8r;
1993 break;
1994 case MVT::i16:
1995 SAROpc = X86::SAR16ri;
1996 SHROpc = X86::SHR16ri;
1997 ADDOpc = X86::ADD16rr;
1998 NEGOpc = X86::NEG16r;
1999 break;
2000 case MVT::i32:
2001 SAROpc = X86::SAR32ri;
2002 SHROpc = X86::SHR32ri;
2003 ADDOpc = X86::ADD32rr;
2004 NEGOpc = X86::NEG32r;
2005 break;
2006 }
Chris Lattner11333092005-01-11 03:11:44 +00002007 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002008 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2009 unsigned TmpReg2 = MakeReg(N.getValueType());
2010 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2011 unsigned TmpReg3 = MakeReg(N.getValueType());
2012 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
2013
2014 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2015 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2016 if (isNeg)
2017 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2018 return Result;
2019 }
2020 }
2021
Chris Lattner11333092005-01-11 03:11:44 +00002022 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2023 Tmp1 = SelectExpr(N.getOperand(0));
2024 Tmp2 = SelectExpr(N.getOperand(1));
2025 } else {
2026 Tmp2 = SelectExpr(N.getOperand(1));
2027 Tmp1 = SelectExpr(N.getOperand(0));
2028 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002029
2030 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2031 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2032 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2033 switch (N.getValueType()) {
2034 default: assert(0 && "Cannot sdiv this type!");
2035 case MVT::i8:
2036 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2037 LoReg = X86::AL;
2038 HiReg = X86::AH;
2039 MovOpcode = X86::MOV8rr;
2040 ClrOpcode = X86::MOV8ri;
2041 SExtOpcode = X86::CBW;
2042 break;
2043 case MVT::i16:
2044 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2045 LoReg = X86::AX;
2046 HiReg = X86::DX;
2047 MovOpcode = X86::MOV16rr;
2048 ClrOpcode = X86::MOV16ri;
2049 SExtOpcode = X86::CWD;
2050 break;
2051 case MVT::i32:
2052 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002053 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002054 HiReg = X86::EDX;
2055 MovOpcode = X86::MOV32rr;
2056 ClrOpcode = X86::MOV32ri;
2057 SExtOpcode = X86::CDQ;
2058 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002059 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002060 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002061 return Result;
2062 }
2063
2064 // Set up the low part.
2065 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2066
2067 if (isSigned) {
2068 // Sign extend the low part into the high part.
2069 BuildMI(BB, SExtOpcode, 0);
2070 } else {
2071 // Zero out the high part, effectively zero extending the input.
2072 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2073 }
2074
2075 // Emit the DIV/IDIV instruction.
2076 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
2077
2078 // Get the result of the divide or rem.
2079 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2080 return Result;
2081 }
2082
2083 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002084 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002085 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2086 switch (N.getValueType()) {
2087 default: assert(0 && "Cannot shift this type!");
2088 case MVT::i8: Opc = X86::ADD8rr; break;
2089 case MVT::i16: Opc = X86::ADD16rr; break;
2090 case MVT::i32: Opc = X86::ADD32rr; break;
2091 }
2092 Tmp1 = SelectExpr(N.getOperand(0));
2093 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2094 return Result;
2095 }
2096
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002097 switch (N.getValueType()) {
2098 default: assert(0 && "Cannot shift this type!");
2099 case MVT::i8: Opc = X86::SHL8ri; break;
2100 case MVT::i16: Opc = X86::SHL16ri; break;
2101 case MVT::i32: Opc = X86::SHL32ri; break;
2102 }
Chris Lattner11333092005-01-11 03:11:44 +00002103 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002104 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2105 return Result;
2106 }
Chris Lattner11333092005-01-11 03:11:44 +00002107
2108 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2109 Tmp1 = SelectExpr(N.getOperand(0));
2110 Tmp2 = SelectExpr(N.getOperand(1));
2111 } else {
2112 Tmp2 = SelectExpr(N.getOperand(1));
2113 Tmp1 = SelectExpr(N.getOperand(0));
2114 }
2115
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002116 switch (N.getValueType()) {
2117 default: assert(0 && "Cannot shift this type!");
2118 case MVT::i8 : Opc = X86::SHL8rCL; break;
2119 case MVT::i16: Opc = X86::SHL16rCL; break;
2120 case MVT::i32: Opc = X86::SHL32rCL; break;
2121 }
2122 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2123 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2124 return Result;
2125 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002126 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2127 switch (N.getValueType()) {
2128 default: assert(0 && "Cannot shift this type!");
2129 case MVT::i8: Opc = X86::SHR8ri; break;
2130 case MVT::i16: Opc = X86::SHR16ri; break;
2131 case MVT::i32: Opc = X86::SHR32ri; break;
2132 }
Chris Lattner11333092005-01-11 03:11:44 +00002133 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002134 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2135 return Result;
2136 }
Chris Lattner11333092005-01-11 03:11:44 +00002137
2138 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2139 Tmp1 = SelectExpr(N.getOperand(0));
2140 Tmp2 = SelectExpr(N.getOperand(1));
2141 } else {
2142 Tmp2 = SelectExpr(N.getOperand(1));
2143 Tmp1 = SelectExpr(N.getOperand(0));
2144 }
2145
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002146 switch (N.getValueType()) {
2147 default: assert(0 && "Cannot shift this type!");
2148 case MVT::i8 : Opc = X86::SHR8rCL; break;
2149 case MVT::i16: Opc = X86::SHR16rCL; break;
2150 case MVT::i32: Opc = X86::SHR32rCL; break;
2151 }
2152 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2153 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2154 return Result;
2155 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002156 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2157 switch (N.getValueType()) {
2158 default: assert(0 && "Cannot shift this type!");
2159 case MVT::i8: Opc = X86::SAR8ri; break;
2160 case MVT::i16: Opc = X86::SAR16ri; break;
2161 case MVT::i32: Opc = X86::SAR32ri; break;
2162 }
Chris Lattner11333092005-01-11 03:11:44 +00002163 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002164 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2165 return Result;
2166 }
Chris Lattner11333092005-01-11 03:11:44 +00002167
2168 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2169 Tmp1 = SelectExpr(N.getOperand(0));
2170 Tmp2 = SelectExpr(N.getOperand(1));
2171 } else {
2172 Tmp2 = SelectExpr(N.getOperand(1));
2173 Tmp1 = SelectExpr(N.getOperand(0));
2174 }
2175
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002176 switch (N.getValueType()) {
2177 default: assert(0 && "Cannot shift this type!");
2178 case MVT::i8 : Opc = X86::SAR8rCL; break;
2179 case MVT::i16: Opc = X86::SAR16rCL; break;
2180 case MVT::i32: Opc = X86::SAR32rCL; break;
2181 }
2182 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2183 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2184 return Result;
2185
2186 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002187 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002188 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2189 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2190 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002191 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002192 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002193 if (Result != 1) { // Generate the token
2194 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2195 assert(0 && "Load already emitted!?");
2196 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002197 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2198
Chris Lattner5188ad72005-01-08 19:28:19 +00002199 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002200 default: assert(0 && "Cannot load this type!");
2201 case MVT::i1:
2202 case MVT::i8: Opc = X86::MOV8rm; break;
2203 case MVT::i16: Opc = X86::MOV16rm; break;
2204 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002205 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2206 }
Chris Lattner11333092005-01-11 03:11:44 +00002207
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002208 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002209 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002210 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2211 } else {
2212 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002213
2214 SDOperand Chain = N.getOperand(0);
2215 SDOperand Address = N.getOperand(1);
2216 if (getRegPressure(Chain) > getRegPressure(Address)) {
2217 Select(Chain);
2218 SelectAddress(Address, AM);
2219 } else {
2220 SelectAddress(Address, AM);
2221 Select(Chain);
2222 }
2223
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002224 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2225 }
2226 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002227
2228 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2229 case ISD::ZEXTLOAD: {
2230 // Make sure we generate both values.
2231 if (Result != 1)
2232 ExprMap[N.getValue(1)] = 1; // Generate the token
2233 else
2234 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2235
Chris Lattnerda2ce112005-01-16 07:34:08 +00002236 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2237 if (Node->getValueType(0) == MVT::f64) {
2238 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2239 "Bad EXTLOAD!");
2240 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2241 CP->getIndex());
2242 return Result;
2243 }
2244
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002245 X86AddressMode AM;
2246 if (getRegPressure(Node->getOperand(0)) >
2247 getRegPressure(Node->getOperand(1))) {
2248 Select(Node->getOperand(0)); // chain
2249 SelectAddress(Node->getOperand(1), AM);
2250 } else {
2251 SelectAddress(Node->getOperand(1), AM);
2252 Select(Node->getOperand(0)); // chain
2253 }
2254
2255 switch (Node->getValueType(0)) {
2256 default: assert(0 && "Unknown type to sign extend to.");
2257 case MVT::f64:
2258 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2259 "Bad EXTLOAD!");
2260 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2261 break;
2262 case MVT::i32:
2263 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2264 default:
2265 assert(0 && "Bad zero extend!");
2266 case MVT::i1:
2267 case MVT::i8:
2268 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2269 break;
2270 case MVT::i16:
2271 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2272 break;
2273 }
2274 break;
2275 case MVT::i16:
2276 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2277 "Bad zero extend!");
2278 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2279 break;
2280 case MVT::i8:
2281 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2282 "Bad zero extend!");
2283 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2284 break;
2285 }
2286 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002287 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002288 case ISD::SEXTLOAD: {
2289 // Make sure we generate both values.
2290 if (Result != 1)
2291 ExprMap[N.getValue(1)] = 1; // Generate the token
2292 else
2293 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2294
2295 X86AddressMode AM;
2296 if (getRegPressure(Node->getOperand(0)) >
2297 getRegPressure(Node->getOperand(1))) {
2298 Select(Node->getOperand(0)); // chain
2299 SelectAddress(Node->getOperand(1), AM);
2300 } else {
2301 SelectAddress(Node->getOperand(1), AM);
2302 Select(Node->getOperand(0)); // chain
2303 }
2304
2305 switch (Node->getValueType(0)) {
2306 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2307 default: assert(0 && "Unknown type to sign extend to.");
2308 case MVT::i32:
2309 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2310 default:
2311 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2312 case MVT::i8:
2313 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2314 break;
2315 case MVT::i16:
2316 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2317 break;
2318 }
2319 break;
2320 case MVT::i16:
2321 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2322 "Cannot sign extend from bool!");
2323 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2324 break;
2325 }
2326 return Result;
2327 }
2328
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002329 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002330 // Generate both result values.
2331 if (Result != 1)
2332 ExprMap[N.getValue(1)] = 1; // Generate the token
2333 else
2334 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2335
2336 // FIXME: We are currently ignoring the requested alignment for handling
2337 // greater than the stack alignment. This will need to be revisited at some
2338 // point. Align = N.getOperand(2);
2339
2340 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2341 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2342 std::cerr << "Cannot allocate stack object with greater alignment than"
2343 << " the stack alignment yet!";
2344 abort();
2345 }
2346
2347 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002348 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002349 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2350 .addImm(CN->getValue());
2351 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002352 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2353 Select(N.getOperand(0));
2354 Tmp1 = SelectExpr(N.getOperand(1));
2355 } else {
2356 Tmp1 = SelectExpr(N.getOperand(1));
2357 Select(N.getOperand(0));
2358 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002359
2360 // Subtract size from stack pointer, thereby allocating some space.
2361 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2362 }
2363
2364 // Put a pointer to the space into the result register, by copying the stack
2365 // pointer.
2366 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2367 return Result;
2368
2369 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002370 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002371 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002372
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002373 if (GlobalAddressSDNode *GASD =
2374 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002375 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002376 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2377 } else if (ExternalSymbolSDNode *ESSDN =
2378 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002379 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002380 BuildMI(BB, X86::CALLpcrel32,
2381 1).addExternalSymbol(ESSDN->getSymbol(), true);
2382 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002383 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2384 Select(N.getOperand(0));
2385 Tmp1 = SelectExpr(N.getOperand(1));
2386 } else {
2387 Tmp1 = SelectExpr(N.getOperand(1));
2388 Select(N.getOperand(0));
2389 }
2390
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002391 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2392 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002393 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002394 default: assert(0 && "Unknown value type for call result!");
2395 case MVT::Other: return 1;
2396 case MVT::i1:
2397 case MVT::i8:
2398 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2399 break;
2400 case MVT::i16:
2401 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2402 break;
2403 case MVT::i32:
2404 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002405 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002406 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2407 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002408 case MVT::f64: // Floating-point return values live in %ST(0)
2409 ContainsFPCode = true;
2410 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2411 break;
2412 }
2413 return Result+N.ResNo;
2414 }
2415
2416 return 0;
2417}
2418
Chris Lattnere10269b2005-01-17 19:25:26 +00002419/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2420/// load/op/store instruction. If successful return true.
2421bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2422 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2423 SDOperand Chain = Node->getOperand(0);
2424 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002425 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002426
2427 // The chain has to be a load, the stored value must be an integer binary
2428 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002429 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002430 MVT::isFloatingPoint(StVal.getValueType()))
2431 return false;
2432
Chris Lattner5c659812005-01-17 22:10:42 +00002433 // Token chain must either be a factor node or the load to fold.
2434 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2435 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002436
Chris Lattner5c659812005-01-17 22:10:42 +00002437 SDOperand TheLoad;
2438
2439 // Check to see if there is a load from the same pointer that we're storing
2440 // to in either operand of the binop.
2441 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2442 StVal.getOperand(0).getOperand(1) == StPtr)
2443 TheLoad = StVal.getOperand(0);
2444 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2445 StVal.getOperand(1).getOperand(1) == StPtr)
2446 TheLoad = StVal.getOperand(1);
2447 else
2448 return false; // No matching load operand.
2449
2450 // We can only fold the load if there are no intervening side-effecting
2451 // operations. This means that the store uses the load as its token chain, or
2452 // there are only token factor nodes in between the store and load.
2453 if (Chain != TheLoad.getValue(1)) {
2454 // Okay, the other option is that we have a store referring to (possibly
2455 // nested) token factor nodes. For now, just try peeking through one level
2456 // of token factors to see if this is the case.
2457 bool ChainOk = false;
2458 if (Chain.getOpcode() == ISD::TokenFactor) {
2459 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2460 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2461 ChainOk = true;
2462 break;
2463 }
2464 }
2465
2466 if (!ChainOk) return false;
2467 }
2468
2469 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002470 return false;
2471
2472 // Make sure that one of the operands of the binop is the load, and that the
2473 // load folds into the binop.
2474 if (((StVal.getOperand(0) != TheLoad ||
2475 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2476 (StVal.getOperand(1) != TheLoad ||
2477 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2478 return false;
2479
2480 // Finally, check to see if this is one of the ops we can handle!
2481 static const unsigned ADDTAB[] = {
2482 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2483 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2484 };
2485 static const unsigned SUBTAB[] = {
2486 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2487 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2488 };
2489 static const unsigned ANDTAB[] = {
2490 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2491 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2492 };
2493 static const unsigned ORTAB[] = {
2494 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2495 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2496 };
2497 static const unsigned XORTAB[] = {
2498 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2499 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2500 };
2501 static const unsigned SHLTAB[] = {
2502 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2503 /*Have to put the reg in CL*/0, 0, 0,
2504 };
2505 static const unsigned SARTAB[] = {
2506 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2507 /*Have to put the reg in CL*/0, 0, 0,
2508 };
2509 static const unsigned SHRTAB[] = {
2510 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2511 /*Have to put the reg in CL*/0, 0, 0,
2512 };
2513
2514 const unsigned *TabPtr = 0;
2515 switch (StVal.getOpcode()) {
2516 default:
2517 std::cerr << "CANNOT [mem] op= val: ";
2518 StVal.Val->dump(); std::cerr << "\n";
2519 case ISD::MUL:
2520 case ISD::SDIV:
2521 case ISD::UDIV:
2522 case ISD::SREM:
2523 case ISD::UREM: return false;
2524
2525 case ISD::ADD: TabPtr = ADDTAB; break;
2526 case ISD::SUB: TabPtr = SUBTAB; break;
2527 case ISD::AND: TabPtr = ANDTAB; break;
2528 case ISD:: OR: TabPtr = ORTAB; break;
2529 case ISD::XOR: TabPtr = XORTAB; break;
2530 case ISD::SHL: TabPtr = SHLTAB; break;
2531 case ISD::SRA: TabPtr = SARTAB; break;
2532 case ISD::SRL: TabPtr = SHRTAB; break;
2533 }
2534
2535 // Handle: [mem] op= CST
2536 SDOperand Op0 = StVal.getOperand(0);
2537 SDOperand Op1 = StVal.getOperand(1);
2538 unsigned Opc;
2539 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2540 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2541 default: break;
2542 case MVT::i1:
2543 case MVT::i8: Opc = TabPtr[0]; break;
2544 case MVT::i16: Opc = TabPtr[1]; break;
2545 case MVT::i32: Opc = TabPtr[2]; break;
2546 }
2547
2548 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002549 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2550 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002551 Select(Chain);
2552
Chris Lattnere10269b2005-01-17 19:25:26 +00002553 X86AddressMode AM;
2554 if (getRegPressure(TheLoad.getOperand(0)) >
2555 getRegPressure(TheLoad.getOperand(1))) {
2556 Select(TheLoad.getOperand(0));
2557 SelectAddress(TheLoad.getOperand(1), AM);
2558 } else {
2559 SelectAddress(TheLoad.getOperand(1), AM);
2560 Select(TheLoad.getOperand(0));
2561 }
Chris Lattner5c659812005-01-17 22:10:42 +00002562
2563 if (StVal.getOpcode() == ISD::ADD) {
2564 if (CN->getValue() == 1) {
2565 switch (Op0.getValueType()) {
2566 default: break;
2567 case MVT::i8:
2568 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2569 return true;
2570 case MVT::i16: Opc = TabPtr[1];
2571 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2572 return true;
2573 case MVT::i32: Opc = TabPtr[2];
2574 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2575 return true;
2576 }
2577 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2578 switch (Op0.getValueType()) {
2579 default: break;
2580 case MVT::i8:
2581 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2582 return true;
2583 case MVT::i16: Opc = TabPtr[1];
2584 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2585 return true;
2586 case MVT::i32: Opc = TabPtr[2];
2587 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2588 return true;
2589 }
2590 }
2591 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002592
2593 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2594 return true;
2595 }
2596 }
2597
2598 // If we have [mem] = V op [mem], try to turn it into:
2599 // [mem] = [mem] op V.
2600 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2601 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2602 StVal.getOpcode() != ISD::SRL)
2603 std::swap(Op0, Op1);
2604
2605 if (Op0 != TheLoad) return false;
2606
2607 switch (Op0.getValueType()) {
2608 default: return false;
2609 case MVT::i1:
2610 case MVT::i8: Opc = TabPtr[3]; break;
2611 case MVT::i16: Opc = TabPtr[4]; break;
2612 case MVT::i32: Opc = TabPtr[5]; break;
2613 }
Chris Lattner5c659812005-01-17 22:10:42 +00002614
Chris Lattnerb422aea2005-01-18 17:35:28 +00002615 // Table entry doesn't exist?
2616 if (Opc == 0) return false;
2617
Chris Lattner4a108662005-01-18 03:51:59 +00002618 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2619 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002620 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002621 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002622
Chris Lattnere10269b2005-01-17 19:25:26 +00002623 X86AddressMode AM;
2624 SelectAddress(TheLoad.getOperand(1), AM);
2625 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002626 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002627 return true;
2628}
2629
2630
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002631void ISel::Select(SDOperand N) {
2632 unsigned Tmp1, Tmp2, Opc;
2633
2634 // FIXME: Disable for our current expansion model!
Chris Lattner4a108662005-01-18 03:51:59 +00002635 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002636 return; // Already selected.
2637
Chris Lattner989de032005-01-11 06:14:36 +00002638 SDNode *Node = N.Val;
2639
2640 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002641 default:
Chris Lattner989de032005-01-11 06:14:36 +00002642 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002643 assert(0 && "Node not handled yet!");
2644 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002645 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002646 if (Node->getNumOperands() == 2) {
2647 bool OneFirst =
2648 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2649 Select(Node->getOperand(OneFirst));
2650 Select(Node->getOperand(!OneFirst));
2651 } else {
2652 std::vector<std::pair<unsigned, unsigned> > OpsP;
2653 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2654 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2655 std::sort(OpsP.begin(), OpsP.end());
2656 std::reverse(OpsP.begin(), OpsP.end());
2657 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2658 Select(Node->getOperand(OpsP[i].second));
2659 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002660 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002661 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002662 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2663 Select(N.getOperand(0));
2664 Tmp1 = SelectExpr(N.getOperand(1));
2665 } else {
2666 Tmp1 = SelectExpr(N.getOperand(1));
2667 Select(N.getOperand(0));
2668 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002669 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002670
2671 if (Tmp1 != Tmp2) {
2672 switch (N.getOperand(1).getValueType()) {
2673 default: assert(0 && "Invalid type for operation!");
2674 case MVT::i1:
2675 case MVT::i8: Opc = X86::MOV8rr; break;
2676 case MVT::i16: Opc = X86::MOV16rr; break;
2677 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002678 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002679 }
2680 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2681 }
2682 return;
2683 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002684 switch (N.getNumOperands()) {
2685 default:
2686 assert(0 && "Unknown return instruction!");
2687 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002688 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2689 N.getOperand(2).getValueType() == MVT::i32 &&
2690 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002691 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2692 Tmp1 = SelectExpr(N.getOperand(1));
2693 Tmp2 = SelectExpr(N.getOperand(2));
2694 } else {
2695 Tmp2 = SelectExpr(N.getOperand(2));
2696 Tmp1 = SelectExpr(N.getOperand(1));
2697 }
2698 Select(N.getOperand(0));
2699
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002700 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2701 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2702 // Declare that EAX & EDX are live on exit.
2703 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2704 .addReg(X86::ESP);
2705 break;
2706 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002707 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2708 Select(N.getOperand(0));
2709 Tmp1 = SelectExpr(N.getOperand(1));
2710 } else {
2711 Tmp1 = SelectExpr(N.getOperand(1));
2712 Select(N.getOperand(0));
2713 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002714 switch (N.getOperand(1).getValueType()) {
2715 default: assert(0 && "All other types should have been promoted!!");
2716 case MVT::f64:
2717 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2718 // Declare that top-of-stack is live on exit
2719 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2720 break;
2721 case MVT::i32:
2722 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2723 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2724 break;
2725 }
2726 break;
2727 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002728 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002729 break;
2730 }
2731 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2732 return;
2733 case ISD::BR: {
2734 Select(N.getOperand(0));
2735 MachineBasicBlock *Dest =
2736 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2737 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2738 return;
2739 }
2740
2741 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002742 MachineBasicBlock *Dest =
2743 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002744
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002745 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2746 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002747 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2748 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2749 Select(N.getOperand(0));
2750 Tmp1 = SelectExpr(N.getOperand(1));
2751 } else {
2752 Tmp1 = SelectExpr(N.getOperand(1));
2753 Select(N.getOperand(0));
2754 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002755 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2756 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2757 }
Chris Lattner11333092005-01-11 03:11:44 +00002758
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002759 return;
2760 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002761
Chris Lattner4df0de92005-01-17 00:00:33 +00002762 case ISD::LOAD:
2763 // If this load could be folded into the only using instruction, and if it
2764 // is safe to emit the instruction here, try to do so now.
2765 if (Node->hasNUsesOfValue(1, 0)) {
2766 SDOperand TheVal = N.getValue(0);
2767 SDNode *User = 0;
2768 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2769 assert(UI != Node->use_end() && "Didn't find use!");
2770 SDNode *UN = *UI;
2771 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2772 if (UN->getOperand(i) == TheVal) {
2773 User = UN;
2774 goto FoundIt;
2775 }
2776 }
2777 FoundIt:
2778 // Only handle unary operators right now.
2779 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00002780 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002781 SelectExpr(SDOperand(User, 0));
2782 return;
2783 }
2784 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002785 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002786 SelectExpr(N);
2787 return;
2788
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002789 case ISD::EXTLOAD:
2790 case ISD::SEXTLOAD:
2791 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002792 case ISD::CALL:
2793 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002794 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002795 SelectExpr(N);
2796 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002797
2798 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2799 // On X86, we can represent all types except for Bool and Float natively.
2800 X86AddressMode AM;
2801 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002802 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2803 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2804 && "Unsupported TRUNCSTORE for this target!");
2805
2806 if (StoredTy == MVT::i16) {
2807 // FIXME: This is here just to allow testing. X86 doesn't really have a
2808 // TRUNCSTORE i16 operation, but this is required for targets that do not
2809 // have 16-bit integer registers. We occasionally disable 16-bit integer
2810 // registers to test the promotion code.
2811 Select(N.getOperand(0));
2812 Tmp1 = SelectExpr(N.getOperand(1));
2813 SelectAddress(N.getOperand(2), AM);
2814
2815 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2816 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2817 return;
2818 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002819
2820 // Store of constant bool?
2821 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2822 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2823 Select(N.getOperand(0));
2824 SelectAddress(N.getOperand(2), AM);
2825 } else {
2826 SelectAddress(N.getOperand(2), AM);
2827 Select(N.getOperand(0));
2828 }
2829 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2830 return;
2831 }
2832
2833 switch (StoredTy) {
2834 default: assert(0 && "Cannot truncstore this type!");
2835 case MVT::i1: Opc = X86::MOV8mr; break;
2836 case MVT::f32: Opc = X86::FST32m; break;
2837 }
2838
2839 std::vector<std::pair<unsigned, unsigned> > RP;
2840 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2841 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2842 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2843 std::sort(RP.begin(), RP.end());
2844
2845 for (unsigned i = 0; i != 3; ++i)
2846 switch (RP[2-i].second) {
2847 default: assert(0 && "Unknown operand number!");
2848 case 0: Select(N.getOperand(0)); break;
2849 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2850 case 2: SelectAddress(N.getOperand(2), AM); break;
2851 }
2852
2853 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2854 return;
2855 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002856 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002857 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002858
2859 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2860 Opc = 0;
2861 switch (CN->getValueType(0)) {
2862 default: assert(0 && "Invalid type for operation!");
2863 case MVT::i1:
2864 case MVT::i8: Opc = X86::MOV8mi; break;
2865 case MVT::i16: Opc = X86::MOV16mi; break;
2866 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002867 case MVT::f64: break;
2868 }
2869 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002870 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2871 Select(N.getOperand(0));
2872 SelectAddress(N.getOperand(2), AM);
2873 } else {
2874 SelectAddress(N.getOperand(2), AM);
2875 Select(N.getOperand(0));
2876 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002877 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2878 return;
2879 }
2880 }
Chris Lattner837caa72005-01-11 23:21:30 +00002881
2882 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00002883 if (TryToFoldLoadOpStore(Node))
2884 return;
Chris Lattner837caa72005-01-11 23:21:30 +00002885
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002886 switch (N.getOperand(1).getValueType()) {
2887 default: assert(0 && "Cannot store this type!");
2888 case MVT::i1:
2889 case MVT::i8: Opc = X86::MOV8mr; break;
2890 case MVT::i16: Opc = X86::MOV16mr; break;
2891 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002892 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002893 }
Chris Lattner11333092005-01-11 03:11:44 +00002894
2895 std::vector<std::pair<unsigned, unsigned> > RP;
2896 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2897 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2898 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2899 std::sort(RP.begin(), RP.end());
2900
2901 for (unsigned i = 0; i != 3; ++i)
2902 switch (RP[2-i].second) {
2903 default: assert(0 && "Unknown operand number!");
2904 case 0: Select(N.getOperand(0)); break;
2905 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002906 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002907 }
2908
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002909 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2910 return;
2911 }
2912 case ISD::ADJCALLSTACKDOWN:
2913 case ISD::ADJCALLSTACKUP:
2914 Select(N.getOperand(0));
2915 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2916
2917 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2918 X86::ADJCALLSTACKUP;
2919 BuildMI(BB, Opc, 1).addImm(Tmp1);
2920 return;
Chris Lattner989de032005-01-11 06:14:36 +00002921 case ISD::MEMSET: {
2922 Select(N.getOperand(0)); // Select the chain.
2923 unsigned Align =
2924 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2925 if (Align == 0) Align = 1;
2926
2927 // Turn the byte code into # iterations
2928 unsigned CountReg;
2929 unsigned Opcode;
2930 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2931 unsigned Val = ValC->getValue() & 255;
2932
2933 // If the value is a constant, then we can potentially use larger sets.
2934 switch (Align & 3) {
2935 case 2: // WORD aligned
2936 CountReg = MakeReg(MVT::i32);
2937 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2938 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2939 } else {
2940 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2941 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2942 }
2943 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2944 Opcode = X86::REP_STOSW;
2945 break;
2946 case 0: // DWORD aligned
2947 CountReg = MakeReg(MVT::i32);
2948 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2949 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2950 } else {
2951 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2952 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2953 }
2954 Val = (Val << 8) | Val;
2955 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2956 Opcode = X86::REP_STOSD;
2957 break;
2958 default: // BYTE aligned
2959 CountReg = SelectExpr(Node->getOperand(3));
2960 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2961 Opcode = X86::REP_STOSB;
2962 break;
2963 }
2964 } else {
2965 // If it's not a constant value we are storing, just fall back. We could
2966 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2967 unsigned ValReg = SelectExpr(Node->getOperand(2));
2968 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2969 CountReg = SelectExpr(Node->getOperand(3));
2970 Opcode = X86::REP_STOSB;
2971 }
2972
2973 // No matter what the alignment is, we put the source in ESI, the
2974 // destination in EDI, and the count in ECX.
2975 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2976 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2977 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2978 BuildMI(BB, Opcode, 0);
2979 return;
2980 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002981 case ISD::MEMCPY:
2982 Select(N.getOperand(0)); // Select the chain.
2983 unsigned Align =
2984 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2985 if (Align == 0) Align = 1;
2986
2987 // Turn the byte code into # iterations
2988 unsigned CountReg;
2989 unsigned Opcode;
2990 switch (Align & 3) {
2991 case 2: // WORD aligned
2992 CountReg = MakeReg(MVT::i32);
2993 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2994 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2995 } else {
2996 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2997 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2998 }
2999 Opcode = X86::REP_MOVSW;
3000 break;
3001 case 0: // DWORD aligned
3002 CountReg = MakeReg(MVT::i32);
3003 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3004 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3005 } else {
3006 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3007 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3008 }
3009 Opcode = X86::REP_MOVSD;
3010 break;
3011 default: // BYTE aligned
3012 CountReg = SelectExpr(Node->getOperand(3));
3013 Opcode = X86::REP_MOVSB;
3014 break;
3015 }
3016
3017 // No matter what the alignment is, we put the source in ESI, the
3018 // destination in EDI, and the count in ECX.
3019 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3020 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3021 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3022 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3023 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3024 BuildMI(BB, Opcode, 0);
3025 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003026 }
3027 assert(0 && "Should not be reached!");
3028}
3029
3030
3031/// createX86PatternInstructionSelector - This pass converts an LLVM function
3032/// into a machine code representation using pattern matching and a machine
3033/// description file.
3034///
3035FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
3036 return new ISel(TM);
3037}