blob: fe56208f00941663fcd88313549eda96c2f9dade [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
53 setOperationUnsupported(ISD::MEMSET, MVT::Other);
54 setOperationUnsupported(ISD::MEMCPY, MVT::Other);
55 setOperationUnsupported(ISD::MEMMOVE, MVT::Other);
56
Chris Lattner8acb1ba2005-01-07 07:49:41 +000057 setOperationUnsupported(ISD::MUL, MVT::i8);
58 setOperationUnsupported(ISD::SELECT, MVT::i1);
59 setOperationUnsupported(ISD::SELECT, MVT::i8);
60
61 addLegalFPImmediate(+0.0); // FLD0
62 addLegalFPImmediate(+1.0); // FLD1
63 addLegalFPImmediate(-0.0); // FLD0/FCHS
64 addLegalFPImmediate(-1.0); // FLD1/FCHS
65 }
66
67 /// LowerArguments - This hook must be implemented to indicate how we should
68 /// lower the arguments for the specified function, into the specified DAG.
69 virtual std::vector<SDOperand>
70 LowerArguments(Function &F, SelectionDAG &DAG);
71
72 /// LowerCallTo - This hook lowers an abstract call to a function into an
73 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000074 virtual std::pair<SDOperand, SDOperand>
75 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
76 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000077
78 virtual std::pair<SDOperand, SDOperand>
79 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
80
81 virtual std::pair<SDOperand,SDOperand>
82 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
83 const Type *ArgTy, SelectionDAG &DAG);
84
85 virtual std::pair<SDOperand, SDOperand>
86 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
87 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000088 };
89}
90
91
92std::vector<SDOperand>
93X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
94 std::vector<SDOperand> ArgValues;
95
96 // Add DAG nodes to load the arguments... On entry to a function on the X86,
97 // the stack frame looks like this:
98 //
99 // [ESP] -- return address
100 // [ESP + 4] -- first argument (leftmost lexically)
101 // [ESP + 8] -- second argument, if first argument is four bytes in size
102 // ...
103 //
104 MachineFunction &MF = DAG.getMachineFunction();
105 MachineFrameInfo *MFI = MF.getFrameInfo();
106
107 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
108 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
109 MVT::ValueType ObjectVT = getValueType(I->getType());
110 unsigned ArgIncrement = 4;
111 unsigned ObjSize;
112 switch (ObjectVT) {
113 default: assert(0 && "Unhandled argument type!");
114 case MVT::i1:
115 case MVT::i8: ObjSize = 1; break;
116 case MVT::i16: ObjSize = 2; break;
117 case MVT::i32: ObjSize = 4; break;
118 case MVT::i64: ObjSize = ArgIncrement = 8; break;
119 case MVT::f32: ObjSize = 4; break;
120 case MVT::f64: ObjSize = ArgIncrement = 8; break;
121 }
122 // Create the frame index object for this incoming parameter...
123 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
124
125 // Create the SelectionDAG nodes corresponding to a load from this parameter
126 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
127
128 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
129 // dead loads.
130 SDOperand ArgValue;
131 if (!I->use_empty())
132 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
133 else {
134 if (MVT::isInteger(ObjectVT))
135 ArgValue = DAG.getConstant(0, ObjectVT);
136 else
137 ArgValue = DAG.getConstantFP(0, ObjectVT);
138 }
139 ArgValues.push_back(ArgValue);
140
141 ArgOffset += ArgIncrement; // Move on to the next argument...
142 }
143
144 // If the function takes variable number of arguments, make a frame index for
145 // the start of the first vararg value... for expansion of llvm.va_start.
146 if (F.isVarArg())
147 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000148 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000149 return ArgValues;
150}
151
Chris Lattner5188ad72005-01-08 19:28:19 +0000152std::pair<SDOperand, SDOperand>
153X86TargetLowering::LowerCallTo(SDOperand Chain,
154 const Type *RetTy, SDOperand Callee,
155 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000156 // Count how many bytes are to be pushed on the stack.
157 unsigned NumBytes = 0;
158
159 if (Args.empty()) {
160 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000161 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
162 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000163 } else {
164 for (unsigned i = 0, e = Args.size(); i != e; ++i)
165 switch (getValueType(Args[i].second)) {
166 default: assert(0 && "Unknown value type!");
167 case MVT::i1:
168 case MVT::i8:
169 case MVT::i16:
170 case MVT::i32:
171 case MVT::f32:
172 NumBytes += 4;
173 break;
174 case MVT::i64:
175 case MVT::f64:
176 NumBytes += 8;
177 break;
178 }
179
Chris Lattner5188ad72005-01-08 19:28:19 +0000180 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
181 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000182
183 // Arguments go on the stack in reverse order, as specified by the ABI.
184 unsigned ArgOffset = 0;
185 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32);
186 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
187 unsigned ArgReg;
188 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
189 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
190
191 switch (getValueType(Args[i].second)) {
192 default: assert(0 && "Unexpected ValueType for argument!");
193 case MVT::i1:
194 case MVT::i8:
195 case MVT::i16:
196 // Promote the integer to 32 bits. If the input type is signed use a
197 // sign extend, otherwise use a zero extend.
198 if (Args[i].second->isSigned())
199 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
200 else
201 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
202
203 // FALL THROUGH
204 case MVT::i32:
205 case MVT::f32:
206 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000207 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
208 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000209 ArgOffset += 4;
210 break;
211 case MVT::i64:
212 case MVT::f64:
213 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000214 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
215 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000216 ArgOffset += 8;
217 break;
218 }
219 }
220 }
221
222 std::vector<MVT::ValueType> RetVals;
223 MVT::ValueType RetTyVT = getValueType(RetTy);
224 if (RetTyVT != MVT::isVoid)
225 RetVals.push_back(RetTyVT);
226 RetVals.push_back(MVT::Other);
227
Chris Lattner5188ad72005-01-08 19:28:19 +0000228 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000229 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000230 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
231 DAG.getConstant(NumBytes, getPointerTy()));
232 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000233}
234
Chris Lattner14824582005-01-09 00:01:27 +0000235std::pair<SDOperand, SDOperand>
236X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
237 // vastart just returns the address of the VarArgsFrameIndex slot.
238 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
239}
240
241std::pair<SDOperand,SDOperand> X86TargetLowering::
242LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
243 const Type *ArgTy, SelectionDAG &DAG) {
244 MVT::ValueType ArgVT = getValueType(ArgTy);
245 SDOperand Result;
246 if (!isVANext) {
247 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
248 } else {
249 unsigned Amt;
250 if (ArgVT == MVT::i32)
251 Amt = 4;
252 else {
253 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
254 "Other types should have been promoted for varargs!");
255 Amt = 8;
256 }
257 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
258 DAG.getConstant(Amt, VAList.getValueType()));
259 }
260 return std::make_pair(Result, Chain);
261}
262
263
264std::pair<SDOperand, SDOperand> X86TargetLowering::
265LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
266 SelectionDAG &DAG) {
267 SDOperand Result;
268 if (Depth) // Depths > 0 not supported yet!
269 Result = DAG.getConstant(0, getPointerTy());
270 else {
271 if (ReturnAddrIndex == 0) {
272 // Set up a frame object for the return address.
273 MachineFunction &MF = DAG.getMachineFunction();
274 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
275 }
276
277 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
278
279 if (!isFrameAddress)
280 // Just load the return address
281 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
282 else
283 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
284 DAG.getConstant(4, MVT::i32));
285 }
286 return std::make_pair(Result, Chain);
287}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000288
289
290
291
292
293namespace {
294 Statistic<>
295 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
296
297 //===--------------------------------------------------------------------===//
298 /// ISel - X86 specific code to select X86 machine instructions for
299 /// SelectionDAG operations.
300 ///
301 class ISel : public SelectionDAGISel {
302 /// ContainsFPCode - Every instruction we select that uses or defines a FP
303 /// register should set this to true.
304 bool ContainsFPCode;
305
306 /// X86Lowering - This object fully describes how to lower LLVM code to an
307 /// X86-specific SelectionDAG.
308 X86TargetLowering X86Lowering;
309
Chris Lattner11333092005-01-11 03:11:44 +0000310 /// RegPressureMap - This keeps an approximate count of the number of
311 /// registers required to evaluate each node in the graph.
312 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000313
314 /// ExprMap - As shared expressions are codegen'd, we keep track of which
315 /// vreg the value is produced in, so we only emit one copy of each compiled
316 /// tree.
317 std::map<SDOperand, unsigned> ExprMap;
318 std::set<SDOperand> LoweredTokens;
319
320 public:
321 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
322 }
323
Chris Lattner11333092005-01-11 03:11:44 +0000324 unsigned getRegPressure(SDOperand O) {
325 return RegPressureMap[O.Val];
326 }
327 unsigned ComputeRegPressure(SDOperand O);
328
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000329 /// InstructionSelectBasicBlock - This callback is invoked by
330 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
331 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
332 // While we're doing this, keep track of whether we see any FP code for
333 // FP_REG_KILL insertion.
334 ContainsFPCode = false;
335
Chris Lattner11333092005-01-11 03:11:44 +0000336 // Compute the RegPressureMap, which is an approximation for the number of
337 // registers required to compute each node.
338 ComputeRegPressure(DAG.getRoot());
339
340 //DAG.viewGraph();
341
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000342 // Codegen the basic block.
343 Select(DAG.getRoot());
344
345 // Insert FP_REG_KILL instructions into basic blocks that need them. This
346 // only occurs due to the floating point stackifier not being aggressive
347 // enough to handle arbitrary global stackification.
348 //
349 // Currently we insert an FP_REG_KILL instruction into each block that
350 // uses or defines a floating point virtual register.
351 //
352 // When the global register allocators (like linear scan) finally update
353 // live variable analysis, we can keep floating point values in registers
354 // across basic blocks. This will be a huge win, but we are waiting on
355 // the global allocators before we can do this.
356 //
357 if (ContainsFPCode && BB->succ_size()) {
358 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
359 ++NumFPKill;
360 }
361
362 // Clear state used for selection.
363 ExprMap.clear();
364 LoweredTokens.clear();
Chris Lattner11333092005-01-11 03:11:44 +0000365 RegPressureMap.clear();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000366 }
367
368 void EmitCMP(SDOperand LHS, SDOperand RHS);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000369 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000370 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
371 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000372 unsigned SelectExpr(SDOperand N);
373 bool SelectAddress(SDOperand N, X86AddressMode &AM);
374 void Select(SDOperand N);
375 };
376}
377
Chris Lattner11333092005-01-11 03:11:44 +0000378// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
379// for the number of registers required to compute each node. This is basically
380// computing a generalized form of the Sethi-Ullman number for each node.
381unsigned ISel::ComputeRegPressure(SDOperand O) {
382 SDNode *N = O.Val;
383 unsigned &Result = RegPressureMap[N];
384 if (Result) return Result;
385
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000386 // FIXME: Should operations like CALL (which clobber lots o regs) have a
387 // higher fixed cost??
388
Chris Lattner11333092005-01-11 03:11:44 +0000389 if (N->getNumOperands() == 0)
390 return Result = 1;
391
392 unsigned MaxRegUse = 0;
393 unsigned NumExtraMaxRegUsers = 0;
394 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
395 unsigned Regs = ComputeRegPressure(N->getOperand(i));
396 if (Regs > MaxRegUse) {
397 MaxRegUse = Regs;
398 NumExtraMaxRegUsers = 0;
399 } else if (Regs == MaxRegUse) {
400 ++NumExtraMaxRegUsers;
401 }
402 }
403
404 return Result = MaxRegUse+NumExtraMaxRegUsers;
405}
406
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000407/// SelectAddress - Add the specified node to the specified addressing mode,
408/// returning true if it cannot be done.
409bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
410 switch (N.getOpcode()) {
411 default: break;
412 case ISD::FrameIndex:
413 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
414 AM.BaseType = X86AddressMode::FrameIndexBase;
415 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
416 return false;
417 }
418 break;
419 case ISD::GlobalAddress:
420 if (AM.GV == 0) {
421 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
422 return false;
423 }
424 break;
425 case ISD::Constant:
426 AM.Disp += cast<ConstantSDNode>(N)->getValue();
427 return false;
428 case ISD::SHL:
429 if (AM.IndexReg == 0 || AM.Scale == 1)
430 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
431 unsigned Val = CN->getValue();
432 if (Val == 1 || Val == 2 || Val == 3) {
433 AM.Scale = 1 << Val;
434 AM.IndexReg = SelectExpr(N.Val->getOperand(0));
435 return false;
436 }
437 }
438 break;
439
440 case ISD::ADD: {
441 X86AddressMode Backup = AM;
442 if (!SelectAddress(N.Val->getOperand(0), AM) &&
443 !SelectAddress(N.Val->getOperand(1), AM))
444 return false;
445 AM = Backup;
446 break;
447 }
448 }
449
Chris Lattnera95589b2005-01-11 04:40:19 +0000450 // Is the base register already occupied?
451 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
452 // If so, check to see if the scale index register is set.
453 if (AM.IndexReg == 0) {
454 AM.IndexReg = SelectExpr(N);
455 AM.Scale = 1;
456 return false;
457 }
458
459 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000460 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000461 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000462
463 // Default, generate it as a register.
464 AM.BaseType = X86AddressMode::RegBase;
465 AM.Base.Reg = SelectExpr(N);
466 return false;
467}
468
469/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
470/// assuming that the temporary registers are in the 8-bit register class.
471///
472/// Tmp1 = setcc1
473/// Tmp2 = setcc2
474/// DestReg = logicalop Tmp1, Tmp2
475///
476static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
477 unsigned SetCC2, unsigned LogicalOp,
478 unsigned DestReg) {
479 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
480 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
481 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
482 BuildMI(BB, SetCC1, 0, Tmp1);
483 BuildMI(BB, SetCC2, 0, Tmp2);
484 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
485}
486
487/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
488/// condition codes match the specified SetCCOpcode. Note that some conditions
489/// require multiple instructions to generate the correct value.
490static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
491 ISD::CondCode SetCCOpcode, bool isFP) {
492 unsigned Opc;
493 if (!isFP) {
494 switch (SetCCOpcode) {
495 default: assert(0 && "Illegal integer SetCC!");
496 case ISD::SETEQ: Opc = X86::SETEr; break;
497 case ISD::SETGT: Opc = X86::SETGr; break;
498 case ISD::SETGE: Opc = X86::SETGEr; break;
499 case ISD::SETLT: Opc = X86::SETLr; break;
500 case ISD::SETLE: Opc = X86::SETLEr; break;
501 case ISD::SETNE: Opc = X86::SETNEr; break;
502 case ISD::SETULT: Opc = X86::SETBr; break;
503 case ISD::SETUGT: Opc = X86::SETAr; break;
504 case ISD::SETULE: Opc = X86::SETBEr; break;
505 case ISD::SETUGE: Opc = X86::SETAEr; break;
506 }
507 } else {
508 // On a floating point condition, the flags are set as follows:
509 // ZF PF CF op
510 // 0 | 0 | 0 | X > Y
511 // 0 | 0 | 1 | X < Y
512 // 1 | 0 | 0 | X == Y
513 // 1 | 1 | 1 | unordered
514 //
515 switch (SetCCOpcode) {
516 default: assert(0 && "Invalid FP setcc!");
517 case ISD::SETUEQ:
518 case ISD::SETEQ:
519 Opc = X86::SETEr; // True if ZF = 1
520 break;
521 case ISD::SETOGT:
522 case ISD::SETGT:
523 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
524 break;
525 case ISD::SETOGE:
526 case ISD::SETGE:
527 Opc = X86::SETAEr; // True if CF = 0
528 break;
529 case ISD::SETULT:
530 case ISD::SETLT:
531 Opc = X86::SETBr; // True if CF = 1
532 break;
533 case ISD::SETULE:
534 case ISD::SETLE:
535 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
536 break;
537 case ISD::SETONE:
538 case ISD::SETNE:
539 Opc = X86::SETNEr; // True if ZF = 0
540 break;
541 case ISD::SETUO:
542 Opc = X86::SETPr; // True if PF = 1
543 break;
544 case ISD::SETO:
545 Opc = X86::SETNPr; // True if PF = 0
546 break;
547 case ISD::SETOEQ: // !PF & ZF
548 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
549 return;
550 case ISD::SETOLT: // !PF & CF
551 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
552 return;
553 case ISD::SETOLE: // !PF & (CF || ZF)
554 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
555 return;
556 case ISD::SETUGT: // PF | (!ZF & !CF)
557 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
558 return;
559 case ISD::SETUGE: // PF | !CF
560 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
561 return;
562 case ISD::SETUNE: // PF | !ZF
563 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
564 return;
565 }
566 }
567 BuildMI(BB, Opc, 0, DestReg);
568}
569
570
571/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
572/// the Dest block if the Cond condition is true. If we cannot fold this
573/// condition into the branch, return true.
574///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000575bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
576 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000577 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
578 // B) using two conditional branches instead of one condbr, two setcc's, and
579 // an or.
580 if ((Cond.getOpcode() == ISD::OR ||
581 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
582 // And and or set the flags for us, so there is no need to emit a TST of the
583 // result. It is only safe to do this if there is only a single use of the
584 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000585 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000586 SelectExpr(Cond);
587 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
588 return false;
589 }
590
591 // Codegen br not C -> JE.
592 if (Cond.getOpcode() == ISD::XOR)
593 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
594 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000595 unsigned CondR;
596 if (getRegPressure(Chain) > getRegPressure(Cond)) {
597 Select(Chain);
598 CondR = SelectExpr(Cond.Val->getOperand(0));
599 } else {
600 CondR = SelectExpr(Cond.Val->getOperand(0));
601 Select(Chain);
602 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000603 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
604 BuildMI(BB, X86::JE, 1).addMBB(Dest);
605 return false;
606 }
607
608 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
609 if (SetCC == 0)
610 return true; // Can only handle simple setcc's so far.
611
612 unsigned Opc;
613
614 // Handle integer conditions first.
615 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
616 switch (SetCC->getCondition()) {
617 default: assert(0 && "Illegal integer SetCC!");
618 case ISD::SETEQ: Opc = X86::JE; break;
619 case ISD::SETGT: Opc = X86::JG; break;
620 case ISD::SETGE: Opc = X86::JGE; break;
621 case ISD::SETLT: Opc = X86::JL; break;
622 case ISD::SETLE: Opc = X86::JLE; break;
623 case ISD::SETNE: Opc = X86::JNE; break;
624 case ISD::SETULT: Opc = X86::JB; break;
625 case ISD::SETUGT: Opc = X86::JA; break;
626 case ISD::SETULE: Opc = X86::JBE; break;
627 case ISD::SETUGE: Opc = X86::JAE; break;
628 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000629 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000630 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
631 BuildMI(BB, Opc, 1).addMBB(Dest);
632 return false;
633 }
634
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000635 unsigned Opc2 = 0; // Second branch if needed.
636
637 // On a floating point condition, the flags are set as follows:
638 // ZF PF CF op
639 // 0 | 0 | 0 | X > Y
640 // 0 | 0 | 1 | X < Y
641 // 1 | 0 | 0 | X == Y
642 // 1 | 1 | 1 | unordered
643 //
644 switch (SetCC->getCondition()) {
645 default: assert(0 && "Invalid FP setcc!");
646 case ISD::SETUEQ:
647 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
648 case ISD::SETOGT:
649 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
650 case ISD::SETOGE:
651 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
652 case ISD::SETULT:
653 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
654 case ISD::SETULE:
655 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
656 case ISD::SETONE:
657 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
658 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
659 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
660 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
661 Opc = X86::JA; // ZF = 0 & CF = 0
662 Opc2 = X86::JP; // PF = 1
663 break;
664 case ISD::SETUGE: // PF = 1 | CF = 0
665 Opc = X86::JAE; // CF = 0
666 Opc2 = X86::JP; // PF = 1
667 break;
668 case ISD::SETUNE: // PF = 1 | ZF = 0
669 Opc = X86::JNE; // ZF = 0
670 Opc2 = X86::JP; // PF = 1
671 break;
672 case ISD::SETOEQ: // PF = 0 & ZF = 1
673 //X86::JNP, X86::JE
674 //X86::AND8rr
675 return true; // FIXME: Emit more efficient code for this branch.
676 case ISD::SETOLT: // PF = 0 & CF = 1
677 //X86::JNP, X86::JB
678 //X86::AND8rr
679 return true; // FIXME: Emit more efficient code for this branch.
680 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
681 //X86::JNP, X86::JBE
682 //X86::AND8rr
683 return true; // FIXME: Emit more efficient code for this branch.
684 }
685
Chris Lattner6c07aee2005-01-11 04:06:27 +0000686 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000687 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
688 BuildMI(BB, Opc, 1).addMBB(Dest);
689 if (Opc2)
690 BuildMI(BB, Opc2, 1).addMBB(Dest);
691 return false;
692}
693
Chris Lattner24aad1b2005-01-10 22:10:13 +0000694/// EmitSelectCC - Emit code into BB that performs a select operation between
695/// the two registers RTrue and RFalse, generating a result into RDest. Return
696/// true if the fold cannot be performed.
697///
698void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
699 unsigned RTrue, unsigned RFalse, unsigned RDest) {
700 enum Condition {
701 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
702 NOT_SET
703 } CondCode = NOT_SET;
704
705 static const unsigned CMOVTAB16[] = {
706 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
707 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
708 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
709 };
710 static const unsigned CMOVTAB32[] = {
711 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
712 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
713 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
714 };
715 static const unsigned CMOVTABFP[] = {
716 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
717 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
718 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
719 };
720
721 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
722 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
723 switch (SetCC->getCondition()) {
724 default: assert(0 && "Unknown integer comparison!");
725 case ISD::SETEQ: CondCode = EQ; break;
726 case ISD::SETGT: CondCode = GT; break;
727 case ISD::SETGE: CondCode = GE; break;
728 case ISD::SETLT: CondCode = LT; break;
729 case ISD::SETLE: CondCode = LE; break;
730 case ISD::SETNE: CondCode = NE; break;
731 case ISD::SETULT: CondCode = B; break;
732 case ISD::SETUGT: CondCode = A; break;
733 case ISD::SETULE: CondCode = BE; break;
734 case ISD::SETUGE: CondCode = AE; break;
735 }
736 } else {
737 // On a floating point condition, the flags are set as follows:
738 // ZF PF CF op
739 // 0 | 0 | 0 | X > Y
740 // 0 | 0 | 1 | X < Y
741 // 1 | 0 | 0 | X == Y
742 // 1 | 1 | 1 | unordered
743 //
744 switch (SetCC->getCondition()) {
745 default: assert(0 && "Unknown FP comparison!");
746 case ISD::SETUEQ:
747 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
748 case ISD::SETOGT:
749 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
750 case ISD::SETOGE:
751 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
752 case ISD::SETULT:
753 case ISD::SETLT: CondCode = B; break; // True if CF = 1
754 case ISD::SETULE:
755 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
756 case ISD::SETONE:
757 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
758 case ISD::SETUO: CondCode = P; break; // True if PF = 1
759 case ISD::SETO: CondCode = NP; break; // True if PF = 0
760 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
761 case ISD::SETUGE: // PF = 1 | CF = 0
762 case ISD::SETUNE: // PF = 1 | ZF = 0
763 case ISD::SETOEQ: // PF = 0 & ZF = 1
764 case ISD::SETOLT: // PF = 0 & CF = 1
765 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
766 // We cannot emit this comparison as a single cmov.
767 break;
768 }
769 }
770 }
771
772 unsigned Opc = 0;
773 if (CondCode != NOT_SET) {
774 switch (SVT) {
775 default: assert(0 && "Cannot select this type!");
776 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
777 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
778 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000779 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000780 }
781 }
782
783 // Finally, if we weren't able to fold this, just emit the condition and test
784 // it.
785 if (CondCode == NOT_SET || Opc == 0) {
786 // Get the condition into the zero flag.
787 unsigned CondReg = SelectExpr(Cond);
788 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
789
790 switch (SVT) {
791 default: assert(0 && "Cannot select this type!");
792 case MVT::i16: Opc = X86::CMOVE16rr; break;
793 case MVT::i32: Opc = X86::CMOVE32rr; break;
794 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000795 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000796 }
797 } else {
798 // FIXME: CMP R, 0 -> TEST R, R
799 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000800 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000801 }
802 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
803}
804
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000805void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000806 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000807 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
808 Opc = 0;
809 switch (RHS.getValueType()) {
810 default: break;
811 case MVT::i1:
812 case MVT::i8: Opc = X86::CMP8ri; break;
813 case MVT::i16: Opc = X86::CMP16ri; break;
814 case MVT::i32: Opc = X86::CMP32ri; break;
815 }
816 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000817 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000818 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
819 return;
820 }
821 }
822
823 switch (LHS.getValueType()) {
824 default: assert(0 && "Cannot compare this value!");
825 case MVT::i1:
826 case MVT::i8: Opc = X86::CMP8rr; break;
827 case MVT::i16: Opc = X86::CMP16rr; break;
828 case MVT::i32: Opc = X86::CMP32rr; break;
829 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000830 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000831 }
Chris Lattner11333092005-01-11 03:11:44 +0000832 unsigned Tmp1, Tmp2;
833 if (getRegPressure(LHS) > getRegPressure(RHS)) {
834 Tmp1 = SelectExpr(LHS);
835 Tmp2 = SelectExpr(RHS);
836 } else {
837 Tmp2 = SelectExpr(RHS);
838 Tmp1 = SelectExpr(LHS);
839 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000840 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
841}
842
843unsigned ISel::SelectExpr(SDOperand N) {
844 unsigned Result;
845 unsigned Tmp1, Tmp2, Tmp3;
846 unsigned Opc = 0;
847
Chris Lattner5188ad72005-01-08 19:28:19 +0000848 SDNode *Node = N.Val;
849
Chris Lattner590d8002005-01-09 18:52:44 +0000850 if (Node->getOpcode() == ISD::CopyFromReg)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000851 // Just use the specified register as our input.
Chris Lattner590d8002005-01-09 18:52:44 +0000852 return dyn_cast<CopyRegSDNode>(Node)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000853
854 // If there are multiple uses of this expression, memorize the
855 // register it is code generated in, instead of emitting it multiple
856 // times.
857 // FIXME: Disabled for our current selection model.
Chris Lattner5188ad72005-01-08 19:28:19 +0000858 if (1 || !Node->hasOneUse()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000859 unsigned &Reg = ExprMap[N];
860 if (Reg) return Reg;
861
862 if (N.getOpcode() != ISD::CALL)
863 Reg = Result = (N.getValueType() != MVT::Other) ?
864 MakeReg(N.getValueType()) : 1;
865 else {
866 // If this is a call instruction, make sure to prepare ALL of the result
867 // values as well as the chain.
Chris Lattner5188ad72005-01-08 19:28:19 +0000868 if (Node->getNumValues() == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000869 Reg = Result = 1; // Void call, just a chain.
870 else {
Chris Lattner5188ad72005-01-08 19:28:19 +0000871 Result = MakeReg(Node->getValueType(0));
872 ExprMap[N.getValue(0)] = Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000873 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattner5188ad72005-01-08 19:28:19 +0000874 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
875 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000876 }
877 }
878 } else {
879 Result = MakeReg(N.getValueType());
880 }
881
882 switch (N.getOpcode()) {
883 default:
Chris Lattner5188ad72005-01-08 19:28:19 +0000884 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000885 assert(0 && "Node not handled!\n");
886 case ISD::FrameIndex:
887 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
888 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
889 return Result;
890 case ISD::ConstantPool:
891 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
892 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
893 return Result;
894 case ISD::ConstantFP:
895 ContainsFPCode = true;
896 Tmp1 = Result; // Intermediate Register
897 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
898 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
899 Tmp1 = MakeReg(MVT::f64);
900
901 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
902 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
903 BuildMI(BB, X86::FLD0, 0, Tmp1);
904 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
905 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
906 BuildMI(BB, X86::FLD1, 0, Tmp1);
907 else
908 assert(0 && "Unexpected constant!");
909 if (Tmp1 != Result)
910 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
911 return Result;
912 case ISD::Constant:
913 switch (N.getValueType()) {
914 default: assert(0 && "Cannot use constants of this type!");
915 case MVT::i1:
916 case MVT::i8: Opc = X86::MOV8ri; break;
917 case MVT::i16: Opc = X86::MOV16ri; break;
918 case MVT::i32: Opc = X86::MOV32ri; break;
919 }
920 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
921 return Result;
922 case ISD::GlobalAddress: {
923 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
924 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
925 return Result;
926 }
927 case ISD::ExternalSymbol: {
928 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
929 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
930 return Result;
931 }
932 case ISD::FP_EXTEND:
933 Tmp1 = SelectExpr(N.getOperand(0));
934 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000935 return Result;
936 case ISD::ZERO_EXTEND: {
937 int DestIs16 = N.getValueType() == MVT::i16;
938 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +0000939 Tmp1 = SelectExpr(N.getOperand(0));
940
941 // FIXME: This hack is here for zero extension casts from bool to i8. This
942 // would not be needed if bools were promoted by Legalize.
943 if (N.getValueType() == MVT::i8) {
944 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
945 return Result;
946 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000947
948 static const unsigned Opc[3] = {
949 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
950 };
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000951 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
952 return Result;
953 }
954 case ISD::SIGN_EXTEND: {
955 int DestIs16 = N.getValueType() == MVT::i16;
956 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
957
Chris Lattner590d8002005-01-09 18:52:44 +0000958 // FIXME: Legalize should promote bools to i8!
959 assert(N.getOperand(0).getValueType() != MVT::i1 &&
960 "Sign extend from bool not implemented!");
961
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000962 static const unsigned Opc[3] = {
963 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
964 };
965 Tmp1 = SelectExpr(N.getOperand(0));
966 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
967 return Result;
968 }
969 case ISD::TRUNCATE:
970 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
971 // a move out of AX or AL.
972 switch (N.getOperand(0).getValueType()) {
973 default: assert(0 && "Unknown truncate!");
974 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
975 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
976 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
977 }
978 Tmp1 = SelectExpr(N.getOperand(0));
979 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
980
981 switch (N.getValueType()) {
982 default: assert(0 && "Unknown truncate!");
983 case MVT::i1:
984 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
985 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
986 }
987 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
988 return Result;
989
990 case ISD::FP_ROUND:
991 // Truncate from double to float by storing to memory as float,
992 // then reading it back into a register.
993
994 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +0000995 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000996 Tmp1 = TLI.getTargetData().getFloatAlignment();
997 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
998
999 // Codegen the input.
1000 Tmp1 = SelectExpr(N.getOperand(0));
1001
1002 // Emit the store, then the reload.
1003 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1004 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001005 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001006
1007 case ISD::SINT_TO_FP:
1008 case ISD::UINT_TO_FP: {
1009 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001010 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001011
1012 // Promote the integer to a type supported by FLD. We do this because there
1013 // are no unsigned FLD instructions, so we must promote an unsigned value to
1014 // a larger signed value, then use FLD on the larger value.
1015 //
1016 MVT::ValueType PromoteType = MVT::Other;
1017 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1018 unsigned PromoteOpcode = 0;
1019 unsigned RealDestReg = Result;
1020 switch (SrcTy) {
1021 case MVT::i1:
1022 case MVT::i8:
1023 // We don't have the facilities for directly loading byte sized data from
1024 // memory (even signed). Promote it to 16 bits.
1025 PromoteType = MVT::i16;
1026 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1027 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1028 break;
1029 case MVT::i16:
1030 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1031 PromoteType = MVT::i32;
1032 PromoteOpcode = X86::MOVZX32rr16;
1033 }
1034 break;
1035 default:
1036 // Don't fild into the real destination.
1037 if (Node->getOpcode() == ISD::UINT_TO_FP)
1038 Result = MakeReg(Node->getValueType(0));
1039 break;
1040 }
1041
1042 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1043
1044 if (PromoteType != MVT::Other) {
1045 Tmp2 = MakeReg(PromoteType);
1046 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1047 SrcTy = PromoteType;
1048 Tmp1 = Tmp2;
1049 }
1050
1051 // Spill the integer to memory and reload it from there.
1052 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1053 MachineFunction *F = BB->getParent();
1054 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1055
1056 switch (SrcTy) {
1057 case MVT::i64:
1058 // FIXME: this won't work for cast [u]long to FP
1059 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1060 FrameIdx).addReg(Tmp1);
1061 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1062 FrameIdx, 4).addReg(Tmp1+1);
1063 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1064 break;
1065 case MVT::i32:
1066 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1067 FrameIdx).addReg(Tmp1);
1068 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1069 break;
1070 case MVT::i16:
1071 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1072 FrameIdx).addReg(Tmp1);
1073 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1074 break;
1075 default: break; // No promotion required.
1076 }
1077
1078 if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i32) {
1079 // If this is a cast from uint -> double, we need to be careful when if
1080 // the "sign" bit is set. If so, we don't want to make a negative number,
1081 // we want to make a positive number. Emit code to add an offset if the
1082 // sign bit is set.
1083
1084 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1085 unsigned IsNeg = MakeReg(MVT::i32);
1086 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1087
1088 // Create a CP value that has the offset in one word and 0 in the other.
1089 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1090 0x4f80000000000000ULL);
1091 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1092 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1093 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1094
1095 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1096 // We need special handling for unsigned 64-bit integer sources. If the
1097 // input number has the "sign bit" set, then we loaded it incorrectly as a
1098 // negative 64-bit number. In this case, add an offset value.
1099
1100 // Emit a test instruction to see if the dynamic input value was signed.
1101 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1102
1103 // If the sign bit is set, get a pointer to an offset, otherwise get a
1104 // pointer to a zero.
1105 MachineConstantPool *CP = F->getConstantPool();
1106 unsigned Zero = MakeReg(MVT::i32);
1107 Constant *Null = Constant::getNullValue(Type::UIntTy);
1108 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1109 CP->getConstantPoolIndex(Null));
1110 unsigned Offset = MakeReg(MVT::i32);
1111 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1112
1113 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1114 CP->getConstantPoolIndex(OffsetCst));
1115 unsigned Addr = MakeReg(MVT::i32);
1116 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1117
1118 // Load the constant for an add. FIXME: this could make an 'fadd' that
1119 // reads directly from memory, but we don't support these yet.
1120 unsigned ConstReg = MakeReg(MVT::f64);
1121 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1122
1123 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1124 }
1125 return RealDestReg;
1126 }
1127 case ISD::FP_TO_SINT:
1128 case ISD::FP_TO_UINT: {
1129 // FIXME: Most of this grunt work should be done by legalize!
1130 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1131
1132 // Change the floating point control register to use "round towards zero"
1133 // mode when truncating to an integer value.
1134 //
1135 MachineFunction *F = BB->getParent();
1136 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1137 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1138
1139 // Load the old value of the high byte of the control word...
1140 unsigned HighPartOfCW = MakeReg(MVT::i8);
1141 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1142 CWFrameIdx, 1);
1143
1144 // Set the high part to be round to zero...
1145 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1146 CWFrameIdx, 1).addImm(12);
1147
1148 // Reload the modified control word now...
1149 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1150
1151 // Restore the memory image of control word to original value
1152 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1153 CWFrameIdx, 1).addReg(HighPartOfCW);
1154
1155 // We don't have the facilities for directly storing byte sized data to
1156 // memory. Promote it to 16 bits. We also must promote unsigned values to
1157 // larger classes because we only have signed FP stores.
1158 MVT::ValueType StoreClass = Node->getValueType(0);
1159 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1160 switch (StoreClass) {
1161 case MVT::i8: StoreClass = MVT::i16; break;
1162 case MVT::i16: StoreClass = MVT::i32; break;
1163 case MVT::i32: StoreClass = MVT::i64; break;
1164 // The following treatment of cLong may not be perfectly right,
1165 // but it survives chains of casts of the form
1166 // double->ulong->double.
1167 case MVT::i64: StoreClass = MVT::i64; break;
1168 default: assert(0 && "Unknown store class!");
1169 }
1170
1171 // Spill the integer to memory and reload it from there.
1172 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1173 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1174
1175 switch (StoreClass) {
1176 default: assert(0 && "Unknown store class!");
1177 case MVT::i16:
1178 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1179 break;
1180 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001181 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001182 break;
1183 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001184 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001185 break;
1186 }
1187
1188 switch (Node->getValueType(0)) {
1189 default:
1190 assert(0 && "Unknown integer type!");
1191 case MVT::i64:
1192 // FIXME: this isn't gunna work.
1193 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1194 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1195 case MVT::i32:
1196 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1197 break;
1198 case MVT::i16:
1199 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1200 break;
1201 case MVT::i8:
1202 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1203 break;
1204 }
1205
1206 // Reload the original control word now.
1207 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1208 return Result;
1209 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001210 case ISD::ADD:
1211 // See if we can codegen this as an LEA to fold operations together.
1212 if (N.getValueType() == MVT::i32) {
1213 X86AddressMode AM;
1214 if (!SelectAddress(N.getOperand(0), AM) &&
1215 !SelectAddress(N.getOperand(1), AM)) {
1216 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001217 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001218 // leave this as LEA, then peephole it to 'ADD' after two address elim
1219 // happens.
1220 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001221 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001222 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1223 return Result;
1224 }
1225 }
1226 }
Chris Lattner11333092005-01-11 03:11:44 +00001227
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001228 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1229 Opc = 0;
1230 if (CN->getValue() == 1) { // add X, 1 -> inc X
1231 switch (N.getValueType()) {
1232 default: assert(0 && "Cannot integer add this type!");
1233 case MVT::i8: Opc = X86::INC8r; break;
1234 case MVT::i16: Opc = X86::INC16r; break;
1235 case MVT::i32: Opc = X86::INC32r; break;
1236 }
1237 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1238 switch (N.getValueType()) {
1239 default: assert(0 && "Cannot integer add this type!");
1240 case MVT::i8: Opc = X86::DEC8r; break;
1241 case MVT::i16: Opc = X86::DEC16r; break;
1242 case MVT::i32: Opc = X86::DEC32r; break;
1243 }
1244 }
1245
1246 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001247 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001248 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1249 return Result;
1250 }
1251
1252 switch (N.getValueType()) {
1253 default: assert(0 && "Cannot add this type!");
1254 case MVT::i8: Opc = X86::ADD8ri; break;
1255 case MVT::i16: Opc = X86::ADD16ri; break;
1256 case MVT::i32: Opc = X86::ADD32ri; break;
1257 }
1258 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001259 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001260 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1261 return Result;
1262 }
1263 }
1264
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001265 switch (N.getValueType()) {
1266 default: assert(0 && "Cannot add this type!");
1267 case MVT::i8: Opc = X86::ADD8rr; break;
1268 case MVT::i16: Opc = X86::ADD16rr; break;
1269 case MVT::i32: Opc = X86::ADD32rr; break;
1270 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001271 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001272 }
Chris Lattner11333092005-01-11 03:11:44 +00001273
1274 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1275 Tmp1 = SelectExpr(N.getOperand(0));
1276 Tmp2 = SelectExpr(N.getOperand(1));
1277 } else {
1278 Tmp2 = SelectExpr(N.getOperand(1));
1279 Tmp1 = SelectExpr(N.getOperand(0));
1280 }
1281
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001282 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1283 return Result;
1284 case ISD::SUB:
1285 if (MVT::isInteger(N.getValueType()))
1286 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1287 if (CN->isNullValue()) { // 0 - N -> neg N
1288 switch (N.getValueType()) {
1289 default: assert(0 && "Cannot sub this type!");
1290 case MVT::i1:
1291 case MVT::i8: Opc = X86::NEG8r; break;
1292 case MVT::i16: Opc = X86::NEG16r; break;
1293 case MVT::i32: Opc = X86::NEG32r; break;
1294 }
1295 Tmp1 = SelectExpr(N.getOperand(1));
1296 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1297 return Result;
1298 }
1299
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001300 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1301 switch (N.getValueType()) {
1302 default: assert(0 && "Cannot sub this type!");
1303 case MVT::i1:
1304 case MVT::i8: Opc = X86::SUB8ri; break;
1305 case MVT::i16: Opc = X86::SUB16ri; break;
1306 case MVT::i32: Opc = X86::SUB32ri; break;
1307 }
Chris Lattner11333092005-01-11 03:11:44 +00001308 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001309 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1310 return Result;
1311 }
Chris Lattner11333092005-01-11 03:11:44 +00001312
1313 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1314 Tmp1 = SelectExpr(N.getOperand(0));
1315 Tmp2 = SelectExpr(N.getOperand(1));
1316 } else {
1317 Tmp2 = SelectExpr(N.getOperand(1));
1318 Tmp1 = SelectExpr(N.getOperand(0));
1319 }
1320
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001321 switch (N.getValueType()) {
1322 default: assert(0 && "Cannot add this type!");
1323 case MVT::i1:
1324 case MVT::i8: Opc = X86::SUB8rr; break;
1325 case MVT::i16: Opc = X86::SUB16rr; break;
1326 case MVT::i32: Opc = X86::SUB32rr; break;
1327 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001328 case MVT::f64: Opc = X86::FpSUB; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001329 }
1330 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1331 return Result;
1332
1333 case ISD::AND:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001334 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1335 switch (N.getValueType()) {
1336 default: assert(0 && "Cannot add this type!");
1337 case MVT::i1:
1338 case MVT::i8: Opc = X86::AND8ri; break;
1339 case MVT::i16: Opc = X86::AND16ri; break;
1340 case MVT::i32: Opc = X86::AND32ri; break;
1341 }
Chris Lattner11333092005-01-11 03:11:44 +00001342 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001343 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1344 return Result;
1345 }
Chris Lattner11333092005-01-11 03:11:44 +00001346
1347 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1348 Tmp1 = SelectExpr(N.getOperand(0));
1349 Tmp2 = SelectExpr(N.getOperand(1));
1350 } else {
1351 Tmp2 = SelectExpr(N.getOperand(1));
1352 Tmp1 = SelectExpr(N.getOperand(0));
1353 }
1354
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001355 switch (N.getValueType()) {
1356 default: assert(0 && "Cannot add this type!");
1357 case MVT::i1:
1358 case MVT::i8: Opc = X86::AND8rr; break;
1359 case MVT::i16: Opc = X86::AND16rr; break;
1360 case MVT::i32: Opc = X86::AND32rr; break;
1361 }
1362 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1363 return Result;
1364 case ISD::OR:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001365 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001366 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001367 switch (N.getValueType()) {
1368 default: assert(0 && "Cannot add this type!");
1369 case MVT::i1:
1370 case MVT::i8: Opc = X86::OR8ri; break;
1371 case MVT::i16: Opc = X86::OR16ri; break;
1372 case MVT::i32: Opc = X86::OR32ri; break;
1373 }
1374 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1375 return Result;
1376 }
Chris Lattner11333092005-01-11 03:11:44 +00001377
1378 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1379 Tmp1 = SelectExpr(N.getOperand(0));
1380 Tmp2 = SelectExpr(N.getOperand(1));
1381 } else {
1382 Tmp2 = SelectExpr(N.getOperand(1));
1383 Tmp1 = SelectExpr(N.getOperand(0));
1384 }
1385
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001386 switch (N.getValueType()) {
1387 default: assert(0 && "Cannot add this type!");
1388 case MVT::i1:
1389 case MVT::i8: Opc = X86::OR8rr; break;
1390 case MVT::i16: Opc = X86::OR16rr; break;
1391 case MVT::i32: Opc = X86::OR32rr; break;
1392 }
1393 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1394 return Result;
1395 case ISD::XOR:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001396 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001397 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerd4dab922005-01-11 04:31:30 +00001398
1399 if (CN->isAllOnesValue()) {
1400 switch (N.getValueType()) {
1401 default: assert(0 && "Cannot add this type!");
1402 case MVT::i1:
1403 case MVT::i8: Opc = X86::NOT8r; break;
1404 case MVT::i16: Opc = X86::NOT16r; break;
1405 case MVT::i32: Opc = X86::NOT32r; break;
1406 }
1407 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1408 return Result;
1409 }
1410
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001411 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001412 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001413 case MVT::i1:
1414 case MVT::i8: Opc = X86::XOR8ri; break;
1415 case MVT::i16: Opc = X86::XOR16ri; break;
1416 case MVT::i32: Opc = X86::XOR32ri; break;
1417 }
1418 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1419 return Result;
1420 }
Chris Lattner11333092005-01-11 03:11:44 +00001421
1422 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1423 Tmp1 = SelectExpr(N.getOperand(0));
1424 Tmp2 = SelectExpr(N.getOperand(1));
1425 } else {
1426 Tmp2 = SelectExpr(N.getOperand(1));
1427 Tmp1 = SelectExpr(N.getOperand(0));
1428 }
1429
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001430 switch (N.getValueType()) {
1431 default: assert(0 && "Cannot add this type!");
1432 case MVT::i1:
1433 case MVT::i8: Opc = X86::XOR8rr; break;
1434 case MVT::i16: Opc = X86::XOR16rr; break;
1435 case MVT::i32: Opc = X86::XOR32rr; break;
1436 }
1437 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1438 return Result;
1439
1440 case ISD::MUL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001441 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1442 Opc = 0;
1443 switch (N.getValueType()) {
1444 default: assert(0 && "Cannot multiply this type!");
1445 case MVT::i8: break;
1446 case MVT::i16: Opc = X86::IMUL16rri; break;
1447 case MVT::i32: Opc = X86::IMUL32rri; break;
1448 }
1449 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001450 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001451 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1452 return Result;
1453 }
1454 }
Chris Lattner11333092005-01-11 03:11:44 +00001455
1456 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1457 Tmp1 = SelectExpr(N.getOperand(0));
1458 Tmp2 = SelectExpr(N.getOperand(1));
1459 } else {
1460 Tmp2 = SelectExpr(N.getOperand(1));
1461 Tmp1 = SelectExpr(N.getOperand(0));
1462 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001463 switch (N.getValueType()) {
1464 default: assert(0 && "Cannot add this type!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001465 case MVT::i8:
1466 // Must use the MUL instruction, which forces use of AL.
1467 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1468 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1469 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1470 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001471 case MVT::i16: Opc = X86::IMUL16rr; break;
1472 case MVT::i32: Opc = X86::IMUL32rr; break;
1473 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001474 case MVT::f64: Opc = X86::FpMUL; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001475 }
1476 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1477 return Result;
1478
1479 case ISD::SELECT:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001480 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001481 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1482 Tmp2 = SelectExpr(N.getOperand(1));
1483 Tmp3 = SelectExpr(N.getOperand(2));
1484 } else {
1485 Tmp3 = SelectExpr(N.getOperand(2));
1486 Tmp2 = SelectExpr(N.getOperand(1));
1487 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001488 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001489 return Result;
1490 } else {
1491 // FIXME: This should not be implemented here, it should be in the generic
1492 // code!
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001493 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1494 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1495 N.getOperand(1)));
1496 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1497 N.getOperand(2)));
1498 } else {
1499 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1500 N.getOperand(2)));
1501 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1502 N.getOperand(1)));
1503 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001504 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001505 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1506 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001507 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1508 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1509 return Result;
1510 }
1511
1512 case ISD::SDIV:
1513 case ISD::UDIV:
1514 case ISD::SREM:
1515 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001516 if (N.getOpcode() == ISD::SDIV)
1517 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1518 // FIXME: These special cases should be handled by the lowering impl!
1519 unsigned RHS = CN->getValue();
1520 bool isNeg = false;
1521 if ((int)RHS < 0) {
1522 isNeg = true;
1523 RHS = -RHS;
1524 }
1525 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1526 unsigned Log = log2(RHS);
1527 unsigned TmpReg = MakeReg(N.getValueType());
1528 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1529 switch (N.getValueType()) {
1530 default: assert("Unknown type to signed divide!");
1531 case MVT::i8:
1532 SAROpc = X86::SAR8ri;
1533 SHROpc = X86::SHR8ri;
1534 ADDOpc = X86::ADD8rr;
1535 NEGOpc = X86::NEG8r;
1536 break;
1537 case MVT::i16:
1538 SAROpc = X86::SAR16ri;
1539 SHROpc = X86::SHR16ri;
1540 ADDOpc = X86::ADD16rr;
1541 NEGOpc = X86::NEG16r;
1542 break;
1543 case MVT::i32:
1544 SAROpc = X86::SAR32ri;
1545 SHROpc = X86::SHR32ri;
1546 ADDOpc = X86::ADD32rr;
1547 NEGOpc = X86::NEG32r;
1548 break;
1549 }
Chris Lattner11333092005-01-11 03:11:44 +00001550 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001551 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1552 unsigned TmpReg2 = MakeReg(N.getValueType());
1553 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1554 unsigned TmpReg3 = MakeReg(N.getValueType());
1555 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1556
1557 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1558 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1559 if (isNeg)
1560 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1561 return Result;
1562 }
1563 }
1564
Chris Lattner11333092005-01-11 03:11:44 +00001565 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1566 Tmp1 = SelectExpr(N.getOperand(0));
1567 Tmp2 = SelectExpr(N.getOperand(1));
1568 } else {
1569 Tmp2 = SelectExpr(N.getOperand(1));
1570 Tmp1 = SelectExpr(N.getOperand(0));
1571 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001572
1573 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1574 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1575 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1576 switch (N.getValueType()) {
1577 default: assert(0 && "Cannot sdiv this type!");
1578 case MVT::i8:
1579 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1580 LoReg = X86::AL;
1581 HiReg = X86::AH;
1582 MovOpcode = X86::MOV8rr;
1583 ClrOpcode = X86::MOV8ri;
1584 SExtOpcode = X86::CBW;
1585 break;
1586 case MVT::i16:
1587 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1588 LoReg = X86::AX;
1589 HiReg = X86::DX;
1590 MovOpcode = X86::MOV16rr;
1591 ClrOpcode = X86::MOV16ri;
1592 SExtOpcode = X86::CWD;
1593 break;
1594 case MVT::i32:
1595 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
1596 LoReg =X86::EAX;
1597 HiReg = X86::EDX;
1598 MovOpcode = X86::MOV32rr;
1599 ClrOpcode = X86::MOV32ri;
1600 SExtOpcode = X86::CDQ;
1601 break;
1602 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1603 case MVT::f32:
1604 case MVT::f64:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001605 if (N.getOpcode() == ISD::SDIV)
1606 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1607 else
1608 assert(0 && "FIXME: Emit frem libcall to fmod!");
1609 return Result;
1610 }
1611
1612 // Set up the low part.
1613 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1614
1615 if (isSigned) {
1616 // Sign extend the low part into the high part.
1617 BuildMI(BB, SExtOpcode, 0);
1618 } else {
1619 // Zero out the high part, effectively zero extending the input.
1620 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1621 }
1622
1623 // Emit the DIV/IDIV instruction.
1624 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1625
1626 // Get the result of the divide or rem.
1627 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1628 return Result;
1629 }
1630
1631 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001632 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1633 switch (N.getValueType()) {
1634 default: assert(0 && "Cannot shift this type!");
1635 case MVT::i8: Opc = X86::SHL8ri; break;
1636 case MVT::i16: Opc = X86::SHL16ri; break;
1637 case MVT::i32: Opc = X86::SHL32ri; break;
1638 }
Chris Lattner11333092005-01-11 03:11:44 +00001639 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001640 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1641 return Result;
1642 }
Chris Lattner11333092005-01-11 03:11:44 +00001643
1644 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1645 Tmp1 = SelectExpr(N.getOperand(0));
1646 Tmp2 = SelectExpr(N.getOperand(1));
1647 } else {
1648 Tmp2 = SelectExpr(N.getOperand(1));
1649 Tmp1 = SelectExpr(N.getOperand(0));
1650 }
1651
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001652 switch (N.getValueType()) {
1653 default: assert(0 && "Cannot shift this type!");
1654 case MVT::i8 : Opc = X86::SHL8rCL; break;
1655 case MVT::i16: Opc = X86::SHL16rCL; break;
1656 case MVT::i32: Opc = X86::SHL32rCL; break;
1657 }
1658 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1659 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1660 return Result;
1661 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001662 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1663 switch (N.getValueType()) {
1664 default: assert(0 && "Cannot shift this type!");
1665 case MVT::i8: Opc = X86::SHR8ri; break;
1666 case MVT::i16: Opc = X86::SHR16ri; break;
1667 case MVT::i32: Opc = X86::SHR32ri; break;
1668 }
Chris Lattner11333092005-01-11 03:11:44 +00001669 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001670 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1671 return Result;
1672 }
Chris Lattner11333092005-01-11 03:11:44 +00001673
1674 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1675 Tmp1 = SelectExpr(N.getOperand(0));
1676 Tmp2 = SelectExpr(N.getOperand(1));
1677 } else {
1678 Tmp2 = SelectExpr(N.getOperand(1));
1679 Tmp1 = SelectExpr(N.getOperand(0));
1680 }
1681
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001682 switch (N.getValueType()) {
1683 default: assert(0 && "Cannot shift this type!");
1684 case MVT::i8 : Opc = X86::SHR8rCL; break;
1685 case MVT::i16: Opc = X86::SHR16rCL; break;
1686 case MVT::i32: Opc = X86::SHR32rCL; break;
1687 }
1688 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1689 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1690 return Result;
1691 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001692 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1693 switch (N.getValueType()) {
1694 default: assert(0 && "Cannot shift this type!");
1695 case MVT::i8: Opc = X86::SAR8ri; break;
1696 case MVT::i16: Opc = X86::SAR16ri; break;
1697 case MVT::i32: Opc = X86::SAR32ri; break;
1698 }
Chris Lattner11333092005-01-11 03:11:44 +00001699 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001700 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1701 return Result;
1702 }
Chris Lattner11333092005-01-11 03:11:44 +00001703
1704 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1705 Tmp1 = SelectExpr(N.getOperand(0));
1706 Tmp2 = SelectExpr(N.getOperand(1));
1707 } else {
1708 Tmp2 = SelectExpr(N.getOperand(1));
1709 Tmp1 = SelectExpr(N.getOperand(0));
1710 }
1711
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001712 switch (N.getValueType()) {
1713 default: assert(0 && "Cannot shift this type!");
1714 case MVT::i8 : Opc = X86::SAR8rCL; break;
1715 case MVT::i16: Opc = X86::SAR16rCL; break;
1716 case MVT::i32: Opc = X86::SAR32rCL; break;
1717 }
1718 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1719 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1720 return Result;
1721
1722 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001723 EmitCMP(N.getOperand(0), N.getOperand(1));
1724 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1725 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1726 return Result;
1727 case ISD::LOAD: {
Chris Lattner5188ad72005-01-08 19:28:19 +00001728 // The chain for this load is now lowered.
1729 LoweredTokens.insert(SDOperand(Node, 1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001730
1731 // Make sure we generate both values.
1732 if (Result != 1)
1733 ExprMap[N.getValue(1)] = 1; // Generate the token
1734 else
1735 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1736
Chris Lattner5188ad72005-01-08 19:28:19 +00001737 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001738 default: assert(0 && "Cannot load this type!");
1739 case MVT::i1:
1740 case MVT::i8: Opc = X86::MOV8rm; break;
1741 case MVT::i16: Opc = X86::MOV16rm; break;
1742 case MVT::i32: Opc = X86::MOV32rm; break;
1743 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1744 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1745 }
Chris Lattner11333092005-01-11 03:11:44 +00001746
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001747 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001748 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001749 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1750 } else {
1751 X86AddressMode AM;
Chris Lattner11333092005-01-11 03:11:44 +00001752 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1753 Select(N.getOperand(0));
1754 SelectAddress(N.getOperand(1), AM);
1755 } else {
1756 SelectAddress(N.getOperand(1), AM);
1757 Select(N.getOperand(0));
1758 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001759 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1760 }
1761 return Result;
1762 }
1763 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001764 // Generate both result values.
1765 if (Result != 1)
1766 ExprMap[N.getValue(1)] = 1; // Generate the token
1767 else
1768 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1769
1770 // FIXME: We are currently ignoring the requested alignment for handling
1771 // greater than the stack alignment. This will need to be revisited at some
1772 // point. Align = N.getOperand(2);
1773
1774 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1775 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1776 std::cerr << "Cannot allocate stack object with greater alignment than"
1777 << " the stack alignment yet!";
1778 abort();
1779 }
1780
1781 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001782 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001783 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1784 .addImm(CN->getValue());
1785 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001786 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1787 Select(N.getOperand(0));
1788 Tmp1 = SelectExpr(N.getOperand(1));
1789 } else {
1790 Tmp1 = SelectExpr(N.getOperand(1));
1791 Select(N.getOperand(0));
1792 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001793
1794 // Subtract size from stack pointer, thereby allocating some space.
1795 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
1796 }
1797
1798 // Put a pointer to the space into the result register, by copying the stack
1799 // pointer.
1800 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
1801 return Result;
1802
1803 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00001804 // The chain for this call is now lowered.
1805 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
1806
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001807 if (GlobalAddressSDNode *GASD =
1808 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001809 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001810 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
1811 } else if (ExternalSymbolSDNode *ESSDN =
1812 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001813 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001814 BuildMI(BB, X86::CALLpcrel32,
1815 1).addExternalSymbol(ESSDN->getSymbol(), true);
1816 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001817 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1818 Select(N.getOperand(0));
1819 Tmp1 = SelectExpr(N.getOperand(1));
1820 } else {
1821 Tmp1 = SelectExpr(N.getOperand(1));
1822 Select(N.getOperand(0));
1823 }
1824
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001825 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
1826 }
Chris Lattner5188ad72005-01-08 19:28:19 +00001827 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001828 default: assert(0 && "Unknown value type for call result!");
1829 case MVT::Other: return 1;
1830 case MVT::i1:
1831 case MVT::i8:
1832 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1833 break;
1834 case MVT::i16:
1835 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
1836 break;
1837 case MVT::i32:
1838 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00001839 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001840 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
1841 break;
1842 case MVT::f32:
1843 case MVT::f64: // Floating-point return values live in %ST(0)
1844 ContainsFPCode = true;
1845 BuildMI(BB, X86::FpGETRESULT, 1, Result);
1846 break;
1847 }
1848 return Result+N.ResNo;
1849 }
1850
1851 return 0;
1852}
1853
1854void ISel::Select(SDOperand N) {
1855 unsigned Tmp1, Tmp2, Opc;
1856
1857 // FIXME: Disable for our current expansion model!
1858 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
1859 return; // Already selected.
1860
1861 switch (N.getOpcode()) {
1862 default:
1863 N.Val->dump(); std::cerr << "\n";
1864 assert(0 && "Node not handled yet!");
1865 case ISD::EntryToken: return; // Noop
1866 case ISD::CopyToReg:
1867 Select(N.getOperand(0));
1868 Tmp1 = SelectExpr(N.getOperand(1));
1869 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
1870
1871 if (Tmp1 != Tmp2) {
1872 switch (N.getOperand(1).getValueType()) {
1873 default: assert(0 && "Invalid type for operation!");
1874 case MVT::i1:
1875 case MVT::i8: Opc = X86::MOV8rr; break;
1876 case MVT::i16: Opc = X86::MOV16rr; break;
1877 case MVT::i32: Opc = X86::MOV32rr; break;
1878 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001879 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001880 }
1881 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1882 }
1883 return;
1884 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001885 switch (N.getNumOperands()) {
1886 default:
1887 assert(0 && "Unknown return instruction!");
1888 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001889 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1890 N.getOperand(2).getValueType() == MVT::i32 &&
1891 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00001892 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1893 Tmp1 = SelectExpr(N.getOperand(1));
1894 Tmp2 = SelectExpr(N.getOperand(2));
1895 } else {
1896 Tmp2 = SelectExpr(N.getOperand(2));
1897 Tmp1 = SelectExpr(N.getOperand(1));
1898 }
1899 Select(N.getOperand(0));
1900
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001901 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1902 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
1903 // Declare that EAX & EDX are live on exit.
1904 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1905 .addReg(X86::ESP);
1906 break;
1907 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00001908 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1909 Select(N.getOperand(0));
1910 Tmp1 = SelectExpr(N.getOperand(1));
1911 } else {
1912 Tmp1 = SelectExpr(N.getOperand(1));
1913 Select(N.getOperand(0));
1914 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001915 switch (N.getOperand(1).getValueType()) {
1916 default: assert(0 && "All other types should have been promoted!!");
1917 case MVT::f64:
1918 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
1919 // Declare that top-of-stack is live on exit
1920 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
1921 break;
1922 case MVT::i32:
1923 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1924 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
1925 break;
1926 }
1927 break;
1928 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00001929 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001930 break;
1931 }
1932 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1933 return;
1934 case ISD::BR: {
1935 Select(N.getOperand(0));
1936 MachineBasicBlock *Dest =
1937 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1938 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
1939 return;
1940 }
1941
1942 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001943 MachineBasicBlock *Dest =
1944 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00001945
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001946 // Try to fold a setcc into the branch. If this fails, emit a test/jne
1947 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00001948 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
1949 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1950 Select(N.getOperand(0));
1951 Tmp1 = SelectExpr(N.getOperand(1));
1952 } else {
1953 Tmp1 = SelectExpr(N.getOperand(1));
1954 Select(N.getOperand(0));
1955 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001956 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
1957 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1958 }
Chris Lattner11333092005-01-11 03:11:44 +00001959
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001960 return;
1961 }
1962 case ISD::LOAD:
1963 case ISD::CALL:
1964 case ISD::DYNAMIC_STACKALLOC:
1965 SelectExpr(N);
1966 return;
1967 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001968 // Select the address.
1969 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001970
1971 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1972 Opc = 0;
1973 switch (CN->getValueType(0)) {
1974 default: assert(0 && "Invalid type for operation!");
1975 case MVT::i1:
1976 case MVT::i8: Opc = X86::MOV8mi; break;
1977 case MVT::i16: Opc = X86::MOV16mi; break;
1978 case MVT::i32: Opc = X86::MOV32mi; break;
1979 case MVT::f32:
1980 case MVT::f64: break;
1981 }
1982 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001983 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
1984 Select(N.getOperand(0));
1985 SelectAddress(N.getOperand(2), AM);
1986 } else {
1987 SelectAddress(N.getOperand(2), AM);
1988 Select(N.getOperand(0));
1989 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001990 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
1991 return;
1992 }
1993 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001994 switch (N.getOperand(1).getValueType()) {
1995 default: assert(0 && "Cannot store this type!");
1996 case MVT::i1:
1997 case MVT::i8: Opc = X86::MOV8mr; break;
1998 case MVT::i16: Opc = X86::MOV16mr; break;
1999 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002000 case MVT::f32: Opc = X86::FST32m; break;
2001 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002002 }
Chris Lattner11333092005-01-11 03:11:44 +00002003
2004 std::vector<std::pair<unsigned, unsigned> > RP;
2005 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2006 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2007 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2008 std::sort(RP.begin(), RP.end());
2009
2010 for (unsigned i = 0; i != 3; ++i)
2011 switch (RP[2-i].second) {
2012 default: assert(0 && "Unknown operand number!");
2013 case 0: Select(N.getOperand(0)); break;
2014 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002015 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002016 }
2017
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002018 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2019 return;
2020 }
2021 case ISD::ADJCALLSTACKDOWN:
2022 case ISD::ADJCALLSTACKUP:
2023 Select(N.getOperand(0));
2024 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2025
2026 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2027 X86::ADJCALLSTACKUP;
2028 BuildMI(BB, Opc, 1).addImm(Tmp1);
2029 return;
2030 }
2031 assert(0 && "Should not be reached!");
2032}
2033
2034
2035/// createX86PatternInstructionSelector - This pass converts an LLVM function
2036/// into a machine code representation using pattern matching and a machine
2037/// description file.
2038///
2039FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2040 return new ISel(TM);
2041}