blob: 005433b0516342b31404f39359b3c2aa6196559e [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 Lattnera5ade062005-01-11 21:19:59 +0000346 bool isFoldableLoad(SDOperand Op);
347 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
348
349
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000350 void EmitCMP(SDOperand LHS, SDOperand RHS);
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 Lattner8acb1ba2005-01-07 07:49:41 +0000744 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
745 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 Lattner8acb1ba2005-01-07 07:49:41 +0000801 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
802 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
913 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
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 Lattner8acb1ba2005-01-07 07:49:41 +0000919void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
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 Lattneref6806c2005-01-12 02:02:48 +0000923 if (isFoldableLoad(LHS)) {
924 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;
962 if (isFoldableLoad(LHS)) {
963 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 Lattnera5ade062005-01-11 21:19:59 +0000999/// isFoldableLoad - Return true if this is a load instruction that can safely
1000/// be folded into an operation that uses it.
1001bool ISel::isFoldableLoad(SDOperand Op) {
1002 if (Op.getOpcode() != ISD::LOAD ||
1003 // FIXME: currently can't fold constant pool indexes.
1004 isa<ConstantPoolSDNode>(Op.getOperand(1)))
1005 return false;
1006
1007 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001008 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1009 if (ExprMap.count(Op.getValue(1))) return false;
1010 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
1011 assert(!LoweredTokens.count(Op.getValue(1)) &&
1012 "Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001013
Chris Lattnera0bb6922005-01-12 18:38:26 +00001014 // Finally, there can only be one use of its value.
1015 return Op.Val->hasNUsesOfValue(1, 0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001016}
1017
1018/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1019/// and compute the address being loaded into AM.
1020void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1021 SDOperand Chain = Op.getOperand(0);
1022 SDOperand Address = Op.getOperand(1);
1023 if (getRegPressure(Chain) > getRegPressure(Address)) {
1024 Select(Chain);
1025 SelectAddress(Address, AM);
1026 } else {
1027 SelectAddress(Address, AM);
1028 Select(Chain);
1029 }
1030
1031 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001032 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1033 "Load emitted more than once?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001034 ExprMap[SDOperand(Op.Val, 1)] = 1;
Chris Lattner636e79a2005-01-13 05:53:16 +00001035 if (!LoweredTokens.insert(Op.getValue(1)).second)
1036 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001037}
1038
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001039unsigned ISel::SelectExpr(SDOperand N) {
1040 unsigned Result;
1041 unsigned Tmp1, Tmp2, Tmp3;
1042 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001043 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001044 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001045
Chris Lattner7f2afac2005-01-14 22:37:41 +00001046 if (Node->getOpcode() == ISD::CopyFromReg) {
1047 // FIXME: Handle copy from physregs!
1048
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001049 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001050 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001051 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001052
1053 unsigned &Reg = ExprMap[N];
1054 if (Reg) return Reg;
1055
1056 if (N.getOpcode() != ISD::CALL)
1057 Reg = Result = (N.getValueType() != MVT::Other) ?
1058 MakeReg(N.getValueType()) : 1;
1059 else {
1060 // If this is a call instruction, make sure to prepare ALL of the result
1061 // values as well as the chain.
1062 if (Node->getNumValues() == 1)
1063 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001064 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001065 Result = MakeReg(Node->getValueType(0));
1066 ExprMap[N.getValue(0)] = Result;
1067 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1068 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1069 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001070 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001071 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001072
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001073 switch (N.getOpcode()) {
1074 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001075 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001076 assert(0 && "Node not handled!\n");
1077 case ISD::FrameIndex:
1078 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1079 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1080 return Result;
1081 case ISD::ConstantPool:
1082 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1083 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1084 return Result;
1085 case ISD::ConstantFP:
1086 ContainsFPCode = true;
1087 Tmp1 = Result; // Intermediate Register
1088 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1089 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1090 Tmp1 = MakeReg(MVT::f64);
1091
1092 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1093 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1094 BuildMI(BB, X86::FLD0, 0, Tmp1);
1095 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1096 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1097 BuildMI(BB, X86::FLD1, 0, Tmp1);
1098 else
1099 assert(0 && "Unexpected constant!");
1100 if (Tmp1 != Result)
1101 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1102 return Result;
1103 case ISD::Constant:
1104 switch (N.getValueType()) {
1105 default: assert(0 && "Cannot use constants of this type!");
1106 case MVT::i1:
1107 case MVT::i8: Opc = X86::MOV8ri; break;
1108 case MVT::i16: Opc = X86::MOV16ri; break;
1109 case MVT::i32: Opc = X86::MOV32ri; break;
1110 }
1111 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1112 return Result;
1113 case ISD::GlobalAddress: {
1114 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1115 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1116 return Result;
1117 }
1118 case ISD::ExternalSymbol: {
1119 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1120 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1121 return Result;
1122 }
1123 case ISD::FP_EXTEND:
1124 Tmp1 = SelectExpr(N.getOperand(0));
1125 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001126 return Result;
1127 case ISD::ZERO_EXTEND: {
1128 int DestIs16 = N.getValueType() == MVT::i16;
1129 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001130
1131 // FIXME: This hack is here for zero extension casts from bool to i8. This
1132 // would not be needed if bools were promoted by Legalize.
1133 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001134 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001135 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1136 return Result;
1137 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001138
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001139 if (isFoldableLoad(N.getOperand(0))) {
1140 static const unsigned Opc[3] = {
1141 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1142 };
1143
1144 X86AddressMode AM;
1145 EmitFoldedLoad(N.getOperand(0), AM);
1146 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1147
1148 return Result;
1149 }
1150
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001151 static const unsigned Opc[3] = {
1152 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1153 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001154 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001155 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1156 return Result;
1157 }
1158 case ISD::SIGN_EXTEND: {
1159 int DestIs16 = N.getValueType() == MVT::i16;
1160 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1161
Chris Lattner590d8002005-01-09 18:52:44 +00001162 // FIXME: Legalize should promote bools to i8!
1163 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1164 "Sign extend from bool not implemented!");
1165
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001166 if (isFoldableLoad(N.getOperand(0))) {
1167 static const unsigned Opc[3] = {
1168 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1169 };
1170
1171 X86AddressMode AM;
1172 EmitFoldedLoad(N.getOperand(0), AM);
1173 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1174 return Result;
1175 }
1176
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001177 static const unsigned Opc[3] = {
1178 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1179 };
1180 Tmp1 = SelectExpr(N.getOperand(0));
1181 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1182 return Result;
1183 }
1184 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001185 // Fold TRUNCATE (LOAD P) into a smaller load from P.
1186 if (isFoldableLoad(N.getOperand(0))) {
1187 switch (N.getValueType()) {
1188 default: assert(0 && "Unknown truncate!");
1189 case MVT::i1:
1190 case MVT::i8: Opc = X86::MOV8rm; break;
1191 case MVT::i16: Opc = X86::MOV16rm; break;
1192 }
1193 X86AddressMode AM;
1194 EmitFoldedLoad(N.getOperand(0), AM);
1195 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1196 return Result;
1197 }
1198
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001199 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1200 // a move out of AX or AL.
1201 switch (N.getOperand(0).getValueType()) {
1202 default: assert(0 && "Unknown truncate!");
1203 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1204 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1205 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1206 }
1207 Tmp1 = SelectExpr(N.getOperand(0));
1208 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1209
1210 switch (N.getValueType()) {
1211 default: assert(0 && "Unknown truncate!");
1212 case MVT::i1:
1213 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1214 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1215 }
1216 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1217 return Result;
1218
1219 case ISD::FP_ROUND:
1220 // Truncate from double to float by storing to memory as float,
1221 // then reading it back into a register.
1222
1223 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001224 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001225 Tmp1 = TLI.getTargetData().getFloatAlignment();
1226 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1227
1228 // Codegen the input.
1229 Tmp1 = SelectExpr(N.getOperand(0));
1230
1231 // Emit the store, then the reload.
1232 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1233 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001234 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001235
1236 case ISD::SINT_TO_FP:
1237 case ISD::UINT_TO_FP: {
1238 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001239 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001240
1241 // Promote the integer to a type supported by FLD. We do this because there
1242 // are no unsigned FLD instructions, so we must promote an unsigned value to
1243 // a larger signed value, then use FLD on the larger value.
1244 //
1245 MVT::ValueType PromoteType = MVT::Other;
1246 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1247 unsigned PromoteOpcode = 0;
1248 unsigned RealDestReg = Result;
1249 switch (SrcTy) {
1250 case MVT::i1:
1251 case MVT::i8:
1252 // We don't have the facilities for directly loading byte sized data from
1253 // memory (even signed). Promote it to 16 bits.
1254 PromoteType = MVT::i16;
1255 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1256 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1257 break;
1258 case MVT::i16:
1259 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1260 PromoteType = MVT::i32;
1261 PromoteOpcode = X86::MOVZX32rr16;
1262 }
1263 break;
1264 default:
1265 // Don't fild into the real destination.
1266 if (Node->getOpcode() == ISD::UINT_TO_FP)
1267 Result = MakeReg(Node->getValueType(0));
1268 break;
1269 }
1270
1271 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1272
1273 if (PromoteType != MVT::Other) {
1274 Tmp2 = MakeReg(PromoteType);
1275 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1276 SrcTy = PromoteType;
1277 Tmp1 = Tmp2;
1278 }
1279
1280 // Spill the integer to memory and reload it from there.
1281 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1282 MachineFunction *F = BB->getParent();
1283 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1284
1285 switch (SrcTy) {
1286 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001287 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001288 // FIXME: this won't work for cast [u]long to FP
1289 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1290 FrameIdx).addReg(Tmp1);
1291 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1292 FrameIdx, 4).addReg(Tmp1+1);
1293 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1294 break;
1295 case MVT::i32:
1296 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1297 FrameIdx).addReg(Tmp1);
1298 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1299 break;
1300 case MVT::i16:
1301 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1302 FrameIdx).addReg(Tmp1);
1303 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1304 break;
1305 default: break; // No promotion required.
1306 }
1307
Chris Lattner085c9952005-01-12 04:00:00 +00001308 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001309 // If this is a cast from uint -> double, we need to be careful when if
1310 // the "sign" bit is set. If so, we don't want to make a negative number,
1311 // we want to make a positive number. Emit code to add an offset if the
1312 // sign bit is set.
1313
1314 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1315 unsigned IsNeg = MakeReg(MVT::i32);
1316 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1317
1318 // Create a CP value that has the offset in one word and 0 in the other.
1319 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1320 0x4f80000000000000ULL);
1321 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1322 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1323 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1324
1325 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1326 // We need special handling for unsigned 64-bit integer sources. If the
1327 // input number has the "sign bit" set, then we loaded it incorrectly as a
1328 // negative 64-bit number. In this case, add an offset value.
1329
1330 // Emit a test instruction to see if the dynamic input value was signed.
1331 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1332
1333 // If the sign bit is set, get a pointer to an offset, otherwise get a
1334 // pointer to a zero.
1335 MachineConstantPool *CP = F->getConstantPool();
1336 unsigned Zero = MakeReg(MVT::i32);
1337 Constant *Null = Constant::getNullValue(Type::UIntTy);
1338 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1339 CP->getConstantPoolIndex(Null));
1340 unsigned Offset = MakeReg(MVT::i32);
1341 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1342
1343 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1344 CP->getConstantPoolIndex(OffsetCst));
1345 unsigned Addr = MakeReg(MVT::i32);
1346 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1347
1348 // Load the constant for an add. FIXME: this could make an 'fadd' that
1349 // reads directly from memory, but we don't support these yet.
1350 unsigned ConstReg = MakeReg(MVT::f64);
1351 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1352
1353 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1354 }
1355 return RealDestReg;
1356 }
1357 case ISD::FP_TO_SINT:
1358 case ISD::FP_TO_UINT: {
1359 // FIXME: Most of this grunt work should be done by legalize!
1360 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1361
1362 // Change the floating point control register to use "round towards zero"
1363 // mode when truncating to an integer value.
1364 //
1365 MachineFunction *F = BB->getParent();
1366 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1367 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1368
1369 // Load the old value of the high byte of the control word...
1370 unsigned HighPartOfCW = MakeReg(MVT::i8);
1371 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1372 CWFrameIdx, 1);
1373
1374 // Set the high part to be round to zero...
1375 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1376 CWFrameIdx, 1).addImm(12);
1377
1378 // Reload the modified control word now...
1379 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1380
1381 // Restore the memory image of control word to original value
1382 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1383 CWFrameIdx, 1).addReg(HighPartOfCW);
1384
1385 // We don't have the facilities for directly storing byte sized data to
1386 // memory. Promote it to 16 bits. We also must promote unsigned values to
1387 // larger classes because we only have signed FP stores.
1388 MVT::ValueType StoreClass = Node->getValueType(0);
1389 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1390 switch (StoreClass) {
1391 case MVT::i8: StoreClass = MVT::i16; break;
1392 case MVT::i16: StoreClass = MVT::i32; break;
1393 case MVT::i32: StoreClass = MVT::i64; break;
1394 // The following treatment of cLong may not be perfectly right,
1395 // but it survives chains of casts of the form
1396 // double->ulong->double.
1397 case MVT::i64: StoreClass = MVT::i64; break;
1398 default: assert(0 && "Unknown store class!");
1399 }
1400
1401 // Spill the integer to memory and reload it from there.
1402 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1403 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1404
1405 switch (StoreClass) {
1406 default: assert(0 && "Unknown store class!");
1407 case MVT::i16:
1408 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1409 break;
1410 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001411 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001412 break;
1413 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001414 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001415 break;
1416 }
1417
1418 switch (Node->getValueType(0)) {
1419 default:
1420 assert(0 && "Unknown integer type!");
1421 case MVT::i64:
1422 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001423 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001424 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1425 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1426 case MVT::i32:
1427 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1428 break;
1429 case MVT::i16:
1430 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1431 break;
1432 case MVT::i8:
1433 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1434 break;
1435 }
1436
1437 // Reload the original control word now.
1438 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1439 return Result;
1440 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001441 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001442 Op0 = N.getOperand(0);
1443 Op1 = N.getOperand(1);
1444
1445 if (isFoldableLoad(Op0))
1446 std::swap(Op0, Op1);
1447
1448 if (isFoldableLoad(Op1)) {
1449 switch (N.getValueType()) {
1450 default: assert(0 && "Cannot add this type!");
1451 case MVT::i1:
1452 case MVT::i8: Opc = X86::ADD8rm; break;
1453 case MVT::i16: Opc = X86::ADD16rm; break;
1454 case MVT::i32: Opc = X86::ADD32rm; break;
1455 case MVT::f32: Opc = X86::FADD32m; break;
1456 case MVT::f64: Opc = X86::FADD64m; break;
1457 }
1458 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001459 EmitFoldedLoad(Op1, AM);
1460 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001461 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1462 return Result;
1463 }
1464
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001465 // See if we can codegen this as an LEA to fold operations together.
1466 if (N.getValueType() == MVT::i32) {
1467 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001468 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001469 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001470 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001471 // leave this as LEA, then peephole it to 'ADD' after two address elim
1472 // happens.
1473 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001474 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001475 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1476 return Result;
1477 }
1478 }
1479 }
Chris Lattner11333092005-01-11 03:11:44 +00001480
Chris Lattnera5ade062005-01-11 21:19:59 +00001481 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001482 Opc = 0;
1483 if (CN->getValue() == 1) { // add X, 1 -> inc X
1484 switch (N.getValueType()) {
1485 default: assert(0 && "Cannot integer add this type!");
1486 case MVT::i8: Opc = X86::INC8r; break;
1487 case MVT::i16: Opc = X86::INC16r; break;
1488 case MVT::i32: Opc = X86::INC32r; break;
1489 }
1490 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1491 switch (N.getValueType()) {
1492 default: assert(0 && "Cannot integer add this type!");
1493 case MVT::i8: Opc = X86::DEC8r; break;
1494 case MVT::i16: Opc = X86::DEC16r; break;
1495 case MVT::i32: Opc = X86::DEC32r; break;
1496 }
1497 }
1498
1499 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001500 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001501 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1502 return Result;
1503 }
1504
1505 switch (N.getValueType()) {
1506 default: assert(0 && "Cannot add this type!");
1507 case MVT::i8: Opc = X86::ADD8ri; break;
1508 case MVT::i16: Opc = X86::ADD16ri; break;
1509 case MVT::i32: Opc = X86::ADD32ri; break;
1510 }
1511 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001512 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001513 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1514 return Result;
1515 }
1516 }
1517
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001518 switch (N.getValueType()) {
1519 default: assert(0 && "Cannot add this type!");
1520 case MVT::i8: Opc = X86::ADD8rr; break;
1521 case MVT::i16: Opc = X86::ADD16rr; break;
1522 case MVT::i32: Opc = X86::ADD32rr; break;
1523 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001524 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001525 }
Chris Lattner11333092005-01-11 03:11:44 +00001526
Chris Lattnera5ade062005-01-11 21:19:59 +00001527 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1528 Tmp1 = SelectExpr(Op0);
1529 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001530 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001531 Tmp2 = SelectExpr(Op1);
1532 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001533 }
1534
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001535 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1536 return Result;
1537 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001538 case ISD::MUL:
1539 case ISD::AND:
1540 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001541 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001542 static const unsigned SUBTab[] = {
1543 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1544 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1545 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1546 };
1547 static const unsigned MULTab[] = {
1548 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1549 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1550 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1551 };
1552 static const unsigned ANDTab[] = {
1553 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1554 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1555 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1556 };
1557 static const unsigned ORTab[] = {
1558 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1559 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1560 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1561 };
1562 static const unsigned XORTab[] = {
1563 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1564 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1565 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1566 };
1567
1568 Op0 = Node->getOperand(0);
1569 Op1 = Node->getOperand(1);
1570
1571 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001572 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1573 if (CN->isNullValue()) { // 0 - N -> neg N
1574 switch (N.getValueType()) {
1575 default: assert(0 && "Cannot sub this type!");
1576 case MVT::i1:
1577 case MVT::i8: Opc = X86::NEG8r; break;
1578 case MVT::i16: Opc = X86::NEG16r; break;
1579 case MVT::i32: Opc = X86::NEG32r; break;
1580 }
1581 Tmp1 = SelectExpr(N.getOperand(1));
1582 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1583 return Result;
1584 }
1585
Chris Lattnera5ade062005-01-11 21:19:59 +00001586 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1587 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001588 switch (N.getValueType()) {
1589 default: assert(0 && "Cannot add this type!");
1590 case MVT::i1:
1591 case MVT::i8: Opc = X86::NOT8r; break;
1592 case MVT::i16: Opc = X86::NOT16r; break;
1593 case MVT::i32: Opc = X86::NOT32r; break;
1594 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001595 Tmp1 = SelectExpr(Op0);
Chris Lattnerd4dab922005-01-11 04:31:30 +00001596 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1597 return Result;
1598 }
1599
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001600 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001601 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001602 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001603 case MVT::i8: Opc = 0; break;
1604 case MVT::i16: Opc = 1; break;
1605 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001606 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001607 switch (Node->getOpcode()) {
1608 default: assert(0 && "Unreachable!");
1609 case ISD::SUB: Opc = SUBTab[Opc]; break;
1610 case ISD::MUL: Opc = MULTab[Opc]; break;
1611 case ISD::AND: Opc = ANDTab[Opc]; break;
1612 case ISD::OR: Opc = ORTab[Opc]; break;
1613 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001614 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001615 if (Opc) { // Can't fold MUL:i8 R, imm
1616 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001617 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1618 return Result;
1619 }
1620 }
Chris Lattner11333092005-01-11 03:11:44 +00001621
Chris Lattnera5ade062005-01-11 21:19:59 +00001622 if (isFoldableLoad(Op0))
1623 if (Node->getOpcode() != ISD::SUB) {
1624 std::swap(Op0, Op1);
1625 } else {
1626 // Emit 'reverse' subract, with a memory operand.
1627 switch (N.getValueType()) {
1628 default: Opc = 0; break;
1629 case MVT::f32: Opc = X86::FSUBR32m; break;
1630 case MVT::f64: Opc = X86::FSUBR64m; break;
1631 }
1632 if (Opc) {
1633 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001634 EmitFoldedLoad(Op0, AM);
1635 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001636 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1637 return Result;
1638 }
1639 }
1640
1641 if (isFoldableLoad(Op1)) {
1642 switch (N.getValueType()) {
1643 default: assert(0 && "Cannot operate on this type!");
1644 case MVT::i1:
1645 case MVT::i8: Opc = 5; break;
1646 case MVT::i16: Opc = 6; break;
1647 case MVT::i32: Opc = 7; break;
1648 case MVT::f32: Opc = 8; break;
1649 case MVT::f64: Opc = 9; break;
1650 }
1651 switch (Node->getOpcode()) {
1652 default: assert(0 && "Unreachable!");
1653 case ISD::SUB: Opc = SUBTab[Opc]; break;
1654 case ISD::MUL: Opc = MULTab[Opc]; break;
1655 case ISD::AND: Opc = ANDTab[Opc]; break;
1656 case ISD::OR: Opc = ORTab[Opc]; break;
1657 case ISD::XOR: Opc = XORTab[Opc]; break;
1658 }
1659
1660 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001661 EmitFoldedLoad(Op1, AM);
1662 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001663 if (Opc) {
1664 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1665 } else {
1666 assert(Node->getOpcode() == ISD::MUL &&
1667 N.getValueType() == MVT::i8 && "Unexpected situation!");
1668 // Must use the MUL instruction, which forces use of AL.
1669 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1670 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1671 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1672 }
1673 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001674 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001675
1676 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1677 Tmp1 = SelectExpr(Op0);
1678 Tmp2 = SelectExpr(Op1);
1679 } else {
1680 Tmp2 = SelectExpr(Op1);
1681 Tmp1 = SelectExpr(Op0);
1682 }
1683
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001684 switch (N.getValueType()) {
1685 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001686 case MVT::i1:
1687 case MVT::i8: Opc = 10; break;
1688 case MVT::i16: Opc = 11; break;
1689 case MVT::i32: Opc = 12; break;
1690 case MVT::f32: Opc = 13; break;
1691 case MVT::f64: Opc = 14; break;
1692 }
1693 switch (Node->getOpcode()) {
1694 default: assert(0 && "Unreachable!");
1695 case ISD::SUB: Opc = SUBTab[Opc]; break;
1696 case ISD::MUL: Opc = MULTab[Opc]; break;
1697 case ISD::AND: Opc = ANDTab[Opc]; break;
1698 case ISD::OR: Opc = ORTab[Opc]; break;
1699 case ISD::XOR: Opc = XORTab[Opc]; break;
1700 }
1701 if (Opc) {
1702 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1703 } else {
1704 assert(Node->getOpcode() == ISD::MUL &&
1705 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001706 // Must use the MUL instruction, which forces use of AL.
1707 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1708 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1709 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001710 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001711 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001712 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001713 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001714 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1715 Tmp2 = SelectExpr(N.getOperand(1));
1716 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001717 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001718 Tmp3 = SelectExpr(N.getOperand(2));
1719 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001720 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00001721 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
1722 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001723
1724 case ISD::SDIV:
1725 case ISD::UDIV:
1726 case ISD::SREM:
1727 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001728 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
1729 "We don't support this operator!");
1730
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001731 if (N.getOpcode() == ISD::SDIV)
1732 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1733 // FIXME: These special cases should be handled by the lowering impl!
1734 unsigned RHS = CN->getValue();
1735 bool isNeg = false;
1736 if ((int)RHS < 0) {
1737 isNeg = true;
1738 RHS = -RHS;
1739 }
1740 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1741 unsigned Log = log2(RHS);
1742 unsigned TmpReg = MakeReg(N.getValueType());
1743 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1744 switch (N.getValueType()) {
1745 default: assert("Unknown type to signed divide!");
1746 case MVT::i8:
1747 SAROpc = X86::SAR8ri;
1748 SHROpc = X86::SHR8ri;
1749 ADDOpc = X86::ADD8rr;
1750 NEGOpc = X86::NEG8r;
1751 break;
1752 case MVT::i16:
1753 SAROpc = X86::SAR16ri;
1754 SHROpc = X86::SHR16ri;
1755 ADDOpc = X86::ADD16rr;
1756 NEGOpc = X86::NEG16r;
1757 break;
1758 case MVT::i32:
1759 SAROpc = X86::SAR32ri;
1760 SHROpc = X86::SHR32ri;
1761 ADDOpc = X86::ADD32rr;
1762 NEGOpc = X86::NEG32r;
1763 break;
1764 }
Chris Lattner11333092005-01-11 03:11:44 +00001765 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001766 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1767 unsigned TmpReg2 = MakeReg(N.getValueType());
1768 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1769 unsigned TmpReg3 = MakeReg(N.getValueType());
1770 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1771
1772 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1773 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1774 if (isNeg)
1775 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1776 return Result;
1777 }
1778 }
1779
Chris Lattner11333092005-01-11 03:11:44 +00001780 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1781 Tmp1 = SelectExpr(N.getOperand(0));
1782 Tmp2 = SelectExpr(N.getOperand(1));
1783 } else {
1784 Tmp2 = SelectExpr(N.getOperand(1));
1785 Tmp1 = SelectExpr(N.getOperand(0));
1786 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001787
1788 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1789 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1790 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1791 switch (N.getValueType()) {
1792 default: assert(0 && "Cannot sdiv this type!");
1793 case MVT::i8:
1794 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1795 LoReg = X86::AL;
1796 HiReg = X86::AH;
1797 MovOpcode = X86::MOV8rr;
1798 ClrOpcode = X86::MOV8ri;
1799 SExtOpcode = X86::CBW;
1800 break;
1801 case MVT::i16:
1802 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1803 LoReg = X86::AX;
1804 HiReg = X86::DX;
1805 MovOpcode = X86::MOV16rr;
1806 ClrOpcode = X86::MOV16ri;
1807 SExtOpcode = X86::CWD;
1808 break;
1809 case MVT::i32:
1810 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001811 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001812 HiReg = X86::EDX;
1813 MovOpcode = X86::MOV32rr;
1814 ClrOpcode = X86::MOV32ri;
1815 SExtOpcode = X86::CDQ;
1816 break;
1817 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1818 case MVT::f32:
1819 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001820 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001821 return Result;
1822 }
1823
1824 // Set up the low part.
1825 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1826
1827 if (isSigned) {
1828 // Sign extend the low part into the high part.
1829 BuildMI(BB, SExtOpcode, 0);
1830 } else {
1831 // Zero out the high part, effectively zero extending the input.
1832 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1833 }
1834
1835 // Emit the DIV/IDIV instruction.
1836 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1837
1838 // Get the result of the divide or rem.
1839 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1840 return Result;
1841 }
1842
1843 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001844 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001845 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1846 switch (N.getValueType()) {
1847 default: assert(0 && "Cannot shift this type!");
1848 case MVT::i8: Opc = X86::ADD8rr; break;
1849 case MVT::i16: Opc = X86::ADD16rr; break;
1850 case MVT::i32: Opc = X86::ADD32rr; break;
1851 }
1852 Tmp1 = SelectExpr(N.getOperand(0));
1853 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1854 return Result;
1855 }
1856
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001857 switch (N.getValueType()) {
1858 default: assert(0 && "Cannot shift this type!");
1859 case MVT::i8: Opc = X86::SHL8ri; break;
1860 case MVT::i16: Opc = X86::SHL16ri; break;
1861 case MVT::i32: Opc = X86::SHL32ri; break;
1862 }
Chris Lattner11333092005-01-11 03:11:44 +00001863 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001864 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1865 return Result;
1866 }
Chris Lattner11333092005-01-11 03:11:44 +00001867
1868 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1869 Tmp1 = SelectExpr(N.getOperand(0));
1870 Tmp2 = SelectExpr(N.getOperand(1));
1871 } else {
1872 Tmp2 = SelectExpr(N.getOperand(1));
1873 Tmp1 = SelectExpr(N.getOperand(0));
1874 }
1875
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001876 switch (N.getValueType()) {
1877 default: assert(0 && "Cannot shift this type!");
1878 case MVT::i8 : Opc = X86::SHL8rCL; break;
1879 case MVT::i16: Opc = X86::SHL16rCL; break;
1880 case MVT::i32: Opc = X86::SHL32rCL; break;
1881 }
1882 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1883 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1884 return Result;
1885 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001886 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1887 switch (N.getValueType()) {
1888 default: assert(0 && "Cannot shift this type!");
1889 case MVT::i8: Opc = X86::SHR8ri; break;
1890 case MVT::i16: Opc = X86::SHR16ri; break;
1891 case MVT::i32: Opc = X86::SHR32ri; break;
1892 }
Chris Lattner11333092005-01-11 03:11:44 +00001893 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001894 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1895 return Result;
1896 }
Chris Lattner11333092005-01-11 03:11:44 +00001897
1898 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1899 Tmp1 = SelectExpr(N.getOperand(0));
1900 Tmp2 = SelectExpr(N.getOperand(1));
1901 } else {
1902 Tmp2 = SelectExpr(N.getOperand(1));
1903 Tmp1 = SelectExpr(N.getOperand(0));
1904 }
1905
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001906 switch (N.getValueType()) {
1907 default: assert(0 && "Cannot shift this type!");
1908 case MVT::i8 : Opc = X86::SHR8rCL; break;
1909 case MVT::i16: Opc = X86::SHR16rCL; break;
1910 case MVT::i32: Opc = X86::SHR32rCL; break;
1911 }
1912 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1913 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1914 return Result;
1915 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001916 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1917 switch (N.getValueType()) {
1918 default: assert(0 && "Cannot shift this type!");
1919 case MVT::i8: Opc = X86::SAR8ri; break;
1920 case MVT::i16: Opc = X86::SAR16ri; break;
1921 case MVT::i32: Opc = X86::SAR32ri; break;
1922 }
Chris Lattner11333092005-01-11 03:11:44 +00001923 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001924 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1925 return Result;
1926 }
Chris Lattner11333092005-01-11 03:11:44 +00001927
1928 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1929 Tmp1 = SelectExpr(N.getOperand(0));
1930 Tmp2 = SelectExpr(N.getOperand(1));
1931 } else {
1932 Tmp2 = SelectExpr(N.getOperand(1));
1933 Tmp1 = SelectExpr(N.getOperand(0));
1934 }
1935
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001936 switch (N.getValueType()) {
1937 default: assert(0 && "Cannot shift this type!");
1938 case MVT::i8 : Opc = X86::SAR8rCL; break;
1939 case MVT::i16: Opc = X86::SAR16rCL; break;
1940 case MVT::i32: Opc = X86::SAR32rCL; break;
1941 }
1942 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1943 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1944 return Result;
1945
1946 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001947 EmitCMP(N.getOperand(0), N.getOperand(1));
1948 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1949 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1950 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00001951 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001952 // Make sure we generate both values.
1953 if (Result != 1)
1954 ExprMap[N.getValue(1)] = 1; // Generate the token
1955 else
1956 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1957
Chris Lattner5188ad72005-01-08 19:28:19 +00001958 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001959 default: assert(0 && "Cannot load this type!");
1960 case MVT::i1:
1961 case MVT::i8: Opc = X86::MOV8rm; break;
1962 case MVT::i16: Opc = X86::MOV16rm; break;
1963 case MVT::i32: Opc = X86::MOV32rm; break;
1964 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1965 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1966 }
Chris Lattner11333092005-01-11 03:11:44 +00001967
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001968 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001969 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001970 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1971 } else {
1972 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001973
1974 SDOperand Chain = N.getOperand(0);
1975 SDOperand Address = N.getOperand(1);
1976 if (getRegPressure(Chain) > getRegPressure(Address)) {
1977 Select(Chain);
1978 SelectAddress(Address, AM);
1979 } else {
1980 SelectAddress(Address, AM);
1981 Select(Chain);
1982 }
1983
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001984 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1985 }
1986 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00001987
1988 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
1989 case ISD::ZEXTLOAD: {
1990 // Make sure we generate both values.
1991 if (Result != 1)
1992 ExprMap[N.getValue(1)] = 1; // Generate the token
1993 else
1994 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1995
Chris Lattnerda2ce112005-01-16 07:34:08 +00001996 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
1997 if (Node->getValueType(0) == MVT::f64) {
1998 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
1999 "Bad EXTLOAD!");
2000 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2001 CP->getIndex());
2002 return Result;
2003 }
2004
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002005 X86AddressMode AM;
2006 if (getRegPressure(Node->getOperand(0)) >
2007 getRegPressure(Node->getOperand(1))) {
2008 Select(Node->getOperand(0)); // chain
2009 SelectAddress(Node->getOperand(1), AM);
2010 } else {
2011 SelectAddress(Node->getOperand(1), AM);
2012 Select(Node->getOperand(0)); // chain
2013 }
2014
2015 switch (Node->getValueType(0)) {
2016 default: assert(0 && "Unknown type to sign extend to.");
2017 case MVT::f64:
2018 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2019 "Bad EXTLOAD!");
2020 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2021 break;
2022 case MVT::i32:
2023 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2024 default:
2025 assert(0 && "Bad zero extend!");
2026 case MVT::i1:
2027 case MVT::i8:
2028 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2029 break;
2030 case MVT::i16:
2031 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2032 break;
2033 }
2034 break;
2035 case MVT::i16:
2036 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2037 "Bad zero extend!");
2038 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2039 break;
2040 case MVT::i8:
2041 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2042 "Bad zero extend!");
2043 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2044 break;
2045 }
2046 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002047 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002048 case ISD::SEXTLOAD: {
2049 // Make sure we generate both values.
2050 if (Result != 1)
2051 ExprMap[N.getValue(1)] = 1; // Generate the token
2052 else
2053 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2054
2055 X86AddressMode AM;
2056 if (getRegPressure(Node->getOperand(0)) >
2057 getRegPressure(Node->getOperand(1))) {
2058 Select(Node->getOperand(0)); // chain
2059 SelectAddress(Node->getOperand(1), AM);
2060 } else {
2061 SelectAddress(Node->getOperand(1), AM);
2062 Select(Node->getOperand(0)); // chain
2063 }
2064
2065 switch (Node->getValueType(0)) {
2066 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2067 default: assert(0 && "Unknown type to sign extend to.");
2068 case MVT::i32:
2069 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2070 default:
2071 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2072 case MVT::i8:
2073 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2074 break;
2075 case MVT::i16:
2076 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2077 break;
2078 }
2079 break;
2080 case MVT::i16:
2081 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2082 "Cannot sign extend from bool!");
2083 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2084 break;
2085 }
2086 return Result;
2087 }
2088
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002089 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002090 // Generate both result 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 // FIXME: We are currently ignoring the requested alignment for handling
2097 // greater than the stack alignment. This will need to be revisited at some
2098 // point. Align = N.getOperand(2);
2099
2100 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2101 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2102 std::cerr << "Cannot allocate stack object with greater alignment than"
2103 << " the stack alignment yet!";
2104 abort();
2105 }
2106
2107 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002108 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002109 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2110 .addImm(CN->getValue());
2111 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002112 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2113 Select(N.getOperand(0));
2114 Tmp1 = SelectExpr(N.getOperand(1));
2115 } else {
2116 Tmp1 = SelectExpr(N.getOperand(1));
2117 Select(N.getOperand(0));
2118 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002119
2120 // Subtract size from stack pointer, thereby allocating some space.
2121 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2122 }
2123
2124 // Put a pointer to the space into the result register, by copying the stack
2125 // pointer.
2126 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2127 return Result;
2128
2129 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002130 // The chain for this call is now lowered.
2131 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
2132
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002133 if (GlobalAddressSDNode *GASD =
2134 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002135 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002136 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2137 } else if (ExternalSymbolSDNode *ESSDN =
2138 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002139 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002140 BuildMI(BB, X86::CALLpcrel32,
2141 1).addExternalSymbol(ESSDN->getSymbol(), true);
2142 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002143 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2144 Select(N.getOperand(0));
2145 Tmp1 = SelectExpr(N.getOperand(1));
2146 } else {
2147 Tmp1 = SelectExpr(N.getOperand(1));
2148 Select(N.getOperand(0));
2149 }
2150
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002151 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2152 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002153 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002154 default: assert(0 && "Unknown value type for call result!");
2155 case MVT::Other: return 1;
2156 case MVT::i1:
2157 case MVT::i8:
2158 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2159 break;
2160 case MVT::i16:
2161 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2162 break;
2163 case MVT::i32:
2164 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002165 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002166 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2167 break;
2168 case MVT::f32:
2169 case MVT::f64: // Floating-point return values live in %ST(0)
2170 ContainsFPCode = true;
2171 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2172 break;
2173 }
2174 return Result+N.ResNo;
2175 }
2176
2177 return 0;
2178}
2179
2180void ISel::Select(SDOperand N) {
2181 unsigned Tmp1, Tmp2, Opc;
2182
2183 // FIXME: Disable for our current expansion model!
2184 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
2185 return; // Already selected.
2186
Chris Lattner989de032005-01-11 06:14:36 +00002187 SDNode *Node = N.Val;
2188
2189 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002190 default:
Chris Lattner989de032005-01-11 06:14:36 +00002191 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002192 assert(0 && "Node not handled yet!");
2193 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002194 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002195 if (Node->getNumOperands() == 2) {
2196 bool OneFirst =
2197 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2198 Select(Node->getOperand(OneFirst));
2199 Select(Node->getOperand(!OneFirst));
2200 } else {
2201 std::vector<std::pair<unsigned, unsigned> > OpsP;
2202 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2203 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2204 std::sort(OpsP.begin(), OpsP.end());
2205 std::reverse(OpsP.begin(), OpsP.end());
2206 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2207 Select(Node->getOperand(OpsP[i].second));
2208 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002209 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002210 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002211 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2212 Select(N.getOperand(0));
2213 Tmp1 = SelectExpr(N.getOperand(1));
2214 } else {
2215 Tmp1 = SelectExpr(N.getOperand(1));
2216 Select(N.getOperand(0));
2217 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002218 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002219
2220 if (Tmp1 != Tmp2) {
2221 switch (N.getOperand(1).getValueType()) {
2222 default: assert(0 && "Invalid type for operation!");
2223 case MVT::i1:
2224 case MVT::i8: Opc = X86::MOV8rr; break;
2225 case MVT::i16: Opc = X86::MOV16rr; break;
2226 case MVT::i32: Opc = X86::MOV32rr; break;
2227 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002228 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002229 }
2230 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2231 }
2232 return;
2233 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002234 switch (N.getNumOperands()) {
2235 default:
2236 assert(0 && "Unknown return instruction!");
2237 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002238 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2239 N.getOperand(2).getValueType() == MVT::i32 &&
2240 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002241 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2242 Tmp1 = SelectExpr(N.getOperand(1));
2243 Tmp2 = SelectExpr(N.getOperand(2));
2244 } else {
2245 Tmp2 = SelectExpr(N.getOperand(2));
2246 Tmp1 = SelectExpr(N.getOperand(1));
2247 }
2248 Select(N.getOperand(0));
2249
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002250 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2251 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2252 // Declare that EAX & EDX are live on exit.
2253 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2254 .addReg(X86::ESP);
2255 break;
2256 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002257 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2258 Select(N.getOperand(0));
2259 Tmp1 = SelectExpr(N.getOperand(1));
2260 } else {
2261 Tmp1 = SelectExpr(N.getOperand(1));
2262 Select(N.getOperand(0));
2263 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002264 switch (N.getOperand(1).getValueType()) {
2265 default: assert(0 && "All other types should have been promoted!!");
2266 case MVT::f64:
2267 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2268 // Declare that top-of-stack is live on exit
2269 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2270 break;
2271 case MVT::i32:
2272 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2273 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2274 break;
2275 }
2276 break;
2277 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002278 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002279 break;
2280 }
2281 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2282 return;
2283 case ISD::BR: {
2284 Select(N.getOperand(0));
2285 MachineBasicBlock *Dest =
2286 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2287 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2288 return;
2289 }
2290
2291 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002292 MachineBasicBlock *Dest =
2293 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002294
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002295 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2296 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002297 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2298 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 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2306 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2307 }
Chris Lattner11333092005-01-11 03:11:44 +00002308
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002309 return;
2310 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002311
Chris Lattner4df0de92005-01-17 00:00:33 +00002312 case ISD::LOAD:
2313 // If this load could be folded into the only using instruction, and if it
2314 // is safe to emit the instruction here, try to do so now.
2315 if (Node->hasNUsesOfValue(1, 0)) {
2316 SDOperand TheVal = N.getValue(0);
2317 SDNode *User = 0;
2318 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2319 assert(UI != Node->use_end() && "Didn't find use!");
2320 SDNode *UN = *UI;
2321 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2322 if (UN->getOperand(i) == TheVal) {
2323 User = UN;
2324 goto FoundIt;
2325 }
2326 }
2327 FoundIt:
2328 // Only handle unary operators right now.
2329 if (User->getNumOperands() == 1) {
2330 LoweredTokens.erase(N);
2331 SelectExpr(SDOperand(User, 0));
2332 return;
2333 }
2334 }
2335 SelectExpr(N);
2336 return;
2337
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002338 case ISD::EXTLOAD:
2339 case ISD::SEXTLOAD:
2340 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002341 case ISD::CALL:
2342 case ISD::DYNAMIC_STACKALLOC:
2343 SelectExpr(N);
2344 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002345
2346 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2347 // On X86, we can represent all types except for Bool and Float natively.
2348 X86AddressMode AM;
2349 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002350 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2351 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2352 && "Unsupported TRUNCSTORE for this target!");
2353
2354 if (StoredTy == MVT::i16) {
2355 // FIXME: This is here just to allow testing. X86 doesn't really have a
2356 // TRUNCSTORE i16 operation, but this is required for targets that do not
2357 // have 16-bit integer registers. We occasionally disable 16-bit integer
2358 // registers to test the promotion code.
2359 Select(N.getOperand(0));
2360 Tmp1 = SelectExpr(N.getOperand(1));
2361 SelectAddress(N.getOperand(2), AM);
2362
2363 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2364 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2365 return;
2366 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002367
2368 // Store of constant bool?
2369 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2370 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2371 Select(N.getOperand(0));
2372 SelectAddress(N.getOperand(2), AM);
2373 } else {
2374 SelectAddress(N.getOperand(2), AM);
2375 Select(N.getOperand(0));
2376 }
2377 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2378 return;
2379 }
2380
2381 switch (StoredTy) {
2382 default: assert(0 && "Cannot truncstore this type!");
2383 case MVT::i1: Opc = X86::MOV8mr; break;
2384 case MVT::f32: Opc = X86::FST32m; break;
2385 }
2386
2387 std::vector<std::pair<unsigned, unsigned> > RP;
2388 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2389 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2390 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2391 std::sort(RP.begin(), RP.end());
2392
2393 for (unsigned i = 0; i != 3; ++i)
2394 switch (RP[2-i].second) {
2395 default: assert(0 && "Unknown operand number!");
2396 case 0: Select(N.getOperand(0)); break;
2397 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2398 case 2: SelectAddress(N.getOperand(2), AM); break;
2399 }
2400
2401 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2402 return;
2403 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002404 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002405 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002406
2407 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2408 Opc = 0;
2409 switch (CN->getValueType(0)) {
2410 default: assert(0 && "Invalid type for operation!");
2411 case MVT::i1:
2412 case MVT::i8: Opc = X86::MOV8mi; break;
2413 case MVT::i16: Opc = X86::MOV16mi; break;
2414 case MVT::i32: Opc = X86::MOV32mi; break;
2415 case MVT::f32:
2416 case MVT::f64: break;
2417 }
2418 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002419 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2420 Select(N.getOperand(0));
2421 SelectAddress(N.getOperand(2), AM);
2422 } else {
2423 SelectAddress(N.getOperand(2), AM);
2424 Select(N.getOperand(0));
2425 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002426 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2427 return;
2428 }
2429 }
Chris Lattner837caa72005-01-11 23:21:30 +00002430
2431 // Check to see if this is a load/op/store combination.
2432 if (N.getOperand(1).Val->hasOneUse() &&
Chris Lattner42928302005-01-12 03:16:09 +00002433 isFoldableLoad(N.getOperand(0).getValue(0)) &&
2434 !MVT::isFloatingPoint(N.getOperand(0).getValue(0).getValueType())) {
Chris Lattner837caa72005-01-11 23:21:30 +00002435 SDOperand TheLoad = N.getOperand(0).getValue(0);
2436 // Check to see if we are loading the same pointer that we're storing to.
2437 if (TheLoad.getOperand(1) == N.getOperand(2)) {
2438 // See if the stored value is a simple binary operator that uses the
2439 // load as one of its operands.
2440 SDOperand Op = N.getOperand(1);
2441 if (Op.Val->getNumOperands() == 2 &&
2442 (Op.getOperand(0) == TheLoad || Op.getOperand(1) == TheLoad)) {
2443 // Finally, check to see if this is one of the ops we can handle!
2444 static const unsigned ADDTAB[] = {
2445 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002446 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
Chris Lattner837caa72005-01-11 23:21:30 +00002447 };
Chris Lattner7ea64f52005-01-12 01:28:00 +00002448 static const unsigned SUBTAB[] = {
2449 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002450 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002451 };
2452 static const unsigned ANDTAB[] = {
2453 X86::AND8mi, X86::AND16mi, X86::AND32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002454 X86::AND8mr, X86::AND16mr, X86::AND32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002455 };
2456 static const unsigned ORTAB[] = {
2457 X86::OR8mi, X86::OR16mi, X86::OR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002458 X86::OR8mr, X86::OR16mr, X86::OR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002459 };
2460 static const unsigned XORTAB[] = {
2461 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002462 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002463 };
2464 static const unsigned SHLTAB[] = {
2465 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002466 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002467 };
2468 static const unsigned SARTAB[] = {
2469 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002470 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002471 };
2472 static const unsigned SHRTAB[] = {
2473 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002474 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002475 };
Chris Lattner837caa72005-01-11 23:21:30 +00002476
2477 const unsigned *TabPtr = 0;
2478 switch (Op.getOpcode()) {
Chris Lattner7ea64f52005-01-12 01:28:00 +00002479 default: std::cerr << "CANNOT [mem] op= val: "; Op.Val->dump(); std::cerr << "\n"; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002480 case ISD::ADD: TabPtr = ADDTAB; break;
Chris Lattner7ea64f52005-01-12 01:28:00 +00002481 case ISD::SUB: TabPtr = SUBTAB; break;
2482 case ISD::AND: TabPtr = ANDTAB; break;
2483 case ISD:: OR: TabPtr = ORTAB; break;
2484 case ISD::XOR: TabPtr = XORTAB; break;
2485 case ISD::SHL: TabPtr = SHLTAB; break;
2486 case ISD::SRA: TabPtr = SARTAB; break;
2487 case ISD::SRL: TabPtr = SHRTAB; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002488 }
2489
2490 if (TabPtr) {
2491 // Handle: [mem] op= CST
2492 SDOperand Op0 = Op.getOperand(0);
2493 SDOperand Op1 = Op.getOperand(1);
2494 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner48034fd2005-01-12 05:22:07 +00002495 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
Chris Lattner837caa72005-01-11 23:21:30 +00002496 default: break;
2497 case MVT::i1:
2498 case MVT::i8: Opc = TabPtr[0]; break;
2499 case MVT::i16: Opc = TabPtr[1]; break;
2500 case MVT::i32: Opc = TabPtr[2]; break;
2501 }
2502
2503 if (Opc) {
2504 if (getRegPressure(TheLoad.getOperand(0)) >
2505 getRegPressure(TheLoad.getOperand(1))) {
2506 Select(TheLoad.getOperand(0));
2507 SelectAddress(TheLoad.getOperand(1), AM);
2508 } else {
2509 SelectAddress(TheLoad.getOperand(1), AM);
2510 Select(TheLoad.getOperand(0));
2511 }
2512
2513 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2514 return;
2515 }
2516 }
2517
2518 // If we have [mem] = V op [mem], try to turn it into:
2519 // [mem] = [mem] op V.
Chris Lattner7ea64f52005-01-12 01:28:00 +00002520 if (Op1 == TheLoad && Op.getOpcode() != ISD::SUB &&
2521 Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRA &&
2522 Op.getOpcode() != ISD::SRL)
Chris Lattner837caa72005-01-11 23:21:30 +00002523 std::swap(Op0, Op1);
2524
2525 if (Op0 == TheLoad) {
2526 switch (Op0.getValueType()) {
2527 default: break;
2528 case MVT::i1:
2529 case MVT::i8: Opc = TabPtr[3]; break;
2530 case MVT::i16: Opc = TabPtr[4]; break;
2531 case MVT::i32: Opc = TabPtr[5]; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002532 }
2533
2534 if (Opc) {
2535 Select(TheLoad.getOperand(0));
2536 SelectAddress(TheLoad.getOperand(1), AM);
2537 unsigned Reg = SelectExpr(Op1);
2538 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addReg(Reg);
2539 return;
2540 }
2541 }
Chris Lattner837caa72005-01-11 23:21:30 +00002542 }
2543 }
2544 }
2545 }
2546
2547
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002548 switch (N.getOperand(1).getValueType()) {
2549 default: assert(0 && "Cannot store this type!");
2550 case MVT::i1:
2551 case MVT::i8: Opc = X86::MOV8mr; break;
2552 case MVT::i16: Opc = X86::MOV16mr; break;
2553 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002554 case MVT::f32: Opc = X86::FST32m; break;
2555 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002556 }
Chris Lattner11333092005-01-11 03:11:44 +00002557
2558 std::vector<std::pair<unsigned, unsigned> > RP;
2559 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2560 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2561 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2562 std::sort(RP.begin(), RP.end());
2563
2564 for (unsigned i = 0; i != 3; ++i)
2565 switch (RP[2-i].second) {
2566 default: assert(0 && "Unknown operand number!");
2567 case 0: Select(N.getOperand(0)); break;
2568 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002569 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002570 }
2571
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002572 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2573 return;
2574 }
2575 case ISD::ADJCALLSTACKDOWN:
2576 case ISD::ADJCALLSTACKUP:
2577 Select(N.getOperand(0));
2578 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2579
2580 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2581 X86::ADJCALLSTACKUP;
2582 BuildMI(BB, Opc, 1).addImm(Tmp1);
2583 return;
Chris Lattner989de032005-01-11 06:14:36 +00002584 case ISD::MEMSET: {
2585 Select(N.getOperand(0)); // Select the chain.
2586 unsigned Align =
2587 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2588 if (Align == 0) Align = 1;
2589
2590 // Turn the byte code into # iterations
2591 unsigned CountReg;
2592 unsigned Opcode;
2593 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2594 unsigned Val = ValC->getValue() & 255;
2595
2596 // If the value is a constant, then we can potentially use larger sets.
2597 switch (Align & 3) {
2598 case 2: // WORD aligned
2599 CountReg = MakeReg(MVT::i32);
2600 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2601 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2602 } else {
2603 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2604 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2605 }
2606 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2607 Opcode = X86::REP_STOSW;
2608 break;
2609 case 0: // DWORD aligned
2610 CountReg = MakeReg(MVT::i32);
2611 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2612 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2613 } else {
2614 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2615 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2616 }
2617 Val = (Val << 8) | Val;
2618 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2619 Opcode = X86::REP_STOSD;
2620 break;
2621 default: // BYTE aligned
2622 CountReg = SelectExpr(Node->getOperand(3));
2623 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2624 Opcode = X86::REP_STOSB;
2625 break;
2626 }
2627 } else {
2628 // If it's not a constant value we are storing, just fall back. We could
2629 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2630 unsigned ValReg = SelectExpr(Node->getOperand(2));
2631 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2632 CountReg = SelectExpr(Node->getOperand(3));
2633 Opcode = X86::REP_STOSB;
2634 }
2635
2636 // No matter what the alignment is, we put the source in ESI, the
2637 // destination in EDI, and the count in ECX.
2638 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2639 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2640 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2641 BuildMI(BB, Opcode, 0);
2642 return;
2643 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002644 case ISD::MEMCPY:
2645 Select(N.getOperand(0)); // Select the chain.
2646 unsigned Align =
2647 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2648 if (Align == 0) Align = 1;
2649
2650 // Turn the byte code into # iterations
2651 unsigned CountReg;
2652 unsigned Opcode;
2653 switch (Align & 3) {
2654 case 2: // WORD aligned
2655 CountReg = MakeReg(MVT::i32);
2656 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2657 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2658 } else {
2659 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2660 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2661 }
2662 Opcode = X86::REP_MOVSW;
2663 break;
2664 case 0: // DWORD aligned
2665 CountReg = MakeReg(MVT::i32);
2666 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2667 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2668 } else {
2669 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2670 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2671 }
2672 Opcode = X86::REP_MOVSD;
2673 break;
2674 default: // BYTE aligned
2675 CountReg = SelectExpr(Node->getOperand(3));
2676 Opcode = X86::REP_MOVSB;
2677 break;
2678 }
2679
2680 // No matter what the alignment is, we put the source in ESI, the
2681 // destination in EDI, and the count in ECX.
2682 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2683 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2684 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2685 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2686 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2687 BuildMI(BB, Opcode, 0);
2688 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002689 }
2690 assert(0 && "Should not be reached!");
2691}
2692
2693
2694/// createX86PatternInstructionSelector - This pass converts an LLVM function
2695/// into a machine code representation using pattern matching and a machine
2696/// description file.
2697///
2698FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2699 return new ISel(TM);
2700}