blob: a6932faea0ddcbfd2bff31c73de21b1590d49376 [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.
42 addRegisterClass(MVT::i8, X86::R8RegisterClass);
43 addRegisterClass(MVT::i16, X86::R16RegisterClass);
44 addRegisterClass(MVT::i32, X86::R32RegisterClass);
45 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
46
47 // FIXME: Eliminate these two classes when legalize can handle promotions
48 // well.
49 addRegisterClass(MVT::i1, X86::R8RegisterClass);
50 addRegisterClass(MVT::f32, X86::RFPRegisterClass);
51
52 computeRegisterProperties();
Chris Lattner795069d2005-01-11 05:57:36 +000053
Chris Lattner795069d2005-01-11 05:57:36 +000054 setOperationUnsupported(ISD::MEMMOVE, MVT::Other);
55
Chris Lattnere9ef81d2005-01-15 05:22:24 +000056 //setOperationUnsupported(ISD::SEXTLOAD, MVT::i1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000057 setOperationUnsupported(ISD::SELECT, MVT::i1);
58 setOperationUnsupported(ISD::SELECT, MVT::i8);
59
60 addLegalFPImmediate(+0.0); // FLD0
61 addLegalFPImmediate(+1.0); // FLD1
62 addLegalFPImmediate(-0.0); // FLD0/FCHS
63 addLegalFPImmediate(-1.0); // FLD1/FCHS
64 }
65
66 /// LowerArguments - This hook must be implemented to indicate how we should
67 /// lower the arguments for the specified function, into the specified DAG.
68 virtual std::vector<SDOperand>
69 LowerArguments(Function &F, SelectionDAG &DAG);
70
71 /// LowerCallTo - This hook lowers an abstract call to a function into an
72 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000073 virtual std::pair<SDOperand, SDOperand>
74 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
75 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000076
77 virtual std::pair<SDOperand, SDOperand>
78 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
79
80 virtual std::pair<SDOperand,SDOperand>
81 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
82 const Type *ArgTy, SelectionDAG &DAG);
83
84 virtual std::pair<SDOperand, SDOperand>
85 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
86 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000087 };
88}
89
90
91std::vector<SDOperand>
92X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
93 std::vector<SDOperand> ArgValues;
94
95 // Add DAG nodes to load the arguments... On entry to a function on the X86,
96 // the stack frame looks like this:
97 //
98 // [ESP] -- return address
99 // [ESP + 4] -- first argument (leftmost lexically)
100 // [ESP + 8] -- second argument, if first argument is four bytes in size
101 // ...
102 //
103 MachineFunction &MF = DAG.getMachineFunction();
104 MachineFrameInfo *MFI = MF.getFrameInfo();
105
106 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
107 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
108 MVT::ValueType ObjectVT = getValueType(I->getType());
109 unsigned ArgIncrement = 4;
110 unsigned ObjSize;
111 switch (ObjectVT) {
112 default: assert(0 && "Unhandled argument type!");
113 case MVT::i1:
114 case MVT::i8: ObjSize = 1; break;
115 case MVT::i16: ObjSize = 2; break;
116 case MVT::i32: ObjSize = 4; break;
117 case MVT::i64: ObjSize = ArgIncrement = 8; break;
118 case MVT::f32: ObjSize = 4; break;
119 case MVT::f64: ObjSize = ArgIncrement = 8; break;
120 }
121 // Create the frame index object for this incoming parameter...
122 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
123
124 // Create the SelectionDAG nodes corresponding to a load from this parameter
125 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
126
127 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
128 // dead loads.
129 SDOperand ArgValue;
130 if (!I->use_empty())
131 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
132 else {
133 if (MVT::isInteger(ObjectVT))
134 ArgValue = DAG.getConstant(0, ObjectVT);
135 else
136 ArgValue = DAG.getConstantFP(0, ObjectVT);
137 }
138 ArgValues.push_back(ArgValue);
139
140 ArgOffset += ArgIncrement; // Move on to the next argument...
141 }
142
143 // If the function takes variable number of arguments, make a frame index for
144 // the start of the first vararg value... for expansion of llvm.va_start.
145 if (F.isVarArg())
146 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000147 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000148 return ArgValues;
149}
150
Chris Lattner5188ad72005-01-08 19:28:19 +0000151std::pair<SDOperand, SDOperand>
152X86TargetLowering::LowerCallTo(SDOperand Chain,
153 const Type *RetTy, SDOperand Callee,
154 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000155 // Count how many bytes are to be pushed on the stack.
156 unsigned NumBytes = 0;
157
158 if (Args.empty()) {
159 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000160 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
161 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000162 } else {
163 for (unsigned i = 0, e = Args.size(); i != e; ++i)
164 switch (getValueType(Args[i].second)) {
165 default: assert(0 && "Unknown value type!");
166 case MVT::i1:
167 case MVT::i8:
168 case MVT::i16:
169 case MVT::i32:
170 case MVT::f32:
171 NumBytes += 4;
172 break;
173 case MVT::i64:
174 case MVT::f64:
175 NumBytes += 8;
176 break;
177 }
178
Chris Lattner5188ad72005-01-08 19:28:19 +0000179 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
180 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000181
182 // Arguments go on the stack in reverse order, as specified by the ABI.
183 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000184 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
185 DAG.getEntryNode());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000186 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
187 unsigned ArgReg;
188 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
189 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
190
191 switch (getValueType(Args[i].second)) {
192 default: assert(0 && "Unexpected ValueType for argument!");
193 case MVT::i1:
194 case MVT::i8:
195 case MVT::i16:
196 // Promote the integer to 32 bits. If the input type is signed use a
197 // sign extend, otherwise use a zero extend.
198 if (Args[i].second->isSigned())
199 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
200 else
201 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
202
203 // FALL THROUGH
204 case MVT::i32:
205 case MVT::f32:
206 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000207 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
208 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000209 ArgOffset += 4;
210 break;
211 case MVT::i64:
212 case MVT::f64:
213 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000214 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
215 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000216 ArgOffset += 8;
217 break;
218 }
219 }
220 }
221
222 std::vector<MVT::ValueType> RetVals;
223 MVT::ValueType RetTyVT = getValueType(RetTy);
224 if (RetTyVT != MVT::isVoid)
225 RetVals.push_back(RetTyVT);
226 RetVals.push_back(MVT::Other);
227
Chris Lattner5188ad72005-01-08 19:28:19 +0000228 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000229 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000230 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
231 DAG.getConstant(NumBytes, getPointerTy()));
232 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000233}
234
Chris Lattner14824582005-01-09 00:01:27 +0000235std::pair<SDOperand, SDOperand>
236X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
237 // vastart just returns the address of the VarArgsFrameIndex slot.
238 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
239}
240
241std::pair<SDOperand,SDOperand> X86TargetLowering::
242LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
243 const Type *ArgTy, SelectionDAG &DAG) {
244 MVT::ValueType ArgVT = getValueType(ArgTy);
245 SDOperand Result;
246 if (!isVANext) {
247 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
248 } else {
249 unsigned Amt;
250 if (ArgVT == MVT::i32)
251 Amt = 4;
252 else {
253 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
254 "Other types should have been promoted for varargs!");
255 Amt = 8;
256 }
257 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
258 DAG.getConstant(Amt, VAList.getValueType()));
259 }
260 return std::make_pair(Result, Chain);
261}
262
263
264std::pair<SDOperand, SDOperand> X86TargetLowering::
265LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
266 SelectionDAG &DAG) {
267 SDOperand Result;
268 if (Depth) // Depths > 0 not supported yet!
269 Result = DAG.getConstant(0, getPointerTy());
270 else {
271 if (ReturnAddrIndex == 0) {
272 // Set up a frame object for the return address.
273 MachineFunction &MF = DAG.getMachineFunction();
274 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
275 }
276
277 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
278
279 if (!isFrameAddress)
280 // Just load the return address
281 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
282 else
283 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
284 DAG.getConstant(4, MVT::i32));
285 }
286 return std::make_pair(Result, Chain);
287}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000288
289
290
291
292
293namespace {
294 Statistic<>
295 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
296
297 //===--------------------------------------------------------------------===//
298 /// ISel - X86 specific code to select X86 machine instructions for
299 /// SelectionDAG operations.
300 ///
301 class ISel : public SelectionDAGISel {
302 /// ContainsFPCode - Every instruction we select that uses or defines a FP
303 /// register should set this to true.
304 bool ContainsFPCode;
305
306 /// X86Lowering - This object fully describes how to lower LLVM code to an
307 /// X86-specific SelectionDAG.
308 X86TargetLowering X86Lowering;
309
Chris Lattner11333092005-01-11 03:11:44 +0000310 /// RegPressureMap - This keeps an approximate count of the number of
311 /// registers required to evaluate each node in the graph.
312 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000313
314 /// ExprMap - As shared expressions are codegen'd, we keep track of which
315 /// vreg the value is produced in, so we only emit one copy of each compiled
316 /// tree.
317 std::map<SDOperand, unsigned> ExprMap;
318 std::set<SDOperand> LoweredTokens;
319
320 public:
321 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
322 }
323
Chris Lattner11333092005-01-11 03:11:44 +0000324 unsigned getRegPressure(SDOperand O) {
325 return RegPressureMap[O.Val];
326 }
327 unsigned ComputeRegPressure(SDOperand O);
328
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000329 /// InstructionSelectBasicBlock - This callback is invoked by
330 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000331 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000332
Chris Lattnera5ade062005-01-11 21:19:59 +0000333 bool isFoldableLoad(SDOperand Op);
334 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
335
336
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000337 void EmitCMP(SDOperand LHS, SDOperand RHS);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000338 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000339 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
340 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000341 unsigned SelectExpr(SDOperand N);
342 bool SelectAddress(SDOperand N, X86AddressMode &AM);
343 void Select(SDOperand N);
344 };
345}
346
Chris Lattner7dbcb752005-01-12 04:21:28 +0000347/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
348/// when it has created a SelectionDAG for us to codegen.
349void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
350 // While we're doing this, keep track of whether we see any FP code for
351 // FP_REG_KILL insertion.
352 ContainsFPCode = false;
353
354 // Scan the PHI nodes that already are inserted into this basic block. If any
355 // of them is a PHI of a floating point value, we need to insert an
356 // FP_REG_KILL.
357 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
358 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
359 I != E; ++I) {
360 assert(I->getOpcode() == X86::PHI &&
361 "Isn't just PHI nodes?");
362 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
363 X86::RFPRegisterClass) {
364 ContainsFPCode = true;
365 break;
366 }
367 }
368
369 // Compute the RegPressureMap, which is an approximation for the number of
370 // registers required to compute each node.
371 ComputeRegPressure(DAG.getRoot());
372
373 // Codegen the basic block.
374 Select(DAG.getRoot());
375
376 // Finally, look at all of the successors of this block. If any contain a PHI
377 // node of FP type, we need to insert an FP_REG_KILL in this block.
378 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
379 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
380 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
381 I != E && I->getOpcode() == X86::PHI; ++I) {
382 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
383 X86::RFPRegisterClass) {
384 ContainsFPCode = true;
385 break;
386 }
387 }
388
389 // Insert FP_REG_KILL instructions into basic blocks that need them. This
390 // only occurs due to the floating point stackifier not being aggressive
391 // enough to handle arbitrary global stackification.
392 //
393 // Currently we insert an FP_REG_KILL instruction into each block that uses or
394 // defines a floating point virtual register.
395 //
396 // When the global register allocators (like linear scan) finally update live
397 // variable analysis, we can keep floating point values in registers across
398 // basic blocks. This will be a huge win, but we are waiting on the global
399 // allocators before we can do this.
400 //
401 if (ContainsFPCode && BB->succ_size()) {
402 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
403 ++NumFPKill;
404 }
405
406 // Clear state used for selection.
407 ExprMap.clear();
408 LoweredTokens.clear();
409 RegPressureMap.clear();
410}
411
412
Chris Lattner11333092005-01-11 03:11:44 +0000413// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
414// for the number of registers required to compute each node. This is basically
415// computing a generalized form of the Sethi-Ullman number for each node.
416unsigned ISel::ComputeRegPressure(SDOperand O) {
417 SDNode *N = O.Val;
418 unsigned &Result = RegPressureMap[N];
419 if (Result) return Result;
420
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000421 // FIXME: Should operations like CALL (which clobber lots o regs) have a
422 // higher fixed cost??
423
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000424 if (N->getNumOperands() == 0) {
425 Result = 1;
426 } else {
427 unsigned MaxRegUse = 0;
428 unsigned NumExtraMaxRegUsers = 0;
429 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
430 unsigned Regs;
431 if (N->getOperand(i).getOpcode() == ISD::Constant)
432 Regs = 0;
433 else
434 Regs = ComputeRegPressure(N->getOperand(i));
435 if (Regs > MaxRegUse) {
436 MaxRegUse = Regs;
437 NumExtraMaxRegUsers = 0;
438 } else if (Regs == MaxRegUse &&
439 N->getOperand(i).getValueType() != MVT::Other) {
440 ++NumExtraMaxRegUsers;
441 }
Chris Lattner11333092005-01-11 03:11:44 +0000442 }
Chris Lattner11333092005-01-11 03:11:44 +0000443
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000444 Result = MaxRegUse+NumExtraMaxRegUsers;
445 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000446
Chris Lattner837caa72005-01-11 23:21:30 +0000447 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000448 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000449}
450
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000451/// SelectAddress - Add the specified node to the specified addressing mode,
452/// returning true if it cannot be done.
453bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
454 switch (N.getOpcode()) {
455 default: break;
456 case ISD::FrameIndex:
457 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
458 AM.BaseType = X86AddressMode::FrameIndexBase;
459 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
460 return false;
461 }
462 break;
463 case ISD::GlobalAddress:
464 if (AM.GV == 0) {
465 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
466 return false;
467 }
468 break;
469 case ISD::Constant:
470 AM.Disp += cast<ConstantSDNode>(N)->getValue();
471 return false;
472 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000473 // We might have folded the load into this shift, so don't regen the value
474 // if so.
475 if (ExprMap.count(N)) break;
476
Chris Lattner2b937862005-01-12 07:33:20 +0000477 if (AM.IndexReg == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000478 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
479 unsigned Val = CN->getValue();
480 if (Val == 1 || Val == 2 || Val == 3) {
481 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000482 SDOperand ShVal = N.Val->getOperand(0);
483
484 // Okay, we know that we have a scale by now. However, if the scaled
485 // value is an add of something and a constant, we can fold the
486 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000487 if (ShVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(ShVal) &&
Chris Lattner51a26342005-01-11 06:36:20 +0000488 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
489 AM.IndexReg = SelectExpr(ShVal.Val->getOperand(0));
490 ConstantSDNode *AddVal =
491 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
492 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000493 } else {
Chris Lattner51a26342005-01-11 06:36:20 +0000494 AM.IndexReg = SelectExpr(ShVal);
495 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000496 return false;
497 }
498 }
499 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000500 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000501 // We might have folded the load into this mul, so don't regen the value if
502 // so.
503 if (ExprMap.count(N)) break;
504
Chris Lattner947d5442005-01-11 19:37:02 +0000505 // X*[3,5,9] -> X+X*[2,4,8]
506 if (AM.IndexReg == 0 && AM.BaseType == X86AddressMode::RegBase &&
507 AM.Base.Reg == 0)
508 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
509 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
510 AM.Scale = unsigned(CN->getValue())-1;
511
512 SDOperand MulVal = N.Val->getOperand(0);
513 unsigned Reg;
514
515 // Okay, we know that we have a scale by now. However, if the scaled
516 // value is an add of something and a constant, we can fold the
517 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000518 if (MulVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(MulVal) &&
Chris Lattner947d5442005-01-11 19:37:02 +0000519 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
520 Reg = SelectExpr(MulVal.Val->getOperand(0));
521 ConstantSDNode *AddVal =
522 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
523 AM.Disp += AddVal->getValue() * CN->getValue();
524 } else {
525 Reg = SelectExpr(N.Val->getOperand(0));
526 }
527
528 AM.IndexReg = AM.Base.Reg = Reg;
529 return false;
530 }
531 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000532
533 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000534 // We might have folded the load into this mul, so don't regen the value if
535 // so.
536 if (ExprMap.count(N)) break;
537
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000538 X86AddressMode Backup = AM;
539 if (!SelectAddress(N.Val->getOperand(0), AM) &&
540 !SelectAddress(N.Val->getOperand(1), AM))
541 return false;
542 AM = Backup;
Chris Lattner9bbd9922005-01-12 18:08:53 +0000543 if (!SelectAddress(N.Val->getOperand(1), AM) &&
544 !SelectAddress(N.Val->getOperand(0), AM))
545 return false;
546 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000547 break;
548 }
549 }
550
Chris Lattnera95589b2005-01-11 04:40:19 +0000551 // Is the base register already occupied?
552 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
553 // If so, check to see if the scale index register is set.
554 if (AM.IndexReg == 0) {
555 AM.IndexReg = SelectExpr(N);
556 AM.Scale = 1;
557 return false;
558 }
559
560 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000561 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000562 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000563
564 // Default, generate it as a register.
565 AM.BaseType = X86AddressMode::RegBase;
566 AM.Base.Reg = SelectExpr(N);
567 return false;
568}
569
570/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
571/// assuming that the temporary registers are in the 8-bit register class.
572///
573/// Tmp1 = setcc1
574/// Tmp2 = setcc2
575/// DestReg = logicalop Tmp1, Tmp2
576///
577static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
578 unsigned SetCC2, unsigned LogicalOp,
579 unsigned DestReg) {
580 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
581 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
582 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
583 BuildMI(BB, SetCC1, 0, Tmp1);
584 BuildMI(BB, SetCC2, 0, Tmp2);
585 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
586}
587
588/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
589/// condition codes match the specified SetCCOpcode. Note that some conditions
590/// require multiple instructions to generate the correct value.
591static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
592 ISD::CondCode SetCCOpcode, bool isFP) {
593 unsigned Opc;
594 if (!isFP) {
595 switch (SetCCOpcode) {
596 default: assert(0 && "Illegal integer SetCC!");
597 case ISD::SETEQ: Opc = X86::SETEr; break;
598 case ISD::SETGT: Opc = X86::SETGr; break;
599 case ISD::SETGE: Opc = X86::SETGEr; break;
600 case ISD::SETLT: Opc = X86::SETLr; break;
601 case ISD::SETLE: Opc = X86::SETLEr; break;
602 case ISD::SETNE: Opc = X86::SETNEr; break;
603 case ISD::SETULT: Opc = X86::SETBr; break;
604 case ISD::SETUGT: Opc = X86::SETAr; break;
605 case ISD::SETULE: Opc = X86::SETBEr; break;
606 case ISD::SETUGE: Opc = X86::SETAEr; break;
607 }
608 } else {
609 // On a floating point condition, the flags are set as follows:
610 // ZF PF CF op
611 // 0 | 0 | 0 | X > Y
612 // 0 | 0 | 1 | X < Y
613 // 1 | 0 | 0 | X == Y
614 // 1 | 1 | 1 | unordered
615 //
616 switch (SetCCOpcode) {
617 default: assert(0 && "Invalid FP setcc!");
618 case ISD::SETUEQ:
619 case ISD::SETEQ:
620 Opc = X86::SETEr; // True if ZF = 1
621 break;
622 case ISD::SETOGT:
623 case ISD::SETGT:
624 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
625 break;
626 case ISD::SETOGE:
627 case ISD::SETGE:
628 Opc = X86::SETAEr; // True if CF = 0
629 break;
630 case ISD::SETULT:
631 case ISD::SETLT:
632 Opc = X86::SETBr; // True if CF = 1
633 break;
634 case ISD::SETULE:
635 case ISD::SETLE:
636 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
637 break;
638 case ISD::SETONE:
639 case ISD::SETNE:
640 Opc = X86::SETNEr; // True if ZF = 0
641 break;
642 case ISD::SETUO:
643 Opc = X86::SETPr; // True if PF = 1
644 break;
645 case ISD::SETO:
646 Opc = X86::SETNPr; // True if PF = 0
647 break;
648 case ISD::SETOEQ: // !PF & ZF
649 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
650 return;
651 case ISD::SETOLT: // !PF & CF
652 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
653 return;
654 case ISD::SETOLE: // !PF & (CF || ZF)
655 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
656 return;
657 case ISD::SETUGT: // PF | (!ZF & !CF)
658 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
659 return;
660 case ISD::SETUGE: // PF | !CF
661 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
662 return;
663 case ISD::SETUNE: // PF | !ZF
664 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
665 return;
666 }
667 }
668 BuildMI(BB, Opc, 0, DestReg);
669}
670
671
672/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
673/// the Dest block if the Cond condition is true. If we cannot fold this
674/// condition into the branch, return true.
675///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000676bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
677 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000678 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
679 // B) using two conditional branches instead of one condbr, two setcc's, and
680 // an or.
681 if ((Cond.getOpcode() == ISD::OR ||
682 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
683 // And and or set the flags for us, so there is no need to emit a TST of the
684 // result. It is only safe to do this if there is only a single use of the
685 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000686 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000687 SelectExpr(Cond);
688 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
689 return false;
690 }
691
692 // Codegen br not C -> JE.
693 if (Cond.getOpcode() == ISD::XOR)
694 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
695 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000696 unsigned CondR;
697 if (getRegPressure(Chain) > getRegPressure(Cond)) {
698 Select(Chain);
699 CondR = SelectExpr(Cond.Val->getOperand(0));
700 } else {
701 CondR = SelectExpr(Cond.Val->getOperand(0));
702 Select(Chain);
703 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000704 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
705 BuildMI(BB, X86::JE, 1).addMBB(Dest);
706 return false;
707 }
708
709 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
710 if (SetCC == 0)
711 return true; // Can only handle simple setcc's so far.
712
713 unsigned Opc;
714
715 // Handle integer conditions first.
716 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
717 switch (SetCC->getCondition()) {
718 default: assert(0 && "Illegal integer SetCC!");
719 case ISD::SETEQ: Opc = X86::JE; break;
720 case ISD::SETGT: Opc = X86::JG; break;
721 case ISD::SETGE: Opc = X86::JGE; break;
722 case ISD::SETLT: Opc = X86::JL; break;
723 case ISD::SETLE: Opc = X86::JLE; break;
724 case ISD::SETNE: Opc = X86::JNE; break;
725 case ISD::SETULT: Opc = X86::JB; break;
726 case ISD::SETUGT: Opc = X86::JA; break;
727 case ISD::SETULE: Opc = X86::JBE; break;
728 case ISD::SETUGE: Opc = X86::JAE; break;
729 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000730 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000731 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
732 BuildMI(BB, Opc, 1).addMBB(Dest);
733 return false;
734 }
735
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000736 unsigned Opc2 = 0; // Second branch if needed.
737
738 // On a floating point condition, the flags are set as follows:
739 // ZF PF CF op
740 // 0 | 0 | 0 | X > Y
741 // 0 | 0 | 1 | X < Y
742 // 1 | 0 | 0 | X == Y
743 // 1 | 1 | 1 | unordered
744 //
745 switch (SetCC->getCondition()) {
746 default: assert(0 && "Invalid FP setcc!");
747 case ISD::SETUEQ:
748 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
749 case ISD::SETOGT:
750 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
751 case ISD::SETOGE:
752 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
753 case ISD::SETULT:
754 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
755 case ISD::SETULE:
756 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
757 case ISD::SETONE:
758 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
759 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
760 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
761 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
762 Opc = X86::JA; // ZF = 0 & CF = 0
763 Opc2 = X86::JP; // PF = 1
764 break;
765 case ISD::SETUGE: // PF = 1 | CF = 0
766 Opc = X86::JAE; // CF = 0
767 Opc2 = X86::JP; // PF = 1
768 break;
769 case ISD::SETUNE: // PF = 1 | ZF = 0
770 Opc = X86::JNE; // ZF = 0
771 Opc2 = X86::JP; // PF = 1
772 break;
773 case ISD::SETOEQ: // PF = 0 & ZF = 1
774 //X86::JNP, X86::JE
775 //X86::AND8rr
776 return true; // FIXME: Emit more efficient code for this branch.
777 case ISD::SETOLT: // PF = 0 & CF = 1
778 //X86::JNP, X86::JB
779 //X86::AND8rr
780 return true; // FIXME: Emit more efficient code for this branch.
781 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
782 //X86::JNP, X86::JBE
783 //X86::AND8rr
784 return true; // FIXME: Emit more efficient code for this branch.
785 }
786
Chris Lattner6c07aee2005-01-11 04:06:27 +0000787 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000788 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
789 BuildMI(BB, Opc, 1).addMBB(Dest);
790 if (Opc2)
791 BuildMI(BB, Opc2, 1).addMBB(Dest);
792 return false;
793}
794
Chris Lattner24aad1b2005-01-10 22:10:13 +0000795/// EmitSelectCC - Emit code into BB that performs a select operation between
796/// the two registers RTrue and RFalse, generating a result into RDest. Return
797/// true if the fold cannot be performed.
798///
799void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
800 unsigned RTrue, unsigned RFalse, unsigned RDest) {
801 enum Condition {
802 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
803 NOT_SET
804 } CondCode = NOT_SET;
805
806 static const unsigned CMOVTAB16[] = {
807 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
808 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
809 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
810 };
811 static const unsigned CMOVTAB32[] = {
812 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
813 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
814 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
815 };
816 static const unsigned CMOVTABFP[] = {
817 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
818 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
819 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
820 };
821
822 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
823 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
824 switch (SetCC->getCondition()) {
825 default: assert(0 && "Unknown integer comparison!");
826 case ISD::SETEQ: CondCode = EQ; break;
827 case ISD::SETGT: CondCode = GT; break;
828 case ISD::SETGE: CondCode = GE; break;
829 case ISD::SETLT: CondCode = LT; break;
830 case ISD::SETLE: CondCode = LE; break;
831 case ISD::SETNE: CondCode = NE; break;
832 case ISD::SETULT: CondCode = B; break;
833 case ISD::SETUGT: CondCode = A; break;
834 case ISD::SETULE: CondCode = BE; break;
835 case ISD::SETUGE: CondCode = AE; break;
836 }
837 } else {
838 // On a floating point condition, the flags are set as follows:
839 // ZF PF CF op
840 // 0 | 0 | 0 | X > Y
841 // 0 | 0 | 1 | X < Y
842 // 1 | 0 | 0 | X == Y
843 // 1 | 1 | 1 | unordered
844 //
845 switch (SetCC->getCondition()) {
846 default: assert(0 && "Unknown FP comparison!");
847 case ISD::SETUEQ:
848 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
849 case ISD::SETOGT:
850 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
851 case ISD::SETOGE:
852 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
853 case ISD::SETULT:
854 case ISD::SETLT: CondCode = B; break; // True if CF = 1
855 case ISD::SETULE:
856 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
857 case ISD::SETONE:
858 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
859 case ISD::SETUO: CondCode = P; break; // True if PF = 1
860 case ISD::SETO: CondCode = NP; break; // True if PF = 0
861 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
862 case ISD::SETUGE: // PF = 1 | CF = 0
863 case ISD::SETUNE: // PF = 1 | ZF = 0
864 case ISD::SETOEQ: // PF = 0 & ZF = 1
865 case ISD::SETOLT: // PF = 0 & CF = 1
866 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
867 // We cannot emit this comparison as a single cmov.
868 break;
869 }
870 }
871 }
872
873 unsigned Opc = 0;
874 if (CondCode != NOT_SET) {
875 switch (SVT) {
876 default: assert(0 && "Cannot select this type!");
877 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
878 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
879 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000880 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000881 }
882 }
883
884 // Finally, if we weren't able to fold this, just emit the condition and test
885 // it.
886 if (CondCode == NOT_SET || Opc == 0) {
887 // Get the condition into the zero flag.
888 unsigned CondReg = SelectExpr(Cond);
889 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
890
891 switch (SVT) {
892 default: assert(0 && "Cannot select this type!");
893 case MVT::i16: Opc = X86::CMOVE16rr; break;
894 case MVT::i32: Opc = X86::CMOVE32rr; break;
895 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000896 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000897 }
898 } else {
899 // FIXME: CMP R, 0 -> TEST R, R
900 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000901 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000902 }
903 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
904}
905
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000906void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000907 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000908 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
909 Opc = 0;
Chris Lattneref6806c2005-01-12 02:02:48 +0000910 if (isFoldableLoad(LHS)) {
911 switch (RHS.getValueType()) {
912 default: break;
913 case MVT::i1:
914 case MVT::i8: Opc = X86::CMP8mi; break;
915 case MVT::i16: Opc = X86::CMP16mi; break;
916 case MVT::i32: Opc = X86::CMP32mi; break;
917 }
918 if (Opc) {
919 X86AddressMode AM;
920 EmitFoldedLoad(LHS, AM);
921 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
922 return;
923 }
924 }
925
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000926 switch (RHS.getValueType()) {
927 default: break;
928 case MVT::i1:
929 case MVT::i8: Opc = X86::CMP8ri; break;
930 case MVT::i16: Opc = X86::CMP16ri; break;
931 case MVT::i32: Opc = X86::CMP32ri; break;
932 }
933 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000934 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000935 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
936 return;
937 }
Chris Lattner7f2afac2005-01-14 22:37:41 +0000938 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
939 if (CN->isExactlyValue(+0.0) ||
940 CN->isExactlyValue(-0.0)) {
941 unsigned Reg = SelectExpr(LHS);
942 BuildMI(BB, X86::FTST, 1).addReg(Reg);
943 BuildMI(BB, X86::FNSTSW8r, 0);
944 BuildMI(BB, X86::SAHF, 1);
945 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000946 }
947
Chris Lattneref6806c2005-01-12 02:02:48 +0000948 Opc = 0;
949 if (isFoldableLoad(LHS)) {
950 switch (RHS.getValueType()) {
951 default: break;
952 case MVT::i1:
953 case MVT::i8: Opc = X86::CMP8mr; break;
954 case MVT::i16: Opc = X86::CMP16mr; break;
955 case MVT::i32: Opc = X86::CMP32mr; break;
956 }
957 if (Opc) {
958 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +0000959 EmitFoldedLoad(LHS, AM);
960 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +0000961 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
962 return;
963 }
964 }
965
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000966 switch (LHS.getValueType()) {
967 default: assert(0 && "Cannot compare this value!");
968 case MVT::i1:
969 case MVT::i8: Opc = X86::CMP8rr; break;
970 case MVT::i16: Opc = X86::CMP16rr; break;
971 case MVT::i32: Opc = X86::CMP32rr; break;
972 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000973 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000974 }
Chris Lattner11333092005-01-11 03:11:44 +0000975 unsigned Tmp1, Tmp2;
976 if (getRegPressure(LHS) > getRegPressure(RHS)) {
977 Tmp1 = SelectExpr(LHS);
978 Tmp2 = SelectExpr(RHS);
979 } else {
980 Tmp2 = SelectExpr(RHS);
981 Tmp1 = SelectExpr(LHS);
982 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000983 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
984}
985
Chris Lattnera5ade062005-01-11 21:19:59 +0000986/// isFoldableLoad - Return true if this is a load instruction that can safely
987/// be folded into an operation that uses it.
988bool ISel::isFoldableLoad(SDOperand Op) {
989 if (Op.getOpcode() != ISD::LOAD ||
990 // FIXME: currently can't fold constant pool indexes.
991 isa<ConstantPoolSDNode>(Op.getOperand(1)))
992 return false;
993
994 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +0000995 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
996 if (ExprMap.count(Op.getValue(1))) return false;
997 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
998 assert(!LoweredTokens.count(Op.getValue(1)) &&
999 "Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001000
Chris Lattnera0bb6922005-01-12 18:38:26 +00001001 // Finally, there can only be one use of its value.
1002 return Op.Val->hasNUsesOfValue(1, 0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001003}
1004
1005/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1006/// and compute the address being loaded into AM.
1007void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1008 SDOperand Chain = Op.getOperand(0);
1009 SDOperand Address = Op.getOperand(1);
1010 if (getRegPressure(Chain) > getRegPressure(Address)) {
1011 Select(Chain);
1012 SelectAddress(Address, AM);
1013 } else {
1014 SelectAddress(Address, AM);
1015 Select(Chain);
1016 }
1017
1018 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001019 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1020 "Load emitted more than once?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001021 ExprMap[SDOperand(Op.Val, 1)] = 1;
Chris Lattner636e79a2005-01-13 05:53:16 +00001022 if (!LoweredTokens.insert(Op.getValue(1)).second)
1023 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001024}
1025
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001026unsigned ISel::SelectExpr(SDOperand N) {
1027 unsigned Result;
1028 unsigned Tmp1, Tmp2, Tmp3;
1029 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001030 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001031 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001032
Chris Lattner7f2afac2005-01-14 22:37:41 +00001033 if (Node->getOpcode() == ISD::CopyFromReg) {
1034 // FIXME: Handle copy from physregs!
1035
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001036 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001037 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001038 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001039
1040 unsigned &Reg = ExprMap[N];
1041 if (Reg) return Reg;
1042
1043 if (N.getOpcode() != ISD::CALL)
1044 Reg = Result = (N.getValueType() != MVT::Other) ?
1045 MakeReg(N.getValueType()) : 1;
1046 else {
1047 // If this is a call instruction, make sure to prepare ALL of the result
1048 // values as well as the chain.
1049 if (Node->getNumValues() == 1)
1050 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001051 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001052 Result = MakeReg(Node->getValueType(0));
1053 ExprMap[N.getValue(0)] = Result;
1054 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1055 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1056 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001057 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001058 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001059
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001060 switch (N.getOpcode()) {
1061 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001062 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001063 assert(0 && "Node not handled!\n");
1064 case ISD::FrameIndex:
1065 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1066 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1067 return Result;
1068 case ISD::ConstantPool:
1069 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1070 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1071 return Result;
1072 case ISD::ConstantFP:
1073 ContainsFPCode = true;
1074 Tmp1 = Result; // Intermediate Register
1075 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1076 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1077 Tmp1 = MakeReg(MVT::f64);
1078
1079 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1080 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1081 BuildMI(BB, X86::FLD0, 0, Tmp1);
1082 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1083 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1084 BuildMI(BB, X86::FLD1, 0, Tmp1);
1085 else
1086 assert(0 && "Unexpected constant!");
1087 if (Tmp1 != Result)
1088 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1089 return Result;
1090 case ISD::Constant:
1091 switch (N.getValueType()) {
1092 default: assert(0 && "Cannot use constants of this type!");
1093 case MVT::i1:
1094 case MVT::i8: Opc = X86::MOV8ri; break;
1095 case MVT::i16: Opc = X86::MOV16ri; break;
1096 case MVT::i32: Opc = X86::MOV32ri; break;
1097 }
1098 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1099 return Result;
1100 case ISD::GlobalAddress: {
1101 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1102 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1103 return Result;
1104 }
1105 case ISD::ExternalSymbol: {
1106 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1107 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1108 return Result;
1109 }
1110 case ISD::FP_EXTEND:
1111 Tmp1 = SelectExpr(N.getOperand(0));
1112 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001113 return Result;
1114 case ISD::ZERO_EXTEND: {
1115 int DestIs16 = N.getValueType() == MVT::i16;
1116 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001117
1118 // FIXME: This hack is here for zero extension casts from bool to i8. This
1119 // would not be needed if bools were promoted by Legalize.
1120 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001121 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001122 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1123 return Result;
1124 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001125
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001126 if (isFoldableLoad(N.getOperand(0))) {
1127 static const unsigned Opc[3] = {
1128 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1129 };
1130
1131 X86AddressMode AM;
1132 EmitFoldedLoad(N.getOperand(0), AM);
1133 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1134
1135 return Result;
1136 }
1137
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001138 static const unsigned Opc[3] = {
1139 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1140 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001141 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001142 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1143 return Result;
1144 }
1145 case ISD::SIGN_EXTEND: {
1146 int DestIs16 = N.getValueType() == MVT::i16;
1147 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1148
Chris Lattner590d8002005-01-09 18:52:44 +00001149 // FIXME: Legalize should promote bools to i8!
1150 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1151 "Sign extend from bool not implemented!");
1152
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001153 if (isFoldableLoad(N.getOperand(0))) {
1154 static const unsigned Opc[3] = {
1155 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1156 };
1157
1158 X86AddressMode AM;
1159 EmitFoldedLoad(N.getOperand(0), AM);
1160 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1161 return Result;
1162 }
1163
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001164 static const unsigned Opc[3] = {
1165 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1166 };
1167 Tmp1 = SelectExpr(N.getOperand(0));
1168 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1169 return Result;
1170 }
1171 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001172 // Fold TRUNCATE (LOAD P) into a smaller load from P.
1173 if (isFoldableLoad(N.getOperand(0))) {
1174 switch (N.getValueType()) {
1175 default: assert(0 && "Unknown truncate!");
1176 case MVT::i1:
1177 case MVT::i8: Opc = X86::MOV8rm; break;
1178 case MVT::i16: Opc = X86::MOV16rm; break;
1179 }
1180 X86AddressMode AM;
1181 EmitFoldedLoad(N.getOperand(0), AM);
1182 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1183 return Result;
1184 }
1185
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001186 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1187 // a move out of AX or AL.
1188 switch (N.getOperand(0).getValueType()) {
1189 default: assert(0 && "Unknown truncate!");
1190 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1191 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1192 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1193 }
1194 Tmp1 = SelectExpr(N.getOperand(0));
1195 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1196
1197 switch (N.getValueType()) {
1198 default: assert(0 && "Unknown truncate!");
1199 case MVT::i1:
1200 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1201 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1202 }
1203 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1204 return Result;
1205
1206 case ISD::FP_ROUND:
1207 // Truncate from double to float by storing to memory as float,
1208 // then reading it back into a register.
1209
1210 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001211 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001212 Tmp1 = TLI.getTargetData().getFloatAlignment();
1213 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1214
1215 // Codegen the input.
1216 Tmp1 = SelectExpr(N.getOperand(0));
1217
1218 // Emit the store, then the reload.
1219 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1220 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001221 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001222
1223 case ISD::SINT_TO_FP:
1224 case ISD::UINT_TO_FP: {
1225 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001226 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001227
1228 // Promote the integer to a type supported by FLD. We do this because there
1229 // are no unsigned FLD instructions, so we must promote an unsigned value to
1230 // a larger signed value, then use FLD on the larger value.
1231 //
1232 MVT::ValueType PromoteType = MVT::Other;
1233 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1234 unsigned PromoteOpcode = 0;
1235 unsigned RealDestReg = Result;
1236 switch (SrcTy) {
1237 case MVT::i1:
1238 case MVT::i8:
1239 // We don't have the facilities for directly loading byte sized data from
1240 // memory (even signed). Promote it to 16 bits.
1241 PromoteType = MVT::i16;
1242 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1243 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1244 break;
1245 case MVT::i16:
1246 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1247 PromoteType = MVT::i32;
1248 PromoteOpcode = X86::MOVZX32rr16;
1249 }
1250 break;
1251 default:
1252 // Don't fild into the real destination.
1253 if (Node->getOpcode() == ISD::UINT_TO_FP)
1254 Result = MakeReg(Node->getValueType(0));
1255 break;
1256 }
1257
1258 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1259
1260 if (PromoteType != MVT::Other) {
1261 Tmp2 = MakeReg(PromoteType);
1262 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1263 SrcTy = PromoteType;
1264 Tmp1 = Tmp2;
1265 }
1266
1267 // Spill the integer to memory and reload it from there.
1268 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1269 MachineFunction *F = BB->getParent();
1270 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1271
1272 switch (SrcTy) {
1273 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001274 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001275 // FIXME: this won't work for cast [u]long to FP
1276 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1277 FrameIdx).addReg(Tmp1);
1278 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1279 FrameIdx, 4).addReg(Tmp1+1);
1280 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1281 break;
1282 case MVT::i32:
1283 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1284 FrameIdx).addReg(Tmp1);
1285 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1286 break;
1287 case MVT::i16:
1288 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1289 FrameIdx).addReg(Tmp1);
1290 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1291 break;
1292 default: break; // No promotion required.
1293 }
1294
Chris Lattner085c9952005-01-12 04:00:00 +00001295 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001296 // If this is a cast from uint -> double, we need to be careful when if
1297 // the "sign" bit is set. If so, we don't want to make a negative number,
1298 // we want to make a positive number. Emit code to add an offset if the
1299 // sign bit is set.
1300
1301 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1302 unsigned IsNeg = MakeReg(MVT::i32);
1303 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1304
1305 // Create a CP value that has the offset in one word and 0 in the other.
1306 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1307 0x4f80000000000000ULL);
1308 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1309 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1310 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1311
1312 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1313 // We need special handling for unsigned 64-bit integer sources. If the
1314 // input number has the "sign bit" set, then we loaded it incorrectly as a
1315 // negative 64-bit number. In this case, add an offset value.
1316
1317 // Emit a test instruction to see if the dynamic input value was signed.
1318 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1319
1320 // If the sign bit is set, get a pointer to an offset, otherwise get a
1321 // pointer to a zero.
1322 MachineConstantPool *CP = F->getConstantPool();
1323 unsigned Zero = MakeReg(MVT::i32);
1324 Constant *Null = Constant::getNullValue(Type::UIntTy);
1325 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1326 CP->getConstantPoolIndex(Null));
1327 unsigned Offset = MakeReg(MVT::i32);
1328 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1329
1330 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1331 CP->getConstantPoolIndex(OffsetCst));
1332 unsigned Addr = MakeReg(MVT::i32);
1333 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1334
1335 // Load the constant for an add. FIXME: this could make an 'fadd' that
1336 // reads directly from memory, but we don't support these yet.
1337 unsigned ConstReg = MakeReg(MVT::f64);
1338 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1339
1340 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1341 }
1342 return RealDestReg;
1343 }
1344 case ISD::FP_TO_SINT:
1345 case ISD::FP_TO_UINT: {
1346 // FIXME: Most of this grunt work should be done by legalize!
1347 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1348
1349 // Change the floating point control register to use "round towards zero"
1350 // mode when truncating to an integer value.
1351 //
1352 MachineFunction *F = BB->getParent();
1353 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1354 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1355
1356 // Load the old value of the high byte of the control word...
1357 unsigned HighPartOfCW = MakeReg(MVT::i8);
1358 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1359 CWFrameIdx, 1);
1360
1361 // Set the high part to be round to zero...
1362 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1363 CWFrameIdx, 1).addImm(12);
1364
1365 // Reload the modified control word now...
1366 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1367
1368 // Restore the memory image of control word to original value
1369 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1370 CWFrameIdx, 1).addReg(HighPartOfCW);
1371
1372 // We don't have the facilities for directly storing byte sized data to
1373 // memory. Promote it to 16 bits. We also must promote unsigned values to
1374 // larger classes because we only have signed FP stores.
1375 MVT::ValueType StoreClass = Node->getValueType(0);
1376 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1377 switch (StoreClass) {
1378 case MVT::i8: StoreClass = MVT::i16; break;
1379 case MVT::i16: StoreClass = MVT::i32; break;
1380 case MVT::i32: StoreClass = MVT::i64; break;
1381 // The following treatment of cLong may not be perfectly right,
1382 // but it survives chains of casts of the form
1383 // double->ulong->double.
1384 case MVT::i64: StoreClass = MVT::i64; break;
1385 default: assert(0 && "Unknown store class!");
1386 }
1387
1388 // Spill the integer to memory and reload it from there.
1389 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1390 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1391
1392 switch (StoreClass) {
1393 default: assert(0 && "Unknown store class!");
1394 case MVT::i16:
1395 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1396 break;
1397 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001398 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001399 break;
1400 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001401 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001402 break;
1403 }
1404
1405 switch (Node->getValueType(0)) {
1406 default:
1407 assert(0 && "Unknown integer type!");
1408 case MVT::i64:
1409 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001410 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001411 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1412 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1413 case MVT::i32:
1414 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1415 break;
1416 case MVT::i16:
1417 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1418 break;
1419 case MVT::i8:
1420 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1421 break;
1422 }
1423
1424 // Reload the original control word now.
1425 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1426 return Result;
1427 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001428 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001429 Op0 = N.getOperand(0);
1430 Op1 = N.getOperand(1);
1431
1432 if (isFoldableLoad(Op0))
1433 std::swap(Op0, Op1);
1434
1435 if (isFoldableLoad(Op1)) {
1436 switch (N.getValueType()) {
1437 default: assert(0 && "Cannot add this type!");
1438 case MVT::i1:
1439 case MVT::i8: Opc = X86::ADD8rm; break;
1440 case MVT::i16: Opc = X86::ADD16rm; break;
1441 case MVT::i32: Opc = X86::ADD32rm; break;
1442 case MVT::f32: Opc = X86::FADD32m; break;
1443 case MVT::f64: Opc = X86::FADD64m; break;
1444 }
1445 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001446 EmitFoldedLoad(Op1, AM);
1447 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001448 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1449 return Result;
1450 }
1451
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001452 // See if we can codegen this as an LEA to fold operations together.
1453 if (N.getValueType() == MVT::i32) {
1454 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001455 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001456 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001457 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001458 // leave this as LEA, then peephole it to 'ADD' after two address elim
1459 // happens.
1460 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001461 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001462 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1463 return Result;
1464 }
1465 }
1466 }
Chris Lattner11333092005-01-11 03:11:44 +00001467
Chris Lattnera5ade062005-01-11 21:19:59 +00001468 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001469 Opc = 0;
1470 if (CN->getValue() == 1) { // add X, 1 -> inc X
1471 switch (N.getValueType()) {
1472 default: assert(0 && "Cannot integer add this type!");
1473 case MVT::i8: Opc = X86::INC8r; break;
1474 case MVT::i16: Opc = X86::INC16r; break;
1475 case MVT::i32: Opc = X86::INC32r; break;
1476 }
1477 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1478 switch (N.getValueType()) {
1479 default: assert(0 && "Cannot integer add this type!");
1480 case MVT::i8: Opc = X86::DEC8r; break;
1481 case MVT::i16: Opc = X86::DEC16r; break;
1482 case MVT::i32: Opc = X86::DEC32r; break;
1483 }
1484 }
1485
1486 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001487 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001488 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1489 return Result;
1490 }
1491
1492 switch (N.getValueType()) {
1493 default: assert(0 && "Cannot add this type!");
1494 case MVT::i8: Opc = X86::ADD8ri; break;
1495 case MVT::i16: Opc = X86::ADD16ri; break;
1496 case MVT::i32: Opc = X86::ADD32ri; break;
1497 }
1498 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001499 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001500 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1501 return Result;
1502 }
1503 }
1504
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001505 switch (N.getValueType()) {
1506 default: assert(0 && "Cannot add this type!");
1507 case MVT::i8: Opc = X86::ADD8rr; break;
1508 case MVT::i16: Opc = X86::ADD16rr; break;
1509 case MVT::i32: Opc = X86::ADD32rr; break;
1510 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001511 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001512 }
Chris Lattner11333092005-01-11 03:11:44 +00001513
Chris Lattnera5ade062005-01-11 21:19:59 +00001514 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1515 Tmp1 = SelectExpr(Op0);
1516 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001517 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001518 Tmp2 = SelectExpr(Op1);
1519 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001520 }
1521
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001522 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1523 return Result;
1524 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001525 case ISD::MUL:
1526 case ISD::AND:
1527 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001528 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001529 static const unsigned SUBTab[] = {
1530 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1531 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1532 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1533 };
1534 static const unsigned MULTab[] = {
1535 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1536 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1537 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1538 };
1539 static const unsigned ANDTab[] = {
1540 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1541 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1542 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1543 };
1544 static const unsigned ORTab[] = {
1545 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1546 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1547 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1548 };
1549 static const unsigned XORTab[] = {
1550 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1551 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1552 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1553 };
1554
1555 Op0 = Node->getOperand(0);
1556 Op1 = Node->getOperand(1);
1557
1558 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001559 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1560 if (CN->isNullValue()) { // 0 - N -> neg N
1561 switch (N.getValueType()) {
1562 default: assert(0 && "Cannot sub this type!");
1563 case MVT::i1:
1564 case MVT::i8: Opc = X86::NEG8r; break;
1565 case MVT::i16: Opc = X86::NEG16r; break;
1566 case MVT::i32: Opc = X86::NEG32r; break;
1567 }
1568 Tmp1 = SelectExpr(N.getOperand(1));
1569 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1570 return Result;
1571 }
1572
Chris Lattnera5ade062005-01-11 21:19:59 +00001573 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1574 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001575 switch (N.getValueType()) {
1576 default: assert(0 && "Cannot add this type!");
1577 case MVT::i1:
1578 case MVT::i8: Opc = X86::NOT8r; break;
1579 case MVT::i16: Opc = X86::NOT16r; break;
1580 case MVT::i32: Opc = X86::NOT32r; break;
1581 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001582 Tmp1 = SelectExpr(Op0);
Chris Lattnerd4dab922005-01-11 04:31:30 +00001583 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1584 return Result;
1585 }
1586
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001587 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001588 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001589 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001590 case MVT::i8: Opc = 0; break;
1591 case MVT::i16: Opc = 1; break;
1592 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001593 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001594 switch (Node->getOpcode()) {
1595 default: assert(0 && "Unreachable!");
1596 case ISD::SUB: Opc = SUBTab[Opc]; break;
1597 case ISD::MUL: Opc = MULTab[Opc]; break;
1598 case ISD::AND: Opc = ANDTab[Opc]; break;
1599 case ISD::OR: Opc = ORTab[Opc]; break;
1600 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001601 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001602 if (Opc) { // Can't fold MUL:i8 R, imm
1603 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001604 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1605 return Result;
1606 }
1607 }
Chris Lattner11333092005-01-11 03:11:44 +00001608
Chris Lattnera5ade062005-01-11 21:19:59 +00001609 if (isFoldableLoad(Op0))
1610 if (Node->getOpcode() != ISD::SUB) {
1611 std::swap(Op0, Op1);
1612 } else {
1613 // Emit 'reverse' subract, with a memory operand.
1614 switch (N.getValueType()) {
1615 default: Opc = 0; break;
1616 case MVT::f32: Opc = X86::FSUBR32m; break;
1617 case MVT::f64: Opc = X86::FSUBR64m; break;
1618 }
1619 if (Opc) {
1620 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001621 EmitFoldedLoad(Op0, AM);
1622 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001623 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1624 return Result;
1625 }
1626 }
1627
1628 if (isFoldableLoad(Op1)) {
1629 switch (N.getValueType()) {
1630 default: assert(0 && "Cannot operate on this type!");
1631 case MVT::i1:
1632 case MVT::i8: Opc = 5; break;
1633 case MVT::i16: Opc = 6; break;
1634 case MVT::i32: Opc = 7; break;
1635 case MVT::f32: Opc = 8; break;
1636 case MVT::f64: Opc = 9; break;
1637 }
1638 switch (Node->getOpcode()) {
1639 default: assert(0 && "Unreachable!");
1640 case ISD::SUB: Opc = SUBTab[Opc]; break;
1641 case ISD::MUL: Opc = MULTab[Opc]; break;
1642 case ISD::AND: Opc = ANDTab[Opc]; break;
1643 case ISD::OR: Opc = ORTab[Opc]; break;
1644 case ISD::XOR: Opc = XORTab[Opc]; break;
1645 }
1646
1647 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001648 EmitFoldedLoad(Op1, AM);
1649 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001650 if (Opc) {
1651 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1652 } else {
1653 assert(Node->getOpcode() == ISD::MUL &&
1654 N.getValueType() == MVT::i8 && "Unexpected situation!");
1655 // Must use the MUL instruction, which forces use of AL.
1656 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1657 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1658 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1659 }
1660 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001661 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001662
1663 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1664 Tmp1 = SelectExpr(Op0);
1665 Tmp2 = SelectExpr(Op1);
1666 } else {
1667 Tmp2 = SelectExpr(Op1);
1668 Tmp1 = SelectExpr(Op0);
1669 }
1670
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001671 switch (N.getValueType()) {
1672 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001673 case MVT::i1:
1674 case MVT::i8: Opc = 10; break;
1675 case MVT::i16: Opc = 11; break;
1676 case MVT::i32: Opc = 12; break;
1677 case MVT::f32: Opc = 13; break;
1678 case MVT::f64: Opc = 14; break;
1679 }
1680 switch (Node->getOpcode()) {
1681 default: assert(0 && "Unreachable!");
1682 case ISD::SUB: Opc = SUBTab[Opc]; break;
1683 case ISD::MUL: Opc = MULTab[Opc]; break;
1684 case ISD::AND: Opc = ANDTab[Opc]; break;
1685 case ISD::OR: Opc = ORTab[Opc]; break;
1686 case ISD::XOR: Opc = XORTab[Opc]; break;
1687 }
1688 if (Opc) {
1689 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1690 } else {
1691 assert(Node->getOpcode() == ISD::MUL &&
1692 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001693 // Must use the MUL instruction, which forces use of AL.
1694 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1695 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1696 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001697 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001698 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001699 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001700 case ISD::SELECT:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001701 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001702 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1703 Tmp2 = SelectExpr(N.getOperand(1));
1704 Tmp3 = SelectExpr(N.getOperand(2));
1705 } else {
1706 Tmp3 = SelectExpr(N.getOperand(2));
1707 Tmp2 = SelectExpr(N.getOperand(1));
1708 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001709 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001710 return Result;
1711 } else {
1712 // FIXME: This should not be implemented here, it should be in the generic
1713 // code!
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001714 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1715 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1716 N.getOperand(1)));
1717 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1718 N.getOperand(2)));
1719 } else {
1720 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1721 N.getOperand(2)));
1722 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1723 N.getOperand(1)));
1724 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001725 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001726 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1727 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001728 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1729 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1730 return Result;
1731 }
1732
1733 case ISD::SDIV:
1734 case ISD::UDIV:
1735 case ISD::SREM:
1736 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001737 if (N.getOpcode() == ISD::SDIV)
1738 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1739 // FIXME: These special cases should be handled by the lowering impl!
1740 unsigned RHS = CN->getValue();
1741 bool isNeg = false;
1742 if ((int)RHS < 0) {
1743 isNeg = true;
1744 RHS = -RHS;
1745 }
1746 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1747 unsigned Log = log2(RHS);
1748 unsigned TmpReg = MakeReg(N.getValueType());
1749 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1750 switch (N.getValueType()) {
1751 default: assert("Unknown type to signed divide!");
1752 case MVT::i8:
1753 SAROpc = X86::SAR8ri;
1754 SHROpc = X86::SHR8ri;
1755 ADDOpc = X86::ADD8rr;
1756 NEGOpc = X86::NEG8r;
1757 break;
1758 case MVT::i16:
1759 SAROpc = X86::SAR16ri;
1760 SHROpc = X86::SHR16ri;
1761 ADDOpc = X86::ADD16rr;
1762 NEGOpc = X86::NEG16r;
1763 break;
1764 case MVT::i32:
1765 SAROpc = X86::SAR32ri;
1766 SHROpc = X86::SHR32ri;
1767 ADDOpc = X86::ADD32rr;
1768 NEGOpc = X86::NEG32r;
1769 break;
1770 }
Chris Lattner11333092005-01-11 03:11:44 +00001771 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001772 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1773 unsigned TmpReg2 = MakeReg(N.getValueType());
1774 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1775 unsigned TmpReg3 = MakeReg(N.getValueType());
1776 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1777
1778 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1779 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1780 if (isNeg)
1781 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1782 return Result;
1783 }
1784 }
1785
Chris Lattner11333092005-01-11 03:11:44 +00001786 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1787 Tmp1 = SelectExpr(N.getOperand(0));
1788 Tmp2 = SelectExpr(N.getOperand(1));
1789 } else {
1790 Tmp2 = SelectExpr(N.getOperand(1));
1791 Tmp1 = SelectExpr(N.getOperand(0));
1792 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001793
1794 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1795 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1796 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1797 switch (N.getValueType()) {
1798 default: assert(0 && "Cannot sdiv this type!");
1799 case MVT::i8:
1800 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1801 LoReg = X86::AL;
1802 HiReg = X86::AH;
1803 MovOpcode = X86::MOV8rr;
1804 ClrOpcode = X86::MOV8ri;
1805 SExtOpcode = X86::CBW;
1806 break;
1807 case MVT::i16:
1808 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1809 LoReg = X86::AX;
1810 HiReg = X86::DX;
1811 MovOpcode = X86::MOV16rr;
1812 ClrOpcode = X86::MOV16ri;
1813 SExtOpcode = X86::CWD;
1814 break;
1815 case MVT::i32:
1816 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001817 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001818 HiReg = X86::EDX;
1819 MovOpcode = X86::MOV32rr;
1820 ClrOpcode = X86::MOV32ri;
1821 SExtOpcode = X86::CDQ;
1822 break;
1823 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1824 case MVT::f32:
1825 case MVT::f64:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001826 if (N.getOpcode() == ISD::SDIV)
1827 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1828 else
1829 assert(0 && "FIXME: Emit frem libcall to fmod!");
1830 return Result;
1831 }
1832
1833 // Set up the low part.
1834 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1835
1836 if (isSigned) {
1837 // Sign extend the low part into the high part.
1838 BuildMI(BB, SExtOpcode, 0);
1839 } else {
1840 // Zero out the high part, effectively zero extending the input.
1841 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1842 }
1843
1844 // Emit the DIV/IDIV instruction.
1845 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1846
1847 // Get the result of the divide or rem.
1848 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1849 return Result;
1850 }
1851
1852 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001853 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001854 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1855 switch (N.getValueType()) {
1856 default: assert(0 && "Cannot shift this type!");
1857 case MVT::i8: Opc = X86::ADD8rr; break;
1858 case MVT::i16: Opc = X86::ADD16rr; break;
1859 case MVT::i32: Opc = X86::ADD32rr; break;
1860 }
1861 Tmp1 = SelectExpr(N.getOperand(0));
1862 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1863 return Result;
1864 }
1865
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001866 switch (N.getValueType()) {
1867 default: assert(0 && "Cannot shift this type!");
1868 case MVT::i8: Opc = X86::SHL8ri; break;
1869 case MVT::i16: Opc = X86::SHL16ri; break;
1870 case MVT::i32: Opc = X86::SHL32ri; break;
1871 }
Chris Lattner11333092005-01-11 03:11:44 +00001872 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001873 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1874 return Result;
1875 }
Chris Lattner11333092005-01-11 03:11:44 +00001876
1877 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1878 Tmp1 = SelectExpr(N.getOperand(0));
1879 Tmp2 = SelectExpr(N.getOperand(1));
1880 } else {
1881 Tmp2 = SelectExpr(N.getOperand(1));
1882 Tmp1 = SelectExpr(N.getOperand(0));
1883 }
1884
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001885 switch (N.getValueType()) {
1886 default: assert(0 && "Cannot shift this type!");
1887 case MVT::i8 : Opc = X86::SHL8rCL; break;
1888 case MVT::i16: Opc = X86::SHL16rCL; break;
1889 case MVT::i32: Opc = X86::SHL32rCL; break;
1890 }
1891 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1892 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1893 return Result;
1894 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001895 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1896 switch (N.getValueType()) {
1897 default: assert(0 && "Cannot shift this type!");
1898 case MVT::i8: Opc = X86::SHR8ri; break;
1899 case MVT::i16: Opc = X86::SHR16ri; break;
1900 case MVT::i32: Opc = X86::SHR32ri; break;
1901 }
Chris Lattner11333092005-01-11 03:11:44 +00001902 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001903 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1904 return Result;
1905 }
Chris Lattner11333092005-01-11 03:11:44 +00001906
1907 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1908 Tmp1 = SelectExpr(N.getOperand(0));
1909 Tmp2 = SelectExpr(N.getOperand(1));
1910 } else {
1911 Tmp2 = SelectExpr(N.getOperand(1));
1912 Tmp1 = SelectExpr(N.getOperand(0));
1913 }
1914
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001915 switch (N.getValueType()) {
1916 default: assert(0 && "Cannot shift this type!");
1917 case MVT::i8 : Opc = X86::SHR8rCL; break;
1918 case MVT::i16: Opc = X86::SHR16rCL; break;
1919 case MVT::i32: Opc = X86::SHR32rCL; break;
1920 }
1921 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1922 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1923 return Result;
1924 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001925 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1926 switch (N.getValueType()) {
1927 default: assert(0 && "Cannot shift this type!");
1928 case MVT::i8: Opc = X86::SAR8ri; break;
1929 case MVT::i16: Opc = X86::SAR16ri; break;
1930 case MVT::i32: Opc = X86::SAR32ri; break;
1931 }
Chris Lattner11333092005-01-11 03:11:44 +00001932 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001933 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1934 return Result;
1935 }
Chris Lattner11333092005-01-11 03:11:44 +00001936
1937 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1938 Tmp1 = SelectExpr(N.getOperand(0));
1939 Tmp2 = SelectExpr(N.getOperand(1));
1940 } else {
1941 Tmp2 = SelectExpr(N.getOperand(1));
1942 Tmp1 = SelectExpr(N.getOperand(0));
1943 }
1944
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001945 switch (N.getValueType()) {
1946 default: assert(0 && "Cannot shift this type!");
1947 case MVT::i8 : Opc = X86::SAR8rCL; break;
1948 case MVT::i16: Opc = X86::SAR16rCL; break;
1949 case MVT::i32: Opc = X86::SAR32rCL; break;
1950 }
1951 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1952 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1953 return Result;
1954
1955 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001956 EmitCMP(N.getOperand(0), N.getOperand(1));
1957 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1958 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1959 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00001960 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001961 // Make sure we generate both values.
1962 if (Result != 1)
1963 ExprMap[N.getValue(1)] = 1; // Generate the token
1964 else
1965 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1966
Chris Lattner5188ad72005-01-08 19:28:19 +00001967 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001968 default: assert(0 && "Cannot load this type!");
1969 case MVT::i1:
1970 case MVT::i8: Opc = X86::MOV8rm; break;
1971 case MVT::i16: Opc = X86::MOV16rm; break;
1972 case MVT::i32: Opc = X86::MOV32rm; break;
1973 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1974 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1975 }
Chris Lattner11333092005-01-11 03:11:44 +00001976
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001977 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001978 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001979 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1980 } else {
1981 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001982
1983 SDOperand Chain = N.getOperand(0);
1984 SDOperand Address = N.getOperand(1);
1985 if (getRegPressure(Chain) > getRegPressure(Address)) {
1986 Select(Chain);
1987 SelectAddress(Address, AM);
1988 } else {
1989 SelectAddress(Address, AM);
1990 Select(Chain);
1991 }
1992
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001993 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1994 }
1995 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00001996
1997 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
1998 case ISD::ZEXTLOAD: {
1999 // Make sure we generate both values.
2000 if (Result != 1)
2001 ExprMap[N.getValue(1)] = 1; // Generate the token
2002 else
2003 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2004
2005 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
2312 case ISD::EXTLOAD:
2313 case ISD::SEXTLOAD:
2314 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002315 case ISD::LOAD:
2316 case ISD::CALL:
2317 case ISD::DYNAMIC_STACKALLOC:
2318 SelectExpr(N);
2319 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002320
2321 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2322 // On X86, we can represent all types except for Bool and Float natively.
2323 X86AddressMode AM;
2324 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
2325 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32) &&
2326 "Unsupported TRUNCSTORE for this target!");
2327
2328 // Store of constant bool?
2329 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2330 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2331 Select(N.getOperand(0));
2332 SelectAddress(N.getOperand(2), AM);
2333 } else {
2334 SelectAddress(N.getOperand(2), AM);
2335 Select(N.getOperand(0));
2336 }
2337 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2338 return;
2339 }
2340
2341 switch (StoredTy) {
2342 default: assert(0 && "Cannot truncstore this type!");
2343 case MVT::i1: Opc = X86::MOV8mr; break;
2344 case MVT::f32: Opc = X86::FST32m; break;
2345 }
2346
2347 std::vector<std::pair<unsigned, unsigned> > RP;
2348 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2349 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2350 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2351 std::sort(RP.begin(), RP.end());
2352
2353 for (unsigned i = 0; i != 3; ++i)
2354 switch (RP[2-i].second) {
2355 default: assert(0 && "Unknown operand number!");
2356 case 0: Select(N.getOperand(0)); break;
2357 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2358 case 2: SelectAddress(N.getOperand(2), AM); break;
2359 }
2360
2361 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2362 return;
2363 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002364 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002365 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002366
2367 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2368 Opc = 0;
2369 switch (CN->getValueType(0)) {
2370 default: assert(0 && "Invalid type for operation!");
2371 case MVT::i1:
2372 case MVT::i8: Opc = X86::MOV8mi; break;
2373 case MVT::i16: Opc = X86::MOV16mi; break;
2374 case MVT::i32: Opc = X86::MOV32mi; break;
2375 case MVT::f32:
2376 case MVT::f64: break;
2377 }
2378 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002379 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2380 Select(N.getOperand(0));
2381 SelectAddress(N.getOperand(2), AM);
2382 } else {
2383 SelectAddress(N.getOperand(2), AM);
2384 Select(N.getOperand(0));
2385 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002386 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2387 return;
2388 }
2389 }
Chris Lattner837caa72005-01-11 23:21:30 +00002390
2391 // Check to see if this is a load/op/store combination.
2392 if (N.getOperand(1).Val->hasOneUse() &&
Chris Lattner42928302005-01-12 03:16:09 +00002393 isFoldableLoad(N.getOperand(0).getValue(0)) &&
2394 !MVT::isFloatingPoint(N.getOperand(0).getValue(0).getValueType())) {
Chris Lattner837caa72005-01-11 23:21:30 +00002395 SDOperand TheLoad = N.getOperand(0).getValue(0);
2396 // Check to see if we are loading the same pointer that we're storing to.
2397 if (TheLoad.getOperand(1) == N.getOperand(2)) {
2398 // See if the stored value is a simple binary operator that uses the
2399 // load as one of its operands.
2400 SDOperand Op = N.getOperand(1);
2401 if (Op.Val->getNumOperands() == 2 &&
2402 (Op.getOperand(0) == TheLoad || Op.getOperand(1) == TheLoad)) {
2403 // Finally, check to see if this is one of the ops we can handle!
2404 static const unsigned ADDTAB[] = {
2405 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002406 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
Chris Lattner837caa72005-01-11 23:21:30 +00002407 };
Chris Lattner7ea64f52005-01-12 01:28:00 +00002408 static const unsigned SUBTAB[] = {
2409 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002410 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002411 };
2412 static const unsigned ANDTAB[] = {
2413 X86::AND8mi, X86::AND16mi, X86::AND32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002414 X86::AND8mr, X86::AND16mr, X86::AND32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002415 };
2416 static const unsigned ORTAB[] = {
2417 X86::OR8mi, X86::OR16mi, X86::OR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002418 X86::OR8mr, X86::OR16mr, X86::OR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002419 };
2420 static const unsigned XORTAB[] = {
2421 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002422 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002423 };
2424 static const unsigned SHLTAB[] = {
2425 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002426 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002427 };
2428 static const unsigned SARTAB[] = {
2429 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002430 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002431 };
2432 static const unsigned SHRTAB[] = {
2433 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002434 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002435 };
Chris Lattner837caa72005-01-11 23:21:30 +00002436
2437 const unsigned *TabPtr = 0;
2438 switch (Op.getOpcode()) {
Chris Lattner7ea64f52005-01-12 01:28:00 +00002439 default: std::cerr << "CANNOT [mem] op= val: "; Op.Val->dump(); std::cerr << "\n"; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002440 case ISD::ADD: TabPtr = ADDTAB; break;
Chris Lattner7ea64f52005-01-12 01:28:00 +00002441 case ISD::SUB: TabPtr = SUBTAB; break;
2442 case ISD::AND: TabPtr = ANDTAB; break;
2443 case ISD:: OR: TabPtr = ORTAB; break;
2444 case ISD::XOR: TabPtr = XORTAB; break;
2445 case ISD::SHL: TabPtr = SHLTAB; break;
2446 case ISD::SRA: TabPtr = SARTAB; break;
2447 case ISD::SRL: TabPtr = SHRTAB; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002448 }
2449
2450 if (TabPtr) {
2451 // Handle: [mem] op= CST
2452 SDOperand Op0 = Op.getOperand(0);
2453 SDOperand Op1 = Op.getOperand(1);
2454 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner48034fd2005-01-12 05:22:07 +00002455 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
Chris Lattner837caa72005-01-11 23:21:30 +00002456 default: break;
2457 case MVT::i1:
2458 case MVT::i8: Opc = TabPtr[0]; break;
2459 case MVT::i16: Opc = TabPtr[1]; break;
2460 case MVT::i32: Opc = TabPtr[2]; break;
2461 }
2462
2463 if (Opc) {
2464 if (getRegPressure(TheLoad.getOperand(0)) >
2465 getRegPressure(TheLoad.getOperand(1))) {
2466 Select(TheLoad.getOperand(0));
2467 SelectAddress(TheLoad.getOperand(1), AM);
2468 } else {
2469 SelectAddress(TheLoad.getOperand(1), AM);
2470 Select(TheLoad.getOperand(0));
2471 }
2472
2473 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2474 return;
2475 }
2476 }
2477
2478 // If we have [mem] = V op [mem], try to turn it into:
2479 // [mem] = [mem] op V.
Chris Lattner7ea64f52005-01-12 01:28:00 +00002480 if (Op1 == TheLoad && Op.getOpcode() != ISD::SUB &&
2481 Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRA &&
2482 Op.getOpcode() != ISD::SRL)
Chris Lattner837caa72005-01-11 23:21:30 +00002483 std::swap(Op0, Op1);
2484
2485 if (Op0 == TheLoad) {
2486 switch (Op0.getValueType()) {
2487 default: break;
2488 case MVT::i1:
2489 case MVT::i8: Opc = TabPtr[3]; break;
2490 case MVT::i16: Opc = TabPtr[4]; break;
2491 case MVT::i32: Opc = TabPtr[5]; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002492 }
2493
2494 if (Opc) {
2495 Select(TheLoad.getOperand(0));
2496 SelectAddress(TheLoad.getOperand(1), AM);
2497 unsigned Reg = SelectExpr(Op1);
2498 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addReg(Reg);
2499 return;
2500 }
2501 }
Chris Lattner837caa72005-01-11 23:21:30 +00002502 }
2503 }
2504 }
2505 }
2506
2507
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002508 switch (N.getOperand(1).getValueType()) {
2509 default: assert(0 && "Cannot store this type!");
2510 case MVT::i1:
2511 case MVT::i8: Opc = X86::MOV8mr; break;
2512 case MVT::i16: Opc = X86::MOV16mr; break;
2513 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002514 case MVT::f32: Opc = X86::FST32m; break;
2515 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002516 }
Chris Lattner11333092005-01-11 03:11:44 +00002517
2518 std::vector<std::pair<unsigned, unsigned> > RP;
2519 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2520 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2521 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2522 std::sort(RP.begin(), RP.end());
2523
2524 for (unsigned i = 0; i != 3; ++i)
2525 switch (RP[2-i].second) {
2526 default: assert(0 && "Unknown operand number!");
2527 case 0: Select(N.getOperand(0)); break;
2528 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002529 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002530 }
2531
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002532 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2533 return;
2534 }
2535 case ISD::ADJCALLSTACKDOWN:
2536 case ISD::ADJCALLSTACKUP:
2537 Select(N.getOperand(0));
2538 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2539
2540 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2541 X86::ADJCALLSTACKUP;
2542 BuildMI(BB, Opc, 1).addImm(Tmp1);
2543 return;
Chris Lattner989de032005-01-11 06:14:36 +00002544 case ISD::MEMSET: {
2545 Select(N.getOperand(0)); // Select the chain.
2546 unsigned Align =
2547 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2548 if (Align == 0) Align = 1;
2549
2550 // Turn the byte code into # iterations
2551 unsigned CountReg;
2552 unsigned Opcode;
2553 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2554 unsigned Val = ValC->getValue() & 255;
2555
2556 // If the value is a constant, then we can potentially use larger sets.
2557 switch (Align & 3) {
2558 case 2: // WORD aligned
2559 CountReg = MakeReg(MVT::i32);
2560 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2561 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2562 } else {
2563 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2564 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2565 }
2566 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2567 Opcode = X86::REP_STOSW;
2568 break;
2569 case 0: // DWORD aligned
2570 CountReg = MakeReg(MVT::i32);
2571 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2572 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2573 } else {
2574 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2575 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2576 }
2577 Val = (Val << 8) | Val;
2578 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2579 Opcode = X86::REP_STOSD;
2580 break;
2581 default: // BYTE aligned
2582 CountReg = SelectExpr(Node->getOperand(3));
2583 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2584 Opcode = X86::REP_STOSB;
2585 break;
2586 }
2587 } else {
2588 // If it's not a constant value we are storing, just fall back. We could
2589 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2590 unsigned ValReg = SelectExpr(Node->getOperand(2));
2591 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2592 CountReg = SelectExpr(Node->getOperand(3));
2593 Opcode = X86::REP_STOSB;
2594 }
2595
2596 // No matter what the alignment is, we put the source in ESI, the
2597 // destination in EDI, and the count in ECX.
2598 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2599 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2600 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2601 BuildMI(BB, Opcode, 0);
2602 return;
2603 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002604 case ISD::MEMCPY:
2605 Select(N.getOperand(0)); // Select the chain.
2606 unsigned Align =
2607 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2608 if (Align == 0) Align = 1;
2609
2610 // Turn the byte code into # iterations
2611 unsigned CountReg;
2612 unsigned Opcode;
2613 switch (Align & 3) {
2614 case 2: // WORD aligned
2615 CountReg = MakeReg(MVT::i32);
2616 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2617 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2618 } else {
2619 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2620 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2621 }
2622 Opcode = X86::REP_MOVSW;
2623 break;
2624 case 0: // DWORD aligned
2625 CountReg = MakeReg(MVT::i32);
2626 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2627 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2628 } else {
2629 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2630 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2631 }
2632 Opcode = X86::REP_MOVSD;
2633 break;
2634 default: // BYTE aligned
2635 CountReg = SelectExpr(Node->getOperand(3));
2636 Opcode = X86::REP_MOVSB;
2637 break;
2638 }
2639
2640 // No matter what the alignment is, we put the source in ESI, the
2641 // destination in EDI, and the count in ECX.
2642 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2643 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2644 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2645 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2646 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2647 BuildMI(BB, Opcode, 0);
2648 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002649 }
2650 assert(0 && "Should not be reached!");
2651}
2652
2653
2654/// createX86PatternInstructionSelector - This pass converts an LLVM function
2655/// into a machine code representation using pattern matching and a machine
2656/// description file.
2657///
2658FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2659 return new ISel(TM);
2660}