blob: ba359de12e17c1f163538b376b01c6d2ce7151bd [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000030#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// X86TargetLowering - X86 Implementation of the TargetLowering interface
35namespace {
36 class X86TargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000038 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000039 public:
40 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
42 addRegisterClass(MVT::i8, X86::R8RegisterClass);
43 addRegisterClass(MVT::i16, X86::R16RegisterClass);
44 addRegisterClass(MVT::i32, X86::R32RegisterClass);
45 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
46
47 // FIXME: Eliminate these two classes when legalize can handle promotions
48 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000049/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
50/**/ //addRegisterClass(MVT::f32, X86::RFPRegisterClass);
51
52 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
53 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
54 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i16 , Expand);
55 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
56 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i1 , Expand);
57 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
58 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
59 setOperationAction(ISD::SREM , MVT::f64 , Expand);
60
61 // These should be promoted to a larger select which is supported.
62/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
63 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000064
65 computeRegisterProperties();
Chris Lattner8acb1ba2005-01-07 07:49:41 +000066
67 addLegalFPImmediate(+0.0); // FLD0
68 addLegalFPImmediate(+1.0); // FLD1
69 addLegalFPImmediate(-0.0); // FLD0/FCHS
70 addLegalFPImmediate(-1.0); // FLD1/FCHS
71 }
72
73 /// LowerArguments - This hook must be implemented to indicate how we should
74 /// lower the arguments for the specified function, into the specified DAG.
75 virtual std::vector<SDOperand>
76 LowerArguments(Function &F, SelectionDAG &DAG);
77
78 /// LowerCallTo - This hook lowers an abstract call to a function into an
79 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000080 virtual std::pair<SDOperand, SDOperand>
81 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
82 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000083
84 virtual std::pair<SDOperand, SDOperand>
85 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
86
87 virtual std::pair<SDOperand,SDOperand>
88 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
89 const Type *ArgTy, SelectionDAG &DAG);
90
91 virtual std::pair<SDOperand, SDOperand>
92 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
93 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000094 };
95}
96
97
98std::vector<SDOperand>
99X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
100 std::vector<SDOperand> ArgValues;
101
102 // Add DAG nodes to load the arguments... On entry to a function on the X86,
103 // the stack frame looks like this:
104 //
105 // [ESP] -- return address
106 // [ESP + 4] -- first argument (leftmost lexically)
107 // [ESP + 8] -- second argument, if first argument is four bytes in size
108 // ...
109 //
110 MachineFunction &MF = DAG.getMachineFunction();
111 MachineFrameInfo *MFI = MF.getFrameInfo();
112
113 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
114 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
115 MVT::ValueType ObjectVT = getValueType(I->getType());
116 unsigned ArgIncrement = 4;
117 unsigned ObjSize;
118 switch (ObjectVT) {
119 default: assert(0 && "Unhandled argument type!");
120 case MVT::i1:
121 case MVT::i8: ObjSize = 1; break;
122 case MVT::i16: ObjSize = 2; break;
123 case MVT::i32: ObjSize = 4; break;
124 case MVT::i64: ObjSize = ArgIncrement = 8; break;
125 case MVT::f32: ObjSize = 4; break;
126 case MVT::f64: ObjSize = ArgIncrement = 8; break;
127 }
128 // Create the frame index object for this incoming parameter...
129 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
130
131 // Create the SelectionDAG nodes corresponding to a load from this parameter
132 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
133
134 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
135 // dead loads.
136 SDOperand ArgValue;
137 if (!I->use_empty())
138 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
139 else {
140 if (MVT::isInteger(ObjectVT))
141 ArgValue = DAG.getConstant(0, ObjectVT);
142 else
143 ArgValue = DAG.getConstantFP(0, ObjectVT);
144 }
145 ArgValues.push_back(ArgValue);
146
147 ArgOffset += ArgIncrement; // Move on to the next argument...
148 }
149
150 // If the function takes variable number of arguments, make a frame index for
151 // the start of the first vararg value... for expansion of llvm.va_start.
152 if (F.isVarArg())
153 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000154 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000155 return ArgValues;
156}
157
Chris Lattner5188ad72005-01-08 19:28:19 +0000158std::pair<SDOperand, SDOperand>
159X86TargetLowering::LowerCallTo(SDOperand Chain,
160 const Type *RetTy, SDOperand Callee,
161 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000162 // Count how many bytes are to be pushed on the stack.
163 unsigned NumBytes = 0;
164
165 if (Args.empty()) {
166 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000167 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
168 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000169 } else {
170 for (unsigned i = 0, e = Args.size(); i != e; ++i)
171 switch (getValueType(Args[i].second)) {
172 default: assert(0 && "Unknown value type!");
173 case MVT::i1:
174 case MVT::i8:
175 case MVT::i16:
176 case MVT::i32:
177 case MVT::f32:
178 NumBytes += 4;
179 break;
180 case MVT::i64:
181 case MVT::f64:
182 NumBytes += 8;
183 break;
184 }
185
Chris Lattner5188ad72005-01-08 19:28:19 +0000186 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
187 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000188
189 // Arguments go on the stack in reverse order, as specified by the ABI.
190 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000191 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
192 DAG.getEntryNode());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000193 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
194 unsigned ArgReg;
195 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
196 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
197
198 switch (getValueType(Args[i].second)) {
199 default: assert(0 && "Unexpected ValueType for argument!");
200 case MVT::i1:
201 case MVT::i8:
202 case MVT::i16:
203 // Promote the integer to 32 bits. If the input type is signed use a
204 // sign extend, otherwise use a zero extend.
205 if (Args[i].second->isSigned())
206 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
207 else
208 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
209
210 // FALL THROUGH
211 case MVT::i32:
212 case MVT::f32:
213 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000214 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
215 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000216 ArgOffset += 4;
217 break;
218 case MVT::i64:
219 case MVT::f64:
220 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000221 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
222 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000223 ArgOffset += 8;
224 break;
225 }
226 }
227 }
228
229 std::vector<MVT::ValueType> RetVals;
230 MVT::ValueType RetTyVT = getValueType(RetTy);
231 if (RetTyVT != MVT::isVoid)
232 RetVals.push_back(RetTyVT);
233 RetVals.push_back(MVT::Other);
234
Chris Lattner5188ad72005-01-08 19:28:19 +0000235 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000236 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000237 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
238 DAG.getConstant(NumBytes, getPointerTy()));
239 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000240}
241
Chris Lattner14824582005-01-09 00:01:27 +0000242std::pair<SDOperand, SDOperand>
243X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
244 // vastart just returns the address of the VarArgsFrameIndex slot.
245 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
246}
247
248std::pair<SDOperand,SDOperand> X86TargetLowering::
249LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
250 const Type *ArgTy, SelectionDAG &DAG) {
251 MVT::ValueType ArgVT = getValueType(ArgTy);
252 SDOperand Result;
253 if (!isVANext) {
254 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
255 } else {
256 unsigned Amt;
257 if (ArgVT == MVT::i32)
258 Amt = 4;
259 else {
260 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
261 "Other types should have been promoted for varargs!");
262 Amt = 8;
263 }
264 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
265 DAG.getConstant(Amt, VAList.getValueType()));
266 }
267 return std::make_pair(Result, Chain);
268}
269
270
271std::pair<SDOperand, SDOperand> X86TargetLowering::
272LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
273 SelectionDAG &DAG) {
274 SDOperand Result;
275 if (Depth) // Depths > 0 not supported yet!
276 Result = DAG.getConstant(0, getPointerTy());
277 else {
278 if (ReturnAddrIndex == 0) {
279 // Set up a frame object for the return address.
280 MachineFunction &MF = DAG.getMachineFunction();
281 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
282 }
283
284 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
285
286 if (!isFrameAddress)
287 // Just load the return address
288 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
289 else
290 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
291 DAG.getConstant(4, MVT::i32));
292 }
293 return std::make_pair(Result, Chain);
294}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000295
296
297
298
299
300namespace {
301 Statistic<>
302 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
303
304 //===--------------------------------------------------------------------===//
305 /// ISel - X86 specific code to select X86 machine instructions for
306 /// SelectionDAG operations.
307 ///
308 class ISel : public SelectionDAGISel {
309 /// ContainsFPCode - Every instruction we select that uses or defines a FP
310 /// register should set this to true.
311 bool ContainsFPCode;
312
313 /// X86Lowering - This object fully describes how to lower LLVM code to an
314 /// X86-specific SelectionDAG.
315 X86TargetLowering X86Lowering;
316
Chris Lattner11333092005-01-11 03:11:44 +0000317 /// RegPressureMap - This keeps an approximate count of the number of
318 /// registers required to evaluate each node in the graph.
319 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000320
321 /// ExprMap - As shared expressions are codegen'd, we keep track of which
322 /// vreg the value is produced in, so we only emit one copy of each compiled
323 /// tree.
324 std::map<SDOperand, unsigned> ExprMap;
325 std::set<SDOperand> LoweredTokens;
326
327 public:
328 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
329 }
330
Chris Lattner11333092005-01-11 03:11:44 +0000331 unsigned getRegPressure(SDOperand O) {
332 return RegPressureMap[O.Val];
333 }
334 unsigned ComputeRegPressure(SDOperand O);
335
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000336 /// InstructionSelectBasicBlock - This callback is invoked by
337 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000338 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000339
Chris Lattnera5ade062005-01-11 21:19:59 +0000340 bool isFoldableLoad(SDOperand Op);
341 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
342
343
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000344 void EmitCMP(SDOperand LHS, SDOperand RHS);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000345 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000346 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
347 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000348 unsigned SelectExpr(SDOperand N);
349 bool SelectAddress(SDOperand N, X86AddressMode &AM);
350 void Select(SDOperand N);
351 };
352}
353
Chris Lattner7dbcb752005-01-12 04:21:28 +0000354/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
355/// when it has created a SelectionDAG for us to codegen.
356void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
357 // While we're doing this, keep track of whether we see any FP code for
358 // FP_REG_KILL insertion.
359 ContainsFPCode = false;
360
361 // Scan the PHI nodes that already are inserted into this basic block. If any
362 // of them is a PHI of a floating point value, we need to insert an
363 // FP_REG_KILL.
364 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
365 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
366 I != E; ++I) {
367 assert(I->getOpcode() == X86::PHI &&
368 "Isn't just PHI nodes?");
369 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
370 X86::RFPRegisterClass) {
371 ContainsFPCode = true;
372 break;
373 }
374 }
375
376 // Compute the RegPressureMap, which is an approximation for the number of
377 // registers required to compute each node.
378 ComputeRegPressure(DAG.getRoot());
379
380 // Codegen the basic block.
381 Select(DAG.getRoot());
382
383 // Finally, look at all of the successors of this block. If any contain a PHI
384 // node of FP type, we need to insert an FP_REG_KILL in this block.
385 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
386 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
387 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
388 I != E && I->getOpcode() == X86::PHI; ++I) {
389 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
390 X86::RFPRegisterClass) {
391 ContainsFPCode = true;
392 break;
393 }
394 }
395
396 // Insert FP_REG_KILL instructions into basic blocks that need them. This
397 // only occurs due to the floating point stackifier not being aggressive
398 // enough to handle arbitrary global stackification.
399 //
400 // Currently we insert an FP_REG_KILL instruction into each block that uses or
401 // defines a floating point virtual register.
402 //
403 // When the global register allocators (like linear scan) finally update live
404 // variable analysis, we can keep floating point values in registers across
405 // basic blocks. This will be a huge win, but we are waiting on the global
406 // allocators before we can do this.
407 //
408 if (ContainsFPCode && BB->succ_size()) {
409 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
410 ++NumFPKill;
411 }
412
413 // Clear state used for selection.
414 ExprMap.clear();
415 LoweredTokens.clear();
416 RegPressureMap.clear();
417}
418
419
Chris Lattner11333092005-01-11 03:11:44 +0000420// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
421// for the number of registers required to compute each node. This is basically
422// computing a generalized form of the Sethi-Ullman number for each node.
423unsigned ISel::ComputeRegPressure(SDOperand O) {
424 SDNode *N = O.Val;
425 unsigned &Result = RegPressureMap[N];
426 if (Result) return Result;
427
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000428 // FIXME: Should operations like CALL (which clobber lots o regs) have a
429 // higher fixed cost??
430
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000431 if (N->getNumOperands() == 0) {
432 Result = 1;
433 } else {
434 unsigned MaxRegUse = 0;
435 unsigned NumExtraMaxRegUsers = 0;
436 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
437 unsigned Regs;
438 if (N->getOperand(i).getOpcode() == ISD::Constant)
439 Regs = 0;
440 else
441 Regs = ComputeRegPressure(N->getOperand(i));
442 if (Regs > MaxRegUse) {
443 MaxRegUse = Regs;
444 NumExtraMaxRegUsers = 0;
445 } else if (Regs == MaxRegUse &&
446 N->getOperand(i).getValueType() != MVT::Other) {
447 ++NumExtraMaxRegUsers;
448 }
Chris Lattner11333092005-01-11 03:11:44 +0000449 }
Chris Lattner11333092005-01-11 03:11:44 +0000450
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000451 Result = MaxRegUse+NumExtraMaxRegUsers;
452 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000453
Chris Lattner837caa72005-01-11 23:21:30 +0000454 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000455 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000456}
457
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000458/// SelectAddress - Add the specified node to the specified addressing mode,
459/// returning true if it cannot be done.
460bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
461 switch (N.getOpcode()) {
462 default: break;
463 case ISD::FrameIndex:
464 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
465 AM.BaseType = X86AddressMode::FrameIndexBase;
466 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
467 return false;
468 }
469 break;
470 case ISD::GlobalAddress:
471 if (AM.GV == 0) {
472 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
473 return false;
474 }
475 break;
476 case ISD::Constant:
477 AM.Disp += cast<ConstantSDNode>(N)->getValue();
478 return false;
479 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000480 // We might have folded the load into this shift, so don't regen the value
481 // if so.
482 if (ExprMap.count(N)) break;
483
Chris Lattner2b937862005-01-12 07:33:20 +0000484 if (AM.IndexReg == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000485 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
486 unsigned Val = CN->getValue();
487 if (Val == 1 || Val == 2 || Val == 3) {
488 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000489 SDOperand ShVal = N.Val->getOperand(0);
490
491 // Okay, we know that we have a scale by now. However, if the scaled
492 // value is an add of something and a constant, we can fold the
493 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000494 if (ShVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(ShVal) &&
Chris Lattner51a26342005-01-11 06:36:20 +0000495 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
496 AM.IndexReg = SelectExpr(ShVal.Val->getOperand(0));
497 ConstantSDNode *AddVal =
498 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
499 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000500 } else {
Chris Lattner51a26342005-01-11 06:36:20 +0000501 AM.IndexReg = SelectExpr(ShVal);
502 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000503 return false;
504 }
505 }
506 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000507 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000508 // We might have folded the load into this mul, so don't regen the value if
509 // so.
510 if (ExprMap.count(N)) break;
511
Chris Lattner947d5442005-01-11 19:37:02 +0000512 // X*[3,5,9] -> X+X*[2,4,8]
513 if (AM.IndexReg == 0 && AM.BaseType == X86AddressMode::RegBase &&
514 AM.Base.Reg == 0)
515 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
516 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
517 AM.Scale = unsigned(CN->getValue())-1;
518
519 SDOperand MulVal = N.Val->getOperand(0);
520 unsigned Reg;
521
522 // Okay, we know that we have a scale by now. However, if the scaled
523 // value is an add of something and a constant, we can fold the
524 // constant into the disp field here.
Chris Lattner636e79a2005-01-13 05:53:16 +0000525 if (MulVal.Val->getOpcode() == ISD::ADD && !ExprMap.count(MulVal) &&
Chris Lattner947d5442005-01-11 19:37:02 +0000526 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
527 Reg = SelectExpr(MulVal.Val->getOperand(0));
528 ConstantSDNode *AddVal =
529 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
530 AM.Disp += AddVal->getValue() * CN->getValue();
531 } else {
532 Reg = SelectExpr(N.Val->getOperand(0));
533 }
534
535 AM.IndexReg = AM.Base.Reg = Reg;
536 return false;
537 }
538 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000539
540 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000541 // We might have folded the load into this mul, so don't regen the value if
542 // so.
543 if (ExprMap.count(N)) break;
544
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000545 X86AddressMode Backup = AM;
546 if (!SelectAddress(N.Val->getOperand(0), AM) &&
547 !SelectAddress(N.Val->getOperand(1), AM))
548 return false;
549 AM = Backup;
Chris Lattner9bbd9922005-01-12 18:08:53 +0000550 if (!SelectAddress(N.Val->getOperand(1), AM) &&
551 !SelectAddress(N.Val->getOperand(0), AM))
552 return false;
553 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000554 break;
555 }
556 }
557
Chris Lattnera95589b2005-01-11 04:40:19 +0000558 // Is the base register already occupied?
559 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
560 // If so, check to see if the scale index register is set.
561 if (AM.IndexReg == 0) {
562 AM.IndexReg = SelectExpr(N);
563 AM.Scale = 1;
564 return false;
565 }
566
567 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000568 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000569 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000570
571 // Default, generate it as a register.
572 AM.BaseType = X86AddressMode::RegBase;
573 AM.Base.Reg = SelectExpr(N);
574 return false;
575}
576
577/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
578/// assuming that the temporary registers are in the 8-bit register class.
579///
580/// Tmp1 = setcc1
581/// Tmp2 = setcc2
582/// DestReg = logicalop Tmp1, Tmp2
583///
584static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
585 unsigned SetCC2, unsigned LogicalOp,
586 unsigned DestReg) {
587 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
588 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
589 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
590 BuildMI(BB, SetCC1, 0, Tmp1);
591 BuildMI(BB, SetCC2, 0, Tmp2);
592 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
593}
594
595/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
596/// condition codes match the specified SetCCOpcode. Note that some conditions
597/// require multiple instructions to generate the correct value.
598static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
599 ISD::CondCode SetCCOpcode, bool isFP) {
600 unsigned Opc;
601 if (!isFP) {
602 switch (SetCCOpcode) {
603 default: assert(0 && "Illegal integer SetCC!");
604 case ISD::SETEQ: Opc = X86::SETEr; break;
605 case ISD::SETGT: Opc = X86::SETGr; break;
606 case ISD::SETGE: Opc = X86::SETGEr; break;
607 case ISD::SETLT: Opc = X86::SETLr; break;
608 case ISD::SETLE: Opc = X86::SETLEr; break;
609 case ISD::SETNE: Opc = X86::SETNEr; break;
610 case ISD::SETULT: Opc = X86::SETBr; break;
611 case ISD::SETUGT: Opc = X86::SETAr; break;
612 case ISD::SETULE: Opc = X86::SETBEr; break;
613 case ISD::SETUGE: Opc = X86::SETAEr; break;
614 }
615 } else {
616 // On a floating point condition, the flags are set as follows:
617 // ZF PF CF op
618 // 0 | 0 | 0 | X > Y
619 // 0 | 0 | 1 | X < Y
620 // 1 | 0 | 0 | X == Y
621 // 1 | 1 | 1 | unordered
622 //
623 switch (SetCCOpcode) {
624 default: assert(0 && "Invalid FP setcc!");
625 case ISD::SETUEQ:
626 case ISD::SETEQ:
627 Opc = X86::SETEr; // True if ZF = 1
628 break;
629 case ISD::SETOGT:
630 case ISD::SETGT:
631 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
632 break;
633 case ISD::SETOGE:
634 case ISD::SETGE:
635 Opc = X86::SETAEr; // True if CF = 0
636 break;
637 case ISD::SETULT:
638 case ISD::SETLT:
639 Opc = X86::SETBr; // True if CF = 1
640 break;
641 case ISD::SETULE:
642 case ISD::SETLE:
643 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
644 break;
645 case ISD::SETONE:
646 case ISD::SETNE:
647 Opc = X86::SETNEr; // True if ZF = 0
648 break;
649 case ISD::SETUO:
650 Opc = X86::SETPr; // True if PF = 1
651 break;
652 case ISD::SETO:
653 Opc = X86::SETNPr; // True if PF = 0
654 break;
655 case ISD::SETOEQ: // !PF & ZF
656 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
657 return;
658 case ISD::SETOLT: // !PF & CF
659 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
660 return;
661 case ISD::SETOLE: // !PF & (CF || ZF)
662 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
663 return;
664 case ISD::SETUGT: // PF | (!ZF & !CF)
665 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
666 return;
667 case ISD::SETUGE: // PF | !CF
668 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
669 return;
670 case ISD::SETUNE: // PF | !ZF
671 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
672 return;
673 }
674 }
675 BuildMI(BB, Opc, 0, DestReg);
676}
677
678
679/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
680/// the Dest block if the Cond condition is true. If we cannot fold this
681/// condition into the branch, return true.
682///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000683bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
684 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000685 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
686 // B) using two conditional branches instead of one condbr, two setcc's, and
687 // an or.
688 if ((Cond.getOpcode() == ISD::OR ||
689 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
690 // And and or set the flags for us, so there is no need to emit a TST of the
691 // result. It is only safe to do this if there is only a single use of the
692 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000693 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000694 SelectExpr(Cond);
695 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
696 return false;
697 }
698
699 // Codegen br not C -> JE.
700 if (Cond.getOpcode() == ISD::XOR)
701 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
702 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000703 unsigned CondR;
704 if (getRegPressure(Chain) > getRegPressure(Cond)) {
705 Select(Chain);
706 CondR = SelectExpr(Cond.Val->getOperand(0));
707 } else {
708 CondR = SelectExpr(Cond.Val->getOperand(0));
709 Select(Chain);
710 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000711 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
712 BuildMI(BB, X86::JE, 1).addMBB(Dest);
713 return false;
714 }
715
716 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
717 if (SetCC == 0)
718 return true; // Can only handle simple setcc's so far.
719
720 unsigned Opc;
721
722 // Handle integer conditions first.
723 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
724 switch (SetCC->getCondition()) {
725 default: assert(0 && "Illegal integer SetCC!");
726 case ISD::SETEQ: Opc = X86::JE; break;
727 case ISD::SETGT: Opc = X86::JG; break;
728 case ISD::SETGE: Opc = X86::JGE; break;
729 case ISD::SETLT: Opc = X86::JL; break;
730 case ISD::SETLE: Opc = X86::JLE; break;
731 case ISD::SETNE: Opc = X86::JNE; break;
732 case ISD::SETULT: Opc = X86::JB; break;
733 case ISD::SETUGT: Opc = X86::JA; break;
734 case ISD::SETULE: Opc = X86::JBE; break;
735 case ISD::SETUGE: Opc = X86::JAE; break;
736 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000737 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000738 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
739 BuildMI(BB, Opc, 1).addMBB(Dest);
740 return false;
741 }
742
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000743 unsigned Opc2 = 0; // Second branch if needed.
744
745 // On a floating point condition, the flags are set as follows:
746 // ZF PF CF op
747 // 0 | 0 | 0 | X > Y
748 // 0 | 0 | 1 | X < Y
749 // 1 | 0 | 0 | X == Y
750 // 1 | 1 | 1 | unordered
751 //
752 switch (SetCC->getCondition()) {
753 default: assert(0 && "Invalid FP setcc!");
754 case ISD::SETUEQ:
755 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
756 case ISD::SETOGT:
757 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
758 case ISD::SETOGE:
759 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
760 case ISD::SETULT:
761 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
762 case ISD::SETULE:
763 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
764 case ISD::SETONE:
765 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
766 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
767 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
768 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
769 Opc = X86::JA; // ZF = 0 & CF = 0
770 Opc2 = X86::JP; // PF = 1
771 break;
772 case ISD::SETUGE: // PF = 1 | CF = 0
773 Opc = X86::JAE; // CF = 0
774 Opc2 = X86::JP; // PF = 1
775 break;
776 case ISD::SETUNE: // PF = 1 | ZF = 0
777 Opc = X86::JNE; // ZF = 0
778 Opc2 = X86::JP; // PF = 1
779 break;
780 case ISD::SETOEQ: // PF = 0 & ZF = 1
781 //X86::JNP, X86::JE
782 //X86::AND8rr
783 return true; // FIXME: Emit more efficient code for this branch.
784 case ISD::SETOLT: // PF = 0 & CF = 1
785 //X86::JNP, X86::JB
786 //X86::AND8rr
787 return true; // FIXME: Emit more efficient code for this branch.
788 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
789 //X86::JNP, X86::JBE
790 //X86::AND8rr
791 return true; // FIXME: Emit more efficient code for this branch.
792 }
793
Chris Lattner6c07aee2005-01-11 04:06:27 +0000794 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000795 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
796 BuildMI(BB, Opc, 1).addMBB(Dest);
797 if (Opc2)
798 BuildMI(BB, Opc2, 1).addMBB(Dest);
799 return false;
800}
801
Chris Lattner24aad1b2005-01-10 22:10:13 +0000802/// EmitSelectCC - Emit code into BB that performs a select operation between
803/// the two registers RTrue and RFalse, generating a result into RDest. Return
804/// true if the fold cannot be performed.
805///
806void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
807 unsigned RTrue, unsigned RFalse, unsigned RDest) {
808 enum Condition {
809 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
810 NOT_SET
811 } CondCode = NOT_SET;
812
813 static const unsigned CMOVTAB16[] = {
814 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
815 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
816 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
817 };
818 static const unsigned CMOVTAB32[] = {
819 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
820 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
821 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
822 };
823 static const unsigned CMOVTABFP[] = {
824 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
825 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
826 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
827 };
828
829 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
830 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
831 switch (SetCC->getCondition()) {
832 default: assert(0 && "Unknown integer comparison!");
833 case ISD::SETEQ: CondCode = EQ; break;
834 case ISD::SETGT: CondCode = GT; break;
835 case ISD::SETGE: CondCode = GE; break;
836 case ISD::SETLT: CondCode = LT; break;
837 case ISD::SETLE: CondCode = LE; break;
838 case ISD::SETNE: CondCode = NE; break;
839 case ISD::SETULT: CondCode = B; break;
840 case ISD::SETUGT: CondCode = A; break;
841 case ISD::SETULE: CondCode = BE; break;
842 case ISD::SETUGE: CondCode = AE; break;
843 }
844 } else {
845 // On a floating point condition, the flags are set as follows:
846 // ZF PF CF op
847 // 0 | 0 | 0 | X > Y
848 // 0 | 0 | 1 | X < Y
849 // 1 | 0 | 0 | X == Y
850 // 1 | 1 | 1 | unordered
851 //
852 switch (SetCC->getCondition()) {
853 default: assert(0 && "Unknown FP comparison!");
854 case ISD::SETUEQ:
855 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
856 case ISD::SETOGT:
857 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
858 case ISD::SETOGE:
859 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
860 case ISD::SETULT:
861 case ISD::SETLT: CondCode = B; break; // True if CF = 1
862 case ISD::SETULE:
863 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
864 case ISD::SETONE:
865 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
866 case ISD::SETUO: CondCode = P; break; // True if PF = 1
867 case ISD::SETO: CondCode = NP; break; // True if PF = 0
868 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
869 case ISD::SETUGE: // PF = 1 | CF = 0
870 case ISD::SETUNE: // PF = 1 | ZF = 0
871 case ISD::SETOEQ: // PF = 0 & ZF = 1
872 case ISD::SETOLT: // PF = 0 & CF = 1
873 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
874 // We cannot emit this comparison as a single cmov.
875 break;
876 }
877 }
878 }
879
880 unsigned Opc = 0;
881 if (CondCode != NOT_SET) {
882 switch (SVT) {
883 default: assert(0 && "Cannot select this type!");
884 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
885 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
886 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000887 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000888 }
889 }
890
891 // Finally, if we weren't able to fold this, just emit the condition and test
892 // it.
893 if (CondCode == NOT_SET || Opc == 0) {
894 // Get the condition into the zero flag.
895 unsigned CondReg = SelectExpr(Cond);
896 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
897
898 switch (SVT) {
899 default: assert(0 && "Cannot select this type!");
900 case MVT::i16: Opc = X86::CMOVE16rr; break;
901 case MVT::i32: Opc = X86::CMOVE32rr; break;
902 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000903 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000904 }
905 } else {
906 // FIXME: CMP R, 0 -> TEST R, R
907 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000908 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000909 }
910 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
911}
912
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000913void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000914 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000915 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
916 Opc = 0;
Chris Lattneref6806c2005-01-12 02:02:48 +0000917 if (isFoldableLoad(LHS)) {
918 switch (RHS.getValueType()) {
919 default: break;
920 case MVT::i1:
921 case MVT::i8: Opc = X86::CMP8mi; break;
922 case MVT::i16: Opc = X86::CMP16mi; break;
923 case MVT::i32: Opc = X86::CMP32mi; break;
924 }
925 if (Opc) {
926 X86AddressMode AM;
927 EmitFoldedLoad(LHS, AM);
928 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
929 return;
930 }
931 }
932
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000933 switch (RHS.getValueType()) {
934 default: break;
935 case MVT::i1:
936 case MVT::i8: Opc = X86::CMP8ri; break;
937 case MVT::i16: Opc = X86::CMP16ri; break;
938 case MVT::i32: Opc = X86::CMP32ri; break;
939 }
940 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000941 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000942 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
943 return;
944 }
Chris Lattner7f2afac2005-01-14 22:37:41 +0000945 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
946 if (CN->isExactlyValue(+0.0) ||
947 CN->isExactlyValue(-0.0)) {
948 unsigned Reg = SelectExpr(LHS);
949 BuildMI(BB, X86::FTST, 1).addReg(Reg);
950 BuildMI(BB, X86::FNSTSW8r, 0);
951 BuildMI(BB, X86::SAHF, 1);
952 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000953 }
954
Chris Lattneref6806c2005-01-12 02:02:48 +0000955 Opc = 0;
956 if (isFoldableLoad(LHS)) {
957 switch (RHS.getValueType()) {
958 default: break;
959 case MVT::i1:
960 case MVT::i8: Opc = X86::CMP8mr; break;
961 case MVT::i16: Opc = X86::CMP16mr; break;
962 case MVT::i32: Opc = X86::CMP32mr; break;
963 }
964 if (Opc) {
965 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +0000966 EmitFoldedLoad(LHS, AM);
967 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +0000968 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
969 return;
970 }
971 }
972
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000973 switch (LHS.getValueType()) {
974 default: assert(0 && "Cannot compare this value!");
975 case MVT::i1:
976 case MVT::i8: Opc = X86::CMP8rr; break;
977 case MVT::i16: Opc = X86::CMP16rr; break;
978 case MVT::i32: Opc = X86::CMP32rr; break;
979 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000980 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000981 }
Chris Lattner11333092005-01-11 03:11:44 +0000982 unsigned Tmp1, Tmp2;
983 if (getRegPressure(LHS) > getRegPressure(RHS)) {
984 Tmp1 = SelectExpr(LHS);
985 Tmp2 = SelectExpr(RHS);
986 } else {
987 Tmp2 = SelectExpr(RHS);
988 Tmp1 = SelectExpr(LHS);
989 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000990 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
991}
992
Chris Lattnera5ade062005-01-11 21:19:59 +0000993/// isFoldableLoad - Return true if this is a load instruction that can safely
994/// be folded into an operation that uses it.
995bool ISel::isFoldableLoad(SDOperand Op) {
996 if (Op.getOpcode() != ISD::LOAD ||
997 // FIXME: currently can't fold constant pool indexes.
998 isa<ConstantPoolSDNode>(Op.getOperand(1)))
999 return false;
1000
1001 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001002 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1003 if (ExprMap.count(Op.getValue(1))) return false;
1004 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
1005 assert(!LoweredTokens.count(Op.getValue(1)) &&
1006 "Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001007
Chris Lattnera0bb6922005-01-12 18:38:26 +00001008 // Finally, there can only be one use of its value.
1009 return Op.Val->hasNUsesOfValue(1, 0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001010}
1011
1012/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1013/// and compute the address being loaded into AM.
1014void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1015 SDOperand Chain = Op.getOperand(0);
1016 SDOperand Address = Op.getOperand(1);
1017 if (getRegPressure(Chain) > getRegPressure(Address)) {
1018 Select(Chain);
1019 SelectAddress(Address, AM);
1020 } else {
1021 SelectAddress(Address, AM);
1022 Select(Chain);
1023 }
1024
1025 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001026 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1027 "Load emitted more than once?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001028 ExprMap[SDOperand(Op.Val, 1)] = 1;
Chris Lattner636e79a2005-01-13 05:53:16 +00001029 if (!LoweredTokens.insert(Op.getValue(1)).second)
1030 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001031}
1032
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001033unsigned ISel::SelectExpr(SDOperand N) {
1034 unsigned Result;
1035 unsigned Tmp1, Tmp2, Tmp3;
1036 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001037 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001038 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001039
Chris Lattner7f2afac2005-01-14 22:37:41 +00001040 if (Node->getOpcode() == ISD::CopyFromReg) {
1041 // FIXME: Handle copy from physregs!
1042
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001043 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001044 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001045 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001046
1047 unsigned &Reg = ExprMap[N];
1048 if (Reg) return Reg;
1049
1050 if (N.getOpcode() != ISD::CALL)
1051 Reg = Result = (N.getValueType() != MVT::Other) ?
1052 MakeReg(N.getValueType()) : 1;
1053 else {
1054 // If this is a call instruction, make sure to prepare ALL of the result
1055 // values as well as the chain.
1056 if (Node->getNumValues() == 1)
1057 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001058 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001059 Result = MakeReg(Node->getValueType(0));
1060 ExprMap[N.getValue(0)] = Result;
1061 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1062 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1063 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001064 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001065 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001066
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001067 switch (N.getOpcode()) {
1068 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001069 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001070 assert(0 && "Node not handled!\n");
1071 case ISD::FrameIndex:
1072 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1073 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1074 return Result;
1075 case ISD::ConstantPool:
1076 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1077 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1078 return Result;
1079 case ISD::ConstantFP:
1080 ContainsFPCode = true;
1081 Tmp1 = Result; // Intermediate Register
1082 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1083 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1084 Tmp1 = MakeReg(MVT::f64);
1085
1086 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1087 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1088 BuildMI(BB, X86::FLD0, 0, Tmp1);
1089 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1090 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1091 BuildMI(BB, X86::FLD1, 0, Tmp1);
1092 else
1093 assert(0 && "Unexpected constant!");
1094 if (Tmp1 != Result)
1095 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1096 return Result;
1097 case ISD::Constant:
1098 switch (N.getValueType()) {
1099 default: assert(0 && "Cannot use constants of this type!");
1100 case MVT::i1:
1101 case MVT::i8: Opc = X86::MOV8ri; break;
1102 case MVT::i16: Opc = X86::MOV16ri; break;
1103 case MVT::i32: Opc = X86::MOV32ri; break;
1104 }
1105 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1106 return Result;
1107 case ISD::GlobalAddress: {
1108 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1109 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1110 return Result;
1111 }
1112 case ISD::ExternalSymbol: {
1113 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1114 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1115 return Result;
1116 }
1117 case ISD::FP_EXTEND:
1118 Tmp1 = SelectExpr(N.getOperand(0));
1119 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001120 return Result;
1121 case ISD::ZERO_EXTEND: {
1122 int DestIs16 = N.getValueType() == MVT::i16;
1123 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001124
1125 // FIXME: This hack is here for zero extension casts from bool to i8. This
1126 // would not be needed if bools were promoted by Legalize.
1127 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001128 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001129 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1130 return Result;
1131 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001132
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001133 if (isFoldableLoad(N.getOperand(0))) {
1134 static const unsigned Opc[3] = {
1135 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1136 };
1137
1138 X86AddressMode AM;
1139 EmitFoldedLoad(N.getOperand(0), AM);
1140 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1141
1142 return Result;
1143 }
1144
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001145 static const unsigned Opc[3] = {
1146 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1147 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001148 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001149 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1150 return Result;
1151 }
1152 case ISD::SIGN_EXTEND: {
1153 int DestIs16 = N.getValueType() == MVT::i16;
1154 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1155
Chris Lattner590d8002005-01-09 18:52:44 +00001156 // FIXME: Legalize should promote bools to i8!
1157 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1158 "Sign extend from bool not implemented!");
1159
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001160 if (isFoldableLoad(N.getOperand(0))) {
1161 static const unsigned Opc[3] = {
1162 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1163 };
1164
1165 X86AddressMode AM;
1166 EmitFoldedLoad(N.getOperand(0), AM);
1167 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1168 return Result;
1169 }
1170
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001171 static const unsigned Opc[3] = {
1172 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1173 };
1174 Tmp1 = SelectExpr(N.getOperand(0));
1175 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1176 return Result;
1177 }
1178 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001179 // Fold TRUNCATE (LOAD P) into a smaller load from P.
1180 if (isFoldableLoad(N.getOperand(0))) {
1181 switch (N.getValueType()) {
1182 default: assert(0 && "Unknown truncate!");
1183 case MVT::i1:
1184 case MVT::i8: Opc = X86::MOV8rm; break;
1185 case MVT::i16: Opc = X86::MOV16rm; break;
1186 }
1187 X86AddressMode AM;
1188 EmitFoldedLoad(N.getOperand(0), AM);
1189 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1190 return Result;
1191 }
1192
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001193 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1194 // a move out of AX or AL.
1195 switch (N.getOperand(0).getValueType()) {
1196 default: assert(0 && "Unknown truncate!");
1197 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1198 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1199 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1200 }
1201 Tmp1 = SelectExpr(N.getOperand(0));
1202 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1203
1204 switch (N.getValueType()) {
1205 default: assert(0 && "Unknown truncate!");
1206 case MVT::i1:
1207 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1208 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1209 }
1210 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1211 return Result;
1212
1213 case ISD::FP_ROUND:
1214 // Truncate from double to float by storing to memory as float,
1215 // then reading it back into a register.
1216
1217 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001218 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001219 Tmp1 = TLI.getTargetData().getFloatAlignment();
1220 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1221
1222 // Codegen the input.
1223 Tmp1 = SelectExpr(N.getOperand(0));
1224
1225 // Emit the store, then the reload.
1226 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1227 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001228 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001229
1230 case ISD::SINT_TO_FP:
1231 case ISD::UINT_TO_FP: {
1232 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001233 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001234
1235 // Promote the integer to a type supported by FLD. We do this because there
1236 // are no unsigned FLD instructions, so we must promote an unsigned value to
1237 // a larger signed value, then use FLD on the larger value.
1238 //
1239 MVT::ValueType PromoteType = MVT::Other;
1240 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1241 unsigned PromoteOpcode = 0;
1242 unsigned RealDestReg = Result;
1243 switch (SrcTy) {
1244 case MVT::i1:
1245 case MVT::i8:
1246 // We don't have the facilities for directly loading byte sized data from
1247 // memory (even signed). Promote it to 16 bits.
1248 PromoteType = MVT::i16;
1249 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1250 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1251 break;
1252 case MVT::i16:
1253 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1254 PromoteType = MVT::i32;
1255 PromoteOpcode = X86::MOVZX32rr16;
1256 }
1257 break;
1258 default:
1259 // Don't fild into the real destination.
1260 if (Node->getOpcode() == ISD::UINT_TO_FP)
1261 Result = MakeReg(Node->getValueType(0));
1262 break;
1263 }
1264
1265 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1266
1267 if (PromoteType != MVT::Other) {
1268 Tmp2 = MakeReg(PromoteType);
1269 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1270 SrcTy = PromoteType;
1271 Tmp1 = Tmp2;
1272 }
1273
1274 // Spill the integer to memory and reload it from there.
1275 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1276 MachineFunction *F = BB->getParent();
1277 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1278
1279 switch (SrcTy) {
1280 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001281 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001282 // FIXME: this won't work for cast [u]long to FP
1283 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1284 FrameIdx).addReg(Tmp1);
1285 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1286 FrameIdx, 4).addReg(Tmp1+1);
1287 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1288 break;
1289 case MVT::i32:
1290 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1291 FrameIdx).addReg(Tmp1);
1292 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1293 break;
1294 case MVT::i16:
1295 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1296 FrameIdx).addReg(Tmp1);
1297 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1298 break;
1299 default: break; // No promotion required.
1300 }
1301
Chris Lattner085c9952005-01-12 04:00:00 +00001302 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001303 // If this is a cast from uint -> double, we need to be careful when if
1304 // the "sign" bit is set. If so, we don't want to make a negative number,
1305 // we want to make a positive number. Emit code to add an offset if the
1306 // sign bit is set.
1307
1308 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1309 unsigned IsNeg = MakeReg(MVT::i32);
1310 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1311
1312 // Create a CP value that has the offset in one word and 0 in the other.
1313 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1314 0x4f80000000000000ULL);
1315 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1316 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1317 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1318
1319 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1320 // We need special handling for unsigned 64-bit integer sources. If the
1321 // input number has the "sign bit" set, then we loaded it incorrectly as a
1322 // negative 64-bit number. In this case, add an offset value.
1323
1324 // Emit a test instruction to see if the dynamic input value was signed.
1325 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1326
1327 // If the sign bit is set, get a pointer to an offset, otherwise get a
1328 // pointer to a zero.
1329 MachineConstantPool *CP = F->getConstantPool();
1330 unsigned Zero = MakeReg(MVT::i32);
1331 Constant *Null = Constant::getNullValue(Type::UIntTy);
1332 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1333 CP->getConstantPoolIndex(Null));
1334 unsigned Offset = MakeReg(MVT::i32);
1335 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1336
1337 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1338 CP->getConstantPoolIndex(OffsetCst));
1339 unsigned Addr = MakeReg(MVT::i32);
1340 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1341
1342 // Load the constant for an add. FIXME: this could make an 'fadd' that
1343 // reads directly from memory, but we don't support these yet.
1344 unsigned ConstReg = MakeReg(MVT::f64);
1345 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1346
1347 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1348 }
1349 return RealDestReg;
1350 }
1351 case ISD::FP_TO_SINT:
1352 case ISD::FP_TO_UINT: {
1353 // FIXME: Most of this grunt work should be done by legalize!
1354 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1355
1356 // Change the floating point control register to use "round towards zero"
1357 // mode when truncating to an integer value.
1358 //
1359 MachineFunction *F = BB->getParent();
1360 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1361 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1362
1363 // Load the old value of the high byte of the control word...
1364 unsigned HighPartOfCW = MakeReg(MVT::i8);
1365 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1366 CWFrameIdx, 1);
1367
1368 // Set the high part to be round to zero...
1369 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1370 CWFrameIdx, 1).addImm(12);
1371
1372 // Reload the modified control word now...
1373 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1374
1375 // Restore the memory image of control word to original value
1376 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1377 CWFrameIdx, 1).addReg(HighPartOfCW);
1378
1379 // We don't have the facilities for directly storing byte sized data to
1380 // memory. Promote it to 16 bits. We also must promote unsigned values to
1381 // larger classes because we only have signed FP stores.
1382 MVT::ValueType StoreClass = Node->getValueType(0);
1383 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1384 switch (StoreClass) {
1385 case MVT::i8: StoreClass = MVT::i16; break;
1386 case MVT::i16: StoreClass = MVT::i32; break;
1387 case MVT::i32: StoreClass = MVT::i64; break;
1388 // The following treatment of cLong may not be perfectly right,
1389 // but it survives chains of casts of the form
1390 // double->ulong->double.
1391 case MVT::i64: StoreClass = MVT::i64; break;
1392 default: assert(0 && "Unknown store class!");
1393 }
1394
1395 // Spill the integer to memory and reload it from there.
1396 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1397 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1398
1399 switch (StoreClass) {
1400 default: assert(0 && "Unknown store class!");
1401 case MVT::i16:
1402 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1403 break;
1404 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001405 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001406 break;
1407 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001408 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001409 break;
1410 }
1411
1412 switch (Node->getValueType(0)) {
1413 default:
1414 assert(0 && "Unknown integer type!");
1415 case MVT::i64:
1416 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001417 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001418 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1419 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1420 case MVT::i32:
1421 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1422 break;
1423 case MVT::i16:
1424 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1425 break;
1426 case MVT::i8:
1427 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1428 break;
1429 }
1430
1431 // Reload the original control word now.
1432 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1433 return Result;
1434 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001435 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001436 Op0 = N.getOperand(0);
1437 Op1 = N.getOperand(1);
1438
1439 if (isFoldableLoad(Op0))
1440 std::swap(Op0, Op1);
1441
1442 if (isFoldableLoad(Op1)) {
1443 switch (N.getValueType()) {
1444 default: assert(0 && "Cannot add this type!");
1445 case MVT::i1:
1446 case MVT::i8: Opc = X86::ADD8rm; break;
1447 case MVT::i16: Opc = X86::ADD16rm; break;
1448 case MVT::i32: Opc = X86::ADD32rm; break;
1449 case MVT::f32: Opc = X86::FADD32m; break;
1450 case MVT::f64: Opc = X86::FADD64m; break;
1451 }
1452 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001453 EmitFoldedLoad(Op1, AM);
1454 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001455 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1456 return Result;
1457 }
1458
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001459 // See if we can codegen this as an LEA to fold operations together.
1460 if (N.getValueType() == MVT::i32) {
1461 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001462 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001463 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001464 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001465 // leave this as LEA, then peephole it to 'ADD' after two address elim
1466 // happens.
1467 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001468 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001469 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1470 return Result;
1471 }
1472 }
1473 }
Chris Lattner11333092005-01-11 03:11:44 +00001474
Chris Lattnera5ade062005-01-11 21:19:59 +00001475 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001476 Opc = 0;
1477 if (CN->getValue() == 1) { // add X, 1 -> inc X
1478 switch (N.getValueType()) {
1479 default: assert(0 && "Cannot integer add this type!");
1480 case MVT::i8: Opc = X86::INC8r; break;
1481 case MVT::i16: Opc = X86::INC16r; break;
1482 case MVT::i32: Opc = X86::INC32r; break;
1483 }
1484 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1485 switch (N.getValueType()) {
1486 default: assert(0 && "Cannot integer add this type!");
1487 case MVT::i8: Opc = X86::DEC8r; break;
1488 case MVT::i16: Opc = X86::DEC16r; break;
1489 case MVT::i32: Opc = X86::DEC32r; break;
1490 }
1491 }
1492
1493 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001494 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001495 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1496 return Result;
1497 }
1498
1499 switch (N.getValueType()) {
1500 default: assert(0 && "Cannot add this type!");
1501 case MVT::i8: Opc = X86::ADD8ri; break;
1502 case MVT::i16: Opc = X86::ADD16ri; break;
1503 case MVT::i32: Opc = X86::ADD32ri; break;
1504 }
1505 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001506 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001507 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1508 return Result;
1509 }
1510 }
1511
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001512 switch (N.getValueType()) {
1513 default: assert(0 && "Cannot add this type!");
1514 case MVT::i8: Opc = X86::ADD8rr; break;
1515 case MVT::i16: Opc = X86::ADD16rr; break;
1516 case MVT::i32: Opc = X86::ADD32rr; break;
1517 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001518 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001519 }
Chris Lattner11333092005-01-11 03:11:44 +00001520
Chris Lattnera5ade062005-01-11 21:19:59 +00001521 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1522 Tmp1 = SelectExpr(Op0);
1523 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001524 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001525 Tmp2 = SelectExpr(Op1);
1526 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001527 }
1528
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001529 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1530 return Result;
1531 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001532 case ISD::MUL:
1533 case ISD::AND:
1534 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001535 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001536 static const unsigned SUBTab[] = {
1537 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1538 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1539 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1540 };
1541 static const unsigned MULTab[] = {
1542 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1543 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1544 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1545 };
1546 static const unsigned ANDTab[] = {
1547 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1548 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1549 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1550 };
1551 static const unsigned ORTab[] = {
1552 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1553 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1554 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1555 };
1556 static const unsigned XORTab[] = {
1557 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1558 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1559 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1560 };
1561
1562 Op0 = Node->getOperand(0);
1563 Op1 = Node->getOperand(1);
1564
1565 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001566 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1567 if (CN->isNullValue()) { // 0 - N -> neg N
1568 switch (N.getValueType()) {
1569 default: assert(0 && "Cannot sub this type!");
1570 case MVT::i1:
1571 case MVT::i8: Opc = X86::NEG8r; break;
1572 case MVT::i16: Opc = X86::NEG16r; break;
1573 case MVT::i32: Opc = X86::NEG32r; break;
1574 }
1575 Tmp1 = SelectExpr(N.getOperand(1));
1576 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1577 return Result;
1578 }
1579
Chris Lattnera5ade062005-01-11 21:19:59 +00001580 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1581 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001582 switch (N.getValueType()) {
1583 default: assert(0 && "Cannot add this type!");
1584 case MVT::i1:
1585 case MVT::i8: Opc = X86::NOT8r; break;
1586 case MVT::i16: Opc = X86::NOT16r; break;
1587 case MVT::i32: Opc = X86::NOT32r; break;
1588 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001589 Tmp1 = SelectExpr(Op0);
Chris Lattnerd4dab922005-01-11 04:31:30 +00001590 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1591 return Result;
1592 }
1593
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001594 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001595 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001596 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001597 case MVT::i8: Opc = 0; break;
1598 case MVT::i16: Opc = 1; break;
1599 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001600 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001601 switch (Node->getOpcode()) {
1602 default: assert(0 && "Unreachable!");
1603 case ISD::SUB: Opc = SUBTab[Opc]; break;
1604 case ISD::MUL: Opc = MULTab[Opc]; break;
1605 case ISD::AND: Opc = ANDTab[Opc]; break;
1606 case ISD::OR: Opc = ORTab[Opc]; break;
1607 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001608 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001609 if (Opc) { // Can't fold MUL:i8 R, imm
1610 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001611 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1612 return Result;
1613 }
1614 }
Chris Lattner11333092005-01-11 03:11:44 +00001615
Chris Lattnera5ade062005-01-11 21:19:59 +00001616 if (isFoldableLoad(Op0))
1617 if (Node->getOpcode() != ISD::SUB) {
1618 std::swap(Op0, Op1);
1619 } else {
1620 // Emit 'reverse' subract, with a memory operand.
1621 switch (N.getValueType()) {
1622 default: Opc = 0; break;
1623 case MVT::f32: Opc = X86::FSUBR32m; break;
1624 case MVT::f64: Opc = X86::FSUBR64m; break;
1625 }
1626 if (Opc) {
1627 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001628 EmitFoldedLoad(Op0, AM);
1629 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00001630 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1631 return Result;
1632 }
1633 }
1634
1635 if (isFoldableLoad(Op1)) {
1636 switch (N.getValueType()) {
1637 default: assert(0 && "Cannot operate on this type!");
1638 case MVT::i1:
1639 case MVT::i8: Opc = 5; break;
1640 case MVT::i16: Opc = 6; break;
1641 case MVT::i32: Opc = 7; break;
1642 case MVT::f32: Opc = 8; break;
1643 case MVT::f64: Opc = 9; break;
1644 }
1645 switch (Node->getOpcode()) {
1646 default: assert(0 && "Unreachable!");
1647 case ISD::SUB: Opc = SUBTab[Opc]; break;
1648 case ISD::MUL: Opc = MULTab[Opc]; break;
1649 case ISD::AND: Opc = ANDTab[Opc]; break;
1650 case ISD::OR: Opc = ORTab[Opc]; break;
1651 case ISD::XOR: Opc = XORTab[Opc]; break;
1652 }
1653
1654 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001655 EmitFoldedLoad(Op1, AM);
1656 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001657 if (Opc) {
1658 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1659 } else {
1660 assert(Node->getOpcode() == ISD::MUL &&
1661 N.getValueType() == MVT::i8 && "Unexpected situation!");
1662 // Must use the MUL instruction, which forces use of AL.
1663 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1664 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1665 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1666 }
1667 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001668 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001669
1670 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1671 Tmp1 = SelectExpr(Op0);
1672 Tmp2 = SelectExpr(Op1);
1673 } else {
1674 Tmp2 = SelectExpr(Op1);
1675 Tmp1 = SelectExpr(Op0);
1676 }
1677
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001678 switch (N.getValueType()) {
1679 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001680 case MVT::i1:
1681 case MVT::i8: Opc = 10; break;
1682 case MVT::i16: Opc = 11; break;
1683 case MVT::i32: Opc = 12; break;
1684 case MVT::f32: Opc = 13; break;
1685 case MVT::f64: Opc = 14; break;
1686 }
1687 switch (Node->getOpcode()) {
1688 default: assert(0 && "Unreachable!");
1689 case ISD::SUB: Opc = SUBTab[Opc]; break;
1690 case ISD::MUL: Opc = MULTab[Opc]; break;
1691 case ISD::AND: Opc = ANDTab[Opc]; break;
1692 case ISD::OR: Opc = ORTab[Opc]; break;
1693 case ISD::XOR: Opc = XORTab[Opc]; break;
1694 }
1695 if (Opc) {
1696 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1697 } else {
1698 assert(Node->getOpcode() == ISD::MUL &&
1699 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001700 // Must use the MUL instruction, which forces use of AL.
1701 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1702 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1703 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001704 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001705 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001706 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001707 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001708 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1709 Tmp2 = SelectExpr(N.getOperand(1));
1710 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001711 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001712 Tmp3 = SelectExpr(N.getOperand(2));
1713 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001714 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00001715 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
1716 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001717
1718 case ISD::SDIV:
1719 case ISD::UDIV:
1720 case ISD::SREM:
1721 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00001722 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
1723 "We don't support this operator!");
1724
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001725 if (N.getOpcode() == ISD::SDIV)
1726 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1727 // FIXME: These special cases should be handled by the lowering impl!
1728 unsigned RHS = CN->getValue();
1729 bool isNeg = false;
1730 if ((int)RHS < 0) {
1731 isNeg = true;
1732 RHS = -RHS;
1733 }
1734 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1735 unsigned Log = log2(RHS);
1736 unsigned TmpReg = MakeReg(N.getValueType());
1737 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1738 switch (N.getValueType()) {
1739 default: assert("Unknown type to signed divide!");
1740 case MVT::i8:
1741 SAROpc = X86::SAR8ri;
1742 SHROpc = X86::SHR8ri;
1743 ADDOpc = X86::ADD8rr;
1744 NEGOpc = X86::NEG8r;
1745 break;
1746 case MVT::i16:
1747 SAROpc = X86::SAR16ri;
1748 SHROpc = X86::SHR16ri;
1749 ADDOpc = X86::ADD16rr;
1750 NEGOpc = X86::NEG16r;
1751 break;
1752 case MVT::i32:
1753 SAROpc = X86::SAR32ri;
1754 SHROpc = X86::SHR32ri;
1755 ADDOpc = X86::ADD32rr;
1756 NEGOpc = X86::NEG32r;
1757 break;
1758 }
Chris Lattner11333092005-01-11 03:11:44 +00001759 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001760 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1761 unsigned TmpReg2 = MakeReg(N.getValueType());
1762 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1763 unsigned TmpReg3 = MakeReg(N.getValueType());
1764 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1765
1766 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1767 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1768 if (isNeg)
1769 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1770 return Result;
1771 }
1772 }
1773
Chris Lattner11333092005-01-11 03:11:44 +00001774 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1775 Tmp1 = SelectExpr(N.getOperand(0));
1776 Tmp2 = SelectExpr(N.getOperand(1));
1777 } else {
1778 Tmp2 = SelectExpr(N.getOperand(1));
1779 Tmp1 = SelectExpr(N.getOperand(0));
1780 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001781
1782 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1783 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1784 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1785 switch (N.getValueType()) {
1786 default: assert(0 && "Cannot sdiv this type!");
1787 case MVT::i8:
1788 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1789 LoReg = X86::AL;
1790 HiReg = X86::AH;
1791 MovOpcode = X86::MOV8rr;
1792 ClrOpcode = X86::MOV8ri;
1793 SExtOpcode = X86::CBW;
1794 break;
1795 case MVT::i16:
1796 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1797 LoReg = X86::AX;
1798 HiReg = X86::DX;
1799 MovOpcode = X86::MOV16rr;
1800 ClrOpcode = X86::MOV16ri;
1801 SExtOpcode = X86::CWD;
1802 break;
1803 case MVT::i32:
1804 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001805 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001806 HiReg = X86::EDX;
1807 MovOpcode = X86::MOV32rr;
1808 ClrOpcode = X86::MOV32ri;
1809 SExtOpcode = X86::CDQ;
1810 break;
1811 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1812 case MVT::f32:
1813 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00001814 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001815 return Result;
1816 }
1817
1818 // Set up the low part.
1819 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1820
1821 if (isSigned) {
1822 // Sign extend the low part into the high part.
1823 BuildMI(BB, SExtOpcode, 0);
1824 } else {
1825 // Zero out the high part, effectively zero extending the input.
1826 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1827 }
1828
1829 // Emit the DIV/IDIV instruction.
1830 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1831
1832 // Get the result of the divide or rem.
1833 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1834 return Result;
1835 }
1836
1837 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001838 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001839 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1840 switch (N.getValueType()) {
1841 default: assert(0 && "Cannot shift this type!");
1842 case MVT::i8: Opc = X86::ADD8rr; break;
1843 case MVT::i16: Opc = X86::ADD16rr; break;
1844 case MVT::i32: Opc = X86::ADD32rr; break;
1845 }
1846 Tmp1 = SelectExpr(N.getOperand(0));
1847 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1848 return Result;
1849 }
1850
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001851 switch (N.getValueType()) {
1852 default: assert(0 && "Cannot shift this type!");
1853 case MVT::i8: Opc = X86::SHL8ri; break;
1854 case MVT::i16: Opc = X86::SHL16ri; break;
1855 case MVT::i32: Opc = X86::SHL32ri; break;
1856 }
Chris Lattner11333092005-01-11 03:11:44 +00001857 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001858 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1859 return Result;
1860 }
Chris Lattner11333092005-01-11 03:11:44 +00001861
1862 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1863 Tmp1 = SelectExpr(N.getOperand(0));
1864 Tmp2 = SelectExpr(N.getOperand(1));
1865 } else {
1866 Tmp2 = SelectExpr(N.getOperand(1));
1867 Tmp1 = SelectExpr(N.getOperand(0));
1868 }
1869
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001870 switch (N.getValueType()) {
1871 default: assert(0 && "Cannot shift this type!");
1872 case MVT::i8 : Opc = X86::SHL8rCL; break;
1873 case MVT::i16: Opc = X86::SHL16rCL; break;
1874 case MVT::i32: Opc = X86::SHL32rCL; break;
1875 }
1876 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1877 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1878 return Result;
1879 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001880 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1881 switch (N.getValueType()) {
1882 default: assert(0 && "Cannot shift this type!");
1883 case MVT::i8: Opc = X86::SHR8ri; break;
1884 case MVT::i16: Opc = X86::SHR16ri; break;
1885 case MVT::i32: Opc = X86::SHR32ri; break;
1886 }
Chris Lattner11333092005-01-11 03:11:44 +00001887 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001888 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1889 return Result;
1890 }
Chris Lattner11333092005-01-11 03:11:44 +00001891
1892 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1893 Tmp1 = SelectExpr(N.getOperand(0));
1894 Tmp2 = SelectExpr(N.getOperand(1));
1895 } else {
1896 Tmp2 = SelectExpr(N.getOperand(1));
1897 Tmp1 = SelectExpr(N.getOperand(0));
1898 }
1899
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001900 switch (N.getValueType()) {
1901 default: assert(0 && "Cannot shift this type!");
1902 case MVT::i8 : Opc = X86::SHR8rCL; break;
1903 case MVT::i16: Opc = X86::SHR16rCL; break;
1904 case MVT::i32: Opc = X86::SHR32rCL; break;
1905 }
1906 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1907 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1908 return Result;
1909 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001910 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1911 switch (N.getValueType()) {
1912 default: assert(0 && "Cannot shift this type!");
1913 case MVT::i8: Opc = X86::SAR8ri; break;
1914 case MVT::i16: Opc = X86::SAR16ri; break;
1915 case MVT::i32: Opc = X86::SAR32ri; break;
1916 }
Chris Lattner11333092005-01-11 03:11:44 +00001917 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001918 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1919 return Result;
1920 }
Chris Lattner11333092005-01-11 03:11:44 +00001921
1922 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1923 Tmp1 = SelectExpr(N.getOperand(0));
1924 Tmp2 = SelectExpr(N.getOperand(1));
1925 } else {
1926 Tmp2 = SelectExpr(N.getOperand(1));
1927 Tmp1 = SelectExpr(N.getOperand(0));
1928 }
1929
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001930 switch (N.getValueType()) {
1931 default: assert(0 && "Cannot shift this type!");
1932 case MVT::i8 : Opc = X86::SAR8rCL; break;
1933 case MVT::i16: Opc = X86::SAR16rCL; break;
1934 case MVT::i32: Opc = X86::SAR32rCL; break;
1935 }
1936 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1937 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1938 return Result;
1939
1940 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001941 EmitCMP(N.getOperand(0), N.getOperand(1));
1942 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1943 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1944 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00001945 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001946 // Make sure we generate both values.
1947 if (Result != 1)
1948 ExprMap[N.getValue(1)] = 1; // Generate the token
1949 else
1950 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1951
Chris Lattner5188ad72005-01-08 19:28:19 +00001952 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001953 default: assert(0 && "Cannot load this type!");
1954 case MVT::i1:
1955 case MVT::i8: Opc = X86::MOV8rm; break;
1956 case MVT::i16: Opc = X86::MOV16rm; break;
1957 case MVT::i32: Opc = X86::MOV32rm; break;
1958 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1959 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1960 }
Chris Lattner11333092005-01-11 03:11:44 +00001961
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001962 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001963 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001964 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1965 } else {
1966 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001967
1968 SDOperand Chain = N.getOperand(0);
1969 SDOperand Address = N.getOperand(1);
1970 if (getRegPressure(Chain) > getRegPressure(Address)) {
1971 Select(Chain);
1972 SelectAddress(Address, AM);
1973 } else {
1974 SelectAddress(Address, AM);
1975 Select(Chain);
1976 }
1977
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001978 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1979 }
1980 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00001981
1982 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
1983 case ISD::ZEXTLOAD: {
1984 // Make sure we generate both values.
1985 if (Result != 1)
1986 ExprMap[N.getValue(1)] = 1; // Generate the token
1987 else
1988 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1989
Chris Lattnerda2ce112005-01-16 07:34:08 +00001990 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
1991 if (Node->getValueType(0) == MVT::f64) {
1992 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
1993 "Bad EXTLOAD!");
1994 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
1995 CP->getIndex());
1996 return Result;
1997 }
1998
Chris Lattnere9ef81d2005-01-15 05:22:24 +00001999 X86AddressMode AM;
2000 if (getRegPressure(Node->getOperand(0)) >
2001 getRegPressure(Node->getOperand(1))) {
2002 Select(Node->getOperand(0)); // chain
2003 SelectAddress(Node->getOperand(1), AM);
2004 } else {
2005 SelectAddress(Node->getOperand(1), AM);
2006 Select(Node->getOperand(0)); // chain
2007 }
2008
2009 switch (Node->getValueType(0)) {
2010 default: assert(0 && "Unknown type to sign extend to.");
2011 case MVT::f64:
2012 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2013 "Bad EXTLOAD!");
2014 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2015 break;
2016 case MVT::i32:
2017 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2018 default:
2019 assert(0 && "Bad zero extend!");
2020 case MVT::i1:
2021 case MVT::i8:
2022 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2023 break;
2024 case MVT::i16:
2025 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2026 break;
2027 }
2028 break;
2029 case MVT::i16:
2030 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2031 "Bad zero extend!");
2032 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2033 break;
2034 case MVT::i8:
2035 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2036 "Bad zero extend!");
2037 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2038 break;
2039 }
2040 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002041 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002042 case ISD::SEXTLOAD: {
2043 // Make sure we generate both values.
2044 if (Result != 1)
2045 ExprMap[N.getValue(1)] = 1; // Generate the token
2046 else
2047 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2048
2049 X86AddressMode AM;
2050 if (getRegPressure(Node->getOperand(0)) >
2051 getRegPressure(Node->getOperand(1))) {
2052 Select(Node->getOperand(0)); // chain
2053 SelectAddress(Node->getOperand(1), AM);
2054 } else {
2055 SelectAddress(Node->getOperand(1), AM);
2056 Select(Node->getOperand(0)); // chain
2057 }
2058
2059 switch (Node->getValueType(0)) {
2060 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2061 default: assert(0 && "Unknown type to sign extend to.");
2062 case MVT::i32:
2063 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2064 default:
2065 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2066 case MVT::i8:
2067 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2068 break;
2069 case MVT::i16:
2070 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2071 break;
2072 }
2073 break;
2074 case MVT::i16:
2075 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2076 "Cannot sign extend from bool!");
2077 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2078 break;
2079 }
2080 return Result;
2081 }
2082
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002083 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002084 // Generate both result values.
2085 if (Result != 1)
2086 ExprMap[N.getValue(1)] = 1; // Generate the token
2087 else
2088 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2089
2090 // FIXME: We are currently ignoring the requested alignment for handling
2091 // greater than the stack alignment. This will need to be revisited at some
2092 // point. Align = N.getOperand(2);
2093
2094 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2095 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2096 std::cerr << "Cannot allocate stack object with greater alignment than"
2097 << " the stack alignment yet!";
2098 abort();
2099 }
2100
2101 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002102 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002103 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2104 .addImm(CN->getValue());
2105 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002106 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2107 Select(N.getOperand(0));
2108 Tmp1 = SelectExpr(N.getOperand(1));
2109 } else {
2110 Tmp1 = SelectExpr(N.getOperand(1));
2111 Select(N.getOperand(0));
2112 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002113
2114 // Subtract size from stack pointer, thereby allocating some space.
2115 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2116 }
2117
2118 // Put a pointer to the space into the result register, by copying the stack
2119 // pointer.
2120 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2121 return Result;
2122
2123 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002124 // The chain for this call is now lowered.
2125 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
2126
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002127 if (GlobalAddressSDNode *GASD =
2128 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002129 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002130 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2131 } else if (ExternalSymbolSDNode *ESSDN =
2132 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002133 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002134 BuildMI(BB, X86::CALLpcrel32,
2135 1).addExternalSymbol(ESSDN->getSymbol(), true);
2136 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002137 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2138 Select(N.getOperand(0));
2139 Tmp1 = SelectExpr(N.getOperand(1));
2140 } else {
2141 Tmp1 = SelectExpr(N.getOperand(1));
2142 Select(N.getOperand(0));
2143 }
2144
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002145 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2146 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002147 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002148 default: assert(0 && "Unknown value type for call result!");
2149 case MVT::Other: return 1;
2150 case MVT::i1:
2151 case MVT::i8:
2152 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2153 break;
2154 case MVT::i16:
2155 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2156 break;
2157 case MVT::i32:
2158 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002159 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002160 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2161 break;
2162 case MVT::f32:
2163 case MVT::f64: // Floating-point return values live in %ST(0)
2164 ContainsFPCode = true;
2165 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2166 break;
2167 }
2168 return Result+N.ResNo;
2169 }
2170
2171 return 0;
2172}
2173
2174void ISel::Select(SDOperand N) {
2175 unsigned Tmp1, Tmp2, Opc;
2176
2177 // FIXME: Disable for our current expansion model!
2178 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
2179 return; // Already selected.
2180
Chris Lattner989de032005-01-11 06:14:36 +00002181 SDNode *Node = N.Val;
2182
2183 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002184 default:
Chris Lattner989de032005-01-11 06:14:36 +00002185 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002186 assert(0 && "Node not handled yet!");
2187 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002188 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002189 if (Node->getNumOperands() == 2) {
2190 bool OneFirst =
2191 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2192 Select(Node->getOperand(OneFirst));
2193 Select(Node->getOperand(!OneFirst));
2194 } else {
2195 std::vector<std::pair<unsigned, unsigned> > OpsP;
2196 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2197 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2198 std::sort(OpsP.begin(), OpsP.end());
2199 std::reverse(OpsP.begin(), OpsP.end());
2200 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2201 Select(Node->getOperand(OpsP[i].second));
2202 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002203 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002204 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002205 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2206 Select(N.getOperand(0));
2207 Tmp1 = SelectExpr(N.getOperand(1));
2208 } else {
2209 Tmp1 = SelectExpr(N.getOperand(1));
2210 Select(N.getOperand(0));
2211 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002212 Tmp2 = cast<RegSDNode>(N)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002213
2214 if (Tmp1 != Tmp2) {
2215 switch (N.getOperand(1).getValueType()) {
2216 default: assert(0 && "Invalid type for operation!");
2217 case MVT::i1:
2218 case MVT::i8: Opc = X86::MOV8rr; break;
2219 case MVT::i16: Opc = X86::MOV16rr; break;
2220 case MVT::i32: Opc = X86::MOV32rr; break;
2221 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002222 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002223 }
2224 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2225 }
2226 return;
2227 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002228 switch (N.getNumOperands()) {
2229 default:
2230 assert(0 && "Unknown return instruction!");
2231 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002232 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2233 N.getOperand(2).getValueType() == MVT::i32 &&
2234 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002235 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2236 Tmp1 = SelectExpr(N.getOperand(1));
2237 Tmp2 = SelectExpr(N.getOperand(2));
2238 } else {
2239 Tmp2 = SelectExpr(N.getOperand(2));
2240 Tmp1 = SelectExpr(N.getOperand(1));
2241 }
2242 Select(N.getOperand(0));
2243
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002244 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2245 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2246 // Declare that EAX & EDX are live on exit.
2247 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2248 .addReg(X86::ESP);
2249 break;
2250 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002251 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2252 Select(N.getOperand(0));
2253 Tmp1 = SelectExpr(N.getOperand(1));
2254 } else {
2255 Tmp1 = SelectExpr(N.getOperand(1));
2256 Select(N.getOperand(0));
2257 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002258 switch (N.getOperand(1).getValueType()) {
2259 default: assert(0 && "All other types should have been promoted!!");
2260 case MVT::f64:
2261 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2262 // Declare that top-of-stack is live on exit
2263 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2264 break;
2265 case MVT::i32:
2266 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2267 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2268 break;
2269 }
2270 break;
2271 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002272 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002273 break;
2274 }
2275 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2276 return;
2277 case ISD::BR: {
2278 Select(N.getOperand(0));
2279 MachineBasicBlock *Dest =
2280 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2281 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2282 return;
2283 }
2284
2285 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002286 MachineBasicBlock *Dest =
2287 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002288
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002289 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2290 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002291 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2292 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2293 Select(N.getOperand(0));
2294 Tmp1 = SelectExpr(N.getOperand(1));
2295 } else {
2296 Tmp1 = SelectExpr(N.getOperand(1));
2297 Select(N.getOperand(0));
2298 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002299 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2300 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2301 }
Chris Lattner11333092005-01-11 03:11:44 +00002302
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002303 return;
2304 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002305
2306 case ISD::EXTLOAD:
2307 case ISD::SEXTLOAD:
2308 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002309 case ISD::LOAD:
2310 case ISD::CALL:
2311 case ISD::DYNAMIC_STACKALLOC:
2312 SelectExpr(N);
2313 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002314
2315 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
2316 // On X86, we can represent all types except for Bool and Float natively.
2317 X86AddressMode AM;
2318 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00002319 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
2320 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
2321 && "Unsupported TRUNCSTORE for this target!");
2322
2323 if (StoredTy == MVT::i16) {
2324 // FIXME: This is here just to allow testing. X86 doesn't really have a
2325 // TRUNCSTORE i16 operation, but this is required for targets that do not
2326 // have 16-bit integer registers. We occasionally disable 16-bit integer
2327 // registers to test the promotion code.
2328 Select(N.getOperand(0));
2329 Tmp1 = SelectExpr(N.getOperand(1));
2330 SelectAddress(N.getOperand(2), AM);
2331
2332 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2333 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
2334 return;
2335 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002336
2337 // Store of constant bool?
2338 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2339 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2340 Select(N.getOperand(0));
2341 SelectAddress(N.getOperand(2), AM);
2342 } else {
2343 SelectAddress(N.getOperand(2), AM);
2344 Select(N.getOperand(0));
2345 }
2346 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
2347 return;
2348 }
2349
2350 switch (StoredTy) {
2351 default: assert(0 && "Cannot truncstore this type!");
2352 case MVT::i1: Opc = X86::MOV8mr; break;
2353 case MVT::f32: Opc = X86::FST32m; break;
2354 }
2355
2356 std::vector<std::pair<unsigned, unsigned> > RP;
2357 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2358 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2359 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2360 std::sort(RP.begin(), RP.end());
2361
2362 for (unsigned i = 0; i != 3; ++i)
2363 switch (RP[2-i].second) {
2364 default: assert(0 && "Unknown operand number!");
2365 case 0: Select(N.getOperand(0)); break;
2366 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
2367 case 2: SelectAddress(N.getOperand(2), AM); break;
2368 }
2369
2370 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2371 return;
2372 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002373 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002374 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002375
2376 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2377 Opc = 0;
2378 switch (CN->getValueType(0)) {
2379 default: assert(0 && "Invalid type for operation!");
2380 case MVT::i1:
2381 case MVT::i8: Opc = X86::MOV8mi; break;
2382 case MVT::i16: Opc = X86::MOV16mi; break;
2383 case MVT::i32: Opc = X86::MOV32mi; break;
2384 case MVT::f32:
2385 case MVT::f64: break;
2386 }
2387 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002388 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2389 Select(N.getOperand(0));
2390 SelectAddress(N.getOperand(2), AM);
2391 } else {
2392 SelectAddress(N.getOperand(2), AM);
2393 Select(N.getOperand(0));
2394 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002395 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2396 return;
2397 }
2398 }
Chris Lattner837caa72005-01-11 23:21:30 +00002399
2400 // Check to see if this is a load/op/store combination.
2401 if (N.getOperand(1).Val->hasOneUse() &&
Chris Lattner42928302005-01-12 03:16:09 +00002402 isFoldableLoad(N.getOperand(0).getValue(0)) &&
2403 !MVT::isFloatingPoint(N.getOperand(0).getValue(0).getValueType())) {
Chris Lattner837caa72005-01-11 23:21:30 +00002404 SDOperand TheLoad = N.getOperand(0).getValue(0);
2405 // Check to see if we are loading the same pointer that we're storing to.
2406 if (TheLoad.getOperand(1) == N.getOperand(2)) {
2407 // See if the stored value is a simple binary operator that uses the
2408 // load as one of its operands.
2409 SDOperand Op = N.getOperand(1);
2410 if (Op.Val->getNumOperands() == 2 &&
2411 (Op.getOperand(0) == TheLoad || Op.getOperand(1) == TheLoad)) {
2412 // Finally, check to see if this is one of the ops we can handle!
2413 static const unsigned ADDTAB[] = {
2414 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002415 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
Chris Lattner837caa72005-01-11 23:21:30 +00002416 };
Chris Lattner7ea64f52005-01-12 01:28:00 +00002417 static const unsigned SUBTAB[] = {
2418 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002419 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002420 };
2421 static const unsigned ANDTAB[] = {
2422 X86::AND8mi, X86::AND16mi, X86::AND32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002423 X86::AND8mr, X86::AND16mr, X86::AND32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002424 };
2425 static const unsigned ORTAB[] = {
2426 X86::OR8mi, X86::OR16mi, X86::OR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002427 X86::OR8mr, X86::OR16mr, X86::OR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002428 };
2429 static const unsigned XORTAB[] = {
2430 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002431 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002432 };
2433 static const unsigned SHLTAB[] = {
2434 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002435 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002436 };
2437 static const unsigned SARTAB[] = {
2438 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002439 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002440 };
2441 static const unsigned SHRTAB[] = {
2442 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002443 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002444 };
Chris Lattner837caa72005-01-11 23:21:30 +00002445
2446 const unsigned *TabPtr = 0;
2447 switch (Op.getOpcode()) {
Chris Lattner7ea64f52005-01-12 01:28:00 +00002448 default: std::cerr << "CANNOT [mem] op= val: "; Op.Val->dump(); std::cerr << "\n"; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002449 case ISD::ADD: TabPtr = ADDTAB; break;
Chris Lattner7ea64f52005-01-12 01:28:00 +00002450 case ISD::SUB: TabPtr = SUBTAB; break;
2451 case ISD::AND: TabPtr = ANDTAB; break;
2452 case ISD:: OR: TabPtr = ORTAB; break;
2453 case ISD::XOR: TabPtr = XORTAB; break;
2454 case ISD::SHL: TabPtr = SHLTAB; break;
2455 case ISD::SRA: TabPtr = SARTAB; break;
2456 case ISD::SRL: TabPtr = SHRTAB; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002457 }
2458
2459 if (TabPtr) {
2460 // Handle: [mem] op= CST
2461 SDOperand Op0 = Op.getOperand(0);
2462 SDOperand Op1 = Op.getOperand(1);
2463 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner48034fd2005-01-12 05:22:07 +00002464 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
Chris Lattner837caa72005-01-11 23:21:30 +00002465 default: break;
2466 case MVT::i1:
2467 case MVT::i8: Opc = TabPtr[0]; break;
2468 case MVT::i16: Opc = TabPtr[1]; break;
2469 case MVT::i32: Opc = TabPtr[2]; break;
2470 }
2471
2472 if (Opc) {
2473 if (getRegPressure(TheLoad.getOperand(0)) >
2474 getRegPressure(TheLoad.getOperand(1))) {
2475 Select(TheLoad.getOperand(0));
2476 SelectAddress(TheLoad.getOperand(1), AM);
2477 } else {
2478 SelectAddress(TheLoad.getOperand(1), AM);
2479 Select(TheLoad.getOperand(0));
2480 }
2481
2482 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2483 return;
2484 }
2485 }
2486
2487 // If we have [mem] = V op [mem], try to turn it into:
2488 // [mem] = [mem] op V.
Chris Lattner7ea64f52005-01-12 01:28:00 +00002489 if (Op1 == TheLoad && Op.getOpcode() != ISD::SUB &&
2490 Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRA &&
2491 Op.getOpcode() != ISD::SRL)
Chris Lattner837caa72005-01-11 23:21:30 +00002492 std::swap(Op0, Op1);
2493
2494 if (Op0 == TheLoad) {
2495 switch (Op0.getValueType()) {
2496 default: break;
2497 case MVT::i1:
2498 case MVT::i8: Opc = TabPtr[3]; break;
2499 case MVT::i16: Opc = TabPtr[4]; break;
2500 case MVT::i32: Opc = TabPtr[5]; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002501 }
2502
2503 if (Opc) {
2504 Select(TheLoad.getOperand(0));
2505 SelectAddress(TheLoad.getOperand(1), AM);
2506 unsigned Reg = SelectExpr(Op1);
2507 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addReg(Reg);
2508 return;
2509 }
2510 }
Chris Lattner837caa72005-01-11 23:21:30 +00002511 }
2512 }
2513 }
2514 }
2515
2516
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002517 switch (N.getOperand(1).getValueType()) {
2518 default: assert(0 && "Cannot store this type!");
2519 case MVT::i1:
2520 case MVT::i8: Opc = X86::MOV8mr; break;
2521 case MVT::i16: Opc = X86::MOV16mr; break;
2522 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002523 case MVT::f32: Opc = X86::FST32m; break;
2524 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002525 }
Chris Lattner11333092005-01-11 03:11:44 +00002526
2527 std::vector<std::pair<unsigned, unsigned> > RP;
2528 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2529 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2530 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2531 std::sort(RP.begin(), RP.end());
2532
2533 for (unsigned i = 0; i != 3; ++i)
2534 switch (RP[2-i].second) {
2535 default: assert(0 && "Unknown operand number!");
2536 case 0: Select(N.getOperand(0)); break;
2537 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002538 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002539 }
2540
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002541 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2542 return;
2543 }
2544 case ISD::ADJCALLSTACKDOWN:
2545 case ISD::ADJCALLSTACKUP:
2546 Select(N.getOperand(0));
2547 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2548
2549 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2550 X86::ADJCALLSTACKUP;
2551 BuildMI(BB, Opc, 1).addImm(Tmp1);
2552 return;
Chris Lattner989de032005-01-11 06:14:36 +00002553 case ISD::MEMSET: {
2554 Select(N.getOperand(0)); // Select the chain.
2555 unsigned Align =
2556 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2557 if (Align == 0) Align = 1;
2558
2559 // Turn the byte code into # iterations
2560 unsigned CountReg;
2561 unsigned Opcode;
2562 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2563 unsigned Val = ValC->getValue() & 255;
2564
2565 // If the value is a constant, then we can potentially use larger sets.
2566 switch (Align & 3) {
2567 case 2: // WORD aligned
2568 CountReg = MakeReg(MVT::i32);
2569 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2570 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2571 } else {
2572 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2573 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2574 }
2575 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2576 Opcode = X86::REP_STOSW;
2577 break;
2578 case 0: // DWORD aligned
2579 CountReg = MakeReg(MVT::i32);
2580 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2581 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2582 } else {
2583 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2584 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2585 }
2586 Val = (Val << 8) | Val;
2587 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2588 Opcode = X86::REP_STOSD;
2589 break;
2590 default: // BYTE aligned
2591 CountReg = SelectExpr(Node->getOperand(3));
2592 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2593 Opcode = X86::REP_STOSB;
2594 break;
2595 }
2596 } else {
2597 // If it's not a constant value we are storing, just fall back. We could
2598 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2599 unsigned ValReg = SelectExpr(Node->getOperand(2));
2600 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2601 CountReg = SelectExpr(Node->getOperand(3));
2602 Opcode = X86::REP_STOSB;
2603 }
2604
2605 // No matter what the alignment is, we put the source in ESI, the
2606 // destination in EDI, and the count in ECX.
2607 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2608 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2609 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2610 BuildMI(BB, Opcode, 0);
2611 return;
2612 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002613 case ISD::MEMCPY:
2614 Select(N.getOperand(0)); // Select the chain.
2615 unsigned Align =
2616 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2617 if (Align == 0) Align = 1;
2618
2619 // Turn the byte code into # iterations
2620 unsigned CountReg;
2621 unsigned Opcode;
2622 switch (Align & 3) {
2623 case 2: // WORD aligned
2624 CountReg = MakeReg(MVT::i32);
2625 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2626 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2627 } else {
2628 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2629 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2630 }
2631 Opcode = X86::REP_MOVSW;
2632 break;
2633 case 0: // DWORD aligned
2634 CountReg = MakeReg(MVT::i32);
2635 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2636 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2637 } else {
2638 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2639 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2640 }
2641 Opcode = X86::REP_MOVSD;
2642 break;
2643 default: // BYTE aligned
2644 CountReg = SelectExpr(Node->getOperand(3));
2645 Opcode = X86::REP_MOVSB;
2646 break;
2647 }
2648
2649 // No matter what the alignment is, we put the source in ESI, the
2650 // destination in EDI, and the count in ECX.
2651 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2652 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2653 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2654 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2655 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2656 BuildMI(BB, Opcode, 0);
2657 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002658 }
2659 assert(0 && "Should not be reached!");
2660}
2661
2662
2663/// createX86PatternInstructionSelector - This pass converts an LLVM function
2664/// into a machine code representation using pattern matching and a machine
2665/// description file.
2666///
2667FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2668 return new ISel(TM);
2669}