blob: 9b68ad8b02d15c5e79a84d2a6204bec8ea0fa529 [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);
1081 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001082 }
1083
Chris Lattneref6806c2005-01-12 02:02:48 +00001084 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001085 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001086 switch (RHS.getValueType()) {
1087 default: break;
1088 case MVT::i1:
1089 case MVT::i8: Opc = X86::CMP8mr; break;
1090 case MVT::i16: Opc = X86::CMP16mr; break;
1091 case MVT::i32: Opc = X86::CMP32mr; break;
1092 }
1093 if (Opc) {
1094 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001095 EmitFoldedLoad(LHS, AM);
1096 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001097 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1098 return;
1099 }
1100 }
1101
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001102 switch (LHS.getValueType()) {
1103 default: assert(0 && "Cannot compare this value!");
1104 case MVT::i1:
1105 case MVT::i8: Opc = X86::CMP8rr; break;
1106 case MVT::i16: Opc = X86::CMP16rr; break;
1107 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001108 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001109 }
Chris Lattner11333092005-01-11 03:11:44 +00001110 unsigned Tmp1, Tmp2;
1111 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1112 Tmp1 = SelectExpr(LHS);
1113 Tmp2 = SelectExpr(RHS);
1114 } else {
1115 Tmp2 = SelectExpr(RHS);
1116 Tmp1 = SelectExpr(LHS);
1117 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001118 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1119}
1120
Chris Lattnera5ade062005-01-11 21:19:59 +00001121/// isFoldableLoad - Return true if this is a load instruction that can safely
1122/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001123bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1124 if (Op.getOpcode() == ISD::LOAD) {
1125 // FIXME: currently can't fold constant pool indexes.
1126 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1127 return false;
1128 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1129 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1130 // FIXME: currently can't fold constant pool indexes.
1131 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1132 return false;
1133 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001134 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001135 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001136
1137 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001138 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1139 if (ExprMap.count(Op.getValue(1))) return false;
1140 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001141 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001142
Chris Lattner4ff348b2005-01-17 06:26:58 +00001143 // If there is not just one use of its value, we cannot fold.
1144 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1145
1146 // Finally, we cannot fold the load into the operation if this would induce a
1147 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1148 // operand of the operation we are folding the load into) can possible use the
1149 // chain node defined by the load.
1150 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1151 std::set<SDNode*> Visited;
1152 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1153 return false;
1154 }
1155 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001156}
1157
Chris Lattner4ff348b2005-01-17 06:26:58 +00001158
Chris Lattnera5ade062005-01-11 21:19:59 +00001159/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1160/// and compute the address being loaded into AM.
1161void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1162 SDOperand Chain = Op.getOperand(0);
1163 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001164
Chris Lattnera5ade062005-01-11 21:19:59 +00001165 if (getRegPressure(Chain) > getRegPressure(Address)) {
1166 Select(Chain);
1167 SelectAddress(Address, AM);
1168 } else {
1169 SelectAddress(Address, AM);
1170 Select(Chain);
1171 }
1172
1173 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001174 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1175 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001176 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001177 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001178}
1179
Chris Lattner30ea1e92005-01-19 07:37:26 +00001180// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1181// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1182// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1183// return true.
1184bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001185 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1186 // good!
1187 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1188 std::swap(Op1, Op2); // Op1 is the SHL now.
1189 } else {
1190 return false; // No match
1191 }
1192
1193 SDOperand ShlVal = Op1.getOperand(0);
1194 SDOperand ShlAmt = Op1.getOperand(1);
1195 SDOperand ShrVal = Op2.getOperand(0);
1196 SDOperand ShrAmt = Op2.getOperand(1);
1197
Chris Lattner30ea1e92005-01-19 07:37:26 +00001198 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1199
Chris Lattner85716372005-01-19 06:18:43 +00001200 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1201 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1202 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001203 if (SubCST->getValue() == RegSize) {
1204 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001205 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001206 if (ShrVal == ShlVal) {
1207 unsigned Reg, ShAmt;
1208 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1209 Reg = SelectExpr(ShrVal);
1210 ShAmt = SelectExpr(ShrAmt);
1211 } else {
1212 ShAmt = SelectExpr(ShrAmt);
1213 Reg = SelectExpr(ShrVal);
1214 }
1215 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1216 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1217 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1218 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1219 return true;
1220 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001221 unsigned AReg, BReg;
1222 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001223 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001224 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001225 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001226 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001227 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001228 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001229 unsigned ShAmt = SelectExpr(ShrAmt);
1230 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1231 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1232 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001233 return true;
1234 }
1235 }
1236
Chris Lattner4053b1e2005-01-19 08:07:05 +00001237 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1238 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1239 if (SubCST->getValue() == RegSize) {
1240 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1241 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1242 if (ShrVal == ShlVal) {
1243 unsigned Reg, ShAmt;
1244 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1245 Reg = SelectExpr(ShrVal);
1246 ShAmt = SelectExpr(ShlAmt);
1247 } else {
1248 ShAmt = SelectExpr(ShlAmt);
1249 Reg = SelectExpr(ShrVal);
1250 }
1251 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1252 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1253 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1254 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1255 return true;
1256 } else if (RegSize != 8) {
1257 unsigned AReg, BReg;
1258 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001259 AReg = SelectExpr(ShlVal);
1260 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001261 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001262 BReg = SelectExpr(ShrVal);
1263 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001264 }
1265 unsigned ShAmt = SelectExpr(ShlAmt);
1266 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1267 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1268 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1269 return true;
1270 }
1271 }
Chris Lattner85716372005-01-19 06:18:43 +00001272
Chris Lattner4053b1e2005-01-19 08:07:05 +00001273 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1274 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1275 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1276 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1277 // (A >> 5) | (A << 27) --> ROR A, 5
1278 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1279 if (ShrVal == ShlVal) {
1280 unsigned Reg = SelectExpr(ShrVal);
1281 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1282 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1283 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1284 return true;
1285 } else if (RegSize != 8) {
1286 unsigned AReg, BReg;
1287 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001288 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001289 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001290 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001291 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001292 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001293 }
1294 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1295 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1296 .addImm(ShrCst->getValue());
1297 return true;
1298 }
1299 }
1300
Chris Lattner85716372005-01-19 06:18:43 +00001301 return false;
1302}
1303
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001304unsigned ISel::SelectExpr(SDOperand N) {
1305 unsigned Result;
1306 unsigned Tmp1, Tmp2, Tmp3;
1307 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001308 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001309 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001310
Chris Lattner7f2afac2005-01-14 22:37:41 +00001311 if (Node->getOpcode() == ISD::CopyFromReg) {
1312 // FIXME: Handle copy from physregs!
1313
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001314 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001315 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001316 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001317
1318 unsigned &Reg = ExprMap[N];
1319 if (Reg) return Reg;
1320
Chris Lattner19ad0622005-01-20 18:53:00 +00001321 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
1322 N.getOpcode() != ISD::SUB_PARTS)
Chris Lattnera5ade062005-01-11 21:19:59 +00001323 Reg = Result = (N.getValueType() != MVT::Other) ?
1324 MakeReg(N.getValueType()) : 1;
1325 else {
1326 // If this is a call instruction, make sure to prepare ALL of the result
1327 // values as well as the chain.
Chris Lattner19ad0622005-01-20 18:53:00 +00001328 if (N.getOpcode() == ISD::CALL) {
1329 if (Node->getNumValues() == 1)
1330 Reg = Result = 1; // Void call, just a chain.
1331 else {
1332 Result = MakeReg(Node->getValueType(0));
1333 ExprMap[N.getValue(0)] = Result;
1334 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1335 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1336 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
1337 }
1338 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001339 Result = MakeReg(Node->getValueType(0));
1340 ExprMap[N.getValue(0)] = Result;
Chris Lattner19ad0622005-01-20 18:53:00 +00001341 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001342 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001343 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001344 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001345
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001346 switch (N.getOpcode()) {
1347 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001348 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001349 assert(0 && "Node not handled!\n");
1350 case ISD::FrameIndex:
1351 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1352 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1353 return Result;
1354 case ISD::ConstantPool:
1355 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1356 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1357 return Result;
1358 case ISD::ConstantFP:
1359 ContainsFPCode = true;
1360 Tmp1 = Result; // Intermediate Register
1361 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1362 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1363 Tmp1 = MakeReg(MVT::f64);
1364
1365 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1366 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1367 BuildMI(BB, X86::FLD0, 0, Tmp1);
1368 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1369 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1370 BuildMI(BB, X86::FLD1, 0, Tmp1);
1371 else
1372 assert(0 && "Unexpected constant!");
1373 if (Tmp1 != Result)
1374 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1375 return Result;
1376 case ISD::Constant:
1377 switch (N.getValueType()) {
1378 default: assert(0 && "Cannot use constants of this type!");
1379 case MVT::i1:
1380 case MVT::i8: Opc = X86::MOV8ri; break;
1381 case MVT::i16: Opc = X86::MOV16ri; break;
1382 case MVT::i32: Opc = X86::MOV32ri; break;
1383 }
1384 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1385 return Result;
1386 case ISD::GlobalAddress: {
1387 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1388 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1389 return Result;
1390 }
1391 case ISD::ExternalSymbol: {
1392 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1393 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1394 return Result;
1395 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001396 case ISD::ZERO_EXTEND: {
1397 int DestIs16 = N.getValueType() == MVT::i16;
1398 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001399
1400 // FIXME: This hack is here for zero extension casts from bool to i8. This
1401 // would not be needed if bools were promoted by Legalize.
1402 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001403 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001404 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1405 return Result;
1406 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001407
Chris Lattner4ff348b2005-01-17 06:26:58 +00001408 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001409 static const unsigned Opc[3] = {
1410 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1411 };
1412
1413 X86AddressMode AM;
1414 EmitFoldedLoad(N.getOperand(0), AM);
1415 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1416
1417 return Result;
1418 }
1419
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001420 static const unsigned Opc[3] = {
1421 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1422 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001423 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001424 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1425 return Result;
1426 }
1427 case ISD::SIGN_EXTEND: {
1428 int DestIs16 = N.getValueType() == MVT::i16;
1429 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1430
Chris Lattner590d8002005-01-09 18:52:44 +00001431 // FIXME: Legalize should promote bools to i8!
1432 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1433 "Sign extend from bool not implemented!");
1434
Chris Lattner4ff348b2005-01-17 06:26:58 +00001435 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001436 static const unsigned Opc[3] = {
1437 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1438 };
1439
1440 X86AddressMode AM;
1441 EmitFoldedLoad(N.getOperand(0), AM);
1442 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1443 return Result;
1444 }
1445
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001446 static const unsigned Opc[3] = {
1447 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1448 };
1449 Tmp1 = SelectExpr(N.getOperand(0));
1450 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1451 return Result;
1452 }
1453 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001454 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001455 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001456 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001457 switch (N.getValueType()) {
1458 default: assert(0 && "Unknown truncate!");
1459 case MVT::i1:
1460 case MVT::i8: Opc = X86::MOV8rm; break;
1461 case MVT::i16: Opc = X86::MOV16rm; break;
1462 }
1463 X86AddressMode AM;
1464 EmitFoldedLoad(N.getOperand(0), AM);
1465 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1466 return Result;
1467 }
1468
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001469 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1470 // a move out of AX or AL.
1471 switch (N.getOperand(0).getValueType()) {
1472 default: assert(0 && "Unknown truncate!");
1473 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1474 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1475 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1476 }
1477 Tmp1 = SelectExpr(N.getOperand(0));
1478 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1479
1480 switch (N.getValueType()) {
1481 default: assert(0 && "Unknown truncate!");
1482 case MVT::i1:
1483 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1484 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1485 }
1486 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1487 return Result;
1488
Chris Lattner590d8002005-01-09 18:52:44 +00001489 case ISD::SINT_TO_FP:
1490 case ISD::UINT_TO_FP: {
1491 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001492 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001493
1494 // Promote the integer to a type supported by FLD. We do this because there
1495 // are no unsigned FLD instructions, so we must promote an unsigned value to
1496 // a larger signed value, then use FLD on the larger value.
1497 //
1498 MVT::ValueType PromoteType = MVT::Other;
1499 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1500 unsigned PromoteOpcode = 0;
1501 unsigned RealDestReg = Result;
1502 switch (SrcTy) {
1503 case MVT::i1:
1504 case MVT::i8:
1505 // We don't have the facilities for directly loading byte sized data from
1506 // memory (even signed). Promote it to 16 bits.
1507 PromoteType = MVT::i16;
1508 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1509 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1510 break;
1511 case MVT::i16:
1512 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1513 PromoteType = MVT::i32;
1514 PromoteOpcode = X86::MOVZX32rr16;
1515 }
1516 break;
1517 default:
1518 // Don't fild into the real destination.
1519 if (Node->getOpcode() == ISD::UINT_TO_FP)
1520 Result = MakeReg(Node->getValueType(0));
1521 break;
1522 }
1523
1524 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1525
1526 if (PromoteType != MVT::Other) {
1527 Tmp2 = MakeReg(PromoteType);
1528 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1529 SrcTy = PromoteType;
1530 Tmp1 = Tmp2;
1531 }
1532
1533 // Spill the integer to memory and reload it from there.
1534 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1535 MachineFunction *F = BB->getParent();
1536 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1537
1538 switch (SrcTy) {
1539 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001540 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001541 // FIXME: this won't work for cast [u]long to FP
1542 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1543 FrameIdx).addReg(Tmp1);
1544 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1545 FrameIdx, 4).addReg(Tmp1+1);
1546 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1547 break;
1548 case MVT::i32:
1549 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1550 FrameIdx).addReg(Tmp1);
1551 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1552 break;
1553 case MVT::i16:
1554 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1555 FrameIdx).addReg(Tmp1);
1556 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1557 break;
1558 default: break; // No promotion required.
1559 }
1560
Chris Lattner085c9952005-01-12 04:00:00 +00001561 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001562 // If this is a cast from uint -> double, we need to be careful when if
1563 // the "sign" bit is set. If so, we don't want to make a negative number,
1564 // we want to make a positive number. Emit code to add an offset if the
1565 // sign bit is set.
1566
1567 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1568 unsigned IsNeg = MakeReg(MVT::i32);
1569 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1570
1571 // Create a CP value that has the offset in one word and 0 in the other.
1572 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1573 0x4f80000000000000ULL);
1574 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1575 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1576 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1577
1578 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1579 // We need special handling for unsigned 64-bit integer sources. If the
1580 // input number has the "sign bit" set, then we loaded it incorrectly as a
1581 // negative 64-bit number. In this case, add an offset value.
1582
1583 // Emit a test instruction to see if the dynamic input value was signed.
1584 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1585
1586 // If the sign bit is set, get a pointer to an offset, otherwise get a
1587 // pointer to a zero.
1588 MachineConstantPool *CP = F->getConstantPool();
1589 unsigned Zero = MakeReg(MVT::i32);
1590 Constant *Null = Constant::getNullValue(Type::UIntTy);
1591 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1592 CP->getConstantPoolIndex(Null));
1593 unsigned Offset = MakeReg(MVT::i32);
1594 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1595
1596 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1597 CP->getConstantPoolIndex(OffsetCst));
1598 unsigned Addr = MakeReg(MVT::i32);
1599 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1600
1601 // Load the constant for an add. FIXME: this could make an 'fadd' that
1602 // reads directly from memory, but we don't support these yet.
1603 unsigned ConstReg = MakeReg(MVT::f64);
1604 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1605
1606 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1607 }
1608 return RealDestReg;
1609 }
1610 case ISD::FP_TO_SINT:
1611 case ISD::FP_TO_UINT: {
1612 // FIXME: Most of this grunt work should be done by legalize!
1613 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1614
1615 // Change the floating point control register to use "round towards zero"
1616 // mode when truncating to an integer value.
1617 //
1618 MachineFunction *F = BB->getParent();
1619 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1620 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1621
1622 // Load the old value of the high byte of the control word...
1623 unsigned HighPartOfCW = MakeReg(MVT::i8);
1624 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1625 CWFrameIdx, 1);
1626
1627 // Set the high part to be round to zero...
1628 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1629 CWFrameIdx, 1).addImm(12);
1630
1631 // Reload the modified control word now...
1632 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1633
1634 // Restore the memory image of control word to original value
1635 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1636 CWFrameIdx, 1).addReg(HighPartOfCW);
1637
1638 // We don't have the facilities for directly storing byte sized data to
1639 // memory. Promote it to 16 bits. We also must promote unsigned values to
1640 // larger classes because we only have signed FP stores.
1641 MVT::ValueType StoreClass = Node->getValueType(0);
1642 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1643 switch (StoreClass) {
1644 case MVT::i8: StoreClass = MVT::i16; break;
1645 case MVT::i16: StoreClass = MVT::i32; break;
1646 case MVT::i32: StoreClass = MVT::i64; break;
1647 // The following treatment of cLong may not be perfectly right,
1648 // but it survives chains of casts of the form
1649 // double->ulong->double.
1650 case MVT::i64: StoreClass = MVT::i64; break;
1651 default: assert(0 && "Unknown store class!");
1652 }
1653
1654 // Spill the integer to memory and reload it from there.
1655 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1656 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1657
1658 switch (StoreClass) {
1659 default: assert(0 && "Unknown store class!");
1660 case MVT::i16:
1661 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1662 break;
1663 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001664 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001665 break;
1666 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001667 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001668 break;
1669 }
1670
1671 switch (Node->getValueType(0)) {
1672 default:
1673 assert(0 && "Unknown integer type!");
1674 case MVT::i64:
1675 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001676 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001677 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1678 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1679 case MVT::i32:
1680 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1681 break;
1682 case MVT::i16:
1683 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1684 break;
1685 case MVT::i8:
1686 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1687 break;
1688 }
1689
1690 // Reload the original control word now.
1691 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1692 return Result;
1693 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001694 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001695 Op0 = N.getOperand(0);
1696 Op1 = N.getOperand(1);
1697
Chris Lattner44129b52005-01-25 20:03:11 +00001698 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001699 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001700 goto FoldAdd;
1701 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001702
Chris Lattner44129b52005-01-25 20:03:11 +00001703 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001704 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001705 switch (N.getValueType()) {
1706 default: assert(0 && "Cannot add this type!");
1707 case MVT::i1:
1708 case MVT::i8: Opc = X86::ADD8rm; break;
1709 case MVT::i16: Opc = X86::ADD16rm; break;
1710 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001711 case MVT::f64:
1712 // For F64, handle promoted load operations (from F32) as well!
1713 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1714 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001715 }
1716 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001717 EmitFoldedLoad(Op1, AM);
1718 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001719 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1720 return Result;
1721 }
1722
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001723 // See if we can codegen this as an LEA to fold operations together.
1724 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001725 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001726 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001727 MatchAddress(N, AM);
1728 ExprMap[N] = Result;
1729
1730 // If this is not just an add, emit the LEA. For a simple add (like
1731 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1732 // leave this as LEA, then peephole it to 'ADD' after two address elim
1733 // happens.
1734 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1735 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1736 X86AddressMode XAM = SelectAddrExprs(AM);
1737 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1738 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001739 }
1740 }
Chris Lattner11333092005-01-11 03:11:44 +00001741
Chris Lattnera5ade062005-01-11 21:19:59 +00001742 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001743 Opc = 0;
1744 if (CN->getValue() == 1) { // add X, 1 -> inc X
1745 switch (N.getValueType()) {
1746 default: assert(0 && "Cannot integer add this type!");
1747 case MVT::i8: Opc = X86::INC8r; break;
1748 case MVT::i16: Opc = X86::INC16r; break;
1749 case MVT::i32: Opc = X86::INC32r; break;
1750 }
1751 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1752 switch (N.getValueType()) {
1753 default: assert(0 && "Cannot integer add this type!");
1754 case MVT::i8: Opc = X86::DEC8r; break;
1755 case MVT::i16: Opc = X86::DEC16r; break;
1756 case MVT::i32: Opc = X86::DEC32r; break;
1757 }
1758 }
1759
1760 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001761 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001762 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1763 return Result;
1764 }
1765
1766 switch (N.getValueType()) {
1767 default: assert(0 && "Cannot add this type!");
1768 case MVT::i8: Opc = X86::ADD8ri; break;
1769 case MVT::i16: Opc = X86::ADD16ri; break;
1770 case MVT::i32: Opc = X86::ADD32ri; break;
1771 }
1772 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001773 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001774 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1775 return Result;
1776 }
1777 }
1778
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001779 switch (N.getValueType()) {
1780 default: assert(0 && "Cannot add this type!");
1781 case MVT::i8: Opc = X86::ADD8rr; break;
1782 case MVT::i16: Opc = X86::ADD16rr; break;
1783 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001784 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001785 }
Chris Lattner11333092005-01-11 03:11:44 +00001786
Chris Lattnera5ade062005-01-11 21:19:59 +00001787 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1788 Tmp1 = SelectExpr(Op0);
1789 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001790 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001791 Tmp2 = SelectExpr(Op1);
1792 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001793 }
1794
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001795 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1796 return Result;
1797 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001798 case ISD::MUL:
1799 case ISD::AND:
1800 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001801 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001802 static const unsigned SUBTab[] = {
1803 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1804 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1805 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1806 };
1807 static const unsigned MULTab[] = {
1808 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1809 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1810 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1811 };
1812 static const unsigned ANDTab[] = {
1813 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1814 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1815 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1816 };
1817 static const unsigned ORTab[] = {
1818 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1819 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1820 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1821 };
1822 static const unsigned XORTab[] = {
1823 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1824 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1825 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1826 };
1827
1828 Op0 = Node->getOperand(0);
1829 Op1 = Node->getOperand(1);
1830
Chris Lattner30ea1e92005-01-19 07:37:26 +00001831 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1832 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001833 return Result;
1834
1835 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001836 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1837 if (CN->isNullValue()) { // 0 - N -> neg N
1838 switch (N.getValueType()) {
1839 default: assert(0 && "Cannot sub this type!");
1840 case MVT::i1:
1841 case MVT::i8: Opc = X86::NEG8r; break;
1842 case MVT::i16: Opc = X86::NEG16r; break;
1843 case MVT::i32: Opc = X86::NEG32r; break;
1844 }
1845 Tmp1 = SelectExpr(N.getOperand(1));
1846 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1847 return Result;
1848 }
1849
Chris Lattnera5ade062005-01-11 21:19:59 +00001850 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1851 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001852 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001853 switch (N.getValueType()) {
1854 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001855 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001856 case MVT::i8: Opc = X86::NOT8r; break;
1857 case MVT::i16: Opc = X86::NOT16r; break;
1858 case MVT::i32: Opc = X86::NOT32r; break;
1859 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001860 if (Opc) {
1861 Tmp1 = SelectExpr(Op0);
1862 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1863 return Result;
1864 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001865 }
1866
Chris Lattner2a4e5082005-01-17 06:48:02 +00001867 // Fold common multiplies into LEA instructions.
1868 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1869 switch ((int)CN->getValue()) {
1870 default: break;
1871 case 3:
1872 case 5:
1873 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001874 // Remove N from exprmap so SelectAddress doesn't get confused.
1875 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001876 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001877 SelectAddress(N, AM);
1878 // Restore it to the map.
1879 ExprMap[N] = Result;
1880 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1881 return Result;
1882 }
1883 }
1884
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001885 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001886 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001887 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001888 case MVT::i8: Opc = 0; break;
1889 case MVT::i16: Opc = 1; break;
1890 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001891 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001892 switch (Node->getOpcode()) {
1893 default: assert(0 && "Unreachable!");
1894 case ISD::SUB: Opc = SUBTab[Opc]; break;
1895 case ISD::MUL: Opc = MULTab[Opc]; break;
1896 case ISD::AND: Opc = ANDTab[Opc]; break;
1897 case ISD::OR: Opc = ORTab[Opc]; break;
1898 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001899 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001900 if (Opc) { // Can't fold MUL:i8 R, imm
1901 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001902 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1903 return Result;
1904 }
1905 }
Chris Lattner11333092005-01-11 03:11:44 +00001906
Chris Lattner44129b52005-01-25 20:03:11 +00001907 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00001908 if (Node->getOpcode() != ISD::SUB) {
1909 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001910 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001911 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00001912 // For FP, emit 'reverse' subract, with a memory operand.
1913 if (N.getValueType() == MVT::f64) {
1914 if (Op0.getOpcode() == ISD::EXTLOAD)
1915 Opc = X86::FSUBR32m;
1916 else
1917 Opc = X86::FSUBR64m;
1918
Chris Lattnera5ade062005-01-11 21:19:59 +00001919 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001920 EmitFoldedLoad(Op0, AM);
1921 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001922 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1923 return Result;
1924 }
1925 }
1926
Chris Lattner44129b52005-01-25 20:03:11 +00001927 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001928 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001929 switch (N.getValueType()) {
1930 default: assert(0 && "Cannot operate on this type!");
1931 case MVT::i1:
1932 case MVT::i8: Opc = 5; break;
1933 case MVT::i16: Opc = 6; break;
1934 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001935 // For F64, handle promoted load operations (from F32) as well!
1936 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001937 }
1938 switch (Node->getOpcode()) {
1939 default: assert(0 && "Unreachable!");
1940 case ISD::SUB: Opc = SUBTab[Opc]; break;
1941 case ISD::MUL: Opc = MULTab[Opc]; break;
1942 case ISD::AND: Opc = ANDTab[Opc]; break;
1943 case ISD::OR: Opc = ORTab[Opc]; break;
1944 case ISD::XOR: Opc = XORTab[Opc]; break;
1945 }
1946
1947 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001948 EmitFoldedLoad(Op1, AM);
1949 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001950 if (Opc) {
1951 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1952 } else {
1953 assert(Node->getOpcode() == ISD::MUL &&
1954 N.getValueType() == MVT::i8 && "Unexpected situation!");
1955 // Must use the MUL instruction, which forces use of AL.
1956 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1957 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1958 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1959 }
1960 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001961 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001962
1963 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1964 Tmp1 = SelectExpr(Op0);
1965 Tmp2 = SelectExpr(Op1);
1966 } else {
1967 Tmp2 = SelectExpr(Op1);
1968 Tmp1 = SelectExpr(Op0);
1969 }
1970
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001971 switch (N.getValueType()) {
1972 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001973 case MVT::i1:
1974 case MVT::i8: Opc = 10; break;
1975 case MVT::i16: Opc = 11; break;
1976 case MVT::i32: Opc = 12; break;
1977 case MVT::f32: Opc = 13; break;
1978 case MVT::f64: Opc = 14; break;
1979 }
1980 switch (Node->getOpcode()) {
1981 default: assert(0 && "Unreachable!");
1982 case ISD::SUB: Opc = SUBTab[Opc]; break;
1983 case ISD::MUL: Opc = MULTab[Opc]; break;
1984 case ISD::AND: Opc = ANDTab[Opc]; break;
1985 case ISD::OR: Opc = ORTab[Opc]; break;
1986 case ISD::XOR: Opc = XORTab[Opc]; break;
1987 }
1988 if (Opc) {
1989 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1990 } else {
1991 assert(Node->getOpcode() == ISD::MUL &&
1992 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001993 // Must use the MUL instruction, which forces use of AL.
1994 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1995 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1996 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001997 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001998 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001999 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002000 case ISD::ADD_PARTS:
2001 case ISD::SUB_PARTS: {
2002 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2003 "Not an i64 add/sub!");
2004 // Emit all of the operands.
2005 std::vector<unsigned> InVals;
2006 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2007 InVals.push_back(SelectExpr(N.getOperand(i)));
2008 if (N.getOpcode() == ISD::ADD_PARTS) {
2009 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2010 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2011 } else {
2012 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2013 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2014 }
2015 return Result+N.ResNo;
2016 }
2017
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002018 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002019 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2020 Tmp2 = SelectExpr(N.getOperand(1));
2021 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002022 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002023 Tmp3 = SelectExpr(N.getOperand(2));
2024 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002025 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002026 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2027 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002028
2029 case ISD::SDIV:
2030 case ISD::UDIV:
2031 case ISD::SREM:
2032 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002033 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2034 "We don't support this operator!");
2035
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002036 if (N.getOpcode() == ISD::SDIV)
Chris Lattner3576c842005-01-25 20:35:10 +00002037
2038 // We can fold loads into FpDIVs, but not really into any others.
2039 if (N.getValueType() == MVT::f64) {
2040 // Check for reversed and unreversed DIV.
2041 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2042 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2043 Opc = X86::FDIVR32m;
2044 else
2045 Opc = X86::FDIVR64m;
2046 X86AddressMode AM;
2047 EmitFoldedLoad(N.getOperand(0), AM);
2048 Tmp1 = SelectExpr(N.getOperand(1));
2049 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2050 return Result;
2051 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2052 N.getOperand(1).getOpcode() == ISD::LOAD) {
2053 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2054 Opc = X86::FDIV32m;
2055 else
2056 Opc = X86::FDIV64m;
2057 X86AddressMode AM;
2058 EmitFoldedLoad(N.getOperand(1), AM);
2059 Tmp1 = SelectExpr(N.getOperand(0));
2060 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2061 return Result;
2062 }
2063 }
2064
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002065 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2066 // FIXME: These special cases should be handled by the lowering impl!
2067 unsigned RHS = CN->getValue();
2068 bool isNeg = false;
2069 if ((int)RHS < 0) {
2070 isNeg = true;
2071 RHS = -RHS;
2072 }
2073 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2074 unsigned Log = log2(RHS);
2075 unsigned TmpReg = MakeReg(N.getValueType());
2076 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2077 switch (N.getValueType()) {
2078 default: assert("Unknown type to signed divide!");
2079 case MVT::i8:
2080 SAROpc = X86::SAR8ri;
2081 SHROpc = X86::SHR8ri;
2082 ADDOpc = X86::ADD8rr;
2083 NEGOpc = X86::NEG8r;
2084 break;
2085 case MVT::i16:
2086 SAROpc = X86::SAR16ri;
2087 SHROpc = X86::SHR16ri;
2088 ADDOpc = X86::ADD16rr;
2089 NEGOpc = X86::NEG16r;
2090 break;
2091 case MVT::i32:
2092 SAROpc = X86::SAR32ri;
2093 SHROpc = X86::SHR32ri;
2094 ADDOpc = X86::ADD32rr;
2095 NEGOpc = X86::NEG32r;
2096 break;
2097 }
Chris Lattner11333092005-01-11 03:11:44 +00002098 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002099 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2100 unsigned TmpReg2 = MakeReg(N.getValueType());
2101 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2102 unsigned TmpReg3 = MakeReg(N.getValueType());
2103 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
2104
2105 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2106 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2107 if (isNeg)
2108 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2109 return Result;
2110 }
2111 }
2112
Chris Lattner11333092005-01-11 03:11:44 +00002113 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2114 Tmp1 = SelectExpr(N.getOperand(0));
2115 Tmp2 = SelectExpr(N.getOperand(1));
2116 } else {
2117 Tmp2 = SelectExpr(N.getOperand(1));
2118 Tmp1 = SelectExpr(N.getOperand(0));
2119 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002120
2121 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2122 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2123 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2124 switch (N.getValueType()) {
2125 default: assert(0 && "Cannot sdiv this type!");
2126 case MVT::i8:
2127 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2128 LoReg = X86::AL;
2129 HiReg = X86::AH;
2130 MovOpcode = X86::MOV8rr;
2131 ClrOpcode = X86::MOV8ri;
2132 SExtOpcode = X86::CBW;
2133 break;
2134 case MVT::i16:
2135 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2136 LoReg = X86::AX;
2137 HiReg = X86::DX;
2138 MovOpcode = X86::MOV16rr;
2139 ClrOpcode = X86::MOV16ri;
2140 SExtOpcode = X86::CWD;
2141 break;
2142 case MVT::i32:
2143 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002144 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002145 HiReg = X86::EDX;
2146 MovOpcode = X86::MOV32rr;
2147 ClrOpcode = X86::MOV32ri;
2148 SExtOpcode = X86::CDQ;
2149 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002150 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002151 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002152 return Result;
2153 }
2154
2155 // Set up the low part.
2156 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2157
2158 if (isSigned) {
2159 // Sign extend the low part into the high part.
2160 BuildMI(BB, SExtOpcode, 0);
2161 } else {
2162 // Zero out the high part, effectively zero extending the input.
2163 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2164 }
2165
2166 // Emit the DIV/IDIV instruction.
2167 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
2168
2169 // Get the result of the divide or rem.
2170 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2171 return Result;
2172 }
2173
2174 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002175 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002176 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2177 switch (N.getValueType()) {
2178 default: assert(0 && "Cannot shift this type!");
2179 case MVT::i8: Opc = X86::ADD8rr; break;
2180 case MVT::i16: Opc = X86::ADD16rr; break;
2181 case MVT::i32: Opc = X86::ADD32rr; break;
2182 }
2183 Tmp1 = SelectExpr(N.getOperand(0));
2184 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2185 return Result;
2186 }
2187
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002188 switch (N.getValueType()) {
2189 default: assert(0 && "Cannot shift this type!");
2190 case MVT::i8: Opc = X86::SHL8ri; break;
2191 case MVT::i16: Opc = X86::SHL16ri; break;
2192 case MVT::i32: Opc = X86::SHL32ri; break;
2193 }
Chris Lattner11333092005-01-11 03:11:44 +00002194 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002195 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2196 return Result;
2197 }
Chris Lattner11333092005-01-11 03:11:44 +00002198
2199 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2200 Tmp1 = SelectExpr(N.getOperand(0));
2201 Tmp2 = SelectExpr(N.getOperand(1));
2202 } else {
2203 Tmp2 = SelectExpr(N.getOperand(1));
2204 Tmp1 = SelectExpr(N.getOperand(0));
2205 }
2206
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002207 switch (N.getValueType()) {
2208 default: assert(0 && "Cannot shift this type!");
2209 case MVT::i8 : Opc = X86::SHL8rCL; break;
2210 case MVT::i16: Opc = X86::SHL16rCL; break;
2211 case MVT::i32: Opc = X86::SHL32rCL; break;
2212 }
2213 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2214 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2215 return Result;
2216 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002217 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2218 switch (N.getValueType()) {
2219 default: assert(0 && "Cannot shift this type!");
2220 case MVT::i8: Opc = X86::SHR8ri; break;
2221 case MVT::i16: Opc = X86::SHR16ri; break;
2222 case MVT::i32: Opc = X86::SHR32ri; break;
2223 }
Chris Lattner11333092005-01-11 03:11:44 +00002224 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002225 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2226 return Result;
2227 }
Chris Lattner11333092005-01-11 03:11:44 +00002228
2229 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2230 Tmp1 = SelectExpr(N.getOperand(0));
2231 Tmp2 = SelectExpr(N.getOperand(1));
2232 } else {
2233 Tmp2 = SelectExpr(N.getOperand(1));
2234 Tmp1 = SelectExpr(N.getOperand(0));
2235 }
2236
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002237 switch (N.getValueType()) {
2238 default: assert(0 && "Cannot shift this type!");
2239 case MVT::i8 : Opc = X86::SHR8rCL; break;
2240 case MVT::i16: Opc = X86::SHR16rCL; break;
2241 case MVT::i32: Opc = X86::SHR32rCL; break;
2242 }
2243 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2244 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2245 return Result;
2246 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002247 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2248 switch (N.getValueType()) {
2249 default: assert(0 && "Cannot shift this type!");
2250 case MVT::i8: Opc = X86::SAR8ri; break;
2251 case MVT::i16: Opc = X86::SAR16ri; break;
2252 case MVT::i32: Opc = X86::SAR32ri; break;
2253 }
Chris Lattner11333092005-01-11 03:11:44 +00002254 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002255 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2256 return Result;
2257 }
Chris Lattner11333092005-01-11 03:11:44 +00002258
2259 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2260 Tmp1 = SelectExpr(N.getOperand(0));
2261 Tmp2 = SelectExpr(N.getOperand(1));
2262 } else {
2263 Tmp2 = SelectExpr(N.getOperand(1));
2264 Tmp1 = SelectExpr(N.getOperand(0));
2265 }
2266
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002267 switch (N.getValueType()) {
2268 default: assert(0 && "Cannot shift this type!");
2269 case MVT::i8 : Opc = X86::SAR8rCL; break;
2270 case MVT::i16: Opc = X86::SAR16rCL; break;
2271 case MVT::i32: Opc = X86::SAR32rCL; break;
2272 }
2273 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2274 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2275 return Result;
2276
2277 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002278 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002279 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2280 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2281 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002282 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002283 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002284 if (Result != 1) { // Generate the token
2285 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2286 assert(0 && "Load already emitted!?");
2287 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002288 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2289
Chris Lattner5188ad72005-01-08 19:28:19 +00002290 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002291 default: assert(0 && "Cannot load this type!");
2292 case MVT::i1:
2293 case MVT::i8: Opc = X86::MOV8rm; break;
2294 case MVT::i16: Opc = X86::MOV16rm; break;
2295 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002296 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2297 }
Chris Lattner11333092005-01-11 03:11:44 +00002298
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002299 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002300 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002301 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2302 } else {
2303 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002304
2305 SDOperand Chain = N.getOperand(0);
2306 SDOperand Address = N.getOperand(1);
2307 if (getRegPressure(Chain) > getRegPressure(Address)) {
2308 Select(Chain);
2309 SelectAddress(Address, AM);
2310 } else {
2311 SelectAddress(Address, AM);
2312 Select(Chain);
2313 }
2314
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002315 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2316 }
2317 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002318
2319 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2320 case ISD::ZEXTLOAD: {
2321 // Make sure we generate both values.
2322 if (Result != 1)
2323 ExprMap[N.getValue(1)] = 1; // Generate the token
2324 else
2325 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2326
Chris Lattnerda2ce112005-01-16 07:34:08 +00002327 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2328 if (Node->getValueType(0) == MVT::f64) {
2329 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2330 "Bad EXTLOAD!");
2331 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2332 CP->getIndex());
2333 return Result;
2334 }
2335
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002336 X86AddressMode AM;
2337 if (getRegPressure(Node->getOperand(0)) >
2338 getRegPressure(Node->getOperand(1))) {
2339 Select(Node->getOperand(0)); // chain
2340 SelectAddress(Node->getOperand(1), AM);
2341 } else {
2342 SelectAddress(Node->getOperand(1), AM);
2343 Select(Node->getOperand(0)); // chain
2344 }
2345
2346 switch (Node->getValueType(0)) {
2347 default: assert(0 && "Unknown type to sign extend to.");
2348 case MVT::f64:
2349 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2350 "Bad EXTLOAD!");
2351 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2352 break;
2353 case MVT::i32:
2354 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2355 default:
2356 assert(0 && "Bad zero extend!");
2357 case MVT::i1:
2358 case MVT::i8:
2359 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2360 break;
2361 case MVT::i16:
2362 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2363 break;
2364 }
2365 break;
2366 case MVT::i16:
2367 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2368 "Bad zero extend!");
2369 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2370 break;
2371 case MVT::i8:
2372 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2373 "Bad zero extend!");
2374 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2375 break;
2376 }
2377 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002378 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002379 case ISD::SEXTLOAD: {
2380 // Make sure we generate both values.
2381 if (Result != 1)
2382 ExprMap[N.getValue(1)] = 1; // Generate the token
2383 else
2384 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2385
2386 X86AddressMode AM;
2387 if (getRegPressure(Node->getOperand(0)) >
2388 getRegPressure(Node->getOperand(1))) {
2389 Select(Node->getOperand(0)); // chain
2390 SelectAddress(Node->getOperand(1), AM);
2391 } else {
2392 SelectAddress(Node->getOperand(1), AM);
2393 Select(Node->getOperand(0)); // chain
2394 }
2395
2396 switch (Node->getValueType(0)) {
2397 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2398 default: assert(0 && "Unknown type to sign extend to.");
2399 case MVT::i32:
2400 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2401 default:
2402 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2403 case MVT::i8:
2404 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2405 break;
2406 case MVT::i16:
2407 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2408 break;
2409 }
2410 break;
2411 case MVT::i16:
2412 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2413 "Cannot sign extend from bool!");
2414 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2415 break;
2416 }
2417 return Result;
2418 }
2419
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002420 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002421 // Generate both result values.
2422 if (Result != 1)
2423 ExprMap[N.getValue(1)] = 1; // Generate the token
2424 else
2425 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2426
2427 // FIXME: We are currently ignoring the requested alignment for handling
2428 // greater than the stack alignment. This will need to be revisited at some
2429 // point. Align = N.getOperand(2);
2430
2431 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2432 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2433 std::cerr << "Cannot allocate stack object with greater alignment than"
2434 << " the stack alignment yet!";
2435 abort();
2436 }
2437
2438 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002439 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002440 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2441 .addImm(CN->getValue());
2442 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002443 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2444 Select(N.getOperand(0));
2445 Tmp1 = SelectExpr(N.getOperand(1));
2446 } else {
2447 Tmp1 = SelectExpr(N.getOperand(1));
2448 Select(N.getOperand(0));
2449 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002450
2451 // Subtract size from stack pointer, thereby allocating some space.
2452 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2453 }
2454
2455 // Put a pointer to the space into the result register, by copying the stack
2456 // pointer.
2457 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2458 return Result;
2459
2460 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002461 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002462 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002463
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002464 if (GlobalAddressSDNode *GASD =
2465 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002466 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002467 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2468 } else if (ExternalSymbolSDNode *ESSDN =
2469 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002470 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002471 BuildMI(BB, X86::CALLpcrel32,
2472 1).addExternalSymbol(ESSDN->getSymbol(), true);
2473 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002474 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2475 Select(N.getOperand(0));
2476 Tmp1 = SelectExpr(N.getOperand(1));
2477 } else {
2478 Tmp1 = SelectExpr(N.getOperand(1));
2479 Select(N.getOperand(0));
2480 }
2481
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002482 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2483 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002484 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002485 default: assert(0 && "Unknown value type for call result!");
2486 case MVT::Other: return 1;
2487 case MVT::i1:
2488 case MVT::i8:
2489 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2490 break;
2491 case MVT::i16:
2492 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2493 break;
2494 case MVT::i32:
2495 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002496 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002497 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2498 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002499 case MVT::f64: // Floating-point return values live in %ST(0)
2500 ContainsFPCode = true;
2501 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2502 break;
2503 }
2504 return Result+N.ResNo;
2505 }
2506
2507 return 0;
2508}
2509
Chris Lattnere10269b2005-01-17 19:25:26 +00002510/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2511/// load/op/store instruction. If successful return true.
2512bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2513 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2514 SDOperand Chain = Node->getOperand(0);
2515 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002516 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002517
2518 // The chain has to be a load, the stored value must be an integer binary
2519 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002520 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002521 MVT::isFloatingPoint(StVal.getValueType()))
2522 return false;
2523
Chris Lattner5c659812005-01-17 22:10:42 +00002524 // Token chain must either be a factor node or the load to fold.
2525 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2526 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002527
Chris Lattner5c659812005-01-17 22:10:42 +00002528 SDOperand TheLoad;
2529
2530 // Check to see if there is a load from the same pointer that we're storing
2531 // to in either operand of the binop.
2532 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2533 StVal.getOperand(0).getOperand(1) == StPtr)
2534 TheLoad = StVal.getOperand(0);
2535 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2536 StVal.getOperand(1).getOperand(1) == StPtr)
2537 TheLoad = StVal.getOperand(1);
2538 else
2539 return false; // No matching load operand.
2540
2541 // We can only fold the load if there are no intervening side-effecting
2542 // operations. This means that the store uses the load as its token chain, or
2543 // there are only token factor nodes in between the store and load.
2544 if (Chain != TheLoad.getValue(1)) {
2545 // Okay, the other option is that we have a store referring to (possibly
2546 // nested) token factor nodes. For now, just try peeking through one level
2547 // of token factors to see if this is the case.
2548 bool ChainOk = false;
2549 if (Chain.getOpcode() == ISD::TokenFactor) {
2550 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2551 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2552 ChainOk = true;
2553 break;
2554 }
2555 }
2556
2557 if (!ChainOk) return false;
2558 }
2559
2560 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002561 return false;
2562
2563 // Make sure that one of the operands of the binop is the load, and that the
2564 // load folds into the binop.
2565 if (((StVal.getOperand(0) != TheLoad ||
2566 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2567 (StVal.getOperand(1) != TheLoad ||
2568 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2569 return false;
2570
2571 // Finally, check to see if this is one of the ops we can handle!
2572 static const unsigned ADDTAB[] = {
2573 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2574 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2575 };
2576 static const unsigned SUBTAB[] = {
2577 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2578 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2579 };
2580 static const unsigned ANDTAB[] = {
2581 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2582 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2583 };
2584 static const unsigned ORTAB[] = {
2585 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2586 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2587 };
2588 static const unsigned XORTAB[] = {
2589 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2590 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2591 };
2592 static const unsigned SHLTAB[] = {
2593 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2594 /*Have to put the reg in CL*/0, 0, 0,
2595 };
2596 static const unsigned SARTAB[] = {
2597 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2598 /*Have to put the reg in CL*/0, 0, 0,
2599 };
2600 static const unsigned SHRTAB[] = {
2601 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2602 /*Have to put the reg in CL*/0, 0, 0,
2603 };
2604
2605 const unsigned *TabPtr = 0;
2606 switch (StVal.getOpcode()) {
2607 default:
2608 std::cerr << "CANNOT [mem] op= val: ";
2609 StVal.Val->dump(); std::cerr << "\n";
2610 case ISD::MUL:
2611 case ISD::SDIV:
2612 case ISD::UDIV:
2613 case ISD::SREM:
2614 case ISD::UREM: return false;
2615
2616 case ISD::ADD: TabPtr = ADDTAB; break;
2617 case ISD::SUB: TabPtr = SUBTAB; break;
2618 case ISD::AND: TabPtr = ANDTAB; break;
2619 case ISD:: OR: TabPtr = ORTAB; break;
2620 case ISD::XOR: TabPtr = XORTAB; break;
2621 case ISD::SHL: TabPtr = SHLTAB; break;
2622 case ISD::SRA: TabPtr = SARTAB; break;
2623 case ISD::SRL: TabPtr = SHRTAB; break;
2624 }
2625
2626 // Handle: [mem] op= CST
2627 SDOperand Op0 = StVal.getOperand(0);
2628 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002629 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002630 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2631 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2632 default: break;
2633 case MVT::i1:
2634 case MVT::i8: Opc = TabPtr[0]; break;
2635 case MVT::i16: Opc = TabPtr[1]; break;
2636 case MVT::i32: Opc = TabPtr[2]; break;
2637 }
2638
2639 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002640 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2641 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002642 Select(Chain);
2643
Chris Lattnere10269b2005-01-17 19:25:26 +00002644 X86AddressMode AM;
2645 if (getRegPressure(TheLoad.getOperand(0)) >
2646 getRegPressure(TheLoad.getOperand(1))) {
2647 Select(TheLoad.getOperand(0));
2648 SelectAddress(TheLoad.getOperand(1), AM);
2649 } else {
2650 SelectAddress(TheLoad.getOperand(1), AM);
2651 Select(TheLoad.getOperand(0));
2652 }
Chris Lattner5c659812005-01-17 22:10:42 +00002653
2654 if (StVal.getOpcode() == ISD::ADD) {
2655 if (CN->getValue() == 1) {
2656 switch (Op0.getValueType()) {
2657 default: break;
2658 case MVT::i8:
2659 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2660 return true;
2661 case MVT::i16: Opc = TabPtr[1];
2662 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2663 return true;
2664 case MVT::i32: Opc = TabPtr[2];
2665 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2666 return true;
2667 }
2668 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2669 switch (Op0.getValueType()) {
2670 default: break;
2671 case MVT::i8:
2672 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2673 return true;
2674 case MVT::i16: Opc = TabPtr[1];
2675 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2676 return true;
2677 case MVT::i32: Opc = TabPtr[2];
2678 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2679 return true;
2680 }
2681 }
2682 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002683
2684 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2685 return true;
2686 }
2687 }
2688
2689 // If we have [mem] = V op [mem], try to turn it into:
2690 // [mem] = [mem] op V.
2691 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2692 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2693 StVal.getOpcode() != ISD::SRL)
2694 std::swap(Op0, Op1);
2695
2696 if (Op0 != TheLoad) return false;
2697
2698 switch (Op0.getValueType()) {
2699 default: return false;
2700 case MVT::i1:
2701 case MVT::i8: Opc = TabPtr[3]; break;
2702 case MVT::i16: Opc = TabPtr[4]; break;
2703 case MVT::i32: Opc = TabPtr[5]; break;
2704 }
Chris Lattner5c659812005-01-17 22:10:42 +00002705
Chris Lattnerb422aea2005-01-18 17:35:28 +00002706 // Table entry doesn't exist?
2707 if (Opc == 0) return false;
2708
Chris Lattner4a108662005-01-18 03:51:59 +00002709 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2710 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002711 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002712 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002713
Chris Lattnere10269b2005-01-17 19:25:26 +00002714 X86AddressMode AM;
2715 SelectAddress(TheLoad.getOperand(1), AM);
2716 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002717 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002718 return true;
2719}
2720
2721
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002722void ISel::Select(SDOperand N) {
2723 unsigned Tmp1, Tmp2, Opc;
2724
2725 // FIXME: Disable for our current expansion model!
Chris Lattner4a108662005-01-18 03:51:59 +00002726 if (/*!N->hasOneUse() &&*/ !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}