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 SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
146 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
147 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
148 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
149 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
150 SDOperand N, SDOperand &Base, SDOperand &Scale,
151 SDOperand &Index, SDOperand &Disp,
152 SDOperand &InChain, SDOperand &OutChain);
153 bool TryFoldLoad(SDOperand P, SDOperand N,
154 SDOperand &Base, SDOperand &Scale,
155 SDOperand &Index, SDOperand &Disp);
156 void InstructionSelectPreprocess(SelectionDAG &DAG);
158 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
159 /// inline asm expressions.
160 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
162 std::vector<SDOperand> &OutOps,
165 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
167 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
168 SDOperand &Scale, SDOperand &Index,
170 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
171 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
173 Scale = getI8Imm(AM.Scale);
175 // These are 32-bit even in 64-bit mode since RIP relative offset
178 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
180 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
182 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
183 else if (AM.JT != -1)
184 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
186 Disp = getI32Imm(AM.Disp);
189 /// getI8Imm - Return a target constant with the specified value, of type
191 inline SDOperand getI8Imm(unsigned Imm) {
192 return CurDAG->getTargetConstant(Imm, MVT::i8);
195 /// getI16Imm - Return a target constant with the specified value, of type
197 inline SDOperand getI16Imm(unsigned Imm) {
198 return CurDAG->getTargetConstant(Imm, MVT::i16);
201 /// getI32Imm - Return a target constant with the specified value, of type
203 inline SDOperand getI32Imm(unsigned Imm) {
204 return CurDAG->getTargetConstant(Imm, MVT::i32);
207 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
208 /// base register. Return the virtual register that holds this value.
209 SDNode *getGlobalBaseReg();
211 /// getTruncate - return an SDNode that implements a subreg based truncate
212 /// of the specified operand to the the specified value type.
213 SDNode *getTruncate(SDOperand N0, MVT::ValueType VT);
221 static SDNode *findFlagUse(SDNode *N) {
222 unsigned FlagResNo = N->getNumValues()-1;
223 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
225 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
226 SDOperand Op = User->getOperand(i);
227 if (Op.Val == N && Op.ResNo == FlagResNo)
234 static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
235 SDNode *Root, SDNode *Skip, bool &found,
236 std::set<SDNode *> &Visited) {
238 Use->getNodeId() > Def->getNodeId() ||
239 !Visited.insert(Use).second)
242 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
243 SDNode *N = Use->getOperand(i).Val;
248 continue; // Immediate use is ok.
250 assert(Use->getOpcode() == ISD::STORE ||
251 Use->getOpcode() == X86ISD::CMP);
257 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
261 /// isNonImmUse - Start searching from Root up the DAG to check is Def can
262 /// be reached. Return true if that's the case. However, ignore direct uses
263 /// by ImmedUse (which would be U in the example illustrated in
264 /// CanBeFoldedBy) and by Root (which can happen in the store case).
265 /// FIXME: to be really generic, we should allow direct use by any node
266 /// that is being folded. But realisticly since we only fold loads which
267 /// have one non-chain use, we only need to watch out for load/op/store
268 /// and load/op/cmp case where the root (store / cmp) may reach the load via
269 /// its chain operand.
270 static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
271 SDNode *Skip = NULL) {
272 std::set<SDNode *> Visited;
274 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
279 bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
280 if (FastISel) return false;
282 // If U use can somehow reach N through another path then U can't fold N or
283 // it will create a cycle. e.g. In the following diagram, U can reach N
284 // through X. If N is folded into into U, then X is both a predecessor and
295 if (isNonImmUse(Root, N, U))
298 // If U produces a flag, then it gets (even more) interesting. Since it
299 // would have been "glued" together with its flag use, we need to check if
312 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
313 // NU), then TF is a predecessor of FU and a successor of NU. But since
314 // NU and FU are flagged together, this effectively creates a cycle.
315 bool HasFlagUse = false;
316 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
317 while ((VT == MVT::Flag && !Root->use_empty())) {
318 SDNode *FU = findFlagUse(Root);
325 VT = Root->getValueType(Root->getNumValues()-1);
329 return !isNonImmUse(Root, N, Root, U);
333 /// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
334 /// and move load below the TokenFactor. Replace store's chain operand with
335 /// load's chain result.
336 static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
337 SDOperand Store, SDOperand TF) {
338 std::vector<SDOperand> Ops;
339 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
340 if (Load.Val == TF.Val->getOperand(i).Val)
341 Ops.push_back(Load.Val->getOperand(0));
343 Ops.push_back(TF.Val->getOperand(i));
344 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
345 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
346 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
347 Store.getOperand(2), Store.getOperand(3));
350 /// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
351 /// selector to pick more load-modify-store instructions. This is a common
362 /// [TokenFactor] [Op]
369 /// The fact the store's chain operand != load's chain will prevent the
370 /// (store (op (load))) instruction from being selected. We can transform it to:
389 void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
390 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
391 E = DAG.allnodes_end(); I != E; ++I) {
392 if (!ISD::isNON_TRUNCStore(I))
394 SDOperand Chain = I->getOperand(0);
395 if (Chain.Val->getOpcode() != ISD::TokenFactor)
398 SDOperand N1 = I->getOperand(1);
399 SDOperand N2 = I->getOperand(2);
400 if (MVT::isFloatingPoint(N1.getValueType()) ||
401 MVT::isVector(N1.getValueType()) ||
407 unsigned Opcode = N1.Val->getOpcode();
416 SDOperand N10 = N1.getOperand(0);
417 SDOperand N11 = N1.getOperand(1);
418 if (ISD::isNON_EXTLoad(N10.Val))
420 else if (ISD::isNON_EXTLoad(N11.Val)) {
424 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
425 (N10.getOperand(1) == N2) &&
426 (N10.Val->getValueType(0) == N1.getValueType());
441 SDOperand N10 = N1.getOperand(0);
442 if (ISD::isNON_EXTLoad(N10.Val))
443 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
444 (N10.getOperand(1) == N2) &&
445 (N10.Val->getValueType(0) == N1.getValueType());
453 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
459 /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
460 /// when it has created a SelectionDAG for us to codegen.
461 void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
463 MachineFunction::iterator FirstMBB = BB;
466 InstructionSelectPreprocess(DAG);
468 // Codegen the basic block.
470 DOUT << "===== Instruction selection begins:\n";
473 DAG.setRoot(SelectRoot(DAG.getRoot()));
475 DOUT << "===== Instruction selection ends:\n";
478 DAG.RemoveDeadNodes();
480 // Emit machine code to BB.
481 ScheduleAndEmitDAG(DAG);
483 // If we are emitting FP stack code, scan the basic block to determine if this
484 // block defines any FP values. If so, put an FP_REG_KILL instruction before
485 // the terminator of the block.
487 // Note that FP stack instructions *are* used in SSE code for long double,
488 // so we do need this check.
489 bool ContainsFPCode = false;
491 // Scan all of the machine instructions in these MBBs, checking for FP
492 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
493 MachineFunction::iterator MBBI = FirstMBB;
495 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
496 !ContainsFPCode && I != E; ++I) {
497 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
498 const TargetRegisterClass *clas;
499 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
500 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
501 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
502 ((clas = RegMap->getRegClass(I->getOperand(0).getReg())) ==
503 X86::RFP32RegisterClass ||
504 clas == X86::RFP64RegisterClass ||
505 clas == X86::RFP80RegisterClass)) {
506 ContainsFPCode = true;
512 } while (!ContainsFPCode && &*(MBBI++) != BB);
514 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
515 // a copy of the input value in this block. In SSE mode, we only care about
517 if (!ContainsFPCode) {
518 // Final check, check LLVM BB's that are successors to the LLVM BB
519 // corresponding to BB for FP PHI nodes.
520 const BasicBlock *LLVMBB = BB->getBasicBlock();
522 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
523 !ContainsFPCode && SI != E; ++SI) {
524 for (BasicBlock::const_iterator II = SI->begin();
525 (PN = dyn_cast<PHINode>(II)); ++II) {
526 if (PN->getType()==Type::X86_FP80Ty ||
527 (!Subtarget->hasSSE2() && PN->getType()->isFloatingPoint())) {
528 ContainsFPCode = true;
535 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
536 if (ContainsFPCode) {
537 BuildMI(*BB, BB->getFirstTerminator(),
538 TM.getInstrInfo()->get(X86::FP_REG_KILL));
543 /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
544 /// the main function.
545 void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
546 MachineFrameInfo *MFI) {
547 const TargetInstrInfo *TII = TM.getInstrInfo();
548 if (Subtarget->isTargetCygMing())
549 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
551 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
552 int CWFrameIdx = MFI->CreateStackObject(2, 2);
553 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
555 // Set the high part to be 64-bit precision.
556 addFrameReference(BuildMI(BB, TII->get(X86::MOV8mi)),
557 CWFrameIdx, 1).addImm(2);
559 // Reload the modified control word now.
560 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
563 void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
564 // If this is main, emit special code for main.
565 MachineBasicBlock *BB = MF.begin();
566 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
567 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
570 /// MatchAddress - Add the specified node to the specified addressing mode,
571 /// returning true if it cannot be done. This just pattern matches for the
573 bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
574 bool isRoot, unsigned Depth) {
576 // Default, generate it as a register.
577 AM.BaseType = X86ISelAddressMode::RegBase;
582 // RIP relative addressing: %rip + 32-bit displacement!
584 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
585 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
586 if (isInt32(AM.Disp + Val)) {
594 int id = N.Val->getNodeId();
595 bool Available = isSelected(id);
597 switch (N.getOpcode()) {
599 case ISD::Constant: {
600 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
601 if (isInt32(AM.Disp + Val)) {
608 case X86ISD::Wrapper: {
609 bool is64Bit = Subtarget->is64Bit();
610 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
611 if (is64Bit && TM.getCodeModel() != CodeModel::Small)
613 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
615 // If value is available in a register both base and index components have
616 // been picked, we can't fit the result available in the register in the
617 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
618 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
619 bool isStatic = TM.getRelocationModel() == Reloc::Static;
620 SDOperand N0 = N.getOperand(0);
621 // Mac OS X X86-64 lower 4G address is not available.
622 bool isAbs32 = !is64Bit ||
623 (isStatic && Subtarget->hasLow4GUserSpaceAddress());
624 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
625 GlobalValue *GV = G->getGlobal();
626 if (isAbs32 || isRoot) {
628 AM.Disp += G->getOffset();
629 AM.isRIPRel = !isAbs32;
632 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
633 if (isAbs32 || isRoot) {
634 AM.CP = CP->getConstVal();
635 AM.Align = CP->getAlignment();
636 AM.Disp += CP->getOffset();
637 AM.isRIPRel = !isAbs32;
640 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
641 if (isAbs32 || isRoot) {
642 AM.ES = S->getSymbol();
643 AM.isRIPRel = !isAbs32;
646 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
647 if (isAbs32 || isRoot) {
648 AM.JT = J->getIndex();
649 AM.isRIPRel = !isAbs32;
657 case ISD::FrameIndex:
658 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
659 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
660 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
666 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
667 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
668 unsigned Val = CN->getValue();
669 if (Val == 1 || Val == 2 || Val == 3) {
671 SDOperand ShVal = N.Val->getOperand(0);
673 // Okay, we know that we have a scale by now. However, if the scaled
674 // value is an add of something and a constant, we can fold the
675 // constant into the disp field here.
676 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
677 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
678 AM.IndexReg = ShVal.Val->getOperand(0);
679 ConstantSDNode *AddVal =
680 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
681 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
695 // X*[3,5,9] -> X+X*[2,4,8]
697 AM.BaseType == X86ISelAddressMode::RegBase &&
698 AM.Base.Reg.Val == 0 &&
699 AM.IndexReg.Val == 0) {
700 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
701 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
702 AM.Scale = unsigned(CN->getValue())-1;
704 SDOperand MulVal = N.Val->getOperand(0);
707 // Okay, we know that we have a scale by now. However, if the scaled
708 // value is an add of something and a constant, we can fold the
709 // constant into the disp field here.
710 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
711 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
712 Reg = MulVal.Val->getOperand(0);
713 ConstantSDNode *AddVal =
714 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
715 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
719 Reg = N.Val->getOperand(0);
721 Reg = N.Val->getOperand(0);
724 AM.IndexReg = AM.Base.Reg = Reg;
732 X86ISelAddressMode Backup = AM;
733 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
734 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
737 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
738 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
745 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
747 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
748 X86ISelAddressMode Backup = AM;
749 // Start with the LHS as an addr mode.
750 if (!MatchAddress(N.getOperand(0), AM, false) &&
751 // Address could not have picked a GV address for the displacement.
753 // On x86-64, the resultant disp must fit in 32-bits.
754 isInt32(AM.Disp + CN->getSignExtended()) &&
755 // Check to see if the LHS & C is zero.
756 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getValue())) {
757 AM.Disp += CN->getValue();
766 // Is the base register already occupied?
767 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
768 // If so, check to see if the scale index register is set.
769 if (AM.IndexReg.Val == 0) {
775 // Otherwise, we cannot select it.
779 // Default, generate it as a register.
780 AM.BaseType = X86ISelAddressMode::RegBase;
785 /// SelectAddr - returns true if it is able pattern match an addressing mode.
786 /// It returns the operands which make up the maximal addressing mode it can
787 /// match by reference.
788 bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
789 SDOperand &Scale, SDOperand &Index,
791 X86ISelAddressMode AM;
792 if (MatchAddress(N, AM))
795 MVT::ValueType VT = N.getValueType();
796 if (AM.BaseType == X86ISelAddressMode::RegBase) {
797 if (!AM.Base.Reg.Val)
798 AM.Base.Reg = CurDAG->getRegister(0, VT);
801 if (!AM.IndexReg.Val)
802 AM.IndexReg = CurDAG->getRegister(0, VT);
804 getAddressOperands(AM, Base, Scale, Index, Disp);
808 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
810 static inline bool isZeroNode(SDOperand Elt) {
811 return ((isa<ConstantSDNode>(Elt) &&
812 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
813 (isa<ConstantFPSDNode>(Elt) &&
814 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
818 /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
819 /// match a load whose top elements are either undef or zeros. The load flavor
820 /// is derived from the type of N, which is either v4f32 or v2f64.
821 bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
822 SDOperand N, SDOperand &Base,
823 SDOperand &Scale, SDOperand &Index,
824 SDOperand &Disp, SDOperand &InChain,
825 SDOperand &OutChain) {
826 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
827 InChain = N.getOperand(0).getValue(1);
828 if (ISD::isNON_EXTLoad(InChain.Val) &&
829 InChain.getValue(0).hasOneUse() &&
831 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
832 LoadSDNode *LD = cast<LoadSDNode>(InChain);
833 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
835 OutChain = LD->getChain();
840 // Also handle the case where we explicitly require zeros in the top
841 // elements. This is a vector shuffle from the zero vector.
842 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
843 N.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
844 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
845 N.getOperand(1).Val->hasOneUse() &&
846 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
847 N.getOperand(1).getOperand(0).hasOneUse()) {
848 // Check to see if the BUILD_VECTOR is building a zero vector.
849 SDOperand BV = N.getOperand(0);
850 for (unsigned i = 0, e = BV.getNumOperands(); i != e; ++i)
851 if (!isZeroNode(BV.getOperand(i)) &&
852 BV.getOperand(i).getOpcode() != ISD::UNDEF)
853 return false; // Not a zero/undef vector.
854 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
856 unsigned VecWidth = BV.getNumOperands();
857 SDOperand ShufMask = N.getOperand(2);
858 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
859 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
860 if (C->getValue() == VecWidth) {
861 for (unsigned i = 1; i != VecWidth; ++i) {
862 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
865 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
866 if (C->getValue() >= VecWidth) return false;
871 // Okay, this is a zero extending load. Fold it.
872 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
873 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
875 OutChain = LD->getChain();
876 InChain = SDOperand(LD, 1);
884 /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
885 /// mode it matches can be cost effectively emitted as an LEA instruction.
886 bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
887 SDOperand &Base, SDOperand &Scale,
888 SDOperand &Index, SDOperand &Disp) {
889 X86ISelAddressMode AM;
890 if (MatchAddress(N, AM))
893 MVT::ValueType VT = N.getValueType();
894 unsigned Complexity = 0;
895 if (AM.BaseType == X86ISelAddressMode::RegBase)
899 AM.Base.Reg = CurDAG->getRegister(0, VT);
900 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
906 AM.IndexReg = CurDAG->getRegister(0, VT);
908 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
913 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
914 // to a LEA. This is determined with some expermentation but is by no means
915 // optimal (especially for code size consideration). LEA is nice because of
916 // its three-address nature. Tweak the cost function again when we can run
917 // convertToThreeAddress() at register allocation time.
918 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
919 // For X86-64, we should always use lea to materialize RIP relative
921 if (Subtarget->is64Bit())
927 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
930 if (Complexity > 2) {
931 getAddressOperands(AM, Base, Scale, Index, Disp);
937 bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
938 SDOperand &Base, SDOperand &Scale,
939 SDOperand &Index, SDOperand &Disp) {
940 if (ISD::isNON_EXTLoad(N.Val) &&
942 CanBeFoldedBy(N.Val, P.Val, P.Val))
943 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
947 /// getGlobalBaseReg - Output the instructions required to put the
948 /// base address to use for accessing globals into a register.
950 SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
951 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
952 if (!GlobalBaseReg) {
953 // Insert the set of GlobalBaseReg into the first MBB of the function
954 MachineBasicBlock &FirstMBB = BB->getParent()->front();
955 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
956 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
957 unsigned PC = RegMap->createVirtualRegister(X86::GR32RegisterClass);
959 const TargetInstrInfo *TII = TM.getInstrInfo();
960 BuildMI(FirstMBB, MBBI, TII->get(X86::MovePCtoStack));
961 BuildMI(FirstMBB, MBBI, TII->get(X86::POP32r), PC);
963 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
964 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
965 if (TM.getRelocationModel() == Reloc::PIC_ &&
966 Subtarget->isPICStyleGOT()) {
967 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
968 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg).
970 addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
976 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
979 static SDNode *FindCallStartFromCall(SDNode *Node) {
980 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
981 assert(Node->getOperand(0).getValueType() == MVT::Other &&
982 "Node doesn't have a token chain argument!");
983 return FindCallStartFromCall(Node->getOperand(0).Val);
986 SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) {
990 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
991 // Ensure that the source register has an 8-bit subreg on 32-bit targets
992 if (!Subtarget->is64Bit()) {
995 switch (N0.getValueType()) {
996 default: assert(0 && "Unknown truncate!");
998 Opc = X86::MOV16to16_;
1002 Opc = X86::MOV32to32_;
1007 SDOperand(CurDAG->getTargetNode(Opc, VT, N0), 0);
1011 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1014 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1016 default: assert(0 && "Unknown truncate!");
1018 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1024 SDNode *X86DAGToDAGISel::Select(SDOperand N) {
1025 SDNode *Node = N.Val;
1026 MVT::ValueType NVT = Node->getValueType(0);
1028 unsigned Opcode = Node->getOpcode();
1031 DOUT << std::string(Indent, ' ') << "Selecting: ";
1032 DEBUG(Node->dump(CurDAG));
1037 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
1039 DOUT << std::string(Indent-2, ' ') << "== ";
1040 DEBUG(Node->dump(CurDAG));
1044 return NULL; // Already selected.
1049 case X86ISD::GlobalBaseReg:
1050 return getGlobalBaseReg();
1053 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1054 // code and is matched first so to prevent it from being turned into
1056 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
1057 MVT::ValueType PtrVT = TLI.getPointerTy();
1058 SDOperand N0 = N.getOperand(0);
1059 SDOperand N1 = N.getOperand(1);
1060 if (N.Val->getValueType(0) == PtrVT &&
1061 N0.getOpcode() == X86ISD::Wrapper &&
1062 N1.getOpcode() == ISD::Constant) {
1063 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1065 // TODO: handle ExternalSymbolSDNode.
1066 if (GlobalAddressSDNode *G =
1067 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
1068 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
1069 G->getOffset() + Offset);
1070 } else if (ConstantPoolSDNode *CP =
1071 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
1072 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
1074 CP->getOffset()+Offset);
1078 if (Subtarget->is64Bit()) {
1079 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1080 CurDAG->getRegister(0, PtrVT), C };
1081 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1083 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1087 // Other cases are handled by auto-generated code.
1092 if (NVT == MVT::i8) {
1093 SDOperand N0 = Node->getOperand(0);
1094 SDOperand N1 = Node->getOperand(1);
1095 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1096 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1098 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
1105 SDOperand Chain = N1.getOperand(0);
1107 AddToISelQueue(Chain);
1108 AddToISelQueue(Tmp0);
1109 AddToISelQueue(Tmp1);
1110 AddToISelQueue(Tmp2);
1111 AddToISelQueue(Tmp3);
1112 SDOperand InFlag(0, 0);
1113 Chain = CurDAG->getCopyToReg(Chain, X86::AL, N0, InFlag);
1114 InFlag = Chain.getValue(1);
1115 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
1116 ResNode = CurDAG->getTargetNode(X86::MUL8m, MVT::i8, MVT::i8,
1117 MVT::Other, Ops, 6);
1118 ReplaceUses(N1.getValue(1), SDOperand(ResNode, 2));
1120 SDOperand Chain = CurDAG->getEntryNode();
1123 SDOperand InFlag(0, 0);
1124 InFlag = CurDAG->getCopyToReg(Chain, X86::AL, N0, InFlag).getValue(1);
1125 ResNode = CurDAG->getTargetNode(X86::MUL8r, MVT::i8, MVT::i8,
1129 ReplaceUses(N.getValue(0), SDOperand(ResNode, 0));
1137 if (Opcode == ISD::MULHU)
1139 default: assert(0 && "Unsupported VT!");
1140 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1141 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1142 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1143 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1147 default: assert(0 && "Unsupported VT!");
1148 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1149 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1150 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1151 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1154 unsigned LoReg, HiReg;
1156 default: assert(0 && "Unsupported VT!");
1157 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1158 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1159 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1160 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1163 SDOperand N0 = Node->getOperand(0);
1164 SDOperand N1 = Node->getOperand(1);
1166 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1167 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1168 // MULHU and MULHS are commmutative
1170 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
1177 Chain = N1.getOperand(0);
1178 AddToISelQueue(Chain);
1180 Chain = CurDAG->getEntryNode();
1182 SDOperand InFlag(0, 0);
1184 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
1186 InFlag = Chain.getValue(1);
1189 AddToISelQueue(Tmp0);
1190 AddToISelQueue(Tmp1);
1191 AddToISelQueue(Tmp2);
1192 AddToISelQueue(Tmp3);
1193 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
1195 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
1196 Chain = SDOperand(CNode, 0);
1197 InFlag = SDOperand(CNode, 1);
1201 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1205 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1206 // Prevent use of AH in a REX instruction by referencing AX instead.
1207 // Shift it down 8 bits.
1208 Result = CurDAG->getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1209 Chain = Result.getValue(1);
1210 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1211 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1212 // Then truncate it down to i8.
1213 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1214 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1215 MVT::i8, Result, SRIdx), 0);
1217 Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
1219 ReplaceUses(N.getValue(0), Result);
1221 ReplaceUses(N1.getValue(1), Result.getValue(1));
1224 DOUT << std::string(Indent-2, ' ') << "=> ";
1225 DEBUG(Result.Val->dump(CurDAG));
1236 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
1237 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
1240 default: assert(0 && "Unsupported VT!");
1241 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1242 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1243 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1244 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1248 default: assert(0 && "Unsupported VT!");
1249 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1250 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1251 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1252 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1255 unsigned LoReg, HiReg;
1256 unsigned ClrOpcode, SExtOpcode;
1258 default: assert(0 && "Unsupported VT!");
1260 LoReg = X86::AL; HiReg = X86::AH;
1262 SExtOpcode = X86::CBW;
1265 LoReg = X86::AX; HiReg = X86::DX;
1266 ClrOpcode = X86::MOV16r0;
1267 SExtOpcode = X86::CWD;
1270 LoReg = X86::EAX; HiReg = X86::EDX;
1271 ClrOpcode = X86::MOV32r0;
1272 SExtOpcode = X86::CDQ;
1275 LoReg = X86::RAX; HiReg = X86::RDX;
1276 ClrOpcode = X86::MOV64r0;
1277 SExtOpcode = X86::CQO;
1281 SDOperand N0 = Node->getOperand(0);
1282 SDOperand N1 = Node->getOperand(1);
1283 SDOperand InFlag(0, 0);
1284 if (NVT == MVT::i8 && !isSigned) {
1285 // Special case for div8, just use a move with zero extension to AX to
1286 // clear the upper 8 bits (AH).
1287 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1288 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1289 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1290 AddToISelQueue(N0.getOperand(0));
1291 AddToISelQueue(Tmp0);
1292 AddToISelQueue(Tmp1);
1293 AddToISelQueue(Tmp2);
1294 AddToISelQueue(Tmp3);
1296 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1298 Chain = Move.getValue(1);
1299 ReplaceUses(N0.getValue(1), Chain);
1303 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1304 Chain = CurDAG->getEntryNode();
1306 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, InFlag);
1307 InFlag = Chain.getValue(1);
1311 CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg, N0,
1312 InFlag).getValue(1);
1314 // Sign extend the low part into the high part.
1316 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1318 // Zero out the high part, effectively zero extending the input.
1319 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
1320 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg, ClrNode,
1321 InFlag).getValue(1);
1325 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Chain;
1326 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1328 AddToISelQueue(N1.getOperand(0));
1329 AddToISelQueue(Tmp0);
1330 AddToISelQueue(Tmp1);
1331 AddToISelQueue(Tmp2);
1332 AddToISelQueue(Tmp3);
1333 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
1335 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
1336 Chain = SDOperand(CNode, 0);
1337 InFlag = SDOperand(CNode, 1);
1340 Chain = CurDAG->getEntryNode();
1342 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1345 unsigned Reg = isDiv ? LoReg : HiReg;
1347 if (Reg == X86::AH && Subtarget->is64Bit()) {
1348 // Prevent use of AH in a REX instruction by referencing AX instead.
1349 // Shift it down 8 bits.
1350 Result = CurDAG->getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1351 Chain = Result.getValue(1);
1352 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1353 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1354 // Then truncate it down to i8.
1355 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1356 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1357 MVT::i8, Result, SRIdx), 0);
1359 Result = CurDAG->getCopyFromReg(Chain, Reg, NVT, InFlag);
1360 Chain = Result.getValue(1);
1362 ReplaceUses(N.getValue(0), Result);
1364 ReplaceUses(N1.getValue(1), Chain);
1367 DOUT << std::string(Indent-2, ' ') << "=> ";
1368 DEBUG(Result.Val->dump(CurDAG));
1376 case ISD::ANY_EXTEND: {
1377 SDOperand N0 = Node->getOperand(0);
1379 if (NVT == MVT::i64 || NVT == MVT::i32 || NVT == MVT::i16) {
1381 switch(N0.getValueType()) {
1383 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1386 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1389 if (Subtarget->is64Bit())
1390 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1392 default: assert(0 && "Unknown any_extend!");
1395 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG, NVT, N0, SRIdx);
1398 DOUT << std::string(Indent-2, ' ') << "=> ";
1399 DEBUG(ResNode->dump(CurDAG));
1404 } // Otherwise let generated ISel handle it.
1409 case ISD::SIGN_EXTEND_INREG: {
1410 SDOperand N0 = Node->getOperand(0);
1413 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1414 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
1418 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1419 else assert(0 && "Unknown sign_extend_inreg!");
1423 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1424 case MVT::i16: Opc = X86::MOVSX32rr16; break;
1425 default: assert(0 && "Unknown sign_extend_inreg!");
1430 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1431 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1432 case MVT::i32: Opc = X86::MOVSX64rr32; break;
1433 default: assert(0 && "Unknown sign_extend_inreg!");
1436 default: assert(0 && "Unknown sign_extend_inreg!");
1439 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1442 DOUT << std::string(Indent-2, ' ') << "=> ";
1443 DEBUG(TruncOp.Val->dump(CurDAG));
1445 DOUT << std::string(Indent-2, ' ') << "=> ";
1446 DEBUG(ResNode->dump(CurDAG));
1454 case ISD::TRUNCATE: {
1455 SDOperand Input = Node->getOperand(0);
1456 AddToISelQueue(Node->getOperand(0));
1457 SDNode *ResNode = getTruncate(Input, NVT);
1460 DOUT << std::string(Indent-2, ' ') << "=> ";
1461 DEBUG(ResNode->dump(CurDAG));
1470 SDNode *ResNode = SelectCode(N);
1473 DOUT << std::string(Indent-2, ' ') << "=> ";
1474 if (ResNode == NULL || ResNode == N.Val)
1475 DEBUG(N.Val->dump(CurDAG));
1477 DEBUG(ResNode->dump(CurDAG));
1485 bool X86DAGToDAGISel::
1486 SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1487 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1488 SDOperand Op0, Op1, Op2, Op3;
1489 switch (ConstraintCode) {
1490 case 'o': // offsetable ??
1491 case 'v': // not offsetable ??
1492 default: return true;
1494 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1499 OutOps.push_back(Op0);
1500 OutOps.push_back(Op1);
1501 OutOps.push_back(Op2);
1502 OutOps.push_back(Op3);
1503 AddToISelQueue(Op0);
1504 AddToISelQueue(Op1);
1505 AddToISelQueue(Op2);
1506 AddToISelQueue(Op3);
1510 /// createX86ISelDag - This pass converts a legalized DAG into a
1511 /// X86-specific DAG, ready for instruction scheduling.
1513 FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1514 return new X86DAGToDAGISel(TM, Fast);