blob: caef663d7b6dbd806b078bfb1a2c1083d7d04798 [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 Lattner11333092005-01-11 03:11:44 +0000391 if (N->getNumOperands() == 0)
392 return Result = 1;
393
394 unsigned MaxRegUse = 0;
395 unsigned NumExtraMaxRegUsers = 0;
396 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
397 unsigned Regs = ComputeRegPressure(N->getOperand(i));
398 if (Regs > MaxRegUse) {
399 MaxRegUse = Regs;
400 NumExtraMaxRegUsers = 0;
401 } else if (Regs == MaxRegUse) {
402 ++NumExtraMaxRegUsers;
403 }
404 }
405
406 return Result = MaxRegUse+NumExtraMaxRegUsers;
407}
408
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000409/// SelectAddress - Add the specified node to the specified addressing mode,
410/// returning true if it cannot be done.
411bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
412 switch (N.getOpcode()) {
413 default: break;
414 case ISD::FrameIndex:
415 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
416 AM.BaseType = X86AddressMode::FrameIndexBase;
417 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
418 return false;
419 }
420 break;
421 case ISD::GlobalAddress:
422 if (AM.GV == 0) {
423 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
424 return false;
425 }
426 break;
427 case ISD::Constant:
428 AM.Disp += cast<ConstantSDNode>(N)->getValue();
429 return false;
430 case ISD::SHL:
431 if (AM.IndexReg == 0 || AM.Scale == 1)
432 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
433 unsigned Val = CN->getValue();
434 if (Val == 1 || Val == 2 || Val == 3) {
435 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000436 SDOperand ShVal = N.Val->getOperand(0);
437
438 // Okay, we know that we have a scale by now. However, if the scaled
439 // value is an add of something and a constant, we can fold the
440 // constant into the disp field here.
441 if (ShVal.Val->getOpcode() == ISD::ADD &&
442 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
443 AM.IndexReg = SelectExpr(ShVal.Val->getOperand(0));
444 ConstantSDNode *AddVal =
445 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
446 AM.Disp += AddVal->getValue() << Val;
447 } else {
448 AM.IndexReg = SelectExpr(ShVal);
449 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000450 return false;
451 }
452 }
453 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000454 case ISD::MUL:
455 // X*[3,5,9] -> X+X*[2,4,8]
456 if (AM.IndexReg == 0 && AM.BaseType == X86AddressMode::RegBase &&
457 AM.Base.Reg == 0)
458 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
459 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
460 AM.Scale = unsigned(CN->getValue())-1;
461
462 SDOperand MulVal = N.Val->getOperand(0);
463 unsigned Reg;
464
465 // Okay, we know that we have a scale by now. However, if the scaled
466 // value is an add of something and a constant, we can fold the
467 // constant into the disp field here.
468 if (MulVal.Val->getOpcode() == ISD::ADD &&
469 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
470 Reg = SelectExpr(MulVal.Val->getOperand(0));
471 ConstantSDNode *AddVal =
472 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
473 AM.Disp += AddVal->getValue() * CN->getValue();
474 } else {
475 Reg = SelectExpr(N.Val->getOperand(0));
476 }
477
478 AM.IndexReg = AM.Base.Reg = Reg;
479 return false;
480 }
481 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000482
483 case ISD::ADD: {
484 X86AddressMode Backup = AM;
485 if (!SelectAddress(N.Val->getOperand(0), AM) &&
486 !SelectAddress(N.Val->getOperand(1), AM))
487 return false;
488 AM = Backup;
489 break;
490 }
491 }
492
Chris Lattnera95589b2005-01-11 04:40:19 +0000493 // Is the base register already occupied?
494 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
495 // If so, check to see if the scale index register is set.
496 if (AM.IndexReg == 0) {
497 AM.IndexReg = SelectExpr(N);
498 AM.Scale = 1;
499 return false;
500 }
501
502 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000503 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000504 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000505
506 // Default, generate it as a register.
507 AM.BaseType = X86AddressMode::RegBase;
508 AM.Base.Reg = SelectExpr(N);
509 return false;
510}
511
512/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
513/// assuming that the temporary registers are in the 8-bit register class.
514///
515/// Tmp1 = setcc1
516/// Tmp2 = setcc2
517/// DestReg = logicalop Tmp1, Tmp2
518///
519static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
520 unsigned SetCC2, unsigned LogicalOp,
521 unsigned DestReg) {
522 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
523 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
524 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
525 BuildMI(BB, SetCC1, 0, Tmp1);
526 BuildMI(BB, SetCC2, 0, Tmp2);
527 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
528}
529
530/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
531/// condition codes match the specified SetCCOpcode. Note that some conditions
532/// require multiple instructions to generate the correct value.
533static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
534 ISD::CondCode SetCCOpcode, bool isFP) {
535 unsigned Opc;
536 if (!isFP) {
537 switch (SetCCOpcode) {
538 default: assert(0 && "Illegal integer SetCC!");
539 case ISD::SETEQ: Opc = X86::SETEr; break;
540 case ISD::SETGT: Opc = X86::SETGr; break;
541 case ISD::SETGE: Opc = X86::SETGEr; break;
542 case ISD::SETLT: Opc = X86::SETLr; break;
543 case ISD::SETLE: Opc = X86::SETLEr; break;
544 case ISD::SETNE: Opc = X86::SETNEr; break;
545 case ISD::SETULT: Opc = X86::SETBr; break;
546 case ISD::SETUGT: Opc = X86::SETAr; break;
547 case ISD::SETULE: Opc = X86::SETBEr; break;
548 case ISD::SETUGE: Opc = X86::SETAEr; break;
549 }
550 } else {
551 // On a floating point condition, the flags are set as follows:
552 // ZF PF CF op
553 // 0 | 0 | 0 | X > Y
554 // 0 | 0 | 1 | X < Y
555 // 1 | 0 | 0 | X == Y
556 // 1 | 1 | 1 | unordered
557 //
558 switch (SetCCOpcode) {
559 default: assert(0 && "Invalid FP setcc!");
560 case ISD::SETUEQ:
561 case ISD::SETEQ:
562 Opc = X86::SETEr; // True if ZF = 1
563 break;
564 case ISD::SETOGT:
565 case ISD::SETGT:
566 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
567 break;
568 case ISD::SETOGE:
569 case ISD::SETGE:
570 Opc = X86::SETAEr; // True if CF = 0
571 break;
572 case ISD::SETULT:
573 case ISD::SETLT:
574 Opc = X86::SETBr; // True if CF = 1
575 break;
576 case ISD::SETULE:
577 case ISD::SETLE:
578 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
579 break;
580 case ISD::SETONE:
581 case ISD::SETNE:
582 Opc = X86::SETNEr; // True if ZF = 0
583 break;
584 case ISD::SETUO:
585 Opc = X86::SETPr; // True if PF = 1
586 break;
587 case ISD::SETO:
588 Opc = X86::SETNPr; // True if PF = 0
589 break;
590 case ISD::SETOEQ: // !PF & ZF
591 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
592 return;
593 case ISD::SETOLT: // !PF & CF
594 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
595 return;
596 case ISD::SETOLE: // !PF & (CF || ZF)
597 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
598 return;
599 case ISD::SETUGT: // PF | (!ZF & !CF)
600 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
601 return;
602 case ISD::SETUGE: // PF | !CF
603 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
604 return;
605 case ISD::SETUNE: // PF | !ZF
606 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
607 return;
608 }
609 }
610 BuildMI(BB, Opc, 0, DestReg);
611}
612
613
614/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
615/// the Dest block if the Cond condition is true. If we cannot fold this
616/// condition into the branch, return true.
617///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000618bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
619 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000620 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
621 // B) using two conditional branches instead of one condbr, two setcc's, and
622 // an or.
623 if ((Cond.getOpcode() == ISD::OR ||
624 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
625 // And and or set the flags for us, so there is no need to emit a TST of the
626 // result. It is only safe to do this if there is only a single use of the
627 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000628 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000629 SelectExpr(Cond);
630 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
631 return false;
632 }
633
634 // Codegen br not C -> JE.
635 if (Cond.getOpcode() == ISD::XOR)
636 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
637 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000638 unsigned CondR;
639 if (getRegPressure(Chain) > getRegPressure(Cond)) {
640 Select(Chain);
641 CondR = SelectExpr(Cond.Val->getOperand(0));
642 } else {
643 CondR = SelectExpr(Cond.Val->getOperand(0));
644 Select(Chain);
645 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000646 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
647 BuildMI(BB, X86::JE, 1).addMBB(Dest);
648 return false;
649 }
650
651 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
652 if (SetCC == 0)
653 return true; // Can only handle simple setcc's so far.
654
655 unsigned Opc;
656
657 // Handle integer conditions first.
658 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
659 switch (SetCC->getCondition()) {
660 default: assert(0 && "Illegal integer SetCC!");
661 case ISD::SETEQ: Opc = X86::JE; break;
662 case ISD::SETGT: Opc = X86::JG; break;
663 case ISD::SETGE: Opc = X86::JGE; break;
664 case ISD::SETLT: Opc = X86::JL; break;
665 case ISD::SETLE: Opc = X86::JLE; break;
666 case ISD::SETNE: Opc = X86::JNE; break;
667 case ISD::SETULT: Opc = X86::JB; break;
668 case ISD::SETUGT: Opc = X86::JA; break;
669 case ISD::SETULE: Opc = X86::JBE; break;
670 case ISD::SETUGE: Opc = X86::JAE; break;
671 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000672 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000673 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
674 BuildMI(BB, Opc, 1).addMBB(Dest);
675 return false;
676 }
677
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000678 unsigned Opc2 = 0; // Second branch if needed.
679
680 // On a floating point condition, the flags are set as follows:
681 // ZF PF CF op
682 // 0 | 0 | 0 | X > Y
683 // 0 | 0 | 1 | X < Y
684 // 1 | 0 | 0 | X == Y
685 // 1 | 1 | 1 | unordered
686 //
687 switch (SetCC->getCondition()) {
688 default: assert(0 && "Invalid FP setcc!");
689 case ISD::SETUEQ:
690 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
691 case ISD::SETOGT:
692 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
693 case ISD::SETOGE:
694 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
695 case ISD::SETULT:
696 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
697 case ISD::SETULE:
698 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
699 case ISD::SETONE:
700 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
701 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
702 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
703 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
704 Opc = X86::JA; // ZF = 0 & CF = 0
705 Opc2 = X86::JP; // PF = 1
706 break;
707 case ISD::SETUGE: // PF = 1 | CF = 0
708 Opc = X86::JAE; // CF = 0
709 Opc2 = X86::JP; // PF = 1
710 break;
711 case ISD::SETUNE: // PF = 1 | ZF = 0
712 Opc = X86::JNE; // ZF = 0
713 Opc2 = X86::JP; // PF = 1
714 break;
715 case ISD::SETOEQ: // PF = 0 & ZF = 1
716 //X86::JNP, X86::JE
717 //X86::AND8rr
718 return true; // FIXME: Emit more efficient code for this branch.
719 case ISD::SETOLT: // PF = 0 & CF = 1
720 //X86::JNP, X86::JB
721 //X86::AND8rr
722 return true; // FIXME: Emit more efficient code for this branch.
723 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
724 //X86::JNP, X86::JBE
725 //X86::AND8rr
726 return true; // FIXME: Emit more efficient code for this branch.
727 }
728
Chris Lattner6c07aee2005-01-11 04:06:27 +0000729 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000730 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
731 BuildMI(BB, Opc, 1).addMBB(Dest);
732 if (Opc2)
733 BuildMI(BB, Opc2, 1).addMBB(Dest);
734 return false;
735}
736
Chris Lattner24aad1b2005-01-10 22:10:13 +0000737/// EmitSelectCC - Emit code into BB that performs a select operation between
738/// the two registers RTrue and RFalse, generating a result into RDest. Return
739/// true if the fold cannot be performed.
740///
741void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
742 unsigned RTrue, unsigned RFalse, unsigned RDest) {
743 enum Condition {
744 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
745 NOT_SET
746 } CondCode = NOT_SET;
747
748 static const unsigned CMOVTAB16[] = {
749 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
750 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
751 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
752 };
753 static const unsigned CMOVTAB32[] = {
754 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
755 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
756 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
757 };
758 static const unsigned CMOVTABFP[] = {
759 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
760 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
761 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
762 };
763
764 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
765 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
766 switch (SetCC->getCondition()) {
767 default: assert(0 && "Unknown integer comparison!");
768 case ISD::SETEQ: CondCode = EQ; break;
769 case ISD::SETGT: CondCode = GT; break;
770 case ISD::SETGE: CondCode = GE; break;
771 case ISD::SETLT: CondCode = LT; break;
772 case ISD::SETLE: CondCode = LE; break;
773 case ISD::SETNE: CondCode = NE; break;
774 case ISD::SETULT: CondCode = B; break;
775 case ISD::SETUGT: CondCode = A; break;
776 case ISD::SETULE: CondCode = BE; break;
777 case ISD::SETUGE: CondCode = AE; break;
778 }
779 } else {
780 // On a floating point condition, the flags are set as follows:
781 // ZF PF CF op
782 // 0 | 0 | 0 | X > Y
783 // 0 | 0 | 1 | X < Y
784 // 1 | 0 | 0 | X == Y
785 // 1 | 1 | 1 | unordered
786 //
787 switch (SetCC->getCondition()) {
788 default: assert(0 && "Unknown FP comparison!");
789 case ISD::SETUEQ:
790 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
791 case ISD::SETOGT:
792 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
793 case ISD::SETOGE:
794 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
795 case ISD::SETULT:
796 case ISD::SETLT: CondCode = B; break; // True if CF = 1
797 case ISD::SETULE:
798 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
799 case ISD::SETONE:
800 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
801 case ISD::SETUO: CondCode = P; break; // True if PF = 1
802 case ISD::SETO: CondCode = NP; break; // True if PF = 0
803 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
804 case ISD::SETUGE: // PF = 1 | CF = 0
805 case ISD::SETUNE: // PF = 1 | ZF = 0
806 case ISD::SETOEQ: // PF = 0 & ZF = 1
807 case ISD::SETOLT: // PF = 0 & CF = 1
808 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
809 // We cannot emit this comparison as a single cmov.
810 break;
811 }
812 }
813 }
814
815 unsigned Opc = 0;
816 if (CondCode != NOT_SET) {
817 switch (SVT) {
818 default: assert(0 && "Cannot select this type!");
819 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
820 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
821 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000822 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000823 }
824 }
825
826 // Finally, if we weren't able to fold this, just emit the condition and test
827 // it.
828 if (CondCode == NOT_SET || Opc == 0) {
829 // Get the condition into the zero flag.
830 unsigned CondReg = SelectExpr(Cond);
831 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
832
833 switch (SVT) {
834 default: assert(0 && "Cannot select this type!");
835 case MVT::i16: Opc = X86::CMOVE16rr; break;
836 case MVT::i32: Opc = X86::CMOVE32rr; break;
837 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000838 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000839 }
840 } else {
841 // FIXME: CMP R, 0 -> TEST R, R
842 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000843 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000844 }
845 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
846}
847
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000848void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000849 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000850 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
851 Opc = 0;
852 switch (RHS.getValueType()) {
853 default: break;
854 case MVT::i1:
855 case MVT::i8: Opc = X86::CMP8ri; break;
856 case MVT::i16: Opc = X86::CMP16ri; break;
857 case MVT::i32: Opc = X86::CMP32ri; break;
858 }
859 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000860 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000861 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
862 return;
863 }
864 }
865
866 switch (LHS.getValueType()) {
867 default: assert(0 && "Cannot compare this value!");
868 case MVT::i1:
869 case MVT::i8: Opc = X86::CMP8rr; break;
870 case MVT::i16: Opc = X86::CMP16rr; break;
871 case MVT::i32: Opc = X86::CMP32rr; break;
872 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000873 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000874 }
Chris Lattner11333092005-01-11 03:11:44 +0000875 unsigned Tmp1, Tmp2;
876 if (getRegPressure(LHS) > getRegPressure(RHS)) {
877 Tmp1 = SelectExpr(LHS);
878 Tmp2 = SelectExpr(RHS);
879 } else {
880 Tmp2 = SelectExpr(RHS);
881 Tmp1 = SelectExpr(LHS);
882 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000883 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
884}
885
Chris Lattnera5ade062005-01-11 21:19:59 +0000886/// isFoldableLoad - Return true if this is a load instruction that can safely
887/// be folded into an operation that uses it.
888bool ISel::isFoldableLoad(SDOperand Op) {
889 if (Op.getOpcode() != ISD::LOAD ||
890 // FIXME: currently can't fold constant pool indexes.
891 isa<ConstantPoolSDNode>(Op.getOperand(1)))
892 return false;
893
894 // If this load has already been emitted, we clearly can't fold it.
895 if (ExprMap.count(Op)) return false;
896
897 return Op.Val->use_size() == 2;
898}
899
900/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
901/// and compute the address being loaded into AM.
902void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
903 SDOperand Chain = Op.getOperand(0);
904 SDOperand Address = Op.getOperand(1);
905 if (getRegPressure(Chain) > getRegPressure(Address)) {
906 Select(Chain);
907 SelectAddress(Address, AM);
908 } else {
909 SelectAddress(Address, AM);
910 Select(Chain);
911 }
912
913 // The chain for this load is now lowered.
914 LoweredTokens.insert(SDOperand(Op.Val, 1));
915 ExprMap[SDOperand(Op.Val, 1)] = 1;
916}
917
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000918unsigned ISel::SelectExpr(SDOperand N) {
919 unsigned Result;
920 unsigned Tmp1, Tmp2, Tmp3;
921 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +0000922 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +0000923 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +0000924
Chris Lattner590d8002005-01-09 18:52:44 +0000925 if (Node->getOpcode() == ISD::CopyFromReg)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000926 // Just use the specified register as our input.
Chris Lattner590d8002005-01-09 18:52:44 +0000927 return dyn_cast<CopyRegSDNode>(Node)->getReg();
Chris Lattnera5ade062005-01-11 21:19:59 +0000928
929 unsigned &Reg = ExprMap[N];
930 if (Reg) return Reg;
931
932 if (N.getOpcode() != ISD::CALL)
933 Reg = Result = (N.getValueType() != MVT::Other) ?
934 MakeReg(N.getValueType()) : 1;
935 else {
936 // If this is a call instruction, make sure to prepare ALL of the result
937 // values as well as the chain.
938 if (Node->getNumValues() == 1)
939 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000940 else {
Chris Lattnera5ade062005-01-11 21:19:59 +0000941 Result = MakeReg(Node->getValueType(0));
942 ExprMap[N.getValue(0)] = Result;
943 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
944 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
945 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000946 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000947 }
Chris Lattnera5ade062005-01-11 21:19:59 +0000948
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000949 switch (N.getOpcode()) {
950 default:
Chris Lattner5188ad72005-01-08 19:28:19 +0000951 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000952 assert(0 && "Node not handled!\n");
953 case ISD::FrameIndex:
954 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
955 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
956 return Result;
957 case ISD::ConstantPool:
958 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
959 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
960 return Result;
961 case ISD::ConstantFP:
962 ContainsFPCode = true;
963 Tmp1 = Result; // Intermediate Register
964 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
965 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
966 Tmp1 = MakeReg(MVT::f64);
967
968 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
969 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
970 BuildMI(BB, X86::FLD0, 0, Tmp1);
971 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
972 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
973 BuildMI(BB, X86::FLD1, 0, Tmp1);
974 else
975 assert(0 && "Unexpected constant!");
976 if (Tmp1 != Result)
977 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
978 return Result;
979 case ISD::Constant:
980 switch (N.getValueType()) {
981 default: assert(0 && "Cannot use constants of this type!");
982 case MVT::i1:
983 case MVT::i8: Opc = X86::MOV8ri; break;
984 case MVT::i16: Opc = X86::MOV16ri; break;
985 case MVT::i32: Opc = X86::MOV32ri; break;
986 }
987 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
988 return Result;
989 case ISD::GlobalAddress: {
990 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
991 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
992 return Result;
993 }
994 case ISD::ExternalSymbol: {
995 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
996 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
997 return Result;
998 }
999 case ISD::FP_EXTEND:
1000 Tmp1 = SelectExpr(N.getOperand(0));
1001 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001002 return Result;
1003 case ISD::ZERO_EXTEND: {
1004 int DestIs16 = N.getValueType() == MVT::i16;
1005 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001006 Tmp1 = SelectExpr(N.getOperand(0));
1007
1008 // FIXME: This hack is here for zero extension casts from bool to i8. This
1009 // would not be needed if bools were promoted by Legalize.
1010 if (N.getValueType() == MVT::i8) {
1011 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1012 return Result;
1013 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001014
1015 static const unsigned Opc[3] = {
1016 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1017 };
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001018 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1019 return Result;
1020 }
1021 case ISD::SIGN_EXTEND: {
1022 int DestIs16 = N.getValueType() == MVT::i16;
1023 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1024
Chris Lattner590d8002005-01-09 18:52:44 +00001025 // FIXME: Legalize should promote bools to i8!
1026 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1027 "Sign extend from bool not implemented!");
1028
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001029 static const unsigned Opc[3] = {
1030 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1031 };
1032 Tmp1 = SelectExpr(N.getOperand(0));
1033 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1034 return Result;
1035 }
1036 case ISD::TRUNCATE:
1037 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1038 // a move out of AX or AL.
1039 switch (N.getOperand(0).getValueType()) {
1040 default: assert(0 && "Unknown truncate!");
1041 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1042 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1043 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1044 }
1045 Tmp1 = SelectExpr(N.getOperand(0));
1046 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1047
1048 switch (N.getValueType()) {
1049 default: assert(0 && "Unknown truncate!");
1050 case MVT::i1:
1051 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1052 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1053 }
1054 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1055 return Result;
1056
1057 case ISD::FP_ROUND:
1058 // Truncate from double to float by storing to memory as float,
1059 // then reading it back into a register.
1060
1061 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001062 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001063 Tmp1 = TLI.getTargetData().getFloatAlignment();
1064 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1065
1066 // Codegen the input.
1067 Tmp1 = SelectExpr(N.getOperand(0));
1068
1069 // Emit the store, then the reload.
1070 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1071 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001072 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001073
1074 case ISD::SINT_TO_FP:
1075 case ISD::UINT_TO_FP: {
1076 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001077 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001078
1079 // Promote the integer to a type supported by FLD. We do this because there
1080 // are no unsigned FLD instructions, so we must promote an unsigned value to
1081 // a larger signed value, then use FLD on the larger value.
1082 //
1083 MVT::ValueType PromoteType = MVT::Other;
1084 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1085 unsigned PromoteOpcode = 0;
1086 unsigned RealDestReg = Result;
1087 switch (SrcTy) {
1088 case MVT::i1:
1089 case MVT::i8:
1090 // We don't have the facilities for directly loading byte sized data from
1091 // memory (even signed). Promote it to 16 bits.
1092 PromoteType = MVT::i16;
1093 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1094 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1095 break;
1096 case MVT::i16:
1097 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1098 PromoteType = MVT::i32;
1099 PromoteOpcode = X86::MOVZX32rr16;
1100 }
1101 break;
1102 default:
1103 // Don't fild into the real destination.
1104 if (Node->getOpcode() == ISD::UINT_TO_FP)
1105 Result = MakeReg(Node->getValueType(0));
1106 break;
1107 }
1108
1109 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1110
1111 if (PromoteType != MVT::Other) {
1112 Tmp2 = MakeReg(PromoteType);
1113 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1114 SrcTy = PromoteType;
1115 Tmp1 = Tmp2;
1116 }
1117
1118 // Spill the integer to memory and reload it from there.
1119 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1120 MachineFunction *F = BB->getParent();
1121 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1122
1123 switch (SrcTy) {
1124 case MVT::i64:
1125 // FIXME: this won't work for cast [u]long to FP
1126 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1127 FrameIdx).addReg(Tmp1);
1128 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1129 FrameIdx, 4).addReg(Tmp1+1);
1130 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1131 break;
1132 case MVT::i32:
1133 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1134 FrameIdx).addReg(Tmp1);
1135 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1136 break;
1137 case MVT::i16:
1138 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1139 FrameIdx).addReg(Tmp1);
1140 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1141 break;
1142 default: break; // No promotion required.
1143 }
1144
1145 if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i32) {
1146 // If this is a cast from uint -> double, we need to be careful when if
1147 // the "sign" bit is set. If so, we don't want to make a negative number,
1148 // we want to make a positive number. Emit code to add an offset if the
1149 // sign bit is set.
1150
1151 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1152 unsigned IsNeg = MakeReg(MVT::i32);
1153 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1154
1155 // Create a CP value that has the offset in one word and 0 in the other.
1156 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1157 0x4f80000000000000ULL);
1158 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1159 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1160 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1161
1162 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1163 // We need special handling for unsigned 64-bit integer sources. If the
1164 // input number has the "sign bit" set, then we loaded it incorrectly as a
1165 // negative 64-bit number. In this case, add an offset value.
1166
1167 // Emit a test instruction to see if the dynamic input value was signed.
1168 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1169
1170 // If the sign bit is set, get a pointer to an offset, otherwise get a
1171 // pointer to a zero.
1172 MachineConstantPool *CP = F->getConstantPool();
1173 unsigned Zero = MakeReg(MVT::i32);
1174 Constant *Null = Constant::getNullValue(Type::UIntTy);
1175 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1176 CP->getConstantPoolIndex(Null));
1177 unsigned Offset = MakeReg(MVT::i32);
1178 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1179
1180 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1181 CP->getConstantPoolIndex(OffsetCst));
1182 unsigned Addr = MakeReg(MVT::i32);
1183 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1184
1185 // Load the constant for an add. FIXME: this could make an 'fadd' that
1186 // reads directly from memory, but we don't support these yet.
1187 unsigned ConstReg = MakeReg(MVT::f64);
1188 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1189
1190 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1191 }
1192 return RealDestReg;
1193 }
1194 case ISD::FP_TO_SINT:
1195 case ISD::FP_TO_UINT: {
1196 // FIXME: Most of this grunt work should be done by legalize!
1197 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1198
1199 // Change the floating point control register to use "round towards zero"
1200 // mode when truncating to an integer value.
1201 //
1202 MachineFunction *F = BB->getParent();
1203 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1204 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1205
1206 // Load the old value of the high byte of the control word...
1207 unsigned HighPartOfCW = MakeReg(MVT::i8);
1208 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1209 CWFrameIdx, 1);
1210
1211 // Set the high part to be round to zero...
1212 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1213 CWFrameIdx, 1).addImm(12);
1214
1215 // Reload the modified control word now...
1216 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1217
1218 // Restore the memory image of control word to original value
1219 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1220 CWFrameIdx, 1).addReg(HighPartOfCW);
1221
1222 // We don't have the facilities for directly storing byte sized data to
1223 // memory. Promote it to 16 bits. We also must promote unsigned values to
1224 // larger classes because we only have signed FP stores.
1225 MVT::ValueType StoreClass = Node->getValueType(0);
1226 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1227 switch (StoreClass) {
1228 case MVT::i8: StoreClass = MVT::i16; break;
1229 case MVT::i16: StoreClass = MVT::i32; break;
1230 case MVT::i32: StoreClass = MVT::i64; break;
1231 // The following treatment of cLong may not be perfectly right,
1232 // but it survives chains of casts of the form
1233 // double->ulong->double.
1234 case MVT::i64: StoreClass = MVT::i64; break;
1235 default: assert(0 && "Unknown store class!");
1236 }
1237
1238 // Spill the integer to memory and reload it from there.
1239 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1240 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1241
1242 switch (StoreClass) {
1243 default: assert(0 && "Unknown store class!");
1244 case MVT::i16:
1245 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1246 break;
1247 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001248 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001249 break;
1250 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001251 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001252 break;
1253 }
1254
1255 switch (Node->getValueType(0)) {
1256 default:
1257 assert(0 && "Unknown integer type!");
1258 case MVT::i64:
1259 // FIXME: this isn't gunna work.
1260 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1261 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1262 case MVT::i32:
1263 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1264 break;
1265 case MVT::i16:
1266 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1267 break;
1268 case MVT::i8:
1269 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1270 break;
1271 }
1272
1273 // Reload the original control word now.
1274 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1275 return Result;
1276 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001277 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001278 Op0 = N.getOperand(0);
1279 Op1 = N.getOperand(1);
1280
1281 if (isFoldableLoad(Op0))
1282 std::swap(Op0, Op1);
1283
1284 if (isFoldableLoad(Op1)) {
1285 switch (N.getValueType()) {
1286 default: assert(0 && "Cannot add this type!");
1287 case MVT::i1:
1288 case MVT::i8: Opc = X86::ADD8rm; break;
1289 case MVT::i16: Opc = X86::ADD16rm; break;
1290 case MVT::i32: Opc = X86::ADD32rm; break;
1291 case MVT::f32: Opc = X86::FADD32m; break;
1292 case MVT::f64: Opc = X86::FADD64m; break;
1293 }
1294 X86AddressMode AM;
1295 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1296 Tmp1 = SelectExpr(Op0);
1297 EmitFoldedLoad(Op1, AM);
1298 } else {
1299 EmitFoldedLoad(Op1, AM);
1300 Tmp1 = SelectExpr(Op0);
1301 }
1302 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1303 return Result;
1304 }
1305
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001306 // See if we can codegen this as an LEA to fold operations together.
1307 if (N.getValueType() == MVT::i32) {
1308 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001309 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001310 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001311 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001312 // leave this as LEA, then peephole it to 'ADD' after two address elim
1313 // happens.
1314 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001315 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001316 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1317 return Result;
1318 }
1319 }
1320 }
Chris Lattner11333092005-01-11 03:11:44 +00001321
Chris Lattnera5ade062005-01-11 21:19:59 +00001322 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001323 Opc = 0;
1324 if (CN->getValue() == 1) { // add X, 1 -> inc X
1325 switch (N.getValueType()) {
1326 default: assert(0 && "Cannot integer add this type!");
1327 case MVT::i8: Opc = X86::INC8r; break;
1328 case MVT::i16: Opc = X86::INC16r; break;
1329 case MVT::i32: Opc = X86::INC32r; break;
1330 }
1331 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1332 switch (N.getValueType()) {
1333 default: assert(0 && "Cannot integer add this type!");
1334 case MVT::i8: Opc = X86::DEC8r; break;
1335 case MVT::i16: Opc = X86::DEC16r; break;
1336 case MVT::i32: Opc = X86::DEC32r; break;
1337 }
1338 }
1339
1340 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001341 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001342 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1343 return Result;
1344 }
1345
1346 switch (N.getValueType()) {
1347 default: assert(0 && "Cannot add this type!");
1348 case MVT::i8: Opc = X86::ADD8ri; break;
1349 case MVT::i16: Opc = X86::ADD16ri; break;
1350 case MVT::i32: Opc = X86::ADD32ri; break;
1351 }
1352 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001353 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001354 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1355 return Result;
1356 }
1357 }
1358
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001359 switch (N.getValueType()) {
1360 default: assert(0 && "Cannot add this type!");
1361 case MVT::i8: Opc = X86::ADD8rr; break;
1362 case MVT::i16: Opc = X86::ADD16rr; break;
1363 case MVT::i32: Opc = X86::ADD32rr; break;
1364 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001365 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001366 }
Chris Lattner11333092005-01-11 03:11:44 +00001367
Chris Lattnera5ade062005-01-11 21:19:59 +00001368 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1369 Tmp1 = SelectExpr(Op0);
1370 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001371 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001372 Tmp2 = SelectExpr(Op1);
1373 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001374 }
1375
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001376 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1377 return Result;
1378 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001379 case ISD::MUL:
1380 case ISD::AND:
1381 case ISD::OR:
1382 case ISD::XOR:
1383 static const unsigned SUBTab[] = {
1384 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1385 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1386 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1387 };
1388 static const unsigned MULTab[] = {
1389 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1390 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1391 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1392 };
1393 static const unsigned ANDTab[] = {
1394 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1395 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1396 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1397 };
1398 static const unsigned ORTab[] = {
1399 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1400 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1401 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1402 };
1403 static const unsigned XORTab[] = {
1404 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1405 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1406 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1407 };
1408
1409 Op0 = Node->getOperand(0);
1410 Op1 = Node->getOperand(1);
1411
1412 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001413 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1414 if (CN->isNullValue()) { // 0 - N -> neg N
1415 switch (N.getValueType()) {
1416 default: assert(0 && "Cannot sub this type!");
1417 case MVT::i1:
1418 case MVT::i8: Opc = X86::NEG8r; break;
1419 case MVT::i16: Opc = X86::NEG16r; break;
1420 case MVT::i32: Opc = X86::NEG32r; break;
1421 }
1422 Tmp1 = SelectExpr(N.getOperand(1));
1423 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1424 return Result;
1425 }
1426
Chris Lattnera5ade062005-01-11 21:19:59 +00001427 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1428 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001429 switch (N.getValueType()) {
1430 default: assert(0 && "Cannot add this type!");
1431 case MVT::i1:
1432 case MVT::i8: Opc = X86::NOT8r; break;
1433 case MVT::i16: Opc = X86::NOT16r; break;
1434 case MVT::i32: Opc = X86::NOT32r; break;
1435 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001436 Tmp1 = SelectExpr(Op0);
Chris Lattnerd4dab922005-01-11 04:31:30 +00001437 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1438 return Result;
1439 }
1440
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001441 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001442 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001443 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001444 case MVT::i8: Opc = 0; break;
1445 case MVT::i16: Opc = 1; break;
1446 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001447 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001448 switch (Node->getOpcode()) {
1449 default: assert(0 && "Unreachable!");
1450 case ISD::SUB: Opc = SUBTab[Opc]; break;
1451 case ISD::MUL: Opc = MULTab[Opc]; break;
1452 case ISD::AND: Opc = ANDTab[Opc]; break;
1453 case ISD::OR: Opc = ORTab[Opc]; break;
1454 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001455 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001456 if (Opc) { // Can't fold MUL:i8 R, imm
1457 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001458 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1459 return Result;
1460 }
1461 }
Chris Lattner11333092005-01-11 03:11:44 +00001462
Chris Lattnera5ade062005-01-11 21:19:59 +00001463 if (isFoldableLoad(Op0))
1464 if (Node->getOpcode() != ISD::SUB) {
1465 std::swap(Op0, Op1);
1466 } else {
1467 // Emit 'reverse' subract, with a memory operand.
1468 switch (N.getValueType()) {
1469 default: Opc = 0; break;
1470 case MVT::f32: Opc = X86::FSUBR32m; break;
1471 case MVT::f64: Opc = X86::FSUBR64m; break;
1472 }
1473 if (Opc) {
1474 X86AddressMode AM;
1475 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1476 EmitFoldedLoad(Op0, AM);
1477 Tmp1 = SelectExpr(Op1);
1478 } else {
1479 Tmp1 = SelectExpr(Op1);
1480 EmitFoldedLoad(Op0, AM);
1481 }
1482 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1483 return Result;
1484 }
1485 }
1486
1487 if (isFoldableLoad(Op1)) {
1488 switch (N.getValueType()) {
1489 default: assert(0 && "Cannot operate on this type!");
1490 case MVT::i1:
1491 case MVT::i8: Opc = 5; break;
1492 case MVT::i16: Opc = 6; break;
1493 case MVT::i32: Opc = 7; break;
1494 case MVT::f32: Opc = 8; break;
1495 case MVT::f64: Opc = 9; break;
1496 }
1497 switch (Node->getOpcode()) {
1498 default: assert(0 && "Unreachable!");
1499 case ISD::SUB: Opc = SUBTab[Opc]; break;
1500 case ISD::MUL: Opc = MULTab[Opc]; break;
1501 case ISD::AND: Opc = ANDTab[Opc]; break;
1502 case ISD::OR: Opc = ORTab[Opc]; break;
1503 case ISD::XOR: Opc = XORTab[Opc]; break;
1504 }
1505
1506 X86AddressMode AM;
1507 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1508 Tmp1 = SelectExpr(Op0);
1509 EmitFoldedLoad(Op1, AM);
1510 } else {
1511 EmitFoldedLoad(Op1, AM);
1512 Tmp1 = SelectExpr(Op0);
1513 }
1514 if (Opc) {
1515 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1516 } else {
1517 assert(Node->getOpcode() == ISD::MUL &&
1518 N.getValueType() == MVT::i8 && "Unexpected situation!");
1519 // Must use the MUL instruction, which forces use of AL.
1520 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1521 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1522 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1523 }
1524 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001525 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001526
1527 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1528 Tmp1 = SelectExpr(Op0);
1529 Tmp2 = SelectExpr(Op1);
1530 } else {
1531 Tmp2 = SelectExpr(Op1);
1532 Tmp1 = SelectExpr(Op0);
1533 }
1534
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001535 switch (N.getValueType()) {
1536 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001537 case MVT::i1:
1538 case MVT::i8: Opc = 10; break;
1539 case MVT::i16: Opc = 11; break;
1540 case MVT::i32: Opc = 12; break;
1541 case MVT::f32: Opc = 13; break;
1542 case MVT::f64: Opc = 14; break;
1543 }
1544 switch (Node->getOpcode()) {
1545 default: assert(0 && "Unreachable!");
1546 case ISD::SUB: Opc = SUBTab[Opc]; break;
1547 case ISD::MUL: Opc = MULTab[Opc]; break;
1548 case ISD::AND: Opc = ANDTab[Opc]; break;
1549 case ISD::OR: Opc = ORTab[Opc]; break;
1550 case ISD::XOR: Opc = XORTab[Opc]; break;
1551 }
1552 if (Opc) {
1553 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1554 } else {
1555 assert(Node->getOpcode() == ISD::MUL &&
1556 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001557 // Must use the MUL instruction, which forces use of AL.
1558 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1559 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1560 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001561 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001562 return Result;
1563
1564 case ISD::SELECT:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001565 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001566 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1567 Tmp2 = SelectExpr(N.getOperand(1));
1568 Tmp3 = SelectExpr(N.getOperand(2));
1569 } else {
1570 Tmp3 = SelectExpr(N.getOperand(2));
1571 Tmp2 = SelectExpr(N.getOperand(1));
1572 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001573 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001574 return Result;
1575 } else {
1576 // FIXME: This should not be implemented here, it should be in the generic
1577 // code!
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001578 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1579 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1580 N.getOperand(1)));
1581 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1582 N.getOperand(2)));
1583 } else {
1584 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1585 N.getOperand(2)));
1586 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1587 N.getOperand(1)));
1588 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001589 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001590 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1591 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001592 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1593 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1594 return Result;
1595 }
1596
1597 case ISD::SDIV:
1598 case ISD::UDIV:
1599 case ISD::SREM:
1600 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001601 if (N.getOpcode() == ISD::SDIV)
1602 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1603 // FIXME: These special cases should be handled by the lowering impl!
1604 unsigned RHS = CN->getValue();
1605 bool isNeg = false;
1606 if ((int)RHS < 0) {
1607 isNeg = true;
1608 RHS = -RHS;
1609 }
1610 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1611 unsigned Log = log2(RHS);
1612 unsigned TmpReg = MakeReg(N.getValueType());
1613 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1614 switch (N.getValueType()) {
1615 default: assert("Unknown type to signed divide!");
1616 case MVT::i8:
1617 SAROpc = X86::SAR8ri;
1618 SHROpc = X86::SHR8ri;
1619 ADDOpc = X86::ADD8rr;
1620 NEGOpc = X86::NEG8r;
1621 break;
1622 case MVT::i16:
1623 SAROpc = X86::SAR16ri;
1624 SHROpc = X86::SHR16ri;
1625 ADDOpc = X86::ADD16rr;
1626 NEGOpc = X86::NEG16r;
1627 break;
1628 case MVT::i32:
1629 SAROpc = X86::SAR32ri;
1630 SHROpc = X86::SHR32ri;
1631 ADDOpc = X86::ADD32rr;
1632 NEGOpc = X86::NEG32r;
1633 break;
1634 }
Chris Lattner11333092005-01-11 03:11:44 +00001635 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001636 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1637 unsigned TmpReg2 = MakeReg(N.getValueType());
1638 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1639 unsigned TmpReg3 = MakeReg(N.getValueType());
1640 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1641
1642 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1643 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1644 if (isNeg)
1645 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1646 return Result;
1647 }
1648 }
1649
Chris Lattner11333092005-01-11 03:11:44 +00001650 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1651 Tmp1 = SelectExpr(N.getOperand(0));
1652 Tmp2 = SelectExpr(N.getOperand(1));
1653 } else {
1654 Tmp2 = SelectExpr(N.getOperand(1));
1655 Tmp1 = SelectExpr(N.getOperand(0));
1656 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001657
1658 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1659 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1660 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1661 switch (N.getValueType()) {
1662 default: assert(0 && "Cannot sdiv this type!");
1663 case MVT::i8:
1664 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1665 LoReg = X86::AL;
1666 HiReg = X86::AH;
1667 MovOpcode = X86::MOV8rr;
1668 ClrOpcode = X86::MOV8ri;
1669 SExtOpcode = X86::CBW;
1670 break;
1671 case MVT::i16:
1672 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1673 LoReg = X86::AX;
1674 HiReg = X86::DX;
1675 MovOpcode = X86::MOV16rr;
1676 ClrOpcode = X86::MOV16ri;
1677 SExtOpcode = X86::CWD;
1678 break;
1679 case MVT::i32:
1680 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
1681 LoReg =X86::EAX;
1682 HiReg = X86::EDX;
1683 MovOpcode = X86::MOV32rr;
1684 ClrOpcode = X86::MOV32ri;
1685 SExtOpcode = X86::CDQ;
1686 break;
1687 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1688 case MVT::f32:
1689 case MVT::f64:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001690 if (N.getOpcode() == ISD::SDIV)
1691 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1692 else
1693 assert(0 && "FIXME: Emit frem libcall to fmod!");
1694 return Result;
1695 }
1696
1697 // Set up the low part.
1698 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1699
1700 if (isSigned) {
1701 // Sign extend the low part into the high part.
1702 BuildMI(BB, SExtOpcode, 0);
1703 } else {
1704 // Zero out the high part, effectively zero extending the input.
1705 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1706 }
1707
1708 // Emit the DIV/IDIV instruction.
1709 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1710
1711 // Get the result of the divide or rem.
1712 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1713 return Result;
1714 }
1715
1716 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001717 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001718 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1719 switch (N.getValueType()) {
1720 default: assert(0 && "Cannot shift this type!");
1721 case MVT::i8: Opc = X86::ADD8rr; break;
1722 case MVT::i16: Opc = X86::ADD16rr; break;
1723 case MVT::i32: Opc = X86::ADD32rr; break;
1724 }
1725 Tmp1 = SelectExpr(N.getOperand(0));
1726 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1727 return Result;
1728 }
1729
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001730 switch (N.getValueType()) {
1731 default: assert(0 && "Cannot shift this type!");
1732 case MVT::i8: Opc = X86::SHL8ri; break;
1733 case MVT::i16: Opc = X86::SHL16ri; break;
1734 case MVT::i32: Opc = X86::SHL32ri; break;
1735 }
Chris Lattner11333092005-01-11 03:11:44 +00001736 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001737 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1738 return Result;
1739 }
Chris Lattner11333092005-01-11 03:11:44 +00001740
1741 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1742 Tmp1 = SelectExpr(N.getOperand(0));
1743 Tmp2 = SelectExpr(N.getOperand(1));
1744 } else {
1745 Tmp2 = SelectExpr(N.getOperand(1));
1746 Tmp1 = SelectExpr(N.getOperand(0));
1747 }
1748
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001749 switch (N.getValueType()) {
1750 default: assert(0 && "Cannot shift this type!");
1751 case MVT::i8 : Opc = X86::SHL8rCL; break;
1752 case MVT::i16: Opc = X86::SHL16rCL; break;
1753 case MVT::i32: Opc = X86::SHL32rCL; break;
1754 }
1755 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1756 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1757 return Result;
1758 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001759 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1760 switch (N.getValueType()) {
1761 default: assert(0 && "Cannot shift this type!");
1762 case MVT::i8: Opc = X86::SHR8ri; break;
1763 case MVT::i16: Opc = X86::SHR16ri; break;
1764 case MVT::i32: Opc = X86::SHR32ri; break;
1765 }
Chris Lattner11333092005-01-11 03:11:44 +00001766 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001767 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1768 return Result;
1769 }
Chris Lattner11333092005-01-11 03:11:44 +00001770
1771 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1772 Tmp1 = SelectExpr(N.getOperand(0));
1773 Tmp2 = SelectExpr(N.getOperand(1));
1774 } else {
1775 Tmp2 = SelectExpr(N.getOperand(1));
1776 Tmp1 = SelectExpr(N.getOperand(0));
1777 }
1778
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001779 switch (N.getValueType()) {
1780 default: assert(0 && "Cannot shift this type!");
1781 case MVT::i8 : Opc = X86::SHR8rCL; break;
1782 case MVT::i16: Opc = X86::SHR16rCL; break;
1783 case MVT::i32: Opc = X86::SHR32rCL; break;
1784 }
1785 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1786 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1787 return Result;
1788 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001789 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1790 switch (N.getValueType()) {
1791 default: assert(0 && "Cannot shift this type!");
1792 case MVT::i8: Opc = X86::SAR8ri; break;
1793 case MVT::i16: Opc = X86::SAR16ri; break;
1794 case MVT::i32: Opc = X86::SAR32ri; break;
1795 }
Chris Lattner11333092005-01-11 03:11:44 +00001796 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001797 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1798 return Result;
1799 }
Chris Lattner11333092005-01-11 03:11:44 +00001800
1801 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1802 Tmp1 = SelectExpr(N.getOperand(0));
1803 Tmp2 = SelectExpr(N.getOperand(1));
1804 } else {
1805 Tmp2 = SelectExpr(N.getOperand(1));
1806 Tmp1 = SelectExpr(N.getOperand(0));
1807 }
1808
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001809 switch (N.getValueType()) {
1810 default: assert(0 && "Cannot shift this type!");
1811 case MVT::i8 : Opc = X86::SAR8rCL; break;
1812 case MVT::i16: Opc = X86::SAR16rCL; break;
1813 case MVT::i32: Opc = X86::SAR32rCL; break;
1814 }
1815 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1816 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1817 return Result;
1818
1819 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001820 EmitCMP(N.getOperand(0), N.getOperand(1));
1821 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1822 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1823 return Result;
1824 case ISD::LOAD: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001825 // Make sure we generate both values.
1826 if (Result != 1)
1827 ExprMap[N.getValue(1)] = 1; // Generate the token
1828 else
1829 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1830
Chris Lattner5188ad72005-01-08 19:28:19 +00001831 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001832 default: assert(0 && "Cannot load this type!");
1833 case MVT::i1:
1834 case MVT::i8: Opc = X86::MOV8rm; break;
1835 case MVT::i16: Opc = X86::MOV16rm; break;
1836 case MVT::i32: Opc = X86::MOV32rm; break;
1837 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1838 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1839 }
Chris Lattner11333092005-01-11 03:11:44 +00001840
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001841 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001842 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001843 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1844 } else {
1845 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001846 EmitFoldedLoad(N, AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001847 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1848 }
1849 return Result;
1850 }
1851 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001852 // Generate both result values.
1853 if (Result != 1)
1854 ExprMap[N.getValue(1)] = 1; // Generate the token
1855 else
1856 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1857
1858 // FIXME: We are currently ignoring the requested alignment for handling
1859 // greater than the stack alignment. This will need to be revisited at some
1860 // point. Align = N.getOperand(2);
1861
1862 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1863 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1864 std::cerr << "Cannot allocate stack object with greater alignment than"
1865 << " the stack alignment yet!";
1866 abort();
1867 }
1868
1869 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001870 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001871 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1872 .addImm(CN->getValue());
1873 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001874 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1875 Select(N.getOperand(0));
1876 Tmp1 = SelectExpr(N.getOperand(1));
1877 } else {
1878 Tmp1 = SelectExpr(N.getOperand(1));
1879 Select(N.getOperand(0));
1880 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001881
1882 // Subtract size from stack pointer, thereby allocating some space.
1883 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
1884 }
1885
1886 // Put a pointer to the space into the result register, by copying the stack
1887 // pointer.
1888 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
1889 return Result;
1890
1891 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00001892 // The chain for this call is now lowered.
1893 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
1894
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001895 if (GlobalAddressSDNode *GASD =
1896 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001897 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001898 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
1899 } else if (ExternalSymbolSDNode *ESSDN =
1900 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001901 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001902 BuildMI(BB, X86::CALLpcrel32,
1903 1).addExternalSymbol(ESSDN->getSymbol(), true);
1904 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001905 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1906 Select(N.getOperand(0));
1907 Tmp1 = SelectExpr(N.getOperand(1));
1908 } else {
1909 Tmp1 = SelectExpr(N.getOperand(1));
1910 Select(N.getOperand(0));
1911 }
1912
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001913 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
1914 }
Chris Lattner5188ad72005-01-08 19:28:19 +00001915 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001916 default: assert(0 && "Unknown value type for call result!");
1917 case MVT::Other: return 1;
1918 case MVT::i1:
1919 case MVT::i8:
1920 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1921 break;
1922 case MVT::i16:
1923 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
1924 break;
1925 case MVT::i32:
1926 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00001927 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001928 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
1929 break;
1930 case MVT::f32:
1931 case MVT::f64: // Floating-point return values live in %ST(0)
1932 ContainsFPCode = true;
1933 BuildMI(BB, X86::FpGETRESULT, 1, Result);
1934 break;
1935 }
1936 return Result+N.ResNo;
1937 }
1938
1939 return 0;
1940}
1941
1942void ISel::Select(SDOperand N) {
1943 unsigned Tmp1, Tmp2, Opc;
1944
1945 // FIXME: Disable for our current expansion model!
1946 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
1947 return; // Already selected.
1948
Chris Lattner989de032005-01-11 06:14:36 +00001949 SDNode *Node = N.Val;
1950
1951 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001952 default:
Chris Lattner989de032005-01-11 06:14:36 +00001953 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001954 assert(0 && "Node not handled yet!");
1955 case ISD::EntryToken: return; // Noop
1956 case ISD::CopyToReg:
1957 Select(N.getOperand(0));
1958 Tmp1 = SelectExpr(N.getOperand(1));
1959 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
1960
1961 if (Tmp1 != Tmp2) {
1962 switch (N.getOperand(1).getValueType()) {
1963 default: assert(0 && "Invalid type for operation!");
1964 case MVT::i1:
1965 case MVT::i8: Opc = X86::MOV8rr; break;
1966 case MVT::i16: Opc = X86::MOV16rr; break;
1967 case MVT::i32: Opc = X86::MOV32rr; break;
1968 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001969 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001970 }
1971 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1972 }
1973 return;
1974 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001975 switch (N.getNumOperands()) {
1976 default:
1977 assert(0 && "Unknown return instruction!");
1978 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001979 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1980 N.getOperand(2).getValueType() == MVT::i32 &&
1981 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00001982 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1983 Tmp1 = SelectExpr(N.getOperand(1));
1984 Tmp2 = SelectExpr(N.getOperand(2));
1985 } else {
1986 Tmp2 = SelectExpr(N.getOperand(2));
1987 Tmp1 = SelectExpr(N.getOperand(1));
1988 }
1989 Select(N.getOperand(0));
1990
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001991 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1992 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
1993 // Declare that EAX & EDX are live on exit.
1994 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1995 .addReg(X86::ESP);
1996 break;
1997 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00001998 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1999 Select(N.getOperand(0));
2000 Tmp1 = SelectExpr(N.getOperand(1));
2001 } else {
2002 Tmp1 = SelectExpr(N.getOperand(1));
2003 Select(N.getOperand(0));
2004 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002005 switch (N.getOperand(1).getValueType()) {
2006 default: assert(0 && "All other types should have been promoted!!");
2007 case MVT::f64:
2008 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2009 // Declare that top-of-stack is live on exit
2010 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2011 break;
2012 case MVT::i32:
2013 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2014 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2015 break;
2016 }
2017 break;
2018 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002019 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002020 break;
2021 }
2022 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2023 return;
2024 case ISD::BR: {
2025 Select(N.getOperand(0));
2026 MachineBasicBlock *Dest =
2027 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2028 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2029 return;
2030 }
2031
2032 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002033 MachineBasicBlock *Dest =
2034 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002035
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002036 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2037 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002038 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2039 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2040 Select(N.getOperand(0));
2041 Tmp1 = SelectExpr(N.getOperand(1));
2042 } else {
2043 Tmp1 = SelectExpr(N.getOperand(1));
2044 Select(N.getOperand(0));
2045 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002046 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2047 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2048 }
Chris Lattner11333092005-01-11 03:11:44 +00002049
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002050 return;
2051 }
2052 case ISD::LOAD:
2053 case ISD::CALL:
2054 case ISD::DYNAMIC_STACKALLOC:
2055 SelectExpr(N);
2056 return;
2057 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002058 // Select the address.
2059 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002060
2061 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2062 Opc = 0;
2063 switch (CN->getValueType(0)) {
2064 default: assert(0 && "Invalid type for operation!");
2065 case MVT::i1:
2066 case MVT::i8: Opc = X86::MOV8mi; break;
2067 case MVT::i16: Opc = X86::MOV16mi; break;
2068 case MVT::i32: Opc = X86::MOV32mi; break;
2069 case MVT::f32:
2070 case MVT::f64: break;
2071 }
2072 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002073 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2074 Select(N.getOperand(0));
2075 SelectAddress(N.getOperand(2), AM);
2076 } else {
2077 SelectAddress(N.getOperand(2), AM);
2078 Select(N.getOperand(0));
2079 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002080 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2081 return;
2082 }
2083 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002084 switch (N.getOperand(1).getValueType()) {
2085 default: assert(0 && "Cannot store this type!");
2086 case MVT::i1:
2087 case MVT::i8: Opc = X86::MOV8mr; break;
2088 case MVT::i16: Opc = X86::MOV16mr; break;
2089 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002090 case MVT::f32: Opc = X86::FST32m; break;
2091 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002092 }
Chris Lattner11333092005-01-11 03:11:44 +00002093
2094 std::vector<std::pair<unsigned, unsigned> > RP;
2095 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2096 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2097 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2098 std::sort(RP.begin(), RP.end());
2099
2100 for (unsigned i = 0; i != 3; ++i)
2101 switch (RP[2-i].second) {
2102 default: assert(0 && "Unknown operand number!");
2103 case 0: Select(N.getOperand(0)); break;
2104 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002105 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002106 }
2107
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002108 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2109 return;
2110 }
2111 case ISD::ADJCALLSTACKDOWN:
2112 case ISD::ADJCALLSTACKUP:
2113 Select(N.getOperand(0));
2114 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2115
2116 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2117 X86::ADJCALLSTACKUP;
2118 BuildMI(BB, Opc, 1).addImm(Tmp1);
2119 return;
Chris Lattner989de032005-01-11 06:14:36 +00002120 case ISD::MEMSET: {
2121 Select(N.getOperand(0)); // Select the chain.
2122 unsigned Align =
2123 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2124 if (Align == 0) Align = 1;
2125
2126 // Turn the byte code into # iterations
2127 unsigned CountReg;
2128 unsigned Opcode;
2129 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2130 unsigned Val = ValC->getValue() & 255;
2131
2132 // If the value is a constant, then we can potentially use larger sets.
2133 switch (Align & 3) {
2134 case 2: // WORD aligned
2135 CountReg = MakeReg(MVT::i32);
2136 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2137 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2138 } else {
2139 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2140 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2141 }
2142 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2143 Opcode = X86::REP_STOSW;
2144 break;
2145 case 0: // DWORD aligned
2146 CountReg = MakeReg(MVT::i32);
2147 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2148 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2149 } else {
2150 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2151 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2152 }
2153 Val = (Val << 8) | Val;
2154 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2155 Opcode = X86::REP_STOSD;
2156 break;
2157 default: // BYTE aligned
2158 CountReg = SelectExpr(Node->getOperand(3));
2159 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2160 Opcode = X86::REP_STOSB;
2161 break;
2162 }
2163 } else {
2164 // If it's not a constant value we are storing, just fall back. We could
2165 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2166 unsigned ValReg = SelectExpr(Node->getOperand(2));
2167 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2168 CountReg = SelectExpr(Node->getOperand(3));
2169 Opcode = X86::REP_STOSB;
2170 }
2171
2172 // No matter what the alignment is, we put the source in ESI, the
2173 // destination in EDI, and the count in ECX.
2174 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2175 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2176 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2177 BuildMI(BB, Opcode, 0);
2178 return;
2179 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002180 case ISD::MEMCPY:
2181 Select(N.getOperand(0)); // Select the chain.
2182 unsigned Align =
2183 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2184 if (Align == 0) Align = 1;
2185
2186 // Turn the byte code into # iterations
2187 unsigned CountReg;
2188 unsigned Opcode;
2189 switch (Align & 3) {
2190 case 2: // WORD aligned
2191 CountReg = MakeReg(MVT::i32);
2192 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2193 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2194 } else {
2195 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2196 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2197 }
2198 Opcode = X86::REP_MOVSW;
2199 break;
2200 case 0: // DWORD aligned
2201 CountReg = MakeReg(MVT::i32);
2202 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2203 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2204 } else {
2205 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2206 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2207 }
2208 Opcode = X86::REP_MOVSD;
2209 break;
2210 default: // BYTE aligned
2211 CountReg = SelectExpr(Node->getOperand(3));
2212 Opcode = X86::REP_MOVSB;
2213 break;
2214 }
2215
2216 // No matter what the alignment is, we put the source in ESI, the
2217 // destination in EDI, and the count in ECX.
2218 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2219 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2220 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2221 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2222 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2223 BuildMI(BB, Opcode, 0);
2224 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002225 }
2226 assert(0 && "Should not be reached!");
2227}
2228
2229
2230/// createX86PatternInstructionSelector - This pass converts an LLVM function
2231/// into a machine code representation using pattern matching and a machine
2232/// description file.
2233///
2234FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2235 return new ISel(TM);
2236}