1 //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 "X86MachineFunctionInfo.h"
20 #include "X86RegisterInfo.h"
21 #include "X86Subtarget.h"
22 #include "X86TargetMachine.h"
23 #include "llvm/GlobalValue.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Intrinsics.h"
26 #include "llvm/Support/CFG.h"
27 #include "llvm/Type.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineRegisterInfo.h"
33 #include "llvm/CodeGen/SelectionDAGISel.h"
34 #include "llvm/Target/TargetMachine.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/ADT/Statistic.h"
43 STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
44 STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
47 //===----------------------------------------------------------------------===//
48 // Pattern Matcher Implementation
49 //===----------------------------------------------------------------------===//
52 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
53 /// SDOperand's instead of register numbers for the leaves of the matched
55 struct X86ISelAddressMode {
61 struct { // This is really a union, discriminated by BaseType!
66 bool isRIPRel; // RIP relative?
74 unsigned Align; // CP alignment.
77 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
78 GV(0), CP(0), ES(0), JT(-1), Align(0) {
84 //===--------------------------------------------------------------------===//
85 /// ISel - X86 specific code to select X86 machine instructions for
86 /// SelectionDAG operations.
88 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
89 /// ContainsFPCode - Every instruction we select that uses or defines a FP
90 /// register should set this to true.
93 /// FastISel - Enable fast(er) instruction selection.
97 /// TM - Keep a reference to X86TargetMachine.
101 /// X86Lowering - This object fully describes how to lower LLVM code to an
102 /// X86-specific SelectionDAG.
103 X86TargetLowering X86Lowering;
105 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
106 /// make the right decision when generating code for different targets.
107 const X86Subtarget *Subtarget;
109 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
111 unsigned GlobalBaseReg;
114 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
115 : SelectionDAGISel(X86Lowering),
116 ContainsFPCode(false), FastISel(fast), TM(tm),
117 X86Lowering(*TM.getTargetLowering()),
118 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
120 virtual bool runOnFunction(Function &Fn) {
121 // Make sure we re-emit a set of the global base reg if necessary
123 return SelectionDAGISel::runOnFunction(Fn);
126 virtual const char *getPassName() const {
127 return "X86 DAG->DAG Instruction Selection";
130 /// InstructionSelectBasicBlock - This callback is invoked by
131 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
132 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
134 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
136 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
138 // Include the pieces autogenerated from the target description.
139 #include "X86GenDAGISel.inc"
142 SDNode *Select(SDOperand N);
144 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM,
145 bool isRoot = true, unsigned Depth = 0);
146 bool MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
147 bool isRoot, unsigned Depth);
148 bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
149 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
150 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
151 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
152 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
153 SDOperand N, SDOperand &Base, SDOperand &Scale,
154 SDOperand &Index, SDOperand &Disp,
155 SDOperand &InChain, SDOperand &OutChain);
156 bool TryFoldLoad(SDOperand P, SDOperand N,
157 SDOperand &Base, SDOperand &Scale,
158 SDOperand &Index, SDOperand &Disp);
159 void InstructionSelectPreprocess(SelectionDAG &DAG);
161 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
162 /// inline asm expressions.
163 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
165 std::vector<SDOperand> &OutOps,
168 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
170 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
171 SDOperand &Scale, SDOperand &Index,
173 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
174 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
176 Scale = getI8Imm(AM.Scale);
178 // These are 32-bit even in 64-bit mode since RIP relative offset
181 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
183 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
185 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
186 else if (AM.JT != -1)
187 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
189 Disp = getI32Imm(AM.Disp);
192 /// getI8Imm - Return a target constant with the specified value, of type
194 inline SDOperand getI8Imm(unsigned Imm) {
195 return CurDAG->getTargetConstant(Imm, MVT::i8);
198 /// getI16Imm - Return a target constant with the specified value, of type
200 inline SDOperand getI16Imm(unsigned Imm) {
201 return CurDAG->getTargetConstant(Imm, MVT::i16);
204 /// getI32Imm - Return a target constant with the specified value, of type
206 inline SDOperand getI32Imm(unsigned Imm) {
207 return CurDAG->getTargetConstant(Imm, MVT::i32);
210 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
211 /// base register. Return the virtual register that holds this value.
212 SDNode *getGlobalBaseReg();
214 /// getTruncate - return an SDNode that implements a subreg based truncate
215 /// of the specified operand to the the specified value type.
216 SDNode *getTruncate(SDOperand N0, MVT::ValueType VT);
224 static SDNode *findFlagUse(SDNode *N) {
225 unsigned FlagResNo = N->getNumValues()-1;
226 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
228 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
229 SDOperand Op = User->getOperand(i);
230 if (Op.Val == N && Op.ResNo == FlagResNo)
237 static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
238 SDNode *Root, SDNode *Skip, bool &found,
239 std::set<SDNode *> &Visited) {
241 Use->getNodeId() > Def->getNodeId() ||
242 !Visited.insert(Use).second)
245 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
246 SDNode *N = Use->getOperand(i).Val;
251 continue; // Immediate use is ok.
253 assert(Use->getOpcode() == ISD::STORE ||
254 Use->getOpcode() == X86ISD::CMP);
260 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
264 /// isNonImmUse - Start searching from Root up the DAG to check is Def can
265 /// be reached. Return true if that's the case. However, ignore direct uses
266 /// by ImmedUse (which would be U in the example illustrated in
267 /// CanBeFoldedBy) and by Root (which can happen in the store case).
268 /// FIXME: to be really generic, we should allow direct use by any node
269 /// that is being folded. But realisticly since we only fold loads which
270 /// have one non-chain use, we only need to watch out for load/op/store
271 /// and load/op/cmp case where the root (store / cmp) may reach the load via
272 /// its chain operand.
273 static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
274 SDNode *Skip = NULL) {
275 std::set<SDNode *> Visited;
277 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
282 bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
283 if (FastISel) return false;
285 // If U use can somehow reach N through another path then U can't fold N or
286 // it will create a cycle. e.g. In the following diagram, U can reach N
287 // through X. If N is folded into into U, then X is both a predecessor and
298 if (isNonImmUse(Root, N, U))
301 // If U produces a flag, then it gets (even more) interesting. Since it
302 // would have been "glued" together with its flag use, we need to check if
315 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
316 // NU), then TF is a predecessor of FU and a successor of NU. But since
317 // NU and FU are flagged together, this effectively creates a cycle.
318 bool HasFlagUse = false;
319 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
320 while ((VT == MVT::Flag && !Root->use_empty())) {
321 SDNode *FU = findFlagUse(Root);
328 VT = Root->getValueType(Root->getNumValues()-1);
332 return !isNonImmUse(Root, N, Root, U);
336 /// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
337 /// and move load below the TokenFactor. Replace store's chain operand with
338 /// load's chain result.
339 static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
340 SDOperand Store, SDOperand TF) {
341 std::vector<SDOperand> Ops;
342 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
343 if (Load.Val == TF.Val->getOperand(i).Val)
344 Ops.push_back(Load.Val->getOperand(0));
346 Ops.push_back(TF.Val->getOperand(i));
347 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
348 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
349 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
350 Store.getOperand(2), Store.getOperand(3));
353 /// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
354 /// selector to pick more load-modify-store instructions. This is a common
365 /// [TokenFactor] [Op]
372 /// The fact the store's chain operand != load's chain will prevent the
373 /// (store (op (load))) instruction from being selected. We can transform it to:
392 void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
393 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
394 E = DAG.allnodes_end(); I != E; ++I) {
395 if (!ISD::isNON_TRUNCStore(I))
397 SDOperand Chain = I->getOperand(0);
398 if (Chain.Val->getOpcode() != ISD::TokenFactor)
401 SDOperand N1 = I->getOperand(1);
402 SDOperand N2 = I->getOperand(2);
403 if (MVT::isFloatingPoint(N1.getValueType()) ||
404 MVT::isVector(N1.getValueType()) ||
410 unsigned Opcode = N1.Val->getOpcode();
419 SDOperand N10 = N1.getOperand(0);
420 SDOperand N11 = N1.getOperand(1);
421 if (ISD::isNON_EXTLoad(N10.Val))
423 else if (ISD::isNON_EXTLoad(N11.Val)) {
427 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
428 (N10.getOperand(1) == N2) &&
429 (N10.Val->getValueType(0) == N1.getValueType());
444 SDOperand N10 = N1.getOperand(0);
445 if (ISD::isNON_EXTLoad(N10.Val))
446 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
447 (N10.getOperand(1) == N2) &&
448 (N10.Val->getValueType(0) == N1.getValueType());
456 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
462 /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
463 /// when it has created a SelectionDAG for us to codegen.
464 void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
466 MachineFunction::iterator FirstMBB = BB;
469 InstructionSelectPreprocess(DAG);
471 // Codegen the basic block.
473 DOUT << "===== Instruction selection begins:\n";
476 DAG.setRoot(SelectRoot(DAG.getRoot()));
478 DOUT << "===== Instruction selection ends:\n";
481 DAG.RemoveDeadNodes();
483 // Emit machine code to BB.
484 ScheduleAndEmitDAG(DAG);
486 // If we are emitting FP stack code, scan the basic block to determine if this
487 // block defines any FP values. If so, put an FP_REG_KILL instruction before
488 // the terminator of the block.
490 // Note that FP stack instructions are used in all modes for long double,
491 // so we always need to do this check.
492 // Also note that it's possible for an FP stack register to be live across
493 // an instruction that produces multiple basic blocks (SSE CMOV) so we
494 // must check all the generated basic blocks.
496 // Scan all of the machine instructions in these MBBs, checking for FP
497 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
498 MachineFunction::iterator MBBI = FirstMBB;
500 bool ContainsFPCode = false;
501 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
502 !ContainsFPCode && I != E; ++I) {
503 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
504 const TargetRegisterClass *clas;
505 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
506 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
507 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
508 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
509 X86::RFP32RegisterClass ||
510 clas == X86::RFP64RegisterClass ||
511 clas == X86::RFP80RegisterClass)) {
512 ContainsFPCode = true;
518 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
519 // a copy of the input value in this block. In SSE mode, we only care about
521 if (!ContainsFPCode) {
522 // Final check, check LLVM BB's that are successors to the LLVM BB
523 // corresponding to BB for FP PHI nodes.
524 const BasicBlock *LLVMBB = BB->getBasicBlock();
526 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
527 !ContainsFPCode && SI != E; ++SI) {
528 for (BasicBlock::const_iterator II = SI->begin();
529 (PN = dyn_cast<PHINode>(II)); ++II) {
530 if (PN->getType()==Type::X86_FP80Ty ||
531 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
532 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
533 ContainsFPCode = true;
539 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
540 if (ContainsFPCode) {
541 BuildMI(*MBBI, MBBI->getFirstTerminator(),
542 TM.getInstrInfo()->get(X86::FP_REG_KILL));
545 } while (&*(MBBI++) != BB);
548 /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
549 /// the main function.
550 void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
551 MachineFrameInfo *MFI) {
552 const TargetInstrInfo *TII = TM.getInstrInfo();
553 if (Subtarget->isTargetCygMing())
554 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
557 void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
558 // If this is main, emit special code for main.
559 MachineBasicBlock *BB = MF.begin();
560 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
561 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
564 /// MatchAddress - Add the specified node to the specified addressing mode,
565 /// returning true if it cannot be done. This just pattern matches for the
567 bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
568 bool isRoot, unsigned Depth) {
571 return MatchAddressBase(N, AM, isRoot, Depth);
573 // RIP relative addressing: %rip + 32-bit displacement!
575 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
576 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
577 if (isInt32(AM.Disp + Val)) {
585 int id = N.Val->getNodeId();
586 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
588 switch (N.getOpcode()) {
590 case ISD::Constant: {
591 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
592 if (isInt32(AM.Disp + Val)) {
599 case X86ISD::Wrapper: {
600 bool is64Bit = Subtarget->is64Bit();
601 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
602 if (is64Bit && TM.getCodeModel() != CodeModel::Small)
604 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
606 // If value is available in a register both base and index components have
607 // been picked, we can't fit the result available in the register in the
608 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
609 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
610 bool isStatic = TM.getRelocationModel() == Reloc::Static;
611 SDOperand N0 = N.getOperand(0);
612 // Mac OS X X86-64 lower 4G address is not available.
613 bool isAbs32 = !is64Bit ||
614 (isStatic && Subtarget->hasLow4GUserSpaceAddress());
615 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
616 GlobalValue *GV = G->getGlobal();
617 if (isAbs32 || isRoot) {
619 AM.Disp += G->getOffset();
620 AM.isRIPRel = !isAbs32;
623 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
624 if (isAbs32 || isRoot) {
625 AM.CP = CP->getConstVal();
626 AM.Align = CP->getAlignment();
627 AM.Disp += CP->getOffset();
628 AM.isRIPRel = !isAbs32;
631 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
632 if (isAbs32 || isRoot) {
633 AM.ES = S->getSymbol();
634 AM.isRIPRel = !isAbs32;
637 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
638 if (isAbs32 || isRoot) {
639 AM.JT = J->getIndex();
640 AM.isRIPRel = !isAbs32;
648 case ISD::FrameIndex:
649 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
650 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
651 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
657 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1)
660 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
661 unsigned Val = CN->getValue();
662 if (Val == 1 || Val == 2 || Val == 3) {
664 SDOperand ShVal = N.Val->getOperand(0);
666 // Okay, we know that we have a scale by now. However, if the scaled
667 // value is an add of something and a constant, we can fold the
668 // constant into the disp field here.
669 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
670 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
671 AM.IndexReg = ShVal.Val->getOperand(0);
672 ConstantSDNode *AddVal =
673 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
674 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
689 // A mul_lohi where we need the low part can be folded as a plain multiply.
690 if (N.ResNo != 0) break;
693 // X*[3,5,9] -> X+X*[2,4,8]
694 if (!AlreadySelected &&
695 AM.BaseType == X86ISelAddressMode::RegBase &&
696 AM.Base.Reg.Val == 0 &&
697 AM.IndexReg.Val == 0) {
698 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
699 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
700 AM.Scale = unsigned(CN->getValue())-1;
702 SDOperand MulVal = N.Val->getOperand(0);
705 // Okay, we know that we have a scale by now. However, if the scaled
706 // value is an add of something and a constant, we can fold the
707 // constant into the disp field here.
708 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
709 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
710 Reg = MulVal.Val->getOperand(0);
711 ConstantSDNode *AddVal =
712 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
713 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
717 Reg = N.Val->getOperand(0);
719 Reg = N.Val->getOperand(0);
722 AM.IndexReg = AM.Base.Reg = Reg;
729 if (!AlreadySelected) {
730 X86ISelAddressMode Backup = AM;
731 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
732 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
735 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
736 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
743 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
744 if (AlreadySelected) break;
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();
764 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
765 // allows us to fold the shift into this addressing mode.
766 if (AlreadySelected) break;
767 SDOperand Shift = N.getOperand(0);
768 if (Shift.getOpcode() != ISD::SHL) break;
770 // Scale must not be used already.
771 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
773 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
774 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
775 if (!C1 || !C2) break;
777 // Not likely to be profitable if either the AND or SHIFT node has more
778 // than one use (unless all uses are for address computation). Besides,
779 // isel mechanism requires their node ids to be reused.
780 if (!N.hasOneUse() || !Shift.hasOneUse())
783 // Verify that the shift amount is something we can fold.
784 unsigned ShiftCst = C1->getValue();
785 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
788 // Get the new AND mask, this folds to a constant.
789 SDOperand NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
790 SDOperand(C2, 0), SDOperand(C1, 0));
791 SDOperand NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
792 Shift.getOperand(0), NewANDMask);
793 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
794 NewAND.Val->setNodeId(N.Val->getNodeId());
796 AM.Scale = 1 << ShiftCst;
797 AM.IndexReg = NewAND;
802 return MatchAddressBase(N, AM, isRoot, Depth);
805 /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
806 /// specified addressing mode without any further recursion.
807 bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
808 bool isRoot, unsigned Depth) {
809 // Is the base register already occupied?
810 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
811 // If so, check to see if the scale index register is set.
812 if (AM.IndexReg.Val == 0) {
818 // Otherwise, we cannot select it.
822 // Default, generate it as a register.
823 AM.BaseType = X86ISelAddressMode::RegBase;
828 /// SelectAddr - returns true if it is able pattern match an addressing mode.
829 /// It returns the operands which make up the maximal addressing mode it can
830 /// match by reference.
831 bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
832 SDOperand &Scale, SDOperand &Index,
834 X86ISelAddressMode AM;
835 if (MatchAddress(N, AM))
838 MVT::ValueType VT = N.getValueType();
839 if (AM.BaseType == X86ISelAddressMode::RegBase) {
840 if (!AM.Base.Reg.Val)
841 AM.Base.Reg = CurDAG->getRegister(0, VT);
844 if (!AM.IndexReg.Val)
845 AM.IndexReg = CurDAG->getRegister(0, VT);
847 getAddressOperands(AM, Base, Scale, Index, Disp);
851 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
853 static inline bool isZeroNode(SDOperand Elt) {
854 return ((isa<ConstantSDNode>(Elt) &&
855 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
856 (isa<ConstantFPSDNode>(Elt) &&
857 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
861 /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
862 /// match a load whose top elements are either undef or zeros. The load flavor
863 /// is derived from the type of N, which is either v4f32 or v2f64.
864 bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
865 SDOperand N, SDOperand &Base,
866 SDOperand &Scale, SDOperand &Index,
867 SDOperand &Disp, SDOperand &InChain,
868 SDOperand &OutChain) {
869 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
870 InChain = N.getOperand(0).getValue(1);
871 if (ISD::isNON_EXTLoad(InChain.Val) &&
872 InChain.getValue(0).hasOneUse() &&
874 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
875 LoadSDNode *LD = cast<LoadSDNode>(InChain);
876 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
878 OutChain = LD->getChain();
883 // Also handle the case where we explicitly require zeros in the top
884 // elements. This is a vector shuffle from the zero vector.
885 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
886 // Check to see if the top elements are all zeros (or bitcast of zeros).
887 ISD::isBuildVectorAllZeros(N.getOperand(0).Val) &&
888 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
889 N.getOperand(1).Val->hasOneUse() &&
890 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
891 N.getOperand(1).getOperand(0).hasOneUse()) {
892 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
894 unsigned VecWidth=MVT::getVectorNumElements(N.getOperand(0).getValueType());
895 SDOperand ShufMask = N.getOperand(2);
896 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
897 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
898 if (C->getValue() == VecWidth) {
899 for (unsigned i = 1; i != VecWidth; ++i) {
900 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
903 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
904 if (C->getValue() >= VecWidth) return false;
909 // Okay, this is a zero extending load. Fold it.
910 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
911 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
913 OutChain = LD->getChain();
914 InChain = SDOperand(LD, 1);
922 /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
923 /// mode it matches can be cost effectively emitted as an LEA instruction.
924 bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
925 SDOperand &Base, SDOperand &Scale,
926 SDOperand &Index, SDOperand &Disp) {
927 X86ISelAddressMode AM;
928 if (MatchAddress(N, AM))
931 MVT::ValueType VT = N.getValueType();
932 unsigned Complexity = 0;
933 if (AM.BaseType == X86ISelAddressMode::RegBase)
937 AM.Base.Reg = CurDAG->getRegister(0, VT);
938 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
944 AM.IndexReg = CurDAG->getRegister(0, VT);
946 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
951 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
952 // to a LEA. This is determined with some expermentation but is by no means
953 // optimal (especially for code size consideration). LEA is nice because of
954 // its three-address nature. Tweak the cost function again when we can run
955 // convertToThreeAddress() at register allocation time.
956 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
957 // For X86-64, we should always use lea to materialize RIP relative
959 if (Subtarget->is64Bit())
965 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
968 if (Complexity > 2) {
969 getAddressOperands(AM, Base, Scale, Index, Disp);
975 bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
976 SDOperand &Base, SDOperand &Scale,
977 SDOperand &Index, SDOperand &Disp) {
978 if (ISD::isNON_EXTLoad(N.Val) &&
980 CanBeFoldedBy(N.Val, P.Val, P.Val))
981 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
985 /// getGlobalBaseReg - Output the instructions required to put the
986 /// base address to use for accessing globals into a register.
988 SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
989 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
990 if (!GlobalBaseReg) {
991 // Insert the set of GlobalBaseReg into the first MBB of the function
992 MachineFunction *MF = BB->getParent();
993 MachineBasicBlock &FirstMBB = MF->front();
994 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
995 MachineRegisterInfo &RegInfo = MF->getRegInfo();
996 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
998 const TargetInstrInfo *TII = TM.getInstrInfo();
999 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1000 // only used in JIT code emission as displacement to pc.
1001 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
1003 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1004 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
1005 if (TM.getRelocationModel() == Reloc::PIC_ &&
1006 Subtarget->isPICStyleGOT()) {
1007 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
1008 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1009 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
1015 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
1018 static SDNode *FindCallStartFromCall(SDNode *Node) {
1019 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1020 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1021 "Node doesn't have a token chain argument!");
1022 return FindCallStartFromCall(Node->getOperand(0).Val);
1025 SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) {
1029 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1030 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1031 if (!Subtarget->is64Bit()) {
1034 switch (N0.getValueType()) {
1035 default: assert(0 && "Unknown truncate!");
1037 Opc = X86::MOV16to16_;
1041 Opc = X86::MOV32to32_;
1045 N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0);
1046 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1047 VT, N0, SRIdx, N0.getValue(1));
1051 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1054 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1056 default: assert(0 && "Unknown truncate!"); break;
1058 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
1062 SDNode *X86DAGToDAGISel::Select(SDOperand N) {
1063 SDNode *Node = N.Val;
1064 MVT::ValueType NVT = Node->getValueType(0);
1066 unsigned Opcode = Node->getOpcode();
1069 DOUT << std::string(Indent, ' ') << "Selecting: ";
1070 DEBUG(Node->dump(CurDAG));
1075 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
1077 DOUT << std::string(Indent-2, ' ') << "== ";
1078 DEBUG(Node->dump(CurDAG));
1082 return NULL; // Already selected.
1087 case X86ISD::GlobalBaseReg:
1088 return getGlobalBaseReg();
1091 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1092 // code and is matched first so to prevent it from being turned into
1094 // In 64-bit small code size mode, use LEA to take advantage of
1095 // RIP-relative addressing.
1096 if (TM.getCodeModel() != CodeModel::Small)
1098 MVT::ValueType PtrVT = TLI.getPointerTy();
1099 SDOperand N0 = N.getOperand(0);
1100 SDOperand N1 = N.getOperand(1);
1101 if (N.Val->getValueType(0) == PtrVT &&
1102 N0.getOpcode() == X86ISD::Wrapper &&
1103 N1.getOpcode() == ISD::Constant) {
1104 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1106 // TODO: handle ExternalSymbolSDNode.
1107 if (GlobalAddressSDNode *G =
1108 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
1109 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
1110 G->getOffset() + Offset);
1111 } else if (ConstantPoolSDNode *CP =
1112 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
1113 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
1115 CP->getOffset()+Offset);
1119 if (Subtarget->is64Bit()) {
1120 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1121 CurDAG->getRegister(0, PtrVT), C };
1122 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1124 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1128 // Other cases are handled by auto-generated code.
1132 case ISD::SMUL_LOHI:
1133 case ISD::UMUL_LOHI: {
1134 SDOperand N0 = Node->getOperand(0);
1135 SDOperand N1 = Node->getOperand(1);
1137 // There are several forms of IMUL that just return the low part and
1138 // don't have fixed-register operands. If we don't need the high part,
1139 // use these instead. They can be selected with the generated ISel code.
1140 if (NVT != MVT::i8 &&
1141 N.getValue(1).use_empty()) {
1142 N = CurDAG->getNode(ISD::MUL, NVT, N0, N1);
1146 bool isSigned = Opcode == ISD::SMUL_LOHI;
1149 default: assert(0 && "Unsupported VT!");
1150 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1151 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1152 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1153 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1157 default: assert(0 && "Unsupported VT!");
1158 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1159 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1160 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1161 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1164 unsigned LoReg, HiReg;
1166 default: assert(0 && "Unsupported VT!");
1167 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1168 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1169 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1170 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1173 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1174 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1175 // multiplty is commmutative
1177 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
1183 SDOperand InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1184 N0, SDOperand()).getValue(1);
1187 AddToISelQueue(N1.getOperand(0));
1188 AddToISelQueue(Tmp0);
1189 AddToISelQueue(Tmp1);
1190 AddToISelQueue(Tmp2);
1191 AddToISelQueue(Tmp3);
1192 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
1194 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
1195 InFlag = SDOperand(CNode, 1);
1196 // Update the chain.
1197 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
1201 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1204 // Copy the low half of the result, if it is needed.
1205 if (!N.getValue(0).use_empty()) {
1206 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1207 LoReg, NVT, InFlag);
1208 InFlag = Result.getValue(2);
1209 ReplaceUses(N.getValue(0), Result);
1211 DOUT << std::string(Indent-2, ' ') << "=> ";
1212 DEBUG(Result.Val->dump(CurDAG));
1216 // Copy the high half of the result, if it is needed.
1217 if (!N.getValue(1).use_empty()) {
1219 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1220 // Prevent use of AH in a REX instruction by referencing AX instead.
1221 // Shift it down 8 bits.
1222 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1223 X86::AX, MVT::i16, InFlag);
1224 InFlag = Result.getValue(2);
1225 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1226 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1227 // Then truncate it down to i8.
1228 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1229 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1230 MVT::i8, Result, SRIdx), 0);
1232 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1233 HiReg, NVT, InFlag);
1234 InFlag = Result.getValue(2);
1236 ReplaceUses(N.getValue(1), Result);
1238 DOUT << std::string(Indent-2, ' ') << "=> ";
1239 DEBUG(Result.Val->dump(CurDAG));
1252 case ISD::UDIVREM: {
1253 SDOperand N0 = Node->getOperand(0);
1254 SDOperand N1 = Node->getOperand(1);
1256 bool isSigned = Opcode == ISD::SDIVREM;
1259 default: assert(0 && "Unsupported VT!");
1260 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1261 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1262 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1263 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1267 default: assert(0 && "Unsupported VT!");
1268 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1269 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1270 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1271 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1274 unsigned LoReg, HiReg;
1275 unsigned ClrOpcode, SExtOpcode;
1277 default: assert(0 && "Unsupported VT!");
1279 LoReg = X86::AL; HiReg = X86::AH;
1281 SExtOpcode = X86::CBW;
1284 LoReg = X86::AX; HiReg = X86::DX;
1285 ClrOpcode = X86::MOV16r0;
1286 SExtOpcode = X86::CWD;
1289 LoReg = X86::EAX; HiReg = X86::EDX;
1290 ClrOpcode = X86::MOV32r0;
1291 SExtOpcode = X86::CDQ;
1294 LoReg = X86::RAX; HiReg = X86::RDX;
1295 ClrOpcode = X86::MOV64r0;
1296 SExtOpcode = X86::CQO;
1300 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1301 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1304 if (NVT == MVT::i8 && !isSigned) {
1305 // Special case for div8, just use a move with zero extension to AX to
1306 // clear the upper 8 bits (AH).
1307 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1308 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1309 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1310 AddToISelQueue(N0.getOperand(0));
1311 AddToISelQueue(Tmp0);
1312 AddToISelQueue(Tmp1);
1313 AddToISelQueue(Tmp2);
1314 AddToISelQueue(Tmp3);
1316 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1318 Chain = Move.getValue(1);
1319 ReplaceUses(N0.getValue(1), Chain);
1323 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1324 Chain = CurDAG->getEntryNode();
1326 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDOperand());
1327 InFlag = Chain.getValue(1);
1331 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
1332 LoReg, N0, SDOperand()).getValue(1);
1334 // Sign extend the low part into the high part.
1336 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1338 // Zero out the high part, effectively zero extending the input.
1339 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
1340 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1341 ClrNode, InFlag).getValue(1);
1346 AddToISelQueue(N1.getOperand(0));
1347 AddToISelQueue(Tmp0);
1348 AddToISelQueue(Tmp1);
1349 AddToISelQueue(Tmp2);
1350 AddToISelQueue(Tmp3);
1351 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
1353 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
1354 InFlag = SDOperand(CNode, 1);
1355 // Update the chain.
1356 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
1360 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1363 // Copy the division (low) result, if it is needed.
1364 if (!N.getValue(0).use_empty()) {
1365 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1366 LoReg, NVT, InFlag);
1367 InFlag = Result.getValue(2);
1368 ReplaceUses(N.getValue(0), Result);
1370 DOUT << std::string(Indent-2, ' ') << "=> ";
1371 DEBUG(Result.Val->dump(CurDAG));
1375 // Copy the remainder (high) result, if it is needed.
1376 if (!N.getValue(1).use_empty()) {
1378 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1379 // Prevent use of AH in a REX instruction by referencing AX instead.
1380 // Shift it down 8 bits.
1381 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1382 X86::AX, MVT::i16, InFlag);
1383 InFlag = Result.getValue(2);
1384 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1385 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1386 // Then truncate it down to i8.
1387 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1388 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1389 MVT::i8, Result, SRIdx), 0);
1391 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1392 HiReg, NVT, InFlag);
1393 InFlag = Result.getValue(2);
1395 ReplaceUses(N.getValue(1), Result);
1397 DOUT << std::string(Indent-2, ' ') << "=> ";
1398 DEBUG(Result.Val->dump(CurDAG));
1410 case ISD::ANY_EXTEND: {
1411 SDOperand N0 = Node->getOperand(0);
1413 if (NVT == MVT::i64 || NVT == MVT::i32 || NVT == MVT::i16) {
1415 switch(N0.getValueType()) {
1417 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1420 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1423 if (Subtarget->is64Bit())
1424 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1426 default: assert(0 && "Unknown any_extend!");
1429 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
1433 DOUT << std::string(Indent-2, ' ') << "=> ";
1434 DEBUG(ResNode->dump(CurDAG));
1439 } // Otherwise let generated ISel handle it.
1444 case ISD::SIGN_EXTEND_INREG: {
1445 SDOperand N0 = Node->getOperand(0);
1448 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1449 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
1453 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1454 else assert(0 && "Unknown sign_extend_inreg!");
1458 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1459 case MVT::i16: Opc = X86::MOVSX32rr16; break;
1460 default: assert(0 && "Unknown sign_extend_inreg!");
1465 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1466 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1467 case MVT::i32: Opc = X86::MOVSX64rr32; break;
1468 default: assert(0 && "Unknown sign_extend_inreg!");
1471 default: assert(0 && "Unknown sign_extend_inreg!");
1474 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1477 DOUT << std::string(Indent-2, ' ') << "=> ";
1478 DEBUG(TruncOp.Val->dump(CurDAG));
1480 DOUT << std::string(Indent-2, ' ') << "=> ";
1481 DEBUG(ResNode->dump(CurDAG));
1489 case ISD::TRUNCATE: {
1490 SDOperand Input = Node->getOperand(0);
1491 AddToISelQueue(Node->getOperand(0));
1492 SDNode *ResNode = getTruncate(Input, NVT);
1495 DOUT << std::string(Indent-2, ' ') << "=> ";
1496 DEBUG(ResNode->dump(CurDAG));
1505 SDNode *ResNode = SelectCode(N);
1508 DOUT << std::string(Indent-2, ' ') << "=> ";
1509 if (ResNode == NULL || ResNode == N.Val)
1510 DEBUG(N.Val->dump(CurDAG));
1512 DEBUG(ResNode->dump(CurDAG));
1520 bool X86DAGToDAGISel::
1521 SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1522 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1523 SDOperand Op0, Op1, Op2, Op3;
1524 switch (ConstraintCode) {
1525 case 'o': // offsetable ??
1526 case 'v': // not offsetable ??
1527 default: return true;
1529 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1534 OutOps.push_back(Op0);
1535 OutOps.push_back(Op1);
1536 OutOps.push_back(Op2);
1537 OutOps.push_back(Op3);
1538 AddToISelQueue(Op0);
1539 AddToISelQueue(Op1);
1540 AddToISelQueue(Op2);
1541 AddToISelQueue(Op3);
1545 /// createX86ISelDag - This pass converts a legalized DAG into a
1546 /// X86-specific DAG, ready for instruction scheduling.
1548 FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1549 return new X86DAGToDAGISel(TM, Fast);