blob: 8f118f2f39b472038b7c41c9436015f87e1a862e [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000030#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// X86TargetLowering - X86 Implementation of the TargetLowering interface
35namespace {
36 class X86TargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000038 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000039 public:
40 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000042
43 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
44 setShiftAmountType(MVT::i8);
45 setSetCCResultType(MVT::i8);
Chris Lattner009b55b2005-01-19 03:36:30 +000046 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000047
48 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000049 addRegisterClass(MVT::i8, X86::R8RegisterClass);
50 addRegisterClass(MVT::i16, X86::R16RegisterClass);
51 addRegisterClass(MVT::i32, X86::R32RegisterClass);
52 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
53
54 // FIXME: Eliminate these two classes when legalize can handle promotions
55 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000056/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000057
58 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
59 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
60 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i16 , Expand);
61 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
62 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i1 , Expand);
63 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
64 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
65 setOperationAction(ISD::SREM , MVT::f64 , Expand);
66
67 // These should be promoted to a larger select which is supported.
68/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
69 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000070
71 computeRegisterProperties();
Chris Lattner8acb1ba2005-01-07 07:49:41 +000072
73 addLegalFPImmediate(+0.0); // FLD0
74 addLegalFPImmediate(+1.0); // FLD1
75 addLegalFPImmediate(-0.0); // FLD0/FCHS
76 addLegalFPImmediate(-1.0); // FLD1/FCHS
77 }
78
79 /// LowerArguments - This hook must be implemented to indicate how we should
80 /// lower the arguments for the specified function, into the specified DAG.
81 virtual std::vector<SDOperand>
82 LowerArguments(Function &F, SelectionDAG &DAG);
83
84 /// LowerCallTo - This hook lowers an abstract call to a function into an
85 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000086 virtual std::pair<SDOperand, SDOperand>
87 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
88 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000089
90 virtual std::pair<SDOperand, SDOperand>
91 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
92
93 virtual std::pair<SDOperand,SDOperand>
94 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
95 const Type *ArgTy, SelectionDAG &DAG);
96
97 virtual std::pair<SDOperand, SDOperand>
98 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
99 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000100 };
101}
102
103
104std::vector<SDOperand>
105X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
106 std::vector<SDOperand> ArgValues;
107
108 // Add DAG nodes to load the arguments... On entry to a function on the X86,
109 // the stack frame looks like this:
110 //
111 // [ESP] -- return address
112 // [ESP + 4] -- first argument (leftmost lexically)
113 // [ESP + 8] -- second argument, if first argument is four bytes in size
114 // ...
115 //
116 MachineFunction &MF = DAG.getMachineFunction();
117 MachineFrameInfo *MFI = MF.getFrameInfo();
118
119 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
120 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
121 MVT::ValueType ObjectVT = getValueType(I->getType());
122 unsigned ArgIncrement = 4;
123 unsigned ObjSize;
124 switch (ObjectVT) {
125 default: assert(0 && "Unhandled argument type!");
126 case MVT::i1:
127 case MVT::i8: ObjSize = 1; break;
128 case MVT::i16: ObjSize = 2; break;
129 case MVT::i32: ObjSize = 4; break;
130 case MVT::i64: ObjSize = ArgIncrement = 8; break;
131 case MVT::f32: ObjSize = 4; break;
132 case MVT::f64: ObjSize = ArgIncrement = 8; break;
133 }
134 // Create the frame index object for this incoming parameter...
135 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
136
137 // Create the SelectionDAG nodes corresponding to a load from this parameter
138 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
139
140 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
141 // dead loads.
142 SDOperand ArgValue;
143 if (!I->use_empty())
144 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
145 else {
146 if (MVT::isInteger(ObjectVT))
147 ArgValue = DAG.getConstant(0, ObjectVT);
148 else
149 ArgValue = DAG.getConstantFP(0, ObjectVT);
150 }
151 ArgValues.push_back(ArgValue);
152
153 ArgOffset += ArgIncrement; // Move on to the next argument...
154 }
155
156 // If the function takes variable number of arguments, make a frame index for
157 // the start of the first vararg value... for expansion of llvm.va_start.
158 if (F.isVarArg())
159 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000160 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000161 return ArgValues;
162}
163
Chris Lattner5188ad72005-01-08 19:28:19 +0000164std::pair<SDOperand, SDOperand>
165X86TargetLowering::LowerCallTo(SDOperand Chain,
166 const Type *RetTy, SDOperand Callee,
167 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000168 // Count how many bytes are to be pushed on the stack.
169 unsigned NumBytes = 0;
170
171 if (Args.empty()) {
172 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000173 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
174 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000175 } else {
176 for (unsigned i = 0, e = Args.size(); i != e; ++i)
177 switch (getValueType(Args[i].second)) {
178 default: assert(0 && "Unknown value type!");
179 case MVT::i1:
180 case MVT::i8:
181 case MVT::i16:
182 case MVT::i32:
183 case MVT::f32:
184 NumBytes += 4;
185 break;
186 case MVT::i64:
187 case MVT::f64:
188 NumBytes += 8;
189 break;
190 }
191
Chris Lattner5188ad72005-01-08 19:28:19 +0000192 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
193 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000194
195 // Arguments go on the stack in reverse order, as specified by the ABI.
196 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000197 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
198 DAG.getEntryNode());
Chris 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 Lattner11333092005-01-11 03:11:44 +0000361 unsigned getRegPressure(SDOperand O) {
362 return RegPressureMap[O.Val];
363 }
364 unsigned ComputeRegPressure(SDOperand O);
365
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000366 /// InstructionSelectBasicBlock - This callback is invoked by
367 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000368 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000369
Chris Lattner4ff348b2005-01-17 06:26:58 +0000370 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp);
Chris Lattnera5ade062005-01-11 21:19:59 +0000371 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000372 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000373
Chris Lattner30ea1e92005-01-19 07:37:26 +0000374 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000375 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000376 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000377 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
378 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000379 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000380
381 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
382 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
383 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000384 void Select(SDOperand N);
385 };
386}
387
Chris Lattner7dbcb752005-01-12 04:21:28 +0000388/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
389/// when it has created a SelectionDAG for us to codegen.
390void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
391 // While we're doing this, keep track of whether we see any FP code for
392 // FP_REG_KILL insertion.
393 ContainsFPCode = false;
394
395 // Scan the PHI nodes that already are inserted into this basic block. If any
396 // of them is a PHI of a floating point value, we need to insert an
397 // FP_REG_KILL.
398 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
399 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
400 I != E; ++I) {
401 assert(I->getOpcode() == X86::PHI &&
402 "Isn't just PHI nodes?");
403 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
404 X86::RFPRegisterClass) {
405 ContainsFPCode = true;
406 break;
407 }
408 }
409
410 // Compute the RegPressureMap, which is an approximation for the number of
411 // registers required to compute each node.
412 ComputeRegPressure(DAG.getRoot());
413
414 // Codegen the basic block.
415 Select(DAG.getRoot());
416
417 // Finally, look at all of the successors of this block. If any contain a PHI
418 // node of FP type, we need to insert an FP_REG_KILL in this block.
419 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
420 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
421 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
422 I != E && I->getOpcode() == X86::PHI; ++I) {
423 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
424 X86::RFPRegisterClass) {
425 ContainsFPCode = true;
426 break;
427 }
428 }
429
430 // Insert FP_REG_KILL instructions into basic blocks that need them. This
431 // only occurs due to the floating point stackifier not being aggressive
432 // enough to handle arbitrary global stackification.
433 //
434 // Currently we insert an FP_REG_KILL instruction into each block that uses or
435 // defines a floating point virtual register.
436 //
437 // When the global register allocators (like linear scan) finally update live
438 // variable analysis, we can keep floating point values in registers across
439 // basic blocks. This will be a huge win, but we are waiting on the global
440 // allocators before we can do this.
441 //
442 if (ContainsFPCode && BB->succ_size()) {
443 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
444 ++NumFPKill;
445 }
446
447 // Clear state used for selection.
448 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000449 RegPressureMap.clear();
450}
451
452
Chris Lattner11333092005-01-11 03:11:44 +0000453// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
454// for the number of registers required to compute each node. This is basically
455// computing a generalized form of the Sethi-Ullman number for each node.
456unsigned ISel::ComputeRegPressure(SDOperand O) {
457 SDNode *N = O.Val;
458 unsigned &Result = RegPressureMap[N];
459 if (Result) return Result;
460
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000461 // FIXME: Should operations like CALL (which clobber lots o regs) have a
462 // higher fixed cost??
463
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000464 if (N->getNumOperands() == 0) {
465 Result = 1;
466 } else {
467 unsigned MaxRegUse = 0;
468 unsigned NumExtraMaxRegUsers = 0;
469 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
470 unsigned Regs;
471 if (N->getOperand(i).getOpcode() == ISD::Constant)
472 Regs = 0;
473 else
474 Regs = ComputeRegPressure(N->getOperand(i));
475 if (Regs > MaxRegUse) {
476 MaxRegUse = Regs;
477 NumExtraMaxRegUsers = 0;
478 } else if (Regs == MaxRegUse &&
479 N->getOperand(i).getValueType() != MVT::Other) {
480 ++NumExtraMaxRegUsers;
481 }
Chris Lattner11333092005-01-11 03:11:44 +0000482 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000483
484 if (O.getOpcode() != ISD::TokenFactor)
485 Result = MaxRegUse+NumExtraMaxRegUsers;
486 else
Chris Lattner869e0432005-01-17 23:02:13 +0000487 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000488 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000489
Chris Lattner837caa72005-01-11 23:21:30 +0000490 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000491 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000492}
493
Chris Lattnerbf52d492005-01-20 16:50:16 +0000494/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
495/// The DAG cannot have cycles in it, by definition, so the visited set is not
496/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
497/// reuse, so it prevents exponential cases.
498///
499static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
500 std::set<SDNode*> &Visited) {
501 if (N == Op) return true; // Found it.
502 SDNode *Node = N.Val;
503 if (Node->getNumOperands() == 0) return false; // Leaf?
504 if (!Visited.insert(Node).second) return false; // Already visited?
505
506 // Recurse for the first N-1 operands.
507 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
508 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
509 return true;
510
511 // Tail recurse for the last operand.
512 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
513}
514
Chris Lattner98a8ba02005-01-18 01:06:26 +0000515X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
516 X86AddressMode Result;
517
518 // If we need to emit two register operands, emit the one with the highest
519 // register pressure first.
520 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
521 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000522 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000523 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000524 std::set<SDNode*> Visited;
525 EmitBaseThenIndex = true;
526 // If Base ends up pointing to Index, we must emit index first. This is
527 // because of the way we fold loads, we may end up doing bad things with
528 // the folded add.
529 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
530 EmitBaseThenIndex = false;
531 } else {
532 std::set<SDNode*> Visited;
533 EmitBaseThenIndex = false;
534 // If Base ends up pointing to Index, we must emit index first. This is
535 // because of the way we fold loads, we may end up doing bad things with
536 // the folded add.
537 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
538 EmitBaseThenIndex = true;
539 }
540
541 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000542 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
543 Result.IndexReg = SelectExpr(IAM.IndexReg);
544 } else {
545 Result.IndexReg = SelectExpr(IAM.IndexReg);
546 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
547 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000548
Chris Lattner98a8ba02005-01-18 01:06:26 +0000549 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
550 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
551 } else if (IAM.IndexReg.Val) {
552 Result.IndexReg = SelectExpr(IAM.IndexReg);
553 }
554
555 switch (IAM.BaseType) {
556 case X86ISelAddressMode::RegBase:
557 Result.BaseType = X86AddressMode::RegBase;
558 break;
559 case X86ISelAddressMode::FrameIndexBase:
560 Result.BaseType = X86AddressMode::FrameIndexBase;
561 Result.Base.FrameIndex = IAM.Base.FrameIndex;
562 break;
563 default:
564 assert(0 && "Unknown base type!");
565 break;
566 }
567 Result.Scale = IAM.Scale;
568 Result.Disp = IAM.Disp;
569 Result.GV = IAM.GV;
570 return Result;
571}
572
573/// SelectAddress - Pattern match the maximal addressing mode for this node and
574/// emit all of the leaf registers.
575void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
576 X86ISelAddressMode IAM;
577 MatchAddress(N, IAM);
578 AM = SelectAddrExprs(IAM);
579}
580
581/// MatchAddress - Add the specified node to the specified addressing mode,
582/// returning true if it cannot be done. This just pattern matches for the
583/// addressing mode, it does not cause any code to be emitted. For that, use
584/// SelectAddress.
585bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000586 switch (N.getOpcode()) {
587 default: break;
588 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000589 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
590 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000591 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
592 return false;
593 }
594 break;
595 case ISD::GlobalAddress:
596 if (AM.GV == 0) {
597 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
598 return false;
599 }
600 break;
601 case ISD::Constant:
602 AM.Disp += cast<ConstantSDNode>(N)->getValue();
603 return false;
604 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000605 // We might have folded the load into this shift, so don't regen the value
606 // if so.
607 if (ExprMap.count(N)) break;
608
Chris Lattner98a8ba02005-01-18 01:06:26 +0000609 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000610 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
611 unsigned Val = CN->getValue();
612 if (Val == 1 || Val == 2 || Val == 3) {
613 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000614 SDOperand ShVal = N.Val->getOperand(0);
615
616 // Okay, we know that we have a scale by now. However, if the scaled
617 // value is an add of something and a constant, we can fold the
618 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000619 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000620 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000621 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000622 ConstantSDNode *AddVal =
623 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
624 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000625 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000626 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000627 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000628 return false;
629 }
630 }
631 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000632 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000633 // We might have folded the load into this mul, so don't regen the value if
634 // so.
635 if (ExprMap.count(N)) break;
636
Chris Lattner947d5442005-01-11 19:37:02 +0000637 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000638 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
639 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000640 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
641 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
642 AM.Scale = unsigned(CN->getValue())-1;
643
644 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000645 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000646
647 // Okay, we know that we have a scale by now. However, if the scaled
648 // value is an add of something and a constant, we can fold the
649 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000650 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000651 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000652 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000653 ConstantSDNode *AddVal =
654 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
655 AM.Disp += AddVal->getValue() * CN->getValue();
656 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000657 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000658 }
659
660 AM.IndexReg = AM.Base.Reg = Reg;
661 return false;
662 }
663 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000664
665 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000666 // We might have folded the load into this mul, so don't regen the value if
667 // so.
668 if (ExprMap.count(N)) break;
669
Chris Lattner98a8ba02005-01-18 01:06:26 +0000670 X86ISelAddressMode Backup = AM;
671 if (!MatchAddress(N.Val->getOperand(0), AM) &&
672 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000673 return false;
674 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000675 if (!MatchAddress(N.Val->getOperand(1), AM) &&
676 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000677 return false;
678 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000679 break;
680 }
681 }
682
Chris Lattnera95589b2005-01-11 04:40:19 +0000683 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000684 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000685 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000686 if (AM.IndexReg.Val == 0) {
687 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000688 AM.Scale = 1;
689 return false;
690 }
691
692 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000693 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000694 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000695
696 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000697 AM.BaseType = X86ISelAddressMode::RegBase;
698 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000699 return false;
700}
701
702/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
703/// assuming that the temporary registers are in the 8-bit register class.
704///
705/// Tmp1 = setcc1
706/// Tmp2 = setcc2
707/// DestReg = logicalop Tmp1, Tmp2
708///
709static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
710 unsigned SetCC2, unsigned LogicalOp,
711 unsigned DestReg) {
712 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
713 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
714 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
715 BuildMI(BB, SetCC1, 0, Tmp1);
716 BuildMI(BB, SetCC2, 0, Tmp2);
717 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
718}
719
720/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
721/// condition codes match the specified SetCCOpcode. Note that some conditions
722/// require multiple instructions to generate the correct value.
723static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
724 ISD::CondCode SetCCOpcode, bool isFP) {
725 unsigned Opc;
726 if (!isFP) {
727 switch (SetCCOpcode) {
728 default: assert(0 && "Illegal integer SetCC!");
729 case ISD::SETEQ: Opc = X86::SETEr; break;
730 case ISD::SETGT: Opc = X86::SETGr; break;
731 case ISD::SETGE: Opc = X86::SETGEr; break;
732 case ISD::SETLT: Opc = X86::SETLr; break;
733 case ISD::SETLE: Opc = X86::SETLEr; break;
734 case ISD::SETNE: Opc = X86::SETNEr; break;
735 case ISD::SETULT: Opc = X86::SETBr; break;
736 case ISD::SETUGT: Opc = X86::SETAr; break;
737 case ISD::SETULE: Opc = X86::SETBEr; break;
738 case ISD::SETUGE: Opc = X86::SETAEr; break;
739 }
740 } else {
741 // On a floating point condition, the flags are set as follows:
742 // ZF PF CF op
743 // 0 | 0 | 0 | X > Y
744 // 0 | 0 | 1 | X < Y
745 // 1 | 0 | 0 | X == Y
746 // 1 | 1 | 1 | unordered
747 //
748 switch (SetCCOpcode) {
749 default: assert(0 && "Invalid FP setcc!");
750 case ISD::SETUEQ:
751 case ISD::SETEQ:
752 Opc = X86::SETEr; // True if ZF = 1
753 break;
754 case ISD::SETOGT:
755 case ISD::SETGT:
756 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
757 break;
758 case ISD::SETOGE:
759 case ISD::SETGE:
760 Opc = X86::SETAEr; // True if CF = 0
761 break;
762 case ISD::SETULT:
763 case ISD::SETLT:
764 Opc = X86::SETBr; // True if CF = 1
765 break;
766 case ISD::SETULE:
767 case ISD::SETLE:
768 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
769 break;
770 case ISD::SETONE:
771 case ISD::SETNE:
772 Opc = X86::SETNEr; // True if ZF = 0
773 break;
774 case ISD::SETUO:
775 Opc = X86::SETPr; // True if PF = 1
776 break;
777 case ISD::SETO:
778 Opc = X86::SETNPr; // True if PF = 0
779 break;
780 case ISD::SETOEQ: // !PF & ZF
781 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
782 return;
783 case ISD::SETOLT: // !PF & CF
784 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
785 return;
786 case ISD::SETOLE: // !PF & (CF || ZF)
787 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
788 return;
789 case ISD::SETUGT: // PF | (!ZF & !CF)
790 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
791 return;
792 case ISD::SETUGE: // PF | !CF
793 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
794 return;
795 case ISD::SETUNE: // PF | !ZF
796 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
797 return;
798 }
799 }
800 BuildMI(BB, Opc, 0, DestReg);
801}
802
803
804/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
805/// the Dest block if the Cond condition is true. If we cannot fold this
806/// condition into the branch, return true.
807///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000808bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
809 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000810 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
811 // B) using two conditional branches instead of one condbr, two setcc's, and
812 // an or.
813 if ((Cond.getOpcode() == ISD::OR ||
814 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
815 // And and or set the flags for us, so there is no need to emit a TST of the
816 // result. It is only safe to do this if there is only a single use of the
817 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000818 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000819 SelectExpr(Cond);
820 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
821 return false;
822 }
823
824 // Codegen br not C -> JE.
825 if (Cond.getOpcode() == ISD::XOR)
826 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
827 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000828 unsigned CondR;
829 if (getRegPressure(Chain) > getRegPressure(Cond)) {
830 Select(Chain);
831 CondR = SelectExpr(Cond.Val->getOperand(0));
832 } else {
833 CondR = SelectExpr(Cond.Val->getOperand(0));
834 Select(Chain);
835 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000836 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
837 BuildMI(BB, X86::JE, 1).addMBB(Dest);
838 return false;
839 }
840
841 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
842 if (SetCC == 0)
843 return true; // Can only handle simple setcc's so far.
844
845 unsigned Opc;
846
847 // Handle integer conditions first.
848 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
849 switch (SetCC->getCondition()) {
850 default: assert(0 && "Illegal integer SetCC!");
851 case ISD::SETEQ: Opc = X86::JE; break;
852 case ISD::SETGT: Opc = X86::JG; break;
853 case ISD::SETGE: Opc = X86::JGE; break;
854 case ISD::SETLT: Opc = X86::JL; break;
855 case ISD::SETLE: Opc = X86::JLE; break;
856 case ISD::SETNE: Opc = X86::JNE; break;
857 case ISD::SETULT: Opc = X86::JB; break;
858 case ISD::SETUGT: Opc = X86::JA; break;
859 case ISD::SETULE: Opc = X86::JBE; break;
860 case ISD::SETUGE: Opc = X86::JAE; break;
861 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000862 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000863 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000864 BuildMI(BB, Opc, 1).addMBB(Dest);
865 return false;
866 }
867
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000868 unsigned Opc2 = 0; // Second branch if needed.
869
870 // On a floating point condition, the flags are set as follows:
871 // ZF PF CF op
872 // 0 | 0 | 0 | X > Y
873 // 0 | 0 | 1 | X < Y
874 // 1 | 0 | 0 | X == Y
875 // 1 | 1 | 1 | unordered
876 //
877 switch (SetCC->getCondition()) {
878 default: assert(0 && "Invalid FP setcc!");
879 case ISD::SETUEQ:
880 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
881 case ISD::SETOGT:
882 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
883 case ISD::SETOGE:
884 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
885 case ISD::SETULT:
886 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
887 case ISD::SETULE:
888 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
889 case ISD::SETONE:
890 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
891 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
892 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
893 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
894 Opc = X86::JA; // ZF = 0 & CF = 0
895 Opc2 = X86::JP; // PF = 1
896 break;
897 case ISD::SETUGE: // PF = 1 | CF = 0
898 Opc = X86::JAE; // CF = 0
899 Opc2 = X86::JP; // PF = 1
900 break;
901 case ISD::SETUNE: // PF = 1 | ZF = 0
902 Opc = X86::JNE; // ZF = 0
903 Opc2 = X86::JP; // PF = 1
904 break;
905 case ISD::SETOEQ: // PF = 0 & ZF = 1
906 //X86::JNP, X86::JE
907 //X86::AND8rr
908 return true; // FIXME: Emit more efficient code for this branch.
909 case ISD::SETOLT: // PF = 0 & CF = 1
910 //X86::JNP, X86::JB
911 //X86::AND8rr
912 return true; // FIXME: Emit more efficient code for this branch.
913 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
914 //X86::JNP, X86::JBE
915 //X86::AND8rr
916 return true; // FIXME: Emit more efficient code for this branch.
917 }
918
Chris Lattner6c07aee2005-01-11 04:06:27 +0000919 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000920 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000921 BuildMI(BB, Opc, 1).addMBB(Dest);
922 if (Opc2)
923 BuildMI(BB, Opc2, 1).addMBB(Dest);
924 return false;
925}
926
Chris Lattner24aad1b2005-01-10 22:10:13 +0000927/// EmitSelectCC - Emit code into BB that performs a select operation between
928/// the two registers RTrue and RFalse, generating a result into RDest. Return
929/// true if the fold cannot be performed.
930///
931void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
932 unsigned RTrue, unsigned RFalse, unsigned RDest) {
933 enum Condition {
934 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
935 NOT_SET
936 } CondCode = NOT_SET;
937
938 static const unsigned CMOVTAB16[] = {
939 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
940 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
941 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
942 };
943 static const unsigned CMOVTAB32[] = {
944 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
945 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
946 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
947 };
948 static const unsigned CMOVTABFP[] = {
949 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
950 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
951 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
952 };
953
954 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
955 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
956 switch (SetCC->getCondition()) {
957 default: assert(0 && "Unknown integer comparison!");
958 case ISD::SETEQ: CondCode = EQ; break;
959 case ISD::SETGT: CondCode = GT; break;
960 case ISD::SETGE: CondCode = GE; break;
961 case ISD::SETLT: CondCode = LT; break;
962 case ISD::SETLE: CondCode = LE; break;
963 case ISD::SETNE: CondCode = NE; break;
964 case ISD::SETULT: CondCode = B; break;
965 case ISD::SETUGT: CondCode = A; break;
966 case ISD::SETULE: CondCode = BE; break;
967 case ISD::SETUGE: CondCode = AE; break;
968 }
969 } else {
970 // On a floating point condition, the flags are set as follows:
971 // ZF PF CF op
972 // 0 | 0 | 0 | X > Y
973 // 0 | 0 | 1 | X < Y
974 // 1 | 0 | 0 | X == Y
975 // 1 | 1 | 1 | unordered
976 //
977 switch (SetCC->getCondition()) {
978 default: assert(0 && "Unknown FP comparison!");
979 case ISD::SETUEQ:
980 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
981 case ISD::SETOGT:
982 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
983 case ISD::SETOGE:
984 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
985 case ISD::SETULT:
986 case ISD::SETLT: CondCode = B; break; // True if CF = 1
987 case ISD::SETULE:
988 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
989 case ISD::SETONE:
990 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
991 case ISD::SETUO: CondCode = P; break; // True if PF = 1
992 case ISD::SETO: CondCode = NP; break; // True if PF = 0
993 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
994 case ISD::SETUGE: // PF = 1 | CF = 0
995 case ISD::SETUNE: // PF = 1 | ZF = 0
996 case ISD::SETOEQ: // PF = 0 & ZF = 1
997 case ISD::SETOLT: // PF = 0 & CF = 1
998 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
999 // We cannot emit this comparison as a single cmov.
1000 break;
1001 }
1002 }
1003 }
1004
1005 unsigned Opc = 0;
1006 if (CondCode != NOT_SET) {
1007 switch (SVT) {
1008 default: assert(0 && "Cannot select this type!");
1009 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1010 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001011 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001012 }
1013 }
1014
1015 // Finally, if we weren't able to fold this, just emit the condition and test
1016 // it.
1017 if (CondCode == NOT_SET || Opc == 0) {
1018 // Get the condition into the zero flag.
1019 unsigned CondReg = SelectExpr(Cond);
1020 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1021
1022 switch (SVT) {
1023 default: assert(0 && "Cannot select this type!");
1024 case MVT::i16: Opc = X86::CMOVE16rr; break;
1025 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001026 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001027 }
1028 } else {
1029 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001030 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001031 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001032 }
1033 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1034}
1035
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001036void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001037 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001038 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1039 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001040 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001041 switch (RHS.getValueType()) {
1042 default: break;
1043 case MVT::i1:
1044 case MVT::i8: Opc = X86::CMP8mi; break;
1045 case MVT::i16: Opc = X86::CMP16mi; break;
1046 case MVT::i32: Opc = X86::CMP32mi; break;
1047 }
1048 if (Opc) {
1049 X86AddressMode AM;
1050 EmitFoldedLoad(LHS, AM);
1051 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1052 return;
1053 }
1054 }
1055
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001056 switch (RHS.getValueType()) {
1057 default: break;
1058 case MVT::i1:
1059 case MVT::i8: Opc = X86::CMP8ri; break;
1060 case MVT::i16: Opc = X86::CMP16ri; break;
1061 case MVT::i32: Opc = X86::CMP32ri; break;
1062 }
1063 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001064 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001065 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1066 return;
1067 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001068 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1069 if (CN->isExactlyValue(+0.0) ||
1070 CN->isExactlyValue(-0.0)) {
1071 unsigned Reg = SelectExpr(LHS);
1072 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1073 BuildMI(BB, X86::FNSTSW8r, 0);
1074 BuildMI(BB, X86::SAHF, 1);
1075 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001076 }
1077
Chris Lattneref6806c2005-01-12 02:02:48 +00001078 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001079 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001080 switch (RHS.getValueType()) {
1081 default: break;
1082 case MVT::i1:
1083 case MVT::i8: Opc = X86::CMP8mr; break;
1084 case MVT::i16: Opc = X86::CMP16mr; break;
1085 case MVT::i32: Opc = X86::CMP32mr; break;
1086 }
1087 if (Opc) {
1088 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001089 EmitFoldedLoad(LHS, AM);
1090 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001091 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1092 return;
1093 }
1094 }
1095
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001096 switch (LHS.getValueType()) {
1097 default: assert(0 && "Cannot compare this value!");
1098 case MVT::i1:
1099 case MVT::i8: Opc = X86::CMP8rr; break;
1100 case MVT::i16: Opc = X86::CMP16rr; break;
1101 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001102 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001103 }
Chris Lattner11333092005-01-11 03:11:44 +00001104 unsigned Tmp1, Tmp2;
1105 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1106 Tmp1 = SelectExpr(LHS);
1107 Tmp2 = SelectExpr(RHS);
1108 } else {
1109 Tmp2 = SelectExpr(RHS);
1110 Tmp1 = SelectExpr(LHS);
1111 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001112 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1113}
1114
Chris Lattnera5ade062005-01-11 21:19:59 +00001115/// isFoldableLoad - Return true if this is a load instruction that can safely
1116/// be folded into an operation that uses it.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001117bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001118 if (Op.getOpcode() != ISD::LOAD ||
1119 // FIXME: currently can't fold constant pool indexes.
1120 isa<ConstantPoolSDNode>(Op.getOperand(1)))
1121 return false;
1122
1123 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001124 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1125 if (ExprMap.count(Op.getValue(1))) return false;
1126 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001127 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001128
Chris Lattner4ff348b2005-01-17 06:26:58 +00001129 // If there is not just one use of its value, we cannot fold.
1130 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1131
1132 // Finally, we cannot fold the load into the operation if this would induce a
1133 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1134 // operand of the operation we are folding the load into) can possible use the
1135 // chain node defined by the load.
1136 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1137 std::set<SDNode*> Visited;
1138 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1139 return false;
1140 }
1141 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001142}
1143
Chris Lattner4ff348b2005-01-17 06:26:58 +00001144
Chris Lattnera5ade062005-01-11 21:19:59 +00001145/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1146/// and compute the address being loaded into AM.
1147void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1148 SDOperand Chain = Op.getOperand(0);
1149 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001150
Chris Lattnera5ade062005-01-11 21:19:59 +00001151 if (getRegPressure(Chain) > getRegPressure(Address)) {
1152 Select(Chain);
1153 SelectAddress(Address, AM);
1154 } else {
1155 SelectAddress(Address, AM);
1156 Select(Chain);
1157 }
1158
1159 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001160 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1161 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001162 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001163 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001164}
1165
Chris Lattner30ea1e92005-01-19 07:37:26 +00001166// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1167// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1168// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1169// return true.
1170bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001171 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1172 // good!
1173 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1174 std::swap(Op1, Op2); // Op1 is the SHL now.
1175 } else {
1176 return false; // No match
1177 }
1178
1179 SDOperand ShlVal = Op1.getOperand(0);
1180 SDOperand ShlAmt = Op1.getOperand(1);
1181 SDOperand ShrVal = Op2.getOperand(0);
1182 SDOperand ShrAmt = Op2.getOperand(1);
1183
Chris Lattner30ea1e92005-01-19 07:37:26 +00001184 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1185
Chris Lattner85716372005-01-19 06:18:43 +00001186 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1187 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1188 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001189 if (SubCST->getValue() == RegSize) {
1190 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001191 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001192 if (ShrVal == ShlVal) {
1193 unsigned Reg, ShAmt;
1194 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1195 Reg = SelectExpr(ShrVal);
1196 ShAmt = SelectExpr(ShrAmt);
1197 } else {
1198 ShAmt = SelectExpr(ShrAmt);
1199 Reg = SelectExpr(ShrVal);
1200 }
1201 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1202 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1203 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1204 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1205 return true;
1206 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001207 unsigned AReg, BReg;
1208 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001209 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001210 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001211 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001212 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001213 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001214 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001215 unsigned ShAmt = SelectExpr(ShrAmt);
1216 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1217 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1218 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001219 return true;
1220 }
1221 }
1222
Chris Lattner4053b1e2005-01-19 08:07:05 +00001223 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1224 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1225 if (SubCST->getValue() == RegSize) {
1226 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1227 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1228 if (ShrVal == ShlVal) {
1229 unsigned Reg, ShAmt;
1230 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1231 Reg = SelectExpr(ShrVal);
1232 ShAmt = SelectExpr(ShlAmt);
1233 } else {
1234 ShAmt = SelectExpr(ShlAmt);
1235 Reg = SelectExpr(ShrVal);
1236 }
1237 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1238 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1239 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1240 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1241 return true;
1242 } else if (RegSize != 8) {
1243 unsigned AReg, BReg;
1244 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001245 AReg = SelectExpr(ShlVal);
1246 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001247 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001248 BReg = SelectExpr(ShrVal);
1249 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001250 }
1251 unsigned ShAmt = SelectExpr(ShlAmt);
1252 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1253 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1254 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1255 return true;
1256 }
1257 }
Chris Lattner85716372005-01-19 06:18:43 +00001258
Chris Lattner4053b1e2005-01-19 08:07:05 +00001259 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1260 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1261 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1262 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1263 // (A >> 5) | (A << 27) --> ROR A, 5
1264 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1265 if (ShrVal == ShlVal) {
1266 unsigned Reg = SelectExpr(ShrVal);
1267 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1268 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1269 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1270 return true;
1271 } else if (RegSize != 8) {
1272 unsigned AReg, BReg;
1273 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001274 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001275 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001276 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001277 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001278 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001279 }
1280 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1281 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1282 .addImm(ShrCst->getValue());
1283 return true;
1284 }
1285 }
1286
Chris Lattner85716372005-01-19 06:18:43 +00001287 return false;
1288}
1289
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001290unsigned ISel::SelectExpr(SDOperand N) {
1291 unsigned Result;
1292 unsigned Tmp1, Tmp2, Tmp3;
1293 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001294 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001295 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001296
Chris Lattner7f2afac2005-01-14 22:37:41 +00001297 if (Node->getOpcode() == ISD::CopyFromReg) {
1298 // FIXME: Handle copy from physregs!
1299
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001300 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001301 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001302 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001303
1304 unsigned &Reg = ExprMap[N];
1305 if (Reg) return Reg;
1306
Chris Lattner19ad0622005-01-20 18:53:00 +00001307 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
1308 N.getOpcode() != ISD::SUB_PARTS)
Chris Lattnera5ade062005-01-11 21:19:59 +00001309 Reg = Result = (N.getValueType() != MVT::Other) ?
1310 MakeReg(N.getValueType()) : 1;
1311 else {
1312 // If this is a call instruction, make sure to prepare ALL of the result
1313 // values as well as the chain.
Chris Lattner19ad0622005-01-20 18:53:00 +00001314 if (N.getOpcode() == ISD::CALL) {
1315 if (Node->getNumValues() == 1)
1316 Reg = Result = 1; // Void call, just a chain.
1317 else {
1318 Result = MakeReg(Node->getValueType(0));
1319 ExprMap[N.getValue(0)] = Result;
1320 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1321 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1322 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
1323 }
1324 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001325 Result = MakeReg(Node->getValueType(0));
1326 ExprMap[N.getValue(0)] = Result;
Chris Lattner19ad0622005-01-20 18:53:00 +00001327 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001328 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001329 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001330 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001331
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001332 switch (N.getOpcode()) {
1333 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001334 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001335 assert(0 && "Node not handled!\n");
1336 case ISD::FrameIndex:
1337 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1338 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1339 return Result;
1340 case ISD::ConstantPool:
1341 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1342 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1343 return Result;
1344 case ISD::ConstantFP:
1345 ContainsFPCode = true;
1346 Tmp1 = Result; // Intermediate Register
1347 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1348 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1349 Tmp1 = MakeReg(MVT::f64);
1350
1351 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1352 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1353 BuildMI(BB, X86::FLD0, 0, Tmp1);
1354 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1355 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1356 BuildMI(BB, X86::FLD1, 0, Tmp1);
1357 else
1358 assert(0 && "Unexpected constant!");
1359 if (Tmp1 != Result)
1360 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1361 return Result;
1362 case ISD::Constant:
1363 switch (N.getValueType()) {
1364 default: assert(0 && "Cannot use constants of this type!");
1365 case MVT::i1:
1366 case MVT::i8: Opc = X86::MOV8ri; break;
1367 case MVT::i16: Opc = X86::MOV16ri; break;
1368 case MVT::i32: Opc = X86::MOV32ri; break;
1369 }
1370 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1371 return Result;
1372 case ISD::GlobalAddress: {
1373 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1374 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1375 return Result;
1376 }
1377 case ISD::ExternalSymbol: {
1378 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1379 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1380 return Result;
1381 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001382 case ISD::ZERO_EXTEND: {
1383 int DestIs16 = N.getValueType() == MVT::i16;
1384 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001385
1386 // FIXME: This hack is here for zero extension casts from bool to i8. This
1387 // would not be needed if bools were promoted by Legalize.
1388 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001389 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001390 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1391 return Result;
1392 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001393
Chris Lattner4ff348b2005-01-17 06:26:58 +00001394 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001395 static const unsigned Opc[3] = {
1396 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1397 };
1398
1399 X86AddressMode AM;
1400 EmitFoldedLoad(N.getOperand(0), AM);
1401 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1402
1403 return Result;
1404 }
1405
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001406 static const unsigned Opc[3] = {
1407 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1408 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001409 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001410 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1411 return Result;
1412 }
1413 case ISD::SIGN_EXTEND: {
1414 int DestIs16 = N.getValueType() == MVT::i16;
1415 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1416
Chris Lattner590d8002005-01-09 18:52:44 +00001417 // FIXME: Legalize should promote bools to i8!
1418 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1419 "Sign extend from bool not implemented!");
1420
Chris Lattner4ff348b2005-01-17 06:26:58 +00001421 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001422 static const unsigned Opc[3] = {
1423 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1424 };
1425
1426 X86AddressMode AM;
1427 EmitFoldedLoad(N.getOperand(0), AM);
1428 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1429 return Result;
1430 }
1431
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001432 static const unsigned Opc[3] = {
1433 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1434 };
1435 Tmp1 = SelectExpr(N.getOperand(0));
1436 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1437 return Result;
1438 }
1439 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001440 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001441 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001442 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001443 switch (N.getValueType()) {
1444 default: assert(0 && "Unknown truncate!");
1445 case MVT::i1:
1446 case MVT::i8: Opc = X86::MOV8rm; break;
1447 case MVT::i16: Opc = X86::MOV16rm; break;
1448 }
1449 X86AddressMode AM;
1450 EmitFoldedLoad(N.getOperand(0), AM);
1451 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1452 return Result;
1453 }
1454
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001455 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1456 // a move out of AX or AL.
1457 switch (N.getOperand(0).getValueType()) {
1458 default: assert(0 && "Unknown truncate!");
1459 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1460 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1461 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1462 }
1463 Tmp1 = SelectExpr(N.getOperand(0));
1464 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1465
1466 switch (N.getValueType()) {
1467 default: assert(0 && "Unknown truncate!");
1468 case MVT::i1:
1469 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1470 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1471 }
1472 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1473 return Result;
1474
Chris Lattner590d8002005-01-09 18:52:44 +00001475 case ISD::SINT_TO_FP:
1476 case ISD::UINT_TO_FP: {
1477 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001478 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001479
1480 // Promote the integer to a type supported by FLD. We do this because there
1481 // are no unsigned FLD instructions, so we must promote an unsigned value to
1482 // a larger signed value, then use FLD on the larger value.
1483 //
1484 MVT::ValueType PromoteType = MVT::Other;
1485 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1486 unsigned PromoteOpcode = 0;
1487 unsigned RealDestReg = Result;
1488 switch (SrcTy) {
1489 case MVT::i1:
1490 case MVT::i8:
1491 // We don't have the facilities for directly loading byte sized data from
1492 // memory (even signed). Promote it to 16 bits.
1493 PromoteType = MVT::i16;
1494 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1495 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1496 break;
1497 case MVT::i16:
1498 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1499 PromoteType = MVT::i32;
1500 PromoteOpcode = X86::MOVZX32rr16;
1501 }
1502 break;
1503 default:
1504 // Don't fild into the real destination.
1505 if (Node->getOpcode() == ISD::UINT_TO_FP)
1506 Result = MakeReg(Node->getValueType(0));
1507 break;
1508 }
1509
1510 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1511
1512 if (PromoteType != MVT::Other) {
1513 Tmp2 = MakeReg(PromoteType);
1514 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1515 SrcTy = PromoteType;
1516 Tmp1 = Tmp2;
1517 }
1518
1519 // Spill the integer to memory and reload it from there.
1520 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1521 MachineFunction *F = BB->getParent();
1522 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1523
1524 switch (SrcTy) {
1525 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001526 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001527 // FIXME: this won't work for cast [u]long to FP
1528 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1529 FrameIdx).addReg(Tmp1);
1530 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1531 FrameIdx, 4).addReg(Tmp1+1);
1532 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1533 break;
1534 case MVT::i32:
1535 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1536 FrameIdx).addReg(Tmp1);
1537 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1538 break;
1539 case MVT::i16:
1540 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1541 FrameIdx).addReg(Tmp1);
1542 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1543 break;
1544 default: break; // No promotion required.
1545 }
1546
Chris Lattner085c9952005-01-12 04:00:00 +00001547 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001548 // If this is a cast from uint -> double, we need to be careful when if
1549 // the "sign" bit is set. If so, we don't want to make a negative number,
1550 // we want to make a positive number. Emit code to add an offset if the
1551 // sign bit is set.
1552
1553 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1554 unsigned IsNeg = MakeReg(MVT::i32);
1555 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1556
1557 // Create a CP value that has the offset in one word and 0 in the other.
1558 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1559 0x4f80000000000000ULL);
1560 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1561 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1562 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1563
1564 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1565 // We need special handling for unsigned 64-bit integer sources. If the
1566 // input number has the "sign bit" set, then we loaded it incorrectly as a
1567 // negative 64-bit number. In this case, add an offset value.
1568
1569 // Emit a test instruction to see if the dynamic input value was signed.
1570 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1571
1572 // If the sign bit is set, get a pointer to an offset, otherwise get a
1573 // pointer to a zero.
1574 MachineConstantPool *CP = F->getConstantPool();
1575 unsigned Zero = MakeReg(MVT::i32);
1576 Constant *Null = Constant::getNullValue(Type::UIntTy);
1577 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1578 CP->getConstantPoolIndex(Null));
1579 unsigned Offset = MakeReg(MVT::i32);
1580 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1581
1582 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1583 CP->getConstantPoolIndex(OffsetCst));
1584 unsigned Addr = MakeReg(MVT::i32);
1585 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1586
1587 // Load the constant for an add. FIXME: this could make an 'fadd' that
1588 // reads directly from memory, but we don't support these yet.
1589 unsigned ConstReg = MakeReg(MVT::f64);
1590 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1591
1592 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1593 }
1594 return RealDestReg;
1595 }
1596 case ISD::FP_TO_SINT:
1597 case ISD::FP_TO_UINT: {
1598 // FIXME: Most of this grunt work should be done by legalize!
1599 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1600
1601 // Change the floating point control register to use "round towards zero"
1602 // mode when truncating to an integer value.
1603 //
1604 MachineFunction *F = BB->getParent();
1605 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1606 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1607
1608 // Load the old value of the high byte of the control word...
1609 unsigned HighPartOfCW = MakeReg(MVT::i8);
1610 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1611 CWFrameIdx, 1);
1612
1613 // Set the high part to be round to zero...
1614 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1615 CWFrameIdx, 1).addImm(12);
1616
1617 // Reload the modified control word now...
1618 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1619
1620 // Restore the memory image of control word to original value
1621 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1622 CWFrameIdx, 1).addReg(HighPartOfCW);
1623
1624 // We don't have the facilities for directly storing byte sized data to
1625 // memory. Promote it to 16 bits. We also must promote unsigned values to
1626 // larger classes because we only have signed FP stores.
1627 MVT::ValueType StoreClass = Node->getValueType(0);
1628 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1629 switch (StoreClass) {
1630 case MVT::i8: StoreClass = MVT::i16; break;
1631 case MVT::i16: StoreClass = MVT::i32; break;
1632 case MVT::i32: StoreClass = MVT::i64; break;
1633 // The following treatment of cLong may not be perfectly right,
1634 // but it survives chains of casts of the form
1635 // double->ulong->double.
1636 case MVT::i64: StoreClass = MVT::i64; break;
1637 default: assert(0 && "Unknown store class!");
1638 }
1639
1640 // Spill the integer to memory and reload it from there.
1641 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1642 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1643
1644 switch (StoreClass) {
1645 default: assert(0 && "Unknown store class!");
1646 case MVT::i16:
1647 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1648 break;
1649 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001650 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001651 break;
1652 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001653 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001654 break;
1655 }
1656
1657 switch (Node->getValueType(0)) {
1658 default:
1659 assert(0 && "Unknown integer type!");
1660 case MVT::i64:
1661 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001662 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001663 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1664 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1665 case MVT::i32:
1666 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1667 break;
1668 case MVT::i16:
1669 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1670 break;
1671 case MVT::i8:
1672 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1673 break;
1674 }
1675
1676 // Reload the original control word now.
1677 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1678 return Result;
1679 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001680 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001681 Op0 = N.getOperand(0);
1682 Op1 = N.getOperand(1);
1683
Chris Lattner4ff348b2005-01-17 06:26:58 +00001684 if (isFoldableLoad(Op0, Op1)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001685 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001686 goto FoldAdd;
1687 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001688
Chris Lattner4ff348b2005-01-17 06:26:58 +00001689 if (isFoldableLoad(Op1, Op0)) {
1690 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001691 switch (N.getValueType()) {
1692 default: assert(0 && "Cannot add this type!");
1693 case MVT::i1:
1694 case MVT::i8: Opc = X86::ADD8rm; break;
1695 case MVT::i16: Opc = X86::ADD16rm; break;
1696 case MVT::i32: Opc = X86::ADD32rm; break;
1697 case MVT::f32: Opc = X86::FADD32m; break;
1698 case MVT::f64: Opc = X86::FADD64m; break;
1699 }
1700 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001701 EmitFoldedLoad(Op1, AM);
1702 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001703 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1704 return Result;
1705 }
1706
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001707 // See if we can codegen this as an LEA to fold operations together.
1708 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001709 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001710 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001711 MatchAddress(N, AM);
1712 ExprMap[N] = Result;
1713
1714 // If this is not just an add, emit the LEA. For a simple add (like
1715 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1716 // leave this as LEA, then peephole it to 'ADD' after two address elim
1717 // happens.
1718 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1719 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1720 X86AddressMode XAM = SelectAddrExprs(AM);
1721 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1722 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001723 }
1724 }
Chris Lattner11333092005-01-11 03:11:44 +00001725
Chris Lattnera5ade062005-01-11 21:19:59 +00001726 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001727 Opc = 0;
1728 if (CN->getValue() == 1) { // add X, 1 -> inc X
1729 switch (N.getValueType()) {
1730 default: assert(0 && "Cannot integer add this type!");
1731 case MVT::i8: Opc = X86::INC8r; break;
1732 case MVT::i16: Opc = X86::INC16r; break;
1733 case MVT::i32: Opc = X86::INC32r; break;
1734 }
1735 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1736 switch (N.getValueType()) {
1737 default: assert(0 && "Cannot integer add this type!");
1738 case MVT::i8: Opc = X86::DEC8r; break;
1739 case MVT::i16: Opc = X86::DEC16r; break;
1740 case MVT::i32: Opc = X86::DEC32r; break;
1741 }
1742 }
1743
1744 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001745 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001746 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1747 return Result;
1748 }
1749
1750 switch (N.getValueType()) {
1751 default: assert(0 && "Cannot add this type!");
1752 case MVT::i8: Opc = X86::ADD8ri; break;
1753 case MVT::i16: Opc = X86::ADD16ri; break;
1754 case MVT::i32: Opc = X86::ADD32ri; break;
1755 }
1756 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001757 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001758 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1759 return Result;
1760 }
1761 }
1762
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001763 switch (N.getValueType()) {
1764 default: assert(0 && "Cannot add this type!");
1765 case MVT::i8: Opc = X86::ADD8rr; break;
1766 case MVT::i16: Opc = X86::ADD16rr; break;
1767 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001768 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001769 }
Chris Lattner11333092005-01-11 03:11:44 +00001770
Chris Lattnera5ade062005-01-11 21:19:59 +00001771 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1772 Tmp1 = SelectExpr(Op0);
1773 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001774 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001775 Tmp2 = SelectExpr(Op1);
1776 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001777 }
1778
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001779 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1780 return Result;
1781 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001782 case ISD::MUL:
1783 case ISD::AND:
1784 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001785 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001786 static const unsigned SUBTab[] = {
1787 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1788 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1789 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1790 };
1791 static const unsigned MULTab[] = {
1792 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1793 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1794 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1795 };
1796 static const unsigned ANDTab[] = {
1797 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1798 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1799 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1800 };
1801 static const unsigned ORTab[] = {
1802 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1803 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1804 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1805 };
1806 static const unsigned XORTab[] = {
1807 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1808 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1809 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1810 };
1811
1812 Op0 = Node->getOperand(0);
1813 Op1 = Node->getOperand(1);
1814
Chris Lattner30ea1e92005-01-19 07:37:26 +00001815 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1816 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001817 return Result;
1818
1819 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001820 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1821 if (CN->isNullValue()) { // 0 - N -> neg N
1822 switch (N.getValueType()) {
1823 default: assert(0 && "Cannot sub this type!");
1824 case MVT::i1:
1825 case MVT::i8: Opc = X86::NEG8r; break;
1826 case MVT::i16: Opc = X86::NEG16r; break;
1827 case MVT::i32: Opc = X86::NEG32r; break;
1828 }
1829 Tmp1 = SelectExpr(N.getOperand(1));
1830 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1831 return Result;
1832 }
1833
Chris Lattnera5ade062005-01-11 21:19:59 +00001834 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1835 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001836 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001837 switch (N.getValueType()) {
1838 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001839 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001840 case MVT::i8: Opc = X86::NOT8r; break;
1841 case MVT::i16: Opc = X86::NOT16r; break;
1842 case MVT::i32: Opc = X86::NOT32r; break;
1843 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001844 if (Opc) {
1845 Tmp1 = SelectExpr(Op0);
1846 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1847 return Result;
1848 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001849 }
1850
Chris Lattner2a4e5082005-01-17 06:48:02 +00001851 // Fold common multiplies into LEA instructions.
1852 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1853 switch ((int)CN->getValue()) {
1854 default: break;
1855 case 3:
1856 case 5:
1857 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001858 // Remove N from exprmap so SelectAddress doesn't get confused.
1859 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001860 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001861 SelectAddress(N, AM);
1862 // Restore it to the map.
1863 ExprMap[N] = Result;
1864 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1865 return Result;
1866 }
1867 }
1868
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001869 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001870 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001871 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001872 case MVT::i8: Opc = 0; break;
1873 case MVT::i16: Opc = 1; break;
1874 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001875 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001876 switch (Node->getOpcode()) {
1877 default: assert(0 && "Unreachable!");
1878 case ISD::SUB: Opc = SUBTab[Opc]; break;
1879 case ISD::MUL: Opc = MULTab[Opc]; break;
1880 case ISD::AND: Opc = ANDTab[Opc]; break;
1881 case ISD::OR: Opc = ORTab[Opc]; break;
1882 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001883 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001884 if (Opc) { // Can't fold MUL:i8 R, imm
1885 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001886 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1887 return Result;
1888 }
1889 }
Chris Lattner11333092005-01-11 03:11:44 +00001890
Chris Lattner4ff348b2005-01-17 06:26:58 +00001891 if (isFoldableLoad(Op0, Op1))
Chris Lattnera5ade062005-01-11 21:19:59 +00001892 if (Node->getOpcode() != ISD::SUB) {
1893 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001894 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001895 } else {
1896 // Emit 'reverse' subract, with a memory operand.
1897 switch (N.getValueType()) {
1898 default: Opc = 0; break;
1899 case MVT::f32: Opc = X86::FSUBR32m; break;
1900 case MVT::f64: Opc = X86::FSUBR64m; break;
1901 }
1902 if (Opc) {
1903 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001904 EmitFoldedLoad(Op0, AM);
1905 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001906 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1907 return Result;
1908 }
1909 }
1910
Chris Lattner4ff348b2005-01-17 06:26:58 +00001911 if (isFoldableLoad(Op1, Op0)) {
1912 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001913 switch (N.getValueType()) {
1914 default: assert(0 && "Cannot operate on this type!");
1915 case MVT::i1:
1916 case MVT::i8: Opc = 5; break;
1917 case MVT::i16: Opc = 6; break;
1918 case MVT::i32: Opc = 7; break;
1919 case MVT::f32: Opc = 8; break;
1920 case MVT::f64: Opc = 9; break;
1921 }
1922 switch (Node->getOpcode()) {
1923 default: assert(0 && "Unreachable!");
1924 case ISD::SUB: Opc = SUBTab[Opc]; break;
1925 case ISD::MUL: Opc = MULTab[Opc]; break;
1926 case ISD::AND: Opc = ANDTab[Opc]; break;
1927 case ISD::OR: Opc = ORTab[Opc]; break;
1928 case ISD::XOR: Opc = XORTab[Opc]; break;
1929 }
1930
1931 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001932 EmitFoldedLoad(Op1, AM);
1933 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001934 if (Opc) {
1935 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1936 } else {
1937 assert(Node->getOpcode() == ISD::MUL &&
1938 N.getValueType() == MVT::i8 && "Unexpected situation!");
1939 // Must use the MUL instruction, which forces use of AL.
1940 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1941 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1942 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1943 }
1944 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001945 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001946
1947 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1948 Tmp1 = SelectExpr(Op0);
1949 Tmp2 = SelectExpr(Op1);
1950 } else {
1951 Tmp2 = SelectExpr(Op1);
1952 Tmp1 = SelectExpr(Op0);
1953 }
1954
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001955 switch (N.getValueType()) {
1956 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001957 case MVT::i1:
1958 case MVT::i8: Opc = 10; break;
1959 case MVT::i16: Opc = 11; break;
1960 case MVT::i32: Opc = 12; break;
1961 case MVT::f32: Opc = 13; break;
1962 case MVT::f64: Opc = 14; break;
1963 }
1964 switch (Node->getOpcode()) {
1965 default: assert(0 && "Unreachable!");
1966 case ISD::SUB: Opc = SUBTab[Opc]; break;
1967 case ISD::MUL: Opc = MULTab[Opc]; break;
1968 case ISD::AND: Opc = ANDTab[Opc]; break;
1969 case ISD::OR: Opc = ORTab[Opc]; break;
1970 case ISD::XOR: Opc = XORTab[Opc]; break;
1971 }
1972 if (Opc) {
1973 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1974 } else {
1975 assert(Node->getOpcode() == ISD::MUL &&
1976 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001977 // Must use the MUL instruction, which forces use of AL.
1978 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1979 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1980 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001981 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001982 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001983 }
Chris Lattner19ad0622005-01-20 18:53:00 +00001984 case ISD::ADD_PARTS:
1985 case ISD::SUB_PARTS: {
1986 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1987 "Not an i64 add/sub!");
1988 // Emit all of the operands.
1989 std::vector<unsigned> InVals;
1990 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1991 InVals.push_back(SelectExpr(N.getOperand(i)));
1992 if (N.getOpcode() == ISD::ADD_PARTS) {
1993 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1994 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1995 } else {
1996 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1997 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1998 }
1999 return Result+N.ResNo;
2000 }
2001
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002002 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002003 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2004 Tmp2 = SelectExpr(N.getOperand(1));
2005 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002006 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002007 Tmp3 = SelectExpr(N.getOperand(2));
2008 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002009 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002010 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2011 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002012
2013 case ISD::SDIV:
2014 case ISD::UDIV:
2015 case ISD::SREM:
2016 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002017 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2018 "We don't support this operator!");
2019
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002020 if (N.getOpcode() == ISD::SDIV)
2021 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2022 // FIXME: These special cases should be handled by the lowering impl!
2023 unsigned RHS = CN->getValue();
2024 bool isNeg = false;
2025 if ((int)RHS < 0) {
2026 isNeg = true;
2027 RHS = -RHS;
2028 }
2029 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2030 unsigned Log = log2(RHS);
2031 unsigned TmpReg = MakeReg(N.getValueType());
2032 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2033 switch (N.getValueType()) {
2034 default: assert("Unknown type to signed divide!");
2035 case MVT::i8:
2036 SAROpc = X86::SAR8ri;
2037 SHROpc = X86::SHR8ri;
2038 ADDOpc = X86::ADD8rr;
2039 NEGOpc = X86::NEG8r;
2040 break;
2041 case MVT::i16:
2042 SAROpc = X86::SAR16ri;
2043 SHROpc = X86::SHR16ri;
2044 ADDOpc = X86::ADD16rr;
2045 NEGOpc = X86::NEG16r;
2046 break;
2047 case MVT::i32:
2048 SAROpc = X86::SAR32ri;
2049 SHROpc = X86::SHR32ri;
2050 ADDOpc = X86::ADD32rr;
2051 NEGOpc = X86::NEG32r;
2052 break;
2053 }
Chris Lattner11333092005-01-11 03:11:44 +00002054 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002055 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2056 unsigned TmpReg2 = MakeReg(N.getValueType());
2057 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2058 unsigned TmpReg3 = MakeReg(N.getValueType());
2059 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
2060
2061 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2062 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2063 if (isNeg)
2064 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2065 return Result;
2066 }
2067 }
2068
Chris Lattner11333092005-01-11 03:11:44 +00002069 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2070 Tmp1 = SelectExpr(N.getOperand(0));
2071 Tmp2 = SelectExpr(N.getOperand(1));
2072 } else {
2073 Tmp2 = SelectExpr(N.getOperand(1));
2074 Tmp1 = SelectExpr(N.getOperand(0));
2075 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002076
2077 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2078 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2079 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2080 switch (N.getValueType()) {
2081 default: assert(0 && "Cannot sdiv this type!");
2082 case MVT::i8:
2083 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2084 LoReg = X86::AL;
2085 HiReg = X86::AH;
2086 MovOpcode = X86::MOV8rr;
2087 ClrOpcode = X86::MOV8ri;
2088 SExtOpcode = X86::CBW;
2089 break;
2090 case MVT::i16:
2091 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2092 LoReg = X86::AX;
2093 HiReg = X86::DX;
2094 MovOpcode = X86::MOV16rr;
2095 ClrOpcode = X86::MOV16ri;
2096 SExtOpcode = X86::CWD;
2097 break;
2098 case MVT::i32:
2099 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002100 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002101 HiReg = X86::EDX;
2102 MovOpcode = X86::MOV32rr;
2103 ClrOpcode = X86::MOV32ri;
2104 SExtOpcode = X86::CDQ;
2105 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002106 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002107 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002108 return Result;
2109 }
2110
2111 // Set up the low part.
2112 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2113
2114 if (isSigned) {
2115 // Sign extend the low part into the high part.
2116 BuildMI(BB, SExtOpcode, 0);
2117 } else {
2118 // Zero out the high part, effectively zero extending the input.
2119 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2120 }
2121
2122 // Emit the DIV/IDIV instruction.
2123 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
2124
2125 // Get the result of the divide or rem.
2126 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2127 return Result;
2128 }
2129
2130 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002131 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002132 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2133 switch (N.getValueType()) {
2134 default: assert(0 && "Cannot shift this type!");
2135 case MVT::i8: Opc = X86::ADD8rr; break;
2136 case MVT::i16: Opc = X86::ADD16rr; break;
2137 case MVT::i32: Opc = X86::ADD32rr; break;
2138 }
2139 Tmp1 = SelectExpr(N.getOperand(0));
2140 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2141 return Result;
2142 }
2143
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002144 switch (N.getValueType()) {
2145 default: assert(0 && "Cannot shift this type!");
2146 case MVT::i8: Opc = X86::SHL8ri; break;
2147 case MVT::i16: Opc = X86::SHL16ri; break;
2148 case MVT::i32: Opc = X86::SHL32ri; break;
2149 }
Chris Lattner11333092005-01-11 03:11:44 +00002150 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002151 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2152 return Result;
2153 }
Chris Lattner11333092005-01-11 03:11:44 +00002154
2155 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2156 Tmp1 = SelectExpr(N.getOperand(0));
2157 Tmp2 = SelectExpr(N.getOperand(1));
2158 } else {
2159 Tmp2 = SelectExpr(N.getOperand(1));
2160 Tmp1 = SelectExpr(N.getOperand(0));
2161 }
2162
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002163 switch (N.getValueType()) {
2164 default: assert(0 && "Cannot shift this type!");
2165 case MVT::i8 : Opc = X86::SHL8rCL; break;
2166 case MVT::i16: Opc = X86::SHL16rCL; break;
2167 case MVT::i32: Opc = X86::SHL32rCL; break;
2168 }
2169 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2170 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2171 return Result;
2172 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002173 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2174 switch (N.getValueType()) {
2175 default: assert(0 && "Cannot shift this type!");
2176 case MVT::i8: Opc = X86::SHR8ri; break;
2177 case MVT::i16: Opc = X86::SHR16ri; break;
2178 case MVT::i32: Opc = X86::SHR32ri; break;
2179 }
Chris Lattner11333092005-01-11 03:11:44 +00002180 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002181 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2182 return Result;
2183 }
Chris Lattner11333092005-01-11 03:11:44 +00002184
2185 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2186 Tmp1 = SelectExpr(N.getOperand(0));
2187 Tmp2 = SelectExpr(N.getOperand(1));
2188 } else {
2189 Tmp2 = SelectExpr(N.getOperand(1));
2190 Tmp1 = SelectExpr(N.getOperand(0));
2191 }
2192
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002193 switch (N.getValueType()) {
2194 default: assert(0 && "Cannot shift this type!");
2195 case MVT::i8 : Opc = X86::SHR8rCL; break;
2196 case MVT::i16: Opc = X86::SHR16rCL; break;
2197 case MVT::i32: Opc = X86::SHR32rCL; break;
2198 }
2199 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2200 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2201 return Result;
2202 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002203 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2204 switch (N.getValueType()) {
2205 default: assert(0 && "Cannot shift this type!");
2206 case MVT::i8: Opc = X86::SAR8ri; break;
2207 case MVT::i16: Opc = X86::SAR16ri; break;
2208 case MVT::i32: Opc = X86::SAR32ri; break;
2209 }
Chris Lattner11333092005-01-11 03:11:44 +00002210 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002211 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2212 return Result;
2213 }
Chris Lattner11333092005-01-11 03:11:44 +00002214
2215 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2216 Tmp1 = SelectExpr(N.getOperand(0));
2217 Tmp2 = SelectExpr(N.getOperand(1));
2218 } else {
2219 Tmp2 = SelectExpr(N.getOperand(1));
2220 Tmp1 = SelectExpr(N.getOperand(0));
2221 }
2222
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002223 switch (N.getValueType()) {
2224 default: assert(0 && "Cannot shift this type!");
2225 case MVT::i8 : Opc = X86::SAR8rCL; break;
2226 case MVT::i16: Opc = X86::SAR16rCL; break;
2227 case MVT::i32: Opc = X86::SAR32rCL; break;
2228 }
2229 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2230 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2231 return Result;
2232
2233 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002234 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002235 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2236 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2237 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002238 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002239 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002240 if (Result != 1) { // Generate the token
2241 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2242 assert(0 && "Load already emitted!?");
2243 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002244 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2245
Chris Lattner5188ad72005-01-08 19:28:19 +00002246 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002247 default: assert(0 && "Cannot load this type!");
2248 case MVT::i1:
2249 case MVT::i8: Opc = X86::MOV8rm; break;
2250 case MVT::i16: Opc = X86::MOV16rm; break;
2251 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002252 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2253 }
Chris Lattner11333092005-01-11 03:11:44 +00002254
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002255 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002256 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002257 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2258 } else {
2259 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002260
2261 SDOperand Chain = N.getOperand(0);
2262 SDOperand Address = N.getOperand(1);
2263 if (getRegPressure(Chain) > getRegPressure(Address)) {
2264 Select(Chain);
2265 SelectAddress(Address, AM);
2266 } else {
2267 SelectAddress(Address, AM);
2268 Select(Chain);
2269 }
2270
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002271 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2272 }
2273 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002274
2275 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2276 case ISD::ZEXTLOAD: {
2277 // Make sure we generate both values.
2278 if (Result != 1)
2279 ExprMap[N.getValue(1)] = 1; // Generate the token
2280 else
2281 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2282
Chris Lattnerda2ce112005-01-16 07:34:08 +00002283 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2284 if (Node->getValueType(0) == MVT::f64) {
2285 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2286 "Bad EXTLOAD!");
2287 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2288 CP->getIndex());
2289 return Result;
2290 }
2291
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002292 X86AddressMode AM;
2293 if (getRegPressure(Node->getOperand(0)) >
2294 getRegPressure(Node->getOperand(1))) {
2295 Select(Node->getOperand(0)); // chain
2296 SelectAddress(Node->getOperand(1), AM);
2297 } else {
2298 SelectAddress(Node->getOperand(1), AM);
2299 Select(Node->getOperand(0)); // chain
2300 }
2301
2302 switch (Node->getValueType(0)) {
2303 default: assert(0 && "Unknown type to sign extend to.");
2304 case MVT::f64:
2305 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2306 "Bad EXTLOAD!");
2307 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2308 break;
2309 case MVT::i32:
2310 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2311 default:
2312 assert(0 && "Bad zero extend!");
2313 case MVT::i1:
2314 case MVT::i8:
2315 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2316 break;
2317 case MVT::i16:
2318 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2319 break;
2320 }
2321 break;
2322 case MVT::i16:
2323 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2324 "Bad zero extend!");
2325 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2326 break;
2327 case MVT::i8:
2328 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2329 "Bad zero extend!");
2330 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2331 break;
2332 }
2333 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002334 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002335 case ISD::SEXTLOAD: {
2336 // Make sure we generate both values.
2337 if (Result != 1)
2338 ExprMap[N.getValue(1)] = 1; // Generate the token
2339 else
2340 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2341
2342 X86AddressMode AM;
2343 if (getRegPressure(Node->getOperand(0)) >
2344 getRegPressure(Node->getOperand(1))) {
2345 Select(Node->getOperand(0)); // chain
2346 SelectAddress(Node->getOperand(1), AM);
2347 } else {
2348 SelectAddress(Node->getOperand(1), AM);
2349 Select(Node->getOperand(0)); // chain
2350 }
2351
2352 switch (Node->getValueType(0)) {
2353 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2354 default: assert(0 && "Unknown type to sign extend to.");
2355 case MVT::i32:
2356 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2357 default:
2358 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2359 case MVT::i8:
2360 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2361 break;
2362 case MVT::i16:
2363 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2364 break;
2365 }
2366 break;
2367 case MVT::i16:
2368 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2369 "Cannot sign extend from bool!");
2370 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2371 break;
2372 }
2373 return Result;
2374 }
2375
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002376 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002377 // Generate both result values.
2378 if (Result != 1)
2379 ExprMap[N.getValue(1)] = 1; // Generate the token
2380 else
2381 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2382
2383 // FIXME: We are currently ignoring the requested alignment for handling
2384 // greater than the stack alignment. This will need to be revisited at some
2385 // point. Align = N.getOperand(2);
2386
2387 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2388 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2389 std::cerr << "Cannot allocate stack object with greater alignment than"
2390 << " the stack alignment yet!";
2391 abort();
2392 }
2393
2394 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002395 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002396 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2397 .addImm(CN->getValue());
2398 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002399 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2400 Select(N.getOperand(0));
2401 Tmp1 = SelectExpr(N.getOperand(1));
2402 } else {
2403 Tmp1 = SelectExpr(N.getOperand(1));
2404 Select(N.getOperand(0));
2405 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002406
2407 // Subtract size from stack pointer, thereby allocating some space.
2408 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2409 }
2410
2411 // Put a pointer to the space into the result register, by copying the stack
2412 // pointer.
2413 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2414 return Result;
2415
2416 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002417 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002418 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002419
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002420 if (GlobalAddressSDNode *GASD =
2421 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002422 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002423 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2424 } else if (ExternalSymbolSDNode *ESSDN =
2425 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002426 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002427 BuildMI(BB, X86::CALLpcrel32,
2428 1).addExternalSymbol(ESSDN->getSymbol(), true);
2429 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002430 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2431 Select(N.getOperand(0));
2432 Tmp1 = SelectExpr(N.getOperand(1));
2433 } else {
2434 Tmp1 = SelectExpr(N.getOperand(1));
2435 Select(N.getOperand(0));
2436 }
2437
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002438 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2439 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002440 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002441 default: assert(0 && "Unknown value type for call result!");
2442 case MVT::Other: return 1;
2443 case MVT::i1:
2444 case MVT::i8:
2445 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2446 break;
2447 case MVT::i16:
2448 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2449 break;
2450 case MVT::i32:
2451 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002452 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002453 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2454 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002455 case MVT::f64: // Floating-point return values live in %ST(0)
2456 ContainsFPCode = true;
2457 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2458 break;
2459 }
2460 return Result+N.ResNo;
2461 }
2462
2463 return 0;
2464}
2465
Chris Lattnere10269b2005-01-17 19:25:26 +00002466/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2467/// load/op/store instruction. If successful return true.
2468bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2469 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2470 SDOperand Chain = Node->getOperand(0);
2471 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002472 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002473
2474 // The chain has to be a load, the stored value must be an integer binary
2475 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002476 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002477 MVT::isFloatingPoint(StVal.getValueType()))
2478 return false;
2479
Chris Lattner5c659812005-01-17 22:10:42 +00002480 // Token chain must either be a factor node or the load to fold.
2481 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2482 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002483
Chris Lattner5c659812005-01-17 22:10:42 +00002484 SDOperand TheLoad;
2485
2486 // Check to see if there is a load from the same pointer that we're storing
2487 // to in either operand of the binop.
2488 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2489 StVal.getOperand(0).getOperand(1) == StPtr)
2490 TheLoad = StVal.getOperand(0);
2491 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2492 StVal.getOperand(1).getOperand(1) == StPtr)
2493 TheLoad = StVal.getOperand(1);
2494 else
2495 return false; // No matching load operand.
2496
2497 // We can only fold the load if there are no intervening side-effecting
2498 // operations. This means that the store uses the load as its token chain, or
2499 // there are only token factor nodes in between the store and load.
2500 if (Chain != TheLoad.getValue(1)) {
2501 // Okay, the other option is that we have a store referring to (possibly
2502 // nested) token factor nodes. For now, just try peeking through one level
2503 // of token factors to see if this is the case.
2504 bool ChainOk = false;
2505 if (Chain.getOpcode() == ISD::TokenFactor) {
2506 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2507 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2508 ChainOk = true;
2509 break;
2510 }
2511 }
2512
2513 if (!ChainOk) return false;
2514 }
2515
2516 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002517 return false;
2518
2519 // Make sure that one of the operands of the binop is the load, and that the
2520 // load folds into the binop.
2521 if (((StVal.getOperand(0) != TheLoad ||
2522 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2523 (StVal.getOperand(1) != TheLoad ||
2524 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2525 return false;
2526
2527 // Finally, check to see if this is one of the ops we can handle!
2528 static const unsigned ADDTAB[] = {
2529 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2530 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2531 };
2532 static const unsigned SUBTAB[] = {
2533 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2534 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2535 };
2536 static const unsigned ANDTAB[] = {
2537 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2538 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2539 };
2540 static const unsigned ORTAB[] = {
2541 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2542 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2543 };
2544 static const unsigned XORTAB[] = {
2545 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2546 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2547 };
2548 static const unsigned SHLTAB[] = {
2549 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2550 /*Have to put the reg in CL*/0, 0, 0,
2551 };
2552 static const unsigned SARTAB[] = {
2553 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2554 /*Have to put the reg in CL*/0, 0, 0,
2555 };
2556 static const unsigned SHRTAB[] = {
2557 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2558 /*Have to put the reg in CL*/0, 0, 0,
2559 };
2560
2561 const unsigned *TabPtr = 0;
2562 switch (StVal.getOpcode()) {
2563 default:
2564 std::cerr << "CANNOT [mem] op= val: ";
2565 StVal.Val->dump(); std::cerr << "\n";
2566 case ISD::MUL:
2567 case ISD::SDIV:
2568 case ISD::UDIV:
2569 case ISD::SREM:
2570 case ISD::UREM: return false;
2571
2572 case ISD::ADD: TabPtr = ADDTAB; break;
2573 case ISD::SUB: TabPtr = SUBTAB; break;
2574 case ISD::AND: TabPtr = ANDTAB; break;
2575 case ISD:: OR: TabPtr = ORTAB; break;
2576 case ISD::XOR: TabPtr = XORTAB; break;
2577 case ISD::SHL: TabPtr = SHLTAB; break;
2578 case ISD::SRA: TabPtr = SARTAB; break;
2579 case ISD::SRL: TabPtr = SHRTAB; break;
2580 }
2581
2582 // Handle: [mem] op= CST
2583 SDOperand Op0 = StVal.getOperand(0);
2584 SDOperand Op1 = StVal.getOperand(1);
2585 unsigned Opc;
2586 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2587 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2588 default: break;
2589 case MVT::i1:
2590 case MVT::i8: Opc = TabPtr[0]; break;
2591 case MVT::i16: Opc = TabPtr[1]; break;
2592 case MVT::i32: Opc = TabPtr[2]; break;
2593 }
2594
2595 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002596 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2597 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002598 Select(Chain);
2599
Chris Lattnere10269b2005-01-17 19:25:26 +00002600 X86AddressMode AM;
2601 if (getRegPressure(TheLoad.getOperand(0)) >
2602 getRegPressure(TheLoad.getOperand(1))) {
2603 Select(TheLoad.getOperand(0));
2604 SelectAddress(TheLoad.getOperand(1), AM);
2605 } else {
2606 SelectAddress(TheLoad.getOperand(1), AM);
2607 Select(TheLoad.getOperand(0));
2608 }
Chris Lattner5c659812005-01-17 22:10:42 +00002609
2610 if (StVal.getOpcode() == ISD::ADD) {
2611 if (CN->getValue() == 1) {
2612 switch (Op0.getValueType()) {
2613 default: break;
2614 case MVT::i8:
2615 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2616 return true;
2617 case MVT::i16: Opc = TabPtr[1];
2618 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2619 return true;
2620 case MVT::i32: Opc = TabPtr[2];
2621 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2622 return true;
2623 }
2624 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2625 switch (Op0.getValueType()) {
2626 default: break;
2627 case MVT::i8:
2628 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2629 return true;
2630 case MVT::i16: Opc = TabPtr[1];
2631 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2632 return true;
2633 case MVT::i32: Opc = TabPtr[2];
2634 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2635 return true;
2636 }
2637 }
2638 }
Chris Lattnere10269b2005-01-17 19:25:26 +00002639
2640 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2641 return true;
2642 }
2643 }
2644
2645 // If we have [mem] = V op [mem], try to turn it into:
2646 // [mem] = [mem] op V.
2647 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2648 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2649 StVal.getOpcode() != ISD::SRL)
2650 std::swap(Op0, Op1);
2651
2652 if (Op0 != TheLoad) return false;
2653
2654 switch (Op0.getValueType()) {
2655 default: return false;
2656 case MVT::i1:
2657 case MVT::i8: Opc = TabPtr[3]; break;
2658 case MVT::i16: Opc = TabPtr[4]; break;
2659 case MVT::i32: Opc = TabPtr[5]; break;
2660 }
Chris Lattner5c659812005-01-17 22:10:42 +00002661
Chris Lattnerb422aea2005-01-18 17:35:28 +00002662 // Table entry doesn't exist?
2663 if (Opc == 0) return false;
2664
Chris Lattner4a108662005-01-18 03:51:59 +00002665 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2666 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002667 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002668 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002669
Chris Lattnere10269b2005-01-17 19:25:26 +00002670 X86AddressMode AM;
2671 SelectAddress(TheLoad.getOperand(1), AM);
2672 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002673 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002674 return true;
2675}
2676
2677
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002678void ISel::Select(SDOperand N) {
2679 unsigned Tmp1, Tmp2, Opc;
2680
2681 // FIXME: Disable for our current expansion model!
Chris Lattner4a108662005-01-18 03:51:59 +00002682 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002683 return; // Already selected.
2684
Chris Lattner989de032005-01-11 06:14:36 +00002685 SDNode *Node = N.Val;
2686
2687 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002688 default:
Chris Lattner989de032005-01-11 06:14:36 +00002689 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002690 assert(0 && "Node not handled yet!");
2691 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002692 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002693 if (Node->getNumOperands() == 2) {
2694 bool OneFirst =
2695 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2696 Select(Node->getOperand(OneFirst));
2697 Select(Node->getOperand(!OneFirst));
2698 } else {
2699 std::vector<std::pair<unsigned, unsigned> > OpsP;
2700 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2701 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2702 std::sort(OpsP.begin(), OpsP.end());
2703 std::reverse(OpsP.begin(), OpsP.end());
2704 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2705 Select(Node->getOperand(OpsP[i].second));
2706 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002707 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002708 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002709 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2710 Select(N.getOperand(0));
2711 Tmp1 = SelectExpr(N.getOperand(1));
2712 } else {
2713 Tmp1 = SelectExpr(N.getOperand(1));
2714 Select(N.getOperand(0));
2715 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002716 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002717
2718 if (Tmp1 != Tmp2) {
2719 switch (N.getOperand(1).getValueType()) {
2720 default: assert(0 && "Invalid type for operation!");
2721 case MVT::i1:
2722 case MVT::i8: Opc = X86::MOV8rr; break;
2723 case MVT::i16: Opc = X86::MOV16rr; break;
2724 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002725 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002726 }
2727 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2728 }
2729 return;
2730 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002731 switch (N.getNumOperands()) {
2732 default:
2733 assert(0 && "Unknown return instruction!");
2734 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002735 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2736 N.getOperand(2).getValueType() == MVT::i32 &&
2737 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002738 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2739 Tmp1 = SelectExpr(N.getOperand(1));
2740 Tmp2 = SelectExpr(N.getOperand(2));
2741 } else {
2742 Tmp2 = SelectExpr(N.getOperand(2));
2743 Tmp1 = SelectExpr(N.getOperand(1));
2744 }
2745 Select(N.getOperand(0));
2746
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002747 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2748 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2749 // Declare that EAX & EDX are live on exit.
2750 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2751 .addReg(X86::ESP);
2752 break;
2753 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002754 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2755 Select(N.getOperand(0));
2756 Tmp1 = SelectExpr(N.getOperand(1));
2757 } else {
2758 Tmp1 = SelectExpr(N.getOperand(1));
2759 Select(N.getOperand(0));
2760 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002761 switch (N.getOperand(1).getValueType()) {
2762 default: assert(0 && "All other types should have been promoted!!");
2763 case MVT::f64:
2764 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2765 // Declare that top-of-stack is live on exit
2766 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2767 break;
2768 case MVT::i32:
2769 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2770 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2771 break;
2772 }
2773 break;
2774 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002775 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002776 break;
2777 }
2778 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2779 return;
2780 case ISD::BR: {
2781 Select(N.getOperand(0));
2782 MachineBasicBlock *Dest =
2783 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2784 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2785 return;
2786 }
2787
2788 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002789 MachineBasicBlock *Dest =
2790 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002791
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002792 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2793 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002794 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2795 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2796 Select(N.getOperand(0));
2797 Tmp1 = SelectExpr(N.getOperand(1));
2798 } else {
2799 Tmp1 = SelectExpr(N.getOperand(1));
2800 Select(N.getOperand(0));
2801 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002802 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2803 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2804 }
Chris Lattner11333092005-01-11 03:11:44 +00002805
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002806 return;
2807 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002808
Chris Lattner4df0de92005-01-17 00:00:33 +00002809 case ISD::LOAD:
2810 // If this load could be folded into the only using instruction, and if it
2811 // is safe to emit the instruction here, try to do so now.
2812 if (Node->hasNUsesOfValue(1, 0)) {
2813 SDOperand TheVal = N.getValue(0);
2814 SDNode *User = 0;
2815 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2816 assert(UI != Node->use_end() && "Didn't find use!");
2817 SDNode *UN = *UI;
2818 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2819 if (UN->getOperand(i) == TheVal) {
2820 User = UN;
2821 goto FoundIt;
2822 }
2823 }
2824 FoundIt:
2825 // Only handle unary operators right now.
2826 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00002827 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002828 SelectExpr(SDOperand(User, 0));
2829 return;
2830 }
2831 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002832 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00002833 SelectExpr(N);
2834 return;
2835
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002836 case ISD::EXTLOAD:
2837 case ISD::SEXTLOAD:
2838 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002839 case ISD::CALL:
2840 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00002841 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002842 SelectExpr(N);
2843 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002844
2845 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2846 // On X86, we can represent all types except for Bool and Float natively.
2847 X86AddressMode AM;
2848 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002849 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2850 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2851 && "Unsupported TRUNCSTORE for this target!");
2852
2853 if (StoredTy == MVT::i16) {
2854 // FIXME: This is here just to allow testing. X86 doesn't really have a
2855 // TRUNCSTORE i16 operation, but this is required for targets that do not
2856 // have 16-bit integer registers. We occasionally disable 16-bit integer
2857 // registers to test the promotion code.
2858 Select(N.getOperand(0));
2859 Tmp1 = SelectExpr(N.getOperand(1));
2860 SelectAddress(N.getOperand(2), AM);
2861
2862 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2863 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2864 return;
2865 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002866
2867 // Store of constant bool?
2868 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2869 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2870 Select(N.getOperand(0));
2871 SelectAddress(N.getOperand(2), AM);
2872 } else {
2873 SelectAddress(N.getOperand(2), AM);
2874 Select(N.getOperand(0));
2875 }
2876 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2877 return;
2878 }
2879
2880 switch (StoredTy) {
2881 default: assert(0 && "Cannot truncstore this type!");
2882 case MVT::i1: Opc = X86::MOV8mr; break;
2883 case MVT::f32: Opc = X86::FST32m; break;
2884 }
2885
2886 std::vector<std::pair<unsigned, unsigned> > RP;
2887 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2888 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2889 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2890 std::sort(RP.begin(), RP.end());
2891
2892 for (unsigned i = 0; i != 3; ++i)
2893 switch (RP[2-i].second) {
2894 default: assert(0 && "Unknown operand number!");
2895 case 0: Select(N.getOperand(0)); break;
2896 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2897 case 2: SelectAddress(N.getOperand(2), AM); break;
2898 }
2899
2900 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2901 return;
2902 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002903 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002904 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002905
2906 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2907 Opc = 0;
2908 switch (CN->getValueType(0)) {
2909 default: assert(0 && "Invalid type for operation!");
2910 case MVT::i1:
2911 case MVT::i8: Opc = X86::MOV8mi; break;
2912 case MVT::i16: Opc = X86::MOV16mi; break;
2913 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002914 case MVT::f64: break;
2915 }
2916 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002917 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2918 Select(N.getOperand(0));
2919 SelectAddress(N.getOperand(2), AM);
2920 } else {
2921 SelectAddress(N.getOperand(2), AM);
2922 Select(N.getOperand(0));
2923 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002924 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2925 return;
2926 }
2927 }
Chris Lattner837caa72005-01-11 23:21:30 +00002928
2929 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00002930 if (TryToFoldLoadOpStore(Node))
2931 return;
Chris Lattner837caa72005-01-11 23:21:30 +00002932
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002933 switch (N.getOperand(1).getValueType()) {
2934 default: assert(0 && "Cannot store this type!");
2935 case MVT::i1:
2936 case MVT::i8: Opc = X86::MOV8mr; break;
2937 case MVT::i16: Opc = X86::MOV16mr; break;
2938 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002939 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002940 }
Chris Lattner11333092005-01-11 03:11:44 +00002941
2942 std::vector<std::pair<unsigned, unsigned> > RP;
2943 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2944 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2945 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2946 std::sort(RP.begin(), RP.end());
2947
2948 for (unsigned i = 0; i != 3; ++i)
2949 switch (RP[2-i].second) {
2950 default: assert(0 && "Unknown operand number!");
2951 case 0: Select(N.getOperand(0)); break;
2952 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002953 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002954 }
2955
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002956 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2957 return;
2958 }
2959 case ISD::ADJCALLSTACKDOWN:
2960 case ISD::ADJCALLSTACKUP:
2961 Select(N.getOperand(0));
2962 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2963
2964 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2965 X86::ADJCALLSTACKUP;
2966 BuildMI(BB, Opc, 1).addImm(Tmp1);
2967 return;
Chris Lattner989de032005-01-11 06:14:36 +00002968 case ISD::MEMSET: {
2969 Select(N.getOperand(0)); // Select the chain.
2970 unsigned Align =
2971 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2972 if (Align == 0) Align = 1;
2973
2974 // Turn the byte code into # iterations
2975 unsigned CountReg;
2976 unsigned Opcode;
2977 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2978 unsigned Val = ValC->getValue() & 255;
2979
2980 // If the value is a constant, then we can potentially use larger sets.
2981 switch (Align & 3) {
2982 case 2: // WORD aligned
2983 CountReg = MakeReg(MVT::i32);
2984 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2985 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2986 } else {
2987 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2988 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2989 }
2990 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2991 Opcode = X86::REP_STOSW;
2992 break;
2993 case 0: // DWORD aligned
2994 CountReg = MakeReg(MVT::i32);
2995 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2996 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2997 } else {
2998 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2999 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3000 }
3001 Val = (Val << 8) | Val;
3002 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3003 Opcode = X86::REP_STOSD;
3004 break;
3005 default: // BYTE aligned
3006 CountReg = SelectExpr(Node->getOperand(3));
3007 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3008 Opcode = X86::REP_STOSB;
3009 break;
3010 }
3011 } else {
3012 // If it's not a constant value we are storing, just fall back. We could
3013 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3014 unsigned ValReg = SelectExpr(Node->getOperand(2));
3015 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3016 CountReg = SelectExpr(Node->getOperand(3));
3017 Opcode = X86::REP_STOSB;
3018 }
3019
3020 // No matter what the alignment is, we put the source in ESI, the
3021 // destination in EDI, and the count in ECX.
3022 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3023 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3024 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3025 BuildMI(BB, Opcode, 0);
3026 return;
3027 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003028 case ISD::MEMCPY:
3029 Select(N.getOperand(0)); // Select the chain.
3030 unsigned Align =
3031 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3032 if (Align == 0) Align = 1;
3033
3034 // Turn the byte code into # iterations
3035 unsigned CountReg;
3036 unsigned Opcode;
3037 switch (Align & 3) {
3038 case 2: // WORD aligned
3039 CountReg = MakeReg(MVT::i32);
3040 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3041 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3042 } else {
3043 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3044 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3045 }
3046 Opcode = X86::REP_MOVSW;
3047 break;
3048 case 0: // DWORD aligned
3049 CountReg = MakeReg(MVT::i32);
3050 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3051 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3052 } else {
3053 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3054 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3055 }
3056 Opcode = X86::REP_MOVSD;
3057 break;
3058 default: // BYTE aligned
3059 CountReg = SelectExpr(Node->getOperand(3));
3060 Opcode = X86::REP_MOVSB;
3061 break;
3062 }
3063
3064 // No matter what the alignment is, we put the source in ESI, the
3065 // destination in EDI, and the count in ECX.
3066 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3067 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3068 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3069 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3070 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3071 BuildMI(BB, Opcode, 0);
3072 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003073 }
3074 assert(0 && "Should not be reached!");
3075}
3076
3077
3078/// createX86PatternInstructionSelector - This pass converts an LLVM function
3079/// into a machine code representation using pattern matching and a machine
3080/// description file.
3081///
3082FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
3083 return new ISel(TM);
3084}