1 //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the Evan Cheng and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a DAG pattern matching instruction selector for X86,
11 // converting from a legalized dag to a X86 dag.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "x86-isel"
17 #include "X86InstrBuilder.h"
18 #include "X86ISelLowering.h"
19 #include "X86RegisterInfo.h"
20 #include "X86Subtarget.h"
21 #include "X86TargetMachine.h"
22 #include "llvm/GlobalValue.h"
23 #include "llvm/Instructions.h"
24 #include "llvm/Intrinsics.h"
25 #include "llvm/Support/CFG.h"
26 #include "llvm/Type.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/SSARegMap.h"
32 #include "llvm/CodeGen/SelectionDAGISel.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/ADT/Statistic.h"
42 STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
43 STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
46 //===----------------------------------------------------------------------===//
47 // Pattern Matcher Implementation
48 //===----------------------------------------------------------------------===//
51 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
52 /// SDOperand's instead of register numbers for the leaves of the matched
54 struct X86ISelAddressMode {
60 struct { // This is really a union, discriminated by BaseType!
65 bool isRIPRel; // RIP relative?
73 unsigned Align; // CP alignment.
76 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
77 GV(0), CP(0), ES(0), JT(-1), Align(0) {
83 //===--------------------------------------------------------------------===//
84 /// ISel - X86 specific code to select X86 machine instructions for
85 /// SelectionDAG operations.
87 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
88 /// ContainsFPCode - Every instruction we select that uses or defines a FP
89 /// register should set this to true.
92 /// FastISel - Enable fast(er) instruction selection.
96 /// TM - Keep a reference to X86TargetMachine.
100 /// X86Lowering - This object fully describes how to lower LLVM code to an
101 /// X86-specific SelectionDAG.
102 X86TargetLowering X86Lowering;
104 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
105 /// make the right decision when generating code for different targets.
106 const X86Subtarget *Subtarget;
108 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
110 unsigned GlobalBaseReg;
113 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
114 : SelectionDAGISel(X86Lowering),
115 ContainsFPCode(false), FastISel(fast), TM(tm),
116 X86Lowering(*TM.getTargetLowering()),
117 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
119 virtual bool runOnFunction(Function &Fn) {
120 // Make sure we re-emit a set of the global base reg if necessary
122 return SelectionDAGISel::runOnFunction(Fn);
125 virtual const char *getPassName() const {
126 return "X86 DAG->DAG Instruction Selection";
129 /// InstructionSelectBasicBlock - This callback is invoked by
130 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
131 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
133 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
135 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
137 // Include the pieces autogenerated from the target description.
138 #include "X86GenDAGISel.inc"
141 SDNode *Select(SDOperand N);
143 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM,
144 bool isRoot = true, unsigned Depth = 0);
145 bool MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
146 bool isRoot, unsigned Depth);
147 bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
148 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
149 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
150 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
151 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
152 SDOperand N, SDOperand &Base, SDOperand &Scale,
153 SDOperand &Index, SDOperand &Disp,
154 SDOperand &InChain, SDOperand &OutChain);
155 bool TryFoldLoad(SDOperand P, SDOperand N,
156 SDOperand &Base, SDOperand &Scale,
157 SDOperand &Index, SDOperand &Disp);
158 void InstructionSelectPreprocess(SelectionDAG &DAG);
160 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
161 /// inline asm expressions.
162 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
164 std::vector<SDOperand> &OutOps,
167 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
169 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
170 SDOperand &Scale, SDOperand &Index,
172 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
173 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
175 Scale = getI8Imm(AM.Scale);
177 // These are 32-bit even in 64-bit mode since RIP relative offset
180 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
182 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
184 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
185 else if (AM.JT != -1)
186 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
188 Disp = getI32Imm(AM.Disp);
191 /// getI8Imm - Return a target constant with the specified value, of type
193 inline SDOperand getI8Imm(unsigned Imm) {
194 return CurDAG->getTargetConstant(Imm, MVT::i8);
197 /// getI16Imm - Return a target constant with the specified value, of type
199 inline SDOperand getI16Imm(unsigned Imm) {
200 return CurDAG->getTargetConstant(Imm, MVT::i16);
203 /// getI32Imm - Return a target constant with the specified value, of type
205 inline SDOperand getI32Imm(unsigned Imm) {
206 return CurDAG->getTargetConstant(Imm, MVT::i32);
209 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
210 /// base register. Return the virtual register that holds this value.
211 SDNode *getGlobalBaseReg();
213 /// getTruncate - return an SDNode that implements a subreg based truncate
214 /// of the specified operand to the the specified value type.
215 SDNode *getTruncate(SDOperand N0, MVT::ValueType VT);
223 static SDNode *findFlagUse(SDNode *N) {
224 unsigned FlagResNo = N->getNumValues()-1;
225 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
227 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
228 SDOperand Op = User->getOperand(i);
229 if (Op.Val == N && Op.ResNo == FlagResNo)
236 static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
237 SDNode *Root, SDNode *Skip, bool &found,
238 std::set<SDNode *> &Visited) {
240 Use->getNodeId() > Def->getNodeId() ||
241 !Visited.insert(Use).second)
244 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
245 SDNode *N = Use->getOperand(i).Val;
250 continue; // Immediate use is ok.
252 assert(Use->getOpcode() == ISD::STORE ||
253 Use->getOpcode() == X86ISD::CMP);
259 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
263 /// isNonImmUse - Start searching from Root up the DAG to check is Def can
264 /// be reached. Return true if that's the case. However, ignore direct uses
265 /// by ImmedUse (which would be U in the example illustrated in
266 /// CanBeFoldedBy) and by Root (which can happen in the store case).
267 /// FIXME: to be really generic, we should allow direct use by any node
268 /// that is being folded. But realisticly since we only fold loads which
269 /// have one non-chain use, we only need to watch out for load/op/store
270 /// and load/op/cmp case where the root (store / cmp) may reach the load via
271 /// its chain operand.
272 static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
273 SDNode *Skip = NULL) {
274 std::set<SDNode *> Visited;
276 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
281 bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
282 if (FastISel) return false;
284 // If U use can somehow reach N through another path then U can't fold N or
285 // it will create a cycle. e.g. In the following diagram, U can reach N
286 // through X. If N is folded into into U, then X is both a predecessor and
297 if (isNonImmUse(Root, N, U))
300 // If U produces a flag, then it gets (even more) interesting. Since it
301 // would have been "glued" together with its flag use, we need to check if
314 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
315 // NU), then TF is a predecessor of FU and a successor of NU. But since
316 // NU and FU are flagged together, this effectively creates a cycle.
317 bool HasFlagUse = false;
318 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
319 while ((VT == MVT::Flag && !Root->use_empty())) {
320 SDNode *FU = findFlagUse(Root);
327 VT = Root->getValueType(Root->getNumValues()-1);
331 return !isNonImmUse(Root, N, Root, U);
335 /// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
336 /// and move load below the TokenFactor. Replace store's chain operand with
337 /// load's chain result.
338 static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
339 SDOperand Store, SDOperand TF) {
340 std::vector<SDOperand> Ops;
341 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
342 if (Load.Val == TF.Val->getOperand(i).Val)
343 Ops.push_back(Load.Val->getOperand(0));
345 Ops.push_back(TF.Val->getOperand(i));
346 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
347 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
348 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
349 Store.getOperand(2), Store.getOperand(3));
352 /// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
353 /// selector to pick more load-modify-store instructions. This is a common
364 /// [TokenFactor] [Op]
371 /// The fact the store's chain operand != load's chain will prevent the
372 /// (store (op (load))) instruction from being selected. We can transform it to:
391 void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
392 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
393 E = DAG.allnodes_end(); I != E; ++I) {
394 if (!ISD::isNON_TRUNCStore(I))
396 SDOperand Chain = I->getOperand(0);
397 if (Chain.Val->getOpcode() != ISD::TokenFactor)
400 SDOperand N1 = I->getOperand(1);
401 SDOperand N2 = I->getOperand(2);
402 if (MVT::isFloatingPoint(N1.getValueType()) ||
403 MVT::isVector(N1.getValueType()) ||
409 unsigned Opcode = N1.Val->getOpcode();
418 SDOperand N10 = N1.getOperand(0);
419 SDOperand N11 = N1.getOperand(1);
420 if (ISD::isNON_EXTLoad(N10.Val))
422 else if (ISD::isNON_EXTLoad(N11.Val)) {
426 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
427 (N10.getOperand(1) == N2) &&
428 (N10.Val->getValueType(0) == N1.getValueType());
443 SDOperand N10 = N1.getOperand(0);
444 if (ISD::isNON_EXTLoad(N10.Val))
445 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
446 (N10.getOperand(1) == N2) &&
447 (N10.Val->getValueType(0) == N1.getValueType());
455 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
461 /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
462 /// when it has created a SelectionDAG for us to codegen.
463 void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
465 MachineFunction::iterator FirstMBB = BB;
468 InstructionSelectPreprocess(DAG);
470 // Codegen the basic block.
472 DOUT << "===== Instruction selection begins:\n";
475 DAG.setRoot(SelectRoot(DAG.getRoot()));
477 DOUT << "===== Instruction selection ends:\n";
480 DAG.RemoveDeadNodes();
482 // Emit machine code to BB.
483 ScheduleAndEmitDAG(DAG);
485 // If we are emitting FP stack code, scan the basic block to determine if this
486 // block defines any FP values. If so, put an FP_REG_KILL instruction before
487 // the terminator of the block.
489 // Note that FP stack instructions *are* used in SSE code for long double,
490 // so we do need this check.
491 bool ContainsFPCode = false;
493 // Scan all of the machine instructions in these MBBs, checking for FP
494 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
495 MachineFunction::iterator MBBI = FirstMBB;
497 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
498 !ContainsFPCode && I != E; ++I) {
499 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
500 const TargetRegisterClass *clas;
501 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
502 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
503 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
504 ((clas = RegMap->getRegClass(I->getOperand(0).getReg())) ==
505 X86::RFP32RegisterClass ||
506 clas == X86::RFP64RegisterClass ||
507 clas == X86::RFP80RegisterClass)) {
508 ContainsFPCode = true;
514 } while (!ContainsFPCode && &*(MBBI++) != BB);
516 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
517 // a copy of the input value in this block. In SSE mode, we only care about
519 if (!ContainsFPCode) {
520 // Final check, check LLVM BB's that are successors to the LLVM BB
521 // corresponding to BB for FP PHI nodes.
522 const BasicBlock *LLVMBB = BB->getBasicBlock();
524 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
525 !ContainsFPCode && SI != E; ++SI) {
526 for (BasicBlock::const_iterator II = SI->begin();
527 (PN = dyn_cast<PHINode>(II)); ++II) {
528 if (PN->getType()==Type::X86_FP80Ty ||
529 (!Subtarget->hasSSE2() && PN->getType()->isFloatingPoint())) {
530 ContainsFPCode = true;
537 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
538 if (ContainsFPCode) {
539 BuildMI(*BB, BB->getFirstTerminator(),
540 TM.getInstrInfo()->get(X86::FP_REG_KILL));
545 /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
546 /// the main function.
547 void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
548 MachineFrameInfo *MFI) {
549 const TargetInstrInfo *TII = TM.getInstrInfo();
550 if (Subtarget->isTargetCygMing())
551 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
553 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
554 int CWFrameIdx = MFI->CreateStackObject(2, 2);
555 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
557 // Set the high part to be 64-bit precision.
558 addFrameReference(BuildMI(BB, TII->get(X86::MOV8mi)),
559 CWFrameIdx, 1).addImm(2);
561 // Reload the modified control word now.
562 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
565 void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
566 // If this is main, emit special code for main.
567 MachineBasicBlock *BB = MF.begin();
568 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
569 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
572 /// MatchAddress - Add the specified node to the specified addressing mode,
573 /// returning true if it cannot be done. This just pattern matches for the
575 bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
576 bool isRoot, unsigned Depth) {
579 return MatchAddressBase(N, AM, isRoot, Depth);
581 // RIP relative addressing: %rip + 32-bit displacement!
583 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
584 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
585 if (isInt32(AM.Disp + Val)) {
593 int id = N.Val->getNodeId();
594 bool Available = isSelected(id);
596 switch (N.getOpcode()) {
598 case ISD::Constant: {
599 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
600 if (isInt32(AM.Disp + Val)) {
607 case X86ISD::Wrapper: {
608 bool is64Bit = Subtarget->is64Bit();
609 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
610 if (is64Bit && TM.getCodeModel() != CodeModel::Small)
612 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
614 // If value is available in a register both base and index components have
615 // been picked, we can't fit the result available in the register in the
616 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
617 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
618 bool isStatic = TM.getRelocationModel() == Reloc::Static;
619 SDOperand N0 = N.getOperand(0);
620 // Mac OS X X86-64 lower 4G address is not available.
621 bool isAbs32 = !is64Bit ||
622 (isStatic && Subtarget->hasLow4GUserSpaceAddress());
623 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
624 GlobalValue *GV = G->getGlobal();
625 if (isAbs32 || isRoot) {
627 AM.Disp += G->getOffset();
628 AM.isRIPRel = !isAbs32;
631 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
632 if (isAbs32 || isRoot) {
633 AM.CP = CP->getConstVal();
634 AM.Align = CP->getAlignment();
635 AM.Disp += CP->getOffset();
636 AM.isRIPRel = !isAbs32;
639 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
640 if (isAbs32 || isRoot) {
641 AM.ES = S->getSymbol();
642 AM.isRIPRel = !isAbs32;
645 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
646 if (isAbs32 || isRoot) {
647 AM.JT = J->getIndex();
648 AM.isRIPRel = !isAbs32;
656 case ISD::FrameIndex:
657 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
658 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
659 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
665 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
666 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
667 unsigned Val = CN->getValue();
668 if (Val == 1 || Val == 2 || Val == 3) {
670 SDOperand ShVal = N.Val->getOperand(0);
672 // Okay, we know that we have a scale by now. However, if the scaled
673 // value is an add of something and a constant, we can fold the
674 // constant into the disp field here.
675 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
676 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
677 AM.IndexReg = ShVal.Val->getOperand(0);
678 ConstantSDNode *AddVal =
679 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
680 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
694 // X*[3,5,9] -> X+X*[2,4,8]
696 AM.BaseType == X86ISelAddressMode::RegBase &&
697 AM.Base.Reg.Val == 0 &&
698 AM.IndexReg.Val == 0) {
699 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
700 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
701 AM.Scale = unsigned(CN->getValue())-1;
703 SDOperand MulVal = N.Val->getOperand(0);
706 // Okay, we know that we have a scale by now. However, if the scaled
707 // value is an add of something and a constant, we can fold the
708 // constant into the disp field here.
709 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
710 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
711 Reg = MulVal.Val->getOperand(0);
712 ConstantSDNode *AddVal =
713 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
714 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
718 Reg = N.Val->getOperand(0);
720 Reg = N.Val->getOperand(0);
723 AM.IndexReg = AM.Base.Reg = Reg;
731 X86ISelAddressMode Backup = AM;
732 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
733 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
736 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
737 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
744 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
746 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
747 X86ISelAddressMode Backup = AM;
748 // Start with the LHS as an addr mode.
749 if (!MatchAddress(N.getOperand(0), AM, false) &&
750 // Address could not have picked a GV address for the displacement.
752 // On x86-64, the resultant disp must fit in 32-bits.
753 isInt32(AM.Disp + CN->getSignExtended()) &&
754 // Check to see if the LHS & C is zero.
755 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getValue())) {
756 AM.Disp += CN->getValue();
765 return MatchAddressBase(N, AM, isRoot, Depth);
768 /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
769 /// specified addressing mode without any further recursion.
770 bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
771 bool isRoot, unsigned Depth) {
772 // Is the base register already occupied?
773 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
774 // If so, check to see if the scale index register is set.
775 if (AM.IndexReg.Val == 0) {
781 // Otherwise, we cannot select it.
785 // Default, generate it as a register.
786 AM.BaseType = X86ISelAddressMode::RegBase;
791 /// SelectAddr - returns true if it is able pattern match an addressing mode.
792 /// It returns the operands which make up the maximal addressing mode it can
793 /// match by reference.
794 bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
795 SDOperand &Scale, SDOperand &Index,
797 X86ISelAddressMode AM;
798 if (MatchAddress(N, AM))
801 MVT::ValueType VT = N.getValueType();
802 if (AM.BaseType == X86ISelAddressMode::RegBase) {
803 if (!AM.Base.Reg.Val)
804 AM.Base.Reg = CurDAG->getRegister(0, VT);
807 if (!AM.IndexReg.Val)
808 AM.IndexReg = CurDAG->getRegister(0, VT);
810 getAddressOperands(AM, Base, Scale, Index, Disp);
814 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
816 static inline bool isZeroNode(SDOperand Elt) {
817 return ((isa<ConstantSDNode>(Elt) &&
818 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
819 (isa<ConstantFPSDNode>(Elt) &&
820 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
824 /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
825 /// match a load whose top elements are either undef or zeros. The load flavor
826 /// is derived from the type of N, which is either v4f32 or v2f64.
827 bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
828 SDOperand N, SDOperand &Base,
829 SDOperand &Scale, SDOperand &Index,
830 SDOperand &Disp, SDOperand &InChain,
831 SDOperand &OutChain) {
832 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
833 InChain = N.getOperand(0).getValue(1);
834 if (ISD::isNON_EXTLoad(InChain.Val) &&
835 InChain.getValue(0).hasOneUse() &&
837 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
838 LoadSDNode *LD = cast<LoadSDNode>(InChain);
839 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
841 OutChain = LD->getChain();
846 // Also handle the case where we explicitly require zeros in the top
847 // elements. This is a vector shuffle from the zero vector.
848 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
849 N.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
850 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
851 N.getOperand(1).Val->hasOneUse() &&
852 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
853 N.getOperand(1).getOperand(0).hasOneUse()) {
854 // Check to see if the BUILD_VECTOR is building a zero vector.
855 SDOperand BV = N.getOperand(0);
856 for (unsigned i = 0, e = BV.getNumOperands(); i != e; ++i)
857 if (!isZeroNode(BV.getOperand(i)) &&
858 BV.getOperand(i).getOpcode() != ISD::UNDEF)
859 return false; // Not a zero/undef vector.
860 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
862 unsigned VecWidth = BV.getNumOperands();
863 SDOperand ShufMask = N.getOperand(2);
864 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
865 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
866 if (C->getValue() == VecWidth) {
867 for (unsigned i = 1; i != VecWidth; ++i) {
868 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
871 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
872 if (C->getValue() >= VecWidth) return false;
877 // Okay, this is a zero extending load. Fold it.
878 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
879 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
881 OutChain = LD->getChain();
882 InChain = SDOperand(LD, 1);
890 /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
891 /// mode it matches can be cost effectively emitted as an LEA instruction.
892 bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
893 SDOperand &Base, SDOperand &Scale,
894 SDOperand &Index, SDOperand &Disp) {
895 X86ISelAddressMode AM;
896 if (MatchAddress(N, AM))
899 MVT::ValueType VT = N.getValueType();
900 unsigned Complexity = 0;
901 if (AM.BaseType == X86ISelAddressMode::RegBase)
905 AM.Base.Reg = CurDAG->getRegister(0, VT);
906 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
912 AM.IndexReg = CurDAG->getRegister(0, VT);
914 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
919 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
920 // to a LEA. This is determined with some expermentation but is by no means
921 // optimal (especially for code size consideration). LEA is nice because of
922 // its three-address nature. Tweak the cost function again when we can run
923 // convertToThreeAddress() at register allocation time.
924 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
925 // For X86-64, we should always use lea to materialize RIP relative
927 if (Subtarget->is64Bit())
933 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
936 if (Complexity > 2) {
937 getAddressOperands(AM, Base, Scale, Index, Disp);
943 bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
944 SDOperand &Base, SDOperand &Scale,
945 SDOperand &Index, SDOperand &Disp) {
946 if (ISD::isNON_EXTLoad(N.Val) &&
948 CanBeFoldedBy(N.Val, P.Val, P.Val))
949 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
953 /// getGlobalBaseReg - Output the instructions required to put the
954 /// base address to use for accessing globals into a register.
956 SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
957 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
958 if (!GlobalBaseReg) {
959 // Insert the set of GlobalBaseReg into the first MBB of the function
960 MachineBasicBlock &FirstMBB = BB->getParent()->front();
961 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
962 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
963 unsigned PC = RegMap->createVirtualRegister(X86::GR32RegisterClass);
965 const TargetInstrInfo *TII = TM.getInstrInfo();
966 BuildMI(FirstMBB, MBBI, TII->get(X86::MovePCtoStack));
967 BuildMI(FirstMBB, MBBI, TII->get(X86::POP32r), PC);
969 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
970 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
971 if (TM.getRelocationModel() == Reloc::PIC_ &&
972 Subtarget->isPICStyleGOT()) {
973 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
974 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg).
976 addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
982 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
985 static SDNode *FindCallStartFromCall(SDNode *Node) {
986 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
987 assert(Node->getOperand(0).getValueType() == MVT::Other &&
988 "Node doesn't have a token chain argument!");
989 return FindCallStartFromCall(Node->getOperand(0).Val);
992 SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) {
996 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
997 // Ensure that the source register has an 8-bit subreg on 32-bit targets
998 if (!Subtarget->is64Bit()) {
1001 switch (N0.getValueType()) {
1002 default: assert(0 && "Unknown truncate!");
1004 Opc = X86::MOV16to16_;
1008 Opc = X86::MOV32to32_;
1013 SDOperand(CurDAG->getTargetNode(Opc, VT, N0), 0);
1017 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1020 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1022 default: assert(0 && "Unknown truncate!");
1024 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1030 SDNode *X86DAGToDAGISel::Select(SDOperand N) {
1031 SDNode *Node = N.Val;
1032 MVT::ValueType NVT = Node->getValueType(0);
1034 unsigned Opcode = Node->getOpcode();
1037 DOUT << std::string(Indent, ' ') << "Selecting: ";
1038 DEBUG(Node->dump(CurDAG));
1043 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
1045 DOUT << std::string(Indent-2, ' ') << "== ";
1046 DEBUG(Node->dump(CurDAG));
1050 return NULL; // Already selected.
1055 case X86ISD::GlobalBaseReg:
1056 return getGlobalBaseReg();
1059 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1060 // code and is matched first so to prevent it from being turned into
1062 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
1063 MVT::ValueType PtrVT = TLI.getPointerTy();
1064 SDOperand N0 = N.getOperand(0);
1065 SDOperand N1 = N.getOperand(1);
1066 if (N.Val->getValueType(0) == PtrVT &&
1067 N0.getOpcode() == X86ISD::Wrapper &&
1068 N1.getOpcode() == ISD::Constant) {
1069 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1071 // TODO: handle ExternalSymbolSDNode.
1072 if (GlobalAddressSDNode *G =
1073 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
1074 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
1075 G->getOffset() + Offset);
1076 } else if (ConstantPoolSDNode *CP =
1077 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
1078 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
1080 CP->getOffset()+Offset);
1084 if (Subtarget->is64Bit()) {
1085 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1086 CurDAG->getRegister(0, PtrVT), C };
1087 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1089 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1093 // Other cases are handled by auto-generated code.
1098 if (NVT == MVT::i8) {
1099 SDOperand N0 = Node->getOperand(0);
1100 SDOperand N1 = Node->getOperand(1);
1101 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1102 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1104 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
1111 SDOperand Chain = N1.getOperand(0);
1113 AddToISelQueue(Chain);
1114 AddToISelQueue(Tmp0);
1115 AddToISelQueue(Tmp1);
1116 AddToISelQueue(Tmp2);
1117 AddToISelQueue(Tmp3);
1118 SDOperand InFlag(0, 0);
1119 Chain = CurDAG->getCopyToReg(Chain, X86::AL, N0, InFlag);
1120 InFlag = Chain.getValue(1);
1121 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
1122 ResNode = CurDAG->getTargetNode(X86::MUL8m, MVT::i8, MVT::i8,
1123 MVT::Other, Ops, 6);
1124 ReplaceUses(N1.getValue(1), SDOperand(ResNode, 2));
1126 SDOperand Chain = CurDAG->getEntryNode();
1129 SDOperand InFlag(0, 0);
1130 InFlag = CurDAG->getCopyToReg(Chain, X86::AL, N0, InFlag).getValue(1);
1131 ResNode = CurDAG->getTargetNode(X86::MUL8r, MVT::i8, MVT::i8,
1135 ReplaceUses(N.getValue(0), SDOperand(ResNode, 0));
1143 if (Opcode == ISD::MULHU)
1145 default: assert(0 && "Unsupported VT!");
1146 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1147 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1148 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1149 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1153 default: assert(0 && "Unsupported VT!");
1154 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1155 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1156 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1157 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1160 unsigned LoReg, HiReg;
1162 default: assert(0 && "Unsupported VT!");
1163 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1164 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1165 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1166 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1169 SDOperand N0 = Node->getOperand(0);
1170 SDOperand N1 = Node->getOperand(1);
1172 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1173 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1174 // MULHU and MULHS are commmutative
1176 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
1183 Chain = N1.getOperand(0);
1184 AddToISelQueue(Chain);
1186 Chain = CurDAG->getEntryNode();
1188 SDOperand InFlag(0, 0);
1190 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
1192 InFlag = Chain.getValue(1);
1195 AddToISelQueue(Tmp0);
1196 AddToISelQueue(Tmp1);
1197 AddToISelQueue(Tmp2);
1198 AddToISelQueue(Tmp3);
1199 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
1201 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
1202 Chain = SDOperand(CNode, 0);
1203 InFlag = SDOperand(CNode, 1);
1207 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1211 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1212 // Prevent use of AH in a REX instruction by referencing AX instead.
1213 // Shift it down 8 bits.
1214 Result = CurDAG->getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1215 Chain = Result.getValue(1);
1216 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1217 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1218 // Then truncate it down to i8.
1219 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1220 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1221 MVT::i8, Result, SRIdx), 0);
1223 Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
1225 ReplaceUses(N.getValue(0), Result);
1227 ReplaceUses(N1.getValue(1), Result.getValue(1));
1230 DOUT << std::string(Indent-2, ' ') << "=> ";
1231 DEBUG(Result.Val->dump(CurDAG));
1242 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
1243 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
1246 default: assert(0 && "Unsupported VT!");
1247 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1248 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1249 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1250 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1254 default: assert(0 && "Unsupported VT!");
1255 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1256 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1257 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1258 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1261 unsigned LoReg, HiReg;
1262 unsigned ClrOpcode, SExtOpcode;
1264 default: assert(0 && "Unsupported VT!");
1266 LoReg = X86::AL; HiReg = X86::AH;
1268 SExtOpcode = X86::CBW;
1271 LoReg = X86::AX; HiReg = X86::DX;
1272 ClrOpcode = X86::MOV16r0;
1273 SExtOpcode = X86::CWD;
1276 LoReg = X86::EAX; HiReg = X86::EDX;
1277 ClrOpcode = X86::MOV32r0;
1278 SExtOpcode = X86::CDQ;
1281 LoReg = X86::RAX; HiReg = X86::RDX;
1282 ClrOpcode = X86::MOV64r0;
1283 SExtOpcode = X86::CQO;
1287 SDOperand N0 = Node->getOperand(0);
1288 SDOperand N1 = Node->getOperand(1);
1289 SDOperand InFlag(0, 0);
1290 if (NVT == MVT::i8 && !isSigned) {
1291 // Special case for div8, just use a move with zero extension to AX to
1292 // clear the upper 8 bits (AH).
1293 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1294 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1295 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1296 AddToISelQueue(N0.getOperand(0));
1297 AddToISelQueue(Tmp0);
1298 AddToISelQueue(Tmp1);
1299 AddToISelQueue(Tmp2);
1300 AddToISelQueue(Tmp3);
1302 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1304 Chain = Move.getValue(1);
1305 ReplaceUses(N0.getValue(1), Chain);
1309 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1310 Chain = CurDAG->getEntryNode();
1312 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, InFlag);
1313 InFlag = Chain.getValue(1);
1317 CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg, N0,
1318 InFlag).getValue(1);
1320 // Sign extend the low part into the high part.
1322 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1324 // Zero out the high part, effectively zero extending the input.
1325 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
1326 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg, ClrNode,
1327 InFlag).getValue(1);
1331 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Chain;
1332 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1334 AddToISelQueue(N1.getOperand(0));
1335 AddToISelQueue(Tmp0);
1336 AddToISelQueue(Tmp1);
1337 AddToISelQueue(Tmp2);
1338 AddToISelQueue(Tmp3);
1339 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
1341 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
1342 Chain = SDOperand(CNode, 0);
1343 InFlag = SDOperand(CNode, 1);
1346 Chain = CurDAG->getEntryNode();
1348 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1351 unsigned Reg = isDiv ? LoReg : HiReg;
1353 if (Reg == X86::AH && Subtarget->is64Bit()) {
1354 // Prevent use of AH in a REX instruction by referencing AX instead.
1355 // Shift it down 8 bits.
1356 Result = CurDAG->getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1357 Chain = Result.getValue(1);
1358 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1359 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1360 // Then truncate it down to i8.
1361 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1362 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1363 MVT::i8, Result, SRIdx), 0);
1365 Result = CurDAG->getCopyFromReg(Chain, Reg, NVT, InFlag);
1366 Chain = Result.getValue(1);
1368 ReplaceUses(N.getValue(0), Result);
1370 ReplaceUses(N1.getValue(1), Chain);
1373 DOUT << std::string(Indent-2, ' ') << "=> ";
1374 DEBUG(Result.Val->dump(CurDAG));
1382 case ISD::ANY_EXTEND: {
1383 SDOperand N0 = Node->getOperand(0);
1385 if (NVT == MVT::i64 || NVT == MVT::i32 || NVT == MVT::i16) {
1387 switch(N0.getValueType()) {
1389 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1392 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1395 if (Subtarget->is64Bit())
1396 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1398 default: assert(0 && "Unknown any_extend!");
1401 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG, NVT, N0, SRIdx);
1404 DOUT << std::string(Indent-2, ' ') << "=> ";
1405 DEBUG(ResNode->dump(CurDAG));
1410 } // Otherwise let generated ISel handle it.
1415 case ISD::SIGN_EXTEND_INREG: {
1416 SDOperand N0 = Node->getOperand(0);
1419 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1420 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
1424 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1425 else assert(0 && "Unknown sign_extend_inreg!");
1429 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1430 case MVT::i16: Opc = X86::MOVSX32rr16; break;
1431 default: assert(0 && "Unknown sign_extend_inreg!");
1436 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1437 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1438 case MVT::i32: Opc = X86::MOVSX64rr32; break;
1439 default: assert(0 && "Unknown sign_extend_inreg!");
1442 default: assert(0 && "Unknown sign_extend_inreg!");
1445 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1448 DOUT << std::string(Indent-2, ' ') << "=> ";
1449 DEBUG(TruncOp.Val->dump(CurDAG));
1451 DOUT << std::string(Indent-2, ' ') << "=> ";
1452 DEBUG(ResNode->dump(CurDAG));
1460 case ISD::TRUNCATE: {
1461 SDOperand Input = Node->getOperand(0);
1462 AddToISelQueue(Node->getOperand(0));
1463 SDNode *ResNode = getTruncate(Input, NVT);
1466 DOUT << std::string(Indent-2, ' ') << "=> ";
1467 DEBUG(ResNode->dump(CurDAG));
1476 SDNode *ResNode = SelectCode(N);
1479 DOUT << std::string(Indent-2, ' ') << "=> ";
1480 if (ResNode == NULL || ResNode == N.Val)
1481 DEBUG(N.Val->dump(CurDAG));
1483 DEBUG(ResNode->dump(CurDAG));
1491 bool X86DAGToDAGISel::
1492 SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1493 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1494 SDOperand Op0, Op1, Op2, Op3;
1495 switch (ConstraintCode) {
1496 case 'o': // offsetable ??
1497 case 'v': // not offsetable ??
1498 default: return true;
1500 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1505 OutOps.push_back(Op0);
1506 OutOps.push_back(Op1);
1507 OutOps.push_back(Op2);
1508 OutOps.push_back(Op3);
1509 AddToISelQueue(Op0);
1510 AddToISelQueue(Op1);
1511 AddToISelQueue(Op2);
1512 AddToISelQueue(Op3);
1516 /// createX86ISelDag - This pass converts a legalized DAG into a
1517 /// X86-specific DAG, ready for instruction scheduling.
1519 FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1520 return new X86DAGToDAGISel(TM, Fast);