blob: 38745076e5cb2090aef74aad1b95d35cd1b4e938 [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000030#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// X86TargetLowering - X86 Implementation of the TargetLowering interface
35namespace {
36 class X86TargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000038 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000039 public:
40 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000042
43 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
44 setShiftAmountType(MVT::i8);
45 setSetCCResultType(MVT::i8);
46
47 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000048 addRegisterClass(MVT::i8, X86::R8RegisterClass);
49 addRegisterClass(MVT::i16, X86::R16RegisterClass);
50 addRegisterClass(MVT::i32, X86::R32RegisterClass);
51 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
52
53 // FIXME: Eliminate these two classes when legalize can handle promotions
54 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000055/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
56/**/ //addRegisterClass(MVT::f32, X86::RFPRegisterClass);
57
58 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
59 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
60 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i16 , Expand);
61 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
62 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i1 , Expand);
63 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
64 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
65 setOperationAction(ISD::SREM , MVT::f64 , Expand);
66
67 // These should be promoted to a larger select which is supported.
68/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
69 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000070
71 computeRegisterProperties();
Chris Lattner8acb1ba2005-01-07 07:49:41 +000072
73 addLegalFPImmediate(+0.0); // FLD0
74 addLegalFPImmediate(+1.0); // FLD1
75 addLegalFPImmediate(-0.0); // FLD0/FCHS
76 addLegalFPImmediate(-1.0); // FLD1/FCHS
77 }
78
79 /// LowerArguments - This hook must be implemented to indicate how we should
80 /// lower the arguments for the specified function, into the specified DAG.
81 virtual std::vector<SDOperand>
82 LowerArguments(Function &F, SelectionDAG &DAG);
83
84 /// LowerCallTo - This hook lowers an abstract call to a function into an
85 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000086 virtual std::pair<SDOperand, SDOperand>
87 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
88 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000089
90 virtual std::pair<SDOperand, SDOperand>
91 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
92
93 virtual std::pair<SDOperand,SDOperand>
94 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
95 const Type *ArgTy, SelectionDAG &DAG);
96
97 virtual std::pair<SDOperand, SDOperand>
98 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
99 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000100 };
101}
102
103
104std::vector<SDOperand>
105X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
106 std::vector<SDOperand> ArgValues;
107
108 // Add DAG nodes to load the arguments... On entry to a function on the X86,
109 // the stack frame looks like this:
110 //
111 // [ESP] -- return address
112 // [ESP + 4] -- first argument (leftmost lexically)
113 // [ESP + 8] -- second argument, if first argument is four bytes in size
114 // ...
115 //
116 MachineFunction &MF = DAG.getMachineFunction();
117 MachineFrameInfo *MFI = MF.getFrameInfo();
118
119 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
120 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
121 MVT::ValueType ObjectVT = getValueType(I->getType());
122 unsigned ArgIncrement = 4;
123 unsigned ObjSize;
124 switch (ObjectVT) {
125 default: assert(0 && "Unhandled argument type!");
126 case MVT::i1:
127 case MVT::i8: ObjSize = 1; break;
128 case MVT::i16: ObjSize = 2; break;
129 case MVT::i32: ObjSize = 4; break;
130 case MVT::i64: ObjSize = ArgIncrement = 8; break;
131 case MVT::f32: ObjSize = 4; break;
132 case MVT::f64: ObjSize = ArgIncrement = 8; break;
133 }
134 // Create the frame index object for this incoming parameter...
135 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
136
137 // Create the SelectionDAG nodes corresponding to a load from this parameter
138 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
139
140 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
141 // dead loads.
142 SDOperand ArgValue;
143 if (!I->use_empty())
144 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
145 else {
146 if (MVT::isInteger(ObjectVT))
147 ArgValue = DAG.getConstant(0, ObjectVT);
148 else
149 ArgValue = DAG.getConstantFP(0, ObjectVT);
150 }
151 ArgValues.push_back(ArgValue);
152
153 ArgOffset += ArgIncrement; // Move on to the next argument...
154 }
155
156 // If the function takes variable number of arguments, make a frame index for
157 // the start of the first vararg value... for expansion of llvm.va_start.
158 if (F.isVarArg())
159 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000160 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000161 return ArgValues;
162}
163
Chris Lattner5188ad72005-01-08 19:28:19 +0000164std::pair<SDOperand, SDOperand>
165X86TargetLowering::LowerCallTo(SDOperand Chain,
166 const Type *RetTy, SDOperand Callee,
167 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000168 // Count how many bytes are to be pushed on the stack.
169 unsigned NumBytes = 0;
170
171 if (Args.empty()) {
172 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000173 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
174 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000175 } else {
176 for (unsigned i = 0, e = Args.size(); i != e; ++i)
177 switch (getValueType(Args[i].second)) {
178 default: assert(0 && "Unknown value type!");
179 case MVT::i1:
180 case MVT::i8:
181 case MVT::i16:
182 case MVT::i32:
183 case MVT::f32:
184 NumBytes += 4;
185 break;
186 case MVT::i64:
187 case MVT::f64:
188 NumBytes += 8;
189 break;
190 }
191
Chris Lattner5188ad72005-01-08 19:28:19 +0000192 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
193 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000194
195 // Arguments go on the stack in reverse order, as specified by the ABI.
196 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000197 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
198 DAG.getEntryNode());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000199 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
200 unsigned ArgReg;
201 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
202 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
203
204 switch (getValueType(Args[i].second)) {
205 default: assert(0 && "Unexpected ValueType for argument!");
206 case MVT::i1:
207 case MVT::i8:
208 case MVT::i16:
209 // Promote the integer to 32 bits. If the input type is signed use a
210 // sign extend, otherwise use a zero extend.
211 if (Args[i].second->isSigned())
212 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
213 else
214 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
215
216 // FALL THROUGH
217 case MVT::i32:
218 case MVT::f32:
219 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000220 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
221 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000222 ArgOffset += 4;
223 break;
224 case MVT::i64:
225 case MVT::f64:
226 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000227 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
228 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000229 ArgOffset += 8;
230 break;
231 }
232 }
233 }
234
235 std::vector<MVT::ValueType> RetVals;
236 MVT::ValueType RetTyVT = getValueType(RetTy);
237 if (RetTyVT != MVT::isVoid)
238 RetVals.push_back(RetTyVT);
239 RetVals.push_back(MVT::Other);
240
Chris Lattner5188ad72005-01-08 19:28:19 +0000241 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000242 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000243 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
244 DAG.getConstant(NumBytes, getPointerTy()));
245 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000246}
247
Chris Lattner14824582005-01-09 00:01:27 +0000248std::pair<SDOperand, SDOperand>
249X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
250 // vastart just returns the address of the VarArgsFrameIndex slot.
251 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
252}
253
254std::pair<SDOperand,SDOperand> X86TargetLowering::
255LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
256 const Type *ArgTy, SelectionDAG &DAG) {
257 MVT::ValueType ArgVT = getValueType(ArgTy);
258 SDOperand Result;
259 if (!isVANext) {
260 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
261 } else {
262 unsigned Amt;
263 if (ArgVT == MVT::i32)
264 Amt = 4;
265 else {
266 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
267 "Other types should have been promoted for varargs!");
268 Amt = 8;
269 }
270 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
271 DAG.getConstant(Amt, VAList.getValueType()));
272 }
273 return std::make_pair(Result, Chain);
274}
275
276
277std::pair<SDOperand, SDOperand> X86TargetLowering::
278LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
279 SelectionDAG &DAG) {
280 SDOperand Result;
281 if (Depth) // Depths > 0 not supported yet!
282 Result = DAG.getConstant(0, getPointerTy());
283 else {
284 if (ReturnAddrIndex == 0) {
285 // Set up a frame object for the return address.
286 MachineFunction &MF = DAG.getMachineFunction();
287 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
288 }
289
290 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
291
292 if (!isFrameAddress)
293 // Just load the return address
294 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
295 else
296 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
297 DAG.getConstant(4, MVT::i32));
298 }
299 return std::make_pair(Result, Chain);
300}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000301
302
303
304
305
306namespace {
307 Statistic<>
308 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
309
310 //===--------------------------------------------------------------------===//
311 /// ISel - X86 specific code to select X86 machine instructions for
312 /// SelectionDAG operations.
313 ///
314 class ISel : public SelectionDAGISel {
315 /// ContainsFPCode - Every instruction we select that uses or defines a FP
316 /// register should set this to true.
317 bool ContainsFPCode;
318
319 /// X86Lowering - This object fully describes how to lower LLVM code to an
320 /// X86-specific SelectionDAG.
321 X86TargetLowering X86Lowering;
322
Chris Lattner11333092005-01-11 03:11:44 +0000323 /// RegPressureMap - This keeps an approximate count of the number of
324 /// registers required to evaluate each node in the graph.
325 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000326
327 /// ExprMap - As shared expressions are codegen'd, we keep track of which
328 /// vreg the value is produced in, so we only emit one copy of each compiled
329 /// tree.
330 std::map<SDOperand, unsigned> ExprMap;
331 std::set<SDOperand> LoweredTokens;
332
333 public:
334 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
335 }
336
Chris Lattner11333092005-01-11 03:11:44 +0000337 unsigned getRegPressure(SDOperand O) {
338 return RegPressureMap[O.Val];
339 }
340 unsigned ComputeRegPressure(SDOperand O);
341
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000342 /// InstructionSelectBasicBlock - This callback is invoked by
343 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000344 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000345
Chris Lattnera5ade062005-01-11 21:19:59 +0000346 bool isFoldableLoad(SDOperand Op);
347 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
348
349
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000350 void EmitCMP(SDOperand LHS, SDOperand RHS);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000351 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000352 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
353 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000354 unsigned SelectExpr(SDOperand N);
355 bool SelectAddress(SDOperand N, X86AddressMode &AM);
356 void Select(SDOperand N);
357 };
358}
359
Chris Lattner7dbcb752005-01-12 04:21:28 +0000360/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
361/// when it has created a SelectionDAG for us to codegen.
362void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
363 // While we're doing this, keep track of whether we see any FP code for
364 // FP_REG_KILL insertion.
365 ContainsFPCode = false;
366
367 // Scan the PHI nodes that already are inserted into this basic block. If any
368 // of them is a PHI of a floating point value, we need to insert an
369 // FP_REG_KILL.
370 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
371 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
372 I != E; ++I) {
373 assert(I->getOpcode() == X86::PHI &&
374 "Isn't just PHI nodes?");
375 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
376 X86::RFPRegisterClass) {
377 ContainsFPCode = true;
378 break;
379 }
380 }
381
382 // Compute the RegPressureMap, which is an approximation for the number of
383 // registers required to compute each node.
384 ComputeRegPressure(DAG.getRoot());
385
386 // Codegen the basic block.
387 Select(DAG.getRoot());
388
389 // Finally, look at all of the successors of this block. If any contain a PHI
390 // node of FP type, we need to insert an FP_REG_KILL in this block.
391 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
392 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
393 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
394 I != E && I->getOpcode() == X86::PHI; ++I) {
395 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
396 X86::RFPRegisterClass) {
397 ContainsFPCode = true;
398 break;
399 }
400 }
401
402 // Insert FP_REG_KILL instructions into basic blocks that need them. This
403 // only occurs due to the floating point stackifier not being aggressive
404 // enough to handle arbitrary global stackification.
405 //
406 // Currently we insert an FP_REG_KILL instruction into each block that uses or
407 // defines a floating point virtual register.
408 //
409 // When the global register allocators (like linear scan) finally update live
410 // variable analysis, we can keep floating point values in registers across
411 // basic blocks. This will be a huge win, but we are waiting on the global
412 // allocators before we can do this.
413 //
414 if (ContainsFPCode && BB->succ_size()) {
415 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
416 ++NumFPKill;
417 }
418
419 // Clear state used for selection.
420 ExprMap.clear();
421 LoweredTokens.clear();
422 RegPressureMap.clear();
423}
424
425
Chris Lattner11333092005-01-11 03:11:44 +0000426// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
427// for the number of registers required to compute each node. This is basically
428// computing a generalized form of the Sethi-Ullman number for each node.
429unsigned ISel::ComputeRegPressure(SDOperand O) {
430 SDNode *N = O.Val;
431 unsigned &Result = RegPressureMap[N];
432 if (Result) return Result;
433
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000434 // FIXME: Should operations like CALL (which clobber lots o regs) have a
435 // higher fixed cost??
436
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000437 if (N->getNumOperands() == 0) {
438 Result = 1;
439 } else {
440 unsigned MaxRegUse = 0;
441 unsigned NumExtraMaxRegUsers = 0;
442 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
443 unsigned Regs;
444 if (N->getOperand(i).getOpcode() == ISD::Constant)
445 Regs = 0;
446 else
447 Regs = ComputeRegPressure(N->getOperand(i));
448 if (Regs > MaxRegUse) {
449 MaxRegUse = Regs;
450 NumExtraMaxRegUsers = 0;
451 } else if (Regs == MaxRegUse &&
452 N->getOperand(i).getValueType() != MVT::Other) {
453 ++NumExtraMaxRegUsers;
454 }
Chris Lattner11333092005-01-11 03:11:44 +0000455 }
Chris Lattner11333092005-01-11 03:11:44 +0000456
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000457 Result = MaxRegUse+NumExtraMaxRegUsers;
458 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000459
Chris Lattner837caa72005-01-11 23:21:30 +0000460 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000461 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000462}
463
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000464/// SelectAddress - Add the specified node to the specified addressing mode,
465/// returning true if it cannot be done.
466bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
467 switch (N.getOpcode()) {
468 default: break;
469 case ISD::FrameIndex:
470 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
471 AM.BaseType = X86AddressMode::FrameIndexBase;
472 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
473 return false;
474 }
475 break;
476 case ISD::GlobalAddress:
477 if (AM.GV == 0) {
478 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
479 return false;
480 }
481 break;
482 case ISD::Constant:
483 AM.Disp += cast<ConstantSDNode>(N)->getValue();
484 return false;
485 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000486 // We might have folded the load into this shift, so don't regen the value
487 // if so.
488 if (ExprMap.count(N)) break;
489
Chris Lattner2b937862005-01-12 07:33:20 +0000490 if (AM.IndexReg == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000491 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
492 unsigned Val = CN->getValue();
493 if (Val == 1 || Val == 2 || Val == 3) {
494 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000495 SDOperand ShVal = N.Val->getOperand(0);
496
497 // Okay, we know that we have a scale by now. However, if the scaled
498 // value is an add of something and a constant, we can fold the
499 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000500 if (ShVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(ShVal) &&
Chris Lattner51a26342005-01-11 06:36:20 +0000501 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
502 AM.IndexReg = SelectExpr(ShVal.Val->getOperand(0));
503 ConstantSDNode *AddVal =
504 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
505 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000506 } else {
Chris Lattner51a26342005-01-11 06:36:20 +0000507 AM.IndexReg = SelectExpr(ShVal);
508 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000509 return false;
510 }
511 }
512 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000513 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000514 // We might have folded the load into this mul, so don't regen the value if
515 // so.
516 if (ExprMap.count(N)) break;
517
Chris Lattner947d5442005-01-11 19:37:02 +0000518 // X*[3,5,9] -> X+X*[2,4,8]
519 if (AM.IndexReg == 0 && AM.BaseType == X86AddressMode::RegBase &&
520 AM.Base.Reg == 0)
521 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
522 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
523 AM.Scale = unsigned(CN->getValue())-1;
524
525 SDOperand MulVal = N.Val->getOperand(0);
526 unsigned Reg;
527
528 // Okay, we know that we have a scale by now. However, if the scaled
529 // value is an add of something and a constant, we can fold the
530 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000531 if (MulVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(MulVal) &&
Chris Lattner947d5442005-01-11 19:37:02 +0000532 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
533 Reg = SelectExpr(MulVal.Val->getOperand(0));
534 ConstantSDNode *AddVal =
535 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
536 AM.Disp += AddVal->getValue() * CN->getValue();
537 } else {
538 Reg = SelectExpr(N.Val->getOperand(0));
539 }
540
541 AM.IndexReg = AM.Base.Reg = Reg;
542 return false;
543 }
544 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000545
546 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000547 // We might have folded the load into this mul, so don't regen the value if
548 // so.
549 if (ExprMap.count(N)) break;
550
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000551 X86AddressMode Backup = AM;
552 if (!SelectAddress(N.Val->getOperand(0), AM) &&
553 !SelectAddress(N.Val->getOperand(1), AM))
554 return false;
555 AM = Backup;
Chris Lattner9bbd9922005-01-12 18:08:53 +0000556 if (!SelectAddress(N.Val->getOperand(1), AM) &&
557 !SelectAddress(N.Val->getOperand(0), AM))
558 return false;
559 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000560 break;
561 }
562 }
563
Chris Lattnera95589b2005-01-11 04:40:19 +0000564 // Is the base register already occupied?
565 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
566 // If so, check to see if the scale index register is set.
567 if (AM.IndexReg == 0) {
568 AM.IndexReg = SelectExpr(N);
569 AM.Scale = 1;
570 return false;
571 }
572
573 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000574 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000575 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000576
577 // Default, generate it as a register.
578 AM.BaseType = X86AddressMode::RegBase;
579 AM.Base.Reg = SelectExpr(N);
580 return false;
581}
582
583/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
584/// assuming that the temporary registers are in the 8-bit register class.
585///
586/// Tmp1 = setcc1
587/// Tmp2 = setcc2
588/// DestReg = logicalop Tmp1, Tmp2
589///
590static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
591 unsigned SetCC2, unsigned LogicalOp,
592 unsigned DestReg) {
593 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
594 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
595 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
596 BuildMI(BB, SetCC1, 0, Tmp1);
597 BuildMI(BB, SetCC2, 0, Tmp2);
598 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
599}
600
601/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
602/// condition codes match the specified SetCCOpcode. Note that some conditions
603/// require multiple instructions to generate the correct value.
604static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
605 ISD::CondCode SetCCOpcode, bool isFP) {
606 unsigned Opc;
607 if (!isFP) {
608 switch (SetCCOpcode) {
609 default: assert(0 && "Illegal integer SetCC!");
610 case ISD::SETEQ: Opc = X86::SETEr; break;
611 case ISD::SETGT: Opc = X86::SETGr; break;
612 case ISD::SETGE: Opc = X86::SETGEr; break;
613 case ISD::SETLT: Opc = X86::SETLr; break;
614 case ISD::SETLE: Opc = X86::SETLEr; break;
615 case ISD::SETNE: Opc = X86::SETNEr; break;
616 case ISD::SETULT: Opc = X86::SETBr; break;
617 case ISD::SETUGT: Opc = X86::SETAr; break;
618 case ISD::SETULE: Opc = X86::SETBEr; break;
619 case ISD::SETUGE: Opc = X86::SETAEr; break;
620 }
621 } else {
622 // On a floating point condition, the flags are set as follows:
623 // ZF PF CF op
624 // 0 | 0 | 0 | X > Y
625 // 0 | 0 | 1 | X < Y
626 // 1 | 0 | 0 | X == Y
627 // 1 | 1 | 1 | unordered
628 //
629 switch (SetCCOpcode) {
630 default: assert(0 && "Invalid FP setcc!");
631 case ISD::SETUEQ:
632 case ISD::SETEQ:
633 Opc = X86::SETEr; // True if ZF = 1
634 break;
635 case ISD::SETOGT:
636 case ISD::SETGT:
637 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
638 break;
639 case ISD::SETOGE:
640 case ISD::SETGE:
641 Opc = X86::SETAEr; // True if CF = 0
642 break;
643 case ISD::SETULT:
644 case ISD::SETLT:
645 Opc = X86::SETBr; // True if CF = 1
646 break;
647 case ISD::SETULE:
648 case ISD::SETLE:
649 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
650 break;
651 case ISD::SETONE:
652 case ISD::SETNE:
653 Opc = X86::SETNEr; // True if ZF = 0
654 break;
655 case ISD::SETUO:
656 Opc = X86::SETPr; // True if PF = 1
657 break;
658 case ISD::SETO:
659 Opc = X86::SETNPr; // True if PF = 0
660 break;
661 case ISD::SETOEQ: // !PF & ZF
662 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
663 return;
664 case ISD::SETOLT: // !PF & CF
665 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
666 return;
667 case ISD::SETOLE: // !PF & (CF || ZF)
668 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
669 return;
670 case ISD::SETUGT: // PF | (!ZF & !CF)
671 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
672 return;
673 case ISD::SETUGE: // PF | !CF
674 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
675 return;
676 case ISD::SETUNE: // PF | !ZF
677 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
678 return;
679 }
680 }
681 BuildMI(BB, Opc, 0, DestReg);
682}
683
684
685/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
686/// the Dest block if the Cond condition is true. If we cannot fold this
687/// condition into the branch, return true.
688///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000689bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
690 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000691 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
692 // B) using two conditional branches instead of one condbr, two setcc's, and
693 // an or.
694 if ((Cond.getOpcode() == ISD::OR ||
695 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
696 // And and or set the flags for us, so there is no need to emit a TST of the
697 // result. It is only safe to do this if there is only a single use of the
698 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000699 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000700 SelectExpr(Cond);
701 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
702 return false;
703 }
704
705 // Codegen br not C -> JE.
706 if (Cond.getOpcode() == ISD::XOR)
707 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
708 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000709 unsigned CondR;
710 if (getRegPressure(Chain) > getRegPressure(Cond)) {
711 Select(Chain);
712 CondR = SelectExpr(Cond.Val->getOperand(0));
713 } else {
714 CondR = SelectExpr(Cond.Val->getOperand(0));
715 Select(Chain);
716 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000717 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
718 BuildMI(BB, X86::JE, 1).addMBB(Dest);
719 return false;
720 }
721
722 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
723 if (SetCC == 0)
724 return true; // Can only handle simple setcc's so far.
725
726 unsigned Opc;
727
728 // Handle integer conditions first.
729 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
730 switch (SetCC->getCondition()) {
731 default: assert(0 && "Illegal integer SetCC!");
732 case ISD::SETEQ: Opc = X86::JE; break;
733 case ISD::SETGT: Opc = X86::JG; break;
734 case ISD::SETGE: Opc = X86::JGE; break;
735 case ISD::SETLT: Opc = X86::JL; break;
736 case ISD::SETLE: Opc = X86::JLE; break;
737 case ISD::SETNE: Opc = X86::JNE; break;
738 case ISD::SETULT: Opc = X86::JB; break;
739 case ISD::SETUGT: Opc = X86::JA; break;
740 case ISD::SETULE: Opc = X86::JBE; break;
741 case ISD::SETUGE: Opc = X86::JAE; break;
742 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000743 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000744 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
745 BuildMI(BB, Opc, 1).addMBB(Dest);
746 return false;
747 }
748
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000749 unsigned Opc2 = 0; // Second branch if needed.
750
751 // On a floating point condition, the flags are set as follows:
752 // ZF PF CF op
753 // 0 | 0 | 0 | X > Y
754 // 0 | 0 | 1 | X < Y
755 // 1 | 0 | 0 | X == Y
756 // 1 | 1 | 1 | unordered
757 //
758 switch (SetCC->getCondition()) {
759 default: assert(0 && "Invalid FP setcc!");
760 case ISD::SETUEQ:
761 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
762 case ISD::SETOGT:
763 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
764 case ISD::SETOGE:
765 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
766 case ISD::SETULT:
767 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
768 case ISD::SETULE:
769 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
770 case ISD::SETONE:
771 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
772 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
773 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
774 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
775 Opc = X86::JA; // ZF = 0 & CF = 0
776 Opc2 = X86::JP; // PF = 1
777 break;
778 case ISD::SETUGE: // PF = 1 | CF = 0
779 Opc = X86::JAE; // CF = 0
780 Opc2 = X86::JP; // PF = 1
781 break;
782 case ISD::SETUNE: // PF = 1 | ZF = 0
783 Opc = X86::JNE; // ZF = 0
784 Opc2 = X86::JP; // PF = 1
785 break;
786 case ISD::SETOEQ: // PF = 0 & ZF = 1
787 //X86::JNP, X86::JE
788 //X86::AND8rr
789 return true; // FIXME: Emit more efficient code for this branch.
790 case ISD::SETOLT: // PF = 0 & CF = 1
791 //X86::JNP, X86::JB
792 //X86::AND8rr
793 return true; // FIXME: Emit more efficient code for this branch.
794 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
795 //X86::JNP, X86::JBE
796 //X86::AND8rr
797 return true; // FIXME: Emit more efficient code for this branch.
798 }
799
Chris Lattner6c07aee2005-01-11 04:06:27 +0000800 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000801 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
802 BuildMI(BB, Opc, 1).addMBB(Dest);
803 if (Opc2)
804 BuildMI(BB, Opc2, 1).addMBB(Dest);
805 return false;
806}
807
Chris Lattner24aad1b2005-01-10 22:10:13 +0000808/// EmitSelectCC - Emit code into BB that performs a select operation between
809/// the two registers RTrue and RFalse, generating a result into RDest. Return
810/// true if the fold cannot be performed.
811///
812void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
813 unsigned RTrue, unsigned RFalse, unsigned RDest) {
814 enum Condition {
815 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
816 NOT_SET
817 } CondCode = NOT_SET;
818
819 static const unsigned CMOVTAB16[] = {
820 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
821 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
822 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
823 };
824 static const unsigned CMOVTAB32[] = {
825 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
826 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
827 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
828 };
829 static const unsigned CMOVTABFP[] = {
830 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
831 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
832 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
833 };
834
835 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
836 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
837 switch (SetCC->getCondition()) {
838 default: assert(0 && "Unknown integer comparison!");
839 case ISD::SETEQ: CondCode = EQ; break;
840 case ISD::SETGT: CondCode = GT; break;
841 case ISD::SETGE: CondCode = GE; break;
842 case ISD::SETLT: CondCode = LT; break;
843 case ISD::SETLE: CondCode = LE; break;
844 case ISD::SETNE: CondCode = NE; break;
845 case ISD::SETULT: CondCode = B; break;
846 case ISD::SETUGT: CondCode = A; break;
847 case ISD::SETULE: CondCode = BE; break;
848 case ISD::SETUGE: CondCode = AE; break;
849 }
850 } else {
851 // On a floating point condition, the flags are set as follows:
852 // ZF PF CF op
853 // 0 | 0 | 0 | X > Y
854 // 0 | 0 | 1 | X < Y
855 // 1 | 0 | 0 | X == Y
856 // 1 | 1 | 1 | unordered
857 //
858 switch (SetCC->getCondition()) {
859 default: assert(0 && "Unknown FP comparison!");
860 case ISD::SETUEQ:
861 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
862 case ISD::SETOGT:
863 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
864 case ISD::SETOGE:
865 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
866 case ISD::SETULT:
867 case ISD::SETLT: CondCode = B; break; // True if CF = 1
868 case ISD::SETULE:
869 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
870 case ISD::SETONE:
871 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
872 case ISD::SETUO: CondCode = P; break; // True if PF = 1
873 case ISD::SETO: CondCode = NP; break; // True if PF = 0
874 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
875 case ISD::SETUGE: // PF = 1 | CF = 0
876 case ISD::SETUNE: // PF = 1 | ZF = 0
877 case ISD::SETOEQ: // PF = 0 & ZF = 1
878 case ISD::SETOLT: // PF = 0 & CF = 1
879 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
880 // We cannot emit this comparison as a single cmov.
881 break;
882 }
883 }
884 }
885
886 unsigned Opc = 0;
887 if (CondCode != NOT_SET) {
888 switch (SVT) {
889 default: assert(0 && "Cannot select this type!");
890 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
891 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
892 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000893 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000894 }
895 }
896
897 // Finally, if we weren't able to fold this, just emit the condition and test
898 // it.
899 if (CondCode == NOT_SET || Opc == 0) {
900 // Get the condition into the zero flag.
901 unsigned CondReg = SelectExpr(Cond);
902 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
903
904 switch (SVT) {
905 default: assert(0 && "Cannot select this type!");
906 case MVT::i16: Opc = X86::CMOVE16rr; break;
907 case MVT::i32: Opc = X86::CMOVE32rr; break;
908 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000909 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000910 }
911 } else {
912 // FIXME: CMP R, 0 -> TEST R, R
913 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000914 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000915 }
916 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
917}
918
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000919void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000920 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000921 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
922 Opc = 0;
Chris Lattneref6806c2005-01-12 02:02:48 +0000923 if (isFoldableLoad(LHS)) {
924 switch (RHS.getValueType()) {
925 default: break;
926 case MVT::i1:
927 case MVT::i8: Opc = X86::CMP8mi; break;
928 case MVT::i16: Opc = X86::CMP16mi; break;
929 case MVT::i32: Opc = X86::CMP32mi; break;
930 }
931 if (Opc) {
932 X86AddressMode AM;
933 EmitFoldedLoad(LHS, AM);
934 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
935 return;
936 }
937 }
938
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000939 switch (RHS.getValueType()) {
940 default: break;
941 case MVT::i1:
942 case MVT::i8: Opc = X86::CMP8ri; break;
943 case MVT::i16: Opc = X86::CMP16ri; break;
944 case MVT::i32: Opc = X86::CMP32ri; break;
945 }
946 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000947 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000948 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
949 return;
950 }
Chris Lattner7f2afac2005-01-14 22:37:41 +0000951 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
952 if (CN->isExactlyValue(+0.0) ||
953 CN->isExactlyValue(-0.0)) {
954 unsigned Reg = SelectExpr(LHS);
955 BuildMI(BB, X86::FTST, 1).addReg(Reg);
956 BuildMI(BB, X86::FNSTSW8r, 0);
957 BuildMI(BB, X86::SAHF, 1);
958 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000959 }
960
Chris Lattneref6806c2005-01-12 02:02:48 +0000961 Opc = 0;
962 if (isFoldableLoad(LHS)) {
963 switch (RHS.getValueType()) {
964 default: break;
965 case MVT::i1:
966 case MVT::i8: Opc = X86::CMP8mr; break;
967 case MVT::i16: Opc = X86::CMP16mr; break;
968 case MVT::i32: Opc = X86::CMP32mr; break;
969 }
970 if (Opc) {
971 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +0000972 EmitFoldedLoad(LHS, AM);
973 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +0000974 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
975 return;
976 }
977 }
978
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000979 switch (LHS.getValueType()) {
980 default: assert(0 && "Cannot compare this value!");
981 case MVT::i1:
982 case MVT::i8: Opc = X86::CMP8rr; break;
983 case MVT::i16: Opc = X86::CMP16rr; break;
984 case MVT::i32: Opc = X86::CMP32rr; break;
985 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000986 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000987 }
Chris Lattner11333092005-01-11 03:11:44 +0000988 unsigned Tmp1, Tmp2;
989 if (getRegPressure(LHS) > getRegPressure(RHS)) {
990 Tmp1 = SelectExpr(LHS);
991 Tmp2 = SelectExpr(RHS);
992 } else {
993 Tmp2 = SelectExpr(RHS);
994 Tmp1 = SelectExpr(LHS);
995 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000996 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
997}
998
Chris Lattnera5ade062005-01-11 21:19:59 +0000999/// isFoldableLoad - Return true if this is a load instruction that can safely
1000/// be folded into an operation that uses it.
1001bool ISel::isFoldableLoad(SDOperand Op) {
1002 if (Op.getOpcode() != ISD::LOAD ||
1003 // FIXME: currently can't fold constant pool indexes.
1004 isa<ConstantPoolSDNode>(Op.getOperand(1)))
1005 return false;
1006
1007 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001008 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1009 if (ExprMap.count(Op.getValue(1))) return false;
1010 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
1011 assert(!LoweredTokens.count(Op.getValue(1)) &&
1012 "Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001013
Chris Lattnera0bb6922005-01-12 18:38:26 +00001014 // Finally, there can only be one use of its value.
1015 return Op.Val->hasNUsesOfValue(1, 0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001016}
1017
1018/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1019/// and compute the address being loaded into AM.
1020void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1021 SDOperand Chain = Op.getOperand(0);
1022 SDOperand Address = Op.getOperand(1);
1023 if (getRegPressure(Chain) > getRegPressure(Address)) {
1024 Select(Chain);
1025 SelectAddress(Address, AM);
1026 } else {
1027 SelectAddress(Address, AM);
1028 Select(Chain);
1029 }
1030
1031 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001032 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1033 "Load emitted more than once?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001034 ExprMap[SDOperand(Op.Val, 1)] = 1;
Chris Lattner636e79a2005-01-13 05:53:16 +00001035 if (!LoweredTokens.insert(Op.getValue(1)).second)
1036 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001037}
1038
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001039unsigned ISel::SelectExpr(SDOperand N) {
1040 unsigned Result;
1041 unsigned Tmp1, Tmp2, Tmp3;
1042 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001043 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001044 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001045
Chris Lattner7f2afac2005-01-14 22:37:41 +00001046 if (Node->getOpcode() == ISD::CopyFromReg) {
1047 // FIXME: Handle copy from physregs!
1048
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001049 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001050 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001051 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001052
1053 unsigned &Reg = ExprMap[N];
1054 if (Reg) return Reg;
1055
1056 if (N.getOpcode() != ISD::CALL)
1057 Reg = Result = (N.getValueType() != MVT::Other) ?
1058 MakeReg(N.getValueType()) : 1;
1059 else {
1060 // If this is a call instruction, make sure to prepare ALL of the result
1061 // values as well as the chain.
1062 if (Node->getNumValues() == 1)
1063 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001064 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001065 Result = MakeReg(Node->getValueType(0));
1066 ExprMap[N.getValue(0)] = Result;
1067 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1068 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1069 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001070 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001071 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001072
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001073 switch (N.getOpcode()) {
1074 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001075 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001076 assert(0 && "Node not handled!\n");
1077 case ISD::FrameIndex:
1078 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1079 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1080 return Result;
1081 case ISD::ConstantPool:
1082 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1083 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1084 return Result;
1085 case ISD::ConstantFP:
1086 ContainsFPCode = true;
1087 Tmp1 = Result; // Intermediate Register
1088 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1089 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1090 Tmp1 = MakeReg(MVT::f64);
1091
1092 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1093 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1094 BuildMI(BB, X86::FLD0, 0, Tmp1);
1095 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1096 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1097 BuildMI(BB, X86::FLD1, 0, Tmp1);
1098 else
1099 assert(0 && "Unexpected constant!");
1100 if (Tmp1 != Result)
1101 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1102 return Result;
1103 case ISD::Constant:
1104 switch (N.getValueType()) {
1105 default: assert(0 && "Cannot use constants of this type!");
1106 case MVT::i1:
1107 case MVT::i8: Opc = X86::MOV8ri; break;
1108 case MVT::i16: Opc = X86::MOV16ri; break;
1109 case MVT::i32: Opc = X86::MOV32ri; break;
1110 }
1111 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1112 return Result;
1113 case ISD::GlobalAddress: {
1114 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1115 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1116 return Result;
1117 }
1118 case ISD::ExternalSymbol: {
1119 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1120 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1121 return Result;
1122 }
1123 case ISD::FP_EXTEND:
1124 Tmp1 = SelectExpr(N.getOperand(0));
1125 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001126 return Result;
1127 case ISD::ZERO_EXTEND: {
1128 int DestIs16 = N.getValueType() == MVT::i16;
1129 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001130
1131 // FIXME: This hack is here for zero extension casts from bool to i8. This
1132 // would not be needed if bools were promoted by Legalize.
1133 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001134 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001135 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1136 return Result;
1137 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001138
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001139 if (isFoldableLoad(N.getOperand(0))) {
1140 static const unsigned Opc[3] = {
1141 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1142 };
1143
1144 X86AddressMode AM;
1145 EmitFoldedLoad(N.getOperand(0), AM);
1146 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1147
1148 return Result;
1149 }
1150
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001151 static const unsigned Opc[3] = {
1152 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1153 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001154 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001155 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1156 return Result;
1157 }
1158 case ISD::SIGN_EXTEND: {
1159 int DestIs16 = N.getValueType() == MVT::i16;
1160 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1161
Chris Lattner590d8002005-01-09 18:52:44 +00001162 // FIXME: Legalize should promote bools to i8!
1163 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1164 "Sign extend from bool not implemented!");
1165
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001166 if (isFoldableLoad(N.getOperand(0))) {
1167 static const unsigned Opc[3] = {
1168 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1169 };
1170
1171 X86AddressMode AM;
1172 EmitFoldedLoad(N.getOperand(0), AM);
1173 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1174 return Result;
1175 }
1176
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001177 static const unsigned Opc[3] = {
1178 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1179 };
1180 Tmp1 = SelectExpr(N.getOperand(0));
1181 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1182 return Result;
1183 }
1184 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001185 // Fold TRUNCATE (LOAD P) into a smaller load from P.
1186 if (isFoldableLoad(N.getOperand(0))) {
1187 switch (N.getValueType()) {
1188 default: assert(0 && "Unknown truncate!");
1189 case MVT::i1:
1190 case MVT::i8: Opc = X86::MOV8rm; break;
1191 case MVT::i16: Opc = X86::MOV16rm; break;
1192 }
1193 X86AddressMode AM;
1194 EmitFoldedLoad(N.getOperand(0), AM);
1195 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1196 return Result;
1197 }
1198
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001199 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1200 // a move out of AX or AL.
1201 switch (N.getOperand(0).getValueType()) {
1202 default: assert(0 && "Unknown truncate!");
1203 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1204 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1205 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1206 }
1207 Tmp1 = SelectExpr(N.getOperand(0));
1208 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1209
1210 switch (N.getValueType()) {
1211 default: assert(0 && "Unknown truncate!");
1212 case MVT::i1:
1213 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1214 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1215 }
1216 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1217 return Result;
1218
1219 case ISD::FP_ROUND:
1220 // Truncate from double to float by storing to memory as float,
1221 // then reading it back into a register.
1222
1223 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001224 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001225 Tmp1 = TLI.getTargetData().getFloatAlignment();
1226 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1227
1228 // Codegen the input.
1229 Tmp1 = SelectExpr(N.getOperand(0));
1230
1231 // Emit the store, then the reload.
1232 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1233 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001234 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001235
1236 case ISD::SINT_TO_FP:
1237 case ISD::UINT_TO_FP: {
1238 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001239 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001240
1241 // Promote the integer to a type supported by FLD. We do this because there
1242 // are no unsigned FLD instructions, so we must promote an unsigned value to
1243 // a larger signed value, then use FLD on the larger value.
1244 //
1245 MVT::ValueType PromoteType = MVT::Other;
1246 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1247 unsigned PromoteOpcode = 0;
1248 unsigned RealDestReg = Result;
1249 switch (SrcTy) {
1250 case MVT::i1:
1251 case MVT::i8:
1252 // We don't have the facilities for directly loading byte sized data from
1253 // memory (even signed). Promote it to 16 bits.
1254 PromoteType = MVT::i16;
1255 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1256 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1257 break;
1258 case MVT::i16:
1259 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1260 PromoteType = MVT::i32;
1261 PromoteOpcode = X86::MOVZX32rr16;
1262 }
1263 break;
1264 default:
1265 // Don't fild into the real destination.
1266 if (Node->getOpcode() == ISD::UINT_TO_FP)
1267 Result = MakeReg(Node->getValueType(0));
1268 break;
1269 }
1270
1271 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1272
1273 if (PromoteType != MVT::Other) {
1274 Tmp2 = MakeReg(PromoteType);
1275 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1276 SrcTy = PromoteType;
1277 Tmp1 = Tmp2;
1278 }
1279
1280 // Spill the integer to memory and reload it from there.
1281 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1282 MachineFunction *F = BB->getParent();
1283 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1284
1285 switch (SrcTy) {
1286 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001287 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001288 // FIXME: this won't work for cast [u]long to FP
1289 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1290 FrameIdx).addReg(Tmp1);
1291 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1292 FrameIdx, 4).addReg(Tmp1+1);
1293 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1294 break;
1295 case MVT::i32:
1296 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1297 FrameIdx).addReg(Tmp1);
1298 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1299 break;
1300 case MVT::i16:
1301 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1302 FrameIdx).addReg(Tmp1);
1303 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1304 break;
1305 default: break; // No promotion required.
1306 }
1307
Chris Lattner085c9952005-01-12 04:00:00 +00001308 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001309 // If this is a cast from uint -> double, we need to be careful when if
1310 // the "sign" bit is set. If so, we don't want to make a negative number,
1311 // we want to make a positive number. Emit code to add an offset if the
1312 // sign bit is set.
1313
1314 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1315 unsigned IsNeg = MakeReg(MVT::i32);
1316 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1317
1318 // Create a CP value that has the offset in one word and 0 in the other.
1319 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1320 0x4f80000000000000ULL);
1321 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1322 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1323 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1324
1325 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1326 // We need special handling for unsigned 64-bit integer sources. If the
1327 // input number has the "sign bit" set, then we loaded it incorrectly as a
1328 // negative 64-bit number. In this case, add an offset value.
1329
1330 // Emit a test instruction to see if the dynamic input value was signed.
1331 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1332
1333 // If the sign bit is set, get a pointer to an offset, otherwise get a
1334 // pointer to a zero.
1335 MachineConstantPool *CP = F->getConstantPool();
1336 unsigned Zero = MakeReg(MVT::i32);
1337 Constant *Null = Constant::getNullValue(Type::UIntTy);
1338 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1339 CP->getConstantPoolIndex(Null));
1340 unsigned Offset = MakeReg(MVT::i32);
1341 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1342
1343 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1344 CP->getConstantPoolIndex(OffsetCst));
1345 unsigned Addr = MakeReg(MVT::i32);
1346 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1347
1348 // Load the constant for an add. FIXME: this could make an 'fadd' that
1349 // reads directly from memory, but we don't support these yet.
1350 unsigned ConstReg = MakeReg(MVT::f64);
1351 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1352
1353 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1354 }
1355 return RealDestReg;
1356 }
1357 case ISD::FP_TO_SINT:
1358 case ISD::FP_TO_UINT: {
1359 // FIXME: Most of this grunt work should be done by legalize!
1360 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1361
1362 // Change the floating point control register to use "round towards zero"
1363 // mode when truncating to an integer value.
1364 //
1365 MachineFunction *F = BB->getParent();
1366 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1367 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1368
1369 // Load the old value of the high byte of the control word...
1370 unsigned HighPartOfCW = MakeReg(MVT::i8);
1371 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1372 CWFrameIdx, 1);
1373
1374 // Set the high part to be round to zero...
1375 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1376 CWFrameIdx, 1).addImm(12);
1377
1378 // Reload the modified control word now...
1379 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1380
1381 // Restore the memory image of control word to original value
1382 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1383 CWFrameIdx, 1).addReg(HighPartOfCW);
1384
1385 // We don't have the facilities for directly storing byte sized data to
1386 // memory. Promote it to 16 bits. We also must promote unsigned values to
1387 // larger classes because we only have signed FP stores.
1388 MVT::ValueType StoreClass = Node->getValueType(0);
1389 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1390 switch (StoreClass) {
1391 case MVT::i8: StoreClass = MVT::i16; break;
1392 case MVT::i16: StoreClass = MVT::i32; break;
1393 case MVT::i32: StoreClass = MVT::i64; break;
1394 // The following treatment of cLong may not be perfectly right,
1395 // but it survives chains of casts of the form
1396 // double->ulong->double.
1397 case MVT::i64: StoreClass = MVT::i64; break;
1398 default: assert(0 && "Unknown store class!");
1399 }
1400
1401 // Spill the integer to memory and reload it from there.
1402 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1403 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1404
1405 switch (StoreClass) {
1406 default: assert(0 && "Unknown store class!");
1407 case MVT::i16:
1408 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1409 break;
1410 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001411 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001412 break;
1413 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001414 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001415 break;
1416 }
1417
1418 switch (Node->getValueType(0)) {
1419 default:
1420 assert(0 && "Unknown integer type!");
1421 case MVT::i64:
1422 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001423 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001424 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1425 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1426 case MVT::i32:
1427 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1428 break;
1429 case MVT::i16:
1430 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1431 break;
1432 case MVT::i8:
1433 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1434 break;
1435 }
1436
1437 // Reload the original control word now.
1438 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1439 return Result;
1440 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001441 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001442 Op0 = N.getOperand(0);
1443 Op1 = N.getOperand(1);
1444
1445 if (isFoldableLoad(Op0))
1446 std::swap(Op0, Op1);
1447
1448 if (isFoldableLoad(Op1)) {
1449 switch (N.getValueType()) {
1450 default: assert(0 && "Cannot add this type!");
1451 case MVT::i1:
1452 case MVT::i8: Opc = X86::ADD8rm; break;
1453 case MVT::i16: Opc = X86::ADD16rm; break;
1454 case MVT::i32: Opc = X86::ADD32rm; break;
1455 case MVT::f32: Opc = X86::FADD32m; break;
1456 case MVT::f64: Opc = X86::FADD64m; break;
1457 }
1458 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001459 EmitFoldedLoad(Op1, AM);
1460 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001461 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1462 return Result;
1463 }
1464
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001465 // See if we can codegen this as an LEA to fold operations together.
1466 if (N.getValueType() == MVT::i32) {
1467 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001468 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001469 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001470 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001471 // leave this as LEA, then peephole it to 'ADD' after two address elim
1472 // happens.
1473 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001474 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001475 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1476 return Result;
1477 }
1478 }
1479 }
Chris Lattner11333092005-01-11 03:11:44 +00001480
Chris Lattnera5ade062005-01-11 21:19:59 +00001481 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001482 Opc = 0;
1483 if (CN->getValue() == 1) { // add X, 1 -> inc X
1484 switch (N.getValueType()) {
1485 default: assert(0 && "Cannot integer add this type!");
1486 case MVT::i8: Opc = X86::INC8r; break;
1487 case MVT::i16: Opc = X86::INC16r; break;
1488 case MVT::i32: Opc = X86::INC32r; break;
1489 }
1490 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1491 switch (N.getValueType()) {
1492 default: assert(0 && "Cannot integer add this type!");
1493 case MVT::i8: Opc = X86::DEC8r; break;
1494 case MVT::i16: Opc = X86::DEC16r; break;
1495 case MVT::i32: Opc = X86::DEC32r; break;
1496 }
1497 }
1498
1499 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001500 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001501 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1502 return Result;
1503 }
1504
1505 switch (N.getValueType()) {
1506 default: assert(0 && "Cannot add this type!");
1507 case MVT::i8: Opc = X86::ADD8ri; break;
1508 case MVT::i16: Opc = X86::ADD16ri; break;
1509 case MVT::i32: Opc = X86::ADD32ri; break;
1510 }
1511 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001512 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001513 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1514 return Result;
1515 }
1516 }
1517
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001518 switch (N.getValueType()) {
1519 default: assert(0 && "Cannot add this type!");
1520 case MVT::i8: Opc = X86::ADD8rr; break;
1521 case MVT::i16: Opc = X86::ADD16rr; break;
1522 case MVT::i32: Opc = X86::ADD32rr; break;
1523 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001524 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001525 }
Chris Lattner11333092005-01-11 03:11:44 +00001526
Chris Lattnera5ade062005-01-11 21:19:59 +00001527 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1528 Tmp1 = SelectExpr(Op0);
1529 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001530 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001531 Tmp2 = SelectExpr(Op1);
1532 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001533 }
1534
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001535 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1536 return Result;
1537 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001538 case ISD::MUL:
1539 case ISD::AND:
1540 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001541 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001542 static const unsigned SUBTab[] = {
1543 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1544 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1545 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1546 };
1547 static const unsigned MULTab[] = {
1548 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1549 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1550 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1551 };
1552 static const unsigned ANDTab[] = {
1553 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1554 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1555 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1556 };
1557 static const unsigned ORTab[] = {
1558 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1559 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1560 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1561 };
1562 static const unsigned XORTab[] = {
1563 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1564 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1565 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1566 };
1567
1568 Op0 = Node->getOperand(0);
1569 Op1 = Node->getOperand(1);
1570
1571 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001572 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1573 if (CN->isNullValue()) { // 0 - N -> neg N
1574 switch (N.getValueType()) {
1575 default: assert(0 && "Cannot sub this type!");
1576 case MVT::i1:
1577 case MVT::i8: Opc = X86::NEG8r; break;
1578 case MVT::i16: Opc = X86::NEG16r; break;
1579 case MVT::i32: Opc = X86::NEG32r; break;
1580 }
1581 Tmp1 = SelectExpr(N.getOperand(1));
1582 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1583 return Result;
1584 }
1585
Chris Lattnera5ade062005-01-11 21:19:59 +00001586 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1587 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001588 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001589 switch (N.getValueType()) {
1590 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001591 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001592 case MVT::i8: Opc = X86::NOT8r; break;
1593 case MVT::i16: Opc = X86::NOT16r; break;
1594 case MVT::i32: Opc = X86::NOT32r; break;
1595 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001596 if (Opc) {
1597 Tmp1 = SelectExpr(Op0);
1598 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1599 return Result;
1600 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001601 }
1602
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001603 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001604 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001605 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001606 case MVT::i8: Opc = 0; break;
1607 case MVT::i16: Opc = 1; break;
1608 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001609 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001610 switch (Node->getOpcode()) {
1611 default: assert(0 && "Unreachable!");
1612 case ISD::SUB: Opc = SUBTab[Opc]; break;
1613 case ISD::MUL: Opc = MULTab[Opc]; break;
1614 case ISD::AND: Opc = ANDTab[Opc]; break;
1615 case ISD::OR: Opc = ORTab[Opc]; break;
1616 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001617 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001618 if (Opc) { // Can't fold MUL:i8 R, imm
1619 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001620 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1621 return Result;
1622 }
1623 }
Chris Lattner11333092005-01-11 03:11:44 +00001624
Chris Lattnera5ade062005-01-11 21:19:59 +00001625 if (isFoldableLoad(Op0))
1626 if (Node->getOpcode() != ISD::SUB) {
1627 std::swap(Op0, Op1);
1628 } else {
1629 // Emit 'reverse' subract, with a memory operand.
1630 switch (N.getValueType()) {
1631 default: Opc = 0; break;
1632 case MVT::f32: Opc = X86::FSUBR32m; break;
1633 case MVT::f64: Opc = X86::FSUBR64m; break;
1634 }
1635 if (Opc) {
1636 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001637 EmitFoldedLoad(Op0, AM);
1638 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001639 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1640 return Result;
1641 }
1642 }
1643
1644 if (isFoldableLoad(Op1)) {
1645 switch (N.getValueType()) {
1646 default: assert(0 && "Cannot operate on this type!");
1647 case MVT::i1:
1648 case MVT::i8: Opc = 5; break;
1649 case MVT::i16: Opc = 6; break;
1650 case MVT::i32: Opc = 7; break;
1651 case MVT::f32: Opc = 8; break;
1652 case MVT::f64: Opc = 9; break;
1653 }
1654 switch (Node->getOpcode()) {
1655 default: assert(0 && "Unreachable!");
1656 case ISD::SUB: Opc = SUBTab[Opc]; break;
1657 case ISD::MUL: Opc = MULTab[Opc]; break;
1658 case ISD::AND: Opc = ANDTab[Opc]; break;
1659 case ISD::OR: Opc = ORTab[Opc]; break;
1660 case ISD::XOR: Opc = XORTab[Opc]; break;
1661 }
1662
1663 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001664 EmitFoldedLoad(Op1, AM);
1665 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001666 if (Opc) {
1667 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1668 } else {
1669 assert(Node->getOpcode() == ISD::MUL &&
1670 N.getValueType() == MVT::i8 && "Unexpected situation!");
1671 // Must use the MUL instruction, which forces use of AL.
1672 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1673 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1674 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1675 }
1676 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001677 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001678
1679 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1680 Tmp1 = SelectExpr(Op0);
1681 Tmp2 = SelectExpr(Op1);
1682 } else {
1683 Tmp2 = SelectExpr(Op1);
1684 Tmp1 = SelectExpr(Op0);
1685 }
1686
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001687 switch (N.getValueType()) {
1688 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001689 case MVT::i1:
1690 case MVT::i8: Opc = 10; break;
1691 case MVT::i16: Opc = 11; break;
1692 case MVT::i32: Opc = 12; break;
1693 case MVT::f32: Opc = 13; break;
1694 case MVT::f64: Opc = 14; break;
1695 }
1696 switch (Node->getOpcode()) {
1697 default: assert(0 && "Unreachable!");
1698 case ISD::SUB: Opc = SUBTab[Opc]; break;
1699 case ISD::MUL: Opc = MULTab[Opc]; break;
1700 case ISD::AND: Opc = ANDTab[Opc]; break;
1701 case ISD::OR: Opc = ORTab[Opc]; break;
1702 case ISD::XOR: Opc = XORTab[Opc]; break;
1703 }
1704 if (Opc) {
1705 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1706 } else {
1707 assert(Node->getOpcode() == ISD::MUL &&
1708 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001709 // Must use the MUL instruction, which forces use of AL.
1710 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1711 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1712 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001713 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001714 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001715 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001716 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001717 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1718 Tmp2 = SelectExpr(N.getOperand(1));
1719 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001720 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001721 Tmp3 = SelectExpr(N.getOperand(2));
1722 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001723 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00001724 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
1725 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001726
1727 case ISD::SDIV:
1728 case ISD::UDIV:
1729 case ISD::SREM:
1730 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001731 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
1732 "We don't support this operator!");
1733
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001734 if (N.getOpcode() == ISD::SDIV)
1735 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1736 // FIXME: These special cases should be handled by the lowering impl!
1737 unsigned RHS = CN->getValue();
1738 bool isNeg = false;
1739 if ((int)RHS < 0) {
1740 isNeg = true;
1741 RHS = -RHS;
1742 }
1743 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1744 unsigned Log = log2(RHS);
1745 unsigned TmpReg = MakeReg(N.getValueType());
1746 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1747 switch (N.getValueType()) {
1748 default: assert("Unknown type to signed divide!");
1749 case MVT::i8:
1750 SAROpc = X86::SAR8ri;
1751 SHROpc = X86::SHR8ri;
1752 ADDOpc = X86::ADD8rr;
1753 NEGOpc = X86::NEG8r;
1754 break;
1755 case MVT::i16:
1756 SAROpc = X86::SAR16ri;
1757 SHROpc = X86::SHR16ri;
1758 ADDOpc = X86::ADD16rr;
1759 NEGOpc = X86::NEG16r;
1760 break;
1761 case MVT::i32:
1762 SAROpc = X86::SAR32ri;
1763 SHROpc = X86::SHR32ri;
1764 ADDOpc = X86::ADD32rr;
1765 NEGOpc = X86::NEG32r;
1766 break;
1767 }
Chris Lattner11333092005-01-11 03:11:44 +00001768 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001769 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1770 unsigned TmpReg2 = MakeReg(N.getValueType());
1771 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1772 unsigned TmpReg3 = MakeReg(N.getValueType());
1773 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1774
1775 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1776 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1777 if (isNeg)
1778 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1779 return Result;
1780 }
1781 }
1782
Chris Lattner11333092005-01-11 03:11:44 +00001783 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1784 Tmp1 = SelectExpr(N.getOperand(0));
1785 Tmp2 = SelectExpr(N.getOperand(1));
1786 } else {
1787 Tmp2 = SelectExpr(N.getOperand(1));
1788 Tmp1 = SelectExpr(N.getOperand(0));
1789 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001790
1791 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1792 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1793 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1794 switch (N.getValueType()) {
1795 default: assert(0 && "Cannot sdiv this type!");
1796 case MVT::i8:
1797 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1798 LoReg = X86::AL;
1799 HiReg = X86::AH;
1800 MovOpcode = X86::MOV8rr;
1801 ClrOpcode = X86::MOV8ri;
1802 SExtOpcode = X86::CBW;
1803 break;
1804 case MVT::i16:
1805 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1806 LoReg = X86::AX;
1807 HiReg = X86::DX;
1808 MovOpcode = X86::MOV16rr;
1809 ClrOpcode = X86::MOV16ri;
1810 SExtOpcode = X86::CWD;
1811 break;
1812 case MVT::i32:
1813 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001814 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001815 HiReg = X86::EDX;
1816 MovOpcode = X86::MOV32rr;
1817 ClrOpcode = X86::MOV32ri;
1818 SExtOpcode = X86::CDQ;
1819 break;
1820 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1821 case MVT::f32:
1822 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001823 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001824 return Result;
1825 }
1826
1827 // Set up the low part.
1828 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1829
1830 if (isSigned) {
1831 // Sign extend the low part into the high part.
1832 BuildMI(BB, SExtOpcode, 0);
1833 } else {
1834 // Zero out the high part, effectively zero extending the input.
1835 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1836 }
1837
1838 // Emit the DIV/IDIV instruction.
1839 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1840
1841 // Get the result of the divide or rem.
1842 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1843 return Result;
1844 }
1845
1846 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001847 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001848 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1849 switch (N.getValueType()) {
1850 default: assert(0 && "Cannot shift this type!");
1851 case MVT::i8: Opc = X86::ADD8rr; break;
1852 case MVT::i16: Opc = X86::ADD16rr; break;
1853 case MVT::i32: Opc = X86::ADD32rr; break;
1854 }
1855 Tmp1 = SelectExpr(N.getOperand(0));
1856 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1857 return Result;
1858 }
1859
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001860 switch (N.getValueType()) {
1861 default: assert(0 && "Cannot shift this type!");
1862 case MVT::i8: Opc = X86::SHL8ri; break;
1863 case MVT::i16: Opc = X86::SHL16ri; break;
1864 case MVT::i32: Opc = X86::SHL32ri; break;
1865 }
Chris Lattner11333092005-01-11 03:11:44 +00001866 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001867 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1868 return Result;
1869 }
Chris Lattner11333092005-01-11 03:11:44 +00001870
1871 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1872 Tmp1 = SelectExpr(N.getOperand(0));
1873 Tmp2 = SelectExpr(N.getOperand(1));
1874 } else {
1875 Tmp2 = SelectExpr(N.getOperand(1));
1876 Tmp1 = SelectExpr(N.getOperand(0));
1877 }
1878
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001879 switch (N.getValueType()) {
1880 default: assert(0 && "Cannot shift this type!");
1881 case MVT::i8 : Opc = X86::SHL8rCL; break;
1882 case MVT::i16: Opc = X86::SHL16rCL; break;
1883 case MVT::i32: Opc = X86::SHL32rCL; break;
1884 }
1885 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1886 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1887 return Result;
1888 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001889 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1890 switch (N.getValueType()) {
1891 default: assert(0 && "Cannot shift this type!");
1892 case MVT::i8: Opc = X86::SHR8ri; break;
1893 case MVT::i16: Opc = X86::SHR16ri; break;
1894 case MVT::i32: Opc = X86::SHR32ri; break;
1895 }
Chris Lattner11333092005-01-11 03:11:44 +00001896 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001897 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1898 return Result;
1899 }
Chris Lattner11333092005-01-11 03:11:44 +00001900
1901 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1902 Tmp1 = SelectExpr(N.getOperand(0));
1903 Tmp2 = SelectExpr(N.getOperand(1));
1904 } else {
1905 Tmp2 = SelectExpr(N.getOperand(1));
1906 Tmp1 = SelectExpr(N.getOperand(0));
1907 }
1908
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001909 switch (N.getValueType()) {
1910 default: assert(0 && "Cannot shift this type!");
1911 case MVT::i8 : Opc = X86::SHR8rCL; break;
1912 case MVT::i16: Opc = X86::SHR16rCL; break;
1913 case MVT::i32: Opc = X86::SHR32rCL; break;
1914 }
1915 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1916 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1917 return Result;
1918 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001919 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1920 switch (N.getValueType()) {
1921 default: assert(0 && "Cannot shift this type!");
1922 case MVT::i8: Opc = X86::SAR8ri; break;
1923 case MVT::i16: Opc = X86::SAR16ri; break;
1924 case MVT::i32: Opc = X86::SAR32ri; break;
1925 }
Chris Lattner11333092005-01-11 03:11:44 +00001926 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001927 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1928 return Result;
1929 }
Chris Lattner11333092005-01-11 03:11:44 +00001930
1931 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1932 Tmp1 = SelectExpr(N.getOperand(0));
1933 Tmp2 = SelectExpr(N.getOperand(1));
1934 } else {
1935 Tmp2 = SelectExpr(N.getOperand(1));
1936 Tmp1 = SelectExpr(N.getOperand(0));
1937 }
1938
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001939 switch (N.getValueType()) {
1940 default: assert(0 && "Cannot shift this type!");
1941 case MVT::i8 : Opc = X86::SAR8rCL; break;
1942 case MVT::i16: Opc = X86::SAR16rCL; break;
1943 case MVT::i32: Opc = X86::SAR32rCL; break;
1944 }
1945 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1946 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1947 return Result;
1948
1949 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001950 EmitCMP(N.getOperand(0), N.getOperand(1));
1951 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1952 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1953 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00001954 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001955 // Make sure we generate both values.
1956 if (Result != 1)
1957 ExprMap[N.getValue(1)] = 1; // Generate the token
1958 else
1959 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1960
Chris Lattner5188ad72005-01-08 19:28:19 +00001961 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001962 default: assert(0 && "Cannot load this type!");
1963 case MVT::i1:
1964 case MVT::i8: Opc = X86::MOV8rm; break;
1965 case MVT::i16: Opc = X86::MOV16rm; break;
1966 case MVT::i32: Opc = X86::MOV32rm; break;
1967 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1968 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1969 }
Chris Lattner11333092005-01-11 03:11:44 +00001970
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001971 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001972 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001973 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1974 } else {
1975 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001976
1977 SDOperand Chain = N.getOperand(0);
1978 SDOperand Address = N.getOperand(1);
1979 if (getRegPressure(Chain) > getRegPressure(Address)) {
1980 Select(Chain);
1981 SelectAddress(Address, AM);
1982 } else {
1983 SelectAddress(Address, AM);
1984 Select(Chain);
1985 }
1986
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001987 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1988 }
1989 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00001990
1991 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
1992 case ISD::ZEXTLOAD: {
1993 // Make sure we generate both values.
1994 if (Result != 1)
1995 ExprMap[N.getValue(1)] = 1; // Generate the token
1996 else
1997 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1998
Chris Lattnerda2ce112005-01-16 07:34:08 +00001999 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2000 if (Node->getValueType(0) == MVT::f64) {
2001 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2002 "Bad EXTLOAD!");
2003 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2004 CP->getIndex());
2005 return Result;
2006 }
2007
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002008 X86AddressMode AM;
2009 if (getRegPressure(Node->getOperand(0)) >
2010 getRegPressure(Node->getOperand(1))) {
2011 Select(Node->getOperand(0)); // chain
2012 SelectAddress(Node->getOperand(1), AM);
2013 } else {
2014 SelectAddress(Node->getOperand(1), AM);
2015 Select(Node->getOperand(0)); // chain
2016 }
2017
2018 switch (Node->getValueType(0)) {
2019 default: assert(0 && "Unknown type to sign extend to.");
2020 case MVT::f64:
2021 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2022 "Bad EXTLOAD!");
2023 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2024 break;
2025 case MVT::i32:
2026 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2027 default:
2028 assert(0 && "Bad zero extend!");
2029 case MVT::i1:
2030 case MVT::i8:
2031 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2032 break;
2033 case MVT::i16:
2034 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2035 break;
2036 }
2037 break;
2038 case MVT::i16:
2039 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2040 "Bad zero extend!");
2041 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2042 break;
2043 case MVT::i8:
2044 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2045 "Bad zero extend!");
2046 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2047 break;
2048 }
2049 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002050 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002051 case ISD::SEXTLOAD: {
2052 // Make sure we generate both values.
2053 if (Result != 1)
2054 ExprMap[N.getValue(1)] = 1; // Generate the token
2055 else
2056 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2057
2058 X86AddressMode AM;
2059 if (getRegPressure(Node->getOperand(0)) >
2060 getRegPressure(Node->getOperand(1))) {
2061 Select(Node->getOperand(0)); // chain
2062 SelectAddress(Node->getOperand(1), AM);
2063 } else {
2064 SelectAddress(Node->getOperand(1), AM);
2065 Select(Node->getOperand(0)); // chain
2066 }
2067
2068 switch (Node->getValueType(0)) {
2069 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2070 default: assert(0 && "Unknown type to sign extend to.");
2071 case MVT::i32:
2072 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2073 default:
2074 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2075 case MVT::i8:
2076 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2077 break;
2078 case MVT::i16:
2079 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2080 break;
2081 }
2082 break;
2083 case MVT::i16:
2084 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2085 "Cannot sign extend from bool!");
2086 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2087 break;
2088 }
2089 return Result;
2090 }
2091
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002092 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002093 // Generate both result values.
2094 if (Result != 1)
2095 ExprMap[N.getValue(1)] = 1; // Generate the token
2096 else
2097 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2098
2099 // FIXME: We are currently ignoring the requested alignment for handling
2100 // greater than the stack alignment. This will need to be revisited at some
2101 // point. Align = N.getOperand(2);
2102
2103 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2104 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2105 std::cerr << "Cannot allocate stack object with greater alignment than"
2106 << " the stack alignment yet!";
2107 abort();
2108 }
2109
2110 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002111 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002112 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2113 .addImm(CN->getValue());
2114 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002115 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2116 Select(N.getOperand(0));
2117 Tmp1 = SelectExpr(N.getOperand(1));
2118 } else {
2119 Tmp1 = SelectExpr(N.getOperand(1));
2120 Select(N.getOperand(0));
2121 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002122
2123 // Subtract size from stack pointer, thereby allocating some space.
2124 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2125 }
2126
2127 // Put a pointer to the space into the result register, by copying the stack
2128 // pointer.
2129 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2130 return Result;
2131
2132 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002133 // The chain for this call is now lowered.
2134 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
2135
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002136 if (GlobalAddressSDNode *GASD =
2137 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002138 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002139 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2140 } else if (ExternalSymbolSDNode *ESSDN =
2141 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002142 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002143 BuildMI(BB, X86::CALLpcrel32,
2144 1).addExternalSymbol(ESSDN->getSymbol(), true);
2145 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002146 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2147 Select(N.getOperand(0));
2148 Tmp1 = SelectExpr(N.getOperand(1));
2149 } else {
2150 Tmp1 = SelectExpr(N.getOperand(1));
2151 Select(N.getOperand(0));
2152 }
2153
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002154 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2155 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002156 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002157 default: assert(0 && "Unknown value type for call result!");
2158 case MVT::Other: return 1;
2159 case MVT::i1:
2160 case MVT::i8:
2161 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2162 break;
2163 case MVT::i16:
2164 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2165 break;
2166 case MVT::i32:
2167 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002168 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002169 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2170 break;
2171 case MVT::f32:
2172 case MVT::f64: // Floating-point return values live in %ST(0)
2173 ContainsFPCode = true;
2174 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2175 break;
2176 }
2177 return Result+N.ResNo;
2178 }
2179
2180 return 0;
2181}
2182
2183void ISel::Select(SDOperand N) {
2184 unsigned Tmp1, Tmp2, Opc;
2185
2186 // FIXME: Disable for our current expansion model!
2187 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
2188 return; // Already selected.
2189
Chris Lattner989de032005-01-11 06:14:36 +00002190 SDNode *Node = N.Val;
2191
2192 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002193 default:
Chris Lattner989de032005-01-11 06:14:36 +00002194 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002195 assert(0 && "Node not handled yet!");
2196 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002197 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002198 if (Node->getNumOperands() == 2) {
2199 bool OneFirst =
2200 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2201 Select(Node->getOperand(OneFirst));
2202 Select(Node->getOperand(!OneFirst));
2203 } else {
2204 std::vector<std::pair<unsigned, unsigned> > OpsP;
2205 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2206 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2207 std::sort(OpsP.begin(), OpsP.end());
2208 std::reverse(OpsP.begin(), OpsP.end());
2209 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2210 Select(Node->getOperand(OpsP[i].second));
2211 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002212 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002213 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002214 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2215 Select(N.getOperand(0));
2216 Tmp1 = SelectExpr(N.getOperand(1));
2217 } else {
2218 Tmp1 = SelectExpr(N.getOperand(1));
2219 Select(N.getOperand(0));
2220 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002221 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002222
2223 if (Tmp1 != Tmp2) {
2224 switch (N.getOperand(1).getValueType()) {
2225 default: assert(0 && "Invalid type for operation!");
2226 case MVT::i1:
2227 case MVT::i8: Opc = X86::MOV8rr; break;
2228 case MVT::i16: Opc = X86::MOV16rr; break;
2229 case MVT::i32: Opc = X86::MOV32rr; break;
2230 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002231 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002232 }
2233 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2234 }
2235 return;
2236 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002237 switch (N.getNumOperands()) {
2238 default:
2239 assert(0 && "Unknown return instruction!");
2240 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002241 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2242 N.getOperand(2).getValueType() == MVT::i32 &&
2243 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002244 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2245 Tmp1 = SelectExpr(N.getOperand(1));
2246 Tmp2 = SelectExpr(N.getOperand(2));
2247 } else {
2248 Tmp2 = SelectExpr(N.getOperand(2));
2249 Tmp1 = SelectExpr(N.getOperand(1));
2250 }
2251 Select(N.getOperand(0));
2252
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002253 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2254 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2255 // Declare that EAX & EDX are live on exit.
2256 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2257 .addReg(X86::ESP);
2258 break;
2259 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002260 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2261 Select(N.getOperand(0));
2262 Tmp1 = SelectExpr(N.getOperand(1));
2263 } else {
2264 Tmp1 = SelectExpr(N.getOperand(1));
2265 Select(N.getOperand(0));
2266 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002267 switch (N.getOperand(1).getValueType()) {
2268 default: assert(0 && "All other types should have been promoted!!");
2269 case MVT::f64:
2270 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2271 // Declare that top-of-stack is live on exit
2272 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2273 break;
2274 case MVT::i32:
2275 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2276 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2277 break;
2278 }
2279 break;
2280 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002281 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002282 break;
2283 }
2284 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2285 return;
2286 case ISD::BR: {
2287 Select(N.getOperand(0));
2288 MachineBasicBlock *Dest =
2289 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2290 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2291 return;
2292 }
2293
2294 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002295 MachineBasicBlock *Dest =
2296 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002297
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002298 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2299 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002300 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2301 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2302 Select(N.getOperand(0));
2303 Tmp1 = SelectExpr(N.getOperand(1));
2304 } else {
2305 Tmp1 = SelectExpr(N.getOperand(1));
2306 Select(N.getOperand(0));
2307 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002308 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2309 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2310 }
Chris Lattner11333092005-01-11 03:11:44 +00002311
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002312 return;
2313 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002314
Chris Lattner4df0de92005-01-17 00:00:33 +00002315 case ISD::LOAD:
2316 // If this load could be folded into the only using instruction, and if it
2317 // is safe to emit the instruction here, try to do so now.
2318 if (Node->hasNUsesOfValue(1, 0)) {
2319 SDOperand TheVal = N.getValue(0);
2320 SDNode *User = 0;
2321 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
2322 assert(UI != Node->use_end() && "Didn't find use!");
2323 SDNode *UN = *UI;
2324 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
2325 if (UN->getOperand(i) == TheVal) {
2326 User = UN;
2327 goto FoundIt;
2328 }
2329 }
2330 FoundIt:
2331 // Only handle unary operators right now.
2332 if (User->getNumOperands() == 1) {
2333 LoweredTokens.erase(N);
2334 SelectExpr(SDOperand(User, 0));
2335 return;
2336 }
2337 }
2338 SelectExpr(N);
2339 return;
2340
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002341 case ISD::EXTLOAD:
2342 case ISD::SEXTLOAD:
2343 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002344 case ISD::CALL:
2345 case ISD::DYNAMIC_STACKALLOC:
2346 SelectExpr(N);
2347 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002348
2349 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2350 // On X86, we can represent all types except for Bool and Float natively.
2351 X86AddressMode AM;
2352 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002353 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2354 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2355 && "Unsupported TRUNCSTORE for this target!");
2356
2357 if (StoredTy == MVT::i16) {
2358 // FIXME: This is here just to allow testing. X86 doesn't really have a
2359 // TRUNCSTORE i16 operation, but this is required for targets that do not
2360 // have 16-bit integer registers. We occasionally disable 16-bit integer
2361 // registers to test the promotion code.
2362 Select(N.getOperand(0));
2363 Tmp1 = SelectExpr(N.getOperand(1));
2364 SelectAddress(N.getOperand(2), AM);
2365
2366 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2367 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2368 return;
2369 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002370
2371 // Store of constant bool?
2372 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2373 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2374 Select(N.getOperand(0));
2375 SelectAddress(N.getOperand(2), AM);
2376 } else {
2377 SelectAddress(N.getOperand(2), AM);
2378 Select(N.getOperand(0));
2379 }
2380 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2381 return;
2382 }
2383
2384 switch (StoredTy) {
2385 default: assert(0 && "Cannot truncstore this type!");
2386 case MVT::i1: Opc = X86::MOV8mr; break;
2387 case MVT::f32: Opc = X86::FST32m; break;
2388 }
2389
2390 std::vector<std::pair<unsigned, unsigned> > RP;
2391 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2392 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2393 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2394 std::sort(RP.begin(), RP.end());
2395
2396 for (unsigned i = 0; i != 3; ++i)
2397 switch (RP[2-i].second) {
2398 default: assert(0 && "Unknown operand number!");
2399 case 0: Select(N.getOperand(0)); break;
2400 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2401 case 2: SelectAddress(N.getOperand(2), AM); break;
2402 }
2403
2404 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2405 return;
2406 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002407 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002408 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002409
2410 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2411 Opc = 0;
2412 switch (CN->getValueType(0)) {
2413 default: assert(0 && "Invalid type for operation!");
2414 case MVT::i1:
2415 case MVT::i8: Opc = X86::MOV8mi; break;
2416 case MVT::i16: Opc = X86::MOV16mi; break;
2417 case MVT::i32: Opc = X86::MOV32mi; break;
2418 case MVT::f32:
2419 case MVT::f64: break;
2420 }
2421 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002422 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2423 Select(N.getOperand(0));
2424 SelectAddress(N.getOperand(2), AM);
2425 } else {
2426 SelectAddress(N.getOperand(2), AM);
2427 Select(N.getOperand(0));
2428 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002429 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2430 return;
2431 }
2432 }
Chris Lattner837caa72005-01-11 23:21:30 +00002433
2434 // Check to see if this is a load/op/store combination.
2435 if (N.getOperand(1).Val->hasOneUse() &&
Chris Lattner42928302005-01-12 03:16:09 +00002436 isFoldableLoad(N.getOperand(0).getValue(0)) &&
2437 !MVT::isFloatingPoint(N.getOperand(0).getValue(0).getValueType())) {
Chris Lattner837caa72005-01-11 23:21:30 +00002438 SDOperand TheLoad = N.getOperand(0).getValue(0);
2439 // Check to see if we are loading the same pointer that we're storing to.
2440 if (TheLoad.getOperand(1) == N.getOperand(2)) {
2441 // See if the stored value is a simple binary operator that uses the
2442 // load as one of its operands.
2443 SDOperand Op = N.getOperand(1);
2444 if (Op.Val->getNumOperands() == 2 &&
2445 (Op.getOperand(0) == TheLoad || Op.getOperand(1) == TheLoad)) {
2446 // Finally, check to see if this is one of the ops we can handle!
2447 static const unsigned ADDTAB[] = {
2448 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002449 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
Chris Lattner837caa72005-01-11 23:21:30 +00002450 };
Chris Lattner7ea64f52005-01-12 01:28:00 +00002451 static const unsigned SUBTAB[] = {
2452 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002453 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002454 };
2455 static const unsigned ANDTAB[] = {
2456 X86::AND8mi, X86::AND16mi, X86::AND32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002457 X86::AND8mr, X86::AND16mr, X86::AND32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002458 };
2459 static const unsigned ORTAB[] = {
2460 X86::OR8mi, X86::OR16mi, X86::OR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002461 X86::OR8mr, X86::OR16mr, X86::OR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002462 };
2463 static const unsigned XORTAB[] = {
2464 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002465 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002466 };
2467 static const unsigned SHLTAB[] = {
2468 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002469 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002470 };
2471 static const unsigned SARTAB[] = {
2472 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002473 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002474 };
2475 static const unsigned SHRTAB[] = {
2476 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002477 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002478 };
Chris Lattner837caa72005-01-11 23:21:30 +00002479
2480 const unsigned *TabPtr = 0;
2481 switch (Op.getOpcode()) {
Chris Lattner7ea64f52005-01-12 01:28:00 +00002482 default: std::cerr << "CANNOT [mem] op= val: "; Op.Val->dump(); std::cerr << "\n"; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002483 case ISD::ADD: TabPtr = ADDTAB; break;
Chris Lattner7ea64f52005-01-12 01:28:00 +00002484 case ISD::SUB: TabPtr = SUBTAB; break;
2485 case ISD::AND: TabPtr = ANDTAB; break;
2486 case ISD:: OR: TabPtr = ORTAB; break;
2487 case ISD::XOR: TabPtr = XORTAB; break;
2488 case ISD::SHL: TabPtr = SHLTAB; break;
2489 case ISD::SRA: TabPtr = SARTAB; break;
2490 case ISD::SRL: TabPtr = SHRTAB; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002491 }
2492
2493 if (TabPtr) {
2494 // Handle: [mem] op= CST
2495 SDOperand Op0 = Op.getOperand(0);
2496 SDOperand Op1 = Op.getOperand(1);
2497 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner48034fd2005-01-12 05:22:07 +00002498 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
Chris Lattner837caa72005-01-11 23:21:30 +00002499 default: break;
2500 case MVT::i1:
2501 case MVT::i8: Opc = TabPtr[0]; break;
2502 case MVT::i16: Opc = TabPtr[1]; break;
2503 case MVT::i32: Opc = TabPtr[2]; break;
2504 }
2505
2506 if (Opc) {
2507 if (getRegPressure(TheLoad.getOperand(0)) >
2508 getRegPressure(TheLoad.getOperand(1))) {
2509 Select(TheLoad.getOperand(0));
2510 SelectAddress(TheLoad.getOperand(1), AM);
2511 } else {
2512 SelectAddress(TheLoad.getOperand(1), AM);
2513 Select(TheLoad.getOperand(0));
2514 }
2515
2516 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2517 return;
2518 }
2519 }
2520
2521 // If we have [mem] = V op [mem], try to turn it into:
2522 // [mem] = [mem] op V.
Chris Lattner7ea64f52005-01-12 01:28:00 +00002523 if (Op1 == TheLoad && Op.getOpcode() != ISD::SUB &&
2524 Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRA &&
2525 Op.getOpcode() != ISD::SRL)
Chris Lattner837caa72005-01-11 23:21:30 +00002526 std::swap(Op0, Op1);
2527
2528 if (Op0 == TheLoad) {
2529 switch (Op0.getValueType()) {
2530 default: break;
2531 case MVT::i1:
2532 case MVT::i8: Opc = TabPtr[3]; break;
2533 case MVT::i16: Opc = TabPtr[4]; break;
2534 case MVT::i32: Opc = TabPtr[5]; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002535 }
2536
2537 if (Opc) {
2538 Select(TheLoad.getOperand(0));
2539 SelectAddress(TheLoad.getOperand(1), AM);
2540 unsigned Reg = SelectExpr(Op1);
2541 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addReg(Reg);
2542 return;
2543 }
2544 }
Chris Lattner837caa72005-01-11 23:21:30 +00002545 }
2546 }
2547 }
2548 }
2549
2550
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002551 switch (N.getOperand(1).getValueType()) {
2552 default: assert(0 && "Cannot store this type!");
2553 case MVT::i1:
2554 case MVT::i8: Opc = X86::MOV8mr; break;
2555 case MVT::i16: Opc = X86::MOV16mr; break;
2556 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002557 case MVT::f32: Opc = X86::FST32m; break;
2558 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002559 }
Chris Lattner11333092005-01-11 03:11:44 +00002560
2561 std::vector<std::pair<unsigned, unsigned> > RP;
2562 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2563 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2564 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2565 std::sort(RP.begin(), RP.end());
2566
2567 for (unsigned i = 0; i != 3; ++i)
2568 switch (RP[2-i].second) {
2569 default: assert(0 && "Unknown operand number!");
2570 case 0: Select(N.getOperand(0)); break;
2571 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002572 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002573 }
2574
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002575 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2576 return;
2577 }
2578 case ISD::ADJCALLSTACKDOWN:
2579 case ISD::ADJCALLSTACKUP:
2580 Select(N.getOperand(0));
2581 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2582
2583 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2584 X86::ADJCALLSTACKUP;
2585 BuildMI(BB, Opc, 1).addImm(Tmp1);
2586 return;
Chris Lattner989de032005-01-11 06:14:36 +00002587 case ISD::MEMSET: {
2588 Select(N.getOperand(0)); // Select the chain.
2589 unsigned Align =
2590 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2591 if (Align == 0) Align = 1;
2592
2593 // Turn the byte code into # iterations
2594 unsigned CountReg;
2595 unsigned Opcode;
2596 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2597 unsigned Val = ValC->getValue() & 255;
2598
2599 // If the value is a constant, then we can potentially use larger sets.
2600 switch (Align & 3) {
2601 case 2: // WORD aligned
2602 CountReg = MakeReg(MVT::i32);
2603 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2604 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2605 } else {
2606 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2607 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2608 }
2609 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2610 Opcode = X86::REP_STOSW;
2611 break;
2612 case 0: // DWORD aligned
2613 CountReg = MakeReg(MVT::i32);
2614 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2615 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2616 } else {
2617 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2618 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2619 }
2620 Val = (Val << 8) | Val;
2621 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2622 Opcode = X86::REP_STOSD;
2623 break;
2624 default: // BYTE aligned
2625 CountReg = SelectExpr(Node->getOperand(3));
2626 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2627 Opcode = X86::REP_STOSB;
2628 break;
2629 }
2630 } else {
2631 // If it's not a constant value we are storing, just fall back. We could
2632 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2633 unsigned ValReg = SelectExpr(Node->getOperand(2));
2634 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2635 CountReg = SelectExpr(Node->getOperand(3));
2636 Opcode = X86::REP_STOSB;
2637 }
2638
2639 // No matter what the alignment is, we put the source in ESI, the
2640 // destination in EDI, and the count in ECX.
2641 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2642 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2643 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2644 BuildMI(BB, Opcode, 0);
2645 return;
2646 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002647 case ISD::MEMCPY:
2648 Select(N.getOperand(0)); // Select the chain.
2649 unsigned Align =
2650 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2651 if (Align == 0) Align = 1;
2652
2653 // Turn the byte code into # iterations
2654 unsigned CountReg;
2655 unsigned Opcode;
2656 switch (Align & 3) {
2657 case 2: // WORD aligned
2658 CountReg = MakeReg(MVT::i32);
2659 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2660 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2661 } else {
2662 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2663 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2664 }
2665 Opcode = X86::REP_MOVSW;
2666 break;
2667 case 0: // DWORD aligned
2668 CountReg = MakeReg(MVT::i32);
2669 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2670 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2671 } else {
2672 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2673 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2674 }
2675 Opcode = X86::REP_MOVSD;
2676 break;
2677 default: // BYTE aligned
2678 CountReg = SelectExpr(Node->getOperand(3));
2679 Opcode = X86::REP_MOVSB;
2680 break;
2681 }
2682
2683 // No matter what the alignment is, we put the source in ESI, the
2684 // destination in EDI, and the count in ECX.
2685 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2686 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2687 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2688 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2689 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2690 BuildMI(BB, Opcode, 0);
2691 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002692 }
2693 assert(0 && "Should not be reached!");
2694}
2695
2696
2697/// createX86PatternInstructionSelector - This pass converts an LLVM function
2698/// into a machine code representation using pattern matching and a machine
2699/// description file.
2700///
2701FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2702 return new ISel(TM);
2703}