blob: a70305a7e1c10f087b4da8fd6965c4a6fb9203bb [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();
52
53 setOperationUnsupported(ISD::MUL, MVT::i8);
54 setOperationUnsupported(ISD::SELECT, MVT::i1);
55 setOperationUnsupported(ISD::SELECT, MVT::i8);
56
57 addLegalFPImmediate(+0.0); // FLD0
58 addLegalFPImmediate(+1.0); // FLD1
59 addLegalFPImmediate(-0.0); // FLD0/FCHS
60 addLegalFPImmediate(-1.0); // FLD1/FCHS
61 }
62
63 /// LowerArguments - This hook must be implemented to indicate how we should
64 /// lower the arguments for the specified function, into the specified DAG.
65 virtual std::vector<SDOperand>
66 LowerArguments(Function &F, SelectionDAG &DAG);
67
68 /// LowerCallTo - This hook lowers an abstract call to a function into an
69 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000070 virtual std::pair<SDOperand, SDOperand>
71 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
72 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000073
74 virtual std::pair<SDOperand, SDOperand>
75 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
76
77 virtual std::pair<SDOperand,SDOperand>
78 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
79 const Type *ArgTy, SelectionDAG &DAG);
80
81 virtual std::pair<SDOperand, SDOperand>
82 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
83 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000084 };
85}
86
87
88std::vector<SDOperand>
89X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
90 std::vector<SDOperand> ArgValues;
91
92 // Add DAG nodes to load the arguments... On entry to a function on the X86,
93 // the stack frame looks like this:
94 //
95 // [ESP] -- return address
96 // [ESP + 4] -- first argument (leftmost lexically)
97 // [ESP + 8] -- second argument, if first argument is four bytes in size
98 // ...
99 //
100 MachineFunction &MF = DAG.getMachineFunction();
101 MachineFrameInfo *MFI = MF.getFrameInfo();
102
103 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
104 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
105 MVT::ValueType ObjectVT = getValueType(I->getType());
106 unsigned ArgIncrement = 4;
107 unsigned ObjSize;
108 switch (ObjectVT) {
109 default: assert(0 && "Unhandled argument type!");
110 case MVT::i1:
111 case MVT::i8: ObjSize = 1; break;
112 case MVT::i16: ObjSize = 2; break;
113 case MVT::i32: ObjSize = 4; break;
114 case MVT::i64: ObjSize = ArgIncrement = 8; break;
115 case MVT::f32: ObjSize = 4; break;
116 case MVT::f64: ObjSize = ArgIncrement = 8; break;
117 }
118 // Create the frame index object for this incoming parameter...
119 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
120
121 // Create the SelectionDAG nodes corresponding to a load from this parameter
122 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
123
124 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
125 // dead loads.
126 SDOperand ArgValue;
127 if (!I->use_empty())
128 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
129 else {
130 if (MVT::isInteger(ObjectVT))
131 ArgValue = DAG.getConstant(0, ObjectVT);
132 else
133 ArgValue = DAG.getConstantFP(0, ObjectVT);
134 }
135 ArgValues.push_back(ArgValue);
136
137 ArgOffset += ArgIncrement; // Move on to the next argument...
138 }
139
140 // If the function takes variable number of arguments, make a frame index for
141 // the start of the first vararg value... for expansion of llvm.va_start.
142 if (F.isVarArg())
143 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000144 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000145 return ArgValues;
146}
147
Chris Lattner5188ad72005-01-08 19:28:19 +0000148std::pair<SDOperand, SDOperand>
149X86TargetLowering::LowerCallTo(SDOperand Chain,
150 const Type *RetTy, SDOperand Callee,
151 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000152 // Count how many bytes are to be pushed on the stack.
153 unsigned NumBytes = 0;
154
155 if (Args.empty()) {
156 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000157 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
158 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000159 } else {
160 for (unsigned i = 0, e = Args.size(); i != e; ++i)
161 switch (getValueType(Args[i].second)) {
162 default: assert(0 && "Unknown value type!");
163 case MVT::i1:
164 case MVT::i8:
165 case MVT::i16:
166 case MVT::i32:
167 case MVT::f32:
168 NumBytes += 4;
169 break;
170 case MVT::i64:
171 case MVT::f64:
172 NumBytes += 8;
173 break;
174 }
175
Chris Lattner5188ad72005-01-08 19:28:19 +0000176 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
177 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000178
179 // Arguments go on the stack in reverse order, as specified by the ABI.
180 unsigned ArgOffset = 0;
181 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32);
182 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
183 unsigned ArgReg;
184 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
185 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
186
187 switch (getValueType(Args[i].second)) {
188 default: assert(0 && "Unexpected ValueType for argument!");
189 case MVT::i1:
190 case MVT::i8:
191 case MVT::i16:
192 // Promote the integer to 32 bits. If the input type is signed use a
193 // sign extend, otherwise use a zero extend.
194 if (Args[i].second->isSigned())
195 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
196 else
197 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
198
199 // FALL THROUGH
200 case MVT::i32:
201 case MVT::f32:
202 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000203 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
204 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000205 ArgOffset += 4;
206 break;
207 case MVT::i64:
208 case MVT::f64:
209 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000210 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
211 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000212 ArgOffset += 8;
213 break;
214 }
215 }
216 }
217
218 std::vector<MVT::ValueType> RetVals;
219 MVT::ValueType RetTyVT = getValueType(RetTy);
220 if (RetTyVT != MVT::isVoid)
221 RetVals.push_back(RetTyVT);
222 RetVals.push_back(MVT::Other);
223
Chris Lattner5188ad72005-01-08 19:28:19 +0000224 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000225 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000226 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
227 DAG.getConstant(NumBytes, getPointerTy()));
228 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000229}
230
Chris Lattner14824582005-01-09 00:01:27 +0000231std::pair<SDOperand, SDOperand>
232X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
233 // vastart just returns the address of the VarArgsFrameIndex slot.
234 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
235}
236
237std::pair<SDOperand,SDOperand> X86TargetLowering::
238LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
239 const Type *ArgTy, SelectionDAG &DAG) {
240 MVT::ValueType ArgVT = getValueType(ArgTy);
241 SDOperand Result;
242 if (!isVANext) {
243 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
244 } else {
245 unsigned Amt;
246 if (ArgVT == MVT::i32)
247 Amt = 4;
248 else {
249 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
250 "Other types should have been promoted for varargs!");
251 Amt = 8;
252 }
253 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
254 DAG.getConstant(Amt, VAList.getValueType()));
255 }
256 return std::make_pair(Result, Chain);
257}
258
259
260std::pair<SDOperand, SDOperand> X86TargetLowering::
261LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
262 SelectionDAG &DAG) {
263 SDOperand Result;
264 if (Depth) // Depths > 0 not supported yet!
265 Result = DAG.getConstant(0, getPointerTy());
266 else {
267 if (ReturnAddrIndex == 0) {
268 // Set up a frame object for the return address.
269 MachineFunction &MF = DAG.getMachineFunction();
270 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
271 }
272
273 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
274
275 if (!isFrameAddress)
276 // Just load the return address
277 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
278 else
279 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
280 DAG.getConstant(4, MVT::i32));
281 }
282 return std::make_pair(Result, Chain);
283}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000284
285
286
287
288
289namespace {
290 Statistic<>
291 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
292
293 //===--------------------------------------------------------------------===//
294 /// ISel - X86 specific code to select X86 machine instructions for
295 /// SelectionDAG operations.
296 ///
297 class ISel : public SelectionDAGISel {
298 /// ContainsFPCode - Every instruction we select that uses or defines a FP
299 /// register should set this to true.
300 bool ContainsFPCode;
301
302 /// X86Lowering - This object fully describes how to lower LLVM code to an
303 /// X86-specific SelectionDAG.
304 X86TargetLowering X86Lowering;
305
Chris Lattner11333092005-01-11 03:11:44 +0000306 /// RegPressureMap - This keeps an approximate count of the number of
307 /// registers required to evaluate each node in the graph.
308 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000309
310 /// ExprMap - As shared expressions are codegen'd, we keep track of which
311 /// vreg the value is produced in, so we only emit one copy of each compiled
312 /// tree.
313 std::map<SDOperand, unsigned> ExprMap;
314 std::set<SDOperand> LoweredTokens;
315
316 public:
317 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
318 }
319
Chris Lattner11333092005-01-11 03:11:44 +0000320 unsigned getRegPressure(SDOperand O) {
321 return RegPressureMap[O.Val];
322 }
323 unsigned ComputeRegPressure(SDOperand O);
324
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000325 /// InstructionSelectBasicBlock - This callback is invoked by
326 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
327 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
328 // While we're doing this, keep track of whether we see any FP code for
329 // FP_REG_KILL insertion.
330 ContainsFPCode = false;
331
Chris Lattner11333092005-01-11 03:11:44 +0000332 // Compute the RegPressureMap, which is an approximation for the number of
333 // registers required to compute each node.
334 ComputeRegPressure(DAG.getRoot());
335
336 //DAG.viewGraph();
337
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000338 // Codegen the basic block.
339 Select(DAG.getRoot());
340
341 // Insert FP_REG_KILL instructions into basic blocks that need them. This
342 // only occurs due to the floating point stackifier not being aggressive
343 // enough to handle arbitrary global stackification.
344 //
345 // Currently we insert an FP_REG_KILL instruction into each block that
346 // uses or defines a floating point virtual register.
347 //
348 // When the global register allocators (like linear scan) finally update
349 // live variable analysis, we can keep floating point values in registers
350 // across basic blocks. This will be a huge win, but we are waiting on
351 // the global allocators before we can do this.
352 //
353 if (ContainsFPCode && BB->succ_size()) {
354 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
355 ++NumFPKill;
356 }
357
358 // Clear state used for selection.
359 ExprMap.clear();
360 LoweredTokens.clear();
Chris Lattner11333092005-01-11 03:11:44 +0000361 RegPressureMap.clear();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000362 }
363
364 void EmitCMP(SDOperand LHS, SDOperand RHS);
365 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000366 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
367 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000368 unsigned SelectExpr(SDOperand N);
369 bool SelectAddress(SDOperand N, X86AddressMode &AM);
370 void Select(SDOperand N);
371 };
372}
373
Chris Lattner11333092005-01-11 03:11:44 +0000374// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
375// for the number of registers required to compute each node. This is basically
376// computing a generalized form of the Sethi-Ullman number for each node.
377unsigned ISel::ComputeRegPressure(SDOperand O) {
378 SDNode *N = O.Val;
379 unsigned &Result = RegPressureMap[N];
380 if (Result) return Result;
381
382 if (N->getNumOperands() == 0)
383 return Result = 1;
384
385 unsigned MaxRegUse = 0;
386 unsigned NumExtraMaxRegUsers = 0;
387 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
388 unsigned Regs = ComputeRegPressure(N->getOperand(i));
389 if (Regs > MaxRegUse) {
390 MaxRegUse = Regs;
391 NumExtraMaxRegUsers = 0;
392 } else if (Regs == MaxRegUse) {
393 ++NumExtraMaxRegUsers;
394 }
395 }
396
397 return Result = MaxRegUse+NumExtraMaxRegUsers;
398}
399
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000400/// SelectAddress - Add the specified node to the specified addressing mode,
401/// returning true if it cannot be done.
402bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
403 switch (N.getOpcode()) {
404 default: break;
405 case ISD::FrameIndex:
406 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
407 AM.BaseType = X86AddressMode::FrameIndexBase;
408 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
409 return false;
410 }
411 break;
412 case ISD::GlobalAddress:
413 if (AM.GV == 0) {
414 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
415 return false;
416 }
417 break;
418 case ISD::Constant:
419 AM.Disp += cast<ConstantSDNode>(N)->getValue();
420 return false;
421 case ISD::SHL:
422 if (AM.IndexReg == 0 || AM.Scale == 1)
423 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
424 unsigned Val = CN->getValue();
425 if (Val == 1 || Val == 2 || Val == 3) {
426 AM.Scale = 1 << Val;
427 AM.IndexReg = SelectExpr(N.Val->getOperand(0));
428 return false;
429 }
430 }
431 break;
432
433 case ISD::ADD: {
434 X86AddressMode Backup = AM;
435 if (!SelectAddress(N.Val->getOperand(0), AM) &&
436 !SelectAddress(N.Val->getOperand(1), AM))
437 return false;
438 AM = Backup;
439 break;
440 }
441 }
442
443 if (AM.BaseType != X86AddressMode::RegBase ||
444 AM.Base.Reg)
445 return true;
446
447 // Default, generate it as a register.
448 AM.BaseType = X86AddressMode::RegBase;
449 AM.Base.Reg = SelectExpr(N);
450 return false;
451}
452
453/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
454/// assuming that the temporary registers are in the 8-bit register class.
455///
456/// Tmp1 = setcc1
457/// Tmp2 = setcc2
458/// DestReg = logicalop Tmp1, Tmp2
459///
460static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
461 unsigned SetCC2, unsigned LogicalOp,
462 unsigned DestReg) {
463 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
464 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
465 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
466 BuildMI(BB, SetCC1, 0, Tmp1);
467 BuildMI(BB, SetCC2, 0, Tmp2);
468 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
469}
470
471/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
472/// condition codes match the specified SetCCOpcode. Note that some conditions
473/// require multiple instructions to generate the correct value.
474static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
475 ISD::CondCode SetCCOpcode, bool isFP) {
476 unsigned Opc;
477 if (!isFP) {
478 switch (SetCCOpcode) {
479 default: assert(0 && "Illegal integer SetCC!");
480 case ISD::SETEQ: Opc = X86::SETEr; break;
481 case ISD::SETGT: Opc = X86::SETGr; break;
482 case ISD::SETGE: Opc = X86::SETGEr; break;
483 case ISD::SETLT: Opc = X86::SETLr; break;
484 case ISD::SETLE: Opc = X86::SETLEr; break;
485 case ISD::SETNE: Opc = X86::SETNEr; break;
486 case ISD::SETULT: Opc = X86::SETBr; break;
487 case ISD::SETUGT: Opc = X86::SETAr; break;
488 case ISD::SETULE: Opc = X86::SETBEr; break;
489 case ISD::SETUGE: Opc = X86::SETAEr; break;
490 }
491 } else {
492 // On a floating point condition, the flags are set as follows:
493 // ZF PF CF op
494 // 0 | 0 | 0 | X > Y
495 // 0 | 0 | 1 | X < Y
496 // 1 | 0 | 0 | X == Y
497 // 1 | 1 | 1 | unordered
498 //
499 switch (SetCCOpcode) {
500 default: assert(0 && "Invalid FP setcc!");
501 case ISD::SETUEQ:
502 case ISD::SETEQ:
503 Opc = X86::SETEr; // True if ZF = 1
504 break;
505 case ISD::SETOGT:
506 case ISD::SETGT:
507 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
508 break;
509 case ISD::SETOGE:
510 case ISD::SETGE:
511 Opc = X86::SETAEr; // True if CF = 0
512 break;
513 case ISD::SETULT:
514 case ISD::SETLT:
515 Opc = X86::SETBr; // True if CF = 1
516 break;
517 case ISD::SETULE:
518 case ISD::SETLE:
519 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
520 break;
521 case ISD::SETONE:
522 case ISD::SETNE:
523 Opc = X86::SETNEr; // True if ZF = 0
524 break;
525 case ISD::SETUO:
526 Opc = X86::SETPr; // True if PF = 1
527 break;
528 case ISD::SETO:
529 Opc = X86::SETNPr; // True if PF = 0
530 break;
531 case ISD::SETOEQ: // !PF & ZF
532 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
533 return;
534 case ISD::SETOLT: // !PF & CF
535 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
536 return;
537 case ISD::SETOLE: // !PF & (CF || ZF)
538 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
539 return;
540 case ISD::SETUGT: // PF | (!ZF & !CF)
541 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
542 return;
543 case ISD::SETUGE: // PF | !CF
544 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
545 return;
546 case ISD::SETUNE: // PF | !ZF
547 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
548 return;
549 }
550 }
551 BuildMI(BB, Opc, 0, DestReg);
552}
553
554
555/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
556/// the Dest block if the Cond condition is true. If we cannot fold this
557/// condition into the branch, return true.
558///
559bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Cond) {
560 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
561 // B) using two conditional branches instead of one condbr, two setcc's, and
562 // an or.
563 if ((Cond.getOpcode() == ISD::OR ||
564 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
565 // And and or set the flags for us, so there is no need to emit a TST of the
566 // result. It is only safe to do this if there is only a single use of the
567 // AND/OR though, otherwise we don't know it will be emitted here.
568 SelectExpr(Cond);
569 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
570 return false;
571 }
572
573 // Codegen br not C -> JE.
574 if (Cond.getOpcode() == ISD::XOR)
575 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
576 if (NC->isAllOnesValue()) {
577 unsigned CondR = SelectExpr(Cond.Val->getOperand(0));
578 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
579 BuildMI(BB, X86::JE, 1).addMBB(Dest);
580 return false;
581 }
582
583 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
584 if (SetCC == 0)
585 return true; // Can only handle simple setcc's so far.
586
587 unsigned Opc;
588
589 // Handle integer conditions first.
590 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
591 switch (SetCC->getCondition()) {
592 default: assert(0 && "Illegal integer SetCC!");
593 case ISD::SETEQ: Opc = X86::JE; break;
594 case ISD::SETGT: Opc = X86::JG; break;
595 case ISD::SETGE: Opc = X86::JGE; break;
596 case ISD::SETLT: Opc = X86::JL; break;
597 case ISD::SETLE: Opc = X86::JLE; break;
598 case ISD::SETNE: Opc = X86::JNE; break;
599 case ISD::SETULT: Opc = X86::JB; break;
600 case ISD::SETUGT: Opc = X86::JA; break;
601 case ISD::SETULE: Opc = X86::JBE; break;
602 case ISD::SETUGE: Opc = X86::JAE; break;
603 }
604 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
605 BuildMI(BB, Opc, 1).addMBB(Dest);
606 return false;
607 }
608
609 ContainsFPCode = true;
610 unsigned Opc2 = 0; // Second branch if needed.
611
612 // On a floating point condition, the flags are set as follows:
613 // ZF PF CF op
614 // 0 | 0 | 0 | X > Y
615 // 0 | 0 | 1 | X < Y
616 // 1 | 0 | 0 | X == Y
617 // 1 | 1 | 1 | unordered
618 //
619 switch (SetCC->getCondition()) {
620 default: assert(0 && "Invalid FP setcc!");
621 case ISD::SETUEQ:
622 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
623 case ISD::SETOGT:
624 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
625 case ISD::SETOGE:
626 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
627 case ISD::SETULT:
628 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
629 case ISD::SETULE:
630 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
631 case ISD::SETONE:
632 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
633 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
634 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
635 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
636 Opc = X86::JA; // ZF = 0 & CF = 0
637 Opc2 = X86::JP; // PF = 1
638 break;
639 case ISD::SETUGE: // PF = 1 | CF = 0
640 Opc = X86::JAE; // CF = 0
641 Opc2 = X86::JP; // PF = 1
642 break;
643 case ISD::SETUNE: // PF = 1 | ZF = 0
644 Opc = X86::JNE; // ZF = 0
645 Opc2 = X86::JP; // PF = 1
646 break;
647 case ISD::SETOEQ: // PF = 0 & ZF = 1
648 //X86::JNP, X86::JE
649 //X86::AND8rr
650 return true; // FIXME: Emit more efficient code for this branch.
651 case ISD::SETOLT: // PF = 0 & CF = 1
652 //X86::JNP, X86::JB
653 //X86::AND8rr
654 return true; // FIXME: Emit more efficient code for this branch.
655 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
656 //X86::JNP, X86::JBE
657 //X86::AND8rr
658 return true; // FIXME: Emit more efficient code for this branch.
659 }
660
661 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
662 BuildMI(BB, Opc, 1).addMBB(Dest);
663 if (Opc2)
664 BuildMI(BB, Opc2, 1).addMBB(Dest);
665 return false;
666}
667
Chris Lattner24aad1b2005-01-10 22:10:13 +0000668/// EmitSelectCC - Emit code into BB that performs a select operation between
669/// the two registers RTrue and RFalse, generating a result into RDest. Return
670/// true if the fold cannot be performed.
671///
672void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
673 unsigned RTrue, unsigned RFalse, unsigned RDest) {
674 enum Condition {
675 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
676 NOT_SET
677 } CondCode = NOT_SET;
678
679 static const unsigned CMOVTAB16[] = {
680 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
681 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
682 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
683 };
684 static const unsigned CMOVTAB32[] = {
685 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
686 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
687 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
688 };
689 static const unsigned CMOVTABFP[] = {
690 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
691 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
692 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
693 };
694
695 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
696 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
697 switch (SetCC->getCondition()) {
698 default: assert(0 && "Unknown integer comparison!");
699 case ISD::SETEQ: CondCode = EQ; break;
700 case ISD::SETGT: CondCode = GT; break;
701 case ISD::SETGE: CondCode = GE; break;
702 case ISD::SETLT: CondCode = LT; break;
703 case ISD::SETLE: CondCode = LE; break;
704 case ISD::SETNE: CondCode = NE; break;
705 case ISD::SETULT: CondCode = B; break;
706 case ISD::SETUGT: CondCode = A; break;
707 case ISD::SETULE: CondCode = BE; break;
708 case ISD::SETUGE: CondCode = AE; break;
709 }
710 } else {
711 // On a floating point condition, the flags are set as follows:
712 // ZF PF CF op
713 // 0 | 0 | 0 | X > Y
714 // 0 | 0 | 1 | X < Y
715 // 1 | 0 | 0 | X == Y
716 // 1 | 1 | 1 | unordered
717 //
718 switch (SetCC->getCondition()) {
719 default: assert(0 && "Unknown FP comparison!");
720 case ISD::SETUEQ:
721 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
722 case ISD::SETOGT:
723 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
724 case ISD::SETOGE:
725 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
726 case ISD::SETULT:
727 case ISD::SETLT: CondCode = B; break; // True if CF = 1
728 case ISD::SETULE:
729 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
730 case ISD::SETONE:
731 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
732 case ISD::SETUO: CondCode = P; break; // True if PF = 1
733 case ISD::SETO: CondCode = NP; break; // True if PF = 0
734 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
735 case ISD::SETUGE: // PF = 1 | CF = 0
736 case ISD::SETUNE: // PF = 1 | ZF = 0
737 case ISD::SETOEQ: // PF = 0 & ZF = 1
738 case ISD::SETOLT: // PF = 0 & CF = 1
739 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
740 // We cannot emit this comparison as a single cmov.
741 break;
742 }
743 }
744 }
745
746 unsigned Opc = 0;
747 if (CondCode != NOT_SET) {
748 switch (SVT) {
749 default: assert(0 && "Cannot select this type!");
750 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
751 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
752 case MVT::f32:
753 case MVT::f64: Opc = CMOVTABFP[CondCode]; ContainsFPCode = true; break;
754 }
755 }
756
757 // Finally, if we weren't able to fold this, just emit the condition and test
758 // it.
759 if (CondCode == NOT_SET || Opc == 0) {
760 // Get the condition into the zero flag.
761 unsigned CondReg = SelectExpr(Cond);
762 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
763
764 switch (SVT) {
765 default: assert(0 && "Cannot select this type!");
766 case MVT::i16: Opc = X86::CMOVE16rr; break;
767 case MVT::i32: Opc = X86::CMOVE32rr; break;
768 case MVT::f32:
769 case MVT::f64: Opc = X86::FCMOVE; ContainsFPCode = true; break;
770 }
771 } else {
772 // FIXME: CMP R, 0 -> TEST R, R
773 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
774 }
775 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
776}
777
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000778void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000779 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000780 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
781 Opc = 0;
782 switch (RHS.getValueType()) {
783 default: break;
784 case MVT::i1:
785 case MVT::i8: Opc = X86::CMP8ri; break;
786 case MVT::i16: Opc = X86::CMP16ri; break;
787 case MVT::i32: Opc = X86::CMP32ri; break;
788 }
789 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000790 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000791 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
792 return;
793 }
794 }
795
796 switch (LHS.getValueType()) {
797 default: assert(0 && "Cannot compare this value!");
798 case MVT::i1:
799 case MVT::i8: Opc = X86::CMP8rr; break;
800 case MVT::i16: Opc = X86::CMP16rr; break;
801 case MVT::i32: Opc = X86::CMP32rr; break;
802 case MVT::f32:
803 case MVT::f64: Opc = X86::FUCOMIr; ContainsFPCode = true; break;
804 }
Chris Lattner11333092005-01-11 03:11:44 +0000805 unsigned Tmp1, Tmp2;
806 if (getRegPressure(LHS) > getRegPressure(RHS)) {
807 Tmp1 = SelectExpr(LHS);
808 Tmp2 = SelectExpr(RHS);
809 } else {
810 Tmp2 = SelectExpr(RHS);
811 Tmp1 = SelectExpr(LHS);
812 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000813 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
814}
815
816unsigned ISel::SelectExpr(SDOperand N) {
817 unsigned Result;
818 unsigned Tmp1, Tmp2, Tmp3;
819 unsigned Opc = 0;
820
Chris Lattner5188ad72005-01-08 19:28:19 +0000821 SDNode *Node = N.Val;
822
Chris Lattner590d8002005-01-09 18:52:44 +0000823 if (Node->getOpcode() == ISD::CopyFromReg)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000824 // Just use the specified register as our input.
Chris Lattner590d8002005-01-09 18:52:44 +0000825 return dyn_cast<CopyRegSDNode>(Node)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000826
827 // If there are multiple uses of this expression, memorize the
828 // register it is code generated in, instead of emitting it multiple
829 // times.
830 // FIXME: Disabled for our current selection model.
Chris Lattner5188ad72005-01-08 19:28:19 +0000831 if (1 || !Node->hasOneUse()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000832 unsigned &Reg = ExprMap[N];
833 if (Reg) return Reg;
834
835 if (N.getOpcode() != ISD::CALL)
836 Reg = Result = (N.getValueType() != MVT::Other) ?
837 MakeReg(N.getValueType()) : 1;
838 else {
839 // If this is a call instruction, make sure to prepare ALL of the result
840 // values as well as the chain.
Chris Lattner5188ad72005-01-08 19:28:19 +0000841 if (Node->getNumValues() == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000842 Reg = Result = 1; // Void call, just a chain.
843 else {
Chris Lattner5188ad72005-01-08 19:28:19 +0000844 Result = MakeReg(Node->getValueType(0));
845 ExprMap[N.getValue(0)] = Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000846 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattner5188ad72005-01-08 19:28:19 +0000847 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
848 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000849 }
850 }
851 } else {
852 Result = MakeReg(N.getValueType());
853 }
854
855 switch (N.getOpcode()) {
856 default:
Chris Lattner5188ad72005-01-08 19:28:19 +0000857 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000858 assert(0 && "Node not handled!\n");
859 case ISD::FrameIndex:
860 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
861 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
862 return Result;
863 case ISD::ConstantPool:
864 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
865 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
866 return Result;
867 case ISD::ConstantFP:
868 ContainsFPCode = true;
869 Tmp1 = Result; // Intermediate Register
870 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
871 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
872 Tmp1 = MakeReg(MVT::f64);
873
874 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
875 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
876 BuildMI(BB, X86::FLD0, 0, Tmp1);
877 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
878 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
879 BuildMI(BB, X86::FLD1, 0, Tmp1);
880 else
881 assert(0 && "Unexpected constant!");
882 if (Tmp1 != Result)
883 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
884 return Result;
885 case ISD::Constant:
886 switch (N.getValueType()) {
887 default: assert(0 && "Cannot use constants of this type!");
888 case MVT::i1:
889 case MVT::i8: Opc = X86::MOV8ri; break;
890 case MVT::i16: Opc = X86::MOV16ri; break;
891 case MVT::i32: Opc = X86::MOV32ri; break;
892 }
893 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
894 return Result;
895 case ISD::GlobalAddress: {
896 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
897 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
898 return Result;
899 }
900 case ISD::ExternalSymbol: {
901 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
902 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
903 return Result;
904 }
905 case ISD::FP_EXTEND:
906 Tmp1 = SelectExpr(N.getOperand(0));
907 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
908 ContainsFPCode = true;
909 return Result;
910 case ISD::ZERO_EXTEND: {
911 int DestIs16 = N.getValueType() == MVT::i16;
912 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +0000913 Tmp1 = SelectExpr(N.getOperand(0));
914
915 // FIXME: This hack is here for zero extension casts from bool to i8. This
916 // would not be needed if bools were promoted by Legalize.
917 if (N.getValueType() == MVT::i8) {
918 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
919 return Result;
920 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000921
922 static const unsigned Opc[3] = {
923 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
924 };
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000925 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
926 return Result;
927 }
928 case ISD::SIGN_EXTEND: {
929 int DestIs16 = N.getValueType() == MVT::i16;
930 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
931
Chris Lattner590d8002005-01-09 18:52:44 +0000932 // FIXME: Legalize should promote bools to i8!
933 assert(N.getOperand(0).getValueType() != MVT::i1 &&
934 "Sign extend from bool not implemented!");
935
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000936 static const unsigned Opc[3] = {
937 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
938 };
939 Tmp1 = SelectExpr(N.getOperand(0));
940 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
941 return Result;
942 }
943 case ISD::TRUNCATE:
944 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
945 // a move out of AX or AL.
946 switch (N.getOperand(0).getValueType()) {
947 default: assert(0 && "Unknown truncate!");
948 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
949 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
950 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
951 }
952 Tmp1 = SelectExpr(N.getOperand(0));
953 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
954
955 switch (N.getValueType()) {
956 default: assert(0 && "Unknown truncate!");
957 case MVT::i1:
958 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
959 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
960 }
961 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
962 return Result;
963
964 case ISD::FP_ROUND:
965 // Truncate from double to float by storing to memory as float,
966 // then reading it back into a register.
967
968 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +0000969 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000970 Tmp1 = TLI.getTargetData().getFloatAlignment();
971 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
972
973 // Codegen the input.
974 Tmp1 = SelectExpr(N.getOperand(0));
975
976 // Emit the store, then the reload.
977 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
978 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
979 ContainsFPCode = true;
980 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +0000981
982 case ISD::SINT_TO_FP:
983 case ISD::UINT_TO_FP: {
984 // FIXME: Most of this grunt work should be done by legalize!
985
986 // Promote the integer to a type supported by FLD. We do this because there
987 // are no unsigned FLD instructions, so we must promote an unsigned value to
988 // a larger signed value, then use FLD on the larger value.
989 //
990 MVT::ValueType PromoteType = MVT::Other;
991 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
992 unsigned PromoteOpcode = 0;
993 unsigned RealDestReg = Result;
994 switch (SrcTy) {
995 case MVT::i1:
996 case MVT::i8:
997 // We don't have the facilities for directly loading byte sized data from
998 // memory (even signed). Promote it to 16 bits.
999 PromoteType = MVT::i16;
1000 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1001 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1002 break;
1003 case MVT::i16:
1004 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1005 PromoteType = MVT::i32;
1006 PromoteOpcode = X86::MOVZX32rr16;
1007 }
1008 break;
1009 default:
1010 // Don't fild into the real destination.
1011 if (Node->getOpcode() == ISD::UINT_TO_FP)
1012 Result = MakeReg(Node->getValueType(0));
1013 break;
1014 }
1015
1016 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1017
1018 if (PromoteType != MVT::Other) {
1019 Tmp2 = MakeReg(PromoteType);
1020 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1021 SrcTy = PromoteType;
1022 Tmp1 = Tmp2;
1023 }
1024
1025 // Spill the integer to memory and reload it from there.
1026 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1027 MachineFunction *F = BB->getParent();
1028 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1029
1030 switch (SrcTy) {
1031 case MVT::i64:
1032 // FIXME: this won't work for cast [u]long to FP
1033 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1034 FrameIdx).addReg(Tmp1);
1035 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1036 FrameIdx, 4).addReg(Tmp1+1);
1037 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1038 break;
1039 case MVT::i32:
1040 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1041 FrameIdx).addReg(Tmp1);
1042 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1043 break;
1044 case MVT::i16:
1045 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1046 FrameIdx).addReg(Tmp1);
1047 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1048 break;
1049 default: break; // No promotion required.
1050 }
1051
1052 if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i32) {
1053 // If this is a cast from uint -> double, we need to be careful when if
1054 // the "sign" bit is set. If so, we don't want to make a negative number,
1055 // we want to make a positive number. Emit code to add an offset if the
1056 // sign bit is set.
1057
1058 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1059 unsigned IsNeg = MakeReg(MVT::i32);
1060 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1061
1062 // Create a CP value that has the offset in one word and 0 in the other.
1063 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1064 0x4f80000000000000ULL);
1065 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1066 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1067 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1068
1069 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1070 // We need special handling for unsigned 64-bit integer sources. If the
1071 // input number has the "sign bit" set, then we loaded it incorrectly as a
1072 // negative 64-bit number. In this case, add an offset value.
1073
1074 // Emit a test instruction to see if the dynamic input value was signed.
1075 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1076
1077 // If the sign bit is set, get a pointer to an offset, otherwise get a
1078 // pointer to a zero.
1079 MachineConstantPool *CP = F->getConstantPool();
1080 unsigned Zero = MakeReg(MVT::i32);
1081 Constant *Null = Constant::getNullValue(Type::UIntTy);
1082 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1083 CP->getConstantPoolIndex(Null));
1084 unsigned Offset = MakeReg(MVT::i32);
1085 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1086
1087 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1088 CP->getConstantPoolIndex(OffsetCst));
1089 unsigned Addr = MakeReg(MVT::i32);
1090 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1091
1092 // Load the constant for an add. FIXME: this could make an 'fadd' that
1093 // reads directly from memory, but we don't support these yet.
1094 unsigned ConstReg = MakeReg(MVT::f64);
1095 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1096
1097 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1098 }
1099 return RealDestReg;
1100 }
1101 case ISD::FP_TO_SINT:
1102 case ISD::FP_TO_UINT: {
1103 // FIXME: Most of this grunt work should be done by legalize!
1104 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1105
1106 // Change the floating point control register to use "round towards zero"
1107 // mode when truncating to an integer value.
1108 //
1109 MachineFunction *F = BB->getParent();
1110 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1111 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1112
1113 // Load the old value of the high byte of the control word...
1114 unsigned HighPartOfCW = MakeReg(MVT::i8);
1115 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1116 CWFrameIdx, 1);
1117
1118 // Set the high part to be round to zero...
1119 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1120 CWFrameIdx, 1).addImm(12);
1121
1122 // Reload the modified control word now...
1123 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1124
1125 // Restore the memory image of control word to original value
1126 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1127 CWFrameIdx, 1).addReg(HighPartOfCW);
1128
1129 // We don't have the facilities for directly storing byte sized data to
1130 // memory. Promote it to 16 bits. We also must promote unsigned values to
1131 // larger classes because we only have signed FP stores.
1132 MVT::ValueType StoreClass = Node->getValueType(0);
1133 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1134 switch (StoreClass) {
1135 case MVT::i8: StoreClass = MVT::i16; break;
1136 case MVT::i16: StoreClass = MVT::i32; break;
1137 case MVT::i32: StoreClass = MVT::i64; break;
1138 // The following treatment of cLong may not be perfectly right,
1139 // but it survives chains of casts of the form
1140 // double->ulong->double.
1141 case MVT::i64: StoreClass = MVT::i64; break;
1142 default: assert(0 && "Unknown store class!");
1143 }
1144
1145 // Spill the integer to memory and reload it from there.
1146 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1147 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1148
1149 switch (StoreClass) {
1150 default: assert(0 && "Unknown store class!");
1151 case MVT::i16:
1152 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1153 break;
1154 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001155 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001156 break;
1157 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001158 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001159 break;
1160 }
1161
1162 switch (Node->getValueType(0)) {
1163 default:
1164 assert(0 && "Unknown integer type!");
1165 case MVT::i64:
1166 // FIXME: this isn't gunna work.
1167 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1168 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1169 case MVT::i32:
1170 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1171 break;
1172 case MVT::i16:
1173 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1174 break;
1175 case MVT::i8:
1176 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1177 break;
1178 }
1179
1180 // Reload the original control word now.
1181 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1182 return Result;
1183 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001184 case ISD::ADD:
1185 // See if we can codegen this as an LEA to fold operations together.
1186 if (N.getValueType() == MVT::i32) {
1187 X86AddressMode AM;
1188 if (!SelectAddress(N.getOperand(0), AM) &&
1189 !SelectAddress(N.getOperand(1), AM)) {
1190 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001191 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001192 // leave this as LEA, then peephole it to 'ADD' after two address elim
1193 // happens.
1194 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001195 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001196 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1197 return Result;
1198 }
1199 }
1200 }
Chris Lattner11333092005-01-11 03:11:44 +00001201
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001202 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1203 Opc = 0;
1204 if (CN->getValue() == 1) { // add X, 1 -> inc X
1205 switch (N.getValueType()) {
1206 default: assert(0 && "Cannot integer add this type!");
1207 case MVT::i8: Opc = X86::INC8r; break;
1208 case MVT::i16: Opc = X86::INC16r; break;
1209 case MVT::i32: Opc = X86::INC32r; break;
1210 }
1211 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1212 switch (N.getValueType()) {
1213 default: assert(0 && "Cannot integer add this type!");
1214 case MVT::i8: Opc = X86::DEC8r; break;
1215 case MVT::i16: Opc = X86::DEC16r; break;
1216 case MVT::i32: Opc = X86::DEC32r; break;
1217 }
1218 }
1219
1220 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001221 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001222 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1223 return Result;
1224 }
1225
1226 switch (N.getValueType()) {
1227 default: assert(0 && "Cannot add this type!");
1228 case MVT::i8: Opc = X86::ADD8ri; break;
1229 case MVT::i16: Opc = X86::ADD16ri; break;
1230 case MVT::i32: Opc = X86::ADD32ri; break;
1231 }
1232 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001233 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001234 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1235 return Result;
1236 }
1237 }
1238
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001239 switch (N.getValueType()) {
1240 default: assert(0 && "Cannot add this type!");
1241 case MVT::i8: Opc = X86::ADD8rr; break;
1242 case MVT::i16: Opc = X86::ADD16rr; break;
1243 case MVT::i32: Opc = X86::ADD32rr; break;
1244 case MVT::f32:
1245 case MVT::f64: Opc = X86::FpADD; ContainsFPCode = true; break;
1246 }
Chris Lattner11333092005-01-11 03:11:44 +00001247
1248 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1249 Tmp1 = SelectExpr(N.getOperand(0));
1250 Tmp2 = SelectExpr(N.getOperand(1));
1251 } else {
1252 Tmp2 = SelectExpr(N.getOperand(1));
1253 Tmp1 = SelectExpr(N.getOperand(0));
1254 }
1255
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001256 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1257 return Result;
1258 case ISD::SUB:
1259 if (MVT::isInteger(N.getValueType()))
1260 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1261 if (CN->isNullValue()) { // 0 - N -> neg N
1262 switch (N.getValueType()) {
1263 default: assert(0 && "Cannot sub this type!");
1264 case MVT::i1:
1265 case MVT::i8: Opc = X86::NEG8r; break;
1266 case MVT::i16: Opc = X86::NEG16r; break;
1267 case MVT::i32: Opc = X86::NEG32r; break;
1268 }
1269 Tmp1 = SelectExpr(N.getOperand(1));
1270 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1271 return Result;
1272 }
1273
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001274 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1275 switch (N.getValueType()) {
1276 default: assert(0 && "Cannot sub this type!");
1277 case MVT::i1:
1278 case MVT::i8: Opc = X86::SUB8ri; break;
1279 case MVT::i16: Opc = X86::SUB16ri; break;
1280 case MVT::i32: Opc = X86::SUB32ri; break;
1281 }
Chris Lattner11333092005-01-11 03:11:44 +00001282 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001283 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1284 return Result;
1285 }
Chris Lattner11333092005-01-11 03:11:44 +00001286
1287 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1288 Tmp1 = SelectExpr(N.getOperand(0));
1289 Tmp2 = SelectExpr(N.getOperand(1));
1290 } else {
1291 Tmp2 = SelectExpr(N.getOperand(1));
1292 Tmp1 = SelectExpr(N.getOperand(0));
1293 }
1294
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001295 switch (N.getValueType()) {
1296 default: assert(0 && "Cannot add this type!");
1297 case MVT::i1:
1298 case MVT::i8: Opc = X86::SUB8rr; break;
1299 case MVT::i16: Opc = X86::SUB16rr; break;
1300 case MVT::i32: Opc = X86::SUB32rr; break;
1301 case MVT::f32:
1302 case MVT::f64: Opc = X86::FpSUB; ContainsFPCode = true; break;
1303 }
1304 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1305 return Result;
1306
1307 case ISD::AND:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001308 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1309 switch (N.getValueType()) {
1310 default: assert(0 && "Cannot add this type!");
1311 case MVT::i1:
1312 case MVT::i8: Opc = X86::AND8ri; break;
1313 case MVT::i16: Opc = X86::AND16ri; break;
1314 case MVT::i32: Opc = X86::AND32ri; break;
1315 }
Chris Lattner11333092005-01-11 03:11:44 +00001316 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001317 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1318 return Result;
1319 }
Chris Lattner11333092005-01-11 03:11:44 +00001320
1321 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1322 Tmp1 = SelectExpr(N.getOperand(0));
1323 Tmp2 = SelectExpr(N.getOperand(1));
1324 } else {
1325 Tmp2 = SelectExpr(N.getOperand(1));
1326 Tmp1 = SelectExpr(N.getOperand(0));
1327 }
1328
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001329 switch (N.getValueType()) {
1330 default: assert(0 && "Cannot add this type!");
1331 case MVT::i1:
1332 case MVT::i8: Opc = X86::AND8rr; break;
1333 case MVT::i16: Opc = X86::AND16rr; break;
1334 case MVT::i32: Opc = X86::AND32rr; break;
1335 }
1336 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1337 return Result;
1338 case ISD::OR:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001339 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001340 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001341 switch (N.getValueType()) {
1342 default: assert(0 && "Cannot add this type!");
1343 case MVT::i1:
1344 case MVT::i8: Opc = X86::OR8ri; break;
1345 case MVT::i16: Opc = X86::OR16ri; break;
1346 case MVT::i32: Opc = X86::OR32ri; break;
1347 }
1348 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1349 return Result;
1350 }
Chris Lattner11333092005-01-11 03:11:44 +00001351
1352 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1353 Tmp1 = SelectExpr(N.getOperand(0));
1354 Tmp2 = SelectExpr(N.getOperand(1));
1355 } else {
1356 Tmp2 = SelectExpr(N.getOperand(1));
1357 Tmp1 = SelectExpr(N.getOperand(0));
1358 }
1359
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001360 switch (N.getValueType()) {
1361 default: assert(0 && "Cannot add this type!");
1362 case MVT::i1:
1363 case MVT::i8: Opc = X86::OR8rr; break;
1364 case MVT::i16: Opc = X86::OR16rr; break;
1365 case MVT::i32: Opc = X86::OR32rr; break;
1366 }
1367 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1368 return Result;
1369 case ISD::XOR:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001370 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001371 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001372 switch (N.getValueType()) {
1373 default: assert(0 && "Cannot add this type!");
1374 case MVT::i1:
1375 case MVT::i8: Opc = X86::XOR8ri; break;
1376 case MVT::i16: Opc = X86::XOR16ri; break;
1377 case MVT::i32: Opc = X86::XOR32ri; break;
1378 }
1379 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1380 return Result;
1381 }
Chris Lattner11333092005-01-11 03:11:44 +00001382
1383 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1384 Tmp1 = SelectExpr(N.getOperand(0));
1385 Tmp2 = SelectExpr(N.getOperand(1));
1386 } else {
1387 Tmp2 = SelectExpr(N.getOperand(1));
1388 Tmp1 = SelectExpr(N.getOperand(0));
1389 }
1390
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001391 switch (N.getValueType()) {
1392 default: assert(0 && "Cannot add this type!");
1393 case MVT::i1:
1394 case MVT::i8: Opc = X86::XOR8rr; break;
1395 case MVT::i16: Opc = X86::XOR16rr; break;
1396 case MVT::i32: Opc = X86::XOR32rr; break;
1397 }
1398 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1399 return Result;
1400
1401 case ISD::MUL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001402 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1403 Opc = 0;
1404 switch (N.getValueType()) {
1405 default: assert(0 && "Cannot multiply this type!");
1406 case MVT::i8: break;
1407 case MVT::i16: Opc = X86::IMUL16rri; break;
1408 case MVT::i32: Opc = X86::IMUL32rri; break;
1409 }
1410 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001411 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001412 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1413 return Result;
1414 }
1415 }
Chris Lattner11333092005-01-11 03:11:44 +00001416
1417 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1418 Tmp1 = SelectExpr(N.getOperand(0));
1419 Tmp2 = SelectExpr(N.getOperand(1));
1420 } else {
1421 Tmp2 = SelectExpr(N.getOperand(1));
1422 Tmp1 = SelectExpr(N.getOperand(0));
1423 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001424 switch (N.getValueType()) {
1425 default: assert(0 && "Cannot add this type!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001426 case MVT::i8:
1427 // Must use the MUL instruction, which forces use of AL.
1428 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1429 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1430 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1431 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001432 case MVT::i16: Opc = X86::IMUL16rr; break;
1433 case MVT::i32: Opc = X86::IMUL32rr; break;
1434 case MVT::f32:
1435 case MVT::f64: Opc = X86::FpMUL; ContainsFPCode = true; break;
1436 }
1437 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1438 return Result;
1439
1440 case ISD::SELECT:
1441 // FIXME: implement folding of setcc into select.
1442 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001443 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1444 Tmp2 = SelectExpr(N.getOperand(1));
1445 Tmp3 = SelectExpr(N.getOperand(2));
1446 } else {
1447 Tmp3 = SelectExpr(N.getOperand(2));
1448 Tmp2 = SelectExpr(N.getOperand(1));
1449 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001450 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001451 return Result;
1452 } else {
1453 // FIXME: This should not be implemented here, it should be in the generic
1454 // code!
1455 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1456 N.getOperand(1)));
1457 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1458 N.getOperand(2)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001459 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001460 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1461 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001462 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1463 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1464 return Result;
1465 }
1466
1467 case ISD::SDIV:
1468 case ISD::UDIV:
1469 case ISD::SREM:
1470 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001471 if (N.getOpcode() == ISD::SDIV)
1472 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1473 // FIXME: These special cases should be handled by the lowering impl!
1474 unsigned RHS = CN->getValue();
1475 bool isNeg = false;
1476 if ((int)RHS < 0) {
1477 isNeg = true;
1478 RHS = -RHS;
1479 }
1480 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1481 unsigned Log = log2(RHS);
1482 unsigned TmpReg = MakeReg(N.getValueType());
1483 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1484 switch (N.getValueType()) {
1485 default: assert("Unknown type to signed divide!");
1486 case MVT::i8:
1487 SAROpc = X86::SAR8ri;
1488 SHROpc = X86::SHR8ri;
1489 ADDOpc = X86::ADD8rr;
1490 NEGOpc = X86::NEG8r;
1491 break;
1492 case MVT::i16:
1493 SAROpc = X86::SAR16ri;
1494 SHROpc = X86::SHR16ri;
1495 ADDOpc = X86::ADD16rr;
1496 NEGOpc = X86::NEG16r;
1497 break;
1498 case MVT::i32:
1499 SAROpc = X86::SAR32ri;
1500 SHROpc = X86::SHR32ri;
1501 ADDOpc = X86::ADD32rr;
1502 NEGOpc = X86::NEG32r;
1503 break;
1504 }
Chris Lattner11333092005-01-11 03:11:44 +00001505 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001506 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1507 unsigned TmpReg2 = MakeReg(N.getValueType());
1508 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1509 unsigned TmpReg3 = MakeReg(N.getValueType());
1510 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1511
1512 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1513 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1514 if (isNeg)
1515 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1516 return Result;
1517 }
1518 }
1519
Chris Lattner11333092005-01-11 03:11:44 +00001520 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1521 Tmp1 = SelectExpr(N.getOperand(0));
1522 Tmp2 = SelectExpr(N.getOperand(1));
1523 } else {
1524 Tmp2 = SelectExpr(N.getOperand(1));
1525 Tmp1 = SelectExpr(N.getOperand(0));
1526 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001527
1528 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1529 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1530 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1531 switch (N.getValueType()) {
1532 default: assert(0 && "Cannot sdiv this type!");
1533 case MVT::i8:
1534 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1535 LoReg = X86::AL;
1536 HiReg = X86::AH;
1537 MovOpcode = X86::MOV8rr;
1538 ClrOpcode = X86::MOV8ri;
1539 SExtOpcode = X86::CBW;
1540 break;
1541 case MVT::i16:
1542 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1543 LoReg = X86::AX;
1544 HiReg = X86::DX;
1545 MovOpcode = X86::MOV16rr;
1546 ClrOpcode = X86::MOV16ri;
1547 SExtOpcode = X86::CWD;
1548 break;
1549 case MVT::i32:
1550 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
1551 LoReg =X86::EAX;
1552 HiReg = X86::EDX;
1553 MovOpcode = X86::MOV32rr;
1554 ClrOpcode = X86::MOV32ri;
1555 SExtOpcode = X86::CDQ;
1556 break;
1557 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1558 case MVT::f32:
1559 case MVT::f64:
1560 ContainsFPCode = true;
1561 if (N.getOpcode() == ISD::SDIV)
1562 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1563 else
1564 assert(0 && "FIXME: Emit frem libcall to fmod!");
1565 return Result;
1566 }
1567
1568 // Set up the low part.
1569 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1570
1571 if (isSigned) {
1572 // Sign extend the low part into the high part.
1573 BuildMI(BB, SExtOpcode, 0);
1574 } else {
1575 // Zero out the high part, effectively zero extending the input.
1576 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1577 }
1578
1579 // Emit the DIV/IDIV instruction.
1580 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1581
1582 // Get the result of the divide or rem.
1583 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1584 return Result;
1585 }
1586
1587 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001588 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1589 switch (N.getValueType()) {
1590 default: assert(0 && "Cannot shift this type!");
1591 case MVT::i8: Opc = X86::SHL8ri; break;
1592 case MVT::i16: Opc = X86::SHL16ri; break;
1593 case MVT::i32: Opc = X86::SHL32ri; break;
1594 }
Chris Lattner11333092005-01-11 03:11:44 +00001595 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001596 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1597 return Result;
1598 }
Chris Lattner11333092005-01-11 03:11:44 +00001599
1600 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1601 Tmp1 = SelectExpr(N.getOperand(0));
1602 Tmp2 = SelectExpr(N.getOperand(1));
1603 } else {
1604 Tmp2 = SelectExpr(N.getOperand(1));
1605 Tmp1 = SelectExpr(N.getOperand(0));
1606 }
1607
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001608 switch (N.getValueType()) {
1609 default: assert(0 && "Cannot shift this type!");
1610 case MVT::i8 : Opc = X86::SHL8rCL; break;
1611 case MVT::i16: Opc = X86::SHL16rCL; break;
1612 case MVT::i32: Opc = X86::SHL32rCL; break;
1613 }
1614 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1615 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1616 return Result;
1617 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001618 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1619 switch (N.getValueType()) {
1620 default: assert(0 && "Cannot shift this type!");
1621 case MVT::i8: Opc = X86::SHR8ri; break;
1622 case MVT::i16: Opc = X86::SHR16ri; break;
1623 case MVT::i32: Opc = X86::SHR32ri; break;
1624 }
Chris Lattner11333092005-01-11 03:11:44 +00001625 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001626 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1627 return Result;
1628 }
Chris Lattner11333092005-01-11 03:11:44 +00001629
1630 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1631 Tmp1 = SelectExpr(N.getOperand(0));
1632 Tmp2 = SelectExpr(N.getOperand(1));
1633 } else {
1634 Tmp2 = SelectExpr(N.getOperand(1));
1635 Tmp1 = SelectExpr(N.getOperand(0));
1636 }
1637
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001638 switch (N.getValueType()) {
1639 default: assert(0 && "Cannot shift this type!");
1640 case MVT::i8 : Opc = X86::SHR8rCL; break;
1641 case MVT::i16: Opc = X86::SHR16rCL; break;
1642 case MVT::i32: Opc = X86::SHR32rCL; break;
1643 }
1644 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1645 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1646 return Result;
1647 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001648 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1649 switch (N.getValueType()) {
1650 default: assert(0 && "Cannot shift this type!");
1651 case MVT::i8: Opc = X86::SAR8ri; break;
1652 case MVT::i16: Opc = X86::SAR16ri; break;
1653 case MVT::i32: Opc = X86::SAR32ri; break;
1654 }
Chris Lattner11333092005-01-11 03:11:44 +00001655 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001656 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1657 return Result;
1658 }
Chris Lattner11333092005-01-11 03:11:44 +00001659
1660 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1661 Tmp1 = SelectExpr(N.getOperand(0));
1662 Tmp2 = SelectExpr(N.getOperand(1));
1663 } else {
1664 Tmp2 = SelectExpr(N.getOperand(1));
1665 Tmp1 = SelectExpr(N.getOperand(0));
1666 }
1667
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001668 switch (N.getValueType()) {
1669 default: assert(0 && "Cannot shift this type!");
1670 case MVT::i8 : Opc = X86::SAR8rCL; break;
1671 case MVT::i16: Opc = X86::SAR16rCL; break;
1672 case MVT::i32: Opc = X86::SAR32rCL; break;
1673 }
1674 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1675 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1676 return Result;
1677
1678 case ISD::SETCC:
1679 if (MVT::isFloatingPoint(N.getOperand(0).getValueType()))
1680 ContainsFPCode = true;
1681 EmitCMP(N.getOperand(0), N.getOperand(1));
1682 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1683 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1684 return Result;
1685 case ISD::LOAD: {
Chris Lattner5188ad72005-01-08 19:28:19 +00001686 // The chain for this load is now lowered.
1687 LoweredTokens.insert(SDOperand(Node, 1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001688
1689 // Make sure we generate both values.
1690 if (Result != 1)
1691 ExprMap[N.getValue(1)] = 1; // Generate the token
1692 else
1693 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1694
Chris Lattner5188ad72005-01-08 19:28:19 +00001695 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001696 default: assert(0 && "Cannot load this type!");
1697 case MVT::i1:
1698 case MVT::i8: Opc = X86::MOV8rm; break;
1699 case MVT::i16: Opc = X86::MOV16rm; break;
1700 case MVT::i32: Opc = X86::MOV32rm; break;
1701 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1702 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1703 }
Chris Lattner11333092005-01-11 03:11:44 +00001704
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001705 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001706 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001707 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1708 } else {
1709 X86AddressMode AM;
Chris Lattner11333092005-01-11 03:11:44 +00001710 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1711 Select(N.getOperand(0));
1712 SelectAddress(N.getOperand(1), AM);
1713 } else {
1714 SelectAddress(N.getOperand(1), AM);
1715 Select(N.getOperand(0));
1716 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001717 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1718 }
1719 return Result;
1720 }
1721 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001722 // Generate both result values.
1723 if (Result != 1)
1724 ExprMap[N.getValue(1)] = 1; // Generate the token
1725 else
1726 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1727
1728 // FIXME: We are currently ignoring the requested alignment for handling
1729 // greater than the stack alignment. This will need to be revisited at some
1730 // point. Align = N.getOperand(2);
1731
1732 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1733 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1734 std::cerr << "Cannot allocate stack object with greater alignment than"
1735 << " the stack alignment yet!";
1736 abort();
1737 }
1738
1739 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001740 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001741 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1742 .addImm(CN->getValue());
1743 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001744 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1745 Select(N.getOperand(0));
1746 Tmp1 = SelectExpr(N.getOperand(1));
1747 } else {
1748 Tmp1 = SelectExpr(N.getOperand(1));
1749 Select(N.getOperand(0));
1750 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001751
1752 // Subtract size from stack pointer, thereby allocating some space.
1753 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
1754 }
1755
1756 // Put a pointer to the space into the result register, by copying the stack
1757 // pointer.
1758 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
1759 return Result;
1760
1761 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00001762 // The chain for this call is now lowered.
1763 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
1764
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001765 if (GlobalAddressSDNode *GASD =
1766 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001767 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001768 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
1769 } else if (ExternalSymbolSDNode *ESSDN =
1770 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001771 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001772 BuildMI(BB, X86::CALLpcrel32,
1773 1).addExternalSymbol(ESSDN->getSymbol(), true);
1774 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001775 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1776 Select(N.getOperand(0));
1777 Tmp1 = SelectExpr(N.getOperand(1));
1778 } else {
1779 Tmp1 = SelectExpr(N.getOperand(1));
1780 Select(N.getOperand(0));
1781 }
1782
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001783 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
1784 }
Chris Lattner5188ad72005-01-08 19:28:19 +00001785 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001786 default: assert(0 && "Unknown value type for call result!");
1787 case MVT::Other: return 1;
1788 case MVT::i1:
1789 case MVT::i8:
1790 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1791 break;
1792 case MVT::i16:
1793 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
1794 break;
1795 case MVT::i32:
1796 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00001797 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001798 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
1799 break;
1800 case MVT::f32:
1801 case MVT::f64: // Floating-point return values live in %ST(0)
1802 ContainsFPCode = true;
1803 BuildMI(BB, X86::FpGETRESULT, 1, Result);
1804 break;
1805 }
1806 return Result+N.ResNo;
1807 }
1808
1809 return 0;
1810}
1811
1812void ISel::Select(SDOperand N) {
1813 unsigned Tmp1, Tmp2, Opc;
1814
1815 // FIXME: Disable for our current expansion model!
1816 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
1817 return; // Already selected.
1818
1819 switch (N.getOpcode()) {
1820 default:
1821 N.Val->dump(); std::cerr << "\n";
1822 assert(0 && "Node not handled yet!");
1823 case ISD::EntryToken: return; // Noop
1824 case ISD::CopyToReg:
1825 Select(N.getOperand(0));
1826 Tmp1 = SelectExpr(N.getOperand(1));
1827 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
1828
1829 if (Tmp1 != Tmp2) {
1830 switch (N.getOperand(1).getValueType()) {
1831 default: assert(0 && "Invalid type for operation!");
1832 case MVT::i1:
1833 case MVT::i8: Opc = X86::MOV8rr; break;
1834 case MVT::i16: Opc = X86::MOV16rr; break;
1835 case MVT::i32: Opc = X86::MOV32rr; break;
1836 case MVT::f32:
1837 case MVT::f64: Opc = X86::FpMOV; break;
1838 }
1839 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1840 }
1841 return;
1842 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001843 switch (N.getNumOperands()) {
1844 default:
1845 assert(0 && "Unknown return instruction!");
1846 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001847 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1848 N.getOperand(2).getValueType() == MVT::i32 &&
1849 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00001850 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1851 Tmp1 = SelectExpr(N.getOperand(1));
1852 Tmp2 = SelectExpr(N.getOperand(2));
1853 } else {
1854 Tmp2 = SelectExpr(N.getOperand(2));
1855 Tmp1 = SelectExpr(N.getOperand(1));
1856 }
1857 Select(N.getOperand(0));
1858
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001859 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1860 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
1861 // Declare that EAX & EDX are live on exit.
1862 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1863 .addReg(X86::ESP);
1864 break;
1865 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00001866 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1867 Select(N.getOperand(0));
1868 Tmp1 = SelectExpr(N.getOperand(1));
1869 } else {
1870 Tmp1 = SelectExpr(N.getOperand(1));
1871 Select(N.getOperand(0));
1872 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001873 switch (N.getOperand(1).getValueType()) {
1874 default: assert(0 && "All other types should have been promoted!!");
1875 case MVT::f64:
1876 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
1877 // Declare that top-of-stack is live on exit
1878 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
1879 break;
1880 case MVT::i32:
1881 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1882 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
1883 break;
1884 }
1885 break;
1886 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00001887 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001888 break;
1889 }
1890 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1891 return;
1892 case ISD::BR: {
1893 Select(N.getOperand(0));
1894 MachineBasicBlock *Dest =
1895 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1896 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
1897 return;
1898 }
1899
1900 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001901 MachineBasicBlock *Dest =
1902 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00001903
1904 bool ChainFirst =
1905 getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1));
1906
1907 if (ChainFirst) Select(N.getOperand(0));
1908
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001909 // Try to fold a setcc into the branch. If this fails, emit a test/jne
1910 // pair.
1911 if (EmitBranchCC(Dest, N.getOperand(1))) {
1912 Tmp1 = SelectExpr(N.getOperand(1));
1913 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
1914 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1915 }
Chris Lattner11333092005-01-11 03:11:44 +00001916
1917 if (!ChainFirst) Select(N.getOperand(0));
1918
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001919 return;
1920 }
1921 case ISD::LOAD:
1922 case ISD::CALL:
1923 case ISD::DYNAMIC_STACKALLOC:
1924 SelectExpr(N);
1925 return;
1926 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001927 // Select the address.
1928 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001929
1930 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1931 Opc = 0;
1932 switch (CN->getValueType(0)) {
1933 default: assert(0 && "Invalid type for operation!");
1934 case MVT::i1:
1935 case MVT::i8: Opc = X86::MOV8mi; break;
1936 case MVT::i16: Opc = X86::MOV16mi; break;
1937 case MVT::i32: Opc = X86::MOV32mi; break;
1938 case MVT::f32:
1939 case MVT::f64: break;
1940 }
1941 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001942 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
1943 Select(N.getOperand(0));
1944 SelectAddress(N.getOperand(2), AM);
1945 } else {
1946 SelectAddress(N.getOperand(2), AM);
1947 Select(N.getOperand(0));
1948 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001949 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
1950 return;
1951 }
1952 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001953 switch (N.getOperand(1).getValueType()) {
1954 default: assert(0 && "Cannot store this type!");
1955 case MVT::i1:
1956 case MVT::i8: Opc = X86::MOV8mr; break;
1957 case MVT::i16: Opc = X86::MOV16mr; break;
1958 case MVT::i32: Opc = X86::MOV32mr; break;
1959 case MVT::f32: Opc = X86::FST32m; ContainsFPCode = true; break;
1960 case MVT::f64: Opc = X86::FST64m; ContainsFPCode = true; break;
1961 }
Chris Lattner11333092005-01-11 03:11:44 +00001962
1963 std::vector<std::pair<unsigned, unsigned> > RP;
1964 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
1965 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
1966 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
1967 std::sort(RP.begin(), RP.end());
1968
1969 for (unsigned i = 0; i != 3; ++i)
1970 switch (RP[2-i].second) {
1971 default: assert(0 && "Unknown operand number!");
1972 case 0: Select(N.getOperand(0)); break;
1973 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
1974 case 2: SelectAddress(N.getOperand(2), AM);
1975 }
1976
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001977 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
1978 return;
1979 }
1980 case ISD::ADJCALLSTACKDOWN:
1981 case ISD::ADJCALLSTACKUP:
1982 Select(N.getOperand(0));
1983 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1984
1985 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
1986 X86::ADJCALLSTACKUP;
1987 BuildMI(BB, Opc, 1).addImm(Tmp1);
1988 return;
1989 }
1990 assert(0 && "Should not be reached!");
1991}
1992
1993
1994/// createX86PatternInstructionSelector - This pass converts an LLVM function
1995/// into a machine code representation using pattern matching and a machine
1996/// description file.
1997///
1998FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
1999 return new ISel(TM);
2000}