blob: dacd5133c1d127ec28824f0316e6f3f7fd2431d4 [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>
30using namespace llvm;
31
32//===----------------------------------------------------------------------===//
33// X86TargetLowering - X86 Implementation of the TargetLowering interface
34namespace {
35 class X86TargetLowering : public TargetLowering {
36 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000037 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000038 public:
39 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
40 // Set up the TargetLowering object.
41 addRegisterClass(MVT::i8, X86::R8RegisterClass);
42 addRegisterClass(MVT::i16, X86::R16RegisterClass);
43 addRegisterClass(MVT::i32, X86::R32RegisterClass);
44 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
45
46 // FIXME: Eliminate these two classes when legalize can handle promotions
47 // well.
48 addRegisterClass(MVT::i1, X86::R8RegisterClass);
49 addRegisterClass(MVT::f32, X86::RFPRegisterClass);
50
51 computeRegisterProperties();
Chris Lattner795069d2005-01-11 05:57:36 +000052
Chris Lattner795069d2005-01-11 05:57:36 +000053 setOperationUnsupported(ISD::MEMMOVE, MVT::Other);
54
Chris Lattner8acb1ba2005-01-07 07:49:41 +000055 setOperationUnsupported(ISD::MUL, MVT::i8);
56 setOperationUnsupported(ISD::SELECT, MVT::i1);
57 setOperationUnsupported(ISD::SELECT, MVT::i8);
58
59 addLegalFPImmediate(+0.0); // FLD0
60 addLegalFPImmediate(+1.0); // FLD1
61 addLegalFPImmediate(-0.0); // FLD0/FCHS
62 addLegalFPImmediate(-1.0); // FLD1/FCHS
63 }
64
65 /// LowerArguments - This hook must be implemented to indicate how we should
66 /// lower the arguments for the specified function, into the specified DAG.
67 virtual std::vector<SDOperand>
68 LowerArguments(Function &F, SelectionDAG &DAG);
69
70 /// LowerCallTo - This hook lowers an abstract call to a function into an
71 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000072 virtual std::pair<SDOperand, SDOperand>
73 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
74 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000075
76 virtual std::pair<SDOperand, SDOperand>
77 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
78
79 virtual std::pair<SDOperand,SDOperand>
80 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
81 const Type *ArgTy, SelectionDAG &DAG);
82
83 virtual std::pair<SDOperand, SDOperand>
84 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
85 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000086 };
87}
88
89
90std::vector<SDOperand>
91X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
92 std::vector<SDOperand> ArgValues;
93
94 // Add DAG nodes to load the arguments... On entry to a function on the X86,
95 // the stack frame looks like this:
96 //
97 // [ESP] -- return address
98 // [ESP + 4] -- first argument (leftmost lexically)
99 // [ESP + 8] -- second argument, if first argument is four bytes in size
100 // ...
101 //
102 MachineFunction &MF = DAG.getMachineFunction();
103 MachineFrameInfo *MFI = MF.getFrameInfo();
104
105 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
106 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
107 MVT::ValueType ObjectVT = getValueType(I->getType());
108 unsigned ArgIncrement = 4;
109 unsigned ObjSize;
110 switch (ObjectVT) {
111 default: assert(0 && "Unhandled argument type!");
112 case MVT::i1:
113 case MVT::i8: ObjSize = 1; break;
114 case MVT::i16: ObjSize = 2; break;
115 case MVT::i32: ObjSize = 4; break;
116 case MVT::i64: ObjSize = ArgIncrement = 8; break;
117 case MVT::f32: ObjSize = 4; break;
118 case MVT::f64: ObjSize = ArgIncrement = 8; break;
119 }
120 // Create the frame index object for this incoming parameter...
121 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
122
123 // Create the SelectionDAG nodes corresponding to a load from this parameter
124 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
125
126 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
127 // dead loads.
128 SDOperand ArgValue;
129 if (!I->use_empty())
130 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
131 else {
132 if (MVT::isInteger(ObjectVT))
133 ArgValue = DAG.getConstant(0, ObjectVT);
134 else
135 ArgValue = DAG.getConstantFP(0, ObjectVT);
136 }
137 ArgValues.push_back(ArgValue);
138
139 ArgOffset += ArgIncrement; // Move on to the next argument...
140 }
141
142 // If the function takes variable number of arguments, make a frame index for
143 // the start of the first vararg value... for expansion of llvm.va_start.
144 if (F.isVarArg())
145 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000146 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000147 return ArgValues;
148}
149
Chris Lattner5188ad72005-01-08 19:28:19 +0000150std::pair<SDOperand, SDOperand>
151X86TargetLowering::LowerCallTo(SDOperand Chain,
152 const Type *RetTy, SDOperand Callee,
153 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000154 // Count how many bytes are to be pushed on the stack.
155 unsigned NumBytes = 0;
156
157 if (Args.empty()) {
158 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000159 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
160 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000161 } else {
162 for (unsigned i = 0, e = Args.size(); i != e; ++i)
163 switch (getValueType(Args[i].second)) {
164 default: assert(0 && "Unknown value type!");
165 case MVT::i1:
166 case MVT::i8:
167 case MVT::i16:
168 case MVT::i32:
169 case MVT::f32:
170 NumBytes += 4;
171 break;
172 case MVT::i64:
173 case MVT::f64:
174 NumBytes += 8;
175 break;
176 }
177
Chris Lattner5188ad72005-01-08 19:28:19 +0000178 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
179 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000180
181 // Arguments go on the stack in reverse order, as specified by the ABI.
182 unsigned ArgOffset = 0;
183 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32);
184 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
185 unsigned ArgReg;
186 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
187 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
188
189 switch (getValueType(Args[i].second)) {
190 default: assert(0 && "Unexpected ValueType for argument!");
191 case MVT::i1:
192 case MVT::i8:
193 case MVT::i16:
194 // Promote the integer to 32 bits. If the input type is signed use a
195 // sign extend, otherwise use a zero extend.
196 if (Args[i].second->isSigned())
197 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
198 else
199 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
200
201 // FALL THROUGH
202 case MVT::i32:
203 case MVT::f32:
204 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000205 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
206 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000207 ArgOffset += 4;
208 break;
209 case MVT::i64:
210 case MVT::f64:
211 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000212 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
213 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000214 ArgOffset += 8;
215 break;
216 }
217 }
218 }
219
220 std::vector<MVT::ValueType> RetVals;
221 MVT::ValueType RetTyVT = getValueType(RetTy);
222 if (RetTyVT != MVT::isVoid)
223 RetVals.push_back(RetTyVT);
224 RetVals.push_back(MVT::Other);
225
Chris Lattner5188ad72005-01-08 19:28:19 +0000226 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000227 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000228 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
229 DAG.getConstant(NumBytes, getPointerTy()));
230 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000231}
232
Chris Lattner14824582005-01-09 00:01:27 +0000233std::pair<SDOperand, SDOperand>
234X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
235 // vastart just returns the address of the VarArgsFrameIndex slot.
236 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
237}
238
239std::pair<SDOperand,SDOperand> X86TargetLowering::
240LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
241 const Type *ArgTy, SelectionDAG &DAG) {
242 MVT::ValueType ArgVT = getValueType(ArgTy);
243 SDOperand Result;
244 if (!isVANext) {
245 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
246 } else {
247 unsigned Amt;
248 if (ArgVT == MVT::i32)
249 Amt = 4;
250 else {
251 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
252 "Other types should have been promoted for varargs!");
253 Amt = 8;
254 }
255 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
256 DAG.getConstant(Amt, VAList.getValueType()));
257 }
258 return std::make_pair(Result, Chain);
259}
260
261
262std::pair<SDOperand, SDOperand> X86TargetLowering::
263LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
264 SelectionDAG &DAG) {
265 SDOperand Result;
266 if (Depth) // Depths > 0 not supported yet!
267 Result = DAG.getConstant(0, getPointerTy());
268 else {
269 if (ReturnAddrIndex == 0) {
270 // Set up a frame object for the return address.
271 MachineFunction &MF = DAG.getMachineFunction();
272 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
273 }
274
275 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
276
277 if (!isFrameAddress)
278 // Just load the return address
279 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
280 else
281 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
282 DAG.getConstant(4, MVT::i32));
283 }
284 return std::make_pair(Result, Chain);
285}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000286
287
288
289
290
291namespace {
292 Statistic<>
293 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
294
295 //===--------------------------------------------------------------------===//
296 /// ISel - X86 specific code to select X86 machine instructions for
297 /// SelectionDAG operations.
298 ///
299 class ISel : public SelectionDAGISel {
300 /// ContainsFPCode - Every instruction we select that uses or defines a FP
301 /// register should set this to true.
302 bool ContainsFPCode;
303
304 /// X86Lowering - This object fully describes how to lower LLVM code to an
305 /// X86-specific SelectionDAG.
306 X86TargetLowering X86Lowering;
307
Chris Lattner11333092005-01-11 03:11:44 +0000308 /// RegPressureMap - This keeps an approximate count of the number of
309 /// registers required to evaluate each node in the graph.
310 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000311
312 /// ExprMap - As shared expressions are codegen'd, we keep track of which
313 /// vreg the value is produced in, so we only emit one copy of each compiled
314 /// tree.
315 std::map<SDOperand, unsigned> ExprMap;
316 std::set<SDOperand> LoweredTokens;
317
318 public:
319 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
320 }
321
Chris Lattner11333092005-01-11 03:11:44 +0000322 unsigned getRegPressure(SDOperand O) {
323 return RegPressureMap[O.Val];
324 }
325 unsigned ComputeRegPressure(SDOperand O);
326
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000327 /// InstructionSelectBasicBlock - This callback is invoked by
328 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
329 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
330 // While we're doing this, keep track of whether we see any FP code for
331 // FP_REG_KILL insertion.
332 ContainsFPCode = false;
333
Chris Lattner11333092005-01-11 03:11:44 +0000334 // Compute the RegPressureMap, which is an approximation for the number of
335 // registers required to compute each node.
336 ComputeRegPressure(DAG.getRoot());
337
338 //DAG.viewGraph();
339
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000340 // Codegen the basic block.
341 Select(DAG.getRoot());
342
343 // Insert FP_REG_KILL instructions into basic blocks that need them. This
344 // only occurs due to the floating point stackifier not being aggressive
345 // enough to handle arbitrary global stackification.
346 //
347 // Currently we insert an FP_REG_KILL instruction into each block that
348 // uses or defines a floating point virtual register.
349 //
350 // When the global register allocators (like linear scan) finally update
351 // live variable analysis, we can keep floating point values in registers
352 // across basic blocks. This will be a huge win, but we are waiting on
353 // the global allocators before we can do this.
354 //
355 if (ContainsFPCode && BB->succ_size()) {
356 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
357 ++NumFPKill;
358 }
359
360 // Clear state used for selection.
361 ExprMap.clear();
362 LoweredTokens.clear();
Chris Lattner11333092005-01-11 03:11:44 +0000363 RegPressureMap.clear();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000364 }
365
Chris Lattnera5ade062005-01-11 21:19:59 +0000366 bool isFoldableLoad(SDOperand Op);
367 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
368
369
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000370 void EmitCMP(SDOperand LHS, SDOperand RHS);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000371 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000372 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
373 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000374 unsigned SelectExpr(SDOperand N);
375 bool SelectAddress(SDOperand N, X86AddressMode &AM);
376 void Select(SDOperand N);
377 };
378}
379
Chris Lattner11333092005-01-11 03:11:44 +0000380// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
381// for the number of registers required to compute each node. This is basically
382// computing a generalized form of the Sethi-Ullman number for each node.
383unsigned ISel::ComputeRegPressure(SDOperand O) {
384 SDNode *N = O.Val;
385 unsigned &Result = RegPressureMap[N];
386 if (Result) return Result;
387
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000388 // FIXME: Should operations like CALL (which clobber lots o regs) have a
389 // higher fixed cost??
390
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000391 if (N->getNumOperands() == 0) {
392 Result = 1;
393 } else {
394 unsigned MaxRegUse = 0;
395 unsigned NumExtraMaxRegUsers = 0;
396 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
397 unsigned Regs;
398 if (N->getOperand(i).getOpcode() == ISD::Constant)
399 Regs = 0;
400 else
401 Regs = ComputeRegPressure(N->getOperand(i));
402 if (Regs > MaxRegUse) {
403 MaxRegUse = Regs;
404 NumExtraMaxRegUsers = 0;
405 } else if (Regs == MaxRegUse &&
406 N->getOperand(i).getValueType() != MVT::Other) {
407 ++NumExtraMaxRegUsers;
408 }
Chris Lattner11333092005-01-11 03:11:44 +0000409 }
Chris Lattner11333092005-01-11 03:11:44 +0000410
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000411 Result = MaxRegUse+NumExtraMaxRegUsers;
412 }
Chris Lattner837caa72005-01-11 23:21:30 +0000413 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000414 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000415}
416
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000417/// SelectAddress - Add the specified node to the specified addressing mode,
418/// returning true if it cannot be done.
419bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
420 switch (N.getOpcode()) {
421 default: break;
422 case ISD::FrameIndex:
423 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
424 AM.BaseType = X86AddressMode::FrameIndexBase;
425 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
426 return false;
427 }
428 break;
429 case ISD::GlobalAddress:
430 if (AM.GV == 0) {
431 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
432 return false;
433 }
434 break;
435 case ISD::Constant:
436 AM.Disp += cast<ConstantSDNode>(N)->getValue();
437 return false;
438 case ISD::SHL:
439 if (AM.IndexReg == 0 || AM.Scale == 1)
440 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
441 unsigned Val = CN->getValue();
442 if (Val == 1 || Val == 2 || Val == 3) {
443 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000444 SDOperand ShVal = N.Val->getOperand(0);
445
446 // Okay, we know that we have a scale by now. However, if the scaled
447 // value is an add of something and a constant, we can fold the
448 // constant into the disp field here.
449 if (ShVal.Val->getOpcode() == ISD::ADD &&
450 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
451 AM.IndexReg = SelectExpr(ShVal.Val->getOperand(0));
452 ConstantSDNode *AddVal =
453 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
454 AM.Disp += AddVal->getValue() << Val;
455 } else {
456 AM.IndexReg = SelectExpr(ShVal);
457 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000458 return false;
459 }
460 }
461 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000462 case ISD::MUL:
463 // X*[3,5,9] -> X+X*[2,4,8]
464 if (AM.IndexReg == 0 && AM.BaseType == X86AddressMode::RegBase &&
465 AM.Base.Reg == 0)
466 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
467 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
468 AM.Scale = unsigned(CN->getValue())-1;
469
470 SDOperand MulVal = N.Val->getOperand(0);
471 unsigned Reg;
472
473 // Okay, we know that we have a scale by now. However, if the scaled
474 // value is an add of something and a constant, we can fold the
475 // constant into the disp field here.
476 if (MulVal.Val->getOpcode() == ISD::ADD &&
477 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
478 Reg = SelectExpr(MulVal.Val->getOperand(0));
479 ConstantSDNode *AddVal =
480 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
481 AM.Disp += AddVal->getValue() * CN->getValue();
482 } else {
483 Reg = SelectExpr(N.Val->getOperand(0));
484 }
485
486 AM.IndexReg = AM.Base.Reg = Reg;
487 return false;
488 }
489 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000490
491 case ISD::ADD: {
492 X86AddressMode Backup = AM;
493 if (!SelectAddress(N.Val->getOperand(0), AM) &&
494 !SelectAddress(N.Val->getOperand(1), AM))
495 return false;
496 AM = Backup;
497 break;
498 }
499 }
500
Chris Lattnera95589b2005-01-11 04:40:19 +0000501 // Is the base register already occupied?
502 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
503 // If so, check to see if the scale index register is set.
504 if (AM.IndexReg == 0) {
505 AM.IndexReg = SelectExpr(N);
506 AM.Scale = 1;
507 return false;
508 }
509
510 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000511 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000512 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000513
514 // Default, generate it as a register.
515 AM.BaseType = X86AddressMode::RegBase;
516 AM.Base.Reg = SelectExpr(N);
517 return false;
518}
519
520/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
521/// assuming that the temporary registers are in the 8-bit register class.
522///
523/// Tmp1 = setcc1
524/// Tmp2 = setcc2
525/// DestReg = logicalop Tmp1, Tmp2
526///
527static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
528 unsigned SetCC2, unsigned LogicalOp,
529 unsigned DestReg) {
530 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
531 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
532 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
533 BuildMI(BB, SetCC1, 0, Tmp1);
534 BuildMI(BB, SetCC2, 0, Tmp2);
535 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
536}
537
538/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
539/// condition codes match the specified SetCCOpcode. Note that some conditions
540/// require multiple instructions to generate the correct value.
541static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
542 ISD::CondCode SetCCOpcode, bool isFP) {
543 unsigned Opc;
544 if (!isFP) {
545 switch (SetCCOpcode) {
546 default: assert(0 && "Illegal integer SetCC!");
547 case ISD::SETEQ: Opc = X86::SETEr; break;
548 case ISD::SETGT: Opc = X86::SETGr; break;
549 case ISD::SETGE: Opc = X86::SETGEr; break;
550 case ISD::SETLT: Opc = X86::SETLr; break;
551 case ISD::SETLE: Opc = X86::SETLEr; break;
552 case ISD::SETNE: Opc = X86::SETNEr; break;
553 case ISD::SETULT: Opc = X86::SETBr; break;
554 case ISD::SETUGT: Opc = X86::SETAr; break;
555 case ISD::SETULE: Opc = X86::SETBEr; break;
556 case ISD::SETUGE: Opc = X86::SETAEr; break;
557 }
558 } else {
559 // On a floating point condition, the flags are set as follows:
560 // ZF PF CF op
561 // 0 | 0 | 0 | X > Y
562 // 0 | 0 | 1 | X < Y
563 // 1 | 0 | 0 | X == Y
564 // 1 | 1 | 1 | unordered
565 //
566 switch (SetCCOpcode) {
567 default: assert(0 && "Invalid FP setcc!");
568 case ISD::SETUEQ:
569 case ISD::SETEQ:
570 Opc = X86::SETEr; // True if ZF = 1
571 break;
572 case ISD::SETOGT:
573 case ISD::SETGT:
574 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
575 break;
576 case ISD::SETOGE:
577 case ISD::SETGE:
578 Opc = X86::SETAEr; // True if CF = 0
579 break;
580 case ISD::SETULT:
581 case ISD::SETLT:
582 Opc = X86::SETBr; // True if CF = 1
583 break;
584 case ISD::SETULE:
585 case ISD::SETLE:
586 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
587 break;
588 case ISD::SETONE:
589 case ISD::SETNE:
590 Opc = X86::SETNEr; // True if ZF = 0
591 break;
592 case ISD::SETUO:
593 Opc = X86::SETPr; // True if PF = 1
594 break;
595 case ISD::SETO:
596 Opc = X86::SETNPr; // True if PF = 0
597 break;
598 case ISD::SETOEQ: // !PF & ZF
599 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
600 return;
601 case ISD::SETOLT: // !PF & CF
602 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
603 return;
604 case ISD::SETOLE: // !PF & (CF || ZF)
605 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
606 return;
607 case ISD::SETUGT: // PF | (!ZF & !CF)
608 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
609 return;
610 case ISD::SETUGE: // PF | !CF
611 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
612 return;
613 case ISD::SETUNE: // PF | !ZF
614 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
615 return;
616 }
617 }
618 BuildMI(BB, Opc, 0, DestReg);
619}
620
621
622/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
623/// the Dest block if the Cond condition is true. If we cannot fold this
624/// condition into the branch, return true.
625///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000626bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
627 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000628 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
629 // B) using two conditional branches instead of one condbr, two setcc's, and
630 // an or.
631 if ((Cond.getOpcode() == ISD::OR ||
632 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
633 // And and or set the flags for us, so there is no need to emit a TST of the
634 // result. It is only safe to do this if there is only a single use of the
635 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000636 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000637 SelectExpr(Cond);
638 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
639 return false;
640 }
641
642 // Codegen br not C -> JE.
643 if (Cond.getOpcode() == ISD::XOR)
644 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
645 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000646 unsigned CondR;
647 if (getRegPressure(Chain) > getRegPressure(Cond)) {
648 Select(Chain);
649 CondR = SelectExpr(Cond.Val->getOperand(0));
650 } else {
651 CondR = SelectExpr(Cond.Val->getOperand(0));
652 Select(Chain);
653 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000654 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
655 BuildMI(BB, X86::JE, 1).addMBB(Dest);
656 return false;
657 }
658
659 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
660 if (SetCC == 0)
661 return true; // Can only handle simple setcc's so far.
662
663 unsigned Opc;
664
665 // Handle integer conditions first.
666 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
667 switch (SetCC->getCondition()) {
668 default: assert(0 && "Illegal integer SetCC!");
669 case ISD::SETEQ: Opc = X86::JE; break;
670 case ISD::SETGT: Opc = X86::JG; break;
671 case ISD::SETGE: Opc = X86::JGE; break;
672 case ISD::SETLT: Opc = X86::JL; break;
673 case ISD::SETLE: Opc = X86::JLE; break;
674 case ISD::SETNE: Opc = X86::JNE; break;
675 case ISD::SETULT: Opc = X86::JB; break;
676 case ISD::SETUGT: Opc = X86::JA; break;
677 case ISD::SETULE: Opc = X86::JBE; break;
678 case ISD::SETUGE: Opc = X86::JAE; break;
679 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000680 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000681 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
682 BuildMI(BB, Opc, 1).addMBB(Dest);
683 return false;
684 }
685
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000686 unsigned Opc2 = 0; // Second branch if needed.
687
688 // On a floating point condition, the flags are set as follows:
689 // ZF PF CF op
690 // 0 | 0 | 0 | X > Y
691 // 0 | 0 | 1 | X < Y
692 // 1 | 0 | 0 | X == Y
693 // 1 | 1 | 1 | unordered
694 //
695 switch (SetCC->getCondition()) {
696 default: assert(0 && "Invalid FP setcc!");
697 case ISD::SETUEQ:
698 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
699 case ISD::SETOGT:
700 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
701 case ISD::SETOGE:
702 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
703 case ISD::SETULT:
704 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
705 case ISD::SETULE:
706 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
707 case ISD::SETONE:
708 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
709 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
710 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
711 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
712 Opc = X86::JA; // ZF = 0 & CF = 0
713 Opc2 = X86::JP; // PF = 1
714 break;
715 case ISD::SETUGE: // PF = 1 | CF = 0
716 Opc = X86::JAE; // CF = 0
717 Opc2 = X86::JP; // PF = 1
718 break;
719 case ISD::SETUNE: // PF = 1 | ZF = 0
720 Opc = X86::JNE; // ZF = 0
721 Opc2 = X86::JP; // PF = 1
722 break;
723 case ISD::SETOEQ: // PF = 0 & ZF = 1
724 //X86::JNP, X86::JE
725 //X86::AND8rr
726 return true; // FIXME: Emit more efficient code for this branch.
727 case ISD::SETOLT: // PF = 0 & CF = 1
728 //X86::JNP, X86::JB
729 //X86::AND8rr
730 return true; // FIXME: Emit more efficient code for this branch.
731 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
732 //X86::JNP, X86::JBE
733 //X86::AND8rr
734 return true; // FIXME: Emit more efficient code for this branch.
735 }
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 if (Opc2)
741 BuildMI(BB, Opc2, 1).addMBB(Dest);
742 return false;
743}
744
Chris Lattner24aad1b2005-01-10 22:10:13 +0000745/// EmitSelectCC - Emit code into BB that performs a select operation between
746/// the two registers RTrue and RFalse, generating a result into RDest. Return
747/// true if the fold cannot be performed.
748///
749void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
750 unsigned RTrue, unsigned RFalse, unsigned RDest) {
751 enum Condition {
752 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
753 NOT_SET
754 } CondCode = NOT_SET;
755
756 static const unsigned CMOVTAB16[] = {
757 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
758 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
759 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
760 };
761 static const unsigned CMOVTAB32[] = {
762 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
763 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
764 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
765 };
766 static const unsigned CMOVTABFP[] = {
767 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
768 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
769 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
770 };
771
772 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
773 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
774 switch (SetCC->getCondition()) {
775 default: assert(0 && "Unknown integer comparison!");
776 case ISD::SETEQ: CondCode = EQ; break;
777 case ISD::SETGT: CondCode = GT; break;
778 case ISD::SETGE: CondCode = GE; break;
779 case ISD::SETLT: CondCode = LT; break;
780 case ISD::SETLE: CondCode = LE; break;
781 case ISD::SETNE: CondCode = NE; break;
782 case ISD::SETULT: CondCode = B; break;
783 case ISD::SETUGT: CondCode = A; break;
784 case ISD::SETULE: CondCode = BE; break;
785 case ISD::SETUGE: CondCode = AE; break;
786 }
787 } else {
788 // On a floating point condition, the flags are set as follows:
789 // ZF PF CF op
790 // 0 | 0 | 0 | X > Y
791 // 0 | 0 | 1 | X < Y
792 // 1 | 0 | 0 | X == Y
793 // 1 | 1 | 1 | unordered
794 //
795 switch (SetCC->getCondition()) {
796 default: assert(0 && "Unknown FP comparison!");
797 case ISD::SETUEQ:
798 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
799 case ISD::SETOGT:
800 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
801 case ISD::SETOGE:
802 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
803 case ISD::SETULT:
804 case ISD::SETLT: CondCode = B; break; // True if CF = 1
805 case ISD::SETULE:
806 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
807 case ISD::SETONE:
808 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
809 case ISD::SETUO: CondCode = P; break; // True if PF = 1
810 case ISD::SETO: CondCode = NP; break; // True if PF = 0
811 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
812 case ISD::SETUGE: // PF = 1 | CF = 0
813 case ISD::SETUNE: // PF = 1 | ZF = 0
814 case ISD::SETOEQ: // PF = 0 & ZF = 1
815 case ISD::SETOLT: // PF = 0 & CF = 1
816 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
817 // We cannot emit this comparison as a single cmov.
818 break;
819 }
820 }
821 }
822
823 unsigned Opc = 0;
824 if (CondCode != NOT_SET) {
825 switch (SVT) {
826 default: assert(0 && "Cannot select this type!");
827 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
828 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
829 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000830 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000831 }
832 }
833
834 // Finally, if we weren't able to fold this, just emit the condition and test
835 // it.
836 if (CondCode == NOT_SET || Opc == 0) {
837 // Get the condition into the zero flag.
838 unsigned CondReg = SelectExpr(Cond);
839 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
840
841 switch (SVT) {
842 default: assert(0 && "Cannot select this type!");
843 case MVT::i16: Opc = X86::CMOVE16rr; break;
844 case MVT::i32: Opc = X86::CMOVE32rr; break;
845 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000846 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000847 }
848 } else {
849 // FIXME: CMP R, 0 -> TEST R, R
850 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000851 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000852 }
853 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
854}
855
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000856void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000857 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000858 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
859 Opc = 0;
Chris Lattneref6806c2005-01-12 02:02:48 +0000860 if (isFoldableLoad(LHS)) {
861 switch (RHS.getValueType()) {
862 default: break;
863 case MVT::i1:
864 case MVT::i8: Opc = X86::CMP8mi; break;
865 case MVT::i16: Opc = X86::CMP16mi; break;
866 case MVT::i32: Opc = X86::CMP32mi; break;
867 }
868 if (Opc) {
869 X86AddressMode AM;
870 EmitFoldedLoad(LHS, AM);
871 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
872 return;
873 }
874 }
875
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000876 switch (RHS.getValueType()) {
877 default: break;
878 case MVT::i1:
879 case MVT::i8: Opc = X86::CMP8ri; break;
880 case MVT::i16: Opc = X86::CMP16ri; break;
881 case MVT::i32: Opc = X86::CMP32ri; break;
882 }
883 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000884 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000885 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
886 return;
887 }
888 }
889
Chris Lattneref6806c2005-01-12 02:02:48 +0000890 Opc = 0;
891 if (isFoldableLoad(LHS)) {
892 switch (RHS.getValueType()) {
893 default: break;
894 case MVT::i1:
895 case MVT::i8: Opc = X86::CMP8mr; break;
896 case MVT::i16: Opc = X86::CMP16mr; break;
897 case MVT::i32: Opc = X86::CMP32mr; break;
898 }
899 if (Opc) {
900 X86AddressMode AM;
901 unsigned Reg;
902 if (getRegPressure(LHS) > getRegPressure(RHS)) {
903 EmitFoldedLoad(LHS, AM);
904 Reg = SelectExpr(RHS);
905 } else {
906 Reg = SelectExpr(RHS);
907 EmitFoldedLoad(LHS, AM);
908 }
909 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
910 return;
911 }
912 }
913
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000914 switch (LHS.getValueType()) {
915 default: assert(0 && "Cannot compare this value!");
916 case MVT::i1:
917 case MVT::i8: Opc = X86::CMP8rr; break;
918 case MVT::i16: Opc = X86::CMP16rr; break;
919 case MVT::i32: Opc = X86::CMP32rr; break;
920 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000921 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000922 }
Chris Lattner11333092005-01-11 03:11:44 +0000923 unsigned Tmp1, Tmp2;
924 if (getRegPressure(LHS) > getRegPressure(RHS)) {
925 Tmp1 = SelectExpr(LHS);
926 Tmp2 = SelectExpr(RHS);
927 } else {
928 Tmp2 = SelectExpr(RHS);
929 Tmp1 = SelectExpr(LHS);
930 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000931 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
932}
933
Chris Lattnera5ade062005-01-11 21:19:59 +0000934/// isFoldableLoad - Return true if this is a load instruction that can safely
935/// be folded into an operation that uses it.
936bool ISel::isFoldableLoad(SDOperand Op) {
937 if (Op.getOpcode() != ISD::LOAD ||
938 // FIXME: currently can't fold constant pool indexes.
939 isa<ConstantPoolSDNode>(Op.getOperand(1)))
940 return false;
941
942 // If this load has already been emitted, we clearly can't fold it.
943 if (ExprMap.count(Op)) return false;
944
945 return Op.Val->use_size() == 2;
946}
947
948/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
949/// and compute the address being loaded into AM.
950void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
951 SDOperand Chain = Op.getOperand(0);
952 SDOperand Address = Op.getOperand(1);
953 if (getRegPressure(Chain) > getRegPressure(Address)) {
954 Select(Chain);
955 SelectAddress(Address, AM);
956 } else {
957 SelectAddress(Address, AM);
958 Select(Chain);
959 }
960
961 // The chain for this load is now lowered.
962 LoweredTokens.insert(SDOperand(Op.Val, 1));
963 ExprMap[SDOperand(Op.Val, 1)] = 1;
964}
965
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000966unsigned ISel::SelectExpr(SDOperand N) {
967 unsigned Result;
968 unsigned Tmp1, Tmp2, Tmp3;
969 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +0000970 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +0000971 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +0000972
Chris Lattner590d8002005-01-09 18:52:44 +0000973 if (Node->getOpcode() == ISD::CopyFromReg)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000974 // Just use the specified register as our input.
Chris Lattner590d8002005-01-09 18:52:44 +0000975 return dyn_cast<CopyRegSDNode>(Node)->getReg();
Chris Lattnera5ade062005-01-11 21:19:59 +0000976
977 unsigned &Reg = ExprMap[N];
978 if (Reg) return Reg;
979
980 if (N.getOpcode() != ISD::CALL)
981 Reg = Result = (N.getValueType() != MVT::Other) ?
982 MakeReg(N.getValueType()) : 1;
983 else {
984 // If this is a call instruction, make sure to prepare ALL of the result
985 // values as well as the chain.
986 if (Node->getNumValues() == 1)
987 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000988 else {
Chris Lattnera5ade062005-01-11 21:19:59 +0000989 Result = MakeReg(Node->getValueType(0));
990 ExprMap[N.getValue(0)] = Result;
991 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
992 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
993 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000994 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000995 }
Chris Lattnera5ade062005-01-11 21:19:59 +0000996
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000997 switch (N.getOpcode()) {
998 default:
Chris Lattner5188ad72005-01-08 19:28:19 +0000999 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001000 assert(0 && "Node not handled!\n");
1001 case ISD::FrameIndex:
1002 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1003 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1004 return Result;
1005 case ISD::ConstantPool:
1006 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1007 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1008 return Result;
1009 case ISD::ConstantFP:
1010 ContainsFPCode = true;
1011 Tmp1 = Result; // Intermediate Register
1012 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1013 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1014 Tmp1 = MakeReg(MVT::f64);
1015
1016 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1017 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1018 BuildMI(BB, X86::FLD0, 0, Tmp1);
1019 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1020 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1021 BuildMI(BB, X86::FLD1, 0, Tmp1);
1022 else
1023 assert(0 && "Unexpected constant!");
1024 if (Tmp1 != Result)
1025 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1026 return Result;
1027 case ISD::Constant:
1028 switch (N.getValueType()) {
1029 default: assert(0 && "Cannot use constants of this type!");
1030 case MVT::i1:
1031 case MVT::i8: Opc = X86::MOV8ri; break;
1032 case MVT::i16: Opc = X86::MOV16ri; break;
1033 case MVT::i32: Opc = X86::MOV32ri; break;
1034 }
1035 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1036 return Result;
1037 case ISD::GlobalAddress: {
1038 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1039 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1040 return Result;
1041 }
1042 case ISD::ExternalSymbol: {
1043 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1044 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1045 return Result;
1046 }
1047 case ISD::FP_EXTEND:
1048 Tmp1 = SelectExpr(N.getOperand(0));
1049 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001050 return Result;
1051 case ISD::ZERO_EXTEND: {
1052 int DestIs16 = N.getValueType() == MVT::i16;
1053 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001054
1055 // FIXME: This hack is here for zero extension casts from bool to i8. This
1056 // would not be needed if bools were promoted by Legalize.
1057 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001058 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001059 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1060 return Result;
1061 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001062
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001063 if (isFoldableLoad(N.getOperand(0))) {
1064 static const unsigned Opc[3] = {
1065 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1066 };
1067
1068 X86AddressMode AM;
1069 EmitFoldedLoad(N.getOperand(0), AM);
1070 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1071
1072 return Result;
1073 }
1074
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001075 static const unsigned Opc[3] = {
1076 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1077 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001078 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001079 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1080 return Result;
1081 }
1082 case ISD::SIGN_EXTEND: {
1083 int DestIs16 = N.getValueType() == MVT::i16;
1084 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1085
Chris Lattner590d8002005-01-09 18:52:44 +00001086 // FIXME: Legalize should promote bools to i8!
1087 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1088 "Sign extend from bool not implemented!");
1089
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001090 if (isFoldableLoad(N.getOperand(0))) {
1091 static const unsigned Opc[3] = {
1092 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1093 };
1094
1095 X86AddressMode AM;
1096 EmitFoldedLoad(N.getOperand(0), AM);
1097 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1098 return Result;
1099 }
1100
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001101 static const unsigned Opc[3] = {
1102 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1103 };
1104 Tmp1 = SelectExpr(N.getOperand(0));
1105 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1106 return Result;
1107 }
1108 case ISD::TRUNCATE:
1109 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1110 // a move out of AX or AL.
1111 switch (N.getOperand(0).getValueType()) {
1112 default: assert(0 && "Unknown truncate!");
1113 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1114 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1115 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1116 }
1117 Tmp1 = SelectExpr(N.getOperand(0));
1118 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1119
1120 switch (N.getValueType()) {
1121 default: assert(0 && "Unknown truncate!");
1122 case MVT::i1:
1123 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1124 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1125 }
1126 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1127 return Result;
1128
1129 case ISD::FP_ROUND:
1130 // Truncate from double to float by storing to memory as float,
1131 // then reading it back into a register.
1132
1133 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001134 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001135 Tmp1 = TLI.getTargetData().getFloatAlignment();
1136 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1137
1138 // Codegen the input.
1139 Tmp1 = SelectExpr(N.getOperand(0));
1140
1141 // Emit the store, then the reload.
1142 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1143 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001144 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001145
1146 case ISD::SINT_TO_FP:
1147 case ISD::UINT_TO_FP: {
1148 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001149 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001150
1151 // Promote the integer to a type supported by FLD. We do this because there
1152 // are no unsigned FLD instructions, so we must promote an unsigned value to
1153 // a larger signed value, then use FLD on the larger value.
1154 //
1155 MVT::ValueType PromoteType = MVT::Other;
1156 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1157 unsigned PromoteOpcode = 0;
1158 unsigned RealDestReg = Result;
1159 switch (SrcTy) {
1160 case MVT::i1:
1161 case MVT::i8:
1162 // We don't have the facilities for directly loading byte sized data from
1163 // memory (even signed). Promote it to 16 bits.
1164 PromoteType = MVT::i16;
1165 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1166 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1167 break;
1168 case MVT::i16:
1169 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1170 PromoteType = MVT::i32;
1171 PromoteOpcode = X86::MOVZX32rr16;
1172 }
1173 break;
1174 default:
1175 // Don't fild into the real destination.
1176 if (Node->getOpcode() == ISD::UINT_TO_FP)
1177 Result = MakeReg(Node->getValueType(0));
1178 break;
1179 }
1180
1181 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1182
1183 if (PromoteType != MVT::Other) {
1184 Tmp2 = MakeReg(PromoteType);
1185 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1186 SrcTy = PromoteType;
1187 Tmp1 = Tmp2;
1188 }
1189
1190 // Spill the integer to memory and reload it from there.
1191 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1192 MachineFunction *F = BB->getParent();
1193 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1194
1195 switch (SrcTy) {
1196 case MVT::i64:
1197 // FIXME: this won't work for cast [u]long to FP
1198 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1199 FrameIdx).addReg(Tmp1);
1200 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1201 FrameIdx, 4).addReg(Tmp1+1);
1202 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1203 break;
1204 case MVT::i32:
1205 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1206 FrameIdx).addReg(Tmp1);
1207 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1208 break;
1209 case MVT::i16:
1210 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1211 FrameIdx).addReg(Tmp1);
1212 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1213 break;
1214 default: break; // No promotion required.
1215 }
1216
1217 if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i32) {
1218 // If this is a cast from uint -> double, we need to be careful when if
1219 // the "sign" bit is set. If so, we don't want to make a negative number,
1220 // we want to make a positive number. Emit code to add an offset if the
1221 // sign bit is set.
1222
1223 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1224 unsigned IsNeg = MakeReg(MVT::i32);
1225 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1226
1227 // Create a CP value that has the offset in one word and 0 in the other.
1228 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1229 0x4f80000000000000ULL);
1230 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1231 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1232 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1233
1234 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1235 // We need special handling for unsigned 64-bit integer sources. If the
1236 // input number has the "sign bit" set, then we loaded it incorrectly as a
1237 // negative 64-bit number. In this case, add an offset value.
1238
1239 // Emit a test instruction to see if the dynamic input value was signed.
1240 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1241
1242 // If the sign bit is set, get a pointer to an offset, otherwise get a
1243 // pointer to a zero.
1244 MachineConstantPool *CP = F->getConstantPool();
1245 unsigned Zero = MakeReg(MVT::i32);
1246 Constant *Null = Constant::getNullValue(Type::UIntTy);
1247 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1248 CP->getConstantPoolIndex(Null));
1249 unsigned Offset = MakeReg(MVT::i32);
1250 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1251
1252 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1253 CP->getConstantPoolIndex(OffsetCst));
1254 unsigned Addr = MakeReg(MVT::i32);
1255 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1256
1257 // Load the constant for an add. FIXME: this could make an 'fadd' that
1258 // reads directly from memory, but we don't support these yet.
1259 unsigned ConstReg = MakeReg(MVT::f64);
1260 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1261
1262 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1263 }
1264 return RealDestReg;
1265 }
1266 case ISD::FP_TO_SINT:
1267 case ISD::FP_TO_UINT: {
1268 // FIXME: Most of this grunt work should be done by legalize!
1269 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1270
1271 // Change the floating point control register to use "round towards zero"
1272 // mode when truncating to an integer value.
1273 //
1274 MachineFunction *F = BB->getParent();
1275 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1276 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1277
1278 // Load the old value of the high byte of the control word...
1279 unsigned HighPartOfCW = MakeReg(MVT::i8);
1280 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1281 CWFrameIdx, 1);
1282
1283 // Set the high part to be round to zero...
1284 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1285 CWFrameIdx, 1).addImm(12);
1286
1287 // Reload the modified control word now...
1288 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1289
1290 // Restore the memory image of control word to original value
1291 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1292 CWFrameIdx, 1).addReg(HighPartOfCW);
1293
1294 // We don't have the facilities for directly storing byte sized data to
1295 // memory. Promote it to 16 bits. We also must promote unsigned values to
1296 // larger classes because we only have signed FP stores.
1297 MVT::ValueType StoreClass = Node->getValueType(0);
1298 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1299 switch (StoreClass) {
1300 case MVT::i8: StoreClass = MVT::i16; break;
1301 case MVT::i16: StoreClass = MVT::i32; break;
1302 case MVT::i32: StoreClass = MVT::i64; break;
1303 // The following treatment of cLong may not be perfectly right,
1304 // but it survives chains of casts of the form
1305 // double->ulong->double.
1306 case MVT::i64: StoreClass = MVT::i64; break;
1307 default: assert(0 && "Unknown store class!");
1308 }
1309
1310 // Spill the integer to memory and reload it from there.
1311 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1312 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1313
1314 switch (StoreClass) {
1315 default: assert(0 && "Unknown store class!");
1316 case MVT::i16:
1317 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1318 break;
1319 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001320 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001321 break;
1322 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001323 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001324 break;
1325 }
1326
1327 switch (Node->getValueType(0)) {
1328 default:
1329 assert(0 && "Unknown integer type!");
1330 case MVT::i64:
1331 // FIXME: this isn't gunna work.
1332 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1333 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1334 case MVT::i32:
1335 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1336 break;
1337 case MVT::i16:
1338 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1339 break;
1340 case MVT::i8:
1341 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1342 break;
1343 }
1344
1345 // Reload the original control word now.
1346 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1347 return Result;
1348 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001349 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001350 Op0 = N.getOperand(0);
1351 Op1 = N.getOperand(1);
1352
1353 if (isFoldableLoad(Op0))
1354 std::swap(Op0, Op1);
1355
1356 if (isFoldableLoad(Op1)) {
1357 switch (N.getValueType()) {
1358 default: assert(0 && "Cannot add this type!");
1359 case MVT::i1:
1360 case MVT::i8: Opc = X86::ADD8rm; break;
1361 case MVT::i16: Opc = X86::ADD16rm; break;
1362 case MVT::i32: Opc = X86::ADD32rm; break;
1363 case MVT::f32: Opc = X86::FADD32m; break;
1364 case MVT::f64: Opc = X86::FADD64m; break;
1365 }
1366 X86AddressMode AM;
1367 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1368 Tmp1 = SelectExpr(Op0);
1369 EmitFoldedLoad(Op1, AM);
1370 } else {
1371 EmitFoldedLoad(Op1, AM);
1372 Tmp1 = SelectExpr(Op0);
1373 }
1374 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1375 return Result;
1376 }
1377
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001378 // See if we can codegen this as an LEA to fold operations together.
1379 if (N.getValueType() == MVT::i32) {
1380 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001381 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001382 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001383 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001384 // leave this as LEA, then peephole it to 'ADD' after two address elim
1385 // happens.
1386 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001387 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001388 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1389 return Result;
1390 }
1391 }
1392 }
Chris Lattner11333092005-01-11 03:11:44 +00001393
Chris Lattnera5ade062005-01-11 21:19:59 +00001394 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001395 Opc = 0;
1396 if (CN->getValue() == 1) { // add X, 1 -> inc X
1397 switch (N.getValueType()) {
1398 default: assert(0 && "Cannot integer add this type!");
1399 case MVT::i8: Opc = X86::INC8r; break;
1400 case MVT::i16: Opc = X86::INC16r; break;
1401 case MVT::i32: Opc = X86::INC32r; break;
1402 }
1403 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1404 switch (N.getValueType()) {
1405 default: assert(0 && "Cannot integer add this type!");
1406 case MVT::i8: Opc = X86::DEC8r; break;
1407 case MVT::i16: Opc = X86::DEC16r; break;
1408 case MVT::i32: Opc = X86::DEC32r; break;
1409 }
1410 }
1411
1412 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001413 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001414 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1415 return Result;
1416 }
1417
1418 switch (N.getValueType()) {
1419 default: assert(0 && "Cannot add this type!");
1420 case MVT::i8: Opc = X86::ADD8ri; break;
1421 case MVT::i16: Opc = X86::ADD16ri; break;
1422 case MVT::i32: Opc = X86::ADD32ri; break;
1423 }
1424 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001425 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001426 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1427 return Result;
1428 }
1429 }
1430
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001431 switch (N.getValueType()) {
1432 default: assert(0 && "Cannot add this type!");
1433 case MVT::i8: Opc = X86::ADD8rr; break;
1434 case MVT::i16: Opc = X86::ADD16rr; break;
1435 case MVT::i32: Opc = X86::ADD32rr; break;
1436 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001437 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001438 }
Chris Lattner11333092005-01-11 03:11:44 +00001439
Chris Lattnera5ade062005-01-11 21:19:59 +00001440 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1441 Tmp1 = SelectExpr(Op0);
1442 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001443 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001444 Tmp2 = SelectExpr(Op1);
1445 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001446 }
1447
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001448 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1449 return Result;
1450 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001451 case ISD::MUL:
1452 case ISD::AND:
1453 case ISD::OR:
1454 case ISD::XOR:
1455 static const unsigned SUBTab[] = {
1456 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1457 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1458 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1459 };
1460 static const unsigned MULTab[] = {
1461 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1462 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1463 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1464 };
1465 static const unsigned ANDTab[] = {
1466 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1467 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1468 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1469 };
1470 static const unsigned ORTab[] = {
1471 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1472 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1473 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1474 };
1475 static const unsigned XORTab[] = {
1476 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1477 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1478 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1479 };
1480
1481 Op0 = Node->getOperand(0);
1482 Op1 = Node->getOperand(1);
1483
1484 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001485 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1486 if (CN->isNullValue()) { // 0 - N -> neg N
1487 switch (N.getValueType()) {
1488 default: assert(0 && "Cannot sub this type!");
1489 case MVT::i1:
1490 case MVT::i8: Opc = X86::NEG8r; break;
1491 case MVT::i16: Opc = X86::NEG16r; break;
1492 case MVT::i32: Opc = X86::NEG32r; break;
1493 }
1494 Tmp1 = SelectExpr(N.getOperand(1));
1495 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1496 return Result;
1497 }
1498
Chris Lattnera5ade062005-01-11 21:19:59 +00001499 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1500 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001501 switch (N.getValueType()) {
1502 default: assert(0 && "Cannot add this type!");
1503 case MVT::i1:
1504 case MVT::i8: Opc = X86::NOT8r; break;
1505 case MVT::i16: Opc = X86::NOT16r; break;
1506 case MVT::i32: Opc = X86::NOT32r; break;
1507 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001508 Tmp1 = SelectExpr(Op0);
Chris Lattnerd4dab922005-01-11 04:31:30 +00001509 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1510 return Result;
1511 }
1512
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001513 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001514 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001515 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001516 case MVT::i8: Opc = 0; break;
1517 case MVT::i16: Opc = 1; break;
1518 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001519 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001520 switch (Node->getOpcode()) {
1521 default: assert(0 && "Unreachable!");
1522 case ISD::SUB: Opc = SUBTab[Opc]; break;
1523 case ISD::MUL: Opc = MULTab[Opc]; break;
1524 case ISD::AND: Opc = ANDTab[Opc]; break;
1525 case ISD::OR: Opc = ORTab[Opc]; break;
1526 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001527 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001528 if (Opc) { // Can't fold MUL:i8 R, imm
1529 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001530 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1531 return Result;
1532 }
1533 }
Chris Lattner11333092005-01-11 03:11:44 +00001534
Chris Lattnera5ade062005-01-11 21:19:59 +00001535 if (isFoldableLoad(Op0))
1536 if (Node->getOpcode() != ISD::SUB) {
1537 std::swap(Op0, Op1);
1538 } else {
1539 // Emit 'reverse' subract, with a memory operand.
1540 switch (N.getValueType()) {
1541 default: Opc = 0; break;
1542 case MVT::f32: Opc = X86::FSUBR32m; break;
1543 case MVT::f64: Opc = X86::FSUBR64m; break;
1544 }
1545 if (Opc) {
1546 X86AddressMode AM;
1547 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1548 EmitFoldedLoad(Op0, AM);
1549 Tmp1 = SelectExpr(Op1);
1550 } else {
1551 Tmp1 = SelectExpr(Op1);
1552 EmitFoldedLoad(Op0, AM);
1553 }
1554 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1555 return Result;
1556 }
1557 }
1558
1559 if (isFoldableLoad(Op1)) {
1560 switch (N.getValueType()) {
1561 default: assert(0 && "Cannot operate on this type!");
1562 case MVT::i1:
1563 case MVT::i8: Opc = 5; break;
1564 case MVT::i16: Opc = 6; break;
1565 case MVT::i32: Opc = 7; break;
1566 case MVT::f32: Opc = 8; break;
1567 case MVT::f64: Opc = 9; break;
1568 }
1569 switch (Node->getOpcode()) {
1570 default: assert(0 && "Unreachable!");
1571 case ISD::SUB: Opc = SUBTab[Opc]; break;
1572 case ISD::MUL: Opc = MULTab[Opc]; break;
1573 case ISD::AND: Opc = ANDTab[Opc]; break;
1574 case ISD::OR: Opc = ORTab[Opc]; break;
1575 case ISD::XOR: Opc = XORTab[Opc]; break;
1576 }
1577
1578 X86AddressMode AM;
1579 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1580 Tmp1 = SelectExpr(Op0);
1581 EmitFoldedLoad(Op1, AM);
1582 } else {
1583 EmitFoldedLoad(Op1, AM);
1584 Tmp1 = SelectExpr(Op0);
1585 }
1586 if (Opc) {
1587 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1588 } else {
1589 assert(Node->getOpcode() == ISD::MUL &&
1590 N.getValueType() == MVT::i8 && "Unexpected situation!");
1591 // Must use the MUL instruction, which forces use of AL.
1592 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1593 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1594 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1595 }
1596 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001597 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001598
1599 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1600 Tmp1 = SelectExpr(Op0);
1601 Tmp2 = SelectExpr(Op1);
1602 } else {
1603 Tmp2 = SelectExpr(Op1);
1604 Tmp1 = SelectExpr(Op0);
1605 }
1606
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001607 switch (N.getValueType()) {
1608 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001609 case MVT::i1:
1610 case MVT::i8: Opc = 10; break;
1611 case MVT::i16: Opc = 11; break;
1612 case MVT::i32: Opc = 12; break;
1613 case MVT::f32: Opc = 13; break;
1614 case MVT::f64: Opc = 14; break;
1615 }
1616 switch (Node->getOpcode()) {
1617 default: assert(0 && "Unreachable!");
1618 case ISD::SUB: Opc = SUBTab[Opc]; break;
1619 case ISD::MUL: Opc = MULTab[Opc]; break;
1620 case ISD::AND: Opc = ANDTab[Opc]; break;
1621 case ISD::OR: Opc = ORTab[Opc]; break;
1622 case ISD::XOR: Opc = XORTab[Opc]; break;
1623 }
1624 if (Opc) {
1625 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1626 } else {
1627 assert(Node->getOpcode() == ISD::MUL &&
1628 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001629 // Must use the MUL instruction, which forces use of AL.
1630 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1631 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1632 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001633 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001634 return Result;
1635
1636 case ISD::SELECT:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001637 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001638 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1639 Tmp2 = SelectExpr(N.getOperand(1));
1640 Tmp3 = SelectExpr(N.getOperand(2));
1641 } else {
1642 Tmp3 = SelectExpr(N.getOperand(2));
1643 Tmp2 = SelectExpr(N.getOperand(1));
1644 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001645 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001646 return Result;
1647 } else {
1648 // FIXME: This should not be implemented here, it should be in the generic
1649 // code!
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001650 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1651 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1652 N.getOperand(1)));
1653 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1654 N.getOperand(2)));
1655 } else {
1656 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1657 N.getOperand(2)));
1658 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1659 N.getOperand(1)));
1660 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001661 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001662 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1663 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001664 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1665 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1666 return Result;
1667 }
1668
1669 case ISD::SDIV:
1670 case ISD::UDIV:
1671 case ISD::SREM:
1672 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001673 if (N.getOpcode() == ISD::SDIV)
1674 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1675 // FIXME: These special cases should be handled by the lowering impl!
1676 unsigned RHS = CN->getValue();
1677 bool isNeg = false;
1678 if ((int)RHS < 0) {
1679 isNeg = true;
1680 RHS = -RHS;
1681 }
1682 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1683 unsigned Log = log2(RHS);
1684 unsigned TmpReg = MakeReg(N.getValueType());
1685 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1686 switch (N.getValueType()) {
1687 default: assert("Unknown type to signed divide!");
1688 case MVT::i8:
1689 SAROpc = X86::SAR8ri;
1690 SHROpc = X86::SHR8ri;
1691 ADDOpc = X86::ADD8rr;
1692 NEGOpc = X86::NEG8r;
1693 break;
1694 case MVT::i16:
1695 SAROpc = X86::SAR16ri;
1696 SHROpc = X86::SHR16ri;
1697 ADDOpc = X86::ADD16rr;
1698 NEGOpc = X86::NEG16r;
1699 break;
1700 case MVT::i32:
1701 SAROpc = X86::SAR32ri;
1702 SHROpc = X86::SHR32ri;
1703 ADDOpc = X86::ADD32rr;
1704 NEGOpc = X86::NEG32r;
1705 break;
1706 }
Chris Lattner11333092005-01-11 03:11:44 +00001707 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001708 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1709 unsigned TmpReg2 = MakeReg(N.getValueType());
1710 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1711 unsigned TmpReg3 = MakeReg(N.getValueType());
1712 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1713
1714 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1715 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1716 if (isNeg)
1717 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1718 return Result;
1719 }
1720 }
1721
Chris Lattner11333092005-01-11 03:11:44 +00001722 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1723 Tmp1 = SelectExpr(N.getOperand(0));
1724 Tmp2 = SelectExpr(N.getOperand(1));
1725 } else {
1726 Tmp2 = SelectExpr(N.getOperand(1));
1727 Tmp1 = SelectExpr(N.getOperand(0));
1728 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001729
1730 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1731 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1732 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1733 switch (N.getValueType()) {
1734 default: assert(0 && "Cannot sdiv this type!");
1735 case MVT::i8:
1736 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1737 LoReg = X86::AL;
1738 HiReg = X86::AH;
1739 MovOpcode = X86::MOV8rr;
1740 ClrOpcode = X86::MOV8ri;
1741 SExtOpcode = X86::CBW;
1742 break;
1743 case MVT::i16:
1744 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1745 LoReg = X86::AX;
1746 HiReg = X86::DX;
1747 MovOpcode = X86::MOV16rr;
1748 ClrOpcode = X86::MOV16ri;
1749 SExtOpcode = X86::CWD;
1750 break;
1751 case MVT::i32:
1752 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
1753 LoReg =X86::EAX;
1754 HiReg = X86::EDX;
1755 MovOpcode = X86::MOV32rr;
1756 ClrOpcode = X86::MOV32ri;
1757 SExtOpcode = X86::CDQ;
1758 break;
1759 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1760 case MVT::f32:
1761 case MVT::f64:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001762 if (N.getOpcode() == ISD::SDIV)
1763 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1764 else
1765 assert(0 && "FIXME: Emit frem libcall to fmod!");
1766 return Result;
1767 }
1768
1769 // Set up the low part.
1770 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1771
1772 if (isSigned) {
1773 // Sign extend the low part into the high part.
1774 BuildMI(BB, SExtOpcode, 0);
1775 } else {
1776 // Zero out the high part, effectively zero extending the input.
1777 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1778 }
1779
1780 // Emit the DIV/IDIV instruction.
1781 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1782
1783 // Get the result of the divide or rem.
1784 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1785 return Result;
1786 }
1787
1788 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001789 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001790 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1791 switch (N.getValueType()) {
1792 default: assert(0 && "Cannot shift this type!");
1793 case MVT::i8: Opc = X86::ADD8rr; break;
1794 case MVT::i16: Opc = X86::ADD16rr; break;
1795 case MVT::i32: Opc = X86::ADD32rr; break;
1796 }
1797 Tmp1 = SelectExpr(N.getOperand(0));
1798 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1799 return Result;
1800 }
1801
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001802 switch (N.getValueType()) {
1803 default: assert(0 && "Cannot shift this type!");
1804 case MVT::i8: Opc = X86::SHL8ri; break;
1805 case MVT::i16: Opc = X86::SHL16ri; break;
1806 case MVT::i32: Opc = X86::SHL32ri; break;
1807 }
Chris Lattner11333092005-01-11 03:11:44 +00001808 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001809 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1810 return Result;
1811 }
Chris Lattner11333092005-01-11 03:11:44 +00001812
1813 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1814 Tmp1 = SelectExpr(N.getOperand(0));
1815 Tmp2 = SelectExpr(N.getOperand(1));
1816 } else {
1817 Tmp2 = SelectExpr(N.getOperand(1));
1818 Tmp1 = SelectExpr(N.getOperand(0));
1819 }
1820
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001821 switch (N.getValueType()) {
1822 default: assert(0 && "Cannot shift this type!");
1823 case MVT::i8 : Opc = X86::SHL8rCL; break;
1824 case MVT::i16: Opc = X86::SHL16rCL; break;
1825 case MVT::i32: Opc = X86::SHL32rCL; break;
1826 }
1827 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1828 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1829 return Result;
1830 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001831 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1832 switch (N.getValueType()) {
1833 default: assert(0 && "Cannot shift this type!");
1834 case MVT::i8: Opc = X86::SHR8ri; break;
1835 case MVT::i16: Opc = X86::SHR16ri; break;
1836 case MVT::i32: Opc = X86::SHR32ri; break;
1837 }
Chris Lattner11333092005-01-11 03:11:44 +00001838 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001839 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1840 return Result;
1841 }
Chris Lattner11333092005-01-11 03:11:44 +00001842
1843 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1844 Tmp1 = SelectExpr(N.getOperand(0));
1845 Tmp2 = SelectExpr(N.getOperand(1));
1846 } else {
1847 Tmp2 = SelectExpr(N.getOperand(1));
1848 Tmp1 = SelectExpr(N.getOperand(0));
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::SHR8rCL; break;
1854 case MVT::i16: Opc = X86::SHR16rCL; break;
1855 case MVT::i32: Opc = X86::SHR32rCL; break;
1856 }
1857 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1858 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1859 return Result;
1860 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001861 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1862 switch (N.getValueType()) {
1863 default: assert(0 && "Cannot shift this type!");
1864 case MVT::i8: Opc = X86::SAR8ri; break;
1865 case MVT::i16: Opc = X86::SAR16ri; break;
1866 case MVT::i32: Opc = X86::SAR32ri; break;
1867 }
Chris Lattner11333092005-01-11 03:11:44 +00001868 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001869 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1870 return Result;
1871 }
Chris Lattner11333092005-01-11 03:11:44 +00001872
1873 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1874 Tmp1 = SelectExpr(N.getOperand(0));
1875 Tmp2 = SelectExpr(N.getOperand(1));
1876 } else {
1877 Tmp2 = SelectExpr(N.getOperand(1));
1878 Tmp1 = SelectExpr(N.getOperand(0));
1879 }
1880
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001881 switch (N.getValueType()) {
1882 default: assert(0 && "Cannot shift this type!");
1883 case MVT::i8 : Opc = X86::SAR8rCL; break;
1884 case MVT::i16: Opc = X86::SAR16rCL; break;
1885 case MVT::i32: Opc = X86::SAR32rCL; break;
1886 }
1887 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1888 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1889 return Result;
1890
1891 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001892 EmitCMP(N.getOperand(0), N.getOperand(1));
1893 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1894 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1895 return Result;
1896 case ISD::LOAD: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001897 // Make sure we generate both values.
1898 if (Result != 1)
1899 ExprMap[N.getValue(1)] = 1; // Generate the token
1900 else
1901 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1902
Chris Lattner5188ad72005-01-08 19:28:19 +00001903 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001904 default: assert(0 && "Cannot load this type!");
1905 case MVT::i1:
1906 case MVT::i8: Opc = X86::MOV8rm; break;
1907 case MVT::i16: Opc = X86::MOV16rm; break;
1908 case MVT::i32: Opc = X86::MOV32rm; break;
1909 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1910 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1911 }
Chris Lattner11333092005-01-11 03:11:44 +00001912
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001913 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001914 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001915 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1916 } else {
1917 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001918 EmitFoldedLoad(N, AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001919 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1920 }
1921 return Result;
1922 }
1923 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001924 // Generate both result values.
1925 if (Result != 1)
1926 ExprMap[N.getValue(1)] = 1; // Generate the token
1927 else
1928 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1929
1930 // FIXME: We are currently ignoring the requested alignment for handling
1931 // greater than the stack alignment. This will need to be revisited at some
1932 // point. Align = N.getOperand(2);
1933
1934 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1935 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1936 std::cerr << "Cannot allocate stack object with greater alignment than"
1937 << " the stack alignment yet!";
1938 abort();
1939 }
1940
1941 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001942 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001943 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1944 .addImm(CN->getValue());
1945 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001946 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1947 Select(N.getOperand(0));
1948 Tmp1 = SelectExpr(N.getOperand(1));
1949 } else {
1950 Tmp1 = SelectExpr(N.getOperand(1));
1951 Select(N.getOperand(0));
1952 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001953
1954 // Subtract size from stack pointer, thereby allocating some space.
1955 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
1956 }
1957
1958 // Put a pointer to the space into the result register, by copying the stack
1959 // pointer.
1960 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
1961 return Result;
1962
1963 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00001964 // The chain for this call is now lowered.
1965 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
1966
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001967 if (GlobalAddressSDNode *GASD =
1968 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001969 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001970 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
1971 } else if (ExternalSymbolSDNode *ESSDN =
1972 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001973 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001974 BuildMI(BB, X86::CALLpcrel32,
1975 1).addExternalSymbol(ESSDN->getSymbol(), true);
1976 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001977 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1978 Select(N.getOperand(0));
1979 Tmp1 = SelectExpr(N.getOperand(1));
1980 } else {
1981 Tmp1 = SelectExpr(N.getOperand(1));
1982 Select(N.getOperand(0));
1983 }
1984
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001985 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
1986 }
Chris Lattner5188ad72005-01-08 19:28:19 +00001987 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001988 default: assert(0 && "Unknown value type for call result!");
1989 case MVT::Other: return 1;
1990 case MVT::i1:
1991 case MVT::i8:
1992 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1993 break;
1994 case MVT::i16:
1995 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
1996 break;
1997 case MVT::i32:
1998 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00001999 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002000 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2001 break;
2002 case MVT::f32:
2003 case MVT::f64: // Floating-point return values live in %ST(0)
2004 ContainsFPCode = true;
2005 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2006 break;
2007 }
2008 return Result+N.ResNo;
2009 }
2010
2011 return 0;
2012}
2013
2014void ISel::Select(SDOperand N) {
2015 unsigned Tmp1, Tmp2, Opc;
2016
2017 // FIXME: Disable for our current expansion model!
2018 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
2019 return; // Already selected.
2020
Chris Lattner989de032005-01-11 06:14:36 +00002021 SDNode *Node = N.Val;
2022
2023 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002024 default:
Chris Lattner989de032005-01-11 06:14:36 +00002025 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002026 assert(0 && "Node not handled yet!");
2027 case ISD::EntryToken: return; // Noop
2028 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002029 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2030 Select(N.getOperand(0));
2031 Tmp1 = SelectExpr(N.getOperand(1));
2032 } else {
2033 Tmp1 = SelectExpr(N.getOperand(1));
2034 Select(N.getOperand(0));
2035 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002036 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
2037
2038 if (Tmp1 != Tmp2) {
2039 switch (N.getOperand(1).getValueType()) {
2040 default: assert(0 && "Invalid type for operation!");
2041 case MVT::i1:
2042 case MVT::i8: Opc = X86::MOV8rr; break;
2043 case MVT::i16: Opc = X86::MOV16rr; break;
2044 case MVT::i32: Opc = X86::MOV32rr; break;
2045 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002046 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002047 }
2048 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2049 }
2050 return;
2051 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002052 switch (N.getNumOperands()) {
2053 default:
2054 assert(0 && "Unknown return instruction!");
2055 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002056 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2057 N.getOperand(2).getValueType() == MVT::i32 &&
2058 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002059 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2060 Tmp1 = SelectExpr(N.getOperand(1));
2061 Tmp2 = SelectExpr(N.getOperand(2));
2062 } else {
2063 Tmp2 = SelectExpr(N.getOperand(2));
2064 Tmp1 = SelectExpr(N.getOperand(1));
2065 }
2066 Select(N.getOperand(0));
2067
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002068 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2069 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2070 // Declare that EAX & EDX are live on exit.
2071 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2072 .addReg(X86::ESP);
2073 break;
2074 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002075 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2076 Select(N.getOperand(0));
2077 Tmp1 = SelectExpr(N.getOperand(1));
2078 } else {
2079 Tmp1 = SelectExpr(N.getOperand(1));
2080 Select(N.getOperand(0));
2081 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002082 switch (N.getOperand(1).getValueType()) {
2083 default: assert(0 && "All other types should have been promoted!!");
2084 case MVT::f64:
2085 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2086 // Declare that top-of-stack is live on exit
2087 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2088 break;
2089 case MVT::i32:
2090 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2091 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2092 break;
2093 }
2094 break;
2095 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002096 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002097 break;
2098 }
2099 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2100 return;
2101 case ISD::BR: {
2102 Select(N.getOperand(0));
2103 MachineBasicBlock *Dest =
2104 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2105 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2106 return;
2107 }
2108
2109 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002110 MachineBasicBlock *Dest =
2111 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002112
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002113 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2114 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002115 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2116 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2117 Select(N.getOperand(0));
2118 Tmp1 = SelectExpr(N.getOperand(1));
2119 } else {
2120 Tmp1 = SelectExpr(N.getOperand(1));
2121 Select(N.getOperand(0));
2122 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002123 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2124 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2125 }
Chris Lattner11333092005-01-11 03:11:44 +00002126
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002127 return;
2128 }
2129 case ISD::LOAD:
2130 case ISD::CALL:
2131 case ISD::DYNAMIC_STACKALLOC:
2132 SelectExpr(N);
2133 return;
2134 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002135 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002136
2137 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2138 Opc = 0;
2139 switch (CN->getValueType(0)) {
2140 default: assert(0 && "Invalid type for operation!");
2141 case MVT::i1:
2142 case MVT::i8: Opc = X86::MOV8mi; break;
2143 case MVT::i16: Opc = X86::MOV16mi; break;
2144 case MVT::i32: Opc = X86::MOV32mi; break;
2145 case MVT::f32:
2146 case MVT::f64: break;
2147 }
2148 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002149 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2150 Select(N.getOperand(0));
2151 SelectAddress(N.getOperand(2), AM);
2152 } else {
2153 SelectAddress(N.getOperand(2), AM);
2154 Select(N.getOperand(0));
2155 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002156 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2157 return;
2158 }
2159 }
Chris Lattner837caa72005-01-11 23:21:30 +00002160
2161 // Check to see if this is a load/op/store combination.
2162 if (N.getOperand(1).Val->hasOneUse() &&
2163 isFoldableLoad(N.getOperand(0).getValue(0))) {
2164 SDOperand TheLoad = N.getOperand(0).getValue(0);
2165 // Check to see if we are loading the same pointer that we're storing to.
2166 if (TheLoad.getOperand(1) == N.getOperand(2)) {
2167 // See if the stored value is a simple binary operator that uses the
2168 // load as one of its operands.
2169 SDOperand Op = N.getOperand(1);
2170 if (Op.Val->getNumOperands() == 2 &&
2171 (Op.getOperand(0) == TheLoad || Op.getOperand(1) == TheLoad)) {
2172 // Finally, check to see if this is one of the ops we can handle!
2173 static const unsigned ADDTAB[] = {
2174 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2175 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr, 0, 0,
2176 };
Chris Lattner7ea64f52005-01-12 01:28:00 +00002177 static const unsigned SUBTAB[] = {
2178 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2179 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr, 0, 0,
2180 };
2181 static const unsigned ANDTAB[] = {
2182 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2183 X86::AND8mr, X86::AND16mr, X86::AND32mr, 0, 0,
2184 };
2185 static const unsigned ORTAB[] = {
2186 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2187 X86::OR8mr, X86::OR16mr, X86::OR32mr, 0, 0,
2188 };
2189 static const unsigned XORTAB[] = {
2190 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2191 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr, 0, 0,
2192 };
2193 static const unsigned SHLTAB[] = {
2194 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2195 /*Have to put the reg in CL*/0, 0, 0, 0, 0,
2196 };
2197 static const unsigned SARTAB[] = {
2198 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2199 /*Have to put the reg in CL*/0, 0, 0, 0, 0,
2200 };
2201 static const unsigned SHRTAB[] = {
2202 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2203 /*Have to put the reg in CL*/0, 0, 0, 0, 0,
2204 };
Chris Lattner837caa72005-01-11 23:21:30 +00002205
2206 const unsigned *TabPtr = 0;
2207 switch (Op.getOpcode()) {
Chris Lattner7ea64f52005-01-12 01:28:00 +00002208 default: std::cerr << "CANNOT [mem] op= val: "; Op.Val->dump(); std::cerr << "\n"; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002209 case ISD::ADD: TabPtr = ADDTAB; break;
Chris Lattner7ea64f52005-01-12 01:28:00 +00002210 case ISD::SUB: TabPtr = SUBTAB; break;
2211 case ISD::AND: TabPtr = ANDTAB; break;
2212 case ISD:: OR: TabPtr = ORTAB; break;
2213 case ISD::XOR: TabPtr = XORTAB; break;
2214 case ISD::SHL: TabPtr = SHLTAB; break;
2215 case ISD::SRA: TabPtr = SARTAB; break;
2216 case ISD::SRL: TabPtr = SHRTAB; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002217 }
2218
2219 if (TabPtr) {
2220 // Handle: [mem] op= CST
2221 SDOperand Op0 = Op.getOperand(0);
2222 SDOperand Op1 = Op.getOperand(1);
2223 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2224 switch (CN->getValueType(0)) {
2225 default: break;
2226 case MVT::i1:
2227 case MVT::i8: Opc = TabPtr[0]; break;
2228 case MVT::i16: Opc = TabPtr[1]; break;
2229 case MVT::i32: Opc = TabPtr[2]; break;
2230 }
2231
2232 if (Opc) {
2233 if (getRegPressure(TheLoad.getOperand(0)) >
2234 getRegPressure(TheLoad.getOperand(1))) {
2235 Select(TheLoad.getOperand(0));
2236 SelectAddress(TheLoad.getOperand(1), AM);
2237 } else {
2238 SelectAddress(TheLoad.getOperand(1), AM);
2239 Select(TheLoad.getOperand(0));
2240 }
2241
2242 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2243 return;
2244 }
2245 }
2246
2247 // If we have [mem] = V op [mem], try to turn it into:
2248 // [mem] = [mem] op V.
Chris Lattner7ea64f52005-01-12 01:28:00 +00002249 if (Op1 == TheLoad && Op.getOpcode() != ISD::SUB &&
2250 Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRA &&
2251 Op.getOpcode() != ISD::SRL)
Chris Lattner837caa72005-01-11 23:21:30 +00002252 std::swap(Op0, Op1);
2253
2254 if (Op0 == TheLoad) {
2255 switch (Op0.getValueType()) {
2256 default: break;
2257 case MVT::i1:
2258 case MVT::i8: Opc = TabPtr[3]; break;
2259 case MVT::i16: Opc = TabPtr[4]; break;
2260 case MVT::i32: Opc = TabPtr[5]; break;
2261 case MVT::f32: Opc = TabPtr[6]; break;
2262 case MVT::f64: Opc = TabPtr[7]; break;
2263 }
2264
2265 if (Opc) {
2266 Select(TheLoad.getOperand(0));
2267 SelectAddress(TheLoad.getOperand(1), AM);
2268 unsigned Reg = SelectExpr(Op1);
2269 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addReg(Reg);
2270 return;
2271 }
2272 }
Chris Lattner837caa72005-01-11 23:21:30 +00002273 }
2274 }
2275 }
2276 }
2277
2278
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002279 switch (N.getOperand(1).getValueType()) {
2280 default: assert(0 && "Cannot store this type!");
2281 case MVT::i1:
2282 case MVT::i8: Opc = X86::MOV8mr; break;
2283 case MVT::i16: Opc = X86::MOV16mr; break;
2284 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002285 case MVT::f32: Opc = X86::FST32m; break;
2286 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002287 }
Chris Lattner11333092005-01-11 03:11:44 +00002288
2289 std::vector<std::pair<unsigned, unsigned> > RP;
2290 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2291 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2292 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2293 std::sort(RP.begin(), RP.end());
2294
2295 for (unsigned i = 0; i != 3; ++i)
2296 switch (RP[2-i].second) {
2297 default: assert(0 && "Unknown operand number!");
2298 case 0: Select(N.getOperand(0)); break;
2299 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002300 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002301 }
2302
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002303 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2304 return;
2305 }
2306 case ISD::ADJCALLSTACKDOWN:
2307 case ISD::ADJCALLSTACKUP:
2308 Select(N.getOperand(0));
2309 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2310
2311 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2312 X86::ADJCALLSTACKUP;
2313 BuildMI(BB, Opc, 1).addImm(Tmp1);
2314 return;
Chris Lattner989de032005-01-11 06:14:36 +00002315 case ISD::MEMSET: {
2316 Select(N.getOperand(0)); // Select the chain.
2317 unsigned Align =
2318 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2319 if (Align == 0) Align = 1;
2320
2321 // Turn the byte code into # iterations
2322 unsigned CountReg;
2323 unsigned Opcode;
2324 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2325 unsigned Val = ValC->getValue() & 255;
2326
2327 // If the value is a constant, then we can potentially use larger sets.
2328 switch (Align & 3) {
2329 case 2: // WORD aligned
2330 CountReg = MakeReg(MVT::i32);
2331 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2332 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2333 } else {
2334 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2335 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2336 }
2337 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2338 Opcode = X86::REP_STOSW;
2339 break;
2340 case 0: // DWORD aligned
2341 CountReg = MakeReg(MVT::i32);
2342 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2343 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2344 } else {
2345 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2346 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2347 }
2348 Val = (Val << 8) | Val;
2349 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2350 Opcode = X86::REP_STOSD;
2351 break;
2352 default: // BYTE aligned
2353 CountReg = SelectExpr(Node->getOperand(3));
2354 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2355 Opcode = X86::REP_STOSB;
2356 break;
2357 }
2358 } else {
2359 // If it's not a constant value we are storing, just fall back. We could
2360 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2361 unsigned ValReg = SelectExpr(Node->getOperand(2));
2362 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2363 CountReg = SelectExpr(Node->getOperand(3));
2364 Opcode = X86::REP_STOSB;
2365 }
2366
2367 // No matter what the alignment is, we put the source in ESI, the
2368 // destination in EDI, and the count in ECX.
2369 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2370 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2371 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2372 BuildMI(BB, Opcode, 0);
2373 return;
2374 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002375 case ISD::MEMCPY:
2376 Select(N.getOperand(0)); // Select the chain.
2377 unsigned Align =
2378 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2379 if (Align == 0) Align = 1;
2380
2381 // Turn the byte code into # iterations
2382 unsigned CountReg;
2383 unsigned Opcode;
2384 switch (Align & 3) {
2385 case 2: // WORD aligned
2386 CountReg = MakeReg(MVT::i32);
2387 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2388 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2389 } else {
2390 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2391 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2392 }
2393 Opcode = X86::REP_MOVSW;
2394 break;
2395 case 0: // DWORD aligned
2396 CountReg = MakeReg(MVT::i32);
2397 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2398 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2399 } else {
2400 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2401 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2402 }
2403 Opcode = X86::REP_MOVSD;
2404 break;
2405 default: // BYTE aligned
2406 CountReg = SelectExpr(Node->getOperand(3));
2407 Opcode = X86::REP_MOVSB;
2408 break;
2409 }
2410
2411 // No matter what the alignment is, we put the source in ESI, the
2412 // destination in EDI, and the count in ECX.
2413 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2414 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2415 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2416 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2417 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2418 BuildMI(BB, Opcode, 0);
2419 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002420 }
2421 assert(0 && "Should not be reached!");
2422}
2423
2424
2425/// createX86PatternInstructionSelector - This pass converts an LLVM function
2426/// into a machine code representation using pattern matching and a machine
2427/// description file.
2428///
2429FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2430 return new ISel(TM);
2431}