blob: 2e5f91007c2e9f620cac4ae73d2a4132808874f5 [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
Chris Lattnere4d5c442005-03-15 04:54:21 +0000120 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000121 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 Lattnerb62e1e22005-01-21 19:46:38 +0000199 std::vector<SDOperand> Stores;
200
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000201 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
202 unsigned ArgReg;
203 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
204 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
205
206 switch (getValueType(Args[i].second)) {
207 default: assert(0 && "Unexpected ValueType for argument!");
208 case MVT::i1:
209 case MVT::i8:
210 case MVT::i16:
211 // Promote the integer to 32 bits. If the input type is signed use a
212 // sign extend, otherwise use a zero extend.
213 if (Args[i].second->isSigned())
214 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
215 else
216 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
217
218 // FALL THROUGH
219 case MVT::i32:
220 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000221 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
222 Args[i].first, PtrOff));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000223 ArgOffset += 4;
224 break;
225 case MVT::i64:
226 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000227 Stores.push_back(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 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000233 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000234 }
235
236 std::vector<MVT::ValueType> RetVals;
237 MVT::ValueType RetTyVT = getValueType(RetTy);
238 if (RetTyVT != MVT::isVoid)
239 RetVals.push_back(RetTyVT);
240 RetVals.push_back(MVT::Other);
241
Chris Lattner5188ad72005-01-08 19:28:19 +0000242 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000243 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000244 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
245 DAG.getConstant(NumBytes, getPointerTy()));
246 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000247}
248
Chris Lattner14824582005-01-09 00:01:27 +0000249std::pair<SDOperand, SDOperand>
250X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
251 // vastart just returns the address of the VarArgsFrameIndex slot.
252 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
253}
254
255std::pair<SDOperand,SDOperand> X86TargetLowering::
256LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
257 const Type *ArgTy, SelectionDAG &DAG) {
258 MVT::ValueType ArgVT = getValueType(ArgTy);
259 SDOperand Result;
260 if (!isVANext) {
261 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
262 } else {
263 unsigned Amt;
264 if (ArgVT == MVT::i32)
265 Amt = 4;
266 else {
267 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
268 "Other types should have been promoted for varargs!");
269 Amt = 8;
270 }
271 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
272 DAG.getConstant(Amt, VAList.getValueType()));
273 }
274 return std::make_pair(Result, Chain);
275}
276
277
278std::pair<SDOperand, SDOperand> X86TargetLowering::
279LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
280 SelectionDAG &DAG) {
281 SDOperand Result;
282 if (Depth) // Depths > 0 not supported yet!
283 Result = DAG.getConstant(0, getPointerTy());
284 else {
285 if (ReturnAddrIndex == 0) {
286 // Set up a frame object for the return address.
287 MachineFunction &MF = DAG.getMachineFunction();
288 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
289 }
290
291 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
292
293 if (!isFrameAddress)
294 // Just load the return address
295 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
296 else
297 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
298 DAG.getConstant(4, MVT::i32));
299 }
300 return std::make_pair(Result, Chain);
301}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000302
303
Chris Lattner98a8ba02005-01-18 01:06:26 +0000304namespace {
305 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
306 /// SDOperand's instead of register numbers for the leaves of the matched
307 /// tree.
308 struct X86ISelAddressMode {
309 enum {
310 RegBase,
311 FrameIndexBase,
312 } BaseType;
313
314 struct { // This is really a union, discriminated by BaseType!
315 SDOperand Reg;
316 int FrameIndex;
317 } Base;
318
319 unsigned Scale;
320 SDOperand IndexReg;
321 unsigned Disp;
322 GlobalValue *GV;
323
324 X86ISelAddressMode()
325 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
326 }
327 };
328}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000329
330
331namespace {
332 Statistic<>
333 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
334
335 //===--------------------------------------------------------------------===//
336 /// ISel - X86 specific code to select X86 machine instructions for
337 /// SelectionDAG operations.
338 ///
339 class ISel : public SelectionDAGISel {
340 /// ContainsFPCode - Every instruction we select that uses or defines a FP
341 /// register should set this to true.
342 bool ContainsFPCode;
343
344 /// X86Lowering - This object fully describes how to lower LLVM code to an
345 /// X86-specific SelectionDAG.
346 X86TargetLowering X86Lowering;
347
Chris Lattner11333092005-01-11 03:11:44 +0000348 /// RegPressureMap - This keeps an approximate count of the number of
349 /// registers required to evaluate each node in the graph.
350 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000351
352 /// ExprMap - As shared expressions are codegen'd, we keep track of which
353 /// vreg the value is produced in, so we only emit one copy of each compiled
354 /// tree.
355 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000356
357 public:
358 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
359 }
360
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000361 virtual const char *getPassName() const {
362 return "X86 Pattern Instruction Selection";
363 }
364
Chris Lattner11333092005-01-11 03:11:44 +0000365 unsigned getRegPressure(SDOperand O) {
366 return RegPressureMap[O.Val];
367 }
368 unsigned ComputeRegPressure(SDOperand O);
369
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000370 /// InstructionSelectBasicBlock - This callback is invoked by
371 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000372 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000373
Chris Lattner44129b52005-01-25 20:03:11 +0000374 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
375 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000376 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000377 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000378
Chris Lattner30ea1e92005-01-19 07:37:26 +0000379 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000380 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000381 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000382 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
383 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000384 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000385
386 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
387 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
388 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000389 void Select(SDOperand N);
390 };
391}
392
Chris Lattner7dbcb752005-01-12 04:21:28 +0000393/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
394/// when it has created a SelectionDAG for us to codegen.
395void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
396 // While we're doing this, keep track of whether we see any FP code for
397 // FP_REG_KILL insertion.
398 ContainsFPCode = false;
399
400 // Scan the PHI nodes that already are inserted into this basic block. If any
401 // of them is a PHI of a floating point value, we need to insert an
402 // FP_REG_KILL.
403 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
404 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
405 I != E; ++I) {
406 assert(I->getOpcode() == X86::PHI &&
407 "Isn't just PHI nodes?");
408 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
409 X86::RFPRegisterClass) {
410 ContainsFPCode = true;
411 break;
412 }
413 }
414
415 // Compute the RegPressureMap, which is an approximation for the number of
416 // registers required to compute each node.
417 ComputeRegPressure(DAG.getRoot());
418
419 // Codegen the basic block.
420 Select(DAG.getRoot());
421
422 // Finally, look at all of the successors of this block. If any contain a PHI
423 // node of FP type, we need to insert an FP_REG_KILL in this block.
424 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
425 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
426 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
427 I != E && I->getOpcode() == X86::PHI; ++I) {
428 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
429 X86::RFPRegisterClass) {
430 ContainsFPCode = true;
431 break;
432 }
433 }
434
435 // Insert FP_REG_KILL instructions into basic blocks that need them. This
436 // only occurs due to the floating point stackifier not being aggressive
437 // enough to handle arbitrary global stackification.
438 //
439 // Currently we insert an FP_REG_KILL instruction into each block that uses or
440 // defines a floating point virtual register.
441 //
442 // When the global register allocators (like linear scan) finally update live
443 // variable analysis, we can keep floating point values in registers across
444 // basic blocks. This will be a huge win, but we are waiting on the global
445 // allocators before we can do this.
446 //
447 if (ContainsFPCode && BB->succ_size()) {
448 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
449 ++NumFPKill;
450 }
451
452 // Clear state used for selection.
453 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000454 RegPressureMap.clear();
455}
456
457
Chris Lattner11333092005-01-11 03:11:44 +0000458// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
459// for the number of registers required to compute each node. This is basically
460// computing a generalized form of the Sethi-Ullman number for each node.
461unsigned ISel::ComputeRegPressure(SDOperand O) {
462 SDNode *N = O.Val;
463 unsigned &Result = RegPressureMap[N];
464 if (Result) return Result;
465
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000466 // FIXME: Should operations like CALL (which clobber lots o regs) have a
467 // higher fixed cost??
468
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000469 if (N->getNumOperands() == 0) {
470 Result = 1;
471 } else {
472 unsigned MaxRegUse = 0;
473 unsigned NumExtraMaxRegUsers = 0;
474 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
475 unsigned Regs;
476 if (N->getOperand(i).getOpcode() == ISD::Constant)
477 Regs = 0;
478 else
479 Regs = ComputeRegPressure(N->getOperand(i));
480 if (Regs > MaxRegUse) {
481 MaxRegUse = Regs;
482 NumExtraMaxRegUsers = 0;
483 } else if (Regs == MaxRegUse &&
484 N->getOperand(i).getValueType() != MVT::Other) {
485 ++NumExtraMaxRegUsers;
486 }
Chris Lattner11333092005-01-11 03:11:44 +0000487 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000488
489 if (O.getOpcode() != ISD::TokenFactor)
490 Result = MaxRegUse+NumExtraMaxRegUsers;
491 else
Chris Lattner869e0432005-01-17 23:02:13 +0000492 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000493 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000494
Chris Lattner837caa72005-01-11 23:21:30 +0000495 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000496 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000497}
498
Chris Lattnerbf52d492005-01-20 16:50:16 +0000499/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
500/// The DAG cannot have cycles in it, by definition, so the visited set is not
501/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
502/// reuse, so it prevents exponential cases.
503///
504static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
505 std::set<SDNode*> &Visited) {
506 if (N == Op) return true; // Found it.
507 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000508 if (Node->getNumOperands() == 0 || // Leaf?
509 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000510 if (!Visited.insert(Node).second) return false; // Already visited?
511
512 // Recurse for the first N-1 operands.
513 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
514 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
515 return true;
516
517 // Tail recurse for the last operand.
518 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
519}
520
Chris Lattner98a8ba02005-01-18 01:06:26 +0000521X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
522 X86AddressMode Result;
523
524 // If we need to emit two register operands, emit the one with the highest
525 // register pressure first.
526 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
527 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000528 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000529 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000530 std::set<SDNode*> Visited;
531 EmitBaseThenIndex = true;
532 // If Base ends up pointing to Index, we must emit index first. This is
533 // because of the way we fold loads, we may end up doing bad things with
534 // the folded add.
535 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
536 EmitBaseThenIndex = false;
537 } else {
538 std::set<SDNode*> Visited;
539 EmitBaseThenIndex = false;
540 // If Base ends up pointing to Index, we must emit index first. This is
541 // because of the way we fold loads, we may end up doing bad things with
542 // the folded add.
543 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
544 EmitBaseThenIndex = true;
545 }
546
547 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000548 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
549 Result.IndexReg = SelectExpr(IAM.IndexReg);
550 } else {
551 Result.IndexReg = SelectExpr(IAM.IndexReg);
552 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
553 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000554
Chris Lattner98a8ba02005-01-18 01:06:26 +0000555 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
556 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
557 } else if (IAM.IndexReg.Val) {
558 Result.IndexReg = SelectExpr(IAM.IndexReg);
559 }
560
561 switch (IAM.BaseType) {
562 case X86ISelAddressMode::RegBase:
563 Result.BaseType = X86AddressMode::RegBase;
564 break;
565 case X86ISelAddressMode::FrameIndexBase:
566 Result.BaseType = X86AddressMode::FrameIndexBase;
567 Result.Base.FrameIndex = IAM.Base.FrameIndex;
568 break;
569 default:
570 assert(0 && "Unknown base type!");
571 break;
572 }
573 Result.Scale = IAM.Scale;
574 Result.Disp = IAM.Disp;
575 Result.GV = IAM.GV;
576 return Result;
577}
578
579/// SelectAddress - Pattern match the maximal addressing mode for this node and
580/// emit all of the leaf registers.
581void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
582 X86ISelAddressMode IAM;
583 MatchAddress(N, IAM);
584 AM = SelectAddrExprs(IAM);
585}
586
587/// MatchAddress - Add the specified node to the specified addressing mode,
588/// returning true if it cannot be done. This just pattern matches for the
589/// addressing mode, it does not cause any code to be emitted. For that, use
590/// SelectAddress.
591bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000592 switch (N.getOpcode()) {
593 default: break;
594 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000595 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
596 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000597 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
598 return false;
599 }
600 break;
601 case ISD::GlobalAddress:
602 if (AM.GV == 0) {
603 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
604 return false;
605 }
606 break;
607 case ISD::Constant:
608 AM.Disp += cast<ConstantSDNode>(N)->getValue();
609 return false;
610 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000611 // We might have folded the load into this shift, so don't regen the value
612 // if so.
613 if (ExprMap.count(N)) break;
614
Chris Lattner98a8ba02005-01-18 01:06:26 +0000615 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000616 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
617 unsigned Val = CN->getValue();
618 if (Val == 1 || Val == 2 || Val == 3) {
619 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000620 SDOperand ShVal = N.Val->getOperand(0);
621
622 // Okay, we know that we have a scale by now. However, if the scaled
623 // value is an add of something and a constant, we can fold the
624 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000625 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000626 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000627 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000628 ConstantSDNode *AddVal =
629 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
630 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000631 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000632 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000633 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000634 return false;
635 }
636 }
637 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000638 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000639 // We might have folded the load into this mul, so don't regen the value if
640 // so.
641 if (ExprMap.count(N)) break;
642
Chris Lattner947d5442005-01-11 19:37:02 +0000643 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000644 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
645 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000646 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
647 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
648 AM.Scale = unsigned(CN->getValue())-1;
649
650 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000651 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000652
653 // Okay, we know that we have a scale by now. However, if the scaled
654 // value is an add of something and a constant, we can fold the
655 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000656 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000657 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000658 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000659 ConstantSDNode *AddVal =
660 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
661 AM.Disp += AddVal->getValue() * CN->getValue();
662 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000663 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000664 }
665
666 AM.IndexReg = AM.Base.Reg = Reg;
667 return false;
668 }
669 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000670
671 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000672 // We might have folded the load into this mul, so don't regen the value if
673 // so.
674 if (ExprMap.count(N)) break;
675
Chris Lattner98a8ba02005-01-18 01:06:26 +0000676 X86ISelAddressMode Backup = AM;
677 if (!MatchAddress(N.Val->getOperand(0), AM) &&
678 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000679 return false;
680 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000681 if (!MatchAddress(N.Val->getOperand(1), AM) &&
682 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000683 return false;
684 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000685 break;
686 }
687 }
688
Chris Lattnera95589b2005-01-11 04:40:19 +0000689 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000690 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000691 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000692 if (AM.IndexReg.Val == 0) {
693 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000694 AM.Scale = 1;
695 return false;
696 }
697
698 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000699 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000700 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000701
702 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000703 AM.BaseType = X86ISelAddressMode::RegBase;
704 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000705 return false;
706}
707
708/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
709/// assuming that the temporary registers are in the 8-bit register class.
710///
711/// Tmp1 = setcc1
712/// Tmp2 = setcc2
713/// DestReg = logicalop Tmp1, Tmp2
714///
715static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
716 unsigned SetCC2, unsigned LogicalOp,
717 unsigned DestReg) {
718 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
719 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
720 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
721 BuildMI(BB, SetCC1, 0, Tmp1);
722 BuildMI(BB, SetCC2, 0, Tmp2);
723 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
724}
725
726/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
727/// condition codes match the specified SetCCOpcode. Note that some conditions
728/// require multiple instructions to generate the correct value.
729static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
730 ISD::CondCode SetCCOpcode, bool isFP) {
731 unsigned Opc;
732 if (!isFP) {
733 switch (SetCCOpcode) {
734 default: assert(0 && "Illegal integer SetCC!");
735 case ISD::SETEQ: Opc = X86::SETEr; break;
736 case ISD::SETGT: Opc = X86::SETGr; break;
737 case ISD::SETGE: Opc = X86::SETGEr; break;
738 case ISD::SETLT: Opc = X86::SETLr; break;
739 case ISD::SETLE: Opc = X86::SETLEr; break;
740 case ISD::SETNE: Opc = X86::SETNEr; break;
741 case ISD::SETULT: Opc = X86::SETBr; break;
742 case ISD::SETUGT: Opc = X86::SETAr; break;
743 case ISD::SETULE: Opc = X86::SETBEr; break;
744 case ISD::SETUGE: Opc = X86::SETAEr; break;
745 }
746 } else {
747 // On a floating point condition, the flags are set as follows:
748 // ZF PF CF op
749 // 0 | 0 | 0 | X > Y
750 // 0 | 0 | 1 | X < Y
751 // 1 | 0 | 0 | X == Y
752 // 1 | 1 | 1 | unordered
753 //
754 switch (SetCCOpcode) {
755 default: assert(0 && "Invalid FP setcc!");
756 case ISD::SETUEQ:
757 case ISD::SETEQ:
758 Opc = X86::SETEr; // True if ZF = 1
759 break;
760 case ISD::SETOGT:
761 case ISD::SETGT:
762 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
763 break;
764 case ISD::SETOGE:
765 case ISD::SETGE:
766 Opc = X86::SETAEr; // True if CF = 0
767 break;
768 case ISD::SETULT:
769 case ISD::SETLT:
770 Opc = X86::SETBr; // True if CF = 1
771 break;
772 case ISD::SETULE:
773 case ISD::SETLE:
774 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
775 break;
776 case ISD::SETONE:
777 case ISD::SETNE:
778 Opc = X86::SETNEr; // True if ZF = 0
779 break;
780 case ISD::SETUO:
781 Opc = X86::SETPr; // True if PF = 1
782 break;
783 case ISD::SETO:
784 Opc = X86::SETNPr; // True if PF = 0
785 break;
786 case ISD::SETOEQ: // !PF & ZF
787 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
788 return;
789 case ISD::SETOLT: // !PF & CF
790 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
791 return;
792 case ISD::SETOLE: // !PF & (CF || ZF)
793 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
794 return;
795 case ISD::SETUGT: // PF | (!ZF & !CF)
796 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
797 return;
798 case ISD::SETUGE: // PF | !CF
799 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
800 return;
801 case ISD::SETUNE: // PF | !ZF
802 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
803 return;
804 }
805 }
806 BuildMI(BB, Opc, 0, DestReg);
807}
808
809
810/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
811/// the Dest block if the Cond condition is true. If we cannot fold this
812/// condition into the branch, return true.
813///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000814bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
815 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000816 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
817 // B) using two conditional branches instead of one condbr, two setcc's, and
818 // an or.
819 if ((Cond.getOpcode() == ISD::OR ||
820 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
821 // And and or set the flags for us, so there is no need to emit a TST of the
822 // result. It is only safe to do this if there is only a single use of the
823 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000824 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000825 SelectExpr(Cond);
826 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
827 return false;
828 }
829
830 // Codegen br not C -> JE.
831 if (Cond.getOpcode() == ISD::XOR)
832 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
833 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000834 unsigned CondR;
835 if (getRegPressure(Chain) > getRegPressure(Cond)) {
836 Select(Chain);
837 CondR = SelectExpr(Cond.Val->getOperand(0));
838 } else {
839 CondR = SelectExpr(Cond.Val->getOperand(0));
840 Select(Chain);
841 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000842 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
843 BuildMI(BB, X86::JE, 1).addMBB(Dest);
844 return false;
845 }
846
847 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
848 if (SetCC == 0)
849 return true; // Can only handle simple setcc's so far.
850
851 unsigned Opc;
852
853 // Handle integer conditions first.
854 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
855 switch (SetCC->getCondition()) {
856 default: assert(0 && "Illegal integer SetCC!");
857 case ISD::SETEQ: Opc = X86::JE; break;
858 case ISD::SETGT: Opc = X86::JG; break;
859 case ISD::SETGE: Opc = X86::JGE; break;
860 case ISD::SETLT: Opc = X86::JL; break;
861 case ISD::SETLE: Opc = X86::JLE; break;
862 case ISD::SETNE: Opc = X86::JNE; break;
863 case ISD::SETULT: Opc = X86::JB; break;
864 case ISD::SETUGT: Opc = X86::JA; break;
865 case ISD::SETULE: Opc = X86::JBE; break;
866 case ISD::SETUGE: Opc = X86::JAE; break;
867 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000868 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000869 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000870 BuildMI(BB, Opc, 1).addMBB(Dest);
871 return false;
872 }
873
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000874 unsigned Opc2 = 0; // Second branch if needed.
875
876 // On a floating point condition, the flags are set as follows:
877 // ZF PF CF op
878 // 0 | 0 | 0 | X > Y
879 // 0 | 0 | 1 | X < Y
880 // 1 | 0 | 0 | X == Y
881 // 1 | 1 | 1 | unordered
882 //
883 switch (SetCC->getCondition()) {
884 default: assert(0 && "Invalid FP setcc!");
885 case ISD::SETUEQ:
886 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
887 case ISD::SETOGT:
888 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
889 case ISD::SETOGE:
890 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
891 case ISD::SETULT:
892 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
893 case ISD::SETULE:
894 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
895 case ISD::SETONE:
896 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
897 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
898 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
899 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
900 Opc = X86::JA; // ZF = 0 & CF = 0
901 Opc2 = X86::JP; // PF = 1
902 break;
903 case ISD::SETUGE: // PF = 1 | CF = 0
904 Opc = X86::JAE; // CF = 0
905 Opc2 = X86::JP; // PF = 1
906 break;
907 case ISD::SETUNE: // PF = 1 | ZF = 0
908 Opc = X86::JNE; // ZF = 0
909 Opc2 = X86::JP; // PF = 1
910 break;
911 case ISD::SETOEQ: // PF = 0 & ZF = 1
912 //X86::JNP, X86::JE
913 //X86::AND8rr
914 return true; // FIXME: Emit more efficient code for this branch.
915 case ISD::SETOLT: // PF = 0 & CF = 1
916 //X86::JNP, X86::JB
917 //X86::AND8rr
918 return true; // FIXME: Emit more efficient code for this branch.
919 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
920 //X86::JNP, X86::JBE
921 //X86::AND8rr
922 return true; // FIXME: Emit more efficient code for this branch.
923 }
924
Chris Lattner6c07aee2005-01-11 04:06:27 +0000925 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000926 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000927 BuildMI(BB, Opc, 1).addMBB(Dest);
928 if (Opc2)
929 BuildMI(BB, Opc2, 1).addMBB(Dest);
930 return false;
931}
932
Chris Lattner24aad1b2005-01-10 22:10:13 +0000933/// EmitSelectCC - Emit code into BB that performs a select operation between
934/// the two registers RTrue and RFalse, generating a result into RDest. Return
935/// true if the fold cannot be performed.
936///
937void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
938 unsigned RTrue, unsigned RFalse, unsigned RDest) {
939 enum Condition {
940 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
941 NOT_SET
942 } CondCode = NOT_SET;
943
944 static const unsigned CMOVTAB16[] = {
945 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
946 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
947 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
948 };
949 static const unsigned CMOVTAB32[] = {
950 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
951 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
952 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
953 };
954 static const unsigned CMOVTABFP[] = {
955 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
956 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
957 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
958 };
959
960 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
961 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
962 switch (SetCC->getCondition()) {
963 default: assert(0 && "Unknown integer comparison!");
964 case ISD::SETEQ: CondCode = EQ; break;
965 case ISD::SETGT: CondCode = GT; break;
966 case ISD::SETGE: CondCode = GE; break;
967 case ISD::SETLT: CondCode = LT; break;
968 case ISD::SETLE: CondCode = LE; break;
969 case ISD::SETNE: CondCode = NE; break;
970 case ISD::SETULT: CondCode = B; break;
971 case ISD::SETUGT: CondCode = A; break;
972 case ISD::SETULE: CondCode = BE; break;
973 case ISD::SETUGE: CondCode = AE; break;
974 }
975 } else {
976 // On a floating point condition, the flags are set as follows:
977 // ZF PF CF op
978 // 0 | 0 | 0 | X > Y
979 // 0 | 0 | 1 | X < Y
980 // 1 | 0 | 0 | X == Y
981 // 1 | 1 | 1 | unordered
982 //
983 switch (SetCC->getCondition()) {
984 default: assert(0 && "Unknown FP comparison!");
985 case ISD::SETUEQ:
986 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
987 case ISD::SETOGT:
988 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
989 case ISD::SETOGE:
990 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
991 case ISD::SETULT:
992 case ISD::SETLT: CondCode = B; break; // True if CF = 1
993 case ISD::SETULE:
994 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
995 case ISD::SETONE:
996 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
997 case ISD::SETUO: CondCode = P; break; // True if PF = 1
998 case ISD::SETO: CondCode = NP; break; // True if PF = 0
999 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1000 case ISD::SETUGE: // PF = 1 | CF = 0
1001 case ISD::SETUNE: // PF = 1 | ZF = 0
1002 case ISD::SETOEQ: // PF = 0 & ZF = 1
1003 case ISD::SETOLT: // PF = 0 & CF = 1
1004 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1005 // We cannot emit this comparison as a single cmov.
1006 break;
1007 }
1008 }
1009 }
1010
1011 unsigned Opc = 0;
1012 if (CondCode != NOT_SET) {
1013 switch (SVT) {
1014 default: assert(0 && "Cannot select this type!");
1015 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1016 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001017 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001018 }
1019 }
1020
1021 // Finally, if we weren't able to fold this, just emit the condition and test
1022 // it.
1023 if (CondCode == NOT_SET || Opc == 0) {
1024 // Get the condition into the zero flag.
1025 unsigned CondReg = SelectExpr(Cond);
1026 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1027
1028 switch (SVT) {
1029 default: assert(0 && "Cannot select this type!");
1030 case MVT::i16: Opc = X86::CMOVE16rr; break;
1031 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001032 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001033 }
1034 } else {
1035 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001036 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001037 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001038 }
1039 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1040}
1041
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001042void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001043 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001044 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1045 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001046 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001047 switch (RHS.getValueType()) {
1048 default: break;
1049 case MVT::i1:
1050 case MVT::i8: Opc = X86::CMP8mi; break;
1051 case MVT::i16: Opc = X86::CMP16mi; break;
1052 case MVT::i32: Opc = X86::CMP32mi; break;
1053 }
1054 if (Opc) {
1055 X86AddressMode AM;
1056 EmitFoldedLoad(LHS, AM);
1057 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1058 return;
1059 }
1060 }
1061
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001062 switch (RHS.getValueType()) {
1063 default: break;
1064 case MVT::i1:
1065 case MVT::i8: Opc = X86::CMP8ri; break;
1066 case MVT::i16: Opc = X86::CMP16ri; break;
1067 case MVT::i32: Opc = X86::CMP32ri; break;
1068 }
1069 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001070 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001071 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1072 return;
1073 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001074 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1075 if (CN->isExactlyValue(+0.0) ||
1076 CN->isExactlyValue(-0.0)) {
1077 unsigned Reg = SelectExpr(LHS);
1078 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1079 BuildMI(BB, X86::FNSTSW8r, 0);
1080 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001081 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001082 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001083 }
1084
Chris Lattneref6806c2005-01-12 02:02:48 +00001085 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001086 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001087 switch (RHS.getValueType()) {
1088 default: break;
1089 case MVT::i1:
1090 case MVT::i8: Opc = X86::CMP8mr; break;
1091 case MVT::i16: Opc = X86::CMP16mr; break;
1092 case MVT::i32: Opc = X86::CMP32mr; break;
1093 }
1094 if (Opc) {
1095 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001096 EmitFoldedLoad(LHS, AM);
1097 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001098 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1099 return;
1100 }
1101 }
1102
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001103 switch (LHS.getValueType()) {
1104 default: assert(0 && "Cannot compare this value!");
1105 case MVT::i1:
1106 case MVT::i8: Opc = X86::CMP8rr; break;
1107 case MVT::i16: Opc = X86::CMP16rr; break;
1108 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001109 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001110 }
Chris Lattner11333092005-01-11 03:11:44 +00001111 unsigned Tmp1, Tmp2;
1112 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1113 Tmp1 = SelectExpr(LHS);
1114 Tmp2 = SelectExpr(RHS);
1115 } else {
1116 Tmp2 = SelectExpr(RHS);
1117 Tmp1 = SelectExpr(LHS);
1118 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001119 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1120}
1121
Chris Lattnera5ade062005-01-11 21:19:59 +00001122/// isFoldableLoad - Return true if this is a load instruction that can safely
1123/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001124bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1125 if (Op.getOpcode() == ISD::LOAD) {
1126 // FIXME: currently can't fold constant pool indexes.
1127 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1128 return false;
1129 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1130 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1131 // FIXME: currently can't fold constant pool indexes.
1132 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1133 return false;
1134 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001135 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001136 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001137
1138 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001139 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1140 if (ExprMap.count(Op.getValue(1))) return false;
1141 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001142 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001143
Chris Lattner4ff348b2005-01-17 06:26:58 +00001144 // If there is not just one use of its value, we cannot fold.
1145 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1146
1147 // Finally, we cannot fold the load into the operation if this would induce a
1148 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1149 // operand of the operation we are folding the load into) can possible use the
1150 // chain node defined by the load.
1151 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1152 std::set<SDNode*> Visited;
1153 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1154 return false;
1155 }
1156 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001157}
1158
Chris Lattner4ff348b2005-01-17 06:26:58 +00001159
Chris Lattnera5ade062005-01-11 21:19:59 +00001160/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1161/// and compute the address being loaded into AM.
1162void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1163 SDOperand Chain = Op.getOperand(0);
1164 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001165
Chris Lattnera5ade062005-01-11 21:19:59 +00001166 if (getRegPressure(Chain) > getRegPressure(Address)) {
1167 Select(Chain);
1168 SelectAddress(Address, AM);
1169 } else {
1170 SelectAddress(Address, AM);
1171 Select(Chain);
1172 }
1173
1174 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001175 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1176 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001177 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001178 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001179}
1180
Chris Lattner30ea1e92005-01-19 07:37:26 +00001181// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1182// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1183// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1184// return true.
1185bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001186 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1187 // good!
1188 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1189 std::swap(Op1, Op2); // Op1 is the SHL now.
1190 } else {
1191 return false; // No match
1192 }
1193
1194 SDOperand ShlVal = Op1.getOperand(0);
1195 SDOperand ShlAmt = Op1.getOperand(1);
1196 SDOperand ShrVal = Op2.getOperand(0);
1197 SDOperand ShrAmt = Op2.getOperand(1);
1198
Chris Lattner30ea1e92005-01-19 07:37:26 +00001199 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1200
Chris Lattner85716372005-01-19 06:18:43 +00001201 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1202 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1203 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001204 if (SubCST->getValue() == RegSize) {
1205 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001206 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001207 if (ShrVal == ShlVal) {
1208 unsigned Reg, ShAmt;
1209 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1210 Reg = SelectExpr(ShrVal);
1211 ShAmt = SelectExpr(ShrAmt);
1212 } else {
1213 ShAmt = SelectExpr(ShrAmt);
1214 Reg = SelectExpr(ShrVal);
1215 }
1216 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1217 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1218 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1219 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1220 return true;
1221 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001222 unsigned AReg, BReg;
1223 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001224 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001225 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001226 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001227 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001228 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001229 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001230 unsigned ShAmt = SelectExpr(ShrAmt);
1231 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1232 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1233 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001234 return true;
1235 }
1236 }
1237
Chris Lattner4053b1e2005-01-19 08:07:05 +00001238 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1239 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1240 if (SubCST->getValue() == RegSize) {
1241 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1242 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1243 if (ShrVal == ShlVal) {
1244 unsigned Reg, ShAmt;
1245 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1246 Reg = SelectExpr(ShrVal);
1247 ShAmt = SelectExpr(ShlAmt);
1248 } else {
1249 ShAmt = SelectExpr(ShlAmt);
1250 Reg = SelectExpr(ShrVal);
1251 }
1252 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1253 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1254 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1255 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1256 return true;
1257 } else if (RegSize != 8) {
1258 unsigned AReg, BReg;
1259 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001260 AReg = SelectExpr(ShlVal);
1261 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001262 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001263 BReg = SelectExpr(ShrVal);
1264 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001265 }
1266 unsigned ShAmt = SelectExpr(ShlAmt);
1267 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1268 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1269 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1270 return true;
1271 }
1272 }
Chris Lattner85716372005-01-19 06:18:43 +00001273
Chris Lattner4053b1e2005-01-19 08:07:05 +00001274 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1275 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1276 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1277 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1278 // (A >> 5) | (A << 27) --> ROR A, 5
1279 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1280 if (ShrVal == ShlVal) {
1281 unsigned Reg = SelectExpr(ShrVal);
1282 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1283 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1284 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1285 return true;
1286 } else if (RegSize != 8) {
1287 unsigned AReg, BReg;
1288 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001289 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001290 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001291 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001292 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001293 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001294 }
1295 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1296 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1297 .addImm(ShrCst->getValue());
1298 return true;
1299 }
1300 }
1301
Chris Lattner85716372005-01-19 06:18:43 +00001302 return false;
1303}
1304
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001305unsigned ISel::SelectExpr(SDOperand N) {
1306 unsigned Result;
1307 unsigned Tmp1, Tmp2, Tmp3;
1308 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001309 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001310 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001311
Chris Lattner7f2afac2005-01-14 22:37:41 +00001312 if (Node->getOpcode() == ISD::CopyFromReg) {
1313 // FIXME: Handle copy from physregs!
1314
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001315 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001316 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001317 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001318
1319 unsigned &Reg = ExprMap[N];
1320 if (Reg) return Reg;
1321
Chris Lattner19ad0622005-01-20 18:53:00 +00001322 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
1323 N.getOpcode() != ISD::SUB_PARTS)
Chris Lattnera5ade062005-01-11 21:19:59 +00001324 Reg = Result = (N.getValueType() != MVT::Other) ?
1325 MakeReg(N.getValueType()) : 1;
1326 else {
1327 // If this is a call instruction, make sure to prepare ALL of the result
1328 // values as well as the chain.
Chris Lattner19ad0622005-01-20 18:53:00 +00001329 if (N.getOpcode() == ISD::CALL) {
1330 if (Node->getNumValues() == 1)
1331 Reg = Result = 1; // Void call, just a chain.
1332 else {
1333 Result = MakeReg(Node->getValueType(0));
1334 ExprMap[N.getValue(0)] = Result;
1335 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1336 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1337 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
1338 }
1339 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001340 Result = MakeReg(Node->getValueType(0));
1341 ExprMap[N.getValue(0)] = Result;
Chris Lattner19ad0622005-01-20 18:53:00 +00001342 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001343 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001344 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001345 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001346
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001347 switch (N.getOpcode()) {
1348 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001349 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001350 assert(0 && "Node not handled!\n");
1351 case ISD::FrameIndex:
1352 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1353 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1354 return Result;
1355 case ISD::ConstantPool:
1356 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1357 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1358 return Result;
1359 case ISD::ConstantFP:
1360 ContainsFPCode = true;
1361 Tmp1 = Result; // Intermediate Register
1362 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1363 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1364 Tmp1 = MakeReg(MVT::f64);
1365
1366 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1367 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1368 BuildMI(BB, X86::FLD0, 0, Tmp1);
1369 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1370 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1371 BuildMI(BB, X86::FLD1, 0, Tmp1);
1372 else
1373 assert(0 && "Unexpected constant!");
1374 if (Tmp1 != Result)
1375 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1376 return Result;
1377 case ISD::Constant:
1378 switch (N.getValueType()) {
1379 default: assert(0 && "Cannot use constants of this type!");
1380 case MVT::i1:
1381 case MVT::i8: Opc = X86::MOV8ri; break;
1382 case MVT::i16: Opc = X86::MOV16ri; break;
1383 case MVT::i32: Opc = X86::MOV32ri; break;
1384 }
1385 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1386 return Result;
1387 case ISD::GlobalAddress: {
1388 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1389 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1390 return Result;
1391 }
1392 case ISD::ExternalSymbol: {
1393 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1394 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1395 return Result;
1396 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001397 case ISD::ZERO_EXTEND: {
1398 int DestIs16 = N.getValueType() == MVT::i16;
1399 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001400
1401 // FIXME: This hack is here for zero extension casts from bool to i8. This
1402 // would not be needed if bools were promoted by Legalize.
1403 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001404 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001405 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1406 return Result;
1407 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001408
Chris Lattner4ff348b2005-01-17 06:26:58 +00001409 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001410 static const unsigned Opc[3] = {
1411 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1412 };
1413
1414 X86AddressMode AM;
1415 EmitFoldedLoad(N.getOperand(0), AM);
1416 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1417
1418 return Result;
1419 }
1420
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001421 static const unsigned Opc[3] = {
1422 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1423 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001424 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001425 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1426 return Result;
1427 }
1428 case ISD::SIGN_EXTEND: {
1429 int DestIs16 = N.getValueType() == MVT::i16;
1430 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1431
Chris Lattner590d8002005-01-09 18:52:44 +00001432 // FIXME: Legalize should promote bools to i8!
1433 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1434 "Sign extend from bool not implemented!");
1435
Chris Lattner4ff348b2005-01-17 06:26:58 +00001436 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001437 static const unsigned Opc[3] = {
1438 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1439 };
1440
1441 X86AddressMode AM;
1442 EmitFoldedLoad(N.getOperand(0), AM);
1443 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1444 return Result;
1445 }
1446
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001447 static const unsigned Opc[3] = {
1448 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1449 };
1450 Tmp1 = SelectExpr(N.getOperand(0));
1451 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1452 return Result;
1453 }
1454 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001455 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001456 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001457 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001458 switch (N.getValueType()) {
1459 default: assert(0 && "Unknown truncate!");
1460 case MVT::i1:
1461 case MVT::i8: Opc = X86::MOV8rm; break;
1462 case MVT::i16: Opc = X86::MOV16rm; break;
1463 }
1464 X86AddressMode AM;
1465 EmitFoldedLoad(N.getOperand(0), AM);
1466 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1467 return Result;
1468 }
1469
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001470 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1471 // a move out of AX or AL.
1472 switch (N.getOperand(0).getValueType()) {
1473 default: assert(0 && "Unknown truncate!");
1474 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1475 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1476 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1477 }
1478 Tmp1 = SelectExpr(N.getOperand(0));
1479 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1480
1481 switch (N.getValueType()) {
1482 default: assert(0 && "Unknown truncate!");
1483 case MVT::i1:
1484 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1485 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1486 }
1487 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1488 return Result;
1489
Chris Lattner590d8002005-01-09 18:52:44 +00001490 case ISD::SINT_TO_FP:
1491 case ISD::UINT_TO_FP: {
1492 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001493 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001494
1495 // Promote the integer to a type supported by FLD. We do this because there
1496 // are no unsigned FLD instructions, so we must promote an unsigned value to
1497 // a larger signed value, then use FLD on the larger value.
1498 //
1499 MVT::ValueType PromoteType = MVT::Other;
1500 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1501 unsigned PromoteOpcode = 0;
1502 unsigned RealDestReg = Result;
1503 switch (SrcTy) {
1504 case MVT::i1:
1505 case MVT::i8:
1506 // We don't have the facilities for directly loading byte sized data from
1507 // memory (even signed). Promote it to 16 bits.
1508 PromoteType = MVT::i16;
1509 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1510 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1511 break;
1512 case MVT::i16:
1513 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1514 PromoteType = MVT::i32;
1515 PromoteOpcode = X86::MOVZX32rr16;
1516 }
1517 break;
1518 default:
1519 // Don't fild into the real destination.
1520 if (Node->getOpcode() == ISD::UINT_TO_FP)
1521 Result = MakeReg(Node->getValueType(0));
1522 break;
1523 }
1524
1525 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1526
1527 if (PromoteType != MVT::Other) {
1528 Tmp2 = MakeReg(PromoteType);
1529 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1530 SrcTy = PromoteType;
1531 Tmp1 = Tmp2;
1532 }
1533
1534 // Spill the integer to memory and reload it from there.
1535 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1536 MachineFunction *F = BB->getParent();
1537 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1538
1539 switch (SrcTy) {
1540 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001541 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001542 // FIXME: this won't work for cast [u]long to FP
1543 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1544 FrameIdx).addReg(Tmp1);
1545 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1546 FrameIdx, 4).addReg(Tmp1+1);
1547 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1548 break;
1549 case MVT::i32:
1550 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1551 FrameIdx).addReg(Tmp1);
1552 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1553 break;
1554 case MVT::i16:
1555 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1556 FrameIdx).addReg(Tmp1);
1557 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1558 break;
1559 default: break; // No promotion required.
1560 }
1561
Chris Lattner085c9952005-01-12 04:00:00 +00001562 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001563 // If this is a cast from uint -> double, we need to be careful when if
1564 // the "sign" bit is set. If so, we don't want to make a negative number,
1565 // we want to make a positive number. Emit code to add an offset if the
1566 // sign bit is set.
1567
1568 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1569 unsigned IsNeg = MakeReg(MVT::i32);
1570 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1571
1572 // Create a CP value that has the offset in one word and 0 in the other.
1573 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1574 0x4f80000000000000ULL);
1575 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1576 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1577 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1578
1579 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1580 // We need special handling for unsigned 64-bit integer sources. If the
1581 // input number has the "sign bit" set, then we loaded it incorrectly as a
1582 // negative 64-bit number. In this case, add an offset value.
1583
1584 // Emit a test instruction to see if the dynamic input value was signed.
1585 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1586
1587 // If the sign bit is set, get a pointer to an offset, otherwise get a
1588 // pointer to a zero.
1589 MachineConstantPool *CP = F->getConstantPool();
1590 unsigned Zero = MakeReg(MVT::i32);
1591 Constant *Null = Constant::getNullValue(Type::UIntTy);
1592 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1593 CP->getConstantPoolIndex(Null));
1594 unsigned Offset = MakeReg(MVT::i32);
1595 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1596
1597 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1598 CP->getConstantPoolIndex(OffsetCst));
1599 unsigned Addr = MakeReg(MVT::i32);
1600 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1601
1602 // Load the constant for an add. FIXME: this could make an 'fadd' that
1603 // reads directly from memory, but we don't support these yet.
1604 unsigned ConstReg = MakeReg(MVT::f64);
1605 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1606
1607 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1608 }
1609 return RealDestReg;
1610 }
1611 case ISD::FP_TO_SINT:
1612 case ISD::FP_TO_UINT: {
1613 // FIXME: Most of this grunt work should be done by legalize!
1614 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1615
1616 // Change the floating point control register to use "round towards zero"
1617 // mode when truncating to an integer value.
1618 //
1619 MachineFunction *F = BB->getParent();
1620 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1621 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1622
1623 // Load the old value of the high byte of the control word...
1624 unsigned HighPartOfCW = MakeReg(MVT::i8);
1625 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1626 CWFrameIdx, 1);
1627
1628 // Set the high part to be round to zero...
1629 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1630 CWFrameIdx, 1).addImm(12);
1631
1632 // Reload the modified control word now...
1633 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1634
1635 // Restore the memory image of control word to original value
1636 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1637 CWFrameIdx, 1).addReg(HighPartOfCW);
1638
1639 // We don't have the facilities for directly storing byte sized data to
1640 // memory. Promote it to 16 bits. We also must promote unsigned values to
1641 // larger classes because we only have signed FP stores.
1642 MVT::ValueType StoreClass = Node->getValueType(0);
1643 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1644 switch (StoreClass) {
1645 case MVT::i8: StoreClass = MVT::i16; break;
1646 case MVT::i16: StoreClass = MVT::i32; break;
1647 case MVT::i32: StoreClass = MVT::i64; break;
1648 // The following treatment of cLong may not be perfectly right,
1649 // but it survives chains of casts of the form
1650 // double->ulong->double.
1651 case MVT::i64: StoreClass = MVT::i64; break;
1652 default: assert(0 && "Unknown store class!");
1653 }
1654
1655 // Spill the integer to memory and reload it from there.
1656 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1657 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1658
1659 switch (StoreClass) {
1660 default: assert(0 && "Unknown store class!");
1661 case MVT::i16:
1662 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1663 break;
1664 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001665 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001666 break;
1667 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001668 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001669 break;
1670 }
1671
1672 switch (Node->getValueType(0)) {
1673 default:
1674 assert(0 && "Unknown integer type!");
1675 case MVT::i64:
1676 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001677 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001678 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1679 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1680 case MVT::i32:
1681 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1682 break;
1683 case MVT::i16:
1684 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1685 break;
1686 case MVT::i8:
1687 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1688 break;
1689 }
1690
1691 // Reload the original control word now.
1692 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1693 return Result;
1694 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001695 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001696 Op0 = N.getOperand(0);
1697 Op1 = N.getOperand(1);
1698
Chris Lattner44129b52005-01-25 20:03:11 +00001699 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001700 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001701 goto FoldAdd;
1702 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001703
Chris Lattner44129b52005-01-25 20:03:11 +00001704 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001705 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001706 switch (N.getValueType()) {
1707 default: assert(0 && "Cannot add this type!");
1708 case MVT::i1:
1709 case MVT::i8: Opc = X86::ADD8rm; break;
1710 case MVT::i16: Opc = X86::ADD16rm; break;
1711 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001712 case MVT::f64:
1713 // For F64, handle promoted load operations (from F32) as well!
1714 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1715 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001716 }
1717 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001718 EmitFoldedLoad(Op1, AM);
1719 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001720 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1721 return Result;
1722 }
1723
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001724 // See if we can codegen this as an LEA to fold operations together.
1725 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001726 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001727 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001728 MatchAddress(N, AM);
1729 ExprMap[N] = Result;
1730
1731 // If this is not just an add, emit the LEA. For a simple add (like
1732 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1733 // leave this as LEA, then peephole it to 'ADD' after two address elim
1734 // happens.
1735 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1736 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1737 X86AddressMode XAM = SelectAddrExprs(AM);
1738 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1739 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001740 }
1741 }
Chris Lattner11333092005-01-11 03:11:44 +00001742
Chris Lattnera5ade062005-01-11 21:19:59 +00001743 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001744 Opc = 0;
1745 if (CN->getValue() == 1) { // add X, 1 -> inc X
1746 switch (N.getValueType()) {
1747 default: assert(0 && "Cannot integer add this type!");
1748 case MVT::i8: Opc = X86::INC8r; break;
1749 case MVT::i16: Opc = X86::INC16r; break;
1750 case MVT::i32: Opc = X86::INC32r; break;
1751 }
1752 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1753 switch (N.getValueType()) {
1754 default: assert(0 && "Cannot integer add this type!");
1755 case MVT::i8: Opc = X86::DEC8r; break;
1756 case MVT::i16: Opc = X86::DEC16r; break;
1757 case MVT::i32: Opc = X86::DEC32r; break;
1758 }
1759 }
1760
1761 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001762 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001763 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1764 return Result;
1765 }
1766
1767 switch (N.getValueType()) {
1768 default: assert(0 && "Cannot add this type!");
1769 case MVT::i8: Opc = X86::ADD8ri; break;
1770 case MVT::i16: Opc = X86::ADD16ri; break;
1771 case MVT::i32: Opc = X86::ADD32ri; break;
1772 }
1773 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001774 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001775 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1776 return Result;
1777 }
1778 }
1779
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001780 switch (N.getValueType()) {
1781 default: assert(0 && "Cannot add this type!");
1782 case MVT::i8: Opc = X86::ADD8rr; break;
1783 case MVT::i16: Opc = X86::ADD16rr; break;
1784 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001785 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001786 }
Chris Lattner11333092005-01-11 03:11:44 +00001787
Chris Lattnera5ade062005-01-11 21:19:59 +00001788 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1789 Tmp1 = SelectExpr(Op0);
1790 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001791 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001792 Tmp2 = SelectExpr(Op1);
1793 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001794 }
1795
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001796 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1797 return Result;
1798 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001799 case ISD::MUL:
1800 case ISD::AND:
1801 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001802 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001803 static const unsigned SUBTab[] = {
1804 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1805 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1806 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1807 };
1808 static const unsigned MULTab[] = {
1809 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1810 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1811 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1812 };
1813 static const unsigned ANDTab[] = {
1814 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1815 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1816 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1817 };
1818 static const unsigned ORTab[] = {
1819 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1820 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1821 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1822 };
1823 static const unsigned XORTab[] = {
1824 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1825 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1826 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1827 };
1828
1829 Op0 = Node->getOperand(0);
1830 Op1 = Node->getOperand(1);
1831
Chris Lattner30ea1e92005-01-19 07:37:26 +00001832 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1833 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001834 return Result;
1835
1836 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001837 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1838 if (CN->isNullValue()) { // 0 - N -> neg N
1839 switch (N.getValueType()) {
1840 default: assert(0 && "Cannot sub this type!");
1841 case MVT::i1:
1842 case MVT::i8: Opc = X86::NEG8r; break;
1843 case MVT::i16: Opc = X86::NEG16r; break;
1844 case MVT::i32: Opc = X86::NEG32r; break;
1845 }
1846 Tmp1 = SelectExpr(N.getOperand(1));
1847 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1848 return Result;
1849 }
1850
Chris Lattnera5ade062005-01-11 21:19:59 +00001851 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1852 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001853 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001854 switch (N.getValueType()) {
1855 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001856 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001857 case MVT::i8: Opc = X86::NOT8r; break;
1858 case MVT::i16: Opc = X86::NOT16r; break;
1859 case MVT::i32: Opc = X86::NOT32r; break;
1860 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001861 if (Opc) {
1862 Tmp1 = SelectExpr(Op0);
1863 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1864 return Result;
1865 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001866 }
1867
Chris Lattner2a4e5082005-01-17 06:48:02 +00001868 // Fold common multiplies into LEA instructions.
1869 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1870 switch ((int)CN->getValue()) {
1871 default: break;
1872 case 3:
1873 case 5:
1874 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001875 // Remove N from exprmap so SelectAddress doesn't get confused.
1876 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001877 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001878 SelectAddress(N, AM);
1879 // Restore it to the map.
1880 ExprMap[N] = Result;
1881 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1882 return Result;
1883 }
1884 }
1885
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001886 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001887 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001888 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001889 case MVT::i8: Opc = 0; break;
1890 case MVT::i16: Opc = 1; break;
1891 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001892 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001893 switch (Node->getOpcode()) {
1894 default: assert(0 && "Unreachable!");
1895 case ISD::SUB: Opc = SUBTab[Opc]; break;
1896 case ISD::MUL: Opc = MULTab[Opc]; break;
1897 case ISD::AND: Opc = ANDTab[Opc]; break;
1898 case ISD::OR: Opc = ORTab[Opc]; break;
1899 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001900 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001901 if (Opc) { // Can't fold MUL:i8 R, imm
1902 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001903 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1904 return Result;
1905 }
1906 }
Chris Lattner11333092005-01-11 03:11:44 +00001907
Chris Lattner44129b52005-01-25 20:03:11 +00001908 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00001909 if (Node->getOpcode() != ISD::SUB) {
1910 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001911 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001912 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00001913 // For FP, emit 'reverse' subract, with a memory operand.
1914 if (N.getValueType() == MVT::f64) {
1915 if (Op0.getOpcode() == ISD::EXTLOAD)
1916 Opc = X86::FSUBR32m;
1917 else
1918 Opc = X86::FSUBR64m;
1919
Chris Lattnera5ade062005-01-11 21:19:59 +00001920 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001921 EmitFoldedLoad(Op0, AM);
1922 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001923 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1924 return Result;
1925 }
1926 }
1927
Chris Lattner44129b52005-01-25 20:03:11 +00001928 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001929 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001930 switch (N.getValueType()) {
1931 default: assert(0 && "Cannot operate on this type!");
1932 case MVT::i1:
1933 case MVT::i8: Opc = 5; break;
1934 case MVT::i16: Opc = 6; break;
1935 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001936 // For F64, handle promoted load operations (from F32) as well!
1937 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001938 }
1939 switch (Node->getOpcode()) {
1940 default: assert(0 && "Unreachable!");
1941 case ISD::SUB: Opc = SUBTab[Opc]; break;
1942 case ISD::MUL: Opc = MULTab[Opc]; break;
1943 case ISD::AND: Opc = ANDTab[Opc]; break;
1944 case ISD::OR: Opc = ORTab[Opc]; break;
1945 case ISD::XOR: Opc = XORTab[Opc]; break;
1946 }
1947
1948 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001949 EmitFoldedLoad(Op1, AM);
1950 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001951 if (Opc) {
1952 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1953 } else {
1954 assert(Node->getOpcode() == ISD::MUL &&
1955 N.getValueType() == MVT::i8 && "Unexpected situation!");
1956 // Must use the MUL instruction, which forces use of AL.
1957 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1958 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1959 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1960 }
1961 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001962 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001963
1964 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1965 Tmp1 = SelectExpr(Op0);
1966 Tmp2 = SelectExpr(Op1);
1967 } else {
1968 Tmp2 = SelectExpr(Op1);
1969 Tmp1 = SelectExpr(Op0);
1970 }
1971
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001972 switch (N.getValueType()) {
1973 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001974 case MVT::i1:
1975 case MVT::i8: Opc = 10; break;
1976 case MVT::i16: Opc = 11; break;
1977 case MVT::i32: Opc = 12; break;
1978 case MVT::f32: Opc = 13; break;
1979 case MVT::f64: Opc = 14; break;
1980 }
1981 switch (Node->getOpcode()) {
1982 default: assert(0 && "Unreachable!");
1983 case ISD::SUB: Opc = SUBTab[Opc]; break;
1984 case ISD::MUL: Opc = MULTab[Opc]; break;
1985 case ISD::AND: Opc = ANDTab[Opc]; break;
1986 case ISD::OR: Opc = ORTab[Opc]; break;
1987 case ISD::XOR: Opc = XORTab[Opc]; break;
1988 }
1989 if (Opc) {
1990 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1991 } else {
1992 assert(Node->getOpcode() == ISD::MUL &&
1993 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001994 // Must use the MUL instruction, which forces use of AL.
1995 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1996 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1997 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001998 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001999 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002000 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002001 case ISD::ADD_PARTS:
2002 case ISD::SUB_PARTS: {
2003 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2004 "Not an i64 add/sub!");
2005 // Emit all of the operands.
2006 std::vector<unsigned> InVals;
2007 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2008 InVals.push_back(SelectExpr(N.getOperand(i)));
2009 if (N.getOpcode() == ISD::ADD_PARTS) {
2010 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2011 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2012 } else {
2013 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2014 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2015 }
2016 return Result+N.ResNo;
2017 }
2018
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002019 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002020 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2021 Tmp2 = SelectExpr(N.getOperand(1));
2022 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002023 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002024 Tmp3 = SelectExpr(N.getOperand(2));
2025 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002026 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002027 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2028 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002029
2030 case ISD::SDIV:
2031 case ISD::UDIV:
2032 case ISD::SREM:
2033 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002034 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2035 "We don't support this operator!");
2036
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002037 if (N.getOpcode() == ISD::SDIV)
Chris Lattner3576c842005-01-25 20:35:10 +00002038
2039 // We can fold loads into FpDIVs, but not really into any others.
2040 if (N.getValueType() == MVT::f64) {
2041 // Check for reversed and unreversed DIV.
2042 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2043 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2044 Opc = X86::FDIVR32m;
2045 else
2046 Opc = X86::FDIVR64m;
2047 X86AddressMode AM;
2048 EmitFoldedLoad(N.getOperand(0), AM);
2049 Tmp1 = SelectExpr(N.getOperand(1));
2050 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2051 return Result;
2052 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2053 N.getOperand(1).getOpcode() == ISD::LOAD) {
2054 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2055 Opc = X86::FDIV32m;
2056 else
2057 Opc = X86::FDIV64m;
2058 X86AddressMode AM;
2059 EmitFoldedLoad(N.getOperand(1), AM);
2060 Tmp1 = SelectExpr(N.getOperand(0));
2061 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2062 return Result;
2063 }
2064 }
2065
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002066 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2067 // FIXME: These special cases should be handled by the lowering impl!
2068 unsigned RHS = CN->getValue();
2069 bool isNeg = false;
2070 if ((int)RHS < 0) {
2071 isNeg = true;
2072 RHS = -RHS;
2073 }
2074 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2075 unsigned Log = log2(RHS);
2076 unsigned TmpReg = MakeReg(N.getValueType());
2077 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2078 switch (N.getValueType()) {
2079 default: assert("Unknown type to signed divide!");
2080 case MVT::i8:
2081 SAROpc = X86::SAR8ri;
2082 SHROpc = X86::SHR8ri;
2083 ADDOpc = X86::ADD8rr;
2084 NEGOpc = X86::NEG8r;
2085 break;
2086 case MVT::i16:
2087 SAROpc = X86::SAR16ri;
2088 SHROpc = X86::SHR16ri;
2089 ADDOpc = X86::ADD16rr;
2090 NEGOpc = X86::NEG16r;
2091 break;
2092 case MVT::i32:
2093 SAROpc = X86::SAR32ri;
2094 SHROpc = X86::SHR32ri;
2095 ADDOpc = X86::ADD32rr;
2096 NEGOpc = X86::NEG32r;
2097 break;
2098 }
Chris Lattner11333092005-01-11 03:11:44 +00002099 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002100 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2101 unsigned TmpReg2 = MakeReg(N.getValueType());
2102 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2103 unsigned TmpReg3 = MakeReg(N.getValueType());
2104 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
2105
2106 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2107 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2108 if (isNeg)
2109 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2110 return Result;
2111 }
2112 }
2113
Chris Lattner11333092005-01-11 03:11:44 +00002114 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2115 Tmp1 = SelectExpr(N.getOperand(0));
2116 Tmp2 = SelectExpr(N.getOperand(1));
2117 } else {
2118 Tmp2 = SelectExpr(N.getOperand(1));
2119 Tmp1 = SelectExpr(N.getOperand(0));
2120 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002121
2122 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2123 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2124 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2125 switch (N.getValueType()) {
2126 default: assert(0 && "Cannot sdiv this type!");
2127 case MVT::i8:
2128 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2129 LoReg = X86::AL;
2130 HiReg = X86::AH;
2131 MovOpcode = X86::MOV8rr;
2132 ClrOpcode = X86::MOV8ri;
2133 SExtOpcode = X86::CBW;
2134 break;
2135 case MVT::i16:
2136 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2137 LoReg = X86::AX;
2138 HiReg = X86::DX;
2139 MovOpcode = X86::MOV16rr;
2140 ClrOpcode = X86::MOV16ri;
2141 SExtOpcode = X86::CWD;
2142 break;
2143 case MVT::i32:
2144 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002145 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002146 HiReg = X86::EDX;
2147 MovOpcode = X86::MOV32rr;
2148 ClrOpcode = X86::MOV32ri;
2149 SExtOpcode = X86::CDQ;
2150 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002151 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002152 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002153 return Result;
2154 }
2155
2156 // Set up the low part.
2157 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2158
2159 if (isSigned) {
2160 // Sign extend the low part into the high part.
2161 BuildMI(BB, SExtOpcode, 0);
2162 } else {
2163 // Zero out the high part, effectively zero extending the input.
2164 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2165 }
2166
2167 // Emit the DIV/IDIV instruction.
2168 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
2169
2170 // Get the result of the divide or rem.
2171 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2172 return Result;
2173 }
2174
2175 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002176 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002177 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2178 switch (N.getValueType()) {
2179 default: assert(0 && "Cannot shift this type!");
2180 case MVT::i8: Opc = X86::ADD8rr; break;
2181 case MVT::i16: Opc = X86::ADD16rr; break;
2182 case MVT::i32: Opc = X86::ADD32rr; break;
2183 }
2184 Tmp1 = SelectExpr(N.getOperand(0));
2185 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2186 return Result;
2187 }
2188
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002189 switch (N.getValueType()) {
2190 default: assert(0 && "Cannot shift this type!");
2191 case MVT::i8: Opc = X86::SHL8ri; break;
2192 case MVT::i16: Opc = X86::SHL16ri; break;
2193 case MVT::i32: Opc = X86::SHL32ri; break;
2194 }
Chris Lattner11333092005-01-11 03:11:44 +00002195 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002196 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2197 return Result;
2198 }
Chris Lattner11333092005-01-11 03:11:44 +00002199
2200 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2201 Tmp1 = SelectExpr(N.getOperand(0));
2202 Tmp2 = SelectExpr(N.getOperand(1));
2203 } else {
2204 Tmp2 = SelectExpr(N.getOperand(1));
2205 Tmp1 = SelectExpr(N.getOperand(0));
2206 }
2207
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002208 switch (N.getValueType()) {
2209 default: assert(0 && "Cannot shift this type!");
2210 case MVT::i8 : Opc = X86::SHL8rCL; break;
2211 case MVT::i16: Opc = X86::SHL16rCL; break;
2212 case MVT::i32: Opc = X86::SHL32rCL; break;
2213 }
2214 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2215 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2216 return Result;
2217 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002218 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2219 switch (N.getValueType()) {
2220 default: assert(0 && "Cannot shift this type!");
2221 case MVT::i8: Opc = X86::SHR8ri; break;
2222 case MVT::i16: Opc = X86::SHR16ri; break;
2223 case MVT::i32: Opc = X86::SHR32ri; break;
2224 }
Chris Lattner11333092005-01-11 03:11:44 +00002225 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002226 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2227 return Result;
2228 }
Chris Lattner11333092005-01-11 03:11:44 +00002229
2230 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2231 Tmp1 = SelectExpr(N.getOperand(0));
2232 Tmp2 = SelectExpr(N.getOperand(1));
2233 } else {
2234 Tmp2 = SelectExpr(N.getOperand(1));
2235 Tmp1 = SelectExpr(N.getOperand(0));
2236 }
2237
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002238 switch (N.getValueType()) {
2239 default: assert(0 && "Cannot shift this type!");
2240 case MVT::i8 : Opc = X86::SHR8rCL; break;
2241 case MVT::i16: Opc = X86::SHR16rCL; break;
2242 case MVT::i32: Opc = X86::SHR32rCL; break;
2243 }
2244 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2245 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2246 return Result;
2247 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002248 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2249 switch (N.getValueType()) {
2250 default: assert(0 && "Cannot shift this type!");
2251 case MVT::i8: Opc = X86::SAR8ri; break;
2252 case MVT::i16: Opc = X86::SAR16ri; break;
2253 case MVT::i32: Opc = X86::SAR32ri; break;
2254 }
Chris Lattner11333092005-01-11 03:11:44 +00002255 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002256 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2257 return Result;
2258 }
Chris Lattner11333092005-01-11 03:11:44 +00002259
2260 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2261 Tmp1 = SelectExpr(N.getOperand(0));
2262 Tmp2 = SelectExpr(N.getOperand(1));
2263 } else {
2264 Tmp2 = SelectExpr(N.getOperand(1));
2265 Tmp1 = SelectExpr(N.getOperand(0));
2266 }
2267
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002268 switch (N.getValueType()) {
2269 default: assert(0 && "Cannot shift this type!");
2270 case MVT::i8 : Opc = X86::SAR8rCL; break;
2271 case MVT::i16: Opc = X86::SAR16rCL; break;
2272 case MVT::i32: Opc = X86::SAR32rCL; break;
2273 }
2274 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2275 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2276 return Result;
2277
2278 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002279 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002280 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2281 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2282 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002283 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002284 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002285 if (Result != 1) { // Generate the token
2286 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2287 assert(0 && "Load already emitted!?");
2288 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002289 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2290
Chris Lattner5188ad72005-01-08 19:28:19 +00002291 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002292 default: assert(0 && "Cannot load this type!");
2293 case MVT::i1:
2294 case MVT::i8: Opc = X86::MOV8rm; break;
2295 case MVT::i16: Opc = X86::MOV16rm; break;
2296 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002297 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2298 }
Chris Lattner11333092005-01-11 03:11:44 +00002299
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002300 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002301 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002302 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2303 } else {
2304 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002305
2306 SDOperand Chain = N.getOperand(0);
2307 SDOperand Address = N.getOperand(1);
2308 if (getRegPressure(Chain) > getRegPressure(Address)) {
2309 Select(Chain);
2310 SelectAddress(Address, AM);
2311 } else {
2312 SelectAddress(Address, AM);
2313 Select(Chain);
2314 }
2315
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002316 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2317 }
2318 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002319
2320 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2321 case ISD::ZEXTLOAD: {
2322 // Make sure we generate both values.
2323 if (Result != 1)
2324 ExprMap[N.getValue(1)] = 1; // Generate the token
2325 else
2326 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2327
Chris Lattnerda2ce112005-01-16 07:34:08 +00002328 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2329 if (Node->getValueType(0) == MVT::f64) {
2330 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2331 "Bad EXTLOAD!");
2332 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2333 CP->getIndex());
2334 return Result;
2335 }
2336
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002337 X86AddressMode AM;
2338 if (getRegPressure(Node->getOperand(0)) >
2339 getRegPressure(Node->getOperand(1))) {
2340 Select(Node->getOperand(0)); // chain
2341 SelectAddress(Node->getOperand(1), AM);
2342 } else {
2343 SelectAddress(Node->getOperand(1), AM);
2344 Select(Node->getOperand(0)); // chain
2345 }
2346
2347 switch (Node->getValueType(0)) {
2348 default: assert(0 && "Unknown type to sign extend to.");
2349 case MVT::f64:
2350 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2351 "Bad EXTLOAD!");
2352 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2353 break;
2354 case MVT::i32:
2355 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2356 default:
2357 assert(0 && "Bad zero extend!");
2358 case MVT::i1:
2359 case MVT::i8:
2360 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2361 break;
2362 case MVT::i16:
2363 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2364 break;
2365 }
2366 break;
2367 case MVT::i16:
2368 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2369 "Bad zero extend!");
2370 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2371 break;
2372 case MVT::i8:
2373 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2374 "Bad zero extend!");
2375 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2376 break;
2377 }
2378 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002379 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002380 case ISD::SEXTLOAD: {
2381 // Make sure we generate both values.
2382 if (Result != 1)
2383 ExprMap[N.getValue(1)] = 1; // Generate the token
2384 else
2385 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2386
2387 X86AddressMode AM;
2388 if (getRegPressure(Node->getOperand(0)) >
2389 getRegPressure(Node->getOperand(1))) {
2390 Select(Node->getOperand(0)); // chain
2391 SelectAddress(Node->getOperand(1), AM);
2392 } else {
2393 SelectAddress(Node->getOperand(1), AM);
2394 Select(Node->getOperand(0)); // chain
2395 }
2396
2397 switch (Node->getValueType(0)) {
2398 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2399 default: assert(0 && "Unknown type to sign extend to.");
2400 case MVT::i32:
2401 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2402 default:
2403 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2404 case MVT::i8:
2405 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2406 break;
2407 case MVT::i16:
2408 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2409 break;
2410 }
2411 break;
2412 case MVT::i16:
2413 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2414 "Cannot sign extend from bool!");
2415 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2416 break;
2417 }
2418 return Result;
2419 }
2420
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002421 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002422 // Generate both result values.
2423 if (Result != 1)
2424 ExprMap[N.getValue(1)] = 1; // Generate the token
2425 else
2426 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2427
2428 // FIXME: We are currently ignoring the requested alignment for handling
2429 // greater than the stack alignment. This will need to be revisited at some
2430 // point. Align = N.getOperand(2);
2431
2432 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2433 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2434 std::cerr << "Cannot allocate stack object with greater alignment than"
2435 << " the stack alignment yet!";
2436 abort();
2437 }
2438
2439 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002440 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002441 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2442 .addImm(CN->getValue());
2443 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002444 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2445 Select(N.getOperand(0));
2446 Tmp1 = SelectExpr(N.getOperand(1));
2447 } else {
2448 Tmp1 = SelectExpr(N.getOperand(1));
2449 Select(N.getOperand(0));
2450 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002451
2452 // Subtract size from stack pointer, thereby allocating some space.
2453 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2454 }
2455
2456 // Put a pointer to the space into the result register, by copying the stack
2457 // pointer.
2458 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2459 return Result;
2460
2461 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002462 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002463 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002464
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002465 if (GlobalAddressSDNode *GASD =
2466 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002467 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002468 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2469 } else if (ExternalSymbolSDNode *ESSDN =
2470 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002471 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002472 BuildMI(BB, X86::CALLpcrel32,
2473 1).addExternalSymbol(ESSDN->getSymbol(), true);
2474 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002475 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2476 Select(N.getOperand(0));
2477 Tmp1 = SelectExpr(N.getOperand(1));
2478 } else {
2479 Tmp1 = SelectExpr(N.getOperand(1));
2480 Select(N.getOperand(0));
2481 }
2482
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002483 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2484 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002485 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002486 default: assert(0 && "Unknown value type for call result!");
2487 case MVT::Other: return 1;
2488 case MVT::i1:
2489 case MVT::i8:
2490 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2491 break;
2492 case MVT::i16:
2493 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2494 break;
2495 case MVT::i32:
2496 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002497 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002498 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2499 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002500 case MVT::f64: // Floating-point return values live in %ST(0)
2501 ContainsFPCode = true;
2502 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2503 break;
2504 }
2505 return Result+N.ResNo;
2506 }
2507
2508 return 0;
2509}
2510
Chris Lattnere10269b2005-01-17 19:25:26 +00002511/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2512/// load/op/store instruction. If successful return true.
2513bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2514 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2515 SDOperand Chain = Node->getOperand(0);
2516 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002517 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002518
2519 // The chain has to be a load, the stored value must be an integer binary
2520 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002521 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002522 MVT::isFloatingPoint(StVal.getValueType()))
2523 return false;
2524
Chris Lattner5c659812005-01-17 22:10:42 +00002525 // Token chain must either be a factor node or the load to fold.
2526 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2527 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002528
Chris Lattner5c659812005-01-17 22:10:42 +00002529 SDOperand TheLoad;
2530
2531 // Check to see if there is a load from the same pointer that we're storing
2532 // to in either operand of the binop.
2533 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2534 StVal.getOperand(0).getOperand(1) == StPtr)
2535 TheLoad = StVal.getOperand(0);
2536 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2537 StVal.getOperand(1).getOperand(1) == StPtr)
2538 TheLoad = StVal.getOperand(1);
2539 else
2540 return false; // No matching load operand.
2541
2542 // We can only fold the load if there are no intervening side-effecting
2543 // operations. This means that the store uses the load as its token chain, or
2544 // there are only token factor nodes in between the store and load.
2545 if (Chain != TheLoad.getValue(1)) {
2546 // Okay, the other option is that we have a store referring to (possibly
2547 // nested) token factor nodes. For now, just try peeking through one level
2548 // of token factors to see if this is the case.
2549 bool ChainOk = false;
2550 if (Chain.getOpcode() == ISD::TokenFactor) {
2551 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2552 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2553 ChainOk = true;
2554 break;
2555 }
2556 }
2557
2558 if (!ChainOk) return false;
2559 }
2560
2561 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002562 return false;
2563
2564 // Make sure that one of the operands of the binop is the load, and that the
2565 // load folds into the binop.
2566 if (((StVal.getOperand(0) != TheLoad ||
2567 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2568 (StVal.getOperand(1) != TheLoad ||
2569 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2570 return false;
2571
2572 // Finally, check to see if this is one of the ops we can handle!
2573 static const unsigned ADDTAB[] = {
2574 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2575 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2576 };
2577 static const unsigned SUBTAB[] = {
2578 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2579 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2580 };
2581 static const unsigned ANDTAB[] = {
2582 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2583 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2584 };
2585 static const unsigned ORTAB[] = {
2586 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2587 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2588 };
2589 static const unsigned XORTAB[] = {
2590 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2591 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2592 };
2593 static const unsigned SHLTAB[] = {
2594 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2595 /*Have to put the reg in CL*/0, 0, 0,
2596 };
2597 static const unsigned SARTAB[] = {
2598 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2599 /*Have to put the reg in CL*/0, 0, 0,
2600 };
2601 static const unsigned SHRTAB[] = {
2602 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2603 /*Have to put the reg in CL*/0, 0, 0,
2604 };
2605
2606 const unsigned *TabPtr = 0;
2607 switch (StVal.getOpcode()) {
2608 default:
2609 std::cerr << "CANNOT [mem] op= val: ";
2610 StVal.Val->dump(); std::cerr << "\n";
2611 case ISD::MUL:
2612 case ISD::SDIV:
2613 case ISD::UDIV:
2614 case ISD::SREM:
2615 case ISD::UREM: return false;
2616
2617 case ISD::ADD: TabPtr = ADDTAB; break;
2618 case ISD::SUB: TabPtr = SUBTAB; break;
2619 case ISD::AND: TabPtr = ANDTAB; break;
2620 case ISD:: OR: TabPtr = ORTAB; break;
2621 case ISD::XOR: TabPtr = XORTAB; break;
2622 case ISD::SHL: TabPtr = SHLTAB; break;
2623 case ISD::SRA: TabPtr = SARTAB; break;
2624 case ISD::SRL: TabPtr = SHRTAB; break;
2625 }
2626
2627 // Handle: [mem] op= CST
2628 SDOperand Op0 = StVal.getOperand(0);
2629 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002630 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002631 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2632 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2633 default: break;
2634 case MVT::i1:
2635 case MVT::i8: Opc = TabPtr[0]; break;
2636 case MVT::i16: Opc = TabPtr[1]; break;
2637 case MVT::i32: Opc = TabPtr[2]; break;
2638 }
2639
2640 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002641 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2642 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002643 Select(Chain);
2644
Chris Lattnere10269b2005-01-17 19:25:26 +00002645 X86AddressMode AM;
2646 if (getRegPressure(TheLoad.getOperand(0)) >
2647 getRegPressure(TheLoad.getOperand(1))) {
2648 Select(TheLoad.getOperand(0));
2649 SelectAddress(TheLoad.getOperand(1), AM);
2650 } else {
2651 SelectAddress(TheLoad.getOperand(1), AM);
2652 Select(TheLoad.getOperand(0));
2653 }
Chris Lattner5c659812005-01-17 22:10:42 +00002654
2655 if (StVal.getOpcode() == ISD::ADD) {
2656 if (CN->getValue() == 1) {
2657 switch (Op0.getValueType()) {
2658 default: break;
2659 case MVT::i8:
2660 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2661 return true;
2662 case MVT::i16: Opc = TabPtr[1];
2663 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2664 return true;
2665 case MVT::i32: Opc = TabPtr[2];
2666 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2667 return true;
2668 }
2669 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2670 switch (Op0.getValueType()) {
2671 default: break;
2672 case MVT::i8:
2673 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2674 return true;
2675 case MVT::i16: Opc = TabPtr[1];
2676 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2677 return true;
2678 case MVT::i32: Opc = TabPtr[2];
2679 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2680 return true;
2681 }
2682 }
2683 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002684
2685 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2686 return true;
2687 }
2688 }
2689
2690 // If we have [mem] = V op [mem], try to turn it into:
2691 // [mem] = [mem] op V.
2692 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2693 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2694 StVal.getOpcode() != ISD::SRL)
2695 std::swap(Op0, Op1);
2696
2697 if (Op0 != TheLoad) return false;
2698
2699 switch (Op0.getValueType()) {
2700 default: return false;
2701 case MVT::i1:
2702 case MVT::i8: Opc = TabPtr[3]; break;
2703 case MVT::i16: Opc = TabPtr[4]; break;
2704 case MVT::i32: Opc = TabPtr[5]; break;
2705 }
Chris Lattner5c659812005-01-17 22:10:42 +00002706
Chris Lattnerb422aea2005-01-18 17:35:28 +00002707 // Table entry doesn't exist?
2708 if (Opc == 0) return false;
2709
Chris Lattner4a108662005-01-18 03:51:59 +00002710 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2711 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002712 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002713 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002714
Chris Lattnere10269b2005-01-17 19:25:26 +00002715 X86AddressMode AM;
2716 SelectAddress(TheLoad.getOperand(1), AM);
2717 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002718 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002719 return true;
2720}
2721
2722
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002723void ISel::Select(SDOperand N) {
2724 unsigned Tmp1, Tmp2, Opc;
2725
Nate Begeman85fdeb22005-03-24 04:39:54 +00002726 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002727 return; // Already selected.
2728
Chris Lattner989de032005-01-11 06:14:36 +00002729 SDNode *Node = N.Val;
2730
2731 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002732 default:
Chris Lattner989de032005-01-11 06:14:36 +00002733 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002734 assert(0 && "Node not handled yet!");
2735 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002736 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002737 if (Node->getNumOperands() == 2) {
2738 bool OneFirst =
2739 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2740 Select(Node->getOperand(OneFirst));
2741 Select(Node->getOperand(!OneFirst));
2742 } else {
2743 std::vector<std::pair<unsigned, unsigned> > OpsP;
2744 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2745 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2746 std::sort(OpsP.begin(), OpsP.end());
2747 std::reverse(OpsP.begin(), OpsP.end());
2748 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2749 Select(Node->getOperand(OpsP[i].second));
2750 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002751 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002752 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +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 Lattner18c2f132005-01-13 20:50:02 +00002760 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002761
2762 if (Tmp1 != Tmp2) {
2763 switch (N.getOperand(1).getValueType()) {
2764 default: assert(0 && "Invalid type for operation!");
2765 case MVT::i1:
2766 case MVT::i8: Opc = X86::MOV8rr; break;
2767 case MVT::i16: Opc = X86::MOV16rr; break;
2768 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002769 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002770 }
2771 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2772 }
2773 return;
2774 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002775 switch (N.getNumOperands()) {
2776 default:
2777 assert(0 && "Unknown return instruction!");
2778 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002779 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2780 N.getOperand(2).getValueType() == MVT::i32 &&
2781 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002782 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2783 Tmp1 = SelectExpr(N.getOperand(1));
2784 Tmp2 = SelectExpr(N.getOperand(2));
2785 } else {
2786 Tmp2 = SelectExpr(N.getOperand(2));
2787 Tmp1 = SelectExpr(N.getOperand(1));
2788 }
2789 Select(N.getOperand(0));
2790
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002791 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2792 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2793 // Declare that EAX & EDX are live on exit.
2794 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2795 .addReg(X86::ESP);
2796 break;
2797 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002798 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2799 Select(N.getOperand(0));
2800 Tmp1 = SelectExpr(N.getOperand(1));
2801 } else {
2802 Tmp1 = SelectExpr(N.getOperand(1));
2803 Select(N.getOperand(0));
2804 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002805 switch (N.getOperand(1).getValueType()) {
2806 default: assert(0 && "All other types should have been promoted!!");
2807 case MVT::f64:
2808 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2809 // Declare that top-of-stack is live on exit
2810 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2811 break;
2812 case MVT::i32:
2813 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2814 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2815 break;
2816 }
2817 break;
2818 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002819 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002820 break;
2821 }
2822 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2823 return;
2824 case ISD::BR: {
2825 Select(N.getOperand(0));
2826 MachineBasicBlock *Dest =
2827 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2828 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2829 return;
2830 }
2831
2832 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002833 MachineBasicBlock *Dest =
2834 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002835
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002836 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2837 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002838 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2839 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2840 Select(N.getOperand(0));
2841 Tmp1 = SelectExpr(N.getOperand(1));
2842 } else {
2843 Tmp1 = SelectExpr(N.getOperand(1));
2844 Select(N.getOperand(0));
2845 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002846 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2847 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2848 }
Chris Lattner11333092005-01-11 03:11:44 +00002849
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002850 return;
2851 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002852
Chris Lattner4df0de92005-01-17 00:00:33 +00002853 case ISD::LOAD:
2854 // If this load could be folded into the only using instruction, and if it
2855 // is safe to emit the instruction here, try to do so now.
2856 if (Node->hasNUsesOfValue(1, 0)) {
2857 SDOperand TheVal = N.getValue(0);
2858 SDNode *User = 0;
2859 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2860 assert(UI != Node->use_end() && "Didn't find use!");
2861 SDNode *UN = *UI;
2862 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2863 if (UN->getOperand(i) == TheVal) {
2864 User = UN;
2865 goto FoundIt;
2866 }
2867 }
2868 FoundIt:
2869 // Only handle unary operators right now.
2870 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00002871 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002872 SelectExpr(SDOperand(User, 0));
2873 return;
2874 }
2875 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002876 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002877 SelectExpr(N);
2878 return;
2879
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002880 case ISD::EXTLOAD:
2881 case ISD::SEXTLOAD:
2882 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002883 case ISD::CALL:
2884 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002885 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002886 SelectExpr(N);
2887 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002888
2889 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2890 // On X86, we can represent all types except for Bool and Float natively.
2891 X86AddressMode AM;
2892 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002893 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2894 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2895 && "Unsupported TRUNCSTORE for this target!");
2896
2897 if (StoredTy == MVT::i16) {
2898 // FIXME: This is here just to allow testing. X86 doesn't really have a
2899 // TRUNCSTORE i16 operation, but this is required for targets that do not
2900 // have 16-bit integer registers. We occasionally disable 16-bit integer
2901 // registers to test the promotion code.
2902 Select(N.getOperand(0));
2903 Tmp1 = SelectExpr(N.getOperand(1));
2904 SelectAddress(N.getOperand(2), AM);
2905
2906 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2907 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2908 return;
2909 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002910
2911 // Store of constant bool?
2912 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2913 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2914 Select(N.getOperand(0));
2915 SelectAddress(N.getOperand(2), AM);
2916 } else {
2917 SelectAddress(N.getOperand(2), AM);
2918 Select(N.getOperand(0));
2919 }
2920 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2921 return;
2922 }
2923
2924 switch (StoredTy) {
2925 default: assert(0 && "Cannot truncstore this type!");
2926 case MVT::i1: Opc = X86::MOV8mr; break;
2927 case MVT::f32: Opc = X86::FST32m; break;
2928 }
2929
2930 std::vector<std::pair<unsigned, unsigned> > RP;
2931 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2932 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2933 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2934 std::sort(RP.begin(), RP.end());
2935
Chris Lattner572dd082005-02-23 05:57:21 +00002936 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002937 for (unsigned i = 0; i != 3; ++i)
2938 switch (RP[2-i].second) {
2939 default: assert(0 && "Unknown operand number!");
2940 case 0: Select(N.getOperand(0)); break;
2941 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2942 case 2: SelectAddress(N.getOperand(2), AM); break;
2943 }
2944
2945 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2946 return;
2947 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002948 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002949 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002950
2951 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2952 Opc = 0;
2953 switch (CN->getValueType(0)) {
2954 default: assert(0 && "Invalid type for operation!");
2955 case MVT::i1:
2956 case MVT::i8: Opc = X86::MOV8mi; break;
2957 case MVT::i16: Opc = X86::MOV16mi; break;
2958 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002959 case MVT::f64: break;
2960 }
2961 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002962 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2963 Select(N.getOperand(0));
2964 SelectAddress(N.getOperand(2), AM);
2965 } else {
2966 SelectAddress(N.getOperand(2), AM);
2967 Select(N.getOperand(0));
2968 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002969 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2970 return;
2971 }
2972 }
Chris Lattner837caa72005-01-11 23:21:30 +00002973
2974 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00002975 if (TryToFoldLoadOpStore(Node))
2976 return;
Chris Lattner837caa72005-01-11 23:21:30 +00002977
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002978 switch (N.getOperand(1).getValueType()) {
2979 default: assert(0 && "Cannot store this type!");
2980 case MVT::i1:
2981 case MVT::i8: Opc = X86::MOV8mr; break;
2982 case MVT::i16: Opc = X86::MOV16mr; break;
2983 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002984 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002985 }
Chris Lattner11333092005-01-11 03:11:44 +00002986
2987 std::vector<std::pair<unsigned, unsigned> > RP;
2988 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2989 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2990 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2991 std::sort(RP.begin(), RP.end());
2992
Chris Lattner572dd082005-02-23 05:57:21 +00002993 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00002994 for (unsigned i = 0; i != 3; ++i)
2995 switch (RP[2-i].second) {
2996 default: assert(0 && "Unknown operand number!");
2997 case 0: Select(N.getOperand(0)); break;
2998 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002999 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003000 }
3001
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003002 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3003 return;
3004 }
3005 case ISD::ADJCALLSTACKDOWN:
3006 case ISD::ADJCALLSTACKUP:
3007 Select(N.getOperand(0));
3008 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
3009
3010 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3011 X86::ADJCALLSTACKUP;
3012 BuildMI(BB, Opc, 1).addImm(Tmp1);
3013 return;
Chris Lattner989de032005-01-11 06:14:36 +00003014 case ISD::MEMSET: {
3015 Select(N.getOperand(0)); // Select the chain.
3016 unsigned Align =
3017 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3018 if (Align == 0) Align = 1;
3019
3020 // Turn the byte code into # iterations
3021 unsigned CountReg;
3022 unsigned Opcode;
3023 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3024 unsigned Val = ValC->getValue() & 255;
3025
3026 // If the value is a constant, then we can potentially use larger sets.
3027 switch (Align & 3) {
3028 case 2: // WORD aligned
3029 CountReg = MakeReg(MVT::i32);
3030 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3031 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3032 } else {
3033 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3034 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3035 }
3036 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3037 Opcode = X86::REP_STOSW;
3038 break;
3039 case 0: // DWORD aligned
3040 CountReg = MakeReg(MVT::i32);
3041 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3042 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3043 } else {
3044 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3045 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3046 }
3047 Val = (Val << 8) | Val;
3048 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3049 Opcode = X86::REP_STOSD;
3050 break;
3051 default: // BYTE aligned
3052 CountReg = SelectExpr(Node->getOperand(3));
3053 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3054 Opcode = X86::REP_STOSB;
3055 break;
3056 }
3057 } else {
3058 // If it's not a constant value we are storing, just fall back. We could
3059 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3060 unsigned ValReg = SelectExpr(Node->getOperand(2));
3061 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3062 CountReg = SelectExpr(Node->getOperand(3));
3063 Opcode = X86::REP_STOSB;
3064 }
3065
3066 // No matter what the alignment is, we put the source in ESI, the
3067 // destination in EDI, and the count in ECX.
3068 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3069 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3070 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3071 BuildMI(BB, Opcode, 0);
3072 return;
3073 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003074 case ISD::MEMCPY:
3075 Select(N.getOperand(0)); // Select the chain.
3076 unsigned Align =
3077 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3078 if (Align == 0) Align = 1;
3079
3080 // Turn the byte code into # iterations
3081 unsigned CountReg;
3082 unsigned Opcode;
3083 switch (Align & 3) {
3084 case 2: // WORD aligned
3085 CountReg = MakeReg(MVT::i32);
3086 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3087 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3088 } else {
3089 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3090 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3091 }
3092 Opcode = X86::REP_MOVSW;
3093 break;
3094 case 0: // DWORD aligned
3095 CountReg = MakeReg(MVT::i32);
3096 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3097 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3098 } else {
3099 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3100 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3101 }
3102 Opcode = X86::REP_MOVSD;
3103 break;
3104 default: // BYTE aligned
3105 CountReg = SelectExpr(Node->getOperand(3));
3106 Opcode = X86::REP_MOVSB;
3107 break;
3108 }
3109
3110 // No matter what the alignment is, we put the source in ESI, the
3111 // destination in EDI, and the count in ECX.
3112 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3113 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3114 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3115 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3116 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3117 BuildMI(BB, Opcode, 0);
3118 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003119 }
3120 assert(0 && "Should not be reached!");
3121}
3122
3123
3124/// createX86PatternInstructionSelector - This pass converts an LLVM function
3125/// into a machine code representation using pattern matching and a machine
3126/// description file.
3127///
3128FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
3129 return new ISel(TM);
3130}