blob: f5f6c105617095fa12a47c764129382d68482ccd [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;
860 switch (RHS.getValueType()) {
861 default: break;
862 case MVT::i1:
863 case MVT::i8: Opc = X86::CMP8ri; break;
864 case MVT::i16: Opc = X86::CMP16ri; break;
865 case MVT::i32: Opc = X86::CMP32ri; break;
866 }
867 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000868 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000869 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
870 return;
871 }
872 }
873
874 switch (LHS.getValueType()) {
875 default: assert(0 && "Cannot compare this value!");
876 case MVT::i1:
877 case MVT::i8: Opc = X86::CMP8rr; break;
878 case MVT::i16: Opc = X86::CMP16rr; break;
879 case MVT::i32: Opc = X86::CMP32rr; break;
880 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000881 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000882 }
Chris Lattner11333092005-01-11 03:11:44 +0000883 unsigned Tmp1, Tmp2;
884 if (getRegPressure(LHS) > getRegPressure(RHS)) {
885 Tmp1 = SelectExpr(LHS);
886 Tmp2 = SelectExpr(RHS);
887 } else {
888 Tmp2 = SelectExpr(RHS);
889 Tmp1 = SelectExpr(LHS);
890 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000891 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
892}
893
Chris Lattnera5ade062005-01-11 21:19:59 +0000894/// isFoldableLoad - Return true if this is a load instruction that can safely
895/// be folded into an operation that uses it.
896bool ISel::isFoldableLoad(SDOperand Op) {
897 if (Op.getOpcode() != ISD::LOAD ||
898 // FIXME: currently can't fold constant pool indexes.
899 isa<ConstantPoolSDNode>(Op.getOperand(1)))
900 return false;
901
902 // If this load has already been emitted, we clearly can't fold it.
903 if (ExprMap.count(Op)) return false;
904
905 return Op.Val->use_size() == 2;
906}
907
908/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
909/// and compute the address being loaded into AM.
910void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
911 SDOperand Chain = Op.getOperand(0);
912 SDOperand Address = Op.getOperand(1);
913 if (getRegPressure(Chain) > getRegPressure(Address)) {
914 Select(Chain);
915 SelectAddress(Address, AM);
916 } else {
917 SelectAddress(Address, AM);
918 Select(Chain);
919 }
920
921 // The chain for this load is now lowered.
922 LoweredTokens.insert(SDOperand(Op.Val, 1));
923 ExprMap[SDOperand(Op.Val, 1)] = 1;
924}
925
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000926unsigned ISel::SelectExpr(SDOperand N) {
927 unsigned Result;
928 unsigned Tmp1, Tmp2, Tmp3;
929 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +0000930 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +0000931 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +0000932
Chris Lattner590d8002005-01-09 18:52:44 +0000933 if (Node->getOpcode() == ISD::CopyFromReg)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000934 // Just use the specified register as our input.
Chris Lattner590d8002005-01-09 18:52:44 +0000935 return dyn_cast<CopyRegSDNode>(Node)->getReg();
Chris Lattnera5ade062005-01-11 21:19:59 +0000936
937 unsigned &Reg = ExprMap[N];
938 if (Reg) return Reg;
939
940 if (N.getOpcode() != ISD::CALL)
941 Reg = Result = (N.getValueType() != MVT::Other) ?
942 MakeReg(N.getValueType()) : 1;
943 else {
944 // If this is a call instruction, make sure to prepare ALL of the result
945 // values as well as the chain.
946 if (Node->getNumValues() == 1)
947 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000948 else {
Chris Lattnera5ade062005-01-11 21:19:59 +0000949 Result = MakeReg(Node->getValueType(0));
950 ExprMap[N.getValue(0)] = Result;
951 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
952 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
953 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000954 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000955 }
Chris Lattnera5ade062005-01-11 21:19:59 +0000956
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000957 switch (N.getOpcode()) {
958 default:
Chris Lattner5188ad72005-01-08 19:28:19 +0000959 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000960 assert(0 && "Node not handled!\n");
961 case ISD::FrameIndex:
962 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
963 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
964 return Result;
965 case ISD::ConstantPool:
966 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
967 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
968 return Result;
969 case ISD::ConstantFP:
970 ContainsFPCode = true;
971 Tmp1 = Result; // Intermediate Register
972 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
973 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
974 Tmp1 = MakeReg(MVT::f64);
975
976 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
977 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
978 BuildMI(BB, X86::FLD0, 0, Tmp1);
979 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
980 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
981 BuildMI(BB, X86::FLD1, 0, Tmp1);
982 else
983 assert(0 && "Unexpected constant!");
984 if (Tmp1 != Result)
985 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
986 return Result;
987 case ISD::Constant:
988 switch (N.getValueType()) {
989 default: assert(0 && "Cannot use constants of this type!");
990 case MVT::i1:
991 case MVT::i8: Opc = X86::MOV8ri; break;
992 case MVT::i16: Opc = X86::MOV16ri; break;
993 case MVT::i32: Opc = X86::MOV32ri; break;
994 }
995 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
996 return Result;
997 case ISD::GlobalAddress: {
998 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
999 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1000 return Result;
1001 }
1002 case ISD::ExternalSymbol: {
1003 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1004 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1005 return Result;
1006 }
1007 case ISD::FP_EXTEND:
1008 Tmp1 = SelectExpr(N.getOperand(0));
1009 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001010 return Result;
1011 case ISD::ZERO_EXTEND: {
1012 int DestIs16 = N.getValueType() == MVT::i16;
1013 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001014
1015 // FIXME: This hack is here for zero extension casts from bool to i8. This
1016 // would not be needed if bools were promoted by Legalize.
1017 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001018 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001019 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1020 return Result;
1021 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001022
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001023 if (isFoldableLoad(N.getOperand(0))) {
1024 static const unsigned Opc[3] = {
1025 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1026 };
1027
1028 X86AddressMode AM;
1029 EmitFoldedLoad(N.getOperand(0), AM);
1030 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1031
1032 return Result;
1033 }
1034
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001035 static const unsigned Opc[3] = {
1036 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1037 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001038 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001039 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1040 return Result;
1041 }
1042 case ISD::SIGN_EXTEND: {
1043 int DestIs16 = N.getValueType() == MVT::i16;
1044 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1045
Chris Lattner590d8002005-01-09 18:52:44 +00001046 // FIXME: Legalize should promote bools to i8!
1047 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1048 "Sign extend from bool not implemented!");
1049
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001050 if (isFoldableLoad(N.getOperand(0))) {
1051 static const unsigned Opc[3] = {
1052 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1053 };
1054
1055 X86AddressMode AM;
1056 EmitFoldedLoad(N.getOperand(0), AM);
1057 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1058 return Result;
1059 }
1060
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001061 static const unsigned Opc[3] = {
1062 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1063 };
1064 Tmp1 = SelectExpr(N.getOperand(0));
1065 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1066 return Result;
1067 }
1068 case ISD::TRUNCATE:
1069 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1070 // a move out of AX or AL.
1071 switch (N.getOperand(0).getValueType()) {
1072 default: assert(0 && "Unknown truncate!");
1073 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1074 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1075 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1076 }
1077 Tmp1 = SelectExpr(N.getOperand(0));
1078 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1079
1080 switch (N.getValueType()) {
1081 default: assert(0 && "Unknown truncate!");
1082 case MVT::i1:
1083 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1084 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1085 }
1086 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1087 return Result;
1088
1089 case ISD::FP_ROUND:
1090 // Truncate from double to float by storing to memory as float,
1091 // then reading it back into a register.
1092
1093 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001094 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001095 Tmp1 = TLI.getTargetData().getFloatAlignment();
1096 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1097
1098 // Codegen the input.
1099 Tmp1 = SelectExpr(N.getOperand(0));
1100
1101 // Emit the store, then the reload.
1102 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1103 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001104 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001105
1106 case ISD::SINT_TO_FP:
1107 case ISD::UINT_TO_FP: {
1108 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001109 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001110
1111 // Promote the integer to a type supported by FLD. We do this because there
1112 // are no unsigned FLD instructions, so we must promote an unsigned value to
1113 // a larger signed value, then use FLD on the larger value.
1114 //
1115 MVT::ValueType PromoteType = MVT::Other;
1116 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1117 unsigned PromoteOpcode = 0;
1118 unsigned RealDestReg = Result;
1119 switch (SrcTy) {
1120 case MVT::i1:
1121 case MVT::i8:
1122 // We don't have the facilities for directly loading byte sized data from
1123 // memory (even signed). Promote it to 16 bits.
1124 PromoteType = MVT::i16;
1125 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1126 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1127 break;
1128 case MVT::i16:
1129 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1130 PromoteType = MVT::i32;
1131 PromoteOpcode = X86::MOVZX32rr16;
1132 }
1133 break;
1134 default:
1135 // Don't fild into the real destination.
1136 if (Node->getOpcode() == ISD::UINT_TO_FP)
1137 Result = MakeReg(Node->getValueType(0));
1138 break;
1139 }
1140
1141 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1142
1143 if (PromoteType != MVT::Other) {
1144 Tmp2 = MakeReg(PromoteType);
1145 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1146 SrcTy = PromoteType;
1147 Tmp1 = Tmp2;
1148 }
1149
1150 // Spill the integer to memory and reload it from there.
1151 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1152 MachineFunction *F = BB->getParent();
1153 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1154
1155 switch (SrcTy) {
1156 case MVT::i64:
1157 // FIXME: this won't work for cast [u]long to FP
1158 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1159 FrameIdx).addReg(Tmp1);
1160 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1161 FrameIdx, 4).addReg(Tmp1+1);
1162 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1163 break;
1164 case MVT::i32:
1165 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1166 FrameIdx).addReg(Tmp1);
1167 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1168 break;
1169 case MVT::i16:
1170 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1171 FrameIdx).addReg(Tmp1);
1172 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1173 break;
1174 default: break; // No promotion required.
1175 }
1176
1177 if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i32) {
1178 // If this is a cast from uint -> double, we need to be careful when if
1179 // the "sign" bit is set. If so, we don't want to make a negative number,
1180 // we want to make a positive number. Emit code to add an offset if the
1181 // sign bit is set.
1182
1183 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1184 unsigned IsNeg = MakeReg(MVT::i32);
1185 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1186
1187 // Create a CP value that has the offset in one word and 0 in the other.
1188 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1189 0x4f80000000000000ULL);
1190 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1191 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1192 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1193
1194 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1195 // We need special handling for unsigned 64-bit integer sources. If the
1196 // input number has the "sign bit" set, then we loaded it incorrectly as a
1197 // negative 64-bit number. In this case, add an offset value.
1198
1199 // Emit a test instruction to see if the dynamic input value was signed.
1200 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1201
1202 // If the sign bit is set, get a pointer to an offset, otherwise get a
1203 // pointer to a zero.
1204 MachineConstantPool *CP = F->getConstantPool();
1205 unsigned Zero = MakeReg(MVT::i32);
1206 Constant *Null = Constant::getNullValue(Type::UIntTy);
1207 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1208 CP->getConstantPoolIndex(Null));
1209 unsigned Offset = MakeReg(MVT::i32);
1210 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1211
1212 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1213 CP->getConstantPoolIndex(OffsetCst));
1214 unsigned Addr = MakeReg(MVT::i32);
1215 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1216
1217 // Load the constant for an add. FIXME: this could make an 'fadd' that
1218 // reads directly from memory, but we don't support these yet.
1219 unsigned ConstReg = MakeReg(MVT::f64);
1220 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1221
1222 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1223 }
1224 return RealDestReg;
1225 }
1226 case ISD::FP_TO_SINT:
1227 case ISD::FP_TO_UINT: {
1228 // FIXME: Most of this grunt work should be done by legalize!
1229 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1230
1231 // Change the floating point control register to use "round towards zero"
1232 // mode when truncating to an integer value.
1233 //
1234 MachineFunction *F = BB->getParent();
1235 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1236 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1237
1238 // Load the old value of the high byte of the control word...
1239 unsigned HighPartOfCW = MakeReg(MVT::i8);
1240 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1241 CWFrameIdx, 1);
1242
1243 // Set the high part to be round to zero...
1244 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1245 CWFrameIdx, 1).addImm(12);
1246
1247 // Reload the modified control word now...
1248 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1249
1250 // Restore the memory image of control word to original value
1251 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1252 CWFrameIdx, 1).addReg(HighPartOfCW);
1253
1254 // We don't have the facilities for directly storing byte sized data to
1255 // memory. Promote it to 16 bits. We also must promote unsigned values to
1256 // larger classes because we only have signed FP stores.
1257 MVT::ValueType StoreClass = Node->getValueType(0);
1258 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1259 switch (StoreClass) {
1260 case MVT::i8: StoreClass = MVT::i16; break;
1261 case MVT::i16: StoreClass = MVT::i32; break;
1262 case MVT::i32: StoreClass = MVT::i64; break;
1263 // The following treatment of cLong may not be perfectly right,
1264 // but it survives chains of casts of the form
1265 // double->ulong->double.
1266 case MVT::i64: StoreClass = MVT::i64; break;
1267 default: assert(0 && "Unknown store class!");
1268 }
1269
1270 // Spill the integer to memory and reload it from there.
1271 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1272 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1273
1274 switch (StoreClass) {
1275 default: assert(0 && "Unknown store class!");
1276 case MVT::i16:
1277 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1278 break;
1279 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001280 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001281 break;
1282 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001283 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001284 break;
1285 }
1286
1287 switch (Node->getValueType(0)) {
1288 default:
1289 assert(0 && "Unknown integer type!");
1290 case MVT::i64:
1291 // FIXME: this isn't gunna work.
1292 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1293 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1294 case MVT::i32:
1295 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1296 break;
1297 case MVT::i16:
1298 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1299 break;
1300 case MVT::i8:
1301 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1302 break;
1303 }
1304
1305 // Reload the original control word now.
1306 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1307 return Result;
1308 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001309 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001310 Op0 = N.getOperand(0);
1311 Op1 = N.getOperand(1);
1312
1313 if (isFoldableLoad(Op0))
1314 std::swap(Op0, Op1);
1315
1316 if (isFoldableLoad(Op1)) {
1317 switch (N.getValueType()) {
1318 default: assert(0 && "Cannot add this type!");
1319 case MVT::i1:
1320 case MVT::i8: Opc = X86::ADD8rm; break;
1321 case MVT::i16: Opc = X86::ADD16rm; break;
1322 case MVT::i32: Opc = X86::ADD32rm; break;
1323 case MVT::f32: Opc = X86::FADD32m; break;
1324 case MVT::f64: Opc = X86::FADD64m; break;
1325 }
1326 X86AddressMode AM;
1327 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1328 Tmp1 = SelectExpr(Op0);
1329 EmitFoldedLoad(Op1, AM);
1330 } else {
1331 EmitFoldedLoad(Op1, AM);
1332 Tmp1 = SelectExpr(Op0);
1333 }
1334 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1335 return Result;
1336 }
1337
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001338 // See if we can codegen this as an LEA to fold operations together.
1339 if (N.getValueType() == MVT::i32) {
1340 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001341 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001342 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001343 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001344 // leave this as LEA, then peephole it to 'ADD' after two address elim
1345 // happens.
1346 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001347 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001348 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1349 return Result;
1350 }
1351 }
1352 }
Chris Lattner11333092005-01-11 03:11:44 +00001353
Chris Lattnera5ade062005-01-11 21:19:59 +00001354 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001355 Opc = 0;
1356 if (CN->getValue() == 1) { // add X, 1 -> inc X
1357 switch (N.getValueType()) {
1358 default: assert(0 && "Cannot integer add this type!");
1359 case MVT::i8: Opc = X86::INC8r; break;
1360 case MVT::i16: Opc = X86::INC16r; break;
1361 case MVT::i32: Opc = X86::INC32r; break;
1362 }
1363 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1364 switch (N.getValueType()) {
1365 default: assert(0 && "Cannot integer add this type!");
1366 case MVT::i8: Opc = X86::DEC8r; break;
1367 case MVT::i16: Opc = X86::DEC16r; break;
1368 case MVT::i32: Opc = X86::DEC32r; break;
1369 }
1370 }
1371
1372 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001373 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001374 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1375 return Result;
1376 }
1377
1378 switch (N.getValueType()) {
1379 default: assert(0 && "Cannot add this type!");
1380 case MVT::i8: Opc = X86::ADD8ri; break;
1381 case MVT::i16: Opc = X86::ADD16ri; break;
1382 case MVT::i32: Opc = X86::ADD32ri; break;
1383 }
1384 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001385 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001386 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1387 return Result;
1388 }
1389 }
1390
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001391 switch (N.getValueType()) {
1392 default: assert(0 && "Cannot add this type!");
1393 case MVT::i8: Opc = X86::ADD8rr; break;
1394 case MVT::i16: Opc = X86::ADD16rr; break;
1395 case MVT::i32: Opc = X86::ADD32rr; break;
1396 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001397 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001398 }
Chris Lattner11333092005-01-11 03:11:44 +00001399
Chris Lattnera5ade062005-01-11 21:19:59 +00001400 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1401 Tmp1 = SelectExpr(Op0);
1402 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001403 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001404 Tmp2 = SelectExpr(Op1);
1405 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001406 }
1407
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001408 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1409 return Result;
1410 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001411 case ISD::MUL:
1412 case ISD::AND:
1413 case ISD::OR:
1414 case ISD::XOR:
1415 static const unsigned SUBTab[] = {
1416 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1417 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1418 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1419 };
1420 static const unsigned MULTab[] = {
1421 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1422 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1423 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1424 };
1425 static const unsigned ANDTab[] = {
1426 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1427 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1428 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1429 };
1430 static const unsigned ORTab[] = {
1431 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1432 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1433 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1434 };
1435 static const unsigned XORTab[] = {
1436 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1437 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1438 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1439 };
1440
1441 Op0 = Node->getOperand(0);
1442 Op1 = Node->getOperand(1);
1443
1444 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001445 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1446 if (CN->isNullValue()) { // 0 - N -> neg N
1447 switch (N.getValueType()) {
1448 default: assert(0 && "Cannot sub this type!");
1449 case MVT::i1:
1450 case MVT::i8: Opc = X86::NEG8r; break;
1451 case MVT::i16: Opc = X86::NEG16r; break;
1452 case MVT::i32: Opc = X86::NEG32r; break;
1453 }
1454 Tmp1 = SelectExpr(N.getOperand(1));
1455 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1456 return Result;
1457 }
1458
Chris Lattnera5ade062005-01-11 21:19:59 +00001459 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1460 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001461 switch (N.getValueType()) {
1462 default: assert(0 && "Cannot add this type!");
1463 case MVT::i1:
1464 case MVT::i8: Opc = X86::NOT8r; break;
1465 case MVT::i16: Opc = X86::NOT16r; break;
1466 case MVT::i32: Opc = X86::NOT32r; break;
1467 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001468 Tmp1 = SelectExpr(Op0);
Chris Lattnerd4dab922005-01-11 04:31:30 +00001469 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1470 return Result;
1471 }
1472
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001473 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001474 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001475 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001476 case MVT::i8: Opc = 0; break;
1477 case MVT::i16: Opc = 1; break;
1478 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001479 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001480 switch (Node->getOpcode()) {
1481 default: assert(0 && "Unreachable!");
1482 case ISD::SUB: Opc = SUBTab[Opc]; break;
1483 case ISD::MUL: Opc = MULTab[Opc]; break;
1484 case ISD::AND: Opc = ANDTab[Opc]; break;
1485 case ISD::OR: Opc = ORTab[Opc]; break;
1486 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001487 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001488 if (Opc) { // Can't fold MUL:i8 R, imm
1489 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001490 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1491 return Result;
1492 }
1493 }
Chris Lattner11333092005-01-11 03:11:44 +00001494
Chris Lattnera5ade062005-01-11 21:19:59 +00001495 if (isFoldableLoad(Op0))
1496 if (Node->getOpcode() != ISD::SUB) {
1497 std::swap(Op0, Op1);
1498 } else {
1499 // Emit 'reverse' subract, with a memory operand.
1500 switch (N.getValueType()) {
1501 default: Opc = 0; break;
1502 case MVT::f32: Opc = X86::FSUBR32m; break;
1503 case MVT::f64: Opc = X86::FSUBR64m; break;
1504 }
1505 if (Opc) {
1506 X86AddressMode AM;
1507 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1508 EmitFoldedLoad(Op0, AM);
1509 Tmp1 = SelectExpr(Op1);
1510 } else {
1511 Tmp1 = SelectExpr(Op1);
1512 EmitFoldedLoad(Op0, AM);
1513 }
1514 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1515 return Result;
1516 }
1517 }
1518
1519 if (isFoldableLoad(Op1)) {
1520 switch (N.getValueType()) {
1521 default: assert(0 && "Cannot operate on this type!");
1522 case MVT::i1:
1523 case MVT::i8: Opc = 5; break;
1524 case MVT::i16: Opc = 6; break;
1525 case MVT::i32: Opc = 7; break;
1526 case MVT::f32: Opc = 8; break;
1527 case MVT::f64: Opc = 9; break;
1528 }
1529 switch (Node->getOpcode()) {
1530 default: assert(0 && "Unreachable!");
1531 case ISD::SUB: Opc = SUBTab[Opc]; break;
1532 case ISD::MUL: Opc = MULTab[Opc]; break;
1533 case ISD::AND: Opc = ANDTab[Opc]; break;
1534 case ISD::OR: Opc = ORTab[Opc]; break;
1535 case ISD::XOR: Opc = XORTab[Opc]; break;
1536 }
1537
1538 X86AddressMode AM;
1539 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1540 Tmp1 = SelectExpr(Op0);
1541 EmitFoldedLoad(Op1, AM);
1542 } else {
1543 EmitFoldedLoad(Op1, AM);
1544 Tmp1 = SelectExpr(Op0);
1545 }
1546 if (Opc) {
1547 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1548 } else {
1549 assert(Node->getOpcode() == ISD::MUL &&
1550 N.getValueType() == MVT::i8 && "Unexpected situation!");
1551 // Must use the MUL instruction, which forces use of AL.
1552 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1553 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1554 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1555 }
1556 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001557 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001558
1559 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1560 Tmp1 = SelectExpr(Op0);
1561 Tmp2 = SelectExpr(Op1);
1562 } else {
1563 Tmp2 = SelectExpr(Op1);
1564 Tmp1 = SelectExpr(Op0);
1565 }
1566
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001567 switch (N.getValueType()) {
1568 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001569 case MVT::i1:
1570 case MVT::i8: Opc = 10; break;
1571 case MVT::i16: Opc = 11; break;
1572 case MVT::i32: Opc = 12; break;
1573 case MVT::f32: Opc = 13; break;
1574 case MVT::f64: Opc = 14; break;
1575 }
1576 switch (Node->getOpcode()) {
1577 default: assert(0 && "Unreachable!");
1578 case ISD::SUB: Opc = SUBTab[Opc]; break;
1579 case ISD::MUL: Opc = MULTab[Opc]; break;
1580 case ISD::AND: Opc = ANDTab[Opc]; break;
1581 case ISD::OR: Opc = ORTab[Opc]; break;
1582 case ISD::XOR: Opc = XORTab[Opc]; break;
1583 }
1584 if (Opc) {
1585 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1586 } else {
1587 assert(Node->getOpcode() == ISD::MUL &&
1588 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001589 // Must use the MUL instruction, which forces use of AL.
1590 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1591 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1592 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001593 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001594 return Result;
1595
1596 case ISD::SELECT:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001597 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001598 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1599 Tmp2 = SelectExpr(N.getOperand(1));
1600 Tmp3 = SelectExpr(N.getOperand(2));
1601 } else {
1602 Tmp3 = SelectExpr(N.getOperand(2));
1603 Tmp2 = SelectExpr(N.getOperand(1));
1604 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001605 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001606 return Result;
1607 } else {
1608 // FIXME: This should not be implemented here, it should be in the generic
1609 // code!
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001610 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1611 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1612 N.getOperand(1)));
1613 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1614 N.getOperand(2)));
1615 } else {
1616 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1617 N.getOperand(2)));
1618 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1619 N.getOperand(1)));
1620 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001621 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001622 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1623 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001624 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1625 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1626 return Result;
1627 }
1628
1629 case ISD::SDIV:
1630 case ISD::UDIV:
1631 case ISD::SREM:
1632 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001633 if (N.getOpcode() == ISD::SDIV)
1634 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1635 // FIXME: These special cases should be handled by the lowering impl!
1636 unsigned RHS = CN->getValue();
1637 bool isNeg = false;
1638 if ((int)RHS < 0) {
1639 isNeg = true;
1640 RHS = -RHS;
1641 }
1642 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1643 unsigned Log = log2(RHS);
1644 unsigned TmpReg = MakeReg(N.getValueType());
1645 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1646 switch (N.getValueType()) {
1647 default: assert("Unknown type to signed divide!");
1648 case MVT::i8:
1649 SAROpc = X86::SAR8ri;
1650 SHROpc = X86::SHR8ri;
1651 ADDOpc = X86::ADD8rr;
1652 NEGOpc = X86::NEG8r;
1653 break;
1654 case MVT::i16:
1655 SAROpc = X86::SAR16ri;
1656 SHROpc = X86::SHR16ri;
1657 ADDOpc = X86::ADD16rr;
1658 NEGOpc = X86::NEG16r;
1659 break;
1660 case MVT::i32:
1661 SAROpc = X86::SAR32ri;
1662 SHROpc = X86::SHR32ri;
1663 ADDOpc = X86::ADD32rr;
1664 NEGOpc = X86::NEG32r;
1665 break;
1666 }
Chris Lattner11333092005-01-11 03:11:44 +00001667 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001668 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1669 unsigned TmpReg2 = MakeReg(N.getValueType());
1670 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1671 unsigned TmpReg3 = MakeReg(N.getValueType());
1672 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1673
1674 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1675 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1676 if (isNeg)
1677 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1678 return Result;
1679 }
1680 }
1681
Chris Lattner11333092005-01-11 03:11:44 +00001682 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1683 Tmp1 = SelectExpr(N.getOperand(0));
1684 Tmp2 = SelectExpr(N.getOperand(1));
1685 } else {
1686 Tmp2 = SelectExpr(N.getOperand(1));
1687 Tmp1 = SelectExpr(N.getOperand(0));
1688 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001689
1690 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1691 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1692 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1693 switch (N.getValueType()) {
1694 default: assert(0 && "Cannot sdiv this type!");
1695 case MVT::i8:
1696 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1697 LoReg = X86::AL;
1698 HiReg = X86::AH;
1699 MovOpcode = X86::MOV8rr;
1700 ClrOpcode = X86::MOV8ri;
1701 SExtOpcode = X86::CBW;
1702 break;
1703 case MVT::i16:
1704 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1705 LoReg = X86::AX;
1706 HiReg = X86::DX;
1707 MovOpcode = X86::MOV16rr;
1708 ClrOpcode = X86::MOV16ri;
1709 SExtOpcode = X86::CWD;
1710 break;
1711 case MVT::i32:
1712 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
1713 LoReg =X86::EAX;
1714 HiReg = X86::EDX;
1715 MovOpcode = X86::MOV32rr;
1716 ClrOpcode = X86::MOV32ri;
1717 SExtOpcode = X86::CDQ;
1718 break;
1719 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1720 case MVT::f32:
1721 case MVT::f64:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001722 if (N.getOpcode() == ISD::SDIV)
1723 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1724 else
1725 assert(0 && "FIXME: Emit frem libcall to fmod!");
1726 return Result;
1727 }
1728
1729 // Set up the low part.
1730 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1731
1732 if (isSigned) {
1733 // Sign extend the low part into the high part.
1734 BuildMI(BB, SExtOpcode, 0);
1735 } else {
1736 // Zero out the high part, effectively zero extending the input.
1737 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1738 }
1739
1740 // Emit the DIV/IDIV instruction.
1741 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1742
1743 // Get the result of the divide or rem.
1744 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1745 return Result;
1746 }
1747
1748 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001749 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001750 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1751 switch (N.getValueType()) {
1752 default: assert(0 && "Cannot shift this type!");
1753 case MVT::i8: Opc = X86::ADD8rr; break;
1754 case MVT::i16: Opc = X86::ADD16rr; break;
1755 case MVT::i32: Opc = X86::ADD32rr; break;
1756 }
1757 Tmp1 = SelectExpr(N.getOperand(0));
1758 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1759 return Result;
1760 }
1761
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001762 switch (N.getValueType()) {
1763 default: assert(0 && "Cannot shift this type!");
1764 case MVT::i8: Opc = X86::SHL8ri; break;
1765 case MVT::i16: Opc = X86::SHL16ri; break;
1766 case MVT::i32: Opc = X86::SHL32ri; break;
1767 }
Chris Lattner11333092005-01-11 03:11:44 +00001768 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001769 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1770 return Result;
1771 }
Chris Lattner11333092005-01-11 03:11:44 +00001772
1773 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1774 Tmp1 = SelectExpr(N.getOperand(0));
1775 Tmp2 = SelectExpr(N.getOperand(1));
1776 } else {
1777 Tmp2 = SelectExpr(N.getOperand(1));
1778 Tmp1 = SelectExpr(N.getOperand(0));
1779 }
1780
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001781 switch (N.getValueType()) {
1782 default: assert(0 && "Cannot shift this type!");
1783 case MVT::i8 : Opc = X86::SHL8rCL; break;
1784 case MVT::i16: Opc = X86::SHL16rCL; break;
1785 case MVT::i32: Opc = X86::SHL32rCL; break;
1786 }
1787 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1788 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1789 return Result;
1790 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001791 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1792 switch (N.getValueType()) {
1793 default: assert(0 && "Cannot shift this type!");
1794 case MVT::i8: Opc = X86::SHR8ri; break;
1795 case MVT::i16: Opc = X86::SHR16ri; break;
1796 case MVT::i32: Opc = X86::SHR32ri; break;
1797 }
Chris Lattner11333092005-01-11 03:11:44 +00001798 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001799 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1800 return Result;
1801 }
Chris Lattner11333092005-01-11 03:11:44 +00001802
1803 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1804 Tmp1 = SelectExpr(N.getOperand(0));
1805 Tmp2 = SelectExpr(N.getOperand(1));
1806 } else {
1807 Tmp2 = SelectExpr(N.getOperand(1));
1808 Tmp1 = SelectExpr(N.getOperand(0));
1809 }
1810
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001811 switch (N.getValueType()) {
1812 default: assert(0 && "Cannot shift this type!");
1813 case MVT::i8 : Opc = X86::SHR8rCL; break;
1814 case MVT::i16: Opc = X86::SHR16rCL; break;
1815 case MVT::i32: Opc = X86::SHR32rCL; break;
1816 }
1817 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1818 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1819 return Result;
1820 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001821 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1822 switch (N.getValueType()) {
1823 default: assert(0 && "Cannot shift this type!");
1824 case MVT::i8: Opc = X86::SAR8ri; break;
1825 case MVT::i16: Opc = X86::SAR16ri; break;
1826 case MVT::i32: Opc = X86::SAR32ri; break;
1827 }
Chris Lattner11333092005-01-11 03:11:44 +00001828 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001829 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1830 return Result;
1831 }
Chris Lattner11333092005-01-11 03:11:44 +00001832
1833 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1834 Tmp1 = SelectExpr(N.getOperand(0));
1835 Tmp2 = SelectExpr(N.getOperand(1));
1836 } else {
1837 Tmp2 = SelectExpr(N.getOperand(1));
1838 Tmp1 = SelectExpr(N.getOperand(0));
1839 }
1840
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001841 switch (N.getValueType()) {
1842 default: assert(0 && "Cannot shift this type!");
1843 case MVT::i8 : Opc = X86::SAR8rCL; break;
1844 case MVT::i16: Opc = X86::SAR16rCL; break;
1845 case MVT::i32: Opc = X86::SAR32rCL; break;
1846 }
1847 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1848 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1849 return Result;
1850
1851 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001852 EmitCMP(N.getOperand(0), N.getOperand(1));
1853 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1854 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1855 return Result;
1856 case ISD::LOAD: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001857 // Make sure we generate both values.
1858 if (Result != 1)
1859 ExprMap[N.getValue(1)] = 1; // Generate the token
1860 else
1861 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1862
Chris Lattner5188ad72005-01-08 19:28:19 +00001863 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001864 default: assert(0 && "Cannot load this type!");
1865 case MVT::i1:
1866 case MVT::i8: Opc = X86::MOV8rm; break;
1867 case MVT::i16: Opc = X86::MOV16rm; break;
1868 case MVT::i32: Opc = X86::MOV32rm; break;
1869 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1870 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1871 }
Chris Lattner11333092005-01-11 03:11:44 +00001872
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001873 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001874 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001875 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1876 } else {
1877 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001878 EmitFoldedLoad(N, AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001879 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1880 }
1881 return Result;
1882 }
1883 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001884 // Generate both result values.
1885 if (Result != 1)
1886 ExprMap[N.getValue(1)] = 1; // Generate the token
1887 else
1888 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1889
1890 // FIXME: We are currently ignoring the requested alignment for handling
1891 // greater than the stack alignment. This will need to be revisited at some
1892 // point. Align = N.getOperand(2);
1893
1894 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1895 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1896 std::cerr << "Cannot allocate stack object with greater alignment than"
1897 << " the stack alignment yet!";
1898 abort();
1899 }
1900
1901 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001902 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001903 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1904 .addImm(CN->getValue());
1905 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001906 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1907 Select(N.getOperand(0));
1908 Tmp1 = SelectExpr(N.getOperand(1));
1909 } else {
1910 Tmp1 = SelectExpr(N.getOperand(1));
1911 Select(N.getOperand(0));
1912 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001913
1914 // Subtract size from stack pointer, thereby allocating some space.
1915 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
1916 }
1917
1918 // Put a pointer to the space into the result register, by copying the stack
1919 // pointer.
1920 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
1921 return Result;
1922
1923 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00001924 // The chain for this call is now lowered.
1925 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
1926
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001927 if (GlobalAddressSDNode *GASD =
1928 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001929 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001930 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
1931 } else if (ExternalSymbolSDNode *ESSDN =
1932 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001933 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001934 BuildMI(BB, X86::CALLpcrel32,
1935 1).addExternalSymbol(ESSDN->getSymbol(), true);
1936 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001937 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1938 Select(N.getOperand(0));
1939 Tmp1 = SelectExpr(N.getOperand(1));
1940 } else {
1941 Tmp1 = SelectExpr(N.getOperand(1));
1942 Select(N.getOperand(0));
1943 }
1944
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001945 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
1946 }
Chris Lattner5188ad72005-01-08 19:28:19 +00001947 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001948 default: assert(0 && "Unknown value type for call result!");
1949 case MVT::Other: return 1;
1950 case MVT::i1:
1951 case MVT::i8:
1952 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1953 break;
1954 case MVT::i16:
1955 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
1956 break;
1957 case MVT::i32:
1958 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00001959 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001960 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
1961 break;
1962 case MVT::f32:
1963 case MVT::f64: // Floating-point return values live in %ST(0)
1964 ContainsFPCode = true;
1965 BuildMI(BB, X86::FpGETRESULT, 1, Result);
1966 break;
1967 }
1968 return Result+N.ResNo;
1969 }
1970
1971 return 0;
1972}
1973
1974void ISel::Select(SDOperand N) {
1975 unsigned Tmp1, Tmp2, Opc;
1976
1977 // FIXME: Disable for our current expansion model!
1978 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
1979 return; // Already selected.
1980
Chris Lattner989de032005-01-11 06:14:36 +00001981 SDNode *Node = N.Val;
1982
1983 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001984 default:
Chris Lattner989de032005-01-11 06:14:36 +00001985 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001986 assert(0 && "Node not handled yet!");
1987 case ISD::EntryToken: return; // Noop
1988 case ISD::CopyToReg:
1989 Select(N.getOperand(0));
1990 Tmp1 = SelectExpr(N.getOperand(1));
1991 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
1992
1993 if (Tmp1 != Tmp2) {
1994 switch (N.getOperand(1).getValueType()) {
1995 default: assert(0 && "Invalid type for operation!");
1996 case MVT::i1:
1997 case MVT::i8: Opc = X86::MOV8rr; break;
1998 case MVT::i16: Opc = X86::MOV16rr; break;
1999 case MVT::i32: Opc = X86::MOV32rr; break;
2000 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002001 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002002 }
2003 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2004 }
2005 return;
2006 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002007 switch (N.getNumOperands()) {
2008 default:
2009 assert(0 && "Unknown return instruction!");
2010 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002011 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2012 N.getOperand(2).getValueType() == MVT::i32 &&
2013 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002014 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2015 Tmp1 = SelectExpr(N.getOperand(1));
2016 Tmp2 = SelectExpr(N.getOperand(2));
2017 } else {
2018 Tmp2 = SelectExpr(N.getOperand(2));
2019 Tmp1 = SelectExpr(N.getOperand(1));
2020 }
2021 Select(N.getOperand(0));
2022
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002023 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2024 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2025 // Declare that EAX & EDX are live on exit.
2026 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2027 .addReg(X86::ESP);
2028 break;
2029 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002030 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2031 Select(N.getOperand(0));
2032 Tmp1 = SelectExpr(N.getOperand(1));
2033 } else {
2034 Tmp1 = SelectExpr(N.getOperand(1));
2035 Select(N.getOperand(0));
2036 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002037 switch (N.getOperand(1).getValueType()) {
2038 default: assert(0 && "All other types should have been promoted!!");
2039 case MVT::f64:
2040 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2041 // Declare that top-of-stack is live on exit
2042 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2043 break;
2044 case MVT::i32:
2045 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2046 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2047 break;
2048 }
2049 break;
2050 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002051 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002052 break;
2053 }
2054 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2055 return;
2056 case ISD::BR: {
2057 Select(N.getOperand(0));
2058 MachineBasicBlock *Dest =
2059 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2060 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2061 return;
2062 }
2063
2064 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002065 MachineBasicBlock *Dest =
2066 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002067
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002068 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2069 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002070 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2071 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2072 Select(N.getOperand(0));
2073 Tmp1 = SelectExpr(N.getOperand(1));
2074 } else {
2075 Tmp1 = SelectExpr(N.getOperand(1));
2076 Select(N.getOperand(0));
2077 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002078 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2079 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2080 }
Chris Lattner11333092005-01-11 03:11:44 +00002081
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002082 return;
2083 }
2084 case ISD::LOAD:
2085 case ISD::CALL:
2086 case ISD::DYNAMIC_STACKALLOC:
2087 SelectExpr(N);
2088 return;
2089 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002090 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002091
2092 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2093 Opc = 0;
2094 switch (CN->getValueType(0)) {
2095 default: assert(0 && "Invalid type for operation!");
2096 case MVT::i1:
2097 case MVT::i8: Opc = X86::MOV8mi; break;
2098 case MVT::i16: Opc = X86::MOV16mi; break;
2099 case MVT::i32: Opc = X86::MOV32mi; break;
2100 case MVT::f32:
2101 case MVT::f64: break;
2102 }
2103 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002104 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2105 Select(N.getOperand(0));
2106 SelectAddress(N.getOperand(2), AM);
2107 } else {
2108 SelectAddress(N.getOperand(2), AM);
2109 Select(N.getOperand(0));
2110 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002111 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2112 return;
2113 }
2114 }
Chris Lattner837caa72005-01-11 23:21:30 +00002115
2116 // Check to see if this is a load/op/store combination.
2117 if (N.getOperand(1).Val->hasOneUse() &&
2118 isFoldableLoad(N.getOperand(0).getValue(0))) {
2119 SDOperand TheLoad = N.getOperand(0).getValue(0);
2120 // Check to see if we are loading the same pointer that we're storing to.
2121 if (TheLoad.getOperand(1) == N.getOperand(2)) {
2122 // See if the stored value is a simple binary operator that uses the
2123 // load as one of its operands.
2124 SDOperand Op = N.getOperand(1);
2125 if (Op.Val->getNumOperands() == 2 &&
2126 (Op.getOperand(0) == TheLoad || Op.getOperand(1) == TheLoad)) {
2127 // Finally, check to see if this is one of the ops we can handle!
2128 static const unsigned ADDTAB[] = {
2129 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2130 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr, 0, 0,
2131 };
Chris Lattner7ea64f52005-01-12 01:28:00 +00002132 static const unsigned SUBTAB[] = {
2133 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2134 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr, 0, 0,
2135 };
2136 static const unsigned ANDTAB[] = {
2137 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2138 X86::AND8mr, X86::AND16mr, X86::AND32mr, 0, 0,
2139 };
2140 static const unsigned ORTAB[] = {
2141 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2142 X86::OR8mr, X86::OR16mr, X86::OR32mr, 0, 0,
2143 };
2144 static const unsigned XORTAB[] = {
2145 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2146 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr, 0, 0,
2147 };
2148 static const unsigned SHLTAB[] = {
2149 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2150 /*Have to put the reg in CL*/0, 0, 0, 0, 0,
2151 };
2152 static const unsigned SARTAB[] = {
2153 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2154 /*Have to put the reg in CL*/0, 0, 0, 0, 0,
2155 };
2156 static const unsigned SHRTAB[] = {
2157 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2158 /*Have to put the reg in CL*/0, 0, 0, 0, 0,
2159 };
Chris Lattner837caa72005-01-11 23:21:30 +00002160
2161 const unsigned *TabPtr = 0;
2162 switch (Op.getOpcode()) {
Chris Lattner7ea64f52005-01-12 01:28:00 +00002163 default: std::cerr << "CANNOT [mem] op= val: "; Op.Val->dump(); std::cerr << "\n"; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002164 case ISD::ADD: TabPtr = ADDTAB; break;
Chris Lattner7ea64f52005-01-12 01:28:00 +00002165 case ISD::SUB: TabPtr = SUBTAB; break;
2166 case ISD::AND: TabPtr = ANDTAB; break;
2167 case ISD:: OR: TabPtr = ORTAB; break;
2168 case ISD::XOR: TabPtr = XORTAB; break;
2169 case ISD::SHL: TabPtr = SHLTAB; break;
2170 case ISD::SRA: TabPtr = SARTAB; break;
2171 case ISD::SRL: TabPtr = SHRTAB; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002172 }
2173
2174 if (TabPtr) {
2175 // Handle: [mem] op= CST
2176 SDOperand Op0 = Op.getOperand(0);
2177 SDOperand Op1 = Op.getOperand(1);
2178 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2179 switch (CN->getValueType(0)) {
2180 default: break;
2181 case MVT::i1:
2182 case MVT::i8: Opc = TabPtr[0]; break;
2183 case MVT::i16: Opc = TabPtr[1]; break;
2184 case MVT::i32: Opc = TabPtr[2]; break;
2185 }
2186
2187 if (Opc) {
2188 if (getRegPressure(TheLoad.getOperand(0)) >
2189 getRegPressure(TheLoad.getOperand(1))) {
2190 Select(TheLoad.getOperand(0));
2191 SelectAddress(TheLoad.getOperand(1), AM);
2192 } else {
2193 SelectAddress(TheLoad.getOperand(1), AM);
2194 Select(TheLoad.getOperand(0));
2195 }
2196
2197 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2198 return;
2199 }
2200 }
2201
2202 // If we have [mem] = V op [mem], try to turn it into:
2203 // [mem] = [mem] op V.
Chris Lattner7ea64f52005-01-12 01:28:00 +00002204 if (Op1 == TheLoad && Op.getOpcode() != ISD::SUB &&
2205 Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRA &&
2206 Op.getOpcode() != ISD::SRL)
Chris Lattner837caa72005-01-11 23:21:30 +00002207 std::swap(Op0, Op1);
2208
2209 if (Op0 == TheLoad) {
2210 switch (Op0.getValueType()) {
2211 default: break;
2212 case MVT::i1:
2213 case MVT::i8: Opc = TabPtr[3]; break;
2214 case MVT::i16: Opc = TabPtr[4]; break;
2215 case MVT::i32: Opc = TabPtr[5]; break;
2216 case MVT::f32: Opc = TabPtr[6]; break;
2217 case MVT::f64: Opc = TabPtr[7]; break;
2218 }
2219
2220 if (Opc) {
2221 Select(TheLoad.getOperand(0));
2222 SelectAddress(TheLoad.getOperand(1), AM);
2223 unsigned Reg = SelectExpr(Op1);
2224 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addReg(Reg);
2225 return;
2226 }
2227 }
Chris Lattner837caa72005-01-11 23:21:30 +00002228 }
2229 }
2230 }
2231 }
2232
2233
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002234 switch (N.getOperand(1).getValueType()) {
2235 default: assert(0 && "Cannot store this type!");
2236 case MVT::i1:
2237 case MVT::i8: Opc = X86::MOV8mr; break;
2238 case MVT::i16: Opc = X86::MOV16mr; break;
2239 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002240 case MVT::f32: Opc = X86::FST32m; break;
2241 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002242 }
Chris Lattner11333092005-01-11 03:11:44 +00002243
2244 std::vector<std::pair<unsigned, unsigned> > RP;
2245 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2246 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2247 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2248 std::sort(RP.begin(), RP.end());
2249
2250 for (unsigned i = 0; i != 3; ++i)
2251 switch (RP[2-i].second) {
2252 default: assert(0 && "Unknown operand number!");
2253 case 0: Select(N.getOperand(0)); break;
2254 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002255 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002256 }
2257
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002258 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2259 return;
2260 }
2261 case ISD::ADJCALLSTACKDOWN:
2262 case ISD::ADJCALLSTACKUP:
2263 Select(N.getOperand(0));
2264 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2265
2266 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2267 X86::ADJCALLSTACKUP;
2268 BuildMI(BB, Opc, 1).addImm(Tmp1);
2269 return;
Chris Lattner989de032005-01-11 06:14:36 +00002270 case ISD::MEMSET: {
2271 Select(N.getOperand(0)); // Select the chain.
2272 unsigned Align =
2273 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2274 if (Align == 0) Align = 1;
2275
2276 // Turn the byte code into # iterations
2277 unsigned CountReg;
2278 unsigned Opcode;
2279 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2280 unsigned Val = ValC->getValue() & 255;
2281
2282 // If the value is a constant, then we can potentially use larger sets.
2283 switch (Align & 3) {
2284 case 2: // WORD aligned
2285 CountReg = MakeReg(MVT::i32);
2286 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2287 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2288 } else {
2289 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2290 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2291 }
2292 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2293 Opcode = X86::REP_STOSW;
2294 break;
2295 case 0: // DWORD aligned
2296 CountReg = MakeReg(MVT::i32);
2297 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2298 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2299 } else {
2300 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2301 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2302 }
2303 Val = (Val << 8) | Val;
2304 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2305 Opcode = X86::REP_STOSD;
2306 break;
2307 default: // BYTE aligned
2308 CountReg = SelectExpr(Node->getOperand(3));
2309 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2310 Opcode = X86::REP_STOSB;
2311 break;
2312 }
2313 } else {
2314 // If it's not a constant value we are storing, just fall back. We could
2315 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2316 unsigned ValReg = SelectExpr(Node->getOperand(2));
2317 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2318 CountReg = SelectExpr(Node->getOperand(3));
2319 Opcode = X86::REP_STOSB;
2320 }
2321
2322 // No matter what the alignment is, we put the source in ESI, the
2323 // destination in EDI, and the count in ECX.
2324 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2325 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2326 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2327 BuildMI(BB, Opcode, 0);
2328 return;
2329 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002330 case ISD::MEMCPY:
2331 Select(N.getOperand(0)); // Select the chain.
2332 unsigned Align =
2333 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2334 if (Align == 0) Align = 1;
2335
2336 // Turn the byte code into # iterations
2337 unsigned CountReg;
2338 unsigned Opcode;
2339 switch (Align & 3) {
2340 case 2: // WORD 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()/2);
2344 } else {
2345 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2346 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2347 }
2348 Opcode = X86::REP_MOVSW;
2349 break;
2350 case 0: // DWORD aligned
2351 CountReg = MakeReg(MVT::i32);
2352 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2353 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2354 } else {
2355 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2356 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2357 }
2358 Opcode = X86::REP_MOVSD;
2359 break;
2360 default: // BYTE aligned
2361 CountReg = SelectExpr(Node->getOperand(3));
2362 Opcode = X86::REP_MOVSB;
2363 break;
2364 }
2365
2366 // No matter what the alignment is, we put the source in ESI, the
2367 // destination in EDI, and the count in ECX.
2368 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2369 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2370 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2371 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2372 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2373 BuildMI(BB, Opcode, 0);
2374 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002375 }
2376 assert(0 && "Should not be reached!");
2377}
2378
2379
2380/// createX86PatternInstructionSelector - This pass converts an LLVM function
2381/// into a machine code representation using pattern matching and a machine
2382/// description file.
2383///
2384FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2385 return new ISel(TM);
2386}