1 //===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines a pattern matching instruction selector for X86.
12 //===----------------------------------------------------------------------===//
15 #include "X86InstrBuilder.h"
16 #include "X86RegisterInfo.h"
17 #include "llvm/Constants.h" // FIXME: REMOVE
18 #include "llvm/Function.h"
19 #include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
20 #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"
32 //===----------------------------------------------------------------------===//
33 // X86TargetLowering - X86 Implementation of the TargetLowering interface
35 class X86TargetLowering : public TargetLowering {
36 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
37 int ReturnAddrIndex; // FrameIndex for return slot.
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);
46 // FIXME: Eliminate these two classes when legalize can handle promotions
48 addRegisterClass(MVT::i1, X86::R8RegisterClass);
49 addRegisterClass(MVT::f32, X86::RFPRegisterClass);
51 computeRegisterProperties();
53 setOperationUnsupported(ISD::MUL, MVT::i8);
54 setOperationUnsupported(ISD::SELECT, MVT::i1);
55 setOperationUnsupported(ISD::SELECT, MVT::i8);
57 addLegalFPImmediate(+0.0); // FLD0
58 addLegalFPImmediate(+1.0); // FLD1
59 addLegalFPImmediate(-0.0); // FLD0/FCHS
60 addLegalFPImmediate(-1.0); // FLD1/FCHS
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);
68 /// LowerCallTo - This hook lowers an abstract call to a function into an
70 virtual std::pair<SDOperand, SDOperand>
71 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
72 ArgListTy &Args, SelectionDAG &DAG);
74 virtual std::pair<SDOperand, SDOperand>
75 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
77 virtual std::pair<SDOperand,SDOperand>
78 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
79 const Type *ArgTy, SelectionDAG &DAG);
81 virtual std::pair<SDOperand, SDOperand>
82 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
88 std::vector<SDOperand>
89 X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
90 std::vector<SDOperand> ArgValues;
92 // Add DAG nodes to load the arguments... On entry to a function on the X86,
93 // the stack frame looks like this:
95 // [ESP] -- return address
96 // [ESP + 4] -- first argument (leftmost lexically)
97 // [ESP + 8] -- second argument, if first argument is four bytes in size
100 MachineFunction &MF = DAG.getMachineFunction();
101 MachineFrameInfo *MFI = MF.getFrameInfo();
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;
109 default: assert(0 && "Unhandled argument type!");
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;
118 // Create the frame index object for this incoming parameter...
119 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
121 // Create the SelectionDAG nodes corresponding to a load from this parameter
122 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
124 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
128 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
130 if (MVT::isInteger(ObjectVT))
131 ArgValue = DAG.getConstant(0, ObjectVT);
133 ArgValue = DAG.getConstantFP(0, ObjectVT);
135 ArgValues.push_back(ArgValue);
137 ArgOffset += ArgIncrement; // Move on to the next argument...
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.
143 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
144 ReturnAddrIndex = 0; // No return address slot generated yet.
148 std::pair<SDOperand, SDOperand>
149 X86TargetLowering::LowerCallTo(SDOperand Chain,
150 const Type *RetTy, SDOperand Callee,
151 ArgListTy &Args, SelectionDAG &DAG) {
152 // Count how many bytes are to be pushed on the stack.
153 unsigned NumBytes = 0;
157 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
158 DAG.getConstant(0, getPointerTy()));
160 for (unsigned i = 0, e = Args.size(); i != e; ++i)
161 switch (getValueType(Args[i].second)) {
162 default: assert(0 && "Unknown value type!");
176 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
177 DAG.getConstant(NumBytes, getPointerTy()));
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) {
184 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
185 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
187 switch (getValueType(Args[i].second)) {
188 default: assert(0 && "Unexpected ValueType for argument!");
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);
197 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
202 // FIXME: Note that all of these stores are independent of each other.
203 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
204 Args[i].first, PtrOff);
209 // FIXME: Note that all of these stores are independent of each other.
210 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
211 Args[i].first, PtrOff);
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);
224 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
225 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
226 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
227 DAG.getConstant(NumBytes, getPointerTy()));
228 return std::make_pair(TheCall, Chain);
231 std::pair<SDOperand, SDOperand>
232 X86TargetLowering::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);
237 std::pair<SDOperand,SDOperand> X86TargetLowering::
238 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
239 const Type *ArgTy, SelectionDAG &DAG) {
240 MVT::ValueType ArgVT = getValueType(ArgTy);
243 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
246 if (ArgVT == MVT::i32)
249 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
250 "Other types should have been promoted for varargs!");
253 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
254 DAG.getConstant(Amt, VAList.getValueType()));
256 return std::make_pair(Result, Chain);
260 std::pair<SDOperand, SDOperand> X86TargetLowering::
261 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
264 if (Depth) // Depths > 0 not supported yet!
265 Result = DAG.getConstant(0, getPointerTy());
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);
273 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
276 // Just load the return address
277 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
279 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
280 DAG.getConstant(4, MVT::i32));
282 return std::make_pair(Result, Chain);
291 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
293 //===--------------------------------------------------------------------===//
294 /// ISel - X86 specific code to select X86 machine instructions for
295 /// SelectionDAG operations.
297 class ISel : public SelectionDAGISel {
298 /// ContainsFPCode - Every instruction we select that uses or defines a FP
299 /// register should set this to true.
302 /// X86Lowering - This object fully describes how to lower LLVM code to an
303 /// X86-specific SelectionDAG.
304 X86TargetLowering X86Lowering;
307 /// ExprMap - As shared expressions are codegen'd, we keep track of which
308 /// vreg the value is produced in, so we only emit one copy of each compiled
310 std::map<SDOperand, unsigned> ExprMap;
311 std::set<SDOperand> LoweredTokens;
314 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
317 /// InstructionSelectBasicBlock - This callback is invoked by
318 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
319 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
320 // While we're doing this, keep track of whether we see any FP code for
321 // FP_REG_KILL insertion.
322 ContainsFPCode = false;
324 // Codegen the basic block.
325 Select(DAG.getRoot());
327 // Insert FP_REG_KILL instructions into basic blocks that need them. This
328 // only occurs due to the floating point stackifier not being aggressive
329 // enough to handle arbitrary global stackification.
331 // Currently we insert an FP_REG_KILL instruction into each block that
332 // uses or defines a floating point virtual register.
334 // When the global register allocators (like linear scan) finally update
335 // live variable analysis, we can keep floating point values in registers
336 // across basic blocks. This will be a huge win, but we are waiting on
337 // the global allocators before we can do this.
339 if (ContainsFPCode && BB->succ_size()) {
340 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
344 // Clear state used for selection.
346 LoweredTokens.clear();
349 void EmitCMP(SDOperand LHS, SDOperand RHS);
350 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Cond);
351 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
352 unsigned RTrue, unsigned RFalse, unsigned RDest);
353 unsigned SelectExpr(SDOperand N);
354 bool SelectAddress(SDOperand N, X86AddressMode &AM);
355 void Select(SDOperand N);
359 /// SelectAddress - Add the specified node to the specified addressing mode,
360 /// returning true if it cannot be done.
361 bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
362 switch (N.getOpcode()) {
364 case ISD::FrameIndex:
365 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
366 AM.BaseType = X86AddressMode::FrameIndexBase;
367 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
371 case ISD::GlobalAddress:
373 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
378 AM.Disp += cast<ConstantSDNode>(N)->getValue();
381 if (AM.IndexReg == 0 || AM.Scale == 1)
382 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
383 unsigned Val = CN->getValue();
384 if (Val == 1 || Val == 2 || Val == 3) {
386 AM.IndexReg = SelectExpr(N.Val->getOperand(0));
393 X86AddressMode Backup = AM;
394 if (!SelectAddress(N.Val->getOperand(0), AM) &&
395 !SelectAddress(N.Val->getOperand(1), AM))
402 if (AM.BaseType != X86AddressMode::RegBase ||
406 // Default, generate it as a register.
407 AM.BaseType = X86AddressMode::RegBase;
408 AM.Base.Reg = SelectExpr(N);
412 /// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
413 /// assuming that the temporary registers are in the 8-bit register class.
417 /// DestReg = logicalop Tmp1, Tmp2
419 static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
420 unsigned SetCC2, unsigned LogicalOp,
422 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
423 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
424 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
425 BuildMI(BB, SetCC1, 0, Tmp1);
426 BuildMI(BB, SetCC2, 0, Tmp2);
427 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
430 /// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
431 /// condition codes match the specified SetCCOpcode. Note that some conditions
432 /// require multiple instructions to generate the correct value.
433 static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
434 ISD::CondCode SetCCOpcode, bool isFP) {
437 switch (SetCCOpcode) {
438 default: assert(0 && "Illegal integer SetCC!");
439 case ISD::SETEQ: Opc = X86::SETEr; break;
440 case ISD::SETGT: Opc = X86::SETGr; break;
441 case ISD::SETGE: Opc = X86::SETGEr; break;
442 case ISD::SETLT: Opc = X86::SETLr; break;
443 case ISD::SETLE: Opc = X86::SETLEr; break;
444 case ISD::SETNE: Opc = X86::SETNEr; break;
445 case ISD::SETULT: Opc = X86::SETBr; break;
446 case ISD::SETUGT: Opc = X86::SETAr; break;
447 case ISD::SETULE: Opc = X86::SETBEr; break;
448 case ISD::SETUGE: Opc = X86::SETAEr; break;
451 // On a floating point condition, the flags are set as follows:
455 // 1 | 0 | 0 | X == Y
456 // 1 | 1 | 1 | unordered
458 switch (SetCCOpcode) {
459 default: assert(0 && "Invalid FP setcc!");
462 Opc = X86::SETEr; // True if ZF = 1
466 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
470 Opc = X86::SETAEr; // True if CF = 0
474 Opc = X86::SETBr; // True if CF = 1
478 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
482 Opc = X86::SETNEr; // True if ZF = 0
485 Opc = X86::SETPr; // True if PF = 1
488 Opc = X86::SETNPr; // True if PF = 0
490 case ISD::SETOEQ: // !PF & ZF
491 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
493 case ISD::SETOLT: // !PF & CF
494 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
496 case ISD::SETOLE: // !PF & (CF || ZF)
497 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
499 case ISD::SETUGT: // PF | (!ZF & !CF)
500 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
502 case ISD::SETUGE: // PF | !CF
503 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
505 case ISD::SETUNE: // PF | !ZF
506 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
510 BuildMI(BB, Opc, 0, DestReg);
514 /// EmitBranchCC - Emit code into BB that arranges for control to transfer to
515 /// the Dest block if the Cond condition is true. If we cannot fold this
516 /// condition into the branch, return true.
518 bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Cond) {
519 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
520 // B) using two conditional branches instead of one condbr, two setcc's, and
522 if ((Cond.getOpcode() == ISD::OR ||
523 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
524 // And and or set the flags for us, so there is no need to emit a TST of the
525 // result. It is only safe to do this if there is only a single use of the
526 // AND/OR though, otherwise we don't know it will be emitted here.
528 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
532 // Codegen br not C -> JE.
533 if (Cond.getOpcode() == ISD::XOR)
534 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
535 if (NC->isAllOnesValue()) {
536 unsigned CondR = SelectExpr(Cond.Val->getOperand(0));
537 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
538 BuildMI(BB, X86::JE, 1).addMBB(Dest);
542 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
544 return true; // Can only handle simple setcc's so far.
548 // Handle integer conditions first.
549 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
550 switch (SetCC->getCondition()) {
551 default: assert(0 && "Illegal integer SetCC!");
552 case ISD::SETEQ: Opc = X86::JE; break;
553 case ISD::SETGT: Opc = X86::JG; break;
554 case ISD::SETGE: Opc = X86::JGE; break;
555 case ISD::SETLT: Opc = X86::JL; break;
556 case ISD::SETLE: Opc = X86::JLE; break;
557 case ISD::SETNE: Opc = X86::JNE; break;
558 case ISD::SETULT: Opc = X86::JB; break;
559 case ISD::SETUGT: Opc = X86::JA; break;
560 case ISD::SETULE: Opc = X86::JBE; break;
561 case ISD::SETUGE: Opc = X86::JAE; break;
563 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
564 BuildMI(BB, Opc, 1).addMBB(Dest);
568 ContainsFPCode = true;
569 unsigned Opc2 = 0; // Second branch if needed.
571 // On a floating point condition, the flags are set as follows:
575 // 1 | 0 | 0 | X == Y
576 // 1 | 1 | 1 | unordered
578 switch (SetCC->getCondition()) {
579 default: assert(0 && "Invalid FP setcc!");
581 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
583 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
585 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
587 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
589 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
591 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
592 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
593 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
594 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
595 Opc = X86::JA; // ZF = 0 & CF = 0
596 Opc2 = X86::JP; // PF = 1
598 case ISD::SETUGE: // PF = 1 | CF = 0
599 Opc = X86::JAE; // CF = 0
600 Opc2 = X86::JP; // PF = 1
602 case ISD::SETUNE: // PF = 1 | ZF = 0
603 Opc = X86::JNE; // ZF = 0
604 Opc2 = X86::JP; // PF = 1
606 case ISD::SETOEQ: // PF = 0 & ZF = 1
609 return true; // FIXME: Emit more efficient code for this branch.
610 case ISD::SETOLT: // PF = 0 & CF = 1
613 return true; // FIXME: Emit more efficient code for this branch.
614 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
617 return true; // FIXME: Emit more efficient code for this branch.
620 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
621 BuildMI(BB, Opc, 1).addMBB(Dest);
623 BuildMI(BB, Opc2, 1).addMBB(Dest);
627 /// EmitSelectCC - Emit code into BB that performs a select operation between
628 /// the two registers RTrue and RFalse, generating a result into RDest. Return
629 /// true if the fold cannot be performed.
631 void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
632 unsigned RTrue, unsigned RFalse, unsigned RDest) {
634 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
636 } CondCode = NOT_SET;
638 static const unsigned CMOVTAB16[] = {
639 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
640 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
641 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
643 static const unsigned CMOVTAB32[] = {
644 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
645 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
646 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
648 static const unsigned CMOVTABFP[] = {
649 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
650 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
651 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
654 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
655 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
656 switch (SetCC->getCondition()) {
657 default: assert(0 && "Unknown integer comparison!");
658 case ISD::SETEQ: CondCode = EQ; break;
659 case ISD::SETGT: CondCode = GT; break;
660 case ISD::SETGE: CondCode = GE; break;
661 case ISD::SETLT: CondCode = LT; break;
662 case ISD::SETLE: CondCode = LE; break;
663 case ISD::SETNE: CondCode = NE; break;
664 case ISD::SETULT: CondCode = B; break;
665 case ISD::SETUGT: CondCode = A; break;
666 case ISD::SETULE: CondCode = BE; break;
667 case ISD::SETUGE: CondCode = AE; break;
670 // On a floating point condition, the flags are set as follows:
674 // 1 | 0 | 0 | X == Y
675 // 1 | 1 | 1 | unordered
677 switch (SetCC->getCondition()) {
678 default: assert(0 && "Unknown FP comparison!");
680 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
682 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
684 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
686 case ISD::SETLT: CondCode = B; break; // True if CF = 1
688 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
690 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
691 case ISD::SETUO: CondCode = P; break; // True if PF = 1
692 case ISD::SETO: CondCode = NP; break; // True if PF = 0
693 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
694 case ISD::SETUGE: // PF = 1 | CF = 0
695 case ISD::SETUNE: // PF = 1 | ZF = 0
696 case ISD::SETOEQ: // PF = 0 & ZF = 1
697 case ISD::SETOLT: // PF = 0 & CF = 1
698 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
699 // We cannot emit this comparison as a single cmov.
706 if (CondCode != NOT_SET) {
708 default: assert(0 && "Cannot select this type!");
709 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
710 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
712 case MVT::f64: Opc = CMOVTABFP[CondCode]; ContainsFPCode = true; break;
716 // Finally, if we weren't able to fold this, just emit the condition and test
718 if (CondCode == NOT_SET || Opc == 0) {
719 // Get the condition into the zero flag.
720 unsigned CondReg = SelectExpr(Cond);
721 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
724 default: assert(0 && "Cannot select this type!");
725 case MVT::i16: Opc = X86::CMOVE16rr; break;
726 case MVT::i32: Opc = X86::CMOVE32rr; break;
728 case MVT::f64: Opc = X86::FCMOVE; ContainsFPCode = true; break;
731 // FIXME: CMP R, 0 -> TEST R, R
732 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
734 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
737 void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
738 unsigned Tmp1 = SelectExpr(LHS), Opc;
739 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
741 switch (RHS.getValueType()) {
744 case MVT::i8: Opc = X86::CMP8ri; break;
745 case MVT::i16: Opc = X86::CMP16ri; break;
746 case MVT::i32: Opc = X86::CMP32ri; break;
749 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
754 switch (LHS.getValueType()) {
755 default: assert(0 && "Cannot compare this value!");
757 case MVT::i8: Opc = X86::CMP8rr; break;
758 case MVT::i16: Opc = X86::CMP16rr; break;
759 case MVT::i32: Opc = X86::CMP32rr; break;
761 case MVT::f64: Opc = X86::FUCOMIr; ContainsFPCode = true; break;
763 unsigned Tmp2 = SelectExpr(RHS);
764 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
767 unsigned ISel::SelectExpr(SDOperand N) {
769 unsigned Tmp1, Tmp2, Tmp3;
772 SDNode *Node = N.Val;
774 if (Node->getOpcode() == ISD::CopyFromReg)
775 // Just use the specified register as our input.
776 return dyn_cast<CopyRegSDNode>(Node)->getReg();
778 // If there are multiple uses of this expression, memorize the
779 // register it is code generated in, instead of emitting it multiple
781 // FIXME: Disabled for our current selection model.
782 if (1 || !Node->hasOneUse()) {
783 unsigned &Reg = ExprMap[N];
786 if (N.getOpcode() != ISD::CALL)
787 Reg = Result = (N.getValueType() != MVT::Other) ?
788 MakeReg(N.getValueType()) : 1;
790 // If this is a call instruction, make sure to prepare ALL of the result
791 // values as well as the chain.
792 if (Node->getNumValues() == 1)
793 Reg = Result = 1; // Void call, just a chain.
795 Result = MakeReg(Node->getValueType(0));
796 ExprMap[N.getValue(0)] = Result;
797 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
798 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
799 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
803 Result = MakeReg(N.getValueType());
806 switch (N.getOpcode()) {
809 assert(0 && "Node not handled!\n");
810 case ISD::FrameIndex:
811 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
812 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
814 case ISD::ConstantPool:
815 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
816 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
818 case ISD::ConstantFP:
819 ContainsFPCode = true;
820 Tmp1 = Result; // Intermediate Register
821 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
822 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
823 Tmp1 = MakeReg(MVT::f64);
825 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
826 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
827 BuildMI(BB, X86::FLD0, 0, Tmp1);
828 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
829 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
830 BuildMI(BB, X86::FLD1, 0, Tmp1);
832 assert(0 && "Unexpected constant!");
834 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
837 switch (N.getValueType()) {
838 default: assert(0 && "Cannot use constants of this type!");
840 case MVT::i8: Opc = X86::MOV8ri; break;
841 case MVT::i16: Opc = X86::MOV16ri; break;
842 case MVT::i32: Opc = X86::MOV32ri; break;
844 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
846 case ISD::GlobalAddress: {
847 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
848 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
851 case ISD::ExternalSymbol: {
852 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
853 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
857 Tmp1 = SelectExpr(N.getOperand(0));
858 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
859 ContainsFPCode = true;
861 case ISD::ZERO_EXTEND: {
862 int DestIs16 = N.getValueType() == MVT::i16;
863 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
864 Tmp1 = SelectExpr(N.getOperand(0));
866 // FIXME: This hack is here for zero extension casts from bool to i8. This
867 // would not be needed if bools were promoted by Legalize.
868 if (N.getValueType() == MVT::i8) {
869 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
873 static const unsigned Opc[3] = {
874 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
876 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
879 case ISD::SIGN_EXTEND: {
880 int DestIs16 = N.getValueType() == MVT::i16;
881 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
883 // FIXME: Legalize should promote bools to i8!
884 assert(N.getOperand(0).getValueType() != MVT::i1 &&
885 "Sign extend from bool not implemented!");
887 static const unsigned Opc[3] = {
888 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
890 Tmp1 = SelectExpr(N.getOperand(0));
891 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
895 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
896 // a move out of AX or AL.
897 switch (N.getOperand(0).getValueType()) {
898 default: assert(0 && "Unknown truncate!");
899 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
900 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
901 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
903 Tmp1 = SelectExpr(N.getOperand(0));
904 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
906 switch (N.getValueType()) {
907 default: assert(0 && "Unknown truncate!");
909 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
910 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
912 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
916 // Truncate from double to float by storing to memory as float,
917 // then reading it back into a register.
919 // Create as stack slot to use.
920 // FIXME: This should automatically be made by the Legalizer!
921 Tmp1 = TLI.getTargetData().getFloatAlignment();
922 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
924 // Codegen the input.
925 Tmp1 = SelectExpr(N.getOperand(0));
927 // Emit the store, then the reload.
928 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
929 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
930 ContainsFPCode = true;
933 case ISD::SINT_TO_FP:
934 case ISD::UINT_TO_FP: {
935 // FIXME: Most of this grunt work should be done by legalize!
937 // Promote the integer to a type supported by FLD. We do this because there
938 // are no unsigned FLD instructions, so we must promote an unsigned value to
939 // a larger signed value, then use FLD on the larger value.
941 MVT::ValueType PromoteType = MVT::Other;
942 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
943 unsigned PromoteOpcode = 0;
944 unsigned RealDestReg = Result;
948 // We don't have the facilities for directly loading byte sized data from
949 // memory (even signed). Promote it to 16 bits.
950 PromoteType = MVT::i16;
951 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
952 X86::MOVSX16rr8 : X86::MOVZX16rr8;
955 if (Node->getOpcode() == ISD::UINT_TO_FP) {
956 PromoteType = MVT::i32;
957 PromoteOpcode = X86::MOVZX32rr16;
961 // Don't fild into the real destination.
962 if (Node->getOpcode() == ISD::UINT_TO_FP)
963 Result = MakeReg(Node->getValueType(0));
967 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
969 if (PromoteType != MVT::Other) {
970 Tmp2 = MakeReg(PromoteType);
971 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
976 // Spill the integer to memory and reload it from there.
977 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
978 MachineFunction *F = BB->getParent();
979 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
983 // FIXME: this won't work for cast [u]long to FP
984 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
985 FrameIdx).addReg(Tmp1);
986 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
987 FrameIdx, 4).addReg(Tmp1+1);
988 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
991 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
992 FrameIdx).addReg(Tmp1);
993 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
996 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
997 FrameIdx).addReg(Tmp1);
998 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1000 default: break; // No promotion required.
1003 if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i32) {
1004 // If this is a cast from uint -> double, we need to be careful when if
1005 // the "sign" bit is set. If so, we don't want to make a negative number,
1006 // we want to make a positive number. Emit code to add an offset if the
1009 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1010 unsigned IsNeg = MakeReg(MVT::i32);
1011 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1013 // Create a CP value that has the offset in one word and 0 in the other.
1014 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1015 0x4f80000000000000ULL);
1016 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1017 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1018 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1020 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1021 // We need special handling for unsigned 64-bit integer sources. If the
1022 // input number has the "sign bit" set, then we loaded it incorrectly as a
1023 // negative 64-bit number. In this case, add an offset value.
1025 // Emit a test instruction to see if the dynamic input value was signed.
1026 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1028 // If the sign bit is set, get a pointer to an offset, otherwise get a
1029 // pointer to a zero.
1030 MachineConstantPool *CP = F->getConstantPool();
1031 unsigned Zero = MakeReg(MVT::i32);
1032 Constant *Null = Constant::getNullValue(Type::UIntTy);
1033 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1034 CP->getConstantPoolIndex(Null));
1035 unsigned Offset = MakeReg(MVT::i32);
1036 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1038 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1039 CP->getConstantPoolIndex(OffsetCst));
1040 unsigned Addr = MakeReg(MVT::i32);
1041 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1043 // Load the constant for an add. FIXME: this could make an 'fadd' that
1044 // reads directly from memory, but we don't support these yet.
1045 unsigned ConstReg = MakeReg(MVT::f64);
1046 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1048 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1052 case ISD::FP_TO_SINT:
1053 case ISD::FP_TO_UINT: {
1054 // FIXME: Most of this grunt work should be done by legalize!
1055 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1057 // Change the floating point control register to use "round towards zero"
1058 // mode when truncating to an integer value.
1060 MachineFunction *F = BB->getParent();
1061 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1062 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1064 // Load the old value of the high byte of the control word...
1065 unsigned HighPartOfCW = MakeReg(MVT::i8);
1066 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1069 // Set the high part to be round to zero...
1070 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1071 CWFrameIdx, 1).addImm(12);
1073 // Reload the modified control word now...
1074 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1076 // Restore the memory image of control word to original value
1077 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1078 CWFrameIdx, 1).addReg(HighPartOfCW);
1080 // We don't have the facilities for directly storing byte sized data to
1081 // memory. Promote it to 16 bits. We also must promote unsigned values to
1082 // larger classes because we only have signed FP stores.
1083 MVT::ValueType StoreClass = Node->getValueType(0);
1084 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1085 switch (StoreClass) {
1086 case MVT::i8: StoreClass = MVT::i16; break;
1087 case MVT::i16: StoreClass = MVT::i32; break;
1088 case MVT::i32: StoreClass = MVT::i64; break;
1089 // The following treatment of cLong may not be perfectly right,
1090 // but it survives chains of casts of the form
1091 // double->ulong->double.
1092 case MVT::i64: StoreClass = MVT::i64; break;
1093 default: assert(0 && "Unknown store class!");
1096 // Spill the integer to memory and reload it from there.
1097 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1098 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1100 switch (StoreClass) {
1101 default: assert(0 && "Unknown store class!");
1103 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1106 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
1109 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
1113 switch (Node->getValueType(0)) {
1115 assert(0 && "Unknown integer type!");
1117 // FIXME: this isn't gunna work.
1118 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1119 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1121 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1124 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1127 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1131 // Reload the original control word now.
1132 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1136 // See if we can codegen this as an LEA to fold operations together.
1137 if (N.getValueType() == MVT::i32) {
1139 if (!SelectAddress(N.getOperand(0), AM) &&
1140 !SelectAddress(N.getOperand(1), AM)) {
1141 // If this is not just an add, emit the LEA. For a simple add (like
1142 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1143 // leave this as LEA, then peephole it to 'ADD' after two address elim
1145 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
1146 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
1147 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1152 Tmp1 = SelectExpr(N.getOperand(0));
1153 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1155 if (CN->getValue() == 1) { // add X, 1 -> inc X
1156 switch (N.getValueType()) {
1157 default: assert(0 && "Cannot integer add this type!");
1158 case MVT::i8: Opc = X86::INC8r; break;
1159 case MVT::i16: Opc = X86::INC16r; break;
1160 case MVT::i32: Opc = X86::INC32r; break;
1162 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1163 switch (N.getValueType()) {
1164 default: assert(0 && "Cannot integer add this type!");
1165 case MVT::i8: Opc = X86::DEC8r; break;
1166 case MVT::i16: Opc = X86::DEC16r; break;
1167 case MVT::i32: Opc = X86::DEC32r; break;
1172 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1176 switch (N.getValueType()) {
1177 default: assert(0 && "Cannot add this type!");
1178 case MVT::i8: Opc = X86::ADD8ri; break;
1179 case MVT::i16: Opc = X86::ADD16ri; break;
1180 case MVT::i32: Opc = X86::ADD32ri; break;
1183 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1188 Tmp2 = SelectExpr(N.getOperand(1));
1189 switch (N.getValueType()) {
1190 default: assert(0 && "Cannot add this type!");
1191 case MVT::i8: Opc = X86::ADD8rr; break;
1192 case MVT::i16: Opc = X86::ADD16rr; break;
1193 case MVT::i32: Opc = X86::ADD32rr; break;
1195 case MVT::f64: Opc = X86::FpADD; ContainsFPCode = true; break;
1197 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1200 if (MVT::isInteger(N.getValueType()))
1201 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1202 if (CN->isNullValue()) { // 0 - N -> neg N
1203 switch (N.getValueType()) {
1204 default: assert(0 && "Cannot sub this type!");
1206 case MVT::i8: Opc = X86::NEG8r; break;
1207 case MVT::i16: Opc = X86::NEG16r; break;
1208 case MVT::i32: Opc = X86::NEG32r; break;
1210 Tmp1 = SelectExpr(N.getOperand(1));
1211 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1215 Tmp1 = SelectExpr(N.getOperand(0));
1216 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1217 switch (N.getValueType()) {
1218 default: assert(0 && "Cannot sub this type!");
1220 case MVT::i8: Opc = X86::SUB8ri; break;
1221 case MVT::i16: Opc = X86::SUB16ri; break;
1222 case MVT::i32: Opc = X86::SUB32ri; break;
1224 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1227 Tmp2 = SelectExpr(N.getOperand(1));
1228 switch (N.getValueType()) {
1229 default: assert(0 && "Cannot add this type!");
1231 case MVT::i8: Opc = X86::SUB8rr; break;
1232 case MVT::i16: Opc = X86::SUB16rr; break;
1233 case MVT::i32: Opc = X86::SUB32rr; break;
1235 case MVT::f64: Opc = X86::FpSUB; ContainsFPCode = true; break;
1237 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1241 Tmp1 = SelectExpr(N.getOperand(0));
1242 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1243 switch (N.getValueType()) {
1244 default: assert(0 && "Cannot add this type!");
1246 case MVT::i8: Opc = X86::AND8ri; break;
1247 case MVT::i16: Opc = X86::AND16ri; break;
1248 case MVT::i32: Opc = X86::AND32ri; break;
1250 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1253 Tmp2 = SelectExpr(N.getOperand(1));
1254 switch (N.getValueType()) {
1255 default: assert(0 && "Cannot add this type!");
1257 case MVT::i8: Opc = X86::AND8rr; break;
1258 case MVT::i16: Opc = X86::AND16rr; break;
1259 case MVT::i32: Opc = X86::AND32rr; break;
1261 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1264 Tmp1 = SelectExpr(N.getOperand(0));
1265 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1266 switch (N.getValueType()) {
1267 default: assert(0 && "Cannot add this type!");
1269 case MVT::i8: Opc = X86::OR8ri; break;
1270 case MVT::i16: Opc = X86::OR16ri; break;
1271 case MVT::i32: Opc = X86::OR32ri; break;
1273 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1276 Tmp2 = SelectExpr(N.getOperand(1));
1277 switch (N.getValueType()) {
1278 default: assert(0 && "Cannot add this type!");
1280 case MVT::i8: Opc = X86::OR8rr; break;
1281 case MVT::i16: Opc = X86::OR16rr; break;
1282 case MVT::i32: Opc = X86::OR32rr; break;
1284 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1287 Tmp1 = SelectExpr(N.getOperand(0));
1288 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1289 switch (N.getValueType()) {
1290 default: assert(0 && "Cannot add this type!");
1292 case MVT::i8: Opc = X86::XOR8ri; break;
1293 case MVT::i16: Opc = X86::XOR16ri; break;
1294 case MVT::i32: Opc = X86::XOR32ri; break;
1296 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1299 Tmp2 = SelectExpr(N.getOperand(1));
1300 switch (N.getValueType()) {
1301 default: assert(0 && "Cannot add this type!");
1303 case MVT::i8: Opc = X86::XOR8rr; break;
1304 case MVT::i16: Opc = X86::XOR16rr; break;
1305 case MVT::i32: Opc = X86::XOR32rr; break;
1307 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1311 Tmp1 = SelectExpr(N.getOperand(0));
1312 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1314 switch (N.getValueType()) {
1315 default: assert(0 && "Cannot multiply this type!");
1316 case MVT::i8: break;
1317 case MVT::i16: Opc = X86::IMUL16rri; break;
1318 case MVT::i32: Opc = X86::IMUL32rri; break;
1321 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1325 Tmp2 = SelectExpr(N.getOperand(1));
1326 switch (N.getValueType()) {
1327 default: assert(0 && "Cannot add this type!");
1329 // Must use the MUL instruction, which forces use of AL.
1330 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1331 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1332 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1334 case MVT::i16: Opc = X86::IMUL16rr; break;
1335 case MVT::i32: Opc = X86::IMUL32rr; break;
1337 case MVT::f64: Opc = X86::FpMUL; ContainsFPCode = true; break;
1339 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1343 // FIXME: implement folding of setcc into select.
1344 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
1345 Tmp2 = SelectExpr(N.getOperand(1));
1346 Tmp3 = SelectExpr(N.getOperand(2));
1347 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
1350 // FIXME: This should not be implemented here, it should be in the generic
1352 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1354 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1356 unsigned TmpReg = MakeReg(MVT::i16);
1357 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1358 // FIXME: need subregs to do better than this!
1359 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1360 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1368 Tmp1 = SelectExpr(N.getOperand(0));
1370 if (N.getOpcode() == ISD::SDIV)
1371 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1372 // FIXME: These special cases should be handled by the lowering impl!
1373 unsigned RHS = CN->getValue();
1379 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1380 unsigned Log = log2(RHS);
1381 unsigned TmpReg = MakeReg(N.getValueType());
1382 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1383 switch (N.getValueType()) {
1384 default: assert("Unknown type to signed divide!");
1386 SAROpc = X86::SAR8ri;
1387 SHROpc = X86::SHR8ri;
1388 ADDOpc = X86::ADD8rr;
1389 NEGOpc = X86::NEG8r;
1392 SAROpc = X86::SAR16ri;
1393 SHROpc = X86::SHR16ri;
1394 ADDOpc = X86::ADD16rr;
1395 NEGOpc = X86::NEG16r;
1398 SAROpc = X86::SAR32ri;
1399 SHROpc = X86::SHR32ri;
1400 ADDOpc = X86::ADD32rr;
1401 NEGOpc = X86::NEG32r;
1404 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1405 unsigned TmpReg2 = MakeReg(N.getValueType());
1406 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1407 unsigned TmpReg3 = MakeReg(N.getValueType());
1408 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1410 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1411 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1413 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1418 Tmp2 = SelectExpr(N.getOperand(1));
1420 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1421 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1422 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1423 switch (N.getValueType()) {
1424 default: assert(0 && "Cannot sdiv this type!");
1426 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1429 MovOpcode = X86::MOV8rr;
1430 ClrOpcode = X86::MOV8ri;
1431 SExtOpcode = X86::CBW;
1434 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1437 MovOpcode = X86::MOV16rr;
1438 ClrOpcode = X86::MOV16ri;
1439 SExtOpcode = X86::CWD;
1442 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
1445 MovOpcode = X86::MOV32rr;
1446 ClrOpcode = X86::MOV32ri;
1447 SExtOpcode = X86::CDQ;
1449 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1452 ContainsFPCode = true;
1453 if (N.getOpcode() == ISD::SDIV)
1454 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1456 assert(0 && "FIXME: Emit frem libcall to fmod!");
1460 // Set up the low part.
1461 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1464 // Sign extend the low part into the high part.
1465 BuildMI(BB, SExtOpcode, 0);
1467 // Zero out the high part, effectively zero extending the input.
1468 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1471 // Emit the DIV/IDIV instruction.
1472 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1474 // Get the result of the divide or rem.
1475 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1480 Tmp1 = SelectExpr(N.getOperand(0));
1481 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1482 switch (N.getValueType()) {
1483 default: assert(0 && "Cannot shift this type!");
1484 case MVT::i8: Opc = X86::SHL8ri; break;
1485 case MVT::i16: Opc = X86::SHL16ri; break;
1486 case MVT::i32: Opc = X86::SHL32ri; break;
1488 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1491 Tmp2 = SelectExpr(N.getOperand(1));
1492 switch (N.getValueType()) {
1493 default: assert(0 && "Cannot shift this type!");
1494 case MVT::i8 : Opc = X86::SHL8rCL; break;
1495 case MVT::i16: Opc = X86::SHL16rCL; break;
1496 case MVT::i32: Opc = X86::SHL32rCL; break;
1498 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1499 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1502 Tmp1 = SelectExpr(N.getOperand(0));
1503 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1504 switch (N.getValueType()) {
1505 default: assert(0 && "Cannot shift this type!");
1506 case MVT::i8: Opc = X86::SHR8ri; break;
1507 case MVT::i16: Opc = X86::SHR16ri; break;
1508 case MVT::i32: Opc = X86::SHR32ri; break;
1510 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1513 Tmp2 = SelectExpr(N.getOperand(1));
1514 switch (N.getValueType()) {
1515 default: assert(0 && "Cannot shift this type!");
1516 case MVT::i8 : Opc = X86::SHR8rCL; break;
1517 case MVT::i16: Opc = X86::SHR16rCL; break;
1518 case MVT::i32: Opc = X86::SHR32rCL; break;
1520 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1521 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1524 Tmp1 = SelectExpr(N.getOperand(0));
1525 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1526 switch (N.getValueType()) {
1527 default: assert(0 && "Cannot shift this type!");
1528 case MVT::i8: Opc = X86::SAR8ri; break;
1529 case MVT::i16: Opc = X86::SAR16ri; break;
1530 case MVT::i32: Opc = X86::SAR32ri; break;
1532 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1535 Tmp2 = SelectExpr(N.getOperand(1));
1536 switch (N.getValueType()) {
1537 default: assert(0 && "Cannot shift this type!");
1538 case MVT::i8 : Opc = X86::SAR8rCL; break;
1539 case MVT::i16: Opc = X86::SAR16rCL; break;
1540 case MVT::i32: Opc = X86::SAR32rCL; break;
1542 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1543 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1547 if (MVT::isFloatingPoint(N.getOperand(0).getValueType()))
1548 ContainsFPCode = true;
1549 EmitCMP(N.getOperand(0), N.getOperand(1));
1550 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1551 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1554 // The chain for this load is now lowered.
1555 LoweredTokens.insert(SDOperand(Node, 1));
1556 Select(N.getOperand(0));
1558 // Make sure we generate both values.
1560 ExprMap[N.getValue(1)] = 1; // Generate the token
1562 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1564 switch (Node->getValueType(0)) {
1565 default: assert(0 && "Cannot load this type!");
1567 case MVT::i8: Opc = X86::MOV8rm; break;
1568 case MVT::i16: Opc = X86::MOV16rm; break;
1569 case MVT::i32: Opc = X86::MOV32rm; break;
1570 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1571 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1573 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
1574 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1577 SelectAddress(N.getOperand(1), AM);
1578 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1582 case ISD::DYNAMIC_STACKALLOC:
1583 Select(N.getOperand(0));
1585 // Generate both result values.
1587 ExprMap[N.getValue(1)] = 1; // Generate the token
1589 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1591 // FIXME: We are currently ignoring the requested alignment for handling
1592 // greater than the stack alignment. This will need to be revisited at some
1593 // point. Align = N.getOperand(2);
1595 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1596 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1597 std::cerr << "Cannot allocate stack object with greater alignment than"
1598 << " the stack alignment yet!";
1602 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1603 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1604 .addImm(CN->getValue());
1606 Tmp1 = SelectExpr(N.getOperand(1));
1608 // Subtract size from stack pointer, thereby allocating some space.
1609 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
1612 // Put a pointer to the space into the result register, by copying the stack
1614 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
1618 // The chain for this call is now lowered.
1619 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
1621 Select(N.getOperand(0));
1622 if (GlobalAddressSDNode *GASD =
1623 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1624 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
1625 } else if (ExternalSymbolSDNode *ESSDN =
1626 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1627 BuildMI(BB, X86::CALLpcrel32,
1628 1).addExternalSymbol(ESSDN->getSymbol(), true);
1630 Tmp1 = SelectExpr(N.getOperand(1));
1631 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
1633 switch (Node->getValueType(0)) {
1634 default: assert(0 && "Unknown value type for call result!");
1635 case MVT::Other: return 1;
1638 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1641 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
1644 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
1645 if (Node->getValueType(1) == MVT::i32)
1646 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
1649 case MVT::f64: // Floating-point return values live in %ST(0)
1650 ContainsFPCode = true;
1651 BuildMI(BB, X86::FpGETRESULT, 1, Result);
1654 return Result+N.ResNo;
1660 void ISel::Select(SDOperand N) {
1661 unsigned Tmp1, Tmp2, Opc;
1663 // FIXME: Disable for our current expansion model!
1664 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
1665 return; // Already selected.
1667 switch (N.getOpcode()) {
1669 N.Val->dump(); std::cerr << "\n";
1670 assert(0 && "Node not handled yet!");
1671 case ISD::EntryToken: return; // Noop
1672 case ISD::CopyToReg:
1673 Select(N.getOperand(0));
1674 Tmp1 = SelectExpr(N.getOperand(1));
1675 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
1678 switch (N.getOperand(1).getValueType()) {
1679 default: assert(0 && "Invalid type for operation!");
1681 case MVT::i8: Opc = X86::MOV8rr; break;
1682 case MVT::i16: Opc = X86::MOV16rr; break;
1683 case MVT::i32: Opc = X86::MOV32rr; break;
1685 case MVT::f64: Opc = X86::FpMOV; break;
1687 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1691 Select(N.getOperand(0));
1692 switch (N.getNumOperands()) {
1694 assert(0 && "Unknown return instruction!");
1696 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1697 N.getOperand(2).getValueType() == MVT::i32 &&
1698 "Unknown two-register value!");
1699 Tmp1 = SelectExpr(N.getOperand(1));
1700 Tmp2 = SelectExpr(N.getOperand(2));
1701 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1702 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
1703 // Declare that EAX & EDX are live on exit.
1704 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1708 Tmp1 = SelectExpr(N.getOperand(1));
1709 switch (N.getOperand(1).getValueType()) {
1710 default: assert(0 && "All other types should have been promoted!!");
1712 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
1713 // Declare that top-of-stack is live on exit
1714 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
1717 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1718 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
1725 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1728 Select(N.getOperand(0));
1729 MachineBasicBlock *Dest =
1730 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1731 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
1736 Select(N.getOperand(0));
1737 MachineBasicBlock *Dest =
1738 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1739 // Try to fold a setcc into the branch. If this fails, emit a test/jne
1741 if (EmitBranchCC(Dest, N.getOperand(1))) {
1742 Tmp1 = SelectExpr(N.getOperand(1));
1743 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
1744 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1750 case ISD::DYNAMIC_STACKALLOC:
1754 Select(N.getOperand(0));
1755 // Select the address.
1757 SelectAddress(N.getOperand(2), AM);
1759 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1761 switch (CN->getValueType(0)) {
1762 default: assert(0 && "Invalid type for operation!");
1764 case MVT::i8: Opc = X86::MOV8mi; break;
1765 case MVT::i16: Opc = X86::MOV16mi; break;
1766 case MVT::i32: Opc = X86::MOV32mi; break;
1768 case MVT::f64: break;
1771 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
1775 Tmp1 = SelectExpr(N.getOperand(1));
1777 switch (N.getOperand(1).getValueType()) {
1778 default: assert(0 && "Cannot store this type!");
1780 case MVT::i8: Opc = X86::MOV8mr; break;
1781 case MVT::i16: Opc = X86::MOV16mr; break;
1782 case MVT::i32: Opc = X86::MOV32mr; break;
1783 case MVT::f32: Opc = X86::FST32m; ContainsFPCode = true; break;
1784 case MVT::f64: Opc = X86::FST64m; ContainsFPCode = true; break;
1786 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
1789 case ISD::ADJCALLSTACKDOWN:
1790 case ISD::ADJCALLSTACKUP:
1791 Select(N.getOperand(0));
1792 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1794 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
1795 X86::ADJCALLSTACKUP;
1796 BuildMI(BB, Opc, 1).addImm(Tmp1);
1799 assert(0 && "Should not be reached!");
1803 /// createX86PatternInstructionSelector - This pass converts an LLVM function
1804 /// into a machine code representation using pattern matching and a machine
1805 /// description file.
1807 FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
1808 return new ISel(TM);