blob: 0e77a7f8b8eea7fcf02a2ace62ac6c62d7bd4d78 [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>
Nate Begeman8e21e712005-03-26 01:29:23 +000087 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
88 SDOperand Callee, 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,
Nate Begeman8e21e712005-03-26 01:29:23 +0000166 const Type *RetTy, bool isVarArg,
167 SDOperand Callee, 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 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000447 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000448 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
449 ++NumFPKill;
450 }
451
452 // Clear state used for selection.
453 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000454 RegPressureMap.clear();
455}
456
457
Chris Lattner11333092005-01-11 03:11:44 +0000458// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
459// for the number of registers required to compute each node. This is basically
460// computing a generalized form of the Sethi-Ullman number for each node.
461unsigned ISel::ComputeRegPressure(SDOperand O) {
462 SDNode *N = O.Val;
463 unsigned &Result = RegPressureMap[N];
464 if (Result) return Result;
465
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000466 // FIXME: Should operations like CALL (which clobber lots o regs) have a
467 // higher fixed cost??
468
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000469 if (N->getNumOperands() == 0) {
470 Result = 1;
471 } else {
472 unsigned MaxRegUse = 0;
473 unsigned NumExtraMaxRegUsers = 0;
474 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
475 unsigned Regs;
476 if (N->getOperand(i).getOpcode() == ISD::Constant)
477 Regs = 0;
478 else
479 Regs = ComputeRegPressure(N->getOperand(i));
480 if (Regs > MaxRegUse) {
481 MaxRegUse = Regs;
482 NumExtraMaxRegUsers = 0;
483 } else if (Regs == MaxRegUse &&
484 N->getOperand(i).getValueType() != MVT::Other) {
485 ++NumExtraMaxRegUsers;
486 }
Chris Lattner11333092005-01-11 03:11:44 +0000487 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000488
489 if (O.getOpcode() != ISD::TokenFactor)
490 Result = MaxRegUse+NumExtraMaxRegUsers;
491 else
Chris Lattner869e0432005-01-17 23:02:13 +0000492 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000493 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000494
Chris Lattner837caa72005-01-11 23:21:30 +0000495 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000496 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000497}
498
Chris Lattnerbf52d492005-01-20 16:50:16 +0000499/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
500/// The DAG cannot have cycles in it, by definition, so the visited set is not
501/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
502/// reuse, so it prevents exponential cases.
503///
504static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
505 std::set<SDNode*> &Visited) {
506 if (N == Op) return true; // Found it.
507 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000508 if (Node->getNumOperands() == 0 || // Leaf?
509 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000510 if (!Visited.insert(Node).second) return false; // Already visited?
511
512 // Recurse for the first N-1 operands.
513 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
514 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
515 return true;
516
517 // Tail recurse for the last operand.
518 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
519}
520
Chris Lattner98a8ba02005-01-18 01:06:26 +0000521X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
522 X86AddressMode Result;
523
524 // If we need to emit two register operands, emit the one with the highest
525 // register pressure first.
526 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
527 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000528 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000529 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000530 std::set<SDNode*> Visited;
531 EmitBaseThenIndex = true;
532 // If Base ends up pointing to Index, we must emit index first. This is
533 // because of the way we fold loads, we may end up doing bad things with
534 // the folded add.
535 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
536 EmitBaseThenIndex = false;
537 } else {
538 std::set<SDNode*> Visited;
539 EmitBaseThenIndex = false;
540 // If Base ends up pointing to Index, we must emit index first. This is
541 // because of the way we fold loads, we may end up doing bad things with
542 // the folded add.
543 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
544 EmitBaseThenIndex = true;
545 }
546
547 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000548 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
549 Result.IndexReg = SelectExpr(IAM.IndexReg);
550 } else {
551 Result.IndexReg = SelectExpr(IAM.IndexReg);
552 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
553 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000554
Chris Lattner98a8ba02005-01-18 01:06:26 +0000555 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
556 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
557 } else if (IAM.IndexReg.Val) {
558 Result.IndexReg = SelectExpr(IAM.IndexReg);
559 }
560
561 switch (IAM.BaseType) {
562 case X86ISelAddressMode::RegBase:
563 Result.BaseType = X86AddressMode::RegBase;
564 break;
565 case X86ISelAddressMode::FrameIndexBase:
566 Result.BaseType = X86AddressMode::FrameIndexBase;
567 Result.Base.FrameIndex = IAM.Base.FrameIndex;
568 break;
569 default:
570 assert(0 && "Unknown base type!");
571 break;
572 }
573 Result.Scale = IAM.Scale;
574 Result.Disp = IAM.Disp;
575 Result.GV = IAM.GV;
576 return Result;
577}
578
579/// SelectAddress - Pattern match the maximal addressing mode for this node and
580/// emit all of the leaf registers.
581void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
582 X86ISelAddressMode IAM;
583 MatchAddress(N, IAM);
584 AM = SelectAddrExprs(IAM);
585}
586
587/// MatchAddress - Add the specified node to the specified addressing mode,
588/// returning true if it cannot be done. This just pattern matches for the
589/// addressing mode, it does not cause any code to be emitted. For that, use
590/// SelectAddress.
591bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000592 switch (N.getOpcode()) {
593 default: break;
594 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000595 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
596 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000597 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
598 return false;
599 }
600 break;
601 case ISD::GlobalAddress:
602 if (AM.GV == 0) {
603 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
604 return false;
605 }
606 break;
607 case ISD::Constant:
608 AM.Disp += cast<ConstantSDNode>(N)->getValue();
609 return false;
610 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000611 // We might have folded the load into this shift, so don't regen the value
612 // if so.
613 if (ExprMap.count(N)) break;
614
Chris Lattner98a8ba02005-01-18 01:06:26 +0000615 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000616 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
617 unsigned Val = CN->getValue();
618 if (Val == 1 || Val == 2 || Val == 3) {
619 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000620 SDOperand ShVal = N.Val->getOperand(0);
621
622 // Okay, we know that we have a scale by now. However, if the scaled
623 // value is an add of something and a constant, we can fold the
624 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000625 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000626 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000627 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000628 ConstantSDNode *AddVal =
629 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
630 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000631 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000632 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000633 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000634 return false;
635 }
636 }
637 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000638 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000639 // We might have folded the load into this mul, so don't regen the value if
640 // so.
641 if (ExprMap.count(N)) break;
642
Chris Lattner947d5442005-01-11 19:37:02 +0000643 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000644 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
645 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000646 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
647 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
648 AM.Scale = unsigned(CN->getValue())-1;
649
650 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000651 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000652
653 // Okay, we know that we have a scale by now. However, if the scaled
654 // value is an add of something and a constant, we can fold the
655 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000656 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000657 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000658 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000659 ConstantSDNode *AddVal =
660 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
661 AM.Disp += AddVal->getValue() * CN->getValue();
662 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000663 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000664 }
665
666 AM.IndexReg = AM.Base.Reg = Reg;
667 return false;
668 }
669 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000670
671 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000672 // We might have folded the load into this mul, so don't regen the value if
673 // so.
674 if (ExprMap.count(N)) break;
675
Chris Lattner98a8ba02005-01-18 01:06:26 +0000676 X86ISelAddressMode Backup = AM;
677 if (!MatchAddress(N.Val->getOperand(0), AM) &&
678 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000679 return false;
680 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000681 if (!MatchAddress(N.Val->getOperand(1), AM) &&
682 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000683 return false;
684 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000685 break;
686 }
687 }
688
Chris Lattnera95589b2005-01-11 04:40:19 +0000689 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000690 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000691 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000692 if (AM.IndexReg.Val == 0) {
693 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000694 AM.Scale = 1;
695 return false;
696 }
697
698 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000699 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000700 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000701
702 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000703 AM.BaseType = X86ISelAddressMode::RegBase;
704 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000705 return false;
706}
707
708/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
709/// assuming that the temporary registers are in the 8-bit register class.
710///
711/// Tmp1 = setcc1
712/// Tmp2 = setcc2
713/// DestReg = logicalop Tmp1, Tmp2
714///
715static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
716 unsigned SetCC2, unsigned LogicalOp,
717 unsigned DestReg) {
718 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
719 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
720 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
721 BuildMI(BB, SetCC1, 0, Tmp1);
722 BuildMI(BB, SetCC2, 0, Tmp2);
723 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
724}
725
726/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
727/// condition codes match the specified SetCCOpcode. Note that some conditions
728/// require multiple instructions to generate the correct value.
729static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
730 ISD::CondCode SetCCOpcode, bool isFP) {
731 unsigned Opc;
732 if (!isFP) {
733 switch (SetCCOpcode) {
734 default: assert(0 && "Illegal integer SetCC!");
735 case ISD::SETEQ: Opc = X86::SETEr; break;
736 case ISD::SETGT: Opc = X86::SETGr; break;
737 case ISD::SETGE: Opc = X86::SETGEr; break;
738 case ISD::SETLT: Opc = X86::SETLr; break;
739 case ISD::SETLE: Opc = X86::SETLEr; break;
740 case ISD::SETNE: Opc = X86::SETNEr; break;
741 case ISD::SETULT: Opc = X86::SETBr; break;
742 case ISD::SETUGT: Opc = X86::SETAr; break;
743 case ISD::SETULE: Opc = X86::SETBEr; break;
744 case ISD::SETUGE: Opc = X86::SETAEr; break;
745 }
746 } else {
747 // On a floating point condition, the flags are set as follows:
748 // ZF PF CF op
749 // 0 | 0 | 0 | X > Y
750 // 0 | 0 | 1 | X < Y
751 // 1 | 0 | 0 | X == Y
752 // 1 | 1 | 1 | unordered
753 //
754 switch (SetCCOpcode) {
755 default: assert(0 && "Invalid FP setcc!");
756 case ISD::SETUEQ:
757 case ISD::SETEQ:
758 Opc = X86::SETEr; // True if ZF = 1
759 break;
760 case ISD::SETOGT:
761 case ISD::SETGT:
762 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
763 break;
764 case ISD::SETOGE:
765 case ISD::SETGE:
766 Opc = X86::SETAEr; // True if CF = 0
767 break;
768 case ISD::SETULT:
769 case ISD::SETLT:
770 Opc = X86::SETBr; // True if CF = 1
771 break;
772 case ISD::SETULE:
773 case ISD::SETLE:
774 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
775 break;
776 case ISD::SETONE:
777 case ISD::SETNE:
778 Opc = X86::SETNEr; // True if ZF = 0
779 break;
780 case ISD::SETUO:
781 Opc = X86::SETPr; // True if PF = 1
782 break;
783 case ISD::SETO:
784 Opc = X86::SETNPr; // True if PF = 0
785 break;
786 case ISD::SETOEQ: // !PF & ZF
787 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
788 return;
789 case ISD::SETOLT: // !PF & CF
790 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
791 return;
792 case ISD::SETOLE: // !PF & (CF || ZF)
793 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
794 return;
795 case ISD::SETUGT: // PF | (!ZF & !CF)
796 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
797 return;
798 case ISD::SETUGE: // PF | !CF
799 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
800 return;
801 case ISD::SETUNE: // PF | !ZF
802 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
803 return;
804 }
805 }
806 BuildMI(BB, Opc, 0, DestReg);
807}
808
809
810/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
811/// the Dest block if the Cond condition is true. If we cannot fold this
812/// condition into the branch, return true.
813///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000814bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
815 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000816 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
817 // B) using two conditional branches instead of one condbr, two setcc's, and
818 // an or.
819 if ((Cond.getOpcode() == ISD::OR ||
820 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
821 // And and or set the flags for us, so there is no need to emit a TST of the
822 // result. It is only safe to do this if there is only a single use of the
823 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000824 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000825 SelectExpr(Cond);
826 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
827 return false;
828 }
829
830 // Codegen br not C -> JE.
831 if (Cond.getOpcode() == ISD::XOR)
832 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
833 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000834 unsigned CondR;
835 if (getRegPressure(Chain) > getRegPressure(Cond)) {
836 Select(Chain);
837 CondR = SelectExpr(Cond.Val->getOperand(0));
838 } else {
839 CondR = SelectExpr(Cond.Val->getOperand(0));
840 Select(Chain);
841 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000842 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
843 BuildMI(BB, X86::JE, 1).addMBB(Dest);
844 return false;
845 }
846
847 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
848 if (SetCC == 0)
849 return true; // Can only handle simple setcc's so far.
850
851 unsigned Opc;
852
853 // Handle integer conditions first.
854 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
855 switch (SetCC->getCondition()) {
856 default: assert(0 && "Illegal integer SetCC!");
857 case ISD::SETEQ: Opc = X86::JE; break;
858 case ISD::SETGT: Opc = X86::JG; break;
859 case ISD::SETGE: Opc = X86::JGE; break;
860 case ISD::SETLT: Opc = X86::JL; break;
861 case ISD::SETLE: Opc = X86::JLE; break;
862 case ISD::SETNE: Opc = X86::JNE; break;
863 case ISD::SETULT: Opc = X86::JB; break;
864 case ISD::SETUGT: Opc = X86::JA; break;
865 case ISD::SETULE: Opc = X86::JBE; break;
866 case ISD::SETUGE: Opc = X86::JAE; break;
867 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000868 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000869 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000870 BuildMI(BB, Opc, 1).addMBB(Dest);
871 return false;
872 }
873
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000874 unsigned Opc2 = 0; // Second branch if needed.
875
876 // On a floating point condition, the flags are set as follows:
877 // ZF PF CF op
878 // 0 | 0 | 0 | X > Y
879 // 0 | 0 | 1 | X < Y
880 // 1 | 0 | 0 | X == Y
881 // 1 | 1 | 1 | unordered
882 //
883 switch (SetCC->getCondition()) {
884 default: assert(0 && "Invalid FP setcc!");
885 case ISD::SETUEQ:
886 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
887 case ISD::SETOGT:
888 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
889 case ISD::SETOGE:
890 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
891 case ISD::SETULT:
892 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
893 case ISD::SETULE:
894 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
895 case ISD::SETONE:
896 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
897 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
898 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
899 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
900 Opc = X86::JA; // ZF = 0 & CF = 0
901 Opc2 = X86::JP; // PF = 1
902 break;
903 case ISD::SETUGE: // PF = 1 | CF = 0
904 Opc = X86::JAE; // CF = 0
905 Opc2 = X86::JP; // PF = 1
906 break;
907 case ISD::SETUNE: // PF = 1 | ZF = 0
908 Opc = X86::JNE; // ZF = 0
909 Opc2 = X86::JP; // PF = 1
910 break;
911 case ISD::SETOEQ: // PF = 0 & ZF = 1
912 //X86::JNP, X86::JE
913 //X86::AND8rr
914 return true; // FIXME: Emit more efficient code for this branch.
915 case ISD::SETOLT: // PF = 0 & CF = 1
916 //X86::JNP, X86::JB
917 //X86::AND8rr
918 return true; // FIXME: Emit more efficient code for this branch.
919 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
920 //X86::JNP, X86::JBE
921 //X86::AND8rr
922 return true; // FIXME: Emit more efficient code for this branch.
923 }
924
Chris Lattner6c07aee2005-01-11 04:06:27 +0000925 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000926 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000927 BuildMI(BB, Opc, 1).addMBB(Dest);
928 if (Opc2)
929 BuildMI(BB, Opc2, 1).addMBB(Dest);
930 return false;
931}
932
Chris Lattner24aad1b2005-01-10 22:10:13 +0000933/// EmitSelectCC - Emit code into BB that performs a select operation between
934/// the two registers RTrue and RFalse, generating a result into RDest. Return
935/// true if the fold cannot be performed.
936///
937void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
938 unsigned RTrue, unsigned RFalse, unsigned RDest) {
939 enum Condition {
940 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
941 NOT_SET
942 } CondCode = NOT_SET;
943
944 static const unsigned CMOVTAB16[] = {
945 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
946 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
947 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
948 };
949 static const unsigned CMOVTAB32[] = {
950 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
951 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
952 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
953 };
954 static const unsigned CMOVTABFP[] = {
955 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
956 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
957 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
958 };
959
960 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
961 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
962 switch (SetCC->getCondition()) {
963 default: assert(0 && "Unknown integer comparison!");
964 case ISD::SETEQ: CondCode = EQ; break;
965 case ISD::SETGT: CondCode = GT; break;
966 case ISD::SETGE: CondCode = GE; break;
967 case ISD::SETLT: CondCode = LT; break;
968 case ISD::SETLE: CondCode = LE; break;
969 case ISD::SETNE: CondCode = NE; break;
970 case ISD::SETULT: CondCode = B; break;
971 case ISD::SETUGT: CondCode = A; break;
972 case ISD::SETULE: CondCode = BE; break;
973 case ISD::SETUGE: CondCode = AE; break;
974 }
975 } else {
976 // On a floating point condition, the flags are set as follows:
977 // ZF PF CF op
978 // 0 | 0 | 0 | X > Y
979 // 0 | 0 | 1 | X < Y
980 // 1 | 0 | 0 | X == Y
981 // 1 | 1 | 1 | unordered
982 //
983 switch (SetCC->getCondition()) {
984 default: assert(0 && "Unknown FP comparison!");
985 case ISD::SETUEQ:
986 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
987 case ISD::SETOGT:
988 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
989 case ISD::SETOGE:
990 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
991 case ISD::SETULT:
992 case ISD::SETLT: CondCode = B; break; // True if CF = 1
993 case ISD::SETULE:
994 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
995 case ISD::SETONE:
996 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
997 case ISD::SETUO: CondCode = P; break; // True if PF = 1
998 case ISD::SETO: CondCode = NP; break; // True if PF = 0
999 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1000 case ISD::SETUGE: // PF = 1 | CF = 0
1001 case ISD::SETUNE: // PF = 1 | ZF = 0
1002 case ISD::SETOEQ: // PF = 0 & ZF = 1
1003 case ISD::SETOLT: // PF = 0 & CF = 1
1004 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1005 // We cannot emit this comparison as a single cmov.
1006 break;
1007 }
1008 }
1009 }
1010
1011 unsigned Opc = 0;
1012 if (CondCode != NOT_SET) {
1013 switch (SVT) {
1014 default: assert(0 && "Cannot select this type!");
1015 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1016 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001017 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001018 }
1019 }
1020
1021 // Finally, if we weren't able to fold this, just emit the condition and test
1022 // it.
1023 if (CondCode == NOT_SET || Opc == 0) {
1024 // Get the condition into the zero flag.
1025 unsigned CondReg = SelectExpr(Cond);
1026 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1027
1028 switch (SVT) {
1029 default: assert(0 && "Cannot select this type!");
1030 case MVT::i16: Opc = X86::CMOVE16rr; break;
1031 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001032 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001033 }
1034 } else {
1035 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001036 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001037 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001038 }
1039 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1040}
1041
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001042void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001043 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001044 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1045 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001046 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001047 switch (RHS.getValueType()) {
1048 default: break;
1049 case MVT::i1:
1050 case MVT::i8: Opc = X86::CMP8mi; break;
1051 case MVT::i16: Opc = X86::CMP16mi; break;
1052 case MVT::i32: Opc = X86::CMP32mi; break;
1053 }
1054 if (Opc) {
1055 X86AddressMode AM;
1056 EmitFoldedLoad(LHS, AM);
1057 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1058 return;
1059 }
1060 }
1061
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001062 switch (RHS.getValueType()) {
1063 default: break;
1064 case MVT::i1:
1065 case MVT::i8: Opc = X86::CMP8ri; break;
1066 case MVT::i16: Opc = X86::CMP16ri; break;
1067 case MVT::i32: Opc = X86::CMP32ri; break;
1068 }
1069 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001070 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001071 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1072 return;
1073 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001074 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1075 if (CN->isExactlyValue(+0.0) ||
1076 CN->isExactlyValue(-0.0)) {
1077 unsigned Reg = SelectExpr(LHS);
1078 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1079 BuildMI(BB, X86::FNSTSW8r, 0);
1080 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001081 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001082 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001083 }
1084
Chris Lattneref6806c2005-01-12 02:02:48 +00001085 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001086 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001087 switch (RHS.getValueType()) {
1088 default: break;
1089 case MVT::i1:
1090 case MVT::i8: Opc = X86::CMP8mr; break;
1091 case MVT::i16: Opc = X86::CMP16mr; break;
1092 case MVT::i32: Opc = X86::CMP32mr; break;
1093 }
1094 if (Opc) {
1095 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001096 EmitFoldedLoad(LHS, AM);
1097 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001098 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1099 return;
1100 }
1101 }
1102
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001103 switch (LHS.getValueType()) {
1104 default: assert(0 && "Cannot compare this value!");
1105 case MVT::i1:
1106 case MVT::i8: Opc = X86::CMP8rr; break;
1107 case MVT::i16: Opc = X86::CMP16rr; break;
1108 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001109 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001110 }
Chris Lattner11333092005-01-11 03:11:44 +00001111 unsigned Tmp1, Tmp2;
1112 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1113 Tmp1 = SelectExpr(LHS);
1114 Tmp2 = SelectExpr(RHS);
1115 } else {
1116 Tmp2 = SelectExpr(RHS);
1117 Tmp1 = SelectExpr(LHS);
1118 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001119 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1120}
1121
Chris Lattnera5ade062005-01-11 21:19:59 +00001122/// isFoldableLoad - Return true if this is a load instruction that can safely
1123/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001124bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1125 if (Op.getOpcode() == ISD::LOAD) {
1126 // FIXME: currently can't fold constant pool indexes.
1127 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1128 return false;
1129 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1130 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1131 // FIXME: currently can't fold constant pool indexes.
1132 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1133 return false;
1134 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001135 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001136 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001137
1138 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001139 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1140 if (ExprMap.count(Op.getValue(1))) return false;
1141 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001142 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001143
Chris Lattner4ff348b2005-01-17 06:26:58 +00001144 // If there is not just one use of its value, we cannot fold.
1145 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1146
1147 // Finally, we cannot fold the load into the operation if this would induce a
1148 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1149 // operand of the operation we are folding the load into) can possible use the
1150 // chain node defined by the load.
1151 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1152 std::set<SDNode*> Visited;
1153 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1154 return false;
1155 }
1156 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001157}
1158
Chris Lattner4ff348b2005-01-17 06:26:58 +00001159
Chris Lattnera5ade062005-01-11 21:19:59 +00001160/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1161/// and compute the address being loaded into AM.
1162void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1163 SDOperand Chain = Op.getOperand(0);
1164 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001165
Chris Lattnera5ade062005-01-11 21:19:59 +00001166 if (getRegPressure(Chain) > getRegPressure(Address)) {
1167 Select(Chain);
1168 SelectAddress(Address, AM);
1169 } else {
1170 SelectAddress(Address, AM);
1171 Select(Chain);
1172 }
1173
1174 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001175 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1176 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001177 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001178 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001179}
1180
Chris Lattner30ea1e92005-01-19 07:37:26 +00001181// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1182// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1183// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1184// return true.
1185bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001186 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1187 // good!
1188 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1189 std::swap(Op1, Op2); // Op1 is the SHL now.
1190 } else {
1191 return false; // No match
1192 }
1193
1194 SDOperand ShlVal = Op1.getOperand(0);
1195 SDOperand ShlAmt = Op1.getOperand(1);
1196 SDOperand ShrVal = Op2.getOperand(0);
1197 SDOperand ShrAmt = Op2.getOperand(1);
1198
Chris Lattner30ea1e92005-01-19 07:37:26 +00001199 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1200
Chris Lattner85716372005-01-19 06:18:43 +00001201 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1202 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1203 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001204 if (SubCST->getValue() == RegSize) {
1205 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001206 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001207 if (ShrVal == ShlVal) {
1208 unsigned Reg, ShAmt;
1209 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1210 Reg = SelectExpr(ShrVal);
1211 ShAmt = SelectExpr(ShrAmt);
1212 } else {
1213 ShAmt = SelectExpr(ShrAmt);
1214 Reg = SelectExpr(ShrVal);
1215 }
1216 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1217 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1218 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1219 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1220 return true;
1221 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001222 unsigned AReg, BReg;
1223 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001224 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001225 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001226 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001227 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001228 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001229 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001230 unsigned ShAmt = SelectExpr(ShrAmt);
1231 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1232 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1233 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001234 return true;
1235 }
1236 }
1237
Chris Lattner4053b1e2005-01-19 08:07:05 +00001238 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1239 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1240 if (SubCST->getValue() == RegSize) {
1241 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1242 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1243 if (ShrVal == ShlVal) {
1244 unsigned Reg, ShAmt;
1245 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1246 Reg = SelectExpr(ShrVal);
1247 ShAmt = SelectExpr(ShlAmt);
1248 } else {
1249 ShAmt = SelectExpr(ShlAmt);
1250 Reg = SelectExpr(ShrVal);
1251 }
1252 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1253 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1254 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1255 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1256 return true;
1257 } else if (RegSize != 8) {
1258 unsigned AReg, BReg;
1259 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001260 AReg = SelectExpr(ShlVal);
1261 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001262 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001263 BReg = SelectExpr(ShrVal);
1264 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001265 }
1266 unsigned ShAmt = SelectExpr(ShlAmt);
1267 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1268 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1269 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1270 return true;
1271 }
1272 }
Chris Lattner85716372005-01-19 06:18:43 +00001273
Chris Lattner4053b1e2005-01-19 08:07:05 +00001274 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1275 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1276 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1277 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1278 // (A >> 5) | (A << 27) --> ROR A, 5
1279 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1280 if (ShrVal == ShlVal) {
1281 unsigned Reg = SelectExpr(ShrVal);
1282 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1283 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1284 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1285 return true;
1286 } else if (RegSize != 8) {
1287 unsigned AReg, BReg;
1288 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001289 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001290 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001291 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001292 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001293 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001294 }
1295 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1296 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1297 .addImm(ShrCst->getValue());
1298 return true;
1299 }
1300 }
1301
Chris Lattner85716372005-01-19 06:18:43 +00001302 return false;
1303}
1304
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001305unsigned ISel::SelectExpr(SDOperand N) {
1306 unsigned Result;
1307 unsigned Tmp1, Tmp2, Tmp3;
1308 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001309 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001310 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001311
Chris Lattner7f2afac2005-01-14 22:37:41 +00001312 if (Node->getOpcode() == ISD::CopyFromReg) {
1313 // FIXME: Handle copy from physregs!
1314
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001315 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001316 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001317 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001318
1319 unsigned &Reg = ExprMap[N];
1320 if (Reg) return Reg;
1321
Chris Lattner19ad0622005-01-20 18:53:00 +00001322 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
1323 N.getOpcode() != ISD::SUB_PARTS)
Chris Lattnera5ade062005-01-11 21:19:59 +00001324 Reg = Result = (N.getValueType() != MVT::Other) ?
1325 MakeReg(N.getValueType()) : 1;
1326 else {
1327 // If this is a call instruction, make sure to prepare ALL of the result
1328 // values as well as the chain.
Chris Lattner19ad0622005-01-20 18:53:00 +00001329 if (N.getOpcode() == ISD::CALL) {
1330 if (Node->getNumValues() == 1)
1331 Reg = Result = 1; // Void call, just a chain.
1332 else {
1333 Result = MakeReg(Node->getValueType(0));
1334 ExprMap[N.getValue(0)] = Result;
1335 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1336 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1337 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
1338 }
1339 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001340 Result = MakeReg(Node->getValueType(0));
1341 ExprMap[N.getValue(0)] = Result;
Chris Lattner19ad0622005-01-20 18:53:00 +00001342 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001343 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001344 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001345 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001346
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001347 switch (N.getOpcode()) {
1348 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001349 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001350 assert(0 && "Node not handled!\n");
1351 case ISD::FrameIndex:
1352 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1353 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1354 return Result;
1355 case ISD::ConstantPool:
1356 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1357 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1358 return Result;
1359 case ISD::ConstantFP:
1360 ContainsFPCode = true;
1361 Tmp1 = Result; // Intermediate Register
1362 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1363 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1364 Tmp1 = MakeReg(MVT::f64);
1365
1366 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1367 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1368 BuildMI(BB, X86::FLD0, 0, Tmp1);
1369 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1370 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1371 BuildMI(BB, X86::FLD1, 0, Tmp1);
1372 else
1373 assert(0 && "Unexpected constant!");
1374 if (Tmp1 != Result)
1375 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1376 return Result;
1377 case ISD::Constant:
1378 switch (N.getValueType()) {
1379 default: assert(0 && "Cannot use constants of this type!");
1380 case MVT::i1:
1381 case MVT::i8: Opc = X86::MOV8ri; break;
1382 case MVT::i16: Opc = X86::MOV16ri; break;
1383 case MVT::i32: Opc = X86::MOV32ri; break;
1384 }
1385 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1386 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001387 case ISD::UNDEF:
1388 if (Node->getValueType(0) == MVT::f64) {
1389 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1390 BuildMI(BB, X86::FLD0, 0, Result);
1391 } else {
1392 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1393 }
1394 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001395 case ISD::GlobalAddress: {
1396 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1397 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1398 return Result;
1399 }
1400 case ISD::ExternalSymbol: {
1401 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1402 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1403 return Result;
1404 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001405 case ISD::ZERO_EXTEND: {
1406 int DestIs16 = N.getValueType() == MVT::i16;
1407 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001408
1409 // FIXME: This hack is here for zero extension casts from bool to i8. This
1410 // would not be needed if bools were promoted by Legalize.
1411 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001412 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001413 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1414 return Result;
1415 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001416
Chris Lattner4ff348b2005-01-17 06:26:58 +00001417 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001418 static const unsigned Opc[3] = {
1419 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1420 };
1421
1422 X86AddressMode AM;
1423 EmitFoldedLoad(N.getOperand(0), AM);
1424 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1425
1426 return Result;
1427 }
1428
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001429 static const unsigned Opc[3] = {
1430 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1431 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001432 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001433 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1434 return Result;
1435 }
1436 case ISD::SIGN_EXTEND: {
1437 int DestIs16 = N.getValueType() == MVT::i16;
1438 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1439
Chris Lattner590d8002005-01-09 18:52:44 +00001440 // FIXME: Legalize should promote bools to i8!
1441 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1442 "Sign extend from bool not implemented!");
1443
Chris Lattner4ff348b2005-01-17 06:26:58 +00001444 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001445 static const unsigned Opc[3] = {
1446 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1447 };
1448
1449 X86AddressMode AM;
1450 EmitFoldedLoad(N.getOperand(0), AM);
1451 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1452 return Result;
1453 }
1454
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001455 static const unsigned Opc[3] = {
1456 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1457 };
1458 Tmp1 = SelectExpr(N.getOperand(0));
1459 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1460 return Result;
1461 }
1462 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001463 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001464 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001465 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001466 switch (N.getValueType()) {
1467 default: assert(0 && "Unknown truncate!");
1468 case MVT::i1:
1469 case MVT::i8: Opc = X86::MOV8rm; break;
1470 case MVT::i16: Opc = X86::MOV16rm; break;
1471 }
1472 X86AddressMode AM;
1473 EmitFoldedLoad(N.getOperand(0), AM);
1474 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1475 return Result;
1476 }
1477
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001478 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1479 // a move out of AX or AL.
1480 switch (N.getOperand(0).getValueType()) {
1481 default: assert(0 && "Unknown truncate!");
1482 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1483 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1484 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1485 }
1486 Tmp1 = SelectExpr(N.getOperand(0));
1487 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1488
1489 switch (N.getValueType()) {
1490 default: assert(0 && "Unknown truncate!");
1491 case MVT::i1:
1492 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1493 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1494 }
1495 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1496 return Result;
1497
Chris Lattner590d8002005-01-09 18:52:44 +00001498 case ISD::SINT_TO_FP:
1499 case ISD::UINT_TO_FP: {
1500 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001501 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001502
1503 // Promote the integer to a type supported by FLD. We do this because there
1504 // are no unsigned FLD instructions, so we must promote an unsigned value to
1505 // a larger signed value, then use FLD on the larger value.
1506 //
1507 MVT::ValueType PromoteType = MVT::Other;
1508 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1509 unsigned PromoteOpcode = 0;
1510 unsigned RealDestReg = Result;
1511 switch (SrcTy) {
1512 case MVT::i1:
1513 case MVT::i8:
1514 // We don't have the facilities for directly loading byte sized data from
1515 // memory (even signed). Promote it to 16 bits.
1516 PromoteType = MVT::i16;
1517 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1518 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1519 break;
1520 case MVT::i16:
1521 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1522 PromoteType = MVT::i32;
1523 PromoteOpcode = X86::MOVZX32rr16;
1524 }
1525 break;
1526 default:
1527 // Don't fild into the real destination.
1528 if (Node->getOpcode() == ISD::UINT_TO_FP)
1529 Result = MakeReg(Node->getValueType(0));
1530 break;
1531 }
1532
1533 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1534
1535 if (PromoteType != MVT::Other) {
1536 Tmp2 = MakeReg(PromoteType);
1537 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1538 SrcTy = PromoteType;
1539 Tmp1 = Tmp2;
1540 }
1541
1542 // Spill the integer to memory and reload it from there.
1543 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1544 MachineFunction *F = BB->getParent();
1545 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1546
1547 switch (SrcTy) {
1548 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001549 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001550 // FIXME: this won't work for cast [u]long to FP
1551 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1552 FrameIdx).addReg(Tmp1);
1553 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1554 FrameIdx, 4).addReg(Tmp1+1);
1555 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1556 break;
1557 case MVT::i32:
1558 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1559 FrameIdx).addReg(Tmp1);
1560 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1561 break;
1562 case MVT::i16:
1563 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1564 FrameIdx).addReg(Tmp1);
1565 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1566 break;
1567 default: break; // No promotion required.
1568 }
1569
Chris Lattner085c9952005-01-12 04:00:00 +00001570 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001571 // If this is a cast from uint -> double, we need to be careful when if
1572 // the "sign" bit is set. If so, we don't want to make a negative number,
1573 // we want to make a positive number. Emit code to add an offset if the
1574 // sign bit is set.
1575
1576 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1577 unsigned IsNeg = MakeReg(MVT::i32);
1578 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1579
1580 // Create a CP value that has the offset in one word and 0 in the other.
1581 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1582 0x4f80000000000000ULL);
1583 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1584 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1585 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1586
1587 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1588 // We need special handling for unsigned 64-bit integer sources. If the
1589 // input number has the "sign bit" set, then we loaded it incorrectly as a
1590 // negative 64-bit number. In this case, add an offset value.
1591
1592 // Emit a test instruction to see if the dynamic input value was signed.
1593 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1594
1595 // If the sign bit is set, get a pointer to an offset, otherwise get a
1596 // pointer to a zero.
1597 MachineConstantPool *CP = F->getConstantPool();
1598 unsigned Zero = MakeReg(MVT::i32);
1599 Constant *Null = Constant::getNullValue(Type::UIntTy);
1600 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1601 CP->getConstantPoolIndex(Null));
1602 unsigned Offset = MakeReg(MVT::i32);
1603 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1604
1605 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1606 CP->getConstantPoolIndex(OffsetCst));
1607 unsigned Addr = MakeReg(MVT::i32);
1608 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1609
1610 // Load the constant for an add. FIXME: this could make an 'fadd' that
1611 // reads directly from memory, but we don't support these yet.
1612 unsigned ConstReg = MakeReg(MVT::f64);
1613 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1614
1615 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1616 }
1617 return RealDestReg;
1618 }
1619 case ISD::FP_TO_SINT:
1620 case ISD::FP_TO_UINT: {
1621 // FIXME: Most of this grunt work should be done by legalize!
1622 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1623
1624 // Change the floating point control register to use "round towards zero"
1625 // mode when truncating to an integer value.
1626 //
1627 MachineFunction *F = BB->getParent();
1628 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1629 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1630
1631 // Load the old value of the high byte of the control word...
1632 unsigned HighPartOfCW = MakeReg(MVT::i8);
1633 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1634 CWFrameIdx, 1);
1635
1636 // Set the high part to be round to zero...
1637 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1638 CWFrameIdx, 1).addImm(12);
1639
1640 // Reload the modified control word now...
1641 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1642
1643 // Restore the memory image of control word to original value
1644 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1645 CWFrameIdx, 1).addReg(HighPartOfCW);
1646
1647 // We don't have the facilities for directly storing byte sized data to
1648 // memory. Promote it to 16 bits. We also must promote unsigned values to
1649 // larger classes because we only have signed FP stores.
1650 MVT::ValueType StoreClass = Node->getValueType(0);
1651 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1652 switch (StoreClass) {
1653 case MVT::i8: StoreClass = MVT::i16; break;
1654 case MVT::i16: StoreClass = MVT::i32; break;
1655 case MVT::i32: StoreClass = MVT::i64; break;
1656 // The following treatment of cLong may not be perfectly right,
1657 // but it survives chains of casts of the form
1658 // double->ulong->double.
1659 case MVT::i64: StoreClass = MVT::i64; break;
1660 default: assert(0 && "Unknown store class!");
1661 }
1662
1663 // Spill the integer to memory and reload it from there.
1664 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1665 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1666
1667 switch (StoreClass) {
1668 default: assert(0 && "Unknown store class!");
1669 case MVT::i16:
1670 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1671 break;
1672 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001673 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001674 break;
1675 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001676 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001677 break;
1678 }
1679
1680 switch (Node->getValueType(0)) {
1681 default:
1682 assert(0 && "Unknown integer type!");
1683 case MVT::i64:
1684 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001685 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001686 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1687 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1688 case MVT::i32:
1689 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1690 break;
1691 case MVT::i16:
1692 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1693 break;
1694 case MVT::i8:
1695 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1696 break;
1697 }
1698
1699 // Reload the original control word now.
1700 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1701 return Result;
1702 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001703 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001704 Op0 = N.getOperand(0);
1705 Op1 = N.getOperand(1);
1706
Chris Lattner44129b52005-01-25 20:03:11 +00001707 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001708 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001709 goto FoldAdd;
1710 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001711
Chris Lattner44129b52005-01-25 20:03:11 +00001712 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001713 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001714 switch (N.getValueType()) {
1715 default: assert(0 && "Cannot add this type!");
1716 case MVT::i1:
1717 case MVT::i8: Opc = X86::ADD8rm; break;
1718 case MVT::i16: Opc = X86::ADD16rm; break;
1719 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001720 case MVT::f64:
1721 // For F64, handle promoted load operations (from F32) as well!
1722 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1723 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001724 }
1725 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001726 EmitFoldedLoad(Op1, AM);
1727 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001728 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1729 return Result;
1730 }
1731
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001732 // See if we can codegen this as an LEA to fold operations together.
1733 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001734 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001735 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001736 MatchAddress(N, AM);
1737 ExprMap[N] = Result;
1738
1739 // If this is not just an add, emit the LEA. For a simple add (like
1740 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1741 // leave this as LEA, then peephole it to 'ADD' after two address elim
1742 // happens.
1743 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1744 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1745 X86AddressMode XAM = SelectAddrExprs(AM);
1746 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1747 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001748 }
1749 }
Chris Lattner11333092005-01-11 03:11:44 +00001750
Chris Lattnera5ade062005-01-11 21:19:59 +00001751 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001752 Opc = 0;
1753 if (CN->getValue() == 1) { // add X, 1 -> inc X
1754 switch (N.getValueType()) {
1755 default: assert(0 && "Cannot integer add this type!");
1756 case MVT::i8: Opc = X86::INC8r; break;
1757 case MVT::i16: Opc = X86::INC16r; break;
1758 case MVT::i32: Opc = X86::INC32r; break;
1759 }
1760 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1761 switch (N.getValueType()) {
1762 default: assert(0 && "Cannot integer add this type!");
1763 case MVT::i8: Opc = X86::DEC8r; break;
1764 case MVT::i16: Opc = X86::DEC16r; break;
1765 case MVT::i32: Opc = X86::DEC32r; break;
1766 }
1767 }
1768
1769 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001770 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001771 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1772 return Result;
1773 }
1774
1775 switch (N.getValueType()) {
1776 default: assert(0 && "Cannot add this type!");
1777 case MVT::i8: Opc = X86::ADD8ri; break;
1778 case MVT::i16: Opc = X86::ADD16ri; break;
1779 case MVT::i32: Opc = X86::ADD32ri; break;
1780 }
1781 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001782 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001783 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1784 return Result;
1785 }
1786 }
1787
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001788 switch (N.getValueType()) {
1789 default: assert(0 && "Cannot add this type!");
1790 case MVT::i8: Opc = X86::ADD8rr; break;
1791 case MVT::i16: Opc = X86::ADD16rr; break;
1792 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001793 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001794 }
Chris Lattner11333092005-01-11 03:11:44 +00001795
Chris Lattnera5ade062005-01-11 21:19:59 +00001796 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1797 Tmp1 = SelectExpr(Op0);
1798 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001799 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001800 Tmp2 = SelectExpr(Op1);
1801 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001802 }
1803
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001804 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1805 return Result;
1806 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001807 case ISD::MUL:
1808 case ISD::AND:
1809 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001810 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001811 static const unsigned SUBTab[] = {
1812 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1813 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1814 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1815 };
1816 static const unsigned MULTab[] = {
1817 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1818 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1819 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1820 };
1821 static const unsigned ANDTab[] = {
1822 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1823 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1824 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1825 };
1826 static const unsigned ORTab[] = {
1827 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1828 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1829 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1830 };
1831 static const unsigned XORTab[] = {
1832 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1833 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1834 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1835 };
1836
1837 Op0 = Node->getOperand(0);
1838 Op1 = Node->getOperand(1);
1839
Chris Lattner30ea1e92005-01-19 07:37:26 +00001840 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1841 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001842 return Result;
1843
1844 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001845 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1846 if (CN->isNullValue()) { // 0 - N -> neg N
1847 switch (N.getValueType()) {
1848 default: assert(0 && "Cannot sub this type!");
1849 case MVT::i1:
1850 case MVT::i8: Opc = X86::NEG8r; break;
1851 case MVT::i16: Opc = X86::NEG16r; break;
1852 case MVT::i32: Opc = X86::NEG32r; break;
1853 }
1854 Tmp1 = SelectExpr(N.getOperand(1));
1855 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1856 return Result;
1857 }
1858
Chris Lattnera5ade062005-01-11 21:19:59 +00001859 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1860 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001861 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001862 switch (N.getValueType()) {
1863 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001864 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001865 case MVT::i8: Opc = X86::NOT8r; break;
1866 case MVT::i16: Opc = X86::NOT16r; break;
1867 case MVT::i32: Opc = X86::NOT32r; break;
1868 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001869 if (Opc) {
1870 Tmp1 = SelectExpr(Op0);
1871 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1872 return Result;
1873 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001874 }
1875
Chris Lattner2a4e5082005-01-17 06:48:02 +00001876 // Fold common multiplies into LEA instructions.
1877 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1878 switch ((int)CN->getValue()) {
1879 default: break;
1880 case 3:
1881 case 5:
1882 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001883 // Remove N from exprmap so SelectAddress doesn't get confused.
1884 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001885 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001886 SelectAddress(N, AM);
1887 // Restore it to the map.
1888 ExprMap[N] = Result;
1889 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1890 return Result;
1891 }
1892 }
1893
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001894 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001895 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001896 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001897 case MVT::i8: Opc = 0; break;
1898 case MVT::i16: Opc = 1; break;
1899 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001900 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001901 switch (Node->getOpcode()) {
1902 default: assert(0 && "Unreachable!");
1903 case ISD::SUB: Opc = SUBTab[Opc]; break;
1904 case ISD::MUL: Opc = MULTab[Opc]; break;
1905 case ISD::AND: Opc = ANDTab[Opc]; break;
1906 case ISD::OR: Opc = ORTab[Opc]; break;
1907 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001908 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001909 if (Opc) { // Can't fold MUL:i8 R, imm
1910 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001911 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1912 return Result;
1913 }
1914 }
Chris Lattner11333092005-01-11 03:11:44 +00001915
Chris Lattner44129b52005-01-25 20:03:11 +00001916 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00001917 if (Node->getOpcode() != ISD::SUB) {
1918 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001919 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001920 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00001921 // For FP, emit 'reverse' subract, with a memory operand.
1922 if (N.getValueType() == MVT::f64) {
1923 if (Op0.getOpcode() == ISD::EXTLOAD)
1924 Opc = X86::FSUBR32m;
1925 else
1926 Opc = X86::FSUBR64m;
1927
Chris Lattnera5ade062005-01-11 21:19:59 +00001928 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001929 EmitFoldedLoad(Op0, AM);
1930 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001931 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1932 return Result;
1933 }
1934 }
1935
Chris Lattner44129b52005-01-25 20:03:11 +00001936 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001937 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001938 switch (N.getValueType()) {
1939 default: assert(0 && "Cannot operate on this type!");
1940 case MVT::i1:
1941 case MVT::i8: Opc = 5; break;
1942 case MVT::i16: Opc = 6; break;
1943 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001944 // For F64, handle promoted load operations (from F32) as well!
1945 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001946 }
1947 switch (Node->getOpcode()) {
1948 default: assert(0 && "Unreachable!");
1949 case ISD::SUB: Opc = SUBTab[Opc]; break;
1950 case ISD::MUL: Opc = MULTab[Opc]; break;
1951 case ISD::AND: Opc = ANDTab[Opc]; break;
1952 case ISD::OR: Opc = ORTab[Opc]; break;
1953 case ISD::XOR: Opc = XORTab[Opc]; break;
1954 }
1955
1956 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001957 EmitFoldedLoad(Op1, AM);
1958 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001959 if (Opc) {
1960 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1961 } else {
1962 assert(Node->getOpcode() == ISD::MUL &&
1963 N.getValueType() == MVT::i8 && "Unexpected situation!");
1964 // Must use the MUL instruction, which forces use of AL.
1965 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1966 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1967 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1968 }
1969 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001970 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001971
1972 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1973 Tmp1 = SelectExpr(Op0);
1974 Tmp2 = SelectExpr(Op1);
1975 } else {
1976 Tmp2 = SelectExpr(Op1);
1977 Tmp1 = SelectExpr(Op0);
1978 }
1979
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001980 switch (N.getValueType()) {
1981 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001982 case MVT::i1:
1983 case MVT::i8: Opc = 10; break;
1984 case MVT::i16: Opc = 11; break;
1985 case MVT::i32: Opc = 12; break;
1986 case MVT::f32: Opc = 13; break;
1987 case MVT::f64: Opc = 14; break;
1988 }
1989 switch (Node->getOpcode()) {
1990 default: assert(0 && "Unreachable!");
1991 case ISD::SUB: Opc = SUBTab[Opc]; break;
1992 case ISD::MUL: Opc = MULTab[Opc]; break;
1993 case ISD::AND: Opc = ANDTab[Opc]; break;
1994 case ISD::OR: Opc = ORTab[Opc]; break;
1995 case ISD::XOR: Opc = XORTab[Opc]; break;
1996 }
1997 if (Opc) {
1998 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1999 } else {
2000 assert(Node->getOpcode() == ISD::MUL &&
2001 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002002 // Must use the MUL instruction, which forces use of AL.
2003 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2004 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2005 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002006 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002007 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002008 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002009 case ISD::ADD_PARTS:
2010 case ISD::SUB_PARTS: {
2011 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2012 "Not an i64 add/sub!");
2013 // Emit all of the operands.
2014 std::vector<unsigned> InVals;
2015 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2016 InVals.push_back(SelectExpr(N.getOperand(i)));
2017 if (N.getOpcode() == ISD::ADD_PARTS) {
2018 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2019 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2020 } else {
2021 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2022 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2023 }
2024 return Result+N.ResNo;
2025 }
2026
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002027 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002028 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2029 Tmp2 = SelectExpr(N.getOperand(1));
2030 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002031 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002032 Tmp3 = SelectExpr(N.getOperand(2));
2033 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002034 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002035 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2036 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002037
2038 case ISD::SDIV:
2039 case ISD::UDIV:
2040 case ISD::SREM:
2041 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002042 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2043 "We don't support this operator!");
2044
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002045 if (N.getOpcode() == ISD::SDIV)
Chris Lattner3576c842005-01-25 20:35:10 +00002046
2047 // We can fold loads into FpDIVs, but not really into any others.
2048 if (N.getValueType() == MVT::f64) {
2049 // Check for reversed and unreversed DIV.
2050 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2051 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2052 Opc = X86::FDIVR32m;
2053 else
2054 Opc = X86::FDIVR64m;
2055 X86AddressMode AM;
2056 EmitFoldedLoad(N.getOperand(0), AM);
2057 Tmp1 = SelectExpr(N.getOperand(1));
2058 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2059 return Result;
2060 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2061 N.getOperand(1).getOpcode() == ISD::LOAD) {
2062 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2063 Opc = X86::FDIV32m;
2064 else
2065 Opc = X86::FDIV64m;
2066 X86AddressMode AM;
2067 EmitFoldedLoad(N.getOperand(1), AM);
2068 Tmp1 = SelectExpr(N.getOperand(0));
2069 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2070 return Result;
2071 }
2072 }
2073
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002074 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2075 // FIXME: These special cases should be handled by the lowering impl!
2076 unsigned RHS = CN->getValue();
2077 bool isNeg = false;
2078 if ((int)RHS < 0) {
2079 isNeg = true;
2080 RHS = -RHS;
2081 }
2082 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2083 unsigned Log = log2(RHS);
2084 unsigned TmpReg = MakeReg(N.getValueType());
2085 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2086 switch (N.getValueType()) {
2087 default: assert("Unknown type to signed divide!");
2088 case MVT::i8:
2089 SAROpc = X86::SAR8ri;
2090 SHROpc = X86::SHR8ri;
2091 ADDOpc = X86::ADD8rr;
2092 NEGOpc = X86::NEG8r;
2093 break;
2094 case MVT::i16:
2095 SAROpc = X86::SAR16ri;
2096 SHROpc = X86::SHR16ri;
2097 ADDOpc = X86::ADD16rr;
2098 NEGOpc = X86::NEG16r;
2099 break;
2100 case MVT::i32:
2101 SAROpc = X86::SAR32ri;
2102 SHROpc = X86::SHR32ri;
2103 ADDOpc = X86::ADD32rr;
2104 NEGOpc = X86::NEG32r;
2105 break;
2106 }
Chris Lattner11333092005-01-11 03:11:44 +00002107 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002108 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2109 unsigned TmpReg2 = MakeReg(N.getValueType());
2110 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2111 unsigned TmpReg3 = MakeReg(N.getValueType());
2112 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
2113
2114 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2115 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2116 if (isNeg)
2117 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2118 return Result;
2119 }
2120 }
2121
Chris Lattner11333092005-01-11 03:11:44 +00002122 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2123 Tmp1 = SelectExpr(N.getOperand(0));
2124 Tmp2 = SelectExpr(N.getOperand(1));
2125 } else {
2126 Tmp2 = SelectExpr(N.getOperand(1));
2127 Tmp1 = SelectExpr(N.getOperand(0));
2128 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002129
2130 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2131 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2132 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2133 switch (N.getValueType()) {
2134 default: assert(0 && "Cannot sdiv this type!");
2135 case MVT::i8:
2136 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2137 LoReg = X86::AL;
2138 HiReg = X86::AH;
2139 MovOpcode = X86::MOV8rr;
2140 ClrOpcode = X86::MOV8ri;
2141 SExtOpcode = X86::CBW;
2142 break;
2143 case MVT::i16:
2144 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2145 LoReg = X86::AX;
2146 HiReg = X86::DX;
2147 MovOpcode = X86::MOV16rr;
2148 ClrOpcode = X86::MOV16ri;
2149 SExtOpcode = X86::CWD;
2150 break;
2151 case MVT::i32:
2152 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002153 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002154 HiReg = X86::EDX;
2155 MovOpcode = X86::MOV32rr;
2156 ClrOpcode = X86::MOV32ri;
2157 SExtOpcode = X86::CDQ;
2158 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002159 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002160 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002161 return Result;
2162 }
2163
2164 // Set up the low part.
2165 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2166
2167 if (isSigned) {
2168 // Sign extend the low part into the high part.
2169 BuildMI(BB, SExtOpcode, 0);
2170 } else {
2171 // Zero out the high part, effectively zero extending the input.
2172 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2173 }
2174
2175 // Emit the DIV/IDIV instruction.
2176 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
2177
2178 // Get the result of the divide or rem.
2179 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2180 return Result;
2181 }
2182
2183 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002184 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002185 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2186 switch (N.getValueType()) {
2187 default: assert(0 && "Cannot shift this type!");
2188 case MVT::i8: Opc = X86::ADD8rr; break;
2189 case MVT::i16: Opc = X86::ADD16rr; break;
2190 case MVT::i32: Opc = X86::ADD32rr; break;
2191 }
2192 Tmp1 = SelectExpr(N.getOperand(0));
2193 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2194 return Result;
2195 }
2196
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002197 switch (N.getValueType()) {
2198 default: assert(0 && "Cannot shift this type!");
2199 case MVT::i8: Opc = X86::SHL8ri; break;
2200 case MVT::i16: Opc = X86::SHL16ri; break;
2201 case MVT::i32: Opc = X86::SHL32ri; break;
2202 }
Chris Lattner11333092005-01-11 03:11:44 +00002203 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002204 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2205 return Result;
2206 }
Chris Lattner11333092005-01-11 03:11:44 +00002207
2208 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2209 Tmp1 = SelectExpr(N.getOperand(0));
2210 Tmp2 = SelectExpr(N.getOperand(1));
2211 } else {
2212 Tmp2 = SelectExpr(N.getOperand(1));
2213 Tmp1 = SelectExpr(N.getOperand(0));
2214 }
2215
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002216 switch (N.getValueType()) {
2217 default: assert(0 && "Cannot shift this type!");
2218 case MVT::i8 : Opc = X86::SHL8rCL; break;
2219 case MVT::i16: Opc = X86::SHL16rCL; break;
2220 case MVT::i32: Opc = X86::SHL32rCL; break;
2221 }
2222 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2223 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2224 return Result;
2225 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002226 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2227 switch (N.getValueType()) {
2228 default: assert(0 && "Cannot shift this type!");
2229 case MVT::i8: Opc = X86::SHR8ri; break;
2230 case MVT::i16: Opc = X86::SHR16ri; break;
2231 case MVT::i32: Opc = X86::SHR32ri; break;
2232 }
Chris Lattner11333092005-01-11 03:11:44 +00002233 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002234 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2235 return Result;
2236 }
Chris Lattner11333092005-01-11 03:11:44 +00002237
2238 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2239 Tmp1 = SelectExpr(N.getOperand(0));
2240 Tmp2 = SelectExpr(N.getOperand(1));
2241 } else {
2242 Tmp2 = SelectExpr(N.getOperand(1));
2243 Tmp1 = SelectExpr(N.getOperand(0));
2244 }
2245
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002246 switch (N.getValueType()) {
2247 default: assert(0 && "Cannot shift this type!");
2248 case MVT::i8 : Opc = X86::SHR8rCL; break;
2249 case MVT::i16: Opc = X86::SHR16rCL; break;
2250 case MVT::i32: Opc = X86::SHR32rCL; break;
2251 }
2252 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2253 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2254 return Result;
2255 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002256 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2257 switch (N.getValueType()) {
2258 default: assert(0 && "Cannot shift this type!");
2259 case MVT::i8: Opc = X86::SAR8ri; break;
2260 case MVT::i16: Opc = X86::SAR16ri; break;
2261 case MVT::i32: Opc = X86::SAR32ri; break;
2262 }
Chris Lattner11333092005-01-11 03:11:44 +00002263 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002264 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2265 return Result;
2266 }
Chris Lattner11333092005-01-11 03:11:44 +00002267
2268 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2269 Tmp1 = SelectExpr(N.getOperand(0));
2270 Tmp2 = SelectExpr(N.getOperand(1));
2271 } else {
2272 Tmp2 = SelectExpr(N.getOperand(1));
2273 Tmp1 = SelectExpr(N.getOperand(0));
2274 }
2275
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002276 switch (N.getValueType()) {
2277 default: assert(0 && "Cannot shift this type!");
2278 case MVT::i8 : Opc = X86::SAR8rCL; break;
2279 case MVT::i16: Opc = X86::SAR16rCL; break;
2280 case MVT::i32: Opc = X86::SAR32rCL; break;
2281 }
2282 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2283 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2284 return Result;
2285
2286 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002287 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002288 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2289 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2290 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002291 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002292 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002293 if (Result != 1) { // Generate the token
2294 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2295 assert(0 && "Load already emitted!?");
2296 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002297 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2298
Chris Lattner5188ad72005-01-08 19:28:19 +00002299 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002300 default: assert(0 && "Cannot load this type!");
2301 case MVT::i1:
2302 case MVT::i8: Opc = X86::MOV8rm; break;
2303 case MVT::i16: Opc = X86::MOV16rm; break;
2304 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002305 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2306 }
Chris Lattner11333092005-01-11 03:11:44 +00002307
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002308 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002309 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002310 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2311 } else {
2312 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002313
2314 SDOperand Chain = N.getOperand(0);
2315 SDOperand Address = N.getOperand(1);
2316 if (getRegPressure(Chain) > getRegPressure(Address)) {
2317 Select(Chain);
2318 SelectAddress(Address, AM);
2319 } else {
2320 SelectAddress(Address, AM);
2321 Select(Chain);
2322 }
2323
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002324 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2325 }
2326 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002327
2328 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2329 case ISD::ZEXTLOAD: {
2330 // Make sure we generate both values.
2331 if (Result != 1)
2332 ExprMap[N.getValue(1)] = 1; // Generate the token
2333 else
2334 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2335
Chris Lattnerda2ce112005-01-16 07:34:08 +00002336 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2337 if (Node->getValueType(0) == MVT::f64) {
2338 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2339 "Bad EXTLOAD!");
2340 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2341 CP->getIndex());
2342 return Result;
2343 }
2344
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002345 X86AddressMode AM;
2346 if (getRegPressure(Node->getOperand(0)) >
2347 getRegPressure(Node->getOperand(1))) {
2348 Select(Node->getOperand(0)); // chain
2349 SelectAddress(Node->getOperand(1), AM);
2350 } else {
2351 SelectAddress(Node->getOperand(1), AM);
2352 Select(Node->getOperand(0)); // chain
2353 }
2354
2355 switch (Node->getValueType(0)) {
2356 default: assert(0 && "Unknown type to sign extend to.");
2357 case MVT::f64:
2358 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2359 "Bad EXTLOAD!");
2360 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2361 break;
2362 case MVT::i32:
2363 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2364 default:
2365 assert(0 && "Bad zero extend!");
2366 case MVT::i1:
2367 case MVT::i8:
2368 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2369 break;
2370 case MVT::i16:
2371 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2372 break;
2373 }
2374 break;
2375 case MVT::i16:
2376 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2377 "Bad zero extend!");
2378 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2379 break;
2380 case MVT::i8:
2381 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2382 "Bad zero extend!");
2383 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2384 break;
2385 }
2386 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002387 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002388 case ISD::SEXTLOAD: {
2389 // Make sure we generate both values.
2390 if (Result != 1)
2391 ExprMap[N.getValue(1)] = 1; // Generate the token
2392 else
2393 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2394
2395 X86AddressMode AM;
2396 if (getRegPressure(Node->getOperand(0)) >
2397 getRegPressure(Node->getOperand(1))) {
2398 Select(Node->getOperand(0)); // chain
2399 SelectAddress(Node->getOperand(1), AM);
2400 } else {
2401 SelectAddress(Node->getOperand(1), AM);
2402 Select(Node->getOperand(0)); // chain
2403 }
2404
2405 switch (Node->getValueType(0)) {
2406 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2407 default: assert(0 && "Unknown type to sign extend to.");
2408 case MVT::i32:
2409 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2410 default:
2411 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2412 case MVT::i8:
2413 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2414 break;
2415 case MVT::i16:
2416 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2417 break;
2418 }
2419 break;
2420 case MVT::i16:
2421 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2422 "Cannot sign extend from bool!");
2423 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2424 break;
2425 }
2426 return Result;
2427 }
2428
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002429 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002430 // Generate both result values.
2431 if (Result != 1)
2432 ExprMap[N.getValue(1)] = 1; // Generate the token
2433 else
2434 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2435
2436 // FIXME: We are currently ignoring the requested alignment for handling
2437 // greater than the stack alignment. This will need to be revisited at some
2438 // point. Align = N.getOperand(2);
2439
2440 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2441 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2442 std::cerr << "Cannot allocate stack object with greater alignment than"
2443 << " the stack alignment yet!";
2444 abort();
2445 }
2446
2447 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002448 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002449 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2450 .addImm(CN->getValue());
2451 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002452 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2453 Select(N.getOperand(0));
2454 Tmp1 = SelectExpr(N.getOperand(1));
2455 } else {
2456 Tmp1 = SelectExpr(N.getOperand(1));
2457 Select(N.getOperand(0));
2458 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002459
2460 // Subtract size from stack pointer, thereby allocating some space.
2461 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2462 }
2463
2464 // Put a pointer to the space into the result register, by copying the stack
2465 // pointer.
2466 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2467 return Result;
2468
2469 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002470 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002471 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002472
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002473 if (GlobalAddressSDNode *GASD =
2474 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002475 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002476 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2477 } else if (ExternalSymbolSDNode *ESSDN =
2478 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002479 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002480 BuildMI(BB, X86::CALLpcrel32,
2481 1).addExternalSymbol(ESSDN->getSymbol(), true);
2482 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002483 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2484 Select(N.getOperand(0));
2485 Tmp1 = SelectExpr(N.getOperand(1));
2486 } else {
2487 Tmp1 = SelectExpr(N.getOperand(1));
2488 Select(N.getOperand(0));
2489 }
2490
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002491 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2492 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002493 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002494 default: assert(0 && "Unknown value type for call result!");
2495 case MVT::Other: return 1;
2496 case MVT::i1:
2497 case MVT::i8:
2498 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2499 break;
2500 case MVT::i16:
2501 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2502 break;
2503 case MVT::i32:
2504 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002505 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002506 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2507 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002508 case MVT::f64: // Floating-point return values live in %ST(0)
2509 ContainsFPCode = true;
2510 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2511 break;
2512 }
2513 return Result+N.ResNo;
2514 }
2515
2516 return 0;
2517}
2518
Chris Lattnere10269b2005-01-17 19:25:26 +00002519/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2520/// load/op/store instruction. If successful return true.
2521bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2522 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2523 SDOperand Chain = Node->getOperand(0);
2524 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002525 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002526
2527 // The chain has to be a load, the stored value must be an integer binary
2528 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002529 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002530 MVT::isFloatingPoint(StVal.getValueType()))
2531 return false;
2532
Chris Lattner5c659812005-01-17 22:10:42 +00002533 // Token chain must either be a factor node or the load to fold.
2534 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2535 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002536
Chris Lattner5c659812005-01-17 22:10:42 +00002537 SDOperand TheLoad;
2538
2539 // Check to see if there is a load from the same pointer that we're storing
2540 // to in either operand of the binop.
2541 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2542 StVal.getOperand(0).getOperand(1) == StPtr)
2543 TheLoad = StVal.getOperand(0);
2544 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2545 StVal.getOperand(1).getOperand(1) == StPtr)
2546 TheLoad = StVal.getOperand(1);
2547 else
2548 return false; // No matching load operand.
2549
2550 // We can only fold the load if there are no intervening side-effecting
2551 // operations. This means that the store uses the load as its token chain, or
2552 // there are only token factor nodes in between the store and load.
2553 if (Chain != TheLoad.getValue(1)) {
2554 // Okay, the other option is that we have a store referring to (possibly
2555 // nested) token factor nodes. For now, just try peeking through one level
2556 // of token factors to see if this is the case.
2557 bool ChainOk = false;
2558 if (Chain.getOpcode() == ISD::TokenFactor) {
2559 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2560 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2561 ChainOk = true;
2562 break;
2563 }
2564 }
2565
2566 if (!ChainOk) return false;
2567 }
2568
2569 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002570 return false;
2571
2572 // Make sure that one of the operands of the binop is the load, and that the
2573 // load folds into the binop.
2574 if (((StVal.getOperand(0) != TheLoad ||
2575 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2576 (StVal.getOperand(1) != TheLoad ||
2577 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2578 return false;
2579
2580 // Finally, check to see if this is one of the ops we can handle!
2581 static const unsigned ADDTAB[] = {
2582 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2583 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2584 };
2585 static const unsigned SUBTAB[] = {
2586 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2587 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2588 };
2589 static const unsigned ANDTAB[] = {
2590 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2591 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2592 };
2593 static const unsigned ORTAB[] = {
2594 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2595 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2596 };
2597 static const unsigned XORTAB[] = {
2598 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2599 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2600 };
2601 static const unsigned SHLTAB[] = {
2602 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2603 /*Have to put the reg in CL*/0, 0, 0,
2604 };
2605 static const unsigned SARTAB[] = {
2606 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2607 /*Have to put the reg in CL*/0, 0, 0,
2608 };
2609 static const unsigned SHRTAB[] = {
2610 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2611 /*Have to put the reg in CL*/0, 0, 0,
2612 };
2613
2614 const unsigned *TabPtr = 0;
2615 switch (StVal.getOpcode()) {
2616 default:
2617 std::cerr << "CANNOT [mem] op= val: ";
2618 StVal.Val->dump(); std::cerr << "\n";
2619 case ISD::MUL:
2620 case ISD::SDIV:
2621 case ISD::UDIV:
2622 case ISD::SREM:
2623 case ISD::UREM: return false;
2624
2625 case ISD::ADD: TabPtr = ADDTAB; break;
2626 case ISD::SUB: TabPtr = SUBTAB; break;
2627 case ISD::AND: TabPtr = ANDTAB; break;
2628 case ISD:: OR: TabPtr = ORTAB; break;
2629 case ISD::XOR: TabPtr = XORTAB; break;
2630 case ISD::SHL: TabPtr = SHLTAB; break;
2631 case ISD::SRA: TabPtr = SARTAB; break;
2632 case ISD::SRL: TabPtr = SHRTAB; break;
2633 }
2634
2635 // Handle: [mem] op= CST
2636 SDOperand Op0 = StVal.getOperand(0);
2637 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002638 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002639 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2640 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2641 default: break;
2642 case MVT::i1:
2643 case MVT::i8: Opc = TabPtr[0]; break;
2644 case MVT::i16: Opc = TabPtr[1]; break;
2645 case MVT::i32: Opc = TabPtr[2]; break;
2646 }
2647
2648 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002649 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2650 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002651 Select(Chain);
2652
Chris Lattnere10269b2005-01-17 19:25:26 +00002653 X86AddressMode AM;
2654 if (getRegPressure(TheLoad.getOperand(0)) >
2655 getRegPressure(TheLoad.getOperand(1))) {
2656 Select(TheLoad.getOperand(0));
2657 SelectAddress(TheLoad.getOperand(1), AM);
2658 } else {
2659 SelectAddress(TheLoad.getOperand(1), AM);
2660 Select(TheLoad.getOperand(0));
2661 }
Chris Lattner5c659812005-01-17 22:10:42 +00002662
2663 if (StVal.getOpcode() == ISD::ADD) {
2664 if (CN->getValue() == 1) {
2665 switch (Op0.getValueType()) {
2666 default: break;
2667 case MVT::i8:
2668 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2669 return true;
2670 case MVT::i16: Opc = TabPtr[1];
2671 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2672 return true;
2673 case MVT::i32: Opc = TabPtr[2];
2674 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2675 return true;
2676 }
2677 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2678 switch (Op0.getValueType()) {
2679 default: break;
2680 case MVT::i8:
2681 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2682 return true;
2683 case MVT::i16: Opc = TabPtr[1];
2684 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2685 return true;
2686 case MVT::i32: Opc = TabPtr[2];
2687 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2688 return true;
2689 }
2690 }
2691 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002692
2693 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2694 return true;
2695 }
2696 }
2697
2698 // If we have [mem] = V op [mem], try to turn it into:
2699 // [mem] = [mem] op V.
2700 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2701 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2702 StVal.getOpcode() != ISD::SRL)
2703 std::swap(Op0, Op1);
2704
2705 if (Op0 != TheLoad) return false;
2706
2707 switch (Op0.getValueType()) {
2708 default: return false;
2709 case MVT::i1:
2710 case MVT::i8: Opc = TabPtr[3]; break;
2711 case MVT::i16: Opc = TabPtr[4]; break;
2712 case MVT::i32: Opc = TabPtr[5]; break;
2713 }
Chris Lattner5c659812005-01-17 22:10:42 +00002714
Chris Lattnerb422aea2005-01-18 17:35:28 +00002715 // Table entry doesn't exist?
2716 if (Opc == 0) return false;
2717
Chris Lattner4a108662005-01-18 03:51:59 +00002718 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2719 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002720 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002721 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002722
Chris Lattnere10269b2005-01-17 19:25:26 +00002723 X86AddressMode AM;
2724 SelectAddress(TheLoad.getOperand(1), AM);
2725 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002726 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002727 return true;
2728}
2729
2730
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002731void ISel::Select(SDOperand N) {
2732 unsigned Tmp1, Tmp2, Opc;
2733
Nate Begeman85fdeb22005-03-24 04:39:54 +00002734 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002735 return; // Already selected.
2736
Chris Lattner989de032005-01-11 06:14:36 +00002737 SDNode *Node = N.Val;
2738
2739 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002740 default:
Chris Lattner989de032005-01-11 06:14:36 +00002741 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002742 assert(0 && "Node not handled yet!");
2743 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002744 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002745 if (Node->getNumOperands() == 2) {
2746 bool OneFirst =
2747 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2748 Select(Node->getOperand(OneFirst));
2749 Select(Node->getOperand(!OneFirst));
2750 } else {
2751 std::vector<std::pair<unsigned, unsigned> > OpsP;
2752 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2753 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2754 std::sort(OpsP.begin(), OpsP.end());
2755 std::reverse(OpsP.begin(), OpsP.end());
2756 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2757 Select(Node->getOperand(OpsP[i].second));
2758 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002759 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002760 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002761 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2762 Select(N.getOperand(0));
2763 Tmp1 = SelectExpr(N.getOperand(1));
2764 } else {
2765 Tmp1 = SelectExpr(N.getOperand(1));
2766 Select(N.getOperand(0));
2767 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002768 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002769
2770 if (Tmp1 != Tmp2) {
2771 switch (N.getOperand(1).getValueType()) {
2772 default: assert(0 && "Invalid type for operation!");
2773 case MVT::i1:
2774 case MVT::i8: Opc = X86::MOV8rr; break;
2775 case MVT::i16: Opc = X86::MOV16rr; break;
2776 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002777 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002778 }
2779 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2780 }
2781 return;
2782 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002783 switch (N.getNumOperands()) {
2784 default:
2785 assert(0 && "Unknown return instruction!");
2786 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002787 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2788 N.getOperand(2).getValueType() == MVT::i32 &&
2789 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002790 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2791 Tmp1 = SelectExpr(N.getOperand(1));
2792 Tmp2 = SelectExpr(N.getOperand(2));
2793 } else {
2794 Tmp2 = SelectExpr(N.getOperand(2));
2795 Tmp1 = SelectExpr(N.getOperand(1));
2796 }
2797 Select(N.getOperand(0));
2798
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002799 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2800 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2801 // Declare that EAX & EDX are live on exit.
2802 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2803 .addReg(X86::ESP);
2804 break;
2805 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002806 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2807 Select(N.getOperand(0));
2808 Tmp1 = SelectExpr(N.getOperand(1));
2809 } else {
2810 Tmp1 = SelectExpr(N.getOperand(1));
2811 Select(N.getOperand(0));
2812 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002813 switch (N.getOperand(1).getValueType()) {
2814 default: assert(0 && "All other types should have been promoted!!");
2815 case MVT::f64:
2816 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2817 // Declare that top-of-stack is live on exit
2818 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2819 break;
2820 case MVT::i32:
2821 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2822 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2823 break;
2824 }
2825 break;
2826 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002827 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002828 break;
2829 }
2830 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2831 return;
2832 case ISD::BR: {
2833 Select(N.getOperand(0));
2834 MachineBasicBlock *Dest =
2835 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2836 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2837 return;
2838 }
2839
2840 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002841 MachineBasicBlock *Dest =
2842 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002843
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002844 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2845 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002846 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2847 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2848 Select(N.getOperand(0));
2849 Tmp1 = SelectExpr(N.getOperand(1));
2850 } else {
2851 Tmp1 = SelectExpr(N.getOperand(1));
2852 Select(N.getOperand(0));
2853 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002854 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2855 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2856 }
Chris Lattner11333092005-01-11 03:11:44 +00002857
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002858 return;
2859 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002860
Chris Lattner4df0de92005-01-17 00:00:33 +00002861 case ISD::LOAD:
2862 // If this load could be folded into the only using instruction, and if it
2863 // is safe to emit the instruction here, try to do so now.
2864 if (Node->hasNUsesOfValue(1, 0)) {
2865 SDOperand TheVal = N.getValue(0);
2866 SDNode *User = 0;
2867 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2868 assert(UI != Node->use_end() && "Didn't find use!");
2869 SDNode *UN = *UI;
2870 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2871 if (UN->getOperand(i) == TheVal) {
2872 User = UN;
2873 goto FoundIt;
2874 }
2875 }
2876 FoundIt:
2877 // Only handle unary operators right now.
2878 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00002879 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002880 SelectExpr(SDOperand(User, 0));
2881 return;
2882 }
2883 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002884 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002885 SelectExpr(N);
2886 return;
2887
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002888 case ISD::EXTLOAD:
2889 case ISD::SEXTLOAD:
2890 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002891 case ISD::CALL:
2892 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002893 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002894 SelectExpr(N);
2895 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002896
2897 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2898 // On X86, we can represent all types except for Bool and Float natively.
2899 X86AddressMode AM;
2900 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002901 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2902 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2903 && "Unsupported TRUNCSTORE for this target!");
2904
2905 if (StoredTy == MVT::i16) {
2906 // FIXME: This is here just to allow testing. X86 doesn't really have a
2907 // TRUNCSTORE i16 operation, but this is required for targets that do not
2908 // have 16-bit integer registers. We occasionally disable 16-bit integer
2909 // registers to test the promotion code.
2910 Select(N.getOperand(0));
2911 Tmp1 = SelectExpr(N.getOperand(1));
2912 SelectAddress(N.getOperand(2), AM);
2913
2914 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2915 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2916 return;
2917 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002918
2919 // Store of constant bool?
2920 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2921 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2922 Select(N.getOperand(0));
2923 SelectAddress(N.getOperand(2), AM);
2924 } else {
2925 SelectAddress(N.getOperand(2), AM);
2926 Select(N.getOperand(0));
2927 }
2928 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2929 return;
2930 }
2931
2932 switch (StoredTy) {
2933 default: assert(0 && "Cannot truncstore this type!");
2934 case MVT::i1: Opc = X86::MOV8mr; break;
2935 case MVT::f32: Opc = X86::FST32m; break;
2936 }
2937
2938 std::vector<std::pair<unsigned, unsigned> > RP;
2939 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2940 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2941 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2942 std::sort(RP.begin(), RP.end());
2943
Chris Lattner572dd082005-02-23 05:57:21 +00002944 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002945 for (unsigned i = 0; i != 3; ++i)
2946 switch (RP[2-i].second) {
2947 default: assert(0 && "Unknown operand number!");
2948 case 0: Select(N.getOperand(0)); break;
2949 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2950 case 2: SelectAddress(N.getOperand(2), AM); break;
2951 }
2952
2953 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2954 return;
2955 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002956 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002957 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002958
2959 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2960 Opc = 0;
2961 switch (CN->getValueType(0)) {
2962 default: assert(0 && "Invalid type for operation!");
2963 case MVT::i1:
2964 case MVT::i8: Opc = X86::MOV8mi; break;
2965 case MVT::i16: Opc = X86::MOV16mi; break;
2966 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002967 case MVT::f64: break;
2968 }
2969 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002970 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2971 Select(N.getOperand(0));
2972 SelectAddress(N.getOperand(2), AM);
2973 } else {
2974 SelectAddress(N.getOperand(2), AM);
2975 Select(N.getOperand(0));
2976 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002977 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2978 return;
2979 }
2980 }
Chris Lattner837caa72005-01-11 23:21:30 +00002981
2982 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00002983 if (TryToFoldLoadOpStore(Node))
2984 return;
Chris Lattner837caa72005-01-11 23:21:30 +00002985
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002986 switch (N.getOperand(1).getValueType()) {
2987 default: assert(0 && "Cannot store this type!");
2988 case MVT::i1:
2989 case MVT::i8: Opc = X86::MOV8mr; break;
2990 case MVT::i16: Opc = X86::MOV16mr; break;
2991 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002992 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002993 }
Chris Lattner11333092005-01-11 03:11:44 +00002994
2995 std::vector<std::pair<unsigned, unsigned> > RP;
2996 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2997 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2998 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2999 std::sort(RP.begin(), RP.end());
3000
Chris Lattner572dd082005-02-23 05:57:21 +00003001 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003002 for (unsigned i = 0; i != 3; ++i)
3003 switch (RP[2-i].second) {
3004 default: assert(0 && "Unknown operand number!");
3005 case 0: Select(N.getOperand(0)); break;
3006 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003007 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003008 }
3009
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003010 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3011 return;
3012 }
3013 case ISD::ADJCALLSTACKDOWN:
3014 case ISD::ADJCALLSTACKUP:
3015 Select(N.getOperand(0));
3016 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
3017
3018 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3019 X86::ADJCALLSTACKUP;
3020 BuildMI(BB, Opc, 1).addImm(Tmp1);
3021 return;
Chris Lattner989de032005-01-11 06:14:36 +00003022 case ISD::MEMSET: {
3023 Select(N.getOperand(0)); // Select the chain.
3024 unsigned Align =
3025 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3026 if (Align == 0) Align = 1;
3027
3028 // Turn the byte code into # iterations
3029 unsigned CountReg;
3030 unsigned Opcode;
3031 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3032 unsigned Val = ValC->getValue() & 255;
3033
3034 // If the value is a constant, then we can potentially use larger sets.
3035 switch (Align & 3) {
3036 case 2: // WORD aligned
3037 CountReg = MakeReg(MVT::i32);
3038 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3039 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3040 } else {
3041 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3042 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3043 }
3044 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3045 Opcode = X86::REP_STOSW;
3046 break;
3047 case 0: // DWORD aligned
3048 CountReg = MakeReg(MVT::i32);
3049 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3050 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3051 } else {
3052 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3053 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3054 }
3055 Val = (Val << 8) | Val;
3056 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3057 Opcode = X86::REP_STOSD;
3058 break;
3059 default: // BYTE aligned
3060 CountReg = SelectExpr(Node->getOperand(3));
3061 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3062 Opcode = X86::REP_STOSB;
3063 break;
3064 }
3065 } else {
3066 // If it's not a constant value we are storing, just fall back. We could
3067 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3068 unsigned ValReg = SelectExpr(Node->getOperand(2));
3069 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3070 CountReg = SelectExpr(Node->getOperand(3));
3071 Opcode = X86::REP_STOSB;
3072 }
3073
3074 // No matter what the alignment is, we put the source in ESI, the
3075 // destination in EDI, and the count in ECX.
3076 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3077 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3078 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3079 BuildMI(BB, Opcode, 0);
3080 return;
3081 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003082 case ISD::MEMCPY:
3083 Select(N.getOperand(0)); // Select the chain.
3084 unsigned Align =
3085 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3086 if (Align == 0) Align = 1;
3087
3088 // Turn the byte code into # iterations
3089 unsigned CountReg;
3090 unsigned Opcode;
3091 switch (Align & 3) {
3092 case 2: // WORD aligned
3093 CountReg = MakeReg(MVT::i32);
3094 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3095 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3096 } else {
3097 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3098 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3099 }
3100 Opcode = X86::REP_MOVSW;
3101 break;
3102 case 0: // DWORD aligned
3103 CountReg = MakeReg(MVT::i32);
3104 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3105 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3106 } else {
3107 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3108 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3109 }
3110 Opcode = X86::REP_MOVSD;
3111 break;
3112 default: // BYTE aligned
3113 CountReg = SelectExpr(Node->getOperand(3));
3114 Opcode = X86::REP_MOVSB;
3115 break;
3116 }
3117
3118 // No matter what the alignment is, we put the source in ESI, the
3119 // destination in EDI, and the count in ECX.
3120 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3121 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3122 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3123 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3124 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3125 BuildMI(BB, Opcode, 0);
3126 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003127 }
3128 assert(0 && "Should not be reached!");
3129}
3130
3131
3132/// createX86PatternInstructionSelector - This pass converts an LLVM function
3133/// into a machine code representation using pattern matching and a machine
3134/// description file.
3135///
3136FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
3137 return new ISel(TM);
3138}