blob: 2219291905bb6ecfca86200510fe8b8941a6a563 [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 Lattnerbf52d492005-01-20 16:50:16 +0000493/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
494/// The DAG cannot have cycles in it, by definition, so the visited set is not
495/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
496/// reuse, so it prevents exponential cases.
497///
498static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
499 std::set<SDNode*> &Visited) {
500 if (N == Op) return true; // Found it.
501 SDNode *Node = N.Val;
502 if (Node->getNumOperands() == 0) return false; // Leaf?
503 if (!Visited.insert(Node).second) return false; // Already visited?
504
505 // Recurse for the first N-1 operands.
506 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
507 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
508 return true;
509
510 // Tail recurse for the last operand.
511 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
512}
513
Chris Lattner98a8ba02005-01-18 01:06:26 +0000514X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
515 X86AddressMode Result;
516
517 // If we need to emit two register operands, emit the one with the highest
518 // register pressure first.
519 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
520 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000521 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000522 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000523 std::set<SDNode*> Visited;
524 EmitBaseThenIndex = true;
525 // If Base ends up pointing to Index, we must emit index first. This is
526 // because of the way we fold loads, we may end up doing bad things with
527 // the folded add.
528 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
529 EmitBaseThenIndex = false;
530 } else {
531 std::set<SDNode*> Visited;
532 EmitBaseThenIndex = false;
533 // If Base ends up pointing to Index, we must emit index first. This is
534 // because of the way we fold loads, we may end up doing bad things with
535 // the folded add.
536 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
537 EmitBaseThenIndex = true;
538 }
539
540 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000541 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
542 Result.IndexReg = SelectExpr(IAM.IndexReg);
543 } else {
544 Result.IndexReg = SelectExpr(IAM.IndexReg);
545 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
546 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000547
Chris Lattner98a8ba02005-01-18 01:06:26 +0000548 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
549 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
550 } else if (IAM.IndexReg.Val) {
551 Result.IndexReg = SelectExpr(IAM.IndexReg);
552 }
553
554 switch (IAM.BaseType) {
555 case X86ISelAddressMode::RegBase:
556 Result.BaseType = X86AddressMode::RegBase;
557 break;
558 case X86ISelAddressMode::FrameIndexBase:
559 Result.BaseType = X86AddressMode::FrameIndexBase;
560 Result.Base.FrameIndex = IAM.Base.FrameIndex;
561 break;
562 default:
563 assert(0 && "Unknown base type!");
564 break;
565 }
566 Result.Scale = IAM.Scale;
567 Result.Disp = IAM.Disp;
568 Result.GV = IAM.GV;
569 return Result;
570}
571
572/// SelectAddress - Pattern match the maximal addressing mode for this node and
573/// emit all of the leaf registers.
574void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
575 X86ISelAddressMode IAM;
576 MatchAddress(N, IAM);
577 AM = SelectAddrExprs(IAM);
578}
579
580/// MatchAddress - Add the specified node to the specified addressing mode,
581/// returning true if it cannot be done. This just pattern matches for the
582/// addressing mode, it does not cause any code to be emitted. For that, use
583/// SelectAddress.
584bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000585 switch (N.getOpcode()) {
586 default: break;
587 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000588 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
589 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000590 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
591 return false;
592 }
593 break;
594 case ISD::GlobalAddress:
595 if (AM.GV == 0) {
596 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
597 return false;
598 }
599 break;
600 case ISD::Constant:
601 AM.Disp += cast<ConstantSDNode>(N)->getValue();
602 return false;
603 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000604 // We might have folded the load into this shift, so don't regen the value
605 // if so.
606 if (ExprMap.count(N)) break;
607
Chris Lattner98a8ba02005-01-18 01:06:26 +0000608 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000609 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
610 unsigned Val = CN->getValue();
611 if (Val == 1 || Val == 2 || Val == 3) {
612 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000613 SDOperand ShVal = N.Val->getOperand(0);
614
615 // Okay, we know that we have a scale by now. However, if the scaled
616 // value is an add of something and a constant, we can fold the
617 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000618 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000619 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000620 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000621 ConstantSDNode *AddVal =
622 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
623 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000624 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000625 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000626 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000627 return false;
628 }
629 }
630 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000631 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000632 // We might have folded the load into this mul, so don't regen the value if
633 // so.
634 if (ExprMap.count(N)) break;
635
Chris Lattner947d5442005-01-11 19:37:02 +0000636 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000637 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
638 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000639 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
640 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
641 AM.Scale = unsigned(CN->getValue())-1;
642
643 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000644 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000645
646 // Okay, we know that we have a scale by now. However, if the scaled
647 // value is an add of something and a constant, we can fold the
648 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000649 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000650 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000651 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000652 ConstantSDNode *AddVal =
653 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
654 AM.Disp += AddVal->getValue() * CN->getValue();
655 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000656 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000657 }
658
659 AM.IndexReg = AM.Base.Reg = Reg;
660 return false;
661 }
662 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000663
664 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000665 // We might have folded the load into this mul, so don't regen the value if
666 // so.
667 if (ExprMap.count(N)) break;
668
Chris Lattner98a8ba02005-01-18 01:06:26 +0000669 X86ISelAddressMode Backup = AM;
670 if (!MatchAddress(N.Val->getOperand(0), AM) &&
671 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000672 return false;
673 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000674 if (!MatchAddress(N.Val->getOperand(1), AM) &&
675 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000676 return false;
677 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000678 break;
679 }
680 }
681
Chris Lattnera95589b2005-01-11 04:40:19 +0000682 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000683 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000684 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000685 if (AM.IndexReg.Val == 0) {
686 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000687 AM.Scale = 1;
688 return false;
689 }
690
691 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000692 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000693 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000694
695 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000696 AM.BaseType = X86ISelAddressMode::RegBase;
697 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000698 return false;
699}
700
701/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
702/// assuming that the temporary registers are in the 8-bit register class.
703///
704/// Tmp1 = setcc1
705/// Tmp2 = setcc2
706/// DestReg = logicalop Tmp1, Tmp2
707///
708static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
709 unsigned SetCC2, unsigned LogicalOp,
710 unsigned DestReg) {
711 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
712 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
713 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
714 BuildMI(BB, SetCC1, 0, Tmp1);
715 BuildMI(BB, SetCC2, 0, Tmp2);
716 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
717}
718
719/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
720/// condition codes match the specified SetCCOpcode. Note that some conditions
721/// require multiple instructions to generate the correct value.
722static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
723 ISD::CondCode SetCCOpcode, bool isFP) {
724 unsigned Opc;
725 if (!isFP) {
726 switch (SetCCOpcode) {
727 default: assert(0 && "Illegal integer SetCC!");
728 case ISD::SETEQ: Opc = X86::SETEr; break;
729 case ISD::SETGT: Opc = X86::SETGr; break;
730 case ISD::SETGE: Opc = X86::SETGEr; break;
731 case ISD::SETLT: Opc = X86::SETLr; break;
732 case ISD::SETLE: Opc = X86::SETLEr; break;
733 case ISD::SETNE: Opc = X86::SETNEr; break;
734 case ISD::SETULT: Opc = X86::SETBr; break;
735 case ISD::SETUGT: Opc = X86::SETAr; break;
736 case ISD::SETULE: Opc = X86::SETBEr; break;
737 case ISD::SETUGE: Opc = X86::SETAEr; break;
738 }
739 } else {
740 // On a floating point condition, the flags are set as follows:
741 // ZF PF CF op
742 // 0 | 0 | 0 | X > Y
743 // 0 | 0 | 1 | X < Y
744 // 1 | 0 | 0 | X == Y
745 // 1 | 1 | 1 | unordered
746 //
747 switch (SetCCOpcode) {
748 default: assert(0 && "Invalid FP setcc!");
749 case ISD::SETUEQ:
750 case ISD::SETEQ:
751 Opc = X86::SETEr; // True if ZF = 1
752 break;
753 case ISD::SETOGT:
754 case ISD::SETGT:
755 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
756 break;
757 case ISD::SETOGE:
758 case ISD::SETGE:
759 Opc = X86::SETAEr; // True if CF = 0
760 break;
761 case ISD::SETULT:
762 case ISD::SETLT:
763 Opc = X86::SETBr; // True if CF = 1
764 break;
765 case ISD::SETULE:
766 case ISD::SETLE:
767 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
768 break;
769 case ISD::SETONE:
770 case ISD::SETNE:
771 Opc = X86::SETNEr; // True if ZF = 0
772 break;
773 case ISD::SETUO:
774 Opc = X86::SETPr; // True if PF = 1
775 break;
776 case ISD::SETO:
777 Opc = X86::SETNPr; // True if PF = 0
778 break;
779 case ISD::SETOEQ: // !PF & ZF
780 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
781 return;
782 case ISD::SETOLT: // !PF & CF
783 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
784 return;
785 case ISD::SETOLE: // !PF & (CF || ZF)
786 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
787 return;
788 case ISD::SETUGT: // PF | (!ZF & !CF)
789 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
790 return;
791 case ISD::SETUGE: // PF | !CF
792 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
793 return;
794 case ISD::SETUNE: // PF | !ZF
795 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
796 return;
797 }
798 }
799 BuildMI(BB, Opc, 0, DestReg);
800}
801
802
803/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
804/// the Dest block if the Cond condition is true. If we cannot fold this
805/// condition into the branch, return true.
806///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000807bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
808 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000809 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
810 // B) using two conditional branches instead of one condbr, two setcc's, and
811 // an or.
812 if ((Cond.getOpcode() == ISD::OR ||
813 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
814 // And and or set the flags for us, so there is no need to emit a TST of the
815 // result. It is only safe to do this if there is only a single use of the
816 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000817 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000818 SelectExpr(Cond);
819 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
820 return false;
821 }
822
823 // Codegen br not C -> JE.
824 if (Cond.getOpcode() == ISD::XOR)
825 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
826 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000827 unsigned CondR;
828 if (getRegPressure(Chain) > getRegPressure(Cond)) {
829 Select(Chain);
830 CondR = SelectExpr(Cond.Val->getOperand(0));
831 } else {
832 CondR = SelectExpr(Cond.Val->getOperand(0));
833 Select(Chain);
834 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000835 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
836 BuildMI(BB, X86::JE, 1).addMBB(Dest);
837 return false;
838 }
839
840 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
841 if (SetCC == 0)
842 return true; // Can only handle simple setcc's so far.
843
844 unsigned Opc;
845
846 // Handle integer conditions first.
847 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
848 switch (SetCC->getCondition()) {
849 default: assert(0 && "Illegal integer SetCC!");
850 case ISD::SETEQ: Opc = X86::JE; break;
851 case ISD::SETGT: Opc = X86::JG; break;
852 case ISD::SETGE: Opc = X86::JGE; break;
853 case ISD::SETLT: Opc = X86::JL; break;
854 case ISD::SETLE: Opc = X86::JLE; break;
855 case ISD::SETNE: Opc = X86::JNE; break;
856 case ISD::SETULT: Opc = X86::JB; break;
857 case ISD::SETUGT: Opc = X86::JA; break;
858 case ISD::SETULE: Opc = X86::JBE; break;
859 case ISD::SETUGE: Opc = X86::JAE; break;
860 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000861 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000862 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000863 BuildMI(BB, Opc, 1).addMBB(Dest);
864 return false;
865 }
866
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000867 unsigned Opc2 = 0; // Second branch if needed.
868
869 // On a floating point condition, the flags are set as follows:
870 // ZF PF CF op
871 // 0 | 0 | 0 | X > Y
872 // 0 | 0 | 1 | X < Y
873 // 1 | 0 | 0 | X == Y
874 // 1 | 1 | 1 | unordered
875 //
876 switch (SetCC->getCondition()) {
877 default: assert(0 && "Invalid FP setcc!");
878 case ISD::SETUEQ:
879 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
880 case ISD::SETOGT:
881 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
882 case ISD::SETOGE:
883 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
884 case ISD::SETULT:
885 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
886 case ISD::SETULE:
887 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
888 case ISD::SETONE:
889 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
890 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
891 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
892 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
893 Opc = X86::JA; // ZF = 0 & CF = 0
894 Opc2 = X86::JP; // PF = 1
895 break;
896 case ISD::SETUGE: // PF = 1 | CF = 0
897 Opc = X86::JAE; // CF = 0
898 Opc2 = X86::JP; // PF = 1
899 break;
900 case ISD::SETUNE: // PF = 1 | ZF = 0
901 Opc = X86::JNE; // ZF = 0
902 Opc2 = X86::JP; // PF = 1
903 break;
904 case ISD::SETOEQ: // PF = 0 & ZF = 1
905 //X86::JNP, X86::JE
906 //X86::AND8rr
907 return true; // FIXME: Emit more efficient code for this branch.
908 case ISD::SETOLT: // PF = 0 & CF = 1
909 //X86::JNP, X86::JB
910 //X86::AND8rr
911 return true; // FIXME: Emit more efficient code for this branch.
912 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
913 //X86::JNP, X86::JBE
914 //X86::AND8rr
915 return true; // FIXME: Emit more efficient code for this branch.
916 }
917
Chris Lattner6c07aee2005-01-11 04:06:27 +0000918 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000919 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000920 BuildMI(BB, Opc, 1).addMBB(Dest);
921 if (Opc2)
922 BuildMI(BB, Opc2, 1).addMBB(Dest);
923 return false;
924}
925
Chris Lattner24aad1b2005-01-10 22:10:13 +0000926/// EmitSelectCC - Emit code into BB that performs a select operation between
927/// the two registers RTrue and RFalse, generating a result into RDest. Return
928/// true if the fold cannot be performed.
929///
930void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
931 unsigned RTrue, unsigned RFalse, unsigned RDest) {
932 enum Condition {
933 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
934 NOT_SET
935 } CondCode = NOT_SET;
936
937 static const unsigned CMOVTAB16[] = {
938 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
939 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
940 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
941 };
942 static const unsigned CMOVTAB32[] = {
943 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
944 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
945 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
946 };
947 static const unsigned CMOVTABFP[] = {
948 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
949 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
950 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
951 };
952
953 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
954 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
955 switch (SetCC->getCondition()) {
956 default: assert(0 && "Unknown integer comparison!");
957 case ISD::SETEQ: CondCode = EQ; break;
958 case ISD::SETGT: CondCode = GT; break;
959 case ISD::SETGE: CondCode = GE; break;
960 case ISD::SETLT: CondCode = LT; break;
961 case ISD::SETLE: CondCode = LE; break;
962 case ISD::SETNE: CondCode = NE; break;
963 case ISD::SETULT: CondCode = B; break;
964 case ISD::SETUGT: CondCode = A; break;
965 case ISD::SETULE: CondCode = BE; break;
966 case ISD::SETUGE: CondCode = AE; break;
967 }
968 } else {
969 // On a floating point condition, the flags are set as follows:
970 // ZF PF CF op
971 // 0 | 0 | 0 | X > Y
972 // 0 | 0 | 1 | X < Y
973 // 1 | 0 | 0 | X == Y
974 // 1 | 1 | 1 | unordered
975 //
976 switch (SetCC->getCondition()) {
977 default: assert(0 && "Unknown FP comparison!");
978 case ISD::SETUEQ:
979 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
980 case ISD::SETOGT:
981 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
982 case ISD::SETOGE:
983 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
984 case ISD::SETULT:
985 case ISD::SETLT: CondCode = B; break; // True if CF = 1
986 case ISD::SETULE:
987 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
988 case ISD::SETONE:
989 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
990 case ISD::SETUO: CondCode = P; break; // True if PF = 1
991 case ISD::SETO: CondCode = NP; break; // True if PF = 0
992 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
993 case ISD::SETUGE: // PF = 1 | CF = 0
994 case ISD::SETUNE: // PF = 1 | ZF = 0
995 case ISD::SETOEQ: // PF = 0 & ZF = 1
996 case ISD::SETOLT: // PF = 0 & CF = 1
997 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
998 // We cannot emit this comparison as a single cmov.
999 break;
1000 }
1001 }
1002 }
1003
1004 unsigned Opc = 0;
1005 if (CondCode != NOT_SET) {
1006 switch (SVT) {
1007 default: assert(0 && "Cannot select this type!");
1008 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1009 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001010 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001011 }
1012 }
1013
1014 // Finally, if we weren't able to fold this, just emit the condition and test
1015 // it.
1016 if (CondCode == NOT_SET || Opc == 0) {
1017 // Get the condition into the zero flag.
1018 unsigned CondReg = SelectExpr(Cond);
1019 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1020
1021 switch (SVT) {
1022 default: assert(0 && "Cannot select this type!");
1023 case MVT::i16: Opc = X86::CMOVE16rr; break;
1024 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001025 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001026 }
1027 } else {
1028 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001029 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001030 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001031 }
1032 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1033}
1034
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001035void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001036 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001037 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1038 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001039 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001040 switch (RHS.getValueType()) {
1041 default: break;
1042 case MVT::i1:
1043 case MVT::i8: Opc = X86::CMP8mi; break;
1044 case MVT::i16: Opc = X86::CMP16mi; break;
1045 case MVT::i32: Opc = X86::CMP32mi; break;
1046 }
1047 if (Opc) {
1048 X86AddressMode AM;
1049 EmitFoldedLoad(LHS, AM);
1050 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1051 return;
1052 }
1053 }
1054
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001055 switch (RHS.getValueType()) {
1056 default: break;
1057 case MVT::i1:
1058 case MVT::i8: Opc = X86::CMP8ri; break;
1059 case MVT::i16: Opc = X86::CMP16ri; break;
1060 case MVT::i32: Opc = X86::CMP32ri; break;
1061 }
1062 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001063 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001064 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1065 return;
1066 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001067 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1068 if (CN->isExactlyValue(+0.0) ||
1069 CN->isExactlyValue(-0.0)) {
1070 unsigned Reg = SelectExpr(LHS);
1071 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1072 BuildMI(BB, X86::FNSTSW8r, 0);
1073 BuildMI(BB, X86::SAHF, 1);
1074 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001075 }
1076
Chris Lattneref6806c2005-01-12 02:02:48 +00001077 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001078 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001079 switch (RHS.getValueType()) {
1080 default: break;
1081 case MVT::i1:
1082 case MVT::i8: Opc = X86::CMP8mr; break;
1083 case MVT::i16: Opc = X86::CMP16mr; break;
1084 case MVT::i32: Opc = X86::CMP32mr; break;
1085 }
1086 if (Opc) {
1087 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001088 EmitFoldedLoad(LHS, AM);
1089 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001090 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1091 return;
1092 }
1093 }
1094
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001095 switch (LHS.getValueType()) {
1096 default: assert(0 && "Cannot compare this value!");
1097 case MVT::i1:
1098 case MVT::i8: Opc = X86::CMP8rr; break;
1099 case MVT::i16: Opc = X86::CMP16rr; break;
1100 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001101 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001102 }
Chris Lattner11333092005-01-11 03:11:44 +00001103 unsigned Tmp1, Tmp2;
1104 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1105 Tmp1 = SelectExpr(LHS);
1106 Tmp2 = SelectExpr(RHS);
1107 } else {
1108 Tmp2 = SelectExpr(RHS);
1109 Tmp1 = SelectExpr(LHS);
1110 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001111 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1112}
1113
Chris Lattnera5ade062005-01-11 21:19:59 +00001114/// isFoldableLoad - Return true if this is a load instruction that can safely
1115/// be folded into an operation that uses it.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001116bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001117 if (Op.getOpcode() != ISD::LOAD ||
1118 // FIXME: currently can't fold constant pool indexes.
1119 isa<ConstantPoolSDNode>(Op.getOperand(1)))
1120 return false;
1121
1122 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001123 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1124 if (ExprMap.count(Op.getValue(1))) return false;
1125 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001126 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001127
Chris Lattner4ff348b2005-01-17 06:26:58 +00001128 // If there is not just one use of its value, we cannot fold.
1129 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1130
1131 // Finally, we cannot fold the load into the operation if this would induce a
1132 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1133 // operand of the operation we are folding the load into) can possible use the
1134 // chain node defined by the load.
1135 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1136 std::set<SDNode*> Visited;
1137 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1138 return false;
1139 }
1140 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001141}
1142
Chris Lattner4ff348b2005-01-17 06:26:58 +00001143
Chris Lattnera5ade062005-01-11 21:19:59 +00001144/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1145/// and compute the address being loaded into AM.
1146void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1147 SDOperand Chain = Op.getOperand(0);
1148 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001149
Chris Lattnera5ade062005-01-11 21:19:59 +00001150 if (getRegPressure(Chain) > getRegPressure(Address)) {
1151 Select(Chain);
1152 SelectAddress(Address, AM);
1153 } else {
1154 SelectAddress(Address, AM);
1155 Select(Chain);
1156 }
1157
1158 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001159 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1160 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001161 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001162 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001163}
1164
Chris Lattner30ea1e92005-01-19 07:37:26 +00001165// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1166// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1167// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1168// return true.
1169bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001170 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1171 // good!
1172 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1173 std::swap(Op1, Op2); // Op1 is the SHL now.
1174 } else {
1175 return false; // No match
1176 }
1177
1178 SDOperand ShlVal = Op1.getOperand(0);
1179 SDOperand ShlAmt = Op1.getOperand(1);
1180 SDOperand ShrVal = Op2.getOperand(0);
1181 SDOperand ShrAmt = Op2.getOperand(1);
1182
Chris Lattner30ea1e92005-01-19 07:37:26 +00001183 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1184
Chris Lattner85716372005-01-19 06:18:43 +00001185 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1186 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1187 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001188 if (SubCST->getValue() == RegSize) {
1189 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001190 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001191 if (ShrVal == ShlVal) {
1192 unsigned Reg, ShAmt;
1193 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1194 Reg = SelectExpr(ShrVal);
1195 ShAmt = SelectExpr(ShrAmt);
1196 } else {
1197 ShAmt = SelectExpr(ShrAmt);
1198 Reg = SelectExpr(ShrVal);
1199 }
1200 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1201 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1202 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1203 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1204 return true;
1205 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001206 unsigned AReg, BReg;
1207 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001208 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001209 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001210 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001211 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001212 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001213 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001214 unsigned ShAmt = SelectExpr(ShrAmt);
1215 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1216 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1217 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001218 return true;
1219 }
1220 }
1221
Chris Lattner4053b1e2005-01-19 08:07:05 +00001222 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1223 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1224 if (SubCST->getValue() == RegSize) {
1225 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1226 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1227 if (ShrVal == ShlVal) {
1228 unsigned Reg, ShAmt;
1229 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1230 Reg = SelectExpr(ShrVal);
1231 ShAmt = SelectExpr(ShlAmt);
1232 } else {
1233 ShAmt = SelectExpr(ShlAmt);
1234 Reg = SelectExpr(ShrVal);
1235 }
1236 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1237 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1238 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1239 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1240 return true;
1241 } else if (RegSize != 8) {
1242 unsigned AReg, BReg;
1243 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001244 AReg = SelectExpr(ShlVal);
1245 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001246 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001247 BReg = SelectExpr(ShrVal);
1248 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001249 }
1250 unsigned ShAmt = SelectExpr(ShlAmt);
1251 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1252 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1253 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1254 return true;
1255 }
1256 }
Chris Lattner85716372005-01-19 06:18:43 +00001257
Chris Lattner4053b1e2005-01-19 08:07:05 +00001258 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1259 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1260 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1261 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1262 // (A >> 5) | (A << 27) --> ROR A, 5
1263 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1264 if (ShrVal == ShlVal) {
1265 unsigned Reg = SelectExpr(ShrVal);
1266 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1267 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1268 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1269 return true;
1270 } else if (RegSize != 8) {
1271 unsigned AReg, BReg;
1272 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001273 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001274 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001275 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001276 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001277 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001278 }
1279 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1280 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1281 .addImm(ShrCst->getValue());
1282 return true;
1283 }
1284 }
1285
Chris Lattner85716372005-01-19 06:18:43 +00001286 return false;
1287}
1288
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001289unsigned ISel::SelectExpr(SDOperand N) {
1290 unsigned Result;
1291 unsigned Tmp1, Tmp2, Tmp3;
1292 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001293 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001294 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001295
Chris Lattner7f2afac2005-01-14 22:37:41 +00001296 if (Node->getOpcode() == ISD::CopyFromReg) {
1297 // FIXME: Handle copy from physregs!
1298
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001299 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001300 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001301 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001302
1303 unsigned &Reg = ExprMap[N];
1304 if (Reg) return Reg;
1305
Chris Lattner19ad0622005-01-20 18:53:00 +00001306 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
1307 N.getOpcode() != ISD::SUB_PARTS)
Chris Lattnera5ade062005-01-11 21:19:59 +00001308 Reg = Result = (N.getValueType() != MVT::Other) ?
1309 MakeReg(N.getValueType()) : 1;
1310 else {
1311 // If this is a call instruction, make sure to prepare ALL of the result
1312 // values as well as the chain.
Chris Lattner19ad0622005-01-20 18:53:00 +00001313 if (N.getOpcode() == ISD::CALL) {
1314 if (Node->getNumValues() == 1)
1315 Reg = Result = 1; // Void call, just a chain.
1316 else {
1317 Result = MakeReg(Node->getValueType(0));
1318 ExprMap[N.getValue(0)] = Result;
1319 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1320 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1321 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
1322 }
1323 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001324 Result = MakeReg(Node->getValueType(0));
1325 ExprMap[N.getValue(0)] = Result;
Chris Lattner19ad0622005-01-20 18:53:00 +00001326 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001327 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001328 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001329 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001330
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001331 switch (N.getOpcode()) {
1332 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001333 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001334 assert(0 && "Node not handled!\n");
1335 case ISD::FrameIndex:
1336 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1337 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1338 return Result;
1339 case ISD::ConstantPool:
1340 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1341 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1342 return Result;
1343 case ISD::ConstantFP:
1344 ContainsFPCode = true;
1345 Tmp1 = Result; // Intermediate Register
1346 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1347 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1348 Tmp1 = MakeReg(MVT::f64);
1349
1350 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1351 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1352 BuildMI(BB, X86::FLD0, 0, Tmp1);
1353 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1354 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1355 BuildMI(BB, X86::FLD1, 0, Tmp1);
1356 else
1357 assert(0 && "Unexpected constant!");
1358 if (Tmp1 != Result)
1359 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1360 return Result;
1361 case ISD::Constant:
1362 switch (N.getValueType()) {
1363 default: assert(0 && "Cannot use constants of this type!");
1364 case MVT::i1:
1365 case MVT::i8: Opc = X86::MOV8ri; break;
1366 case MVT::i16: Opc = X86::MOV16ri; break;
1367 case MVT::i32: Opc = X86::MOV32ri; break;
1368 }
1369 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1370 return Result;
1371 case ISD::GlobalAddress: {
1372 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1373 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1374 return Result;
1375 }
1376 case ISD::ExternalSymbol: {
1377 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1378 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1379 return Result;
1380 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001381 case ISD::ZERO_EXTEND: {
1382 int DestIs16 = N.getValueType() == MVT::i16;
1383 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001384
1385 // FIXME: This hack is here for zero extension casts from bool to i8. This
1386 // would not be needed if bools were promoted by Legalize.
1387 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001388 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001389 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1390 return Result;
1391 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001392
Chris Lattner4ff348b2005-01-17 06:26:58 +00001393 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001394 static const unsigned Opc[3] = {
1395 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1396 };
1397
1398 X86AddressMode AM;
1399 EmitFoldedLoad(N.getOperand(0), AM);
1400 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1401
1402 return Result;
1403 }
1404
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001405 static const unsigned Opc[3] = {
1406 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1407 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001408 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001409 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1410 return Result;
1411 }
1412 case ISD::SIGN_EXTEND: {
1413 int DestIs16 = N.getValueType() == MVT::i16;
1414 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1415
Chris Lattner590d8002005-01-09 18:52:44 +00001416 // FIXME: Legalize should promote bools to i8!
1417 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1418 "Sign extend from bool not implemented!");
1419
Chris Lattner4ff348b2005-01-17 06:26:58 +00001420 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001421 static const unsigned Opc[3] = {
1422 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1423 };
1424
1425 X86AddressMode AM;
1426 EmitFoldedLoad(N.getOperand(0), AM);
1427 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1428 return Result;
1429 }
1430
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001431 static const unsigned Opc[3] = {
1432 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1433 };
1434 Tmp1 = SelectExpr(N.getOperand(0));
1435 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1436 return Result;
1437 }
1438 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001439 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001440 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001441 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001442 switch (N.getValueType()) {
1443 default: assert(0 && "Unknown truncate!");
1444 case MVT::i1:
1445 case MVT::i8: Opc = X86::MOV8rm; break;
1446 case MVT::i16: Opc = X86::MOV16rm; break;
1447 }
1448 X86AddressMode AM;
1449 EmitFoldedLoad(N.getOperand(0), AM);
1450 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1451 return Result;
1452 }
1453
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001454 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1455 // a move out of AX or AL.
1456 switch (N.getOperand(0).getValueType()) {
1457 default: assert(0 && "Unknown truncate!");
1458 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1459 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1460 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1461 }
1462 Tmp1 = SelectExpr(N.getOperand(0));
1463 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1464
1465 switch (N.getValueType()) {
1466 default: assert(0 && "Unknown truncate!");
1467 case MVT::i1:
1468 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1469 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1470 }
1471 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1472 return Result;
1473
Chris Lattner590d8002005-01-09 18:52:44 +00001474 case ISD::SINT_TO_FP:
1475 case ISD::UINT_TO_FP: {
1476 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001477 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001478
1479 // Promote the integer to a type supported by FLD. We do this because there
1480 // are no unsigned FLD instructions, so we must promote an unsigned value to
1481 // a larger signed value, then use FLD on the larger value.
1482 //
1483 MVT::ValueType PromoteType = MVT::Other;
1484 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1485 unsigned PromoteOpcode = 0;
1486 unsigned RealDestReg = Result;
1487 switch (SrcTy) {
1488 case MVT::i1:
1489 case MVT::i8:
1490 // We don't have the facilities for directly loading byte sized data from
1491 // memory (even signed). Promote it to 16 bits.
1492 PromoteType = MVT::i16;
1493 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1494 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1495 break;
1496 case MVT::i16:
1497 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1498 PromoteType = MVT::i32;
1499 PromoteOpcode = X86::MOVZX32rr16;
1500 }
1501 break;
1502 default:
1503 // Don't fild into the real destination.
1504 if (Node->getOpcode() == ISD::UINT_TO_FP)
1505 Result = MakeReg(Node->getValueType(0));
1506 break;
1507 }
1508
1509 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1510
1511 if (PromoteType != MVT::Other) {
1512 Tmp2 = MakeReg(PromoteType);
1513 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1514 SrcTy = PromoteType;
1515 Tmp1 = Tmp2;
1516 }
1517
1518 // Spill the integer to memory and reload it from there.
1519 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1520 MachineFunction *F = BB->getParent();
1521 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1522
1523 switch (SrcTy) {
1524 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001525 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001526 // FIXME: this won't work for cast [u]long to FP
1527 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1528 FrameIdx).addReg(Tmp1);
1529 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1530 FrameIdx, 4).addReg(Tmp1+1);
1531 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1532 break;
1533 case MVT::i32:
1534 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1535 FrameIdx).addReg(Tmp1);
1536 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1537 break;
1538 case MVT::i16:
1539 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1540 FrameIdx).addReg(Tmp1);
1541 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1542 break;
1543 default: break; // No promotion required.
1544 }
1545
Chris Lattner085c9952005-01-12 04:00:00 +00001546 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001547 // If this is a cast from uint -> double, we need to be careful when if
1548 // the "sign" bit is set. If so, we don't want to make a negative number,
1549 // we want to make a positive number. Emit code to add an offset if the
1550 // sign bit is set.
1551
1552 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1553 unsigned IsNeg = MakeReg(MVT::i32);
1554 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1555
1556 // Create a CP value that has the offset in one word and 0 in the other.
1557 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1558 0x4f80000000000000ULL);
1559 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1560 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1561 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1562
1563 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1564 // We need special handling for unsigned 64-bit integer sources. If the
1565 // input number has the "sign bit" set, then we loaded it incorrectly as a
1566 // negative 64-bit number. In this case, add an offset value.
1567
1568 // Emit a test instruction to see if the dynamic input value was signed.
1569 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1570
1571 // If the sign bit is set, get a pointer to an offset, otherwise get a
1572 // pointer to a zero.
1573 MachineConstantPool *CP = F->getConstantPool();
1574 unsigned Zero = MakeReg(MVT::i32);
1575 Constant *Null = Constant::getNullValue(Type::UIntTy);
1576 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1577 CP->getConstantPoolIndex(Null));
1578 unsigned Offset = MakeReg(MVT::i32);
1579 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1580
1581 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1582 CP->getConstantPoolIndex(OffsetCst));
1583 unsigned Addr = MakeReg(MVT::i32);
1584 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1585
1586 // Load the constant for an add. FIXME: this could make an 'fadd' that
1587 // reads directly from memory, but we don't support these yet.
1588 unsigned ConstReg = MakeReg(MVT::f64);
1589 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1590
1591 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1592 }
1593 return RealDestReg;
1594 }
1595 case ISD::FP_TO_SINT:
1596 case ISD::FP_TO_UINT: {
1597 // FIXME: Most of this grunt work should be done by legalize!
1598 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1599
1600 // Change the floating point control register to use "round towards zero"
1601 // mode when truncating to an integer value.
1602 //
1603 MachineFunction *F = BB->getParent();
1604 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1605 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1606
1607 // Load the old value of the high byte of the control word...
1608 unsigned HighPartOfCW = MakeReg(MVT::i8);
1609 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1610 CWFrameIdx, 1);
1611
1612 // Set the high part to be round to zero...
1613 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1614 CWFrameIdx, 1).addImm(12);
1615
1616 // Reload the modified control word now...
1617 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1618
1619 // Restore the memory image of control word to original value
1620 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1621 CWFrameIdx, 1).addReg(HighPartOfCW);
1622
1623 // We don't have the facilities for directly storing byte sized data to
1624 // memory. Promote it to 16 bits. We also must promote unsigned values to
1625 // larger classes because we only have signed FP stores.
1626 MVT::ValueType StoreClass = Node->getValueType(0);
1627 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1628 switch (StoreClass) {
1629 case MVT::i8: StoreClass = MVT::i16; break;
1630 case MVT::i16: StoreClass = MVT::i32; break;
1631 case MVT::i32: StoreClass = MVT::i64; break;
1632 // The following treatment of cLong may not be perfectly right,
1633 // but it survives chains of casts of the form
1634 // double->ulong->double.
1635 case MVT::i64: StoreClass = MVT::i64; break;
1636 default: assert(0 && "Unknown store class!");
1637 }
1638
1639 // Spill the integer to memory and reload it from there.
1640 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1641 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1642
1643 switch (StoreClass) {
1644 default: assert(0 && "Unknown store class!");
1645 case MVT::i16:
1646 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1647 break;
1648 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001649 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001650 break;
1651 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001652 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001653 break;
1654 }
1655
1656 switch (Node->getValueType(0)) {
1657 default:
1658 assert(0 && "Unknown integer type!");
1659 case MVT::i64:
1660 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001661 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001662 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1663 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1664 case MVT::i32:
1665 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1666 break;
1667 case MVT::i16:
1668 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1669 break;
1670 case MVT::i8:
1671 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1672 break;
1673 }
1674
1675 // Reload the original control word now.
1676 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1677 return Result;
1678 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001679 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001680 Op0 = N.getOperand(0);
1681 Op1 = N.getOperand(1);
1682
Chris Lattner4ff348b2005-01-17 06:26:58 +00001683 if (isFoldableLoad(Op0, Op1)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001684 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001685 goto FoldAdd;
1686 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001687
Chris Lattner4ff348b2005-01-17 06:26:58 +00001688 if (isFoldableLoad(Op1, Op0)) {
1689 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001690 switch (N.getValueType()) {
1691 default: assert(0 && "Cannot add this type!");
1692 case MVT::i1:
1693 case MVT::i8: Opc = X86::ADD8rm; break;
1694 case MVT::i16: Opc = X86::ADD16rm; break;
1695 case MVT::i32: Opc = X86::ADD32rm; break;
1696 case MVT::f32: Opc = X86::FADD32m; break;
1697 case MVT::f64: Opc = X86::FADD64m; break;
1698 }
1699 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001700 EmitFoldedLoad(Op1, AM);
1701 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001702 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1703 return Result;
1704 }
1705
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001706 // See if we can codegen this as an LEA to fold operations together.
1707 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001708 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001709 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001710 MatchAddress(N, AM);
1711 ExprMap[N] = Result;
1712
1713 // If this is not just an add, emit the LEA. For a simple add (like
1714 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1715 // leave this as LEA, then peephole it to 'ADD' after two address elim
1716 // happens.
1717 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1718 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1719 X86AddressMode XAM = SelectAddrExprs(AM);
1720 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1721 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001722 }
1723 }
Chris Lattner11333092005-01-11 03:11:44 +00001724
Chris Lattnera5ade062005-01-11 21:19:59 +00001725 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001726 Opc = 0;
1727 if (CN->getValue() == 1) { // add X, 1 -> inc X
1728 switch (N.getValueType()) {
1729 default: assert(0 && "Cannot integer add this type!");
1730 case MVT::i8: Opc = X86::INC8r; break;
1731 case MVT::i16: Opc = X86::INC16r; break;
1732 case MVT::i32: Opc = X86::INC32r; break;
1733 }
1734 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1735 switch (N.getValueType()) {
1736 default: assert(0 && "Cannot integer add this type!");
1737 case MVT::i8: Opc = X86::DEC8r; break;
1738 case MVT::i16: Opc = X86::DEC16r; break;
1739 case MVT::i32: Opc = X86::DEC32r; break;
1740 }
1741 }
1742
1743 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001744 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001745 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1746 return Result;
1747 }
1748
1749 switch (N.getValueType()) {
1750 default: assert(0 && "Cannot add this type!");
1751 case MVT::i8: Opc = X86::ADD8ri; break;
1752 case MVT::i16: Opc = X86::ADD16ri; break;
1753 case MVT::i32: Opc = X86::ADD32ri; break;
1754 }
1755 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001756 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001757 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1758 return Result;
1759 }
1760 }
1761
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001762 switch (N.getValueType()) {
1763 default: assert(0 && "Cannot add this type!");
1764 case MVT::i8: Opc = X86::ADD8rr; break;
1765 case MVT::i16: Opc = X86::ADD16rr; break;
1766 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001767 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001768 }
Chris Lattner11333092005-01-11 03:11:44 +00001769
Chris Lattnera5ade062005-01-11 21:19:59 +00001770 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1771 Tmp1 = SelectExpr(Op0);
1772 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001773 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001774 Tmp2 = SelectExpr(Op1);
1775 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001776 }
1777
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001778 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1779 return Result;
1780 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001781 case ISD::MUL:
1782 case ISD::AND:
1783 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001784 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001785 static const unsigned SUBTab[] = {
1786 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1787 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1788 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1789 };
1790 static const unsigned MULTab[] = {
1791 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1792 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1793 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1794 };
1795 static const unsigned ANDTab[] = {
1796 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1797 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1798 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1799 };
1800 static const unsigned ORTab[] = {
1801 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1802 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1803 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1804 };
1805 static const unsigned XORTab[] = {
1806 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1807 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1808 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1809 };
1810
1811 Op0 = Node->getOperand(0);
1812 Op1 = Node->getOperand(1);
1813
Chris Lattner30ea1e92005-01-19 07:37:26 +00001814 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1815 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001816 return Result;
1817
1818 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001819 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1820 if (CN->isNullValue()) { // 0 - N -> neg N
1821 switch (N.getValueType()) {
1822 default: assert(0 && "Cannot sub this type!");
1823 case MVT::i1:
1824 case MVT::i8: Opc = X86::NEG8r; break;
1825 case MVT::i16: Opc = X86::NEG16r; break;
1826 case MVT::i32: Opc = X86::NEG32r; break;
1827 }
1828 Tmp1 = SelectExpr(N.getOperand(1));
1829 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1830 return Result;
1831 }
1832
Chris Lattnera5ade062005-01-11 21:19:59 +00001833 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1834 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001835 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001836 switch (N.getValueType()) {
1837 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001838 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001839 case MVT::i8: Opc = X86::NOT8r; break;
1840 case MVT::i16: Opc = X86::NOT16r; break;
1841 case MVT::i32: Opc = X86::NOT32r; break;
1842 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001843 if (Opc) {
1844 Tmp1 = SelectExpr(Op0);
1845 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1846 return Result;
1847 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001848 }
1849
Chris Lattner2a4e5082005-01-17 06:48:02 +00001850 // Fold common multiplies into LEA instructions.
1851 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1852 switch ((int)CN->getValue()) {
1853 default: break;
1854 case 3:
1855 case 5:
1856 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001857 // Remove N from exprmap so SelectAddress doesn't get confused.
1858 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001859 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001860 SelectAddress(N, AM);
1861 // Restore it to the map.
1862 ExprMap[N] = Result;
1863 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1864 return Result;
1865 }
1866 }
1867
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001868 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001869 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001870 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001871 case MVT::i8: Opc = 0; break;
1872 case MVT::i16: Opc = 1; break;
1873 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001874 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001875 switch (Node->getOpcode()) {
1876 default: assert(0 && "Unreachable!");
1877 case ISD::SUB: Opc = SUBTab[Opc]; break;
1878 case ISD::MUL: Opc = MULTab[Opc]; break;
1879 case ISD::AND: Opc = ANDTab[Opc]; break;
1880 case ISD::OR: Opc = ORTab[Opc]; break;
1881 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001882 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001883 if (Opc) { // Can't fold MUL:i8 R, imm
1884 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001885 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1886 return Result;
1887 }
1888 }
Chris Lattner11333092005-01-11 03:11:44 +00001889
Chris Lattner4ff348b2005-01-17 06:26:58 +00001890 if (isFoldableLoad(Op0, Op1))
Chris Lattnera5ade062005-01-11 21:19:59 +00001891 if (Node->getOpcode() != ISD::SUB) {
1892 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001893 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001894 } else {
1895 // Emit 'reverse' subract, with a memory operand.
1896 switch (N.getValueType()) {
1897 default: Opc = 0; break;
1898 case MVT::f32: Opc = X86::FSUBR32m; break;
1899 case MVT::f64: Opc = X86::FSUBR64m; break;
1900 }
1901 if (Opc) {
1902 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001903 EmitFoldedLoad(Op0, AM);
1904 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001905 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1906 return Result;
1907 }
1908 }
1909
Chris Lattner4ff348b2005-01-17 06:26:58 +00001910 if (isFoldableLoad(Op1, Op0)) {
1911 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001912 switch (N.getValueType()) {
1913 default: assert(0 && "Cannot operate on this type!");
1914 case MVT::i1:
1915 case MVT::i8: Opc = 5; break;
1916 case MVT::i16: Opc = 6; break;
1917 case MVT::i32: Opc = 7; break;
1918 case MVT::f32: Opc = 8; break;
1919 case MVT::f64: Opc = 9; break;
1920 }
1921 switch (Node->getOpcode()) {
1922 default: assert(0 && "Unreachable!");
1923 case ISD::SUB: Opc = SUBTab[Opc]; break;
1924 case ISD::MUL: Opc = MULTab[Opc]; break;
1925 case ISD::AND: Opc = ANDTab[Opc]; break;
1926 case ISD::OR: Opc = ORTab[Opc]; break;
1927 case ISD::XOR: Opc = XORTab[Opc]; break;
1928 }
1929
1930 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001931 EmitFoldedLoad(Op1, AM);
1932 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001933 if (Opc) {
1934 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1935 } else {
1936 assert(Node->getOpcode() == ISD::MUL &&
1937 N.getValueType() == MVT::i8 && "Unexpected situation!");
1938 // Must use the MUL instruction, which forces use of AL.
1939 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1940 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1941 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1942 }
1943 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001944 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001945
1946 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1947 Tmp1 = SelectExpr(Op0);
1948 Tmp2 = SelectExpr(Op1);
1949 } else {
1950 Tmp2 = SelectExpr(Op1);
1951 Tmp1 = SelectExpr(Op0);
1952 }
1953
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001954 switch (N.getValueType()) {
1955 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001956 case MVT::i1:
1957 case MVT::i8: Opc = 10; break;
1958 case MVT::i16: Opc = 11; break;
1959 case MVT::i32: Opc = 12; break;
1960 case MVT::f32: Opc = 13; break;
1961 case MVT::f64: Opc = 14; break;
1962 }
1963 switch (Node->getOpcode()) {
1964 default: assert(0 && "Unreachable!");
1965 case ISD::SUB: Opc = SUBTab[Opc]; break;
1966 case ISD::MUL: Opc = MULTab[Opc]; break;
1967 case ISD::AND: Opc = ANDTab[Opc]; break;
1968 case ISD::OR: Opc = ORTab[Opc]; break;
1969 case ISD::XOR: Opc = XORTab[Opc]; break;
1970 }
1971 if (Opc) {
1972 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1973 } else {
1974 assert(Node->getOpcode() == ISD::MUL &&
1975 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001976 // Must use the MUL instruction, which forces use of AL.
1977 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1978 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1979 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001980 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001981 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001982 }
Chris Lattner19ad0622005-01-20 18:53:00 +00001983 case ISD::ADD_PARTS:
1984 case ISD::SUB_PARTS: {
1985 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1986 "Not an i64 add/sub!");
1987 // Emit all of the operands.
1988 std::vector<unsigned> InVals;
1989 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1990 InVals.push_back(SelectExpr(N.getOperand(i)));
1991 if (N.getOpcode() == ISD::ADD_PARTS) {
1992 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1993 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1994 } else {
1995 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1996 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1997 }
1998 return Result+N.ResNo;
1999 }
2000
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002001 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002002 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2003 Tmp2 = SelectExpr(N.getOperand(1));
2004 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002005 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002006 Tmp3 = SelectExpr(N.getOperand(2));
2007 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002008 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002009 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2010 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002011
2012 case ISD::SDIV:
2013 case ISD::UDIV:
2014 case ISD::SREM:
2015 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002016 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2017 "We don't support this operator!");
2018
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002019 if (N.getOpcode() == ISD::SDIV)
2020 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2021 // FIXME: These special cases should be handled by the lowering impl!
2022 unsigned RHS = CN->getValue();
2023 bool isNeg = false;
2024 if ((int)RHS < 0) {
2025 isNeg = true;
2026 RHS = -RHS;
2027 }
2028 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2029 unsigned Log = log2(RHS);
2030 unsigned TmpReg = MakeReg(N.getValueType());
2031 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2032 switch (N.getValueType()) {
2033 default: assert("Unknown type to signed divide!");
2034 case MVT::i8:
2035 SAROpc = X86::SAR8ri;
2036 SHROpc = X86::SHR8ri;
2037 ADDOpc = X86::ADD8rr;
2038 NEGOpc = X86::NEG8r;
2039 break;
2040 case MVT::i16:
2041 SAROpc = X86::SAR16ri;
2042 SHROpc = X86::SHR16ri;
2043 ADDOpc = X86::ADD16rr;
2044 NEGOpc = X86::NEG16r;
2045 break;
2046 case MVT::i32:
2047 SAROpc = X86::SAR32ri;
2048 SHROpc = X86::SHR32ri;
2049 ADDOpc = X86::ADD32rr;
2050 NEGOpc = X86::NEG32r;
2051 break;
2052 }
Chris Lattner11333092005-01-11 03:11:44 +00002053 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002054 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2055 unsigned TmpReg2 = MakeReg(N.getValueType());
2056 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2057 unsigned TmpReg3 = MakeReg(N.getValueType());
2058 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
2059
2060 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2061 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2062 if (isNeg)
2063 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2064 return Result;
2065 }
2066 }
2067
Chris Lattner11333092005-01-11 03:11:44 +00002068 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2069 Tmp1 = SelectExpr(N.getOperand(0));
2070 Tmp2 = SelectExpr(N.getOperand(1));
2071 } else {
2072 Tmp2 = SelectExpr(N.getOperand(1));
2073 Tmp1 = SelectExpr(N.getOperand(0));
2074 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002075
2076 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2077 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2078 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2079 switch (N.getValueType()) {
2080 default: assert(0 && "Cannot sdiv this type!");
2081 case MVT::i8:
2082 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2083 LoReg = X86::AL;
2084 HiReg = X86::AH;
2085 MovOpcode = X86::MOV8rr;
2086 ClrOpcode = X86::MOV8ri;
2087 SExtOpcode = X86::CBW;
2088 break;
2089 case MVT::i16:
2090 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2091 LoReg = X86::AX;
2092 HiReg = X86::DX;
2093 MovOpcode = X86::MOV16rr;
2094 ClrOpcode = X86::MOV16ri;
2095 SExtOpcode = X86::CWD;
2096 break;
2097 case MVT::i32:
2098 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002099 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002100 HiReg = X86::EDX;
2101 MovOpcode = X86::MOV32rr;
2102 ClrOpcode = X86::MOV32ri;
2103 SExtOpcode = X86::CDQ;
2104 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002105 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002106 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002107 return Result;
2108 }
2109
2110 // Set up the low part.
2111 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2112
2113 if (isSigned) {
2114 // Sign extend the low part into the high part.
2115 BuildMI(BB, SExtOpcode, 0);
2116 } else {
2117 // Zero out the high part, effectively zero extending the input.
2118 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2119 }
2120
2121 // Emit the DIV/IDIV instruction.
2122 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
2123
2124 // Get the result of the divide or rem.
2125 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2126 return Result;
2127 }
2128
2129 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002130 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002131 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2132 switch (N.getValueType()) {
2133 default: assert(0 && "Cannot shift this type!");
2134 case MVT::i8: Opc = X86::ADD8rr; break;
2135 case MVT::i16: Opc = X86::ADD16rr; break;
2136 case MVT::i32: Opc = X86::ADD32rr; break;
2137 }
2138 Tmp1 = SelectExpr(N.getOperand(0));
2139 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2140 return Result;
2141 }
2142
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002143 switch (N.getValueType()) {
2144 default: assert(0 && "Cannot shift this type!");
2145 case MVT::i8: Opc = X86::SHL8ri; break;
2146 case MVT::i16: Opc = X86::SHL16ri; break;
2147 case MVT::i32: Opc = X86::SHL32ri; break;
2148 }
Chris Lattner11333092005-01-11 03:11:44 +00002149 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002150 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2151 return Result;
2152 }
Chris Lattner11333092005-01-11 03:11:44 +00002153
2154 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2155 Tmp1 = SelectExpr(N.getOperand(0));
2156 Tmp2 = SelectExpr(N.getOperand(1));
2157 } else {
2158 Tmp2 = SelectExpr(N.getOperand(1));
2159 Tmp1 = SelectExpr(N.getOperand(0));
2160 }
2161
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002162 switch (N.getValueType()) {
2163 default: assert(0 && "Cannot shift this type!");
2164 case MVT::i8 : Opc = X86::SHL8rCL; break;
2165 case MVT::i16: Opc = X86::SHL16rCL; break;
2166 case MVT::i32: Opc = X86::SHL32rCL; break;
2167 }
2168 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2169 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2170 return Result;
2171 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002172 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2173 switch (N.getValueType()) {
2174 default: assert(0 && "Cannot shift this type!");
2175 case MVT::i8: Opc = X86::SHR8ri; break;
2176 case MVT::i16: Opc = X86::SHR16ri; break;
2177 case MVT::i32: Opc = X86::SHR32ri; break;
2178 }
Chris Lattner11333092005-01-11 03:11:44 +00002179 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002180 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2181 return Result;
2182 }
Chris Lattner11333092005-01-11 03:11:44 +00002183
2184 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2185 Tmp1 = SelectExpr(N.getOperand(0));
2186 Tmp2 = SelectExpr(N.getOperand(1));
2187 } else {
2188 Tmp2 = SelectExpr(N.getOperand(1));
2189 Tmp1 = SelectExpr(N.getOperand(0));
2190 }
2191
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002192 switch (N.getValueType()) {
2193 default: assert(0 && "Cannot shift this type!");
2194 case MVT::i8 : Opc = X86::SHR8rCL; break;
2195 case MVT::i16: Opc = X86::SHR16rCL; break;
2196 case MVT::i32: Opc = X86::SHR32rCL; break;
2197 }
2198 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2199 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2200 return Result;
2201 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002202 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2203 switch (N.getValueType()) {
2204 default: assert(0 && "Cannot shift this type!");
2205 case MVT::i8: Opc = X86::SAR8ri; break;
2206 case MVT::i16: Opc = X86::SAR16ri; break;
2207 case MVT::i32: Opc = X86::SAR32ri; break;
2208 }
Chris Lattner11333092005-01-11 03:11:44 +00002209 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002210 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2211 return Result;
2212 }
Chris Lattner11333092005-01-11 03:11:44 +00002213
2214 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2215 Tmp1 = SelectExpr(N.getOperand(0));
2216 Tmp2 = SelectExpr(N.getOperand(1));
2217 } else {
2218 Tmp2 = SelectExpr(N.getOperand(1));
2219 Tmp1 = SelectExpr(N.getOperand(0));
2220 }
2221
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002222 switch (N.getValueType()) {
2223 default: assert(0 && "Cannot shift this type!");
2224 case MVT::i8 : Opc = X86::SAR8rCL; break;
2225 case MVT::i16: Opc = X86::SAR16rCL; break;
2226 case MVT::i32: Opc = X86::SAR32rCL; break;
2227 }
2228 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2229 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2230 return Result;
2231
2232 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002233 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002234 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2235 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2236 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002237 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002238 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002239 if (Result != 1) { // Generate the token
2240 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2241 assert(0 && "Load already emitted!?");
2242 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002243 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2244
Chris Lattner5188ad72005-01-08 19:28:19 +00002245 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002246 default: assert(0 && "Cannot load this type!");
2247 case MVT::i1:
2248 case MVT::i8: Opc = X86::MOV8rm; break;
2249 case MVT::i16: Opc = X86::MOV16rm; break;
2250 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002251 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2252 }
Chris Lattner11333092005-01-11 03:11:44 +00002253
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002254 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002255 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002256 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2257 } else {
2258 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002259
2260 SDOperand Chain = N.getOperand(0);
2261 SDOperand Address = N.getOperand(1);
2262 if (getRegPressure(Chain) > getRegPressure(Address)) {
2263 Select(Chain);
2264 SelectAddress(Address, AM);
2265 } else {
2266 SelectAddress(Address, AM);
2267 Select(Chain);
2268 }
2269
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002270 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2271 }
2272 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002273
2274 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2275 case ISD::ZEXTLOAD: {
2276 // Make sure we generate both values.
2277 if (Result != 1)
2278 ExprMap[N.getValue(1)] = 1; // Generate the token
2279 else
2280 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2281
Chris Lattnerda2ce112005-01-16 07:34:08 +00002282 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2283 if (Node->getValueType(0) == MVT::f64) {
2284 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2285 "Bad EXTLOAD!");
2286 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2287 CP->getIndex());
2288 return Result;
2289 }
2290
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002291 X86AddressMode AM;
2292 if (getRegPressure(Node->getOperand(0)) >
2293 getRegPressure(Node->getOperand(1))) {
2294 Select(Node->getOperand(0)); // chain
2295 SelectAddress(Node->getOperand(1), AM);
2296 } else {
2297 SelectAddress(Node->getOperand(1), AM);
2298 Select(Node->getOperand(0)); // chain
2299 }
2300
2301 switch (Node->getValueType(0)) {
2302 default: assert(0 && "Unknown type to sign extend to.");
2303 case MVT::f64:
2304 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2305 "Bad EXTLOAD!");
2306 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2307 break;
2308 case MVT::i32:
2309 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2310 default:
2311 assert(0 && "Bad zero extend!");
2312 case MVT::i1:
2313 case MVT::i8:
2314 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2315 break;
2316 case MVT::i16:
2317 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2318 break;
2319 }
2320 break;
2321 case MVT::i16:
2322 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2323 "Bad zero extend!");
2324 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2325 break;
2326 case MVT::i8:
2327 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2328 "Bad zero extend!");
2329 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2330 break;
2331 }
2332 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002333 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002334 case ISD::SEXTLOAD: {
2335 // Make sure we generate both values.
2336 if (Result != 1)
2337 ExprMap[N.getValue(1)] = 1; // Generate the token
2338 else
2339 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2340
2341 X86AddressMode AM;
2342 if (getRegPressure(Node->getOperand(0)) >
2343 getRegPressure(Node->getOperand(1))) {
2344 Select(Node->getOperand(0)); // chain
2345 SelectAddress(Node->getOperand(1), AM);
2346 } else {
2347 SelectAddress(Node->getOperand(1), AM);
2348 Select(Node->getOperand(0)); // chain
2349 }
2350
2351 switch (Node->getValueType(0)) {
2352 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2353 default: assert(0 && "Unknown type to sign extend to.");
2354 case MVT::i32:
2355 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2356 default:
2357 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2358 case MVT::i8:
2359 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2360 break;
2361 case MVT::i16:
2362 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2363 break;
2364 }
2365 break;
2366 case MVT::i16:
2367 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2368 "Cannot sign extend from bool!");
2369 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2370 break;
2371 }
2372 return Result;
2373 }
2374
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002375 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002376 // Generate both result values.
2377 if (Result != 1)
2378 ExprMap[N.getValue(1)] = 1; // Generate the token
2379 else
2380 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2381
2382 // FIXME: We are currently ignoring the requested alignment for handling
2383 // greater than the stack alignment. This will need to be revisited at some
2384 // point. Align = N.getOperand(2);
2385
2386 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2387 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2388 std::cerr << "Cannot allocate stack object with greater alignment than"
2389 << " the stack alignment yet!";
2390 abort();
2391 }
2392
2393 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002394 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002395 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2396 .addImm(CN->getValue());
2397 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002398 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2399 Select(N.getOperand(0));
2400 Tmp1 = SelectExpr(N.getOperand(1));
2401 } else {
2402 Tmp1 = SelectExpr(N.getOperand(1));
2403 Select(N.getOperand(0));
2404 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002405
2406 // Subtract size from stack pointer, thereby allocating some space.
2407 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2408 }
2409
2410 // Put a pointer to the space into the result register, by copying the stack
2411 // pointer.
2412 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2413 return Result;
2414
2415 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002416 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002417 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002418
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002419 if (GlobalAddressSDNode *GASD =
2420 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002421 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002422 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2423 } else if (ExternalSymbolSDNode *ESSDN =
2424 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002425 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002426 BuildMI(BB, X86::CALLpcrel32,
2427 1).addExternalSymbol(ESSDN->getSymbol(), true);
2428 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002429 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2430 Select(N.getOperand(0));
2431 Tmp1 = SelectExpr(N.getOperand(1));
2432 } else {
2433 Tmp1 = SelectExpr(N.getOperand(1));
2434 Select(N.getOperand(0));
2435 }
2436
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002437 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2438 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002439 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002440 default: assert(0 && "Unknown value type for call result!");
2441 case MVT::Other: return 1;
2442 case MVT::i1:
2443 case MVT::i8:
2444 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2445 break;
2446 case MVT::i16:
2447 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2448 break;
2449 case MVT::i32:
2450 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002451 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002452 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2453 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002454 case MVT::f64: // Floating-point return values live in %ST(0)
2455 ContainsFPCode = true;
2456 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2457 break;
2458 }
2459 return Result+N.ResNo;
2460 }
2461
2462 return 0;
2463}
2464
Chris Lattnere10269b2005-01-17 19:25:26 +00002465/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2466/// load/op/store instruction. If successful return true.
2467bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2468 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2469 SDOperand Chain = Node->getOperand(0);
2470 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002471 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002472
2473 // The chain has to be a load, the stored value must be an integer binary
2474 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002475 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002476 MVT::isFloatingPoint(StVal.getValueType()))
2477 return false;
2478
Chris Lattner5c659812005-01-17 22:10:42 +00002479 // Token chain must either be a factor node or the load to fold.
2480 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2481 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002482
Chris Lattner5c659812005-01-17 22:10:42 +00002483 SDOperand TheLoad;
2484
2485 // Check to see if there is a load from the same pointer that we're storing
2486 // to in either operand of the binop.
2487 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2488 StVal.getOperand(0).getOperand(1) == StPtr)
2489 TheLoad = StVal.getOperand(0);
2490 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2491 StVal.getOperand(1).getOperand(1) == StPtr)
2492 TheLoad = StVal.getOperand(1);
2493 else
2494 return false; // No matching load operand.
2495
2496 // We can only fold the load if there are no intervening side-effecting
2497 // operations. This means that the store uses the load as its token chain, or
2498 // there are only token factor nodes in between the store and load.
2499 if (Chain != TheLoad.getValue(1)) {
2500 // Okay, the other option is that we have a store referring to (possibly
2501 // nested) token factor nodes. For now, just try peeking through one level
2502 // of token factors to see if this is the case.
2503 bool ChainOk = false;
2504 if (Chain.getOpcode() == ISD::TokenFactor) {
2505 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2506 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2507 ChainOk = true;
2508 break;
2509 }
2510 }
2511
2512 if (!ChainOk) return false;
2513 }
2514
2515 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002516 return false;
2517
2518 // Make sure that one of the operands of the binop is the load, and that the
2519 // load folds into the binop.
2520 if (((StVal.getOperand(0) != TheLoad ||
2521 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2522 (StVal.getOperand(1) != TheLoad ||
2523 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2524 return false;
2525
2526 // Finally, check to see if this is one of the ops we can handle!
2527 static const unsigned ADDTAB[] = {
2528 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2529 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2530 };
2531 static const unsigned SUBTAB[] = {
2532 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2533 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2534 };
2535 static const unsigned ANDTAB[] = {
2536 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2537 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2538 };
2539 static const unsigned ORTAB[] = {
2540 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2541 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2542 };
2543 static const unsigned XORTAB[] = {
2544 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2545 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2546 };
2547 static const unsigned SHLTAB[] = {
2548 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2549 /*Have to put the reg in CL*/0, 0, 0,
2550 };
2551 static const unsigned SARTAB[] = {
2552 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2553 /*Have to put the reg in CL*/0, 0, 0,
2554 };
2555 static const unsigned SHRTAB[] = {
2556 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2557 /*Have to put the reg in CL*/0, 0, 0,
2558 };
2559
2560 const unsigned *TabPtr = 0;
2561 switch (StVal.getOpcode()) {
2562 default:
2563 std::cerr << "CANNOT [mem] op= val: ";
2564 StVal.Val->dump(); std::cerr << "\n";
2565 case ISD::MUL:
2566 case ISD::SDIV:
2567 case ISD::UDIV:
2568 case ISD::SREM:
2569 case ISD::UREM: return false;
2570
2571 case ISD::ADD: TabPtr = ADDTAB; break;
2572 case ISD::SUB: TabPtr = SUBTAB; break;
2573 case ISD::AND: TabPtr = ANDTAB; break;
2574 case ISD:: OR: TabPtr = ORTAB; break;
2575 case ISD::XOR: TabPtr = XORTAB; break;
2576 case ISD::SHL: TabPtr = SHLTAB; break;
2577 case ISD::SRA: TabPtr = SARTAB; break;
2578 case ISD::SRL: TabPtr = SHRTAB; break;
2579 }
2580
2581 // Handle: [mem] op= CST
2582 SDOperand Op0 = StVal.getOperand(0);
2583 SDOperand Op1 = StVal.getOperand(1);
2584 unsigned Opc;
2585 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2586 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2587 default: break;
2588 case MVT::i1:
2589 case MVT::i8: Opc = TabPtr[0]; break;
2590 case MVT::i16: Opc = TabPtr[1]; break;
2591 case MVT::i32: Opc = TabPtr[2]; break;
2592 }
2593
2594 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002595 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2596 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002597 Select(Chain);
2598
Chris Lattnere10269b2005-01-17 19:25:26 +00002599 X86AddressMode AM;
2600 if (getRegPressure(TheLoad.getOperand(0)) >
2601 getRegPressure(TheLoad.getOperand(1))) {
2602 Select(TheLoad.getOperand(0));
2603 SelectAddress(TheLoad.getOperand(1), AM);
2604 } else {
2605 SelectAddress(TheLoad.getOperand(1), AM);
2606 Select(TheLoad.getOperand(0));
2607 }
Chris Lattner5c659812005-01-17 22:10:42 +00002608
2609 if (StVal.getOpcode() == ISD::ADD) {
2610 if (CN->getValue() == 1) {
2611 switch (Op0.getValueType()) {
2612 default: break;
2613 case MVT::i8:
2614 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2615 return true;
2616 case MVT::i16: Opc = TabPtr[1];
2617 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2618 return true;
2619 case MVT::i32: Opc = TabPtr[2];
2620 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2621 return true;
2622 }
2623 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2624 switch (Op0.getValueType()) {
2625 default: break;
2626 case MVT::i8:
2627 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2628 return true;
2629 case MVT::i16: Opc = TabPtr[1];
2630 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2631 return true;
2632 case MVT::i32: Opc = TabPtr[2];
2633 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2634 return true;
2635 }
2636 }
2637 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002638
2639 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2640 return true;
2641 }
2642 }
2643
2644 // If we have [mem] = V op [mem], try to turn it into:
2645 // [mem] = [mem] op V.
2646 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2647 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2648 StVal.getOpcode() != ISD::SRL)
2649 std::swap(Op0, Op1);
2650
2651 if (Op0 != TheLoad) return false;
2652
2653 switch (Op0.getValueType()) {
2654 default: return false;
2655 case MVT::i1:
2656 case MVT::i8: Opc = TabPtr[3]; break;
2657 case MVT::i16: Opc = TabPtr[4]; break;
2658 case MVT::i32: Opc = TabPtr[5]; break;
2659 }
Chris Lattner5c659812005-01-17 22:10:42 +00002660
Chris Lattnerb422aea2005-01-18 17:35:28 +00002661 // Table entry doesn't exist?
2662 if (Opc == 0) return false;
2663
Chris Lattner4a108662005-01-18 03:51:59 +00002664 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2665 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002666 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002667 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002668
Chris Lattnere10269b2005-01-17 19:25:26 +00002669 X86AddressMode AM;
2670 SelectAddress(TheLoad.getOperand(1), AM);
2671 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002672 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002673 return true;
2674}
2675
2676
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002677void ISel::Select(SDOperand N) {
2678 unsigned Tmp1, Tmp2, Opc;
2679
2680 // FIXME: Disable for our current expansion model!
Chris Lattner4a108662005-01-18 03:51:59 +00002681 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002682 return; // Already selected.
2683
Chris Lattner989de032005-01-11 06:14:36 +00002684 SDNode *Node = N.Val;
2685
2686 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002687 default:
Chris Lattner989de032005-01-11 06:14:36 +00002688 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002689 assert(0 && "Node not handled yet!");
2690 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002691 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002692 if (Node->getNumOperands() == 2) {
2693 bool OneFirst =
2694 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2695 Select(Node->getOperand(OneFirst));
2696 Select(Node->getOperand(!OneFirst));
2697 } else {
2698 std::vector<std::pair<unsigned, unsigned> > OpsP;
2699 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2700 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2701 std::sort(OpsP.begin(), OpsP.end());
2702 std::reverse(OpsP.begin(), OpsP.end());
2703 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2704 Select(Node->getOperand(OpsP[i].second));
2705 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002706 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002707 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002708 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2709 Select(N.getOperand(0));
2710 Tmp1 = SelectExpr(N.getOperand(1));
2711 } else {
2712 Tmp1 = SelectExpr(N.getOperand(1));
2713 Select(N.getOperand(0));
2714 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002715 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002716
2717 if (Tmp1 != Tmp2) {
2718 switch (N.getOperand(1).getValueType()) {
2719 default: assert(0 && "Invalid type for operation!");
2720 case MVT::i1:
2721 case MVT::i8: Opc = X86::MOV8rr; break;
2722 case MVT::i16: Opc = X86::MOV16rr; break;
2723 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002724 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002725 }
2726 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2727 }
2728 return;
2729 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002730 switch (N.getNumOperands()) {
2731 default:
2732 assert(0 && "Unknown return instruction!");
2733 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002734 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2735 N.getOperand(2).getValueType() == MVT::i32 &&
2736 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002737 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2738 Tmp1 = SelectExpr(N.getOperand(1));
2739 Tmp2 = SelectExpr(N.getOperand(2));
2740 } else {
2741 Tmp2 = SelectExpr(N.getOperand(2));
2742 Tmp1 = SelectExpr(N.getOperand(1));
2743 }
2744 Select(N.getOperand(0));
2745
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002746 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2747 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2748 // Declare that EAX & EDX are live on exit.
2749 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2750 .addReg(X86::ESP);
2751 break;
2752 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002753 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2754 Select(N.getOperand(0));
2755 Tmp1 = SelectExpr(N.getOperand(1));
2756 } else {
2757 Tmp1 = SelectExpr(N.getOperand(1));
2758 Select(N.getOperand(0));
2759 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002760 switch (N.getOperand(1).getValueType()) {
2761 default: assert(0 && "All other types should have been promoted!!");
2762 case MVT::f64:
2763 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2764 // Declare that top-of-stack is live on exit
2765 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2766 break;
2767 case MVT::i32:
2768 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2769 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2770 break;
2771 }
2772 break;
2773 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002774 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002775 break;
2776 }
2777 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2778 return;
2779 case ISD::BR: {
2780 Select(N.getOperand(0));
2781 MachineBasicBlock *Dest =
2782 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2783 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2784 return;
2785 }
2786
2787 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002788 MachineBasicBlock *Dest =
2789 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002790
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002791 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2792 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002793 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2794 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2795 Select(N.getOperand(0));
2796 Tmp1 = SelectExpr(N.getOperand(1));
2797 } else {
2798 Tmp1 = SelectExpr(N.getOperand(1));
2799 Select(N.getOperand(0));
2800 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002801 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2802 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2803 }
Chris Lattner11333092005-01-11 03:11:44 +00002804
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002805 return;
2806 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002807
Chris Lattner4df0de92005-01-17 00:00:33 +00002808 case ISD::LOAD:
2809 // If this load could be folded into the only using instruction, and if it
2810 // is safe to emit the instruction here, try to do so now.
2811 if (Node->hasNUsesOfValue(1, 0)) {
2812 SDOperand TheVal = N.getValue(0);
2813 SDNode *User = 0;
2814 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2815 assert(UI != Node->use_end() && "Didn't find use!");
2816 SDNode *UN = *UI;
2817 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2818 if (UN->getOperand(i) == TheVal) {
2819 User = UN;
2820 goto FoundIt;
2821 }
2822 }
2823 FoundIt:
2824 // Only handle unary operators right now.
2825 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00002826 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002827 SelectExpr(SDOperand(User, 0));
2828 return;
2829 }
2830 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002831 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002832 SelectExpr(N);
2833 return;
2834
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002835 case ISD::EXTLOAD:
2836 case ISD::SEXTLOAD:
2837 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002838 case ISD::CALL:
2839 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002840 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002841 SelectExpr(N);
2842 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002843
2844 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2845 // On X86, we can represent all types except for Bool and Float natively.
2846 X86AddressMode AM;
2847 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002848 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2849 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2850 && "Unsupported TRUNCSTORE for this target!");
2851
2852 if (StoredTy == MVT::i16) {
2853 // FIXME: This is here just to allow testing. X86 doesn't really have a
2854 // TRUNCSTORE i16 operation, but this is required for targets that do not
2855 // have 16-bit integer registers. We occasionally disable 16-bit integer
2856 // registers to test the promotion code.
2857 Select(N.getOperand(0));
2858 Tmp1 = SelectExpr(N.getOperand(1));
2859 SelectAddress(N.getOperand(2), AM);
2860
2861 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2862 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2863 return;
2864 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002865
2866 // Store of constant bool?
2867 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2868 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2869 Select(N.getOperand(0));
2870 SelectAddress(N.getOperand(2), AM);
2871 } else {
2872 SelectAddress(N.getOperand(2), AM);
2873 Select(N.getOperand(0));
2874 }
2875 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2876 return;
2877 }
2878
2879 switch (StoredTy) {
2880 default: assert(0 && "Cannot truncstore this type!");
2881 case MVT::i1: Opc = X86::MOV8mr; break;
2882 case MVT::f32: Opc = X86::FST32m; break;
2883 }
2884
2885 std::vector<std::pair<unsigned, unsigned> > RP;
2886 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2887 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2888 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2889 std::sort(RP.begin(), RP.end());
2890
2891 for (unsigned i = 0; i != 3; ++i)
2892 switch (RP[2-i].second) {
2893 default: assert(0 && "Unknown operand number!");
2894 case 0: Select(N.getOperand(0)); break;
2895 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2896 case 2: SelectAddress(N.getOperand(2), AM); break;
2897 }
2898
2899 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2900 return;
2901 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002902 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002903 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002904
2905 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2906 Opc = 0;
2907 switch (CN->getValueType(0)) {
2908 default: assert(0 && "Invalid type for operation!");
2909 case MVT::i1:
2910 case MVT::i8: Opc = X86::MOV8mi; break;
2911 case MVT::i16: Opc = X86::MOV16mi; break;
2912 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002913 case MVT::f64: break;
2914 }
2915 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002916 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2917 Select(N.getOperand(0));
2918 SelectAddress(N.getOperand(2), AM);
2919 } else {
2920 SelectAddress(N.getOperand(2), AM);
2921 Select(N.getOperand(0));
2922 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002923 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2924 return;
2925 }
2926 }
Chris Lattner837caa72005-01-11 23:21:30 +00002927
2928 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00002929 if (TryToFoldLoadOpStore(Node))
2930 return;
Chris Lattner837caa72005-01-11 23:21:30 +00002931
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002932 switch (N.getOperand(1).getValueType()) {
2933 default: assert(0 && "Cannot store this type!");
2934 case MVT::i1:
2935 case MVT::i8: Opc = X86::MOV8mr; break;
2936 case MVT::i16: Opc = X86::MOV16mr; break;
2937 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002938 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002939 }
Chris Lattner11333092005-01-11 03:11:44 +00002940
2941 std::vector<std::pair<unsigned, unsigned> > RP;
2942 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2943 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2944 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2945 std::sort(RP.begin(), RP.end());
2946
2947 for (unsigned i = 0; i != 3; ++i)
2948 switch (RP[2-i].second) {
2949 default: assert(0 && "Unknown operand number!");
2950 case 0: Select(N.getOperand(0)); break;
2951 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002952 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002953 }
2954
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002955 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2956 return;
2957 }
2958 case ISD::ADJCALLSTACKDOWN:
2959 case ISD::ADJCALLSTACKUP:
2960 Select(N.getOperand(0));
2961 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2962
2963 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2964 X86::ADJCALLSTACKUP;
2965 BuildMI(BB, Opc, 1).addImm(Tmp1);
2966 return;
Chris Lattner989de032005-01-11 06:14:36 +00002967 case ISD::MEMSET: {
2968 Select(N.getOperand(0)); // Select the chain.
2969 unsigned Align =
2970 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2971 if (Align == 0) Align = 1;
2972
2973 // Turn the byte code into # iterations
2974 unsigned CountReg;
2975 unsigned Opcode;
2976 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2977 unsigned Val = ValC->getValue() & 255;
2978
2979 // If the value is a constant, then we can potentially use larger sets.
2980 switch (Align & 3) {
2981 case 2: // WORD aligned
2982 CountReg = MakeReg(MVT::i32);
2983 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2984 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2985 } else {
2986 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2987 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2988 }
2989 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2990 Opcode = X86::REP_STOSW;
2991 break;
2992 case 0: // DWORD aligned
2993 CountReg = MakeReg(MVT::i32);
2994 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2995 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2996 } else {
2997 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2998 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2999 }
3000 Val = (Val << 8) | Val;
3001 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3002 Opcode = X86::REP_STOSD;
3003 break;
3004 default: // BYTE aligned
3005 CountReg = SelectExpr(Node->getOperand(3));
3006 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3007 Opcode = X86::REP_STOSB;
3008 break;
3009 }
3010 } else {
3011 // If it's not a constant value we are storing, just fall back. We could
3012 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3013 unsigned ValReg = SelectExpr(Node->getOperand(2));
3014 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3015 CountReg = SelectExpr(Node->getOperand(3));
3016 Opcode = X86::REP_STOSB;
3017 }
3018
3019 // No matter what the alignment is, we put the source in ESI, the
3020 // destination in EDI, and the count in ECX.
3021 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3022 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3023 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3024 BuildMI(BB, Opcode, 0);
3025 return;
3026 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003027 case ISD::MEMCPY:
3028 Select(N.getOperand(0)); // Select the chain.
3029 unsigned Align =
3030 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3031 if (Align == 0) Align = 1;
3032
3033 // Turn the byte code into # iterations
3034 unsigned CountReg;
3035 unsigned Opcode;
3036 switch (Align & 3) {
3037 case 2: // WORD aligned
3038 CountReg = MakeReg(MVT::i32);
3039 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3040 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3041 } else {
3042 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3043 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3044 }
3045 Opcode = X86::REP_MOVSW;
3046 break;
3047 case 0: // DWORD aligned
3048 CountReg = MakeReg(MVT::i32);
3049 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3050 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3051 } else {
3052 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3053 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3054 }
3055 Opcode = X86::REP_MOVSD;
3056 break;
3057 default: // BYTE aligned
3058 CountReg = SelectExpr(Node->getOperand(3));
3059 Opcode = X86::REP_MOVSB;
3060 break;
3061 }
3062
3063 // No matter what the alignment is, we put the source in ESI, the
3064 // destination in EDI, and the count in ECX.
3065 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3066 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3067 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3068 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3069 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3070 BuildMI(BB, Opcode, 0);
3071 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003072 }
3073 assert(0 && "Should not be reached!");
3074}
3075
3076
3077/// createX86PatternInstructionSelector - This pass converts an LLVM function
3078/// into a machine code representation using pattern matching and a machine
3079/// description file.
3080///
3081FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
3082 return new ISel(TM);
3083}