blob: 7a623e5b414ae4dc95a4f388d4ae54ce89488344 [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);
46
47 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000048 addRegisterClass(MVT::i8, X86::R8RegisterClass);
49 addRegisterClass(MVT::i16, X86::R16RegisterClass);
50 addRegisterClass(MVT::i32, X86::R32RegisterClass);
51 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
52
53 // FIXME: Eliminate these two classes when legalize can handle promotions
54 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000055/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
56/**/ //addRegisterClass(MVT::f32, X86::RFPRegisterClass);
57
58 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
59 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
60 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i16 , Expand);
61 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
62 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i1 , Expand);
63 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
64 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
65 setOperationAction(ISD::SREM , MVT::f64 , Expand);
66
67 // These should be promoted to a larger select which is supported.
68/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
69 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000070
71 computeRegisterProperties();
Chris Lattner8acb1ba2005-01-07 07:49:41 +000072
73 addLegalFPImmediate(+0.0); // FLD0
74 addLegalFPImmediate(+1.0); // FLD1
75 addLegalFPImmediate(-0.0); // FLD0/FCHS
76 addLegalFPImmediate(-1.0); // FLD1/FCHS
77 }
78
79 /// LowerArguments - This hook must be implemented to indicate how we should
80 /// lower the arguments for the specified function, into the specified DAG.
81 virtual std::vector<SDOperand>
82 LowerArguments(Function &F, SelectionDAG &DAG);
83
84 /// LowerCallTo - This hook lowers an abstract call to a function into an
85 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000086 virtual std::pair<SDOperand, SDOperand>
87 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
88 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000089
90 virtual std::pair<SDOperand, SDOperand>
91 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
92
93 virtual std::pair<SDOperand,SDOperand>
94 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
95 const Type *ArgTy, SelectionDAG &DAG);
96
97 virtual std::pair<SDOperand, SDOperand>
98 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
99 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000100 };
101}
102
103
104std::vector<SDOperand>
105X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
106 std::vector<SDOperand> ArgValues;
107
108 // Add DAG nodes to load the arguments... On entry to a function on the X86,
109 // the stack frame looks like this:
110 //
111 // [ESP] -- return address
112 // [ESP + 4] -- first argument (leftmost lexically)
113 // [ESP + 8] -- second argument, if first argument is four bytes in size
114 // ...
115 //
116 MachineFunction &MF = DAG.getMachineFunction();
117 MachineFrameInfo *MFI = MF.getFrameInfo();
118
119 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
120 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
121 MVT::ValueType ObjectVT = getValueType(I->getType());
122 unsigned ArgIncrement = 4;
123 unsigned ObjSize;
124 switch (ObjectVT) {
125 default: assert(0 && "Unhandled argument type!");
126 case MVT::i1:
127 case MVT::i8: ObjSize = 1; break;
128 case MVT::i16: ObjSize = 2; break;
129 case MVT::i32: ObjSize = 4; break;
130 case MVT::i64: ObjSize = ArgIncrement = 8; break;
131 case MVT::f32: ObjSize = 4; break;
132 case MVT::f64: ObjSize = ArgIncrement = 8; break;
133 }
134 // Create the frame index object for this incoming parameter...
135 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
136
137 // Create the SelectionDAG nodes corresponding to a load from this parameter
138 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
139
140 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
141 // dead loads.
142 SDOperand ArgValue;
143 if (!I->use_empty())
144 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
145 else {
146 if (MVT::isInteger(ObjectVT))
147 ArgValue = DAG.getConstant(0, ObjectVT);
148 else
149 ArgValue = DAG.getConstantFP(0, ObjectVT);
150 }
151 ArgValues.push_back(ArgValue);
152
153 ArgOffset += ArgIncrement; // Move on to the next argument...
154 }
155
156 // If the function takes variable number of arguments, make a frame index for
157 // the start of the first vararg value... for expansion of llvm.va_start.
158 if (F.isVarArg())
159 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000160 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000161 return ArgValues;
162}
163
Chris Lattner5188ad72005-01-08 19:28:19 +0000164std::pair<SDOperand, SDOperand>
165X86TargetLowering::LowerCallTo(SDOperand Chain,
166 const Type *RetTy, SDOperand Callee,
167 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000168 // Count how many bytes are to be pushed on the stack.
169 unsigned NumBytes = 0;
170
171 if (Args.empty()) {
172 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000173 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
174 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000175 } else {
176 for (unsigned i = 0, e = Args.size(); i != e; ++i)
177 switch (getValueType(Args[i].second)) {
178 default: assert(0 && "Unknown value type!");
179 case MVT::i1:
180 case MVT::i8:
181 case MVT::i16:
182 case MVT::i32:
183 case MVT::f32:
184 NumBytes += 4;
185 break;
186 case MVT::i64:
187 case MVT::f64:
188 NumBytes += 8;
189 break;
190 }
191
Chris Lattner5188ad72005-01-08 19:28:19 +0000192 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
193 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000194
195 // Arguments go on the stack in reverse order, as specified by the ABI.
196 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000197 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
198 DAG.getEntryNode());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000199 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
200 unsigned ArgReg;
201 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
202 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
203
204 switch (getValueType(Args[i].second)) {
205 default: assert(0 && "Unexpected ValueType for argument!");
206 case MVT::i1:
207 case MVT::i8:
208 case MVT::i16:
209 // Promote the integer to 32 bits. If the input type is signed use a
210 // sign extend, otherwise use a zero extend.
211 if (Args[i].second->isSigned())
212 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
213 else
214 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
215
216 // FALL THROUGH
217 case MVT::i32:
218 case MVT::f32:
219 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000220 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
221 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000222 ArgOffset += 4;
223 break;
224 case MVT::i64:
225 case MVT::f64:
226 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000227 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
228 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000229 ArgOffset += 8;
230 break;
231 }
232 }
233 }
234
235 std::vector<MVT::ValueType> RetVals;
236 MVT::ValueType RetTyVT = getValueType(RetTy);
237 if (RetTyVT != MVT::isVoid)
238 RetVals.push_back(RetTyVT);
239 RetVals.push_back(MVT::Other);
240
Chris Lattner5188ad72005-01-08 19:28:19 +0000241 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000242 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000243 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
244 DAG.getConstant(NumBytes, getPointerTy()));
245 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000246}
247
Chris Lattner14824582005-01-09 00:01:27 +0000248std::pair<SDOperand, SDOperand>
249X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
250 // vastart just returns the address of the VarArgsFrameIndex slot.
251 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
252}
253
254std::pair<SDOperand,SDOperand> X86TargetLowering::
255LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
256 const Type *ArgTy, SelectionDAG &DAG) {
257 MVT::ValueType ArgVT = getValueType(ArgTy);
258 SDOperand Result;
259 if (!isVANext) {
260 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
261 } else {
262 unsigned Amt;
263 if (ArgVT == MVT::i32)
264 Amt = 4;
265 else {
266 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
267 "Other types should have been promoted for varargs!");
268 Amt = 8;
269 }
270 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
271 DAG.getConstant(Amt, VAList.getValueType()));
272 }
273 return std::make_pair(Result, Chain);
274}
275
276
277std::pair<SDOperand, SDOperand> X86TargetLowering::
278LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
279 SelectionDAG &DAG) {
280 SDOperand Result;
281 if (Depth) // Depths > 0 not supported yet!
282 Result = DAG.getConstant(0, getPointerTy());
283 else {
284 if (ReturnAddrIndex == 0) {
285 // Set up a frame object for the return address.
286 MachineFunction &MF = DAG.getMachineFunction();
287 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
288 }
289
290 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
291
292 if (!isFrameAddress)
293 // Just load the return address
294 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
295 else
296 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
297 DAG.getConstant(4, MVT::i32));
298 }
299 return std::make_pair(Result, Chain);
300}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000301
302
303
304
305
306namespace {
307 Statistic<>
308 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
309
310 //===--------------------------------------------------------------------===//
311 /// ISel - X86 specific code to select X86 machine instructions for
312 /// SelectionDAG operations.
313 ///
314 class ISel : public SelectionDAGISel {
315 /// ContainsFPCode - Every instruction we select that uses or defines a FP
316 /// register should set this to true.
317 bool ContainsFPCode;
318
319 /// X86Lowering - This object fully describes how to lower LLVM code to an
320 /// X86-specific SelectionDAG.
321 X86TargetLowering X86Lowering;
322
Chris Lattner11333092005-01-11 03:11:44 +0000323 /// RegPressureMap - This keeps an approximate count of the number of
324 /// registers required to evaluate each node in the graph.
325 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000326
327 /// ExprMap - As shared expressions are codegen'd, we keep track of which
328 /// vreg the value is produced in, so we only emit one copy of each compiled
329 /// tree.
330 std::map<SDOperand, unsigned> ExprMap;
331 std::set<SDOperand> LoweredTokens;
332
333 public:
334 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
335 }
336
Chris Lattner11333092005-01-11 03:11:44 +0000337 unsigned getRegPressure(SDOperand O) {
338 return RegPressureMap[O.Val];
339 }
340 unsigned ComputeRegPressure(SDOperand O);
341
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000342 /// InstructionSelectBasicBlock - This callback is invoked by
343 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000344 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000345
Chris Lattner4ff348b2005-01-17 06:26:58 +0000346 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp);
Chris Lattnera5ade062005-01-11 21:19:59 +0000347 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
348
349
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000350 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000351 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000352 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
353 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000354 unsigned SelectExpr(SDOperand N);
355 bool SelectAddress(SDOperand N, X86AddressMode &AM);
356 void Select(SDOperand N);
357 };
358}
359
Chris Lattner7dbcb752005-01-12 04:21:28 +0000360/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
361/// when it has created a SelectionDAG for us to codegen.
362void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
363 // While we're doing this, keep track of whether we see any FP code for
364 // FP_REG_KILL insertion.
365 ContainsFPCode = false;
366
367 // Scan the PHI nodes that already are inserted into this basic block. If any
368 // of them is a PHI of a floating point value, we need to insert an
369 // FP_REG_KILL.
370 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
371 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
372 I != E; ++I) {
373 assert(I->getOpcode() == X86::PHI &&
374 "Isn't just PHI nodes?");
375 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
376 X86::RFPRegisterClass) {
377 ContainsFPCode = true;
378 break;
379 }
380 }
381
382 // Compute the RegPressureMap, which is an approximation for the number of
383 // registers required to compute each node.
384 ComputeRegPressure(DAG.getRoot());
385
386 // Codegen the basic block.
387 Select(DAG.getRoot());
388
389 // Finally, look at all of the successors of this block. If any contain a PHI
390 // node of FP type, we need to insert an FP_REG_KILL in this block.
391 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
392 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
393 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
394 I != E && I->getOpcode() == X86::PHI; ++I) {
395 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
396 X86::RFPRegisterClass) {
397 ContainsFPCode = true;
398 break;
399 }
400 }
401
402 // Insert FP_REG_KILL instructions into basic blocks that need them. This
403 // only occurs due to the floating point stackifier not being aggressive
404 // enough to handle arbitrary global stackification.
405 //
406 // Currently we insert an FP_REG_KILL instruction into each block that uses or
407 // defines a floating point virtual register.
408 //
409 // When the global register allocators (like linear scan) finally update live
410 // variable analysis, we can keep floating point values in registers across
411 // basic blocks. This will be a huge win, but we are waiting on the global
412 // allocators before we can do this.
413 //
414 if (ContainsFPCode && BB->succ_size()) {
415 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
416 ++NumFPKill;
417 }
418
419 // Clear state used for selection.
420 ExprMap.clear();
421 LoweredTokens.clear();
422 RegPressureMap.clear();
423}
424
425
Chris Lattner11333092005-01-11 03:11:44 +0000426// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
427// for the number of registers required to compute each node. This is basically
428// computing a generalized form of the Sethi-Ullman number for each node.
429unsigned ISel::ComputeRegPressure(SDOperand O) {
430 SDNode *N = O.Val;
431 unsigned &Result = RegPressureMap[N];
432 if (Result) return Result;
433
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000434 // FIXME: Should operations like CALL (which clobber lots o regs) have a
435 // higher fixed cost??
436
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000437 if (N->getNumOperands() == 0) {
438 Result = 1;
439 } else {
440 unsigned MaxRegUse = 0;
441 unsigned NumExtraMaxRegUsers = 0;
442 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
443 unsigned Regs;
444 if (N->getOperand(i).getOpcode() == ISD::Constant)
445 Regs = 0;
446 else
447 Regs = ComputeRegPressure(N->getOperand(i));
448 if (Regs > MaxRegUse) {
449 MaxRegUse = Regs;
450 NumExtraMaxRegUsers = 0;
451 } else if (Regs == MaxRegUse &&
452 N->getOperand(i).getValueType() != MVT::Other) {
453 ++NumExtraMaxRegUsers;
454 }
Chris Lattner11333092005-01-11 03:11:44 +0000455 }
Chris Lattner11333092005-01-11 03:11:44 +0000456
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000457 Result = MaxRegUse+NumExtraMaxRegUsers;
458 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000459
Chris Lattner837caa72005-01-11 23:21:30 +0000460 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000461 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000462}
463
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000464/// SelectAddress - Add the specified node to the specified addressing mode,
465/// returning true if it cannot be done.
466bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
467 switch (N.getOpcode()) {
468 default: break;
469 case ISD::FrameIndex:
470 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
471 AM.BaseType = X86AddressMode::FrameIndexBase;
472 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
473 return false;
474 }
475 break;
476 case ISD::GlobalAddress:
477 if (AM.GV == 0) {
478 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
479 return false;
480 }
481 break;
482 case ISD::Constant:
483 AM.Disp += cast<ConstantSDNode>(N)->getValue();
484 return false;
485 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000486 // We might have folded the load into this shift, so don't regen the value
487 // if so.
488 if (ExprMap.count(N)) break;
489
Chris Lattner2b937862005-01-12 07:33:20 +0000490 if (AM.IndexReg == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000491 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
492 unsigned Val = CN->getValue();
493 if (Val == 1 || Val == 2 || Val == 3) {
494 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000495 SDOperand ShVal = N.Val->getOperand(0);
496
497 // Okay, we know that we have a scale by now. However, if the scaled
498 // value is an add of something and a constant, we can fold the
499 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000500 if (ShVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(ShVal) &&
Chris Lattner51a26342005-01-11 06:36:20 +0000501 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
502 AM.IndexReg = SelectExpr(ShVal.Val->getOperand(0));
503 ConstantSDNode *AddVal =
504 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
505 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000506 } else {
Chris Lattner51a26342005-01-11 06:36:20 +0000507 AM.IndexReg = SelectExpr(ShVal);
508 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000509 return false;
510 }
511 }
512 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000513 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000514 // We might have folded the load into this mul, so don't regen the value if
515 // so.
516 if (ExprMap.count(N)) break;
517
Chris Lattner947d5442005-01-11 19:37:02 +0000518 // X*[3,5,9] -> X+X*[2,4,8]
519 if (AM.IndexReg == 0 && AM.BaseType == X86AddressMode::RegBase &&
520 AM.Base.Reg == 0)
521 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
522 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
523 AM.Scale = unsigned(CN->getValue())-1;
524
525 SDOperand MulVal = N.Val->getOperand(0);
526 unsigned Reg;
527
528 // Okay, we know that we have a scale by now. However, if the scaled
529 // value is an add of something and a constant, we can fold the
530 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000531 if (MulVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(MulVal) &&
Chris Lattner947d5442005-01-11 19:37:02 +0000532 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
533 Reg = SelectExpr(MulVal.Val->getOperand(0));
534 ConstantSDNode *AddVal =
535 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
536 AM.Disp += AddVal->getValue() * CN->getValue();
537 } else {
538 Reg = SelectExpr(N.Val->getOperand(0));
539 }
540
541 AM.IndexReg = AM.Base.Reg = Reg;
542 return false;
543 }
544 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000545
546 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000547 // We might have folded the load into this mul, so don't regen the value if
548 // so.
549 if (ExprMap.count(N)) break;
550
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000551 X86AddressMode Backup = AM;
552 if (!SelectAddress(N.Val->getOperand(0), AM) &&
553 !SelectAddress(N.Val->getOperand(1), AM))
554 return false;
555 AM = Backup;
Chris Lattner9bbd9922005-01-12 18:08:53 +0000556 if (!SelectAddress(N.Val->getOperand(1), AM) &&
557 !SelectAddress(N.Val->getOperand(0), AM))
558 return false;
559 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000560 break;
561 }
562 }
563
Chris Lattnera95589b2005-01-11 04:40:19 +0000564 // Is the base register already occupied?
565 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
566 // If so, check to see if the scale index register is set.
567 if (AM.IndexReg == 0) {
568 AM.IndexReg = SelectExpr(N);
569 AM.Scale = 1;
570 return false;
571 }
572
573 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000574 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000575 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000576
577 // Default, generate it as a register.
578 AM.BaseType = X86AddressMode::RegBase;
579 AM.Base.Reg = SelectExpr(N);
580 return false;
581}
582
583/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
584/// assuming that the temporary registers are in the 8-bit register class.
585///
586/// Tmp1 = setcc1
587/// Tmp2 = setcc2
588/// DestReg = logicalop Tmp1, Tmp2
589///
590static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
591 unsigned SetCC2, unsigned LogicalOp,
592 unsigned DestReg) {
593 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
594 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
595 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
596 BuildMI(BB, SetCC1, 0, Tmp1);
597 BuildMI(BB, SetCC2, 0, Tmp2);
598 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
599}
600
601/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
602/// condition codes match the specified SetCCOpcode. Note that some conditions
603/// require multiple instructions to generate the correct value.
604static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
605 ISD::CondCode SetCCOpcode, bool isFP) {
606 unsigned Opc;
607 if (!isFP) {
608 switch (SetCCOpcode) {
609 default: assert(0 && "Illegal integer SetCC!");
610 case ISD::SETEQ: Opc = X86::SETEr; break;
611 case ISD::SETGT: Opc = X86::SETGr; break;
612 case ISD::SETGE: Opc = X86::SETGEr; break;
613 case ISD::SETLT: Opc = X86::SETLr; break;
614 case ISD::SETLE: Opc = X86::SETLEr; break;
615 case ISD::SETNE: Opc = X86::SETNEr; break;
616 case ISD::SETULT: Opc = X86::SETBr; break;
617 case ISD::SETUGT: Opc = X86::SETAr; break;
618 case ISD::SETULE: Opc = X86::SETBEr; break;
619 case ISD::SETUGE: Opc = X86::SETAEr; break;
620 }
621 } else {
622 // On a floating point condition, the flags are set as follows:
623 // ZF PF CF op
624 // 0 | 0 | 0 | X > Y
625 // 0 | 0 | 1 | X < Y
626 // 1 | 0 | 0 | X == Y
627 // 1 | 1 | 1 | unordered
628 //
629 switch (SetCCOpcode) {
630 default: assert(0 && "Invalid FP setcc!");
631 case ISD::SETUEQ:
632 case ISD::SETEQ:
633 Opc = X86::SETEr; // True if ZF = 1
634 break;
635 case ISD::SETOGT:
636 case ISD::SETGT:
637 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
638 break;
639 case ISD::SETOGE:
640 case ISD::SETGE:
641 Opc = X86::SETAEr; // True if CF = 0
642 break;
643 case ISD::SETULT:
644 case ISD::SETLT:
645 Opc = X86::SETBr; // True if CF = 1
646 break;
647 case ISD::SETULE:
648 case ISD::SETLE:
649 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
650 break;
651 case ISD::SETONE:
652 case ISD::SETNE:
653 Opc = X86::SETNEr; // True if ZF = 0
654 break;
655 case ISD::SETUO:
656 Opc = X86::SETPr; // True if PF = 1
657 break;
658 case ISD::SETO:
659 Opc = X86::SETNPr; // True if PF = 0
660 break;
661 case ISD::SETOEQ: // !PF & ZF
662 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
663 return;
664 case ISD::SETOLT: // !PF & CF
665 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
666 return;
667 case ISD::SETOLE: // !PF & (CF || ZF)
668 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
669 return;
670 case ISD::SETUGT: // PF | (!ZF & !CF)
671 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
672 return;
673 case ISD::SETUGE: // PF | !CF
674 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
675 return;
676 case ISD::SETUNE: // PF | !ZF
677 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
678 return;
679 }
680 }
681 BuildMI(BB, Opc, 0, DestReg);
682}
683
684
685/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
686/// the Dest block if the Cond condition is true. If we cannot fold this
687/// condition into the branch, return true.
688///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000689bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
690 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000691 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
692 // B) using two conditional branches instead of one condbr, two setcc's, and
693 // an or.
694 if ((Cond.getOpcode() == ISD::OR ||
695 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
696 // And and or set the flags for us, so there is no need to emit a TST of the
697 // result. It is only safe to do this if there is only a single use of the
698 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000699 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000700 SelectExpr(Cond);
701 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
702 return false;
703 }
704
705 // Codegen br not C -> JE.
706 if (Cond.getOpcode() == ISD::XOR)
707 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
708 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000709 unsigned CondR;
710 if (getRegPressure(Chain) > getRegPressure(Cond)) {
711 Select(Chain);
712 CondR = SelectExpr(Cond.Val->getOperand(0));
713 } else {
714 CondR = SelectExpr(Cond.Val->getOperand(0));
715 Select(Chain);
716 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000717 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
718 BuildMI(BB, X86::JE, 1).addMBB(Dest);
719 return false;
720 }
721
722 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
723 if (SetCC == 0)
724 return true; // Can only handle simple setcc's so far.
725
726 unsigned Opc;
727
728 // Handle integer conditions first.
729 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
730 switch (SetCC->getCondition()) {
731 default: assert(0 && "Illegal integer SetCC!");
732 case ISD::SETEQ: Opc = X86::JE; break;
733 case ISD::SETGT: Opc = X86::JG; break;
734 case ISD::SETGE: Opc = X86::JGE; break;
735 case ISD::SETLT: Opc = X86::JL; break;
736 case ISD::SETLE: Opc = X86::JLE; break;
737 case ISD::SETNE: Opc = X86::JNE; break;
738 case ISD::SETULT: Opc = X86::JB; break;
739 case ISD::SETUGT: Opc = X86::JA; break;
740 case ISD::SETULE: Opc = X86::JBE; break;
741 case ISD::SETUGE: Opc = X86::JAE; break;
742 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000743 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000744 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000745 BuildMI(BB, Opc, 1).addMBB(Dest);
746 return false;
747 }
748
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000749 unsigned Opc2 = 0; // Second branch if needed.
750
751 // On a floating point condition, the flags are set as follows:
752 // ZF PF CF op
753 // 0 | 0 | 0 | X > Y
754 // 0 | 0 | 1 | X < Y
755 // 1 | 0 | 0 | X == Y
756 // 1 | 1 | 1 | unordered
757 //
758 switch (SetCC->getCondition()) {
759 default: assert(0 && "Invalid FP setcc!");
760 case ISD::SETUEQ:
761 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
762 case ISD::SETOGT:
763 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
764 case ISD::SETOGE:
765 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
766 case ISD::SETULT:
767 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
768 case ISD::SETULE:
769 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
770 case ISD::SETONE:
771 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
772 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
773 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
774 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
775 Opc = X86::JA; // ZF = 0 & CF = 0
776 Opc2 = X86::JP; // PF = 1
777 break;
778 case ISD::SETUGE: // PF = 1 | CF = 0
779 Opc = X86::JAE; // CF = 0
780 Opc2 = X86::JP; // PF = 1
781 break;
782 case ISD::SETUNE: // PF = 1 | ZF = 0
783 Opc = X86::JNE; // ZF = 0
784 Opc2 = X86::JP; // PF = 1
785 break;
786 case ISD::SETOEQ: // PF = 0 & ZF = 1
787 //X86::JNP, X86::JE
788 //X86::AND8rr
789 return true; // FIXME: Emit more efficient code for this branch.
790 case ISD::SETOLT: // PF = 0 & CF = 1
791 //X86::JNP, X86::JB
792 //X86::AND8rr
793 return true; // FIXME: Emit more efficient code for this branch.
794 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
795 //X86::JNP, X86::JBE
796 //X86::AND8rr
797 return true; // FIXME: Emit more efficient code for this branch.
798 }
799
Chris Lattner6c07aee2005-01-11 04:06:27 +0000800 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000801 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000802 BuildMI(BB, Opc, 1).addMBB(Dest);
803 if (Opc2)
804 BuildMI(BB, Opc2, 1).addMBB(Dest);
805 return false;
806}
807
Chris Lattner24aad1b2005-01-10 22:10:13 +0000808/// EmitSelectCC - Emit code into BB that performs a select operation between
809/// the two registers RTrue and RFalse, generating a result into RDest. Return
810/// true if the fold cannot be performed.
811///
812void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
813 unsigned RTrue, unsigned RFalse, unsigned RDest) {
814 enum Condition {
815 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
816 NOT_SET
817 } CondCode = NOT_SET;
818
819 static const unsigned CMOVTAB16[] = {
820 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
821 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
822 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
823 };
824 static const unsigned CMOVTAB32[] = {
825 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
826 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
827 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
828 };
829 static const unsigned CMOVTABFP[] = {
830 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
831 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
832 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
833 };
834
835 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
836 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
837 switch (SetCC->getCondition()) {
838 default: assert(0 && "Unknown integer comparison!");
839 case ISD::SETEQ: CondCode = EQ; break;
840 case ISD::SETGT: CondCode = GT; break;
841 case ISD::SETGE: CondCode = GE; break;
842 case ISD::SETLT: CondCode = LT; break;
843 case ISD::SETLE: CondCode = LE; break;
844 case ISD::SETNE: CondCode = NE; break;
845 case ISD::SETULT: CondCode = B; break;
846 case ISD::SETUGT: CondCode = A; break;
847 case ISD::SETULE: CondCode = BE; break;
848 case ISD::SETUGE: CondCode = AE; break;
849 }
850 } else {
851 // On a floating point condition, the flags are set as follows:
852 // ZF PF CF op
853 // 0 | 0 | 0 | X > Y
854 // 0 | 0 | 1 | X < Y
855 // 1 | 0 | 0 | X == Y
856 // 1 | 1 | 1 | unordered
857 //
858 switch (SetCC->getCondition()) {
859 default: assert(0 && "Unknown FP comparison!");
860 case ISD::SETUEQ:
861 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
862 case ISD::SETOGT:
863 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
864 case ISD::SETOGE:
865 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
866 case ISD::SETULT:
867 case ISD::SETLT: CondCode = B; break; // True if CF = 1
868 case ISD::SETULE:
869 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
870 case ISD::SETONE:
871 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
872 case ISD::SETUO: CondCode = P; break; // True if PF = 1
873 case ISD::SETO: CondCode = NP; break; // True if PF = 0
874 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
875 case ISD::SETUGE: // PF = 1 | CF = 0
876 case ISD::SETUNE: // PF = 1 | ZF = 0
877 case ISD::SETOEQ: // PF = 0 & ZF = 1
878 case ISD::SETOLT: // PF = 0 & CF = 1
879 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
880 // We cannot emit this comparison as a single cmov.
881 break;
882 }
883 }
884 }
885
886 unsigned Opc = 0;
887 if (CondCode != NOT_SET) {
888 switch (SVT) {
889 default: assert(0 && "Cannot select this type!");
890 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
891 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
892 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000893 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000894 }
895 }
896
897 // Finally, if we weren't able to fold this, just emit the condition and test
898 // it.
899 if (CondCode == NOT_SET || Opc == 0) {
900 // Get the condition into the zero flag.
901 unsigned CondReg = SelectExpr(Cond);
902 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
903
904 switch (SVT) {
905 default: assert(0 && "Cannot select this type!");
906 case MVT::i16: Opc = X86::CMOVE16rr; break;
907 case MVT::i32: Opc = X86::CMOVE32rr; break;
908 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000909 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000910 }
911 } else {
912 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000913 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000914 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000915 }
916 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
917}
918
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000919void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +0000920 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000921 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
922 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +0000923 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +0000924 switch (RHS.getValueType()) {
925 default: break;
926 case MVT::i1:
927 case MVT::i8: Opc = X86::CMP8mi; break;
928 case MVT::i16: Opc = X86::CMP16mi; break;
929 case MVT::i32: Opc = X86::CMP32mi; break;
930 }
931 if (Opc) {
932 X86AddressMode AM;
933 EmitFoldedLoad(LHS, AM);
934 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
935 return;
936 }
937 }
938
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000939 switch (RHS.getValueType()) {
940 default: break;
941 case MVT::i1:
942 case MVT::i8: Opc = X86::CMP8ri; break;
943 case MVT::i16: Opc = X86::CMP16ri; break;
944 case MVT::i32: Opc = X86::CMP32ri; break;
945 }
946 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000947 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000948 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
949 return;
950 }
Chris Lattner7f2afac2005-01-14 22:37:41 +0000951 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
952 if (CN->isExactlyValue(+0.0) ||
953 CN->isExactlyValue(-0.0)) {
954 unsigned Reg = SelectExpr(LHS);
955 BuildMI(BB, X86::FTST, 1).addReg(Reg);
956 BuildMI(BB, X86::FNSTSW8r, 0);
957 BuildMI(BB, X86::SAHF, 1);
958 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000959 }
960
Chris Lattneref6806c2005-01-12 02:02:48 +0000961 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +0000962 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +0000963 switch (RHS.getValueType()) {
964 default: break;
965 case MVT::i1:
966 case MVT::i8: Opc = X86::CMP8mr; break;
967 case MVT::i16: Opc = X86::CMP16mr; break;
968 case MVT::i32: Opc = X86::CMP32mr; break;
969 }
970 if (Opc) {
971 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +0000972 EmitFoldedLoad(LHS, AM);
973 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +0000974 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
975 return;
976 }
977 }
978
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000979 switch (LHS.getValueType()) {
980 default: assert(0 && "Cannot compare this value!");
981 case MVT::i1:
982 case MVT::i8: Opc = X86::CMP8rr; break;
983 case MVT::i16: Opc = X86::CMP16rr; break;
984 case MVT::i32: Opc = X86::CMP32rr; break;
985 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000986 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000987 }
Chris Lattner11333092005-01-11 03:11:44 +0000988 unsigned Tmp1, Tmp2;
989 if (getRegPressure(LHS) > getRegPressure(RHS)) {
990 Tmp1 = SelectExpr(LHS);
991 Tmp2 = SelectExpr(RHS);
992 } else {
993 Tmp2 = SelectExpr(RHS);
994 Tmp1 = SelectExpr(LHS);
995 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000996 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
997}
998
Chris Lattner4ff348b2005-01-17 06:26:58 +0000999/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1000/// The DAG cannot have cycles in it, by definition, so the visited set is not
1001/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1002/// reuse, so it prevents exponential cases.
1003///
1004static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1005 std::set<SDNode*> &Visited) {
1006 if (N == Op) return true; // Found it.
1007 SDNode *Node = N.Val;
1008 if (Node->getNumOperands() == 0) return false; // Leaf?
1009 if (!Visited.insert(Node).second) return false; // Already visited?
1010
1011 // Recurse for the first N-1 operands.
1012 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1013 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1014 return true;
1015
1016 // Tail recurse for the last operand.
1017 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1018}
1019
Chris Lattnera5ade062005-01-11 21:19:59 +00001020/// isFoldableLoad - Return true if this is a load instruction that can safely
1021/// be folded into an operation that uses it.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001022bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001023 if (Op.getOpcode() != ISD::LOAD ||
1024 // FIXME: currently can't fold constant pool indexes.
1025 isa<ConstantPoolSDNode>(Op.getOperand(1)))
1026 return false;
1027
1028 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001029 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1030 if (ExprMap.count(Op.getValue(1))) return false;
1031 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
1032 assert(!LoweredTokens.count(Op.getValue(1)) &&
1033 "Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001034
Chris Lattner4ff348b2005-01-17 06:26:58 +00001035 // If there is not just one use of its value, we cannot fold.
1036 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1037
1038 // Finally, we cannot fold the load into the operation if this would induce a
1039 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1040 // operand of the operation we are folding the load into) can possible use the
1041 // chain node defined by the load.
1042 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1043 std::set<SDNode*> Visited;
1044 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1045 return false;
1046 }
1047 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001048}
1049
Chris Lattner4ff348b2005-01-17 06:26:58 +00001050
Chris Lattnera5ade062005-01-11 21:19:59 +00001051/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1052/// and compute the address being loaded into AM.
1053void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1054 SDOperand Chain = Op.getOperand(0);
1055 SDOperand Address = Op.getOperand(1);
1056 if (getRegPressure(Chain) > getRegPressure(Address)) {
1057 Select(Chain);
1058 SelectAddress(Address, AM);
1059 } else {
1060 SelectAddress(Address, AM);
1061 Select(Chain);
1062 }
1063
1064 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001065 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1066 "Load emitted more than once?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001067 ExprMap[SDOperand(Op.Val, 1)] = 1;
Chris Lattner636e79a2005-01-13 05:53:16 +00001068 if (!LoweredTokens.insert(Op.getValue(1)).second)
1069 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001070}
1071
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001072unsigned ISel::SelectExpr(SDOperand N) {
1073 unsigned Result;
1074 unsigned Tmp1, Tmp2, Tmp3;
1075 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001076 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001077 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001078
Chris Lattner7f2afac2005-01-14 22:37:41 +00001079 if (Node->getOpcode() == ISD::CopyFromReg) {
1080 // FIXME: Handle copy from physregs!
1081
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001082 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001083 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001084 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001085
1086 unsigned &Reg = ExprMap[N];
1087 if (Reg) return Reg;
1088
1089 if (N.getOpcode() != ISD::CALL)
1090 Reg = Result = (N.getValueType() != MVT::Other) ?
1091 MakeReg(N.getValueType()) : 1;
1092 else {
1093 // If this is a call instruction, make sure to prepare ALL of the result
1094 // values as well as the chain.
1095 if (Node->getNumValues() == 1)
1096 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001097 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001098 Result = MakeReg(Node->getValueType(0));
1099 ExprMap[N.getValue(0)] = Result;
1100 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1101 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1102 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001103 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001104 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001105
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001106 switch (N.getOpcode()) {
1107 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001108 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001109 assert(0 && "Node not handled!\n");
1110 case ISD::FrameIndex:
1111 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1112 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1113 return Result;
1114 case ISD::ConstantPool:
1115 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1116 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1117 return Result;
1118 case ISD::ConstantFP:
1119 ContainsFPCode = true;
1120 Tmp1 = Result; // Intermediate Register
1121 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1122 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1123 Tmp1 = MakeReg(MVT::f64);
1124
1125 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1126 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1127 BuildMI(BB, X86::FLD0, 0, Tmp1);
1128 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1129 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1130 BuildMI(BB, X86::FLD1, 0, Tmp1);
1131 else
1132 assert(0 && "Unexpected constant!");
1133 if (Tmp1 != Result)
1134 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1135 return Result;
1136 case ISD::Constant:
1137 switch (N.getValueType()) {
1138 default: assert(0 && "Cannot use constants of this type!");
1139 case MVT::i1:
1140 case MVT::i8: Opc = X86::MOV8ri; break;
1141 case MVT::i16: Opc = X86::MOV16ri; break;
1142 case MVT::i32: Opc = X86::MOV32ri; break;
1143 }
1144 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1145 return Result;
1146 case ISD::GlobalAddress: {
1147 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1148 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1149 return Result;
1150 }
1151 case ISD::ExternalSymbol: {
1152 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1153 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1154 return Result;
1155 }
1156 case ISD::FP_EXTEND:
1157 Tmp1 = SelectExpr(N.getOperand(0));
1158 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001159 return Result;
1160 case ISD::ZERO_EXTEND: {
1161 int DestIs16 = N.getValueType() == MVT::i16;
1162 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001163
1164 // FIXME: This hack is here for zero extension casts from bool to i8. This
1165 // would not be needed if bools were promoted by Legalize.
1166 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001167 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001168 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1169 return Result;
1170 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001171
Chris Lattner4ff348b2005-01-17 06:26:58 +00001172 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001173 static const unsigned Opc[3] = {
1174 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1175 };
1176
1177 X86AddressMode AM;
1178 EmitFoldedLoad(N.getOperand(0), AM);
1179 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1180
1181 return Result;
1182 }
1183
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001184 static const unsigned Opc[3] = {
1185 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1186 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001187 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001188 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1189 return Result;
1190 }
1191 case ISD::SIGN_EXTEND: {
1192 int DestIs16 = N.getValueType() == MVT::i16;
1193 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1194
Chris Lattner590d8002005-01-09 18:52:44 +00001195 // FIXME: Legalize should promote bools to i8!
1196 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1197 "Sign extend from bool not implemented!");
1198
Chris Lattner4ff348b2005-01-17 06:26:58 +00001199 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001200 static const unsigned Opc[3] = {
1201 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1202 };
1203
1204 X86AddressMode AM;
1205 EmitFoldedLoad(N.getOperand(0), AM);
1206 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1207 return Result;
1208 }
1209
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001210 static const unsigned Opc[3] = {
1211 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1212 };
1213 Tmp1 = SelectExpr(N.getOperand(0));
1214 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1215 return Result;
1216 }
1217 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001218 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001219 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001220 switch (N.getValueType()) {
1221 default: assert(0 && "Unknown truncate!");
1222 case MVT::i1:
1223 case MVT::i8: Opc = X86::MOV8rm; break;
1224 case MVT::i16: Opc = X86::MOV16rm; break;
1225 }
1226 X86AddressMode AM;
1227 EmitFoldedLoad(N.getOperand(0), AM);
1228 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1229 return Result;
1230 }
1231
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001232 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1233 // a move out of AX or AL.
1234 switch (N.getOperand(0).getValueType()) {
1235 default: assert(0 && "Unknown truncate!");
1236 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1237 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1238 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1239 }
1240 Tmp1 = SelectExpr(N.getOperand(0));
1241 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1242
1243 switch (N.getValueType()) {
1244 default: assert(0 && "Unknown truncate!");
1245 case MVT::i1:
1246 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1247 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1248 }
1249 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1250 return Result;
1251
1252 case ISD::FP_ROUND:
1253 // Truncate from double to float by storing to memory as float,
1254 // then reading it back into a register.
1255
1256 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001257 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001258 Tmp1 = TLI.getTargetData().getFloatAlignment();
1259 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1260
1261 // Codegen the input.
1262 Tmp1 = SelectExpr(N.getOperand(0));
1263
1264 // Emit the store, then the reload.
1265 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1266 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001267 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001268
1269 case ISD::SINT_TO_FP:
1270 case ISD::UINT_TO_FP: {
1271 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001272 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001273
1274 // Promote the integer to a type supported by FLD. We do this because there
1275 // are no unsigned FLD instructions, so we must promote an unsigned value to
1276 // a larger signed value, then use FLD on the larger value.
1277 //
1278 MVT::ValueType PromoteType = MVT::Other;
1279 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1280 unsigned PromoteOpcode = 0;
1281 unsigned RealDestReg = Result;
1282 switch (SrcTy) {
1283 case MVT::i1:
1284 case MVT::i8:
1285 // We don't have the facilities for directly loading byte sized data from
1286 // memory (even signed). Promote it to 16 bits.
1287 PromoteType = MVT::i16;
1288 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1289 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1290 break;
1291 case MVT::i16:
1292 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1293 PromoteType = MVT::i32;
1294 PromoteOpcode = X86::MOVZX32rr16;
1295 }
1296 break;
1297 default:
1298 // Don't fild into the real destination.
1299 if (Node->getOpcode() == ISD::UINT_TO_FP)
1300 Result = MakeReg(Node->getValueType(0));
1301 break;
1302 }
1303
1304 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1305
1306 if (PromoteType != MVT::Other) {
1307 Tmp2 = MakeReg(PromoteType);
1308 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1309 SrcTy = PromoteType;
1310 Tmp1 = Tmp2;
1311 }
1312
1313 // Spill the integer to memory and reload it from there.
1314 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1315 MachineFunction *F = BB->getParent();
1316 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1317
1318 switch (SrcTy) {
1319 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001320 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001321 // FIXME: this won't work for cast [u]long to FP
1322 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1323 FrameIdx).addReg(Tmp1);
1324 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1325 FrameIdx, 4).addReg(Tmp1+1);
1326 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1327 break;
1328 case MVT::i32:
1329 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1330 FrameIdx).addReg(Tmp1);
1331 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1332 break;
1333 case MVT::i16:
1334 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1335 FrameIdx).addReg(Tmp1);
1336 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1337 break;
1338 default: break; // No promotion required.
1339 }
1340
Chris Lattner085c9952005-01-12 04:00:00 +00001341 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001342 // If this is a cast from uint -> double, we need to be careful when if
1343 // the "sign" bit is set. If so, we don't want to make a negative number,
1344 // we want to make a positive number. Emit code to add an offset if the
1345 // sign bit is set.
1346
1347 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1348 unsigned IsNeg = MakeReg(MVT::i32);
1349 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1350
1351 // Create a CP value that has the offset in one word and 0 in the other.
1352 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1353 0x4f80000000000000ULL);
1354 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1355 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1356 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1357
1358 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1359 // We need special handling for unsigned 64-bit integer sources. If the
1360 // input number has the "sign bit" set, then we loaded it incorrectly as a
1361 // negative 64-bit number. In this case, add an offset value.
1362
1363 // Emit a test instruction to see if the dynamic input value was signed.
1364 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1365
1366 // If the sign bit is set, get a pointer to an offset, otherwise get a
1367 // pointer to a zero.
1368 MachineConstantPool *CP = F->getConstantPool();
1369 unsigned Zero = MakeReg(MVT::i32);
1370 Constant *Null = Constant::getNullValue(Type::UIntTy);
1371 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1372 CP->getConstantPoolIndex(Null));
1373 unsigned Offset = MakeReg(MVT::i32);
1374 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1375
1376 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1377 CP->getConstantPoolIndex(OffsetCst));
1378 unsigned Addr = MakeReg(MVT::i32);
1379 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1380
1381 // Load the constant for an add. FIXME: this could make an 'fadd' that
1382 // reads directly from memory, but we don't support these yet.
1383 unsigned ConstReg = MakeReg(MVT::f64);
1384 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1385
1386 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1387 }
1388 return RealDestReg;
1389 }
1390 case ISD::FP_TO_SINT:
1391 case ISD::FP_TO_UINT: {
1392 // FIXME: Most of this grunt work should be done by legalize!
1393 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1394
1395 // Change the floating point control register to use "round towards zero"
1396 // mode when truncating to an integer value.
1397 //
1398 MachineFunction *F = BB->getParent();
1399 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1400 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1401
1402 // Load the old value of the high byte of the control word...
1403 unsigned HighPartOfCW = MakeReg(MVT::i8);
1404 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1405 CWFrameIdx, 1);
1406
1407 // Set the high part to be round to zero...
1408 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1409 CWFrameIdx, 1).addImm(12);
1410
1411 // Reload the modified control word now...
1412 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1413
1414 // Restore the memory image of control word to original value
1415 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1416 CWFrameIdx, 1).addReg(HighPartOfCW);
1417
1418 // We don't have the facilities for directly storing byte sized data to
1419 // memory. Promote it to 16 bits. We also must promote unsigned values to
1420 // larger classes because we only have signed FP stores.
1421 MVT::ValueType StoreClass = Node->getValueType(0);
1422 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1423 switch (StoreClass) {
1424 case MVT::i8: StoreClass = MVT::i16; break;
1425 case MVT::i16: StoreClass = MVT::i32; break;
1426 case MVT::i32: StoreClass = MVT::i64; break;
1427 // The following treatment of cLong may not be perfectly right,
1428 // but it survives chains of casts of the form
1429 // double->ulong->double.
1430 case MVT::i64: StoreClass = MVT::i64; break;
1431 default: assert(0 && "Unknown store class!");
1432 }
1433
1434 // Spill the integer to memory and reload it from there.
1435 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1436 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1437
1438 switch (StoreClass) {
1439 default: assert(0 && "Unknown store class!");
1440 case MVT::i16:
1441 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1442 break;
1443 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001444 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001445 break;
1446 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001447 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001448 break;
1449 }
1450
1451 switch (Node->getValueType(0)) {
1452 default:
1453 assert(0 && "Unknown integer type!");
1454 case MVT::i64:
1455 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001456 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001457 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1458 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1459 case MVT::i32:
1460 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1461 break;
1462 case MVT::i16:
1463 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1464 break;
1465 case MVT::i8:
1466 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1467 break;
1468 }
1469
1470 // Reload the original control word now.
1471 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1472 return Result;
1473 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001474 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001475 Op0 = N.getOperand(0);
1476 Op1 = N.getOperand(1);
1477
Chris Lattner4ff348b2005-01-17 06:26:58 +00001478 if (isFoldableLoad(Op0, Op1)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001479 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001480 goto FoldAdd;
1481 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001482
Chris Lattner4ff348b2005-01-17 06:26:58 +00001483 if (isFoldableLoad(Op1, Op0)) {
1484 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001485 switch (N.getValueType()) {
1486 default: assert(0 && "Cannot add this type!");
1487 case MVT::i1:
1488 case MVT::i8: Opc = X86::ADD8rm; break;
1489 case MVT::i16: Opc = X86::ADD16rm; break;
1490 case MVT::i32: Opc = X86::ADD32rm; break;
1491 case MVT::f32: Opc = X86::FADD32m; break;
1492 case MVT::f64: Opc = X86::FADD64m; break;
1493 }
1494 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001495 EmitFoldedLoad(Op1, AM);
1496 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001497 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1498 return Result;
1499 }
1500
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001501 // See if we can codegen this as an LEA to fold operations together.
1502 if (N.getValueType() == MVT::i32) {
1503 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001504 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001505 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001506 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001507 // leave this as LEA, then peephole it to 'ADD' after two address elim
1508 // happens.
1509 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001510 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001511 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1512 return Result;
1513 }
1514 }
1515 }
Chris Lattner11333092005-01-11 03:11:44 +00001516
Chris Lattnera5ade062005-01-11 21:19:59 +00001517 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001518 Opc = 0;
1519 if (CN->getValue() == 1) { // add X, 1 -> inc X
1520 switch (N.getValueType()) {
1521 default: assert(0 && "Cannot integer add this type!");
1522 case MVT::i8: Opc = X86::INC8r; break;
1523 case MVT::i16: Opc = X86::INC16r; break;
1524 case MVT::i32: Opc = X86::INC32r; break;
1525 }
1526 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1527 switch (N.getValueType()) {
1528 default: assert(0 && "Cannot integer add this type!");
1529 case MVT::i8: Opc = X86::DEC8r; break;
1530 case MVT::i16: Opc = X86::DEC16r; break;
1531 case MVT::i32: Opc = X86::DEC32r; break;
1532 }
1533 }
1534
1535 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001536 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001537 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1538 return Result;
1539 }
1540
1541 switch (N.getValueType()) {
1542 default: assert(0 && "Cannot add this type!");
1543 case MVT::i8: Opc = X86::ADD8ri; break;
1544 case MVT::i16: Opc = X86::ADD16ri; break;
1545 case MVT::i32: Opc = X86::ADD32ri; break;
1546 }
1547 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001548 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001549 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1550 return Result;
1551 }
1552 }
1553
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001554 switch (N.getValueType()) {
1555 default: assert(0 && "Cannot add this type!");
1556 case MVT::i8: Opc = X86::ADD8rr; break;
1557 case MVT::i16: Opc = X86::ADD16rr; break;
1558 case MVT::i32: Opc = X86::ADD32rr; break;
1559 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001560 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001561 }
Chris Lattner11333092005-01-11 03:11:44 +00001562
Chris Lattnera5ade062005-01-11 21:19:59 +00001563 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1564 Tmp1 = SelectExpr(Op0);
1565 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001566 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001567 Tmp2 = SelectExpr(Op1);
1568 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001569 }
1570
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001571 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1572 return Result;
1573 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001574 case ISD::MUL:
1575 case ISD::AND:
1576 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001577 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001578 static const unsigned SUBTab[] = {
1579 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1580 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1581 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1582 };
1583 static const unsigned MULTab[] = {
1584 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1585 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1586 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1587 };
1588 static const unsigned ANDTab[] = {
1589 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1590 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1591 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1592 };
1593 static const unsigned ORTab[] = {
1594 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1595 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1596 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1597 };
1598 static const unsigned XORTab[] = {
1599 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1600 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1601 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1602 };
1603
1604 Op0 = Node->getOperand(0);
1605 Op1 = Node->getOperand(1);
1606
1607 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001608 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1609 if (CN->isNullValue()) { // 0 - N -> neg N
1610 switch (N.getValueType()) {
1611 default: assert(0 && "Cannot sub this type!");
1612 case MVT::i1:
1613 case MVT::i8: Opc = X86::NEG8r; break;
1614 case MVT::i16: Opc = X86::NEG16r; break;
1615 case MVT::i32: Opc = X86::NEG32r; break;
1616 }
1617 Tmp1 = SelectExpr(N.getOperand(1));
1618 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1619 return Result;
1620 }
1621
Chris Lattnera5ade062005-01-11 21:19:59 +00001622 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1623 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001624 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001625 switch (N.getValueType()) {
1626 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001627 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001628 case MVT::i8: Opc = X86::NOT8r; break;
1629 case MVT::i16: Opc = X86::NOT16r; break;
1630 case MVT::i32: Opc = X86::NOT32r; break;
1631 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001632 if (Opc) {
1633 Tmp1 = SelectExpr(Op0);
1634 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1635 return Result;
1636 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001637 }
1638
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001639 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001640 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001641 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001642 case MVT::i8: Opc = 0; break;
1643 case MVT::i16: Opc = 1; break;
1644 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001645 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001646 switch (Node->getOpcode()) {
1647 default: assert(0 && "Unreachable!");
1648 case ISD::SUB: Opc = SUBTab[Opc]; break;
1649 case ISD::MUL: Opc = MULTab[Opc]; break;
1650 case ISD::AND: Opc = ANDTab[Opc]; break;
1651 case ISD::OR: Opc = ORTab[Opc]; break;
1652 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001653 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001654 if (Opc) { // Can't fold MUL:i8 R, imm
1655 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001656 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1657 return Result;
1658 }
1659 }
Chris Lattner11333092005-01-11 03:11:44 +00001660
Chris Lattner4ff348b2005-01-17 06:26:58 +00001661 if (isFoldableLoad(Op0, Op1))
Chris Lattnera5ade062005-01-11 21:19:59 +00001662 if (Node->getOpcode() != ISD::SUB) {
1663 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001664 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001665 } else {
1666 // Emit 'reverse' subract, with a memory operand.
1667 switch (N.getValueType()) {
1668 default: Opc = 0; break;
1669 case MVT::f32: Opc = X86::FSUBR32m; break;
1670 case MVT::f64: Opc = X86::FSUBR64m; break;
1671 }
1672 if (Opc) {
1673 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001674 EmitFoldedLoad(Op0, AM);
1675 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001676 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1677 return Result;
1678 }
1679 }
1680
Chris Lattner4ff348b2005-01-17 06:26:58 +00001681 if (isFoldableLoad(Op1, Op0)) {
1682 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00001683 switch (N.getValueType()) {
1684 default: assert(0 && "Cannot operate on this type!");
1685 case MVT::i1:
1686 case MVT::i8: Opc = 5; break;
1687 case MVT::i16: Opc = 6; break;
1688 case MVT::i32: Opc = 7; break;
1689 case MVT::f32: Opc = 8; break;
1690 case MVT::f64: Opc = 9; break;
1691 }
1692 switch (Node->getOpcode()) {
1693 default: assert(0 && "Unreachable!");
1694 case ISD::SUB: Opc = SUBTab[Opc]; break;
1695 case ISD::MUL: Opc = MULTab[Opc]; break;
1696 case ISD::AND: Opc = ANDTab[Opc]; break;
1697 case ISD::OR: Opc = ORTab[Opc]; break;
1698 case ISD::XOR: Opc = XORTab[Opc]; break;
1699 }
1700
1701 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001702 EmitFoldedLoad(Op1, AM);
1703 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001704 if (Opc) {
1705 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1706 } else {
1707 assert(Node->getOpcode() == ISD::MUL &&
1708 N.getValueType() == MVT::i8 && "Unexpected situation!");
1709 // Must use the MUL instruction, which forces use of AL.
1710 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1711 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1712 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1713 }
1714 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001715 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001716
1717 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1718 Tmp1 = SelectExpr(Op0);
1719 Tmp2 = SelectExpr(Op1);
1720 } else {
1721 Tmp2 = SelectExpr(Op1);
1722 Tmp1 = SelectExpr(Op0);
1723 }
1724
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001725 switch (N.getValueType()) {
1726 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001727 case MVT::i1:
1728 case MVT::i8: Opc = 10; break;
1729 case MVT::i16: Opc = 11; break;
1730 case MVT::i32: Opc = 12; break;
1731 case MVT::f32: Opc = 13; break;
1732 case MVT::f64: Opc = 14; break;
1733 }
1734 switch (Node->getOpcode()) {
1735 default: assert(0 && "Unreachable!");
1736 case ISD::SUB: Opc = SUBTab[Opc]; break;
1737 case ISD::MUL: Opc = MULTab[Opc]; break;
1738 case ISD::AND: Opc = ANDTab[Opc]; break;
1739 case ISD::OR: Opc = ORTab[Opc]; break;
1740 case ISD::XOR: Opc = XORTab[Opc]; break;
1741 }
1742 if (Opc) {
1743 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1744 } else {
1745 assert(Node->getOpcode() == ISD::MUL &&
1746 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001747 // Must use the MUL instruction, which forces use of AL.
1748 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1749 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1750 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001751 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001752 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001753 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001754 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001755 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1756 Tmp2 = SelectExpr(N.getOperand(1));
1757 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001758 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001759 Tmp3 = SelectExpr(N.getOperand(2));
1760 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001761 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00001762 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
1763 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001764
1765 case ISD::SDIV:
1766 case ISD::UDIV:
1767 case ISD::SREM:
1768 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001769 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
1770 "We don't support this operator!");
1771
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001772 if (N.getOpcode() == ISD::SDIV)
1773 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1774 // FIXME: These special cases should be handled by the lowering impl!
1775 unsigned RHS = CN->getValue();
1776 bool isNeg = false;
1777 if ((int)RHS < 0) {
1778 isNeg = true;
1779 RHS = -RHS;
1780 }
1781 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1782 unsigned Log = log2(RHS);
1783 unsigned TmpReg = MakeReg(N.getValueType());
1784 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1785 switch (N.getValueType()) {
1786 default: assert("Unknown type to signed divide!");
1787 case MVT::i8:
1788 SAROpc = X86::SAR8ri;
1789 SHROpc = X86::SHR8ri;
1790 ADDOpc = X86::ADD8rr;
1791 NEGOpc = X86::NEG8r;
1792 break;
1793 case MVT::i16:
1794 SAROpc = X86::SAR16ri;
1795 SHROpc = X86::SHR16ri;
1796 ADDOpc = X86::ADD16rr;
1797 NEGOpc = X86::NEG16r;
1798 break;
1799 case MVT::i32:
1800 SAROpc = X86::SAR32ri;
1801 SHROpc = X86::SHR32ri;
1802 ADDOpc = X86::ADD32rr;
1803 NEGOpc = X86::NEG32r;
1804 break;
1805 }
Chris Lattner11333092005-01-11 03:11:44 +00001806 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001807 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1808 unsigned TmpReg2 = MakeReg(N.getValueType());
1809 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1810 unsigned TmpReg3 = MakeReg(N.getValueType());
1811 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1812
1813 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1814 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1815 if (isNeg)
1816 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1817 return Result;
1818 }
1819 }
1820
Chris Lattner11333092005-01-11 03:11:44 +00001821 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1822 Tmp1 = SelectExpr(N.getOperand(0));
1823 Tmp2 = SelectExpr(N.getOperand(1));
1824 } else {
1825 Tmp2 = SelectExpr(N.getOperand(1));
1826 Tmp1 = SelectExpr(N.getOperand(0));
1827 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001828
1829 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1830 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1831 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1832 switch (N.getValueType()) {
1833 default: assert(0 && "Cannot sdiv this type!");
1834 case MVT::i8:
1835 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1836 LoReg = X86::AL;
1837 HiReg = X86::AH;
1838 MovOpcode = X86::MOV8rr;
1839 ClrOpcode = X86::MOV8ri;
1840 SExtOpcode = X86::CBW;
1841 break;
1842 case MVT::i16:
1843 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1844 LoReg = X86::AX;
1845 HiReg = X86::DX;
1846 MovOpcode = X86::MOV16rr;
1847 ClrOpcode = X86::MOV16ri;
1848 SExtOpcode = X86::CWD;
1849 break;
1850 case MVT::i32:
1851 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001852 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001853 HiReg = X86::EDX;
1854 MovOpcode = X86::MOV32rr;
1855 ClrOpcode = X86::MOV32ri;
1856 SExtOpcode = X86::CDQ;
1857 break;
1858 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1859 case MVT::f32:
1860 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001861 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001862 return Result;
1863 }
1864
1865 // Set up the low part.
1866 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1867
1868 if (isSigned) {
1869 // Sign extend the low part into the high part.
1870 BuildMI(BB, SExtOpcode, 0);
1871 } else {
1872 // Zero out the high part, effectively zero extending the input.
1873 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1874 }
1875
1876 // Emit the DIV/IDIV instruction.
1877 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1878
1879 // Get the result of the divide or rem.
1880 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1881 return Result;
1882 }
1883
1884 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001885 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001886 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1887 switch (N.getValueType()) {
1888 default: assert(0 && "Cannot shift this type!");
1889 case MVT::i8: Opc = X86::ADD8rr; break;
1890 case MVT::i16: Opc = X86::ADD16rr; break;
1891 case MVT::i32: Opc = X86::ADD32rr; break;
1892 }
1893 Tmp1 = SelectExpr(N.getOperand(0));
1894 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1895 return Result;
1896 }
1897
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001898 switch (N.getValueType()) {
1899 default: assert(0 && "Cannot shift this type!");
1900 case MVT::i8: Opc = X86::SHL8ri; break;
1901 case MVT::i16: Opc = X86::SHL16ri; break;
1902 case MVT::i32: Opc = X86::SHL32ri; break;
1903 }
Chris Lattner11333092005-01-11 03:11:44 +00001904 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001905 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1906 return Result;
1907 }
Chris Lattner11333092005-01-11 03:11:44 +00001908
1909 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1910 Tmp1 = SelectExpr(N.getOperand(0));
1911 Tmp2 = SelectExpr(N.getOperand(1));
1912 } else {
1913 Tmp2 = SelectExpr(N.getOperand(1));
1914 Tmp1 = SelectExpr(N.getOperand(0));
1915 }
1916
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001917 switch (N.getValueType()) {
1918 default: assert(0 && "Cannot shift this type!");
1919 case MVT::i8 : Opc = X86::SHL8rCL; break;
1920 case MVT::i16: Opc = X86::SHL16rCL; break;
1921 case MVT::i32: Opc = X86::SHL32rCL; break;
1922 }
1923 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1924 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1925 return Result;
1926 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001927 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1928 switch (N.getValueType()) {
1929 default: assert(0 && "Cannot shift this type!");
1930 case MVT::i8: Opc = X86::SHR8ri; break;
1931 case MVT::i16: Opc = X86::SHR16ri; break;
1932 case MVT::i32: Opc = X86::SHR32ri; break;
1933 }
Chris Lattner11333092005-01-11 03:11:44 +00001934 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001935 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1936 return Result;
1937 }
Chris Lattner11333092005-01-11 03:11:44 +00001938
1939 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1940 Tmp1 = SelectExpr(N.getOperand(0));
1941 Tmp2 = SelectExpr(N.getOperand(1));
1942 } else {
1943 Tmp2 = SelectExpr(N.getOperand(1));
1944 Tmp1 = SelectExpr(N.getOperand(0));
1945 }
1946
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001947 switch (N.getValueType()) {
1948 default: assert(0 && "Cannot shift this type!");
1949 case MVT::i8 : Opc = X86::SHR8rCL; break;
1950 case MVT::i16: Opc = X86::SHR16rCL; break;
1951 case MVT::i32: Opc = X86::SHR32rCL; break;
1952 }
1953 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1954 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1955 return Result;
1956 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001957 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1958 switch (N.getValueType()) {
1959 default: assert(0 && "Cannot shift this type!");
1960 case MVT::i8: Opc = X86::SAR8ri; break;
1961 case MVT::i16: Opc = X86::SAR16ri; break;
1962 case MVT::i32: Opc = X86::SAR32ri; break;
1963 }
Chris Lattner11333092005-01-11 03:11:44 +00001964 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001965 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1966 return Result;
1967 }
Chris Lattner11333092005-01-11 03:11:44 +00001968
1969 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1970 Tmp1 = SelectExpr(N.getOperand(0));
1971 Tmp2 = SelectExpr(N.getOperand(1));
1972 } else {
1973 Tmp2 = SelectExpr(N.getOperand(1));
1974 Tmp1 = SelectExpr(N.getOperand(0));
1975 }
1976
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001977 switch (N.getValueType()) {
1978 default: assert(0 && "Cannot shift this type!");
1979 case MVT::i8 : Opc = X86::SAR8rCL; break;
1980 case MVT::i16: Opc = X86::SAR16rCL; break;
1981 case MVT::i32: Opc = X86::SAR32rCL; break;
1982 }
1983 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1984 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1985 return Result;
1986
1987 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001988 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001989 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1990 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1991 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00001992 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001993 // Make sure we generate both values.
1994 if (Result != 1)
1995 ExprMap[N.getValue(1)] = 1; // Generate the token
1996 else
1997 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1998
Chris Lattner5188ad72005-01-08 19:28:19 +00001999 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002000 default: assert(0 && "Cannot load this type!");
2001 case MVT::i1:
2002 case MVT::i8: Opc = X86::MOV8rm; break;
2003 case MVT::i16: Opc = X86::MOV16rm; break;
2004 case MVT::i32: Opc = X86::MOV32rm; break;
2005 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
2006 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2007 }
Chris Lattner11333092005-01-11 03:11:44 +00002008
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002009 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002010 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002011 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2012 } else {
2013 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002014
2015 SDOperand Chain = N.getOperand(0);
2016 SDOperand Address = N.getOperand(1);
2017 if (getRegPressure(Chain) > getRegPressure(Address)) {
2018 Select(Chain);
2019 SelectAddress(Address, AM);
2020 } else {
2021 SelectAddress(Address, AM);
2022 Select(Chain);
2023 }
2024
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002025 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2026 }
2027 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002028
2029 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2030 case ISD::ZEXTLOAD: {
2031 // Make sure we generate both values.
2032 if (Result != 1)
2033 ExprMap[N.getValue(1)] = 1; // Generate the token
2034 else
2035 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2036
Chris Lattnerda2ce112005-01-16 07:34:08 +00002037 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2038 if (Node->getValueType(0) == MVT::f64) {
2039 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2040 "Bad EXTLOAD!");
2041 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2042 CP->getIndex());
2043 return Result;
2044 }
2045
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002046 X86AddressMode AM;
2047 if (getRegPressure(Node->getOperand(0)) >
2048 getRegPressure(Node->getOperand(1))) {
2049 Select(Node->getOperand(0)); // chain
2050 SelectAddress(Node->getOperand(1), AM);
2051 } else {
2052 SelectAddress(Node->getOperand(1), AM);
2053 Select(Node->getOperand(0)); // chain
2054 }
2055
2056 switch (Node->getValueType(0)) {
2057 default: assert(0 && "Unknown type to sign extend to.");
2058 case MVT::f64:
2059 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2060 "Bad EXTLOAD!");
2061 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2062 break;
2063 case MVT::i32:
2064 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2065 default:
2066 assert(0 && "Bad zero extend!");
2067 case MVT::i1:
2068 case MVT::i8:
2069 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2070 break;
2071 case MVT::i16:
2072 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2073 break;
2074 }
2075 break;
2076 case MVT::i16:
2077 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2078 "Bad zero extend!");
2079 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2080 break;
2081 case MVT::i8:
2082 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2083 "Bad zero extend!");
2084 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2085 break;
2086 }
2087 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002088 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002089 case ISD::SEXTLOAD: {
2090 // Make sure we generate both values.
2091 if (Result != 1)
2092 ExprMap[N.getValue(1)] = 1; // Generate the token
2093 else
2094 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2095
2096 X86AddressMode AM;
2097 if (getRegPressure(Node->getOperand(0)) >
2098 getRegPressure(Node->getOperand(1))) {
2099 Select(Node->getOperand(0)); // chain
2100 SelectAddress(Node->getOperand(1), AM);
2101 } else {
2102 SelectAddress(Node->getOperand(1), AM);
2103 Select(Node->getOperand(0)); // chain
2104 }
2105
2106 switch (Node->getValueType(0)) {
2107 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2108 default: assert(0 && "Unknown type to sign extend to.");
2109 case MVT::i32:
2110 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2111 default:
2112 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2113 case MVT::i8:
2114 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2115 break;
2116 case MVT::i16:
2117 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2118 break;
2119 }
2120 break;
2121 case MVT::i16:
2122 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2123 "Cannot sign extend from bool!");
2124 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2125 break;
2126 }
2127 return Result;
2128 }
2129
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002130 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002131 // Generate both result values.
2132 if (Result != 1)
2133 ExprMap[N.getValue(1)] = 1; // Generate the token
2134 else
2135 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2136
2137 // FIXME: We are currently ignoring the requested alignment for handling
2138 // greater than the stack alignment. This will need to be revisited at some
2139 // point. Align = N.getOperand(2);
2140
2141 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2142 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2143 std::cerr << "Cannot allocate stack object with greater alignment than"
2144 << " the stack alignment yet!";
2145 abort();
2146 }
2147
2148 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002149 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002150 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2151 .addImm(CN->getValue());
2152 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002153 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2154 Select(N.getOperand(0));
2155 Tmp1 = SelectExpr(N.getOperand(1));
2156 } else {
2157 Tmp1 = SelectExpr(N.getOperand(1));
2158 Select(N.getOperand(0));
2159 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002160
2161 // Subtract size from stack pointer, thereby allocating some space.
2162 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2163 }
2164
2165 // Put a pointer to the space into the result register, by copying the stack
2166 // pointer.
2167 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2168 return Result;
2169
2170 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002171 // The chain for this call is now lowered.
2172 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
2173
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002174 if (GlobalAddressSDNode *GASD =
2175 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002176 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002177 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2178 } else if (ExternalSymbolSDNode *ESSDN =
2179 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002180 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002181 BuildMI(BB, X86::CALLpcrel32,
2182 1).addExternalSymbol(ESSDN->getSymbol(), true);
2183 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002184 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2185 Select(N.getOperand(0));
2186 Tmp1 = SelectExpr(N.getOperand(1));
2187 } else {
2188 Tmp1 = SelectExpr(N.getOperand(1));
2189 Select(N.getOperand(0));
2190 }
2191
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002192 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2193 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002194 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002195 default: assert(0 && "Unknown value type for call result!");
2196 case MVT::Other: return 1;
2197 case MVT::i1:
2198 case MVT::i8:
2199 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2200 break;
2201 case MVT::i16:
2202 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2203 break;
2204 case MVT::i32:
2205 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002206 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002207 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2208 break;
2209 case MVT::f32:
2210 case MVT::f64: // Floating-point return values live in %ST(0)
2211 ContainsFPCode = true;
2212 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2213 break;
2214 }
2215 return Result+N.ResNo;
2216 }
2217
2218 return 0;
2219}
2220
2221void ISel::Select(SDOperand N) {
2222 unsigned Tmp1, Tmp2, Opc;
2223
2224 // FIXME: Disable for our current expansion model!
2225 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
2226 return; // Already selected.
2227
Chris Lattner989de032005-01-11 06:14:36 +00002228 SDNode *Node = N.Val;
2229
2230 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002231 default:
Chris Lattner989de032005-01-11 06:14:36 +00002232 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002233 assert(0 && "Node not handled yet!");
2234 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002235 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002236 if (Node->getNumOperands() == 2) {
2237 bool OneFirst =
2238 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2239 Select(Node->getOperand(OneFirst));
2240 Select(Node->getOperand(!OneFirst));
2241 } else {
2242 std::vector<std::pair<unsigned, unsigned> > OpsP;
2243 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2244 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2245 std::sort(OpsP.begin(), OpsP.end());
2246 std::reverse(OpsP.begin(), OpsP.end());
2247 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2248 Select(Node->getOperand(OpsP[i].second));
2249 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002250 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002251 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002252 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2253 Select(N.getOperand(0));
2254 Tmp1 = SelectExpr(N.getOperand(1));
2255 } else {
2256 Tmp1 = SelectExpr(N.getOperand(1));
2257 Select(N.getOperand(0));
2258 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002259 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002260
2261 if (Tmp1 != Tmp2) {
2262 switch (N.getOperand(1).getValueType()) {
2263 default: assert(0 && "Invalid type for operation!");
2264 case MVT::i1:
2265 case MVT::i8: Opc = X86::MOV8rr; break;
2266 case MVT::i16: Opc = X86::MOV16rr; break;
2267 case MVT::i32: Opc = X86::MOV32rr; break;
2268 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002269 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002270 }
2271 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2272 }
2273 return;
2274 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002275 switch (N.getNumOperands()) {
2276 default:
2277 assert(0 && "Unknown return instruction!");
2278 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002279 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2280 N.getOperand(2).getValueType() == MVT::i32 &&
2281 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002282 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2283 Tmp1 = SelectExpr(N.getOperand(1));
2284 Tmp2 = SelectExpr(N.getOperand(2));
2285 } else {
2286 Tmp2 = SelectExpr(N.getOperand(2));
2287 Tmp1 = SelectExpr(N.getOperand(1));
2288 }
2289 Select(N.getOperand(0));
2290
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002291 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2292 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2293 // Declare that EAX & EDX are live on exit.
2294 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2295 .addReg(X86::ESP);
2296 break;
2297 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002298 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2299 Select(N.getOperand(0));
2300 Tmp1 = SelectExpr(N.getOperand(1));
2301 } else {
2302 Tmp1 = SelectExpr(N.getOperand(1));
2303 Select(N.getOperand(0));
2304 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002305 switch (N.getOperand(1).getValueType()) {
2306 default: assert(0 && "All other types should have been promoted!!");
2307 case MVT::f64:
2308 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2309 // Declare that top-of-stack is live on exit
2310 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2311 break;
2312 case MVT::i32:
2313 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2314 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2315 break;
2316 }
2317 break;
2318 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002319 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002320 break;
2321 }
2322 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2323 return;
2324 case ISD::BR: {
2325 Select(N.getOperand(0));
2326 MachineBasicBlock *Dest =
2327 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2328 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2329 return;
2330 }
2331
2332 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002333 MachineBasicBlock *Dest =
2334 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002335
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002336 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2337 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002338 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2339 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2340 Select(N.getOperand(0));
2341 Tmp1 = SelectExpr(N.getOperand(1));
2342 } else {
2343 Tmp1 = SelectExpr(N.getOperand(1));
2344 Select(N.getOperand(0));
2345 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002346 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2347 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2348 }
Chris Lattner11333092005-01-11 03:11:44 +00002349
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002350 return;
2351 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002352
Chris Lattner4df0de92005-01-17 00:00:33 +00002353 case ISD::LOAD:
2354 // If this load could be folded into the only using instruction, and if it
2355 // is safe to emit the instruction here, try to do so now.
2356 if (Node->hasNUsesOfValue(1, 0)) {
2357 SDOperand TheVal = N.getValue(0);
2358 SDNode *User = 0;
2359 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2360 assert(UI != Node->use_end() && "Didn't find use!");
2361 SDNode *UN = *UI;
2362 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2363 if (UN->getOperand(i) == TheVal) {
2364 User = UN;
2365 goto FoundIt;
2366 }
2367 }
2368 FoundIt:
2369 // Only handle unary operators right now.
2370 if (User->getNumOperands() == 1) {
2371 LoweredTokens.erase(N);
2372 SelectExpr(SDOperand(User, 0));
2373 return;
2374 }
2375 }
2376 SelectExpr(N);
2377 return;
2378
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002379 case ISD::EXTLOAD:
2380 case ISD::SEXTLOAD:
2381 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002382 case ISD::CALL:
2383 case ISD::DYNAMIC_STACKALLOC:
2384 SelectExpr(N);
2385 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002386
2387 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2388 // On X86, we can represent all types except for Bool and Float natively.
2389 X86AddressMode AM;
2390 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002391 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2392 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2393 && "Unsupported TRUNCSTORE for this target!");
2394
2395 if (StoredTy == MVT::i16) {
2396 // FIXME: This is here just to allow testing. X86 doesn't really have a
2397 // TRUNCSTORE i16 operation, but this is required for targets that do not
2398 // have 16-bit integer registers. We occasionally disable 16-bit integer
2399 // registers to test the promotion code.
2400 Select(N.getOperand(0));
2401 Tmp1 = SelectExpr(N.getOperand(1));
2402 SelectAddress(N.getOperand(2), AM);
2403
2404 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2405 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2406 return;
2407 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002408
2409 // Store of constant bool?
2410 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2411 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2412 Select(N.getOperand(0));
2413 SelectAddress(N.getOperand(2), AM);
2414 } else {
2415 SelectAddress(N.getOperand(2), AM);
2416 Select(N.getOperand(0));
2417 }
2418 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2419 return;
2420 }
2421
2422 switch (StoredTy) {
2423 default: assert(0 && "Cannot truncstore this type!");
2424 case MVT::i1: Opc = X86::MOV8mr; break;
2425 case MVT::f32: Opc = X86::FST32m; break;
2426 }
2427
2428 std::vector<std::pair<unsigned, unsigned> > RP;
2429 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2430 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2431 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2432 std::sort(RP.begin(), RP.end());
2433
2434 for (unsigned i = 0; i != 3; ++i)
2435 switch (RP[2-i].second) {
2436 default: assert(0 && "Unknown operand number!");
2437 case 0: Select(N.getOperand(0)); break;
2438 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2439 case 2: SelectAddress(N.getOperand(2), AM); break;
2440 }
2441
2442 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2443 return;
2444 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002445 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002446 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002447
2448 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2449 Opc = 0;
2450 switch (CN->getValueType(0)) {
2451 default: assert(0 && "Invalid type for operation!");
2452 case MVT::i1:
2453 case MVT::i8: Opc = X86::MOV8mi; break;
2454 case MVT::i16: Opc = X86::MOV16mi; break;
2455 case MVT::i32: Opc = X86::MOV32mi; break;
2456 case MVT::f32:
2457 case MVT::f64: break;
2458 }
2459 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002460 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2461 Select(N.getOperand(0));
2462 SelectAddress(N.getOperand(2), AM);
2463 } else {
2464 SelectAddress(N.getOperand(2), AM);
2465 Select(N.getOperand(0));
2466 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002467 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2468 return;
2469 }
2470 }
Chris Lattner837caa72005-01-11 23:21:30 +00002471
2472 // Check to see if this is a load/op/store combination.
2473 if (N.getOperand(1).Val->hasOneUse() &&
Chris Lattner4ff348b2005-01-17 06:26:58 +00002474 N.getOperand(1).Val->getNumOperands() == 2 &&
2475 !MVT::isFloatingPoint(N.getOperand(0).getValue(0).getValueType()) &&
2476 isFoldableLoad(N.getOperand(0).getValue(0),
2477 N.getOperand(0).getValue(1))) {
Chris Lattner837caa72005-01-11 23:21:30 +00002478 SDOperand TheLoad = N.getOperand(0).getValue(0);
2479 // Check to see if we are loading the same pointer that we're storing to.
2480 if (TheLoad.getOperand(1) == N.getOperand(2)) {
2481 // See if the stored value is a simple binary operator that uses the
2482 // load as one of its operands.
2483 SDOperand Op = N.getOperand(1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002484 if ((Op.getOperand(0) == TheLoad || Op.getOperand(1) == TheLoad)) {
Chris Lattner837caa72005-01-11 23:21:30 +00002485 // Finally, check to see if this is one of the ops we can handle!
2486 static const unsigned ADDTAB[] = {
2487 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002488 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
Chris Lattner837caa72005-01-11 23:21:30 +00002489 };
Chris Lattner7ea64f52005-01-12 01:28:00 +00002490 static const unsigned SUBTAB[] = {
2491 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002492 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002493 };
2494 static const unsigned ANDTAB[] = {
2495 X86::AND8mi, X86::AND16mi, X86::AND32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002496 X86::AND8mr, X86::AND16mr, X86::AND32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002497 };
2498 static const unsigned ORTAB[] = {
2499 X86::OR8mi, X86::OR16mi, X86::OR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002500 X86::OR8mr, X86::OR16mr, X86::OR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002501 };
2502 static const unsigned XORTAB[] = {
2503 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002504 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002505 };
2506 static const unsigned SHLTAB[] = {
2507 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002508 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002509 };
2510 static const unsigned SARTAB[] = {
2511 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002512 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002513 };
2514 static const unsigned SHRTAB[] = {
2515 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002516 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002517 };
Chris Lattner837caa72005-01-11 23:21:30 +00002518
2519 const unsigned *TabPtr = 0;
2520 switch (Op.getOpcode()) {
Chris Lattner7ea64f52005-01-12 01:28:00 +00002521 default: std::cerr << "CANNOT [mem] op= val: "; Op.Val->dump(); std::cerr << "\n"; break;
Chris Lattner4ff348b2005-01-17 06:26:58 +00002522 case ISD::MUL:
2523 case ISD::SDIV:
2524 case ISD::UDIV:
2525 case ISD::SREM:
2526 case ISD::UREM: break;
2527
Chris Lattner837caa72005-01-11 23:21:30 +00002528 case ISD::ADD: TabPtr = ADDTAB; break;
Chris Lattner7ea64f52005-01-12 01:28:00 +00002529 case ISD::SUB: TabPtr = SUBTAB; break;
2530 case ISD::AND: TabPtr = ANDTAB; break;
2531 case ISD:: OR: TabPtr = ORTAB; break;
2532 case ISD::XOR: TabPtr = XORTAB; break;
2533 case ISD::SHL: TabPtr = SHLTAB; break;
2534 case ISD::SRA: TabPtr = SARTAB; break;
2535 case ISD::SRL: TabPtr = SHRTAB; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002536 }
2537
2538 if (TabPtr) {
2539 // Handle: [mem] op= CST
2540 SDOperand Op0 = Op.getOperand(0);
2541 SDOperand Op1 = Op.getOperand(1);
2542 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner48034fd2005-01-12 05:22:07 +00002543 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
Chris Lattner837caa72005-01-11 23:21:30 +00002544 default: break;
2545 case MVT::i1:
2546 case MVT::i8: Opc = TabPtr[0]; break;
2547 case MVT::i16: Opc = TabPtr[1]; break;
2548 case MVT::i32: Opc = TabPtr[2]; break;
2549 }
2550
2551 if (Opc) {
2552 if (getRegPressure(TheLoad.getOperand(0)) >
2553 getRegPressure(TheLoad.getOperand(1))) {
2554 Select(TheLoad.getOperand(0));
2555 SelectAddress(TheLoad.getOperand(1), AM);
2556 } else {
2557 SelectAddress(TheLoad.getOperand(1), AM);
2558 Select(TheLoad.getOperand(0));
2559 }
2560
2561 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2562 return;
2563 }
2564 }
2565
2566 // If we have [mem] = V op [mem], try to turn it into:
2567 // [mem] = [mem] op V.
Chris Lattner7ea64f52005-01-12 01:28:00 +00002568 if (Op1 == TheLoad && Op.getOpcode() != ISD::SUB &&
2569 Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRA &&
2570 Op.getOpcode() != ISD::SRL)
Chris Lattner837caa72005-01-11 23:21:30 +00002571 std::swap(Op0, Op1);
2572
2573 if (Op0 == TheLoad) {
2574 switch (Op0.getValueType()) {
2575 default: break;
2576 case MVT::i1:
2577 case MVT::i8: Opc = TabPtr[3]; break;
2578 case MVT::i16: Opc = TabPtr[4]; break;
2579 case MVT::i32: Opc = TabPtr[5]; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002580 }
2581
2582 if (Opc) {
2583 Select(TheLoad.getOperand(0));
2584 SelectAddress(TheLoad.getOperand(1), AM);
2585 unsigned Reg = SelectExpr(Op1);
2586 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addReg(Reg);
2587 return;
2588 }
2589 }
Chris Lattner837caa72005-01-11 23:21:30 +00002590 }
2591 }
2592 }
2593 }
2594
2595
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002596 switch (N.getOperand(1).getValueType()) {
2597 default: assert(0 && "Cannot store this type!");
2598 case MVT::i1:
2599 case MVT::i8: Opc = X86::MOV8mr; break;
2600 case MVT::i16: Opc = X86::MOV16mr; break;
2601 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002602 case MVT::f32: Opc = X86::FST32m; break;
2603 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002604 }
Chris Lattner11333092005-01-11 03:11:44 +00002605
2606 std::vector<std::pair<unsigned, unsigned> > RP;
2607 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2608 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2609 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2610 std::sort(RP.begin(), RP.end());
2611
2612 for (unsigned i = 0; i != 3; ++i)
2613 switch (RP[2-i].second) {
2614 default: assert(0 && "Unknown operand number!");
2615 case 0: Select(N.getOperand(0)); break;
2616 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002617 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002618 }
2619
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002620 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2621 return;
2622 }
2623 case ISD::ADJCALLSTACKDOWN:
2624 case ISD::ADJCALLSTACKUP:
2625 Select(N.getOperand(0));
2626 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2627
2628 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2629 X86::ADJCALLSTACKUP;
2630 BuildMI(BB, Opc, 1).addImm(Tmp1);
2631 return;
Chris Lattner989de032005-01-11 06:14:36 +00002632 case ISD::MEMSET: {
2633 Select(N.getOperand(0)); // Select the chain.
2634 unsigned Align =
2635 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2636 if (Align == 0) Align = 1;
2637
2638 // Turn the byte code into # iterations
2639 unsigned CountReg;
2640 unsigned Opcode;
2641 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2642 unsigned Val = ValC->getValue() & 255;
2643
2644 // If the value is a constant, then we can potentially use larger sets.
2645 switch (Align & 3) {
2646 case 2: // WORD aligned
2647 CountReg = MakeReg(MVT::i32);
2648 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2649 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2650 } else {
2651 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2652 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2653 }
2654 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2655 Opcode = X86::REP_STOSW;
2656 break;
2657 case 0: // DWORD aligned
2658 CountReg = MakeReg(MVT::i32);
2659 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2660 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2661 } else {
2662 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2663 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2664 }
2665 Val = (Val << 8) | Val;
2666 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2667 Opcode = X86::REP_STOSD;
2668 break;
2669 default: // BYTE aligned
2670 CountReg = SelectExpr(Node->getOperand(3));
2671 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2672 Opcode = X86::REP_STOSB;
2673 break;
2674 }
2675 } else {
2676 // If it's not a constant value we are storing, just fall back. We could
2677 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2678 unsigned ValReg = SelectExpr(Node->getOperand(2));
2679 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2680 CountReg = SelectExpr(Node->getOperand(3));
2681 Opcode = X86::REP_STOSB;
2682 }
2683
2684 // No matter what the alignment is, we put the source in ESI, the
2685 // destination in EDI, and the count in ECX.
2686 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2687 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2688 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2689 BuildMI(BB, Opcode, 0);
2690 return;
2691 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002692 case ISD::MEMCPY:
2693 Select(N.getOperand(0)); // Select the chain.
2694 unsigned Align =
2695 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2696 if (Align == 0) Align = 1;
2697
2698 // Turn the byte code into # iterations
2699 unsigned CountReg;
2700 unsigned Opcode;
2701 switch (Align & 3) {
2702 case 2: // WORD aligned
2703 CountReg = MakeReg(MVT::i32);
2704 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2705 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2706 } else {
2707 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2708 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2709 }
2710 Opcode = X86::REP_MOVSW;
2711 break;
2712 case 0: // DWORD aligned
2713 CountReg = MakeReg(MVT::i32);
2714 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2715 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2716 } else {
2717 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2718 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2719 }
2720 Opcode = X86::REP_MOVSD;
2721 break;
2722 default: // BYTE aligned
2723 CountReg = SelectExpr(Node->getOperand(3));
2724 Opcode = X86::REP_MOVSB;
2725 break;
2726 }
2727
2728 // No matter what the alignment is, we put the source in ESI, the
2729 // destination in EDI, and the count in ECX.
2730 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2731 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2732 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2733 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2734 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2735 BuildMI(BB, Opcode, 0);
2736 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002737 }
2738 assert(0 && "Should not be reached!");
2739}
2740
2741
2742/// createX86PatternInstructionSelector - This pass converts an LLVM function
2743/// into a machine code representation using pattern matching and a machine
2744/// description file.
2745///
2746FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2747 return new ISel(TM);
2748}