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/CommandLine.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/MathExtras.h"
39 #include "llvm/ADT/Statistic.h"
44 STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
45 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 as base?
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 PreprocessForRMW(SelectionDAG &DAG);
160 void PreprocessForFPConvert(SelectionDAG &DAG);
162 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
163 /// inline asm expressions.
164 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
166 std::vector<SDOperand> &OutOps,
169 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
171 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
172 SDOperand &Scale, SDOperand &Index,
174 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
175 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
177 Scale = getI8Imm(AM.Scale);
179 // These are 32-bit even in 64-bit mode since RIP relative offset
182 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
184 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
186 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
187 else if (AM.JT != -1)
188 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
190 Disp = getI32Imm(AM.Disp);
193 /// getI8Imm - Return a target constant with the specified value, of type
195 inline SDOperand getI8Imm(unsigned Imm) {
196 return CurDAG->getTargetConstant(Imm, MVT::i8);
199 /// getI16Imm - Return a target constant with the specified value, of type
201 inline SDOperand getI16Imm(unsigned Imm) {
202 return CurDAG->getTargetConstant(Imm, MVT::i16);
205 /// getI32Imm - Return a target constant with the specified value, of type
207 inline SDOperand getI32Imm(unsigned Imm) {
208 return CurDAG->getTargetConstant(Imm, MVT::i32);
211 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
212 /// base register. Return the virtual register that holds this value.
213 SDNode *getGlobalBaseReg();
215 /// getTruncate - return an SDNode that implements a subreg based truncate
216 /// of the specified operand to the the specified value type.
217 SDNode *getTruncate(SDOperand N0, MVT::ValueType VT);
225 static SDNode *findFlagUse(SDNode *N) {
226 unsigned FlagResNo = N->getNumValues()-1;
227 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
229 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
230 SDOperand Op = User->getOperand(i);
231 if (Op.Val == N && Op.ResNo == FlagResNo)
238 static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
239 SDNode *Root, SDNode *Skip, bool &found,
240 std::set<SDNode *> &Visited) {
242 Use->getNodeId() > Def->getNodeId() ||
243 !Visited.insert(Use).second)
246 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
247 SDNode *N = Use->getOperand(i).Val;
252 continue; // Immediate use is ok.
254 assert(Use->getOpcode() == ISD::STORE ||
255 Use->getOpcode() == X86ISD::CMP);
261 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
265 /// isNonImmUse - Start searching from Root up the DAG to check is Def can
266 /// be reached. Return true if that's the case. However, ignore direct uses
267 /// by ImmedUse (which would be U in the example illustrated in
268 /// CanBeFoldedBy) and by Root (which can happen in the store case).
269 /// FIXME: to be really generic, we should allow direct use by any node
270 /// that is being folded. But realisticly since we only fold loads which
271 /// have one non-chain use, we only need to watch out for load/op/store
272 /// and load/op/cmp case where the root (store / cmp) may reach the load via
273 /// its chain operand.
274 static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
275 SDNode *Skip = NULL) {
276 std::set<SDNode *> Visited;
278 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
283 bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
284 if (FastISel) return false;
286 // If U use can somehow reach N through another path then U can't fold N or
287 // it will create a cycle. e.g. In the following diagram, U can reach N
288 // through X. If N is folded into into U, then X is both a predecessor and
299 if (isNonImmUse(Root, N, U))
302 // If U produces a flag, then it gets (even more) interesting. Since it
303 // would have been "glued" together with its flag use, we need to check if
316 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
317 // NU), then TF is a predecessor of FU and a successor of NU. But since
318 // NU and FU are flagged together, this effectively creates a cycle.
319 bool HasFlagUse = false;
320 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
321 while ((VT == MVT::Flag && !Root->use_empty())) {
322 SDNode *FU = findFlagUse(Root);
329 VT = Root->getValueType(Root->getNumValues()-1);
333 return !isNonImmUse(Root, N, Root, U);
337 /// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
338 /// and move load below the TokenFactor. Replace store's chain operand with
339 /// load's chain result.
340 static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
341 SDOperand Store, SDOperand TF) {
342 std::vector<SDOperand> Ops;
343 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
344 if (Load.Val == TF.Val->getOperand(i).Val)
345 Ops.push_back(Load.Val->getOperand(0));
347 Ops.push_back(TF.Val->getOperand(i));
348 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
349 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
350 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
351 Store.getOperand(2), Store.getOperand(3));
354 /// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
355 /// This is only run if not in -fast mode (aka -O0).
356 /// This allows the instruction selector to pick more read-modify-write
357 /// instructions. This is a common case:
367 /// [TokenFactor] [Op]
374 /// The fact the store's chain operand != load's chain will prevent the
375 /// (store (op (load))) instruction from being selected. We can transform it to:
394 void X86DAGToDAGISel::PreprocessForRMW(SelectionDAG &DAG) {
395 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
396 E = DAG.allnodes_end(); I != E; ++I) {
397 if (!ISD::isNON_TRUNCStore(I))
399 SDOperand Chain = I->getOperand(0);
400 if (Chain.Val->getOpcode() != ISD::TokenFactor)
403 SDOperand N1 = I->getOperand(1);
404 SDOperand N2 = I->getOperand(2);
405 if (MVT::isFloatingPoint(N1.getValueType()) ||
406 MVT::isVector(N1.getValueType()) ||
412 unsigned Opcode = N1.Val->getOpcode();
421 SDOperand N10 = N1.getOperand(0);
422 SDOperand N11 = N1.getOperand(1);
423 if (ISD::isNON_EXTLoad(N10.Val))
425 else if (ISD::isNON_EXTLoad(N11.Val)) {
429 RModW = RModW && N10.Val->isOperandOf(Chain.Val) && N10.hasOneUse() &&
430 (N10.getOperand(1) == N2) &&
431 (N10.Val->getValueType(0) == N1.getValueType());
446 SDOperand N10 = N1.getOperand(0);
447 if (ISD::isNON_EXTLoad(N10.Val))
448 RModW = N10.Val->isOperandOf(Chain.Val) && N10.hasOneUse() &&
449 (N10.getOperand(1) == N2) &&
450 (N10.Val->getValueType(0) == N1.getValueType());
458 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
465 /// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
466 /// nodes that target the FP stack to be store and load to the stack. This is a
467 /// gross hack. We would like to simply mark these as being illegal, but when
468 /// we do that, legalize produces these when it expands calls, then expands
469 /// these in the same legalize pass. We would like dag combine to be able to
470 /// hack on these between the call expansion and the node legalization. As such
471 /// this pass basically does "really late" legalization of these inline with the
473 void X86DAGToDAGISel::PreprocessForFPConvert(SelectionDAG &DAG) {
474 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
475 E = DAG.allnodes_end(); I != E; ) {
476 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
477 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
480 // If the source and destination are SSE registers, then this is a legal
481 // conversion that should not be lowered.
482 MVT::ValueType SrcVT = N->getOperand(0).getValueType();
483 MVT::ValueType DstVT = N->getValueType(0);
484 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
485 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
486 if (SrcIsSSE && DstIsSSE)
489 // If this is an FPStack extension (but not a truncation), it is a noop.
490 if (!SrcIsSSE && !DstIsSSE && N->getOpcode() == ISD::FP_EXTEND)
493 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
494 // FPStack has extload and truncstore. SSE can fold direct loads into other
495 // operations. Based on this, decide what we want to do.
496 MVT::ValueType MemVT;
497 if (N->getOpcode() == ISD::FP_ROUND)
498 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
500 MemVT = SrcIsSSE ? SrcVT : DstVT;
502 SDOperand MemTmp = DAG.CreateStackTemporary(MemVT);
504 // FIXME: optimize the case where the src/dest is a load or store?
505 SDOperand Store = DAG.getTruncStore(DAG.getEntryNode(), N->getOperand(0),
506 MemTmp, NULL, 0, MemVT);
507 SDOperand Result = DAG.getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
510 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
511 // extload we created. This will cause general havok on the dag because
512 // anything below the conversion could be folded into other existing nodes.
513 // To avoid invalidating 'I', back it up to the convert node.
515 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result);
517 // Now that we did that, the node is dead. Increment the iterator to the
518 // next node to process, then delete N.
524 /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
525 /// when it has created a SelectionDAG for us to codegen.
526 void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
528 MachineFunction::iterator FirstMBB = BB;
531 PreprocessForRMW(DAG);
533 // FIXME: This should only happen when not -fast.
534 PreprocessForFPConvert(DAG);
536 // Codegen the basic block.
538 DOUT << "===== Instruction selection begins:\n";
541 DAG.setRoot(SelectRoot(DAG.getRoot()));
543 DOUT << "===== Instruction selection ends:\n";
546 DAG.RemoveDeadNodes();
548 // Emit machine code to BB.
549 ScheduleAndEmitDAG(DAG);
551 // If we are emitting FP stack code, scan the basic block to determine if this
552 // block defines any FP values. If so, put an FP_REG_KILL instruction before
553 // the terminator of the block.
555 // Note that FP stack instructions are used in all modes for long double,
556 // so we always need to do this check.
557 // Also note that it's possible for an FP stack register to be live across
558 // an instruction that produces multiple basic blocks (SSE CMOV) so we
559 // must check all the generated basic blocks.
561 // Scan all of the machine instructions in these MBBs, checking for FP
562 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
563 MachineFunction::iterator MBBI = FirstMBB;
565 bool ContainsFPCode = false;
566 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
567 !ContainsFPCode && I != E; ++I) {
568 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
569 const TargetRegisterClass *clas;
570 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
571 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
572 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
573 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
574 X86::RFP32RegisterClass ||
575 clas == X86::RFP64RegisterClass ||
576 clas == X86::RFP80RegisterClass)) {
577 ContainsFPCode = true;
583 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
584 // a copy of the input value in this block. In SSE mode, we only care about
586 if (!ContainsFPCode) {
587 // Final check, check LLVM BB's that are successors to the LLVM BB
588 // corresponding to BB for FP PHI nodes.
589 const BasicBlock *LLVMBB = BB->getBasicBlock();
591 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
592 !ContainsFPCode && SI != E; ++SI) {
593 for (BasicBlock::const_iterator II = SI->begin();
594 (PN = dyn_cast<PHINode>(II)); ++II) {
595 if (PN->getType()==Type::X86_FP80Ty ||
596 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
597 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
598 ContainsFPCode = true;
604 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
605 if (ContainsFPCode) {
606 BuildMI(*MBBI, MBBI->getFirstTerminator(),
607 TM.getInstrInfo()->get(X86::FP_REG_KILL));
610 } while (&*(MBBI++) != BB);
613 /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
614 /// the main function.
615 void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
616 MachineFrameInfo *MFI) {
617 const TargetInstrInfo *TII = TM.getInstrInfo();
618 if (Subtarget->isTargetCygMing())
619 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
622 void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
623 // If this is main, emit special code for main.
624 MachineBasicBlock *BB = MF.begin();
625 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
626 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
629 /// MatchAddress - Add the specified node to the specified addressing mode,
630 /// returning true if it cannot be done. This just pattern matches for the
632 bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
633 bool isRoot, unsigned Depth) {
636 return MatchAddressBase(N, AM, isRoot, Depth);
638 // RIP relative addressing: %rip + 32-bit displacement!
640 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
641 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
642 if (isInt32(AM.Disp + Val)) {
650 int id = N.Val->getNodeId();
651 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
653 switch (N.getOpcode()) {
655 case ISD::Constant: {
656 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
657 if (isInt32(AM.Disp + Val)) {
664 case X86ISD::Wrapper: {
665 bool is64Bit = Subtarget->is64Bit();
666 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
667 // Also, base and index reg must be 0 in order to use rip as base.
668 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
669 AM.Base.Reg.Val || AM.IndexReg.Val))
671 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
673 // If value is available in a register both base and index components have
674 // been picked, we can't fit the result available in the register in the
675 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
676 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
677 SDOperand N0 = N.getOperand(0);
678 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
679 GlobalValue *GV = G->getGlobal();
681 AM.Disp += G->getOffset();
682 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
683 Subtarget->isPICStyleRIPRel();
685 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
686 AM.CP = CP->getConstVal();
687 AM.Align = CP->getAlignment();
688 AM.Disp += CP->getOffset();
689 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
690 Subtarget->isPICStyleRIPRel();
692 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
693 AM.ES = S->getSymbol();
694 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
695 Subtarget->isPICStyleRIPRel();
697 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
698 AM.JT = J->getIndex();
699 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
700 Subtarget->isPICStyleRIPRel();
707 case ISD::FrameIndex:
708 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
709 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
710 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
716 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1 || AM.isRIPRel)
719 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
720 unsigned Val = CN->getValue();
721 if (Val == 1 || Val == 2 || Val == 3) {
723 SDOperand ShVal = N.Val->getOperand(0);
725 // Okay, we know that we have a scale by now. However, if the scaled
726 // value is an add of something and a constant, we can fold the
727 // constant into the disp field here.
728 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
729 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
730 AM.IndexReg = ShVal.Val->getOperand(0);
731 ConstantSDNode *AddVal =
732 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
733 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
748 // A mul_lohi where we need the low part can be folded as a plain multiply.
749 if (N.ResNo != 0) break;
752 // X*[3,5,9] -> X+X*[2,4,8]
753 if (!AlreadySelected &&
754 AM.BaseType == X86ISelAddressMode::RegBase &&
755 AM.Base.Reg.Val == 0 &&
756 AM.IndexReg.Val == 0 &&
758 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
759 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
760 AM.Scale = unsigned(CN->getValue())-1;
762 SDOperand MulVal = N.Val->getOperand(0);
765 // Okay, we know that we have a scale by now. However, if the scaled
766 // value is an add of something and a constant, we can fold the
767 // constant into the disp field here.
768 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
769 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
770 Reg = MulVal.Val->getOperand(0);
771 ConstantSDNode *AddVal =
772 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
773 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
777 Reg = N.Val->getOperand(0);
779 Reg = N.Val->getOperand(0);
782 AM.IndexReg = AM.Base.Reg = Reg;
789 if (!AlreadySelected) {
790 X86ISelAddressMode Backup = AM;
791 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
792 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
795 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
796 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
803 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
804 if (AlreadySelected) break;
806 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
807 X86ISelAddressMode Backup = AM;
808 // Start with the LHS as an addr mode.
809 if (!MatchAddress(N.getOperand(0), AM, false) &&
810 // Address could not have picked a GV address for the displacement.
812 // On x86-64, the resultant disp must fit in 32-bits.
813 isInt32(AM.Disp + CN->getSignExtended()) &&
814 // Check to see if the LHS & C is zero.
815 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
816 AM.Disp += CN->getValue();
824 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
825 // allows us to fold the shift into this addressing mode.
826 if (AlreadySelected) break;
827 SDOperand Shift = N.getOperand(0);
828 if (Shift.getOpcode() != ISD::SHL) break;
830 // Scale must not be used already.
831 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
833 // Not when RIP is used as the base.
834 if (AM.isRIPRel) break;
836 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
837 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
838 if (!C1 || !C2) break;
840 // Not likely to be profitable if either the AND or SHIFT node has more
841 // than one use (unless all uses are for address computation). Besides,
842 // isel mechanism requires their node ids to be reused.
843 if (!N.hasOneUse() || !Shift.hasOneUse())
846 // Verify that the shift amount is something we can fold.
847 unsigned ShiftCst = C1->getValue();
848 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
851 // Get the new AND mask, this folds to a constant.
852 SDOperand NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
853 SDOperand(C2, 0), SDOperand(C1, 0));
854 SDOperand NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
855 Shift.getOperand(0), NewANDMask);
856 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
857 NewAND.Val->setNodeId(N.Val->getNodeId());
859 AM.Scale = 1 << ShiftCst;
860 AM.IndexReg = NewAND;
865 return MatchAddressBase(N, AM, isRoot, Depth);
868 /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
869 /// specified addressing mode without any further recursion.
870 bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
871 bool isRoot, unsigned Depth) {
872 // Is the base register already occupied?
873 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
874 // If so, check to see if the scale index register is set.
875 if (AM.IndexReg.Val == 0 && !AM.isRIPRel) {
881 // Otherwise, we cannot select it.
885 // Default, generate it as a register.
886 AM.BaseType = X86ISelAddressMode::RegBase;
891 /// SelectAddr - returns true if it is able pattern match an addressing mode.
892 /// It returns the operands which make up the maximal addressing mode it can
893 /// match by reference.
894 bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
895 SDOperand &Scale, SDOperand &Index,
897 X86ISelAddressMode AM;
898 if (MatchAddress(N, AM))
901 MVT::ValueType VT = N.getValueType();
902 if (AM.BaseType == X86ISelAddressMode::RegBase) {
903 if (!AM.Base.Reg.Val)
904 AM.Base.Reg = CurDAG->getRegister(0, VT);
907 if (!AM.IndexReg.Val)
908 AM.IndexReg = CurDAG->getRegister(0, VT);
910 getAddressOperands(AM, Base, Scale, Index, Disp);
914 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
916 static inline bool isZeroNode(SDOperand Elt) {
917 return ((isa<ConstantSDNode>(Elt) &&
918 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
919 (isa<ConstantFPSDNode>(Elt) &&
920 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
924 /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
925 /// match a load whose top elements are either undef or zeros. The load flavor
926 /// is derived from the type of N, which is either v4f32 or v2f64.
927 bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
928 SDOperand N, SDOperand &Base,
929 SDOperand &Scale, SDOperand &Index,
930 SDOperand &Disp, SDOperand &InChain,
931 SDOperand &OutChain) {
932 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
933 InChain = N.getOperand(0).getValue(1);
934 if (ISD::isNON_EXTLoad(InChain.Val) &&
935 InChain.getValue(0).hasOneUse() &&
937 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
938 LoadSDNode *LD = cast<LoadSDNode>(InChain);
939 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
941 OutChain = LD->getChain();
946 // Also handle the case where we explicitly require zeros in the top
947 // elements. This is a vector shuffle from the zero vector.
948 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
949 // Check to see if the top elements are all zeros (or bitcast of zeros).
950 ISD::isBuildVectorAllZeros(N.getOperand(0).Val) &&
951 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
952 N.getOperand(1).Val->hasOneUse() &&
953 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
954 N.getOperand(1).getOperand(0).hasOneUse()) {
955 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
957 unsigned VecWidth=MVT::getVectorNumElements(N.getOperand(0).getValueType());
958 SDOperand ShufMask = N.getOperand(2);
959 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
960 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
961 if (C->getValue() == VecWidth) {
962 for (unsigned i = 1; i != VecWidth; ++i) {
963 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
966 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
967 if (C->getValue() >= VecWidth) return false;
972 // Okay, this is a zero extending load. Fold it.
973 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
974 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
976 OutChain = LD->getChain();
977 InChain = SDOperand(LD, 1);
985 /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
986 /// mode it matches can be cost effectively emitted as an LEA instruction.
987 bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
988 SDOperand &Base, SDOperand &Scale,
989 SDOperand &Index, SDOperand &Disp) {
990 X86ISelAddressMode AM;
991 if (MatchAddress(N, AM))
994 MVT::ValueType VT = N.getValueType();
995 unsigned Complexity = 0;
996 if (AM.BaseType == X86ISelAddressMode::RegBase)
1000 AM.Base.Reg = CurDAG->getRegister(0, VT);
1001 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1004 if (AM.IndexReg.Val)
1007 AM.IndexReg = CurDAG->getRegister(0, VT);
1009 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1014 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1015 // to a LEA. This is determined with some expermentation but is by no means
1016 // optimal (especially for code size consideration). LEA is nice because of
1017 // its three-address nature. Tweak the cost function again when we can run
1018 // convertToThreeAddress() at register allocation time.
1019 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1020 // For X86-64, we should always use lea to materialize RIP relative
1022 if (Subtarget->is64Bit())
1028 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
1031 if (Complexity > 2) {
1032 getAddressOperands(AM, Base, Scale, Index, Disp);
1038 bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
1039 SDOperand &Base, SDOperand &Scale,
1040 SDOperand &Index, SDOperand &Disp) {
1041 if (ISD::isNON_EXTLoad(N.Val) &&
1043 CanBeFoldedBy(N.Val, P.Val, P.Val))
1044 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
1048 /// getGlobalBaseReg - Output the instructions required to put the
1049 /// base address to use for accessing globals into a register.
1051 SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
1052 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
1053 if (!GlobalBaseReg) {
1054 // Insert the set of GlobalBaseReg into the first MBB of the function
1055 MachineFunction *MF = BB->getParent();
1056 MachineBasicBlock &FirstMBB = MF->front();
1057 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
1058 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1059 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
1061 const TargetInstrInfo *TII = TM.getInstrInfo();
1062 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1063 // only used in JIT code emission as displacement to pc.
1064 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
1066 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1067 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
1068 if (TM.getRelocationModel() == Reloc::PIC_ &&
1069 Subtarget->isPICStyleGOT()) {
1070 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
1071 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1072 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
1078 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
1081 static SDNode *FindCallStartFromCall(SDNode *Node) {
1082 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1083 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1084 "Node doesn't have a token chain argument!");
1085 return FindCallStartFromCall(Node->getOperand(0).Val);
1088 SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) {
1092 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1093 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1094 if (!Subtarget->is64Bit()) {
1097 switch (N0.getValueType()) {
1098 default: assert(0 && "Unknown truncate!");
1100 Opc = X86::MOV16to16_;
1104 Opc = X86::MOV32to32_;
1108 N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0);
1109 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1110 VT, N0, SRIdx, N0.getValue(1));
1114 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1117 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1119 default: assert(0 && "Unknown truncate!"); break;
1121 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
1125 SDNode *X86DAGToDAGISel::Select(SDOperand N) {
1126 SDNode *Node = N.Val;
1127 MVT::ValueType NVT = Node->getValueType(0);
1129 unsigned Opcode = Node->getOpcode();
1132 DOUT << std::string(Indent, ' ') << "Selecting: ";
1133 DEBUG(Node->dump(CurDAG));
1138 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
1140 DOUT << std::string(Indent-2, ' ') << "== ";
1141 DEBUG(Node->dump(CurDAG));
1145 return NULL; // Already selected.
1150 case X86ISD::GlobalBaseReg:
1151 return getGlobalBaseReg();
1153 case X86ISD::FP_GET_RESULT2: {
1154 SDOperand Chain = N.getOperand(0);
1155 SDOperand InFlag = N.getOperand(1);
1156 AddToISelQueue(Chain);
1157 AddToISelQueue(InFlag);
1158 std::vector<MVT::ValueType> Tys;
1159 Tys.push_back(MVT::f80);
1160 Tys.push_back(MVT::f80);
1161 Tys.push_back(MVT::Other);
1162 Tys.push_back(MVT::Flag);
1163 SDOperand Ops[] = { Chain, InFlag };
1164 SDNode *ResNode = CurDAG->getTargetNode(X86::FpGETRESULT80x2, Tys,
1166 Chain = SDOperand(ResNode, 2);
1167 InFlag = SDOperand(ResNode, 3);
1168 ReplaceUses(SDOperand(N.Val, 2), Chain);
1169 ReplaceUses(SDOperand(N.Val, 3), InFlag);
1174 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1175 // code and is matched first so to prevent it from being turned into
1177 // In 64-bit small code size mode, use LEA to take advantage of
1178 // RIP-relative addressing.
1179 if (TM.getCodeModel() != CodeModel::Small)
1181 MVT::ValueType PtrVT = TLI.getPointerTy();
1182 SDOperand N0 = N.getOperand(0);
1183 SDOperand N1 = N.getOperand(1);
1184 if (N.Val->getValueType(0) == PtrVT &&
1185 N0.getOpcode() == X86ISD::Wrapper &&
1186 N1.getOpcode() == ISD::Constant) {
1187 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1189 // TODO: handle ExternalSymbolSDNode.
1190 if (GlobalAddressSDNode *G =
1191 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
1192 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
1193 G->getOffset() + Offset);
1194 } else if (ConstantPoolSDNode *CP =
1195 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
1196 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
1198 CP->getOffset()+Offset);
1202 if (Subtarget->is64Bit()) {
1203 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1204 CurDAG->getRegister(0, PtrVT), C };
1205 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1207 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1211 // Other cases are handled by auto-generated code.
1215 case ISD::SMUL_LOHI:
1216 case ISD::UMUL_LOHI: {
1217 SDOperand N0 = Node->getOperand(0);
1218 SDOperand N1 = Node->getOperand(1);
1220 bool isSigned = Opcode == ISD::SMUL_LOHI;
1223 default: assert(0 && "Unsupported VT!");
1224 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1225 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1226 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1227 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1231 default: assert(0 && "Unsupported VT!");
1232 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1233 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1234 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1235 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1238 unsigned LoReg, HiReg;
1240 default: assert(0 && "Unsupported VT!");
1241 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1242 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1243 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1244 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1247 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1248 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1249 // multiplty is commmutative
1251 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
1257 SDOperand InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1258 N0, SDOperand()).getValue(1);
1261 AddToISelQueue(N1.getOperand(0));
1262 AddToISelQueue(Tmp0);
1263 AddToISelQueue(Tmp1);
1264 AddToISelQueue(Tmp2);
1265 AddToISelQueue(Tmp3);
1266 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
1268 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
1269 InFlag = SDOperand(CNode, 1);
1270 // Update the chain.
1271 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
1275 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1278 // Copy the low half of the result, if it is needed.
1279 if (!N.getValue(0).use_empty()) {
1280 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1281 LoReg, NVT, InFlag);
1282 InFlag = Result.getValue(2);
1283 ReplaceUses(N.getValue(0), Result);
1285 DOUT << std::string(Indent-2, ' ') << "=> ";
1286 DEBUG(Result.Val->dump(CurDAG));
1290 // Copy the high half of the result, if it is needed.
1291 if (!N.getValue(1).use_empty()) {
1293 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1294 // Prevent use of AH in a REX instruction by referencing AX instead.
1295 // Shift it down 8 bits.
1296 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1297 X86::AX, MVT::i16, InFlag);
1298 InFlag = Result.getValue(2);
1299 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1300 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1301 // Then truncate it down to i8.
1302 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1303 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1304 MVT::i8, Result, SRIdx), 0);
1306 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1307 HiReg, NVT, InFlag);
1308 InFlag = Result.getValue(2);
1310 ReplaceUses(N.getValue(1), Result);
1312 DOUT << std::string(Indent-2, ' ') << "=> ";
1313 DEBUG(Result.Val->dump(CurDAG));
1326 case ISD::UDIVREM: {
1327 SDOperand N0 = Node->getOperand(0);
1328 SDOperand N1 = Node->getOperand(1);
1330 bool isSigned = Opcode == ISD::SDIVREM;
1333 default: assert(0 && "Unsupported VT!");
1334 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1335 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1336 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1337 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1341 default: assert(0 && "Unsupported VT!");
1342 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1343 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1344 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1345 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1348 unsigned LoReg, HiReg;
1349 unsigned ClrOpcode, SExtOpcode;
1351 default: assert(0 && "Unsupported VT!");
1353 LoReg = X86::AL; HiReg = X86::AH;
1355 SExtOpcode = X86::CBW;
1358 LoReg = X86::AX; HiReg = X86::DX;
1359 ClrOpcode = X86::MOV16r0;
1360 SExtOpcode = X86::CWD;
1363 LoReg = X86::EAX; HiReg = X86::EDX;
1364 ClrOpcode = X86::MOV32r0;
1365 SExtOpcode = X86::CDQ;
1368 LoReg = X86::RAX; HiReg = X86::RDX;
1369 ClrOpcode = X86::MOV64r0;
1370 SExtOpcode = X86::CQO;
1374 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1375 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1378 if (NVT == MVT::i8 && !isSigned) {
1379 // Special case for div8, just use a move with zero extension to AX to
1380 // clear the upper 8 bits (AH).
1381 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1382 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1383 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1384 AddToISelQueue(N0.getOperand(0));
1385 AddToISelQueue(Tmp0);
1386 AddToISelQueue(Tmp1);
1387 AddToISelQueue(Tmp2);
1388 AddToISelQueue(Tmp3);
1390 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1392 Chain = Move.getValue(1);
1393 ReplaceUses(N0.getValue(1), Chain);
1397 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1398 Chain = CurDAG->getEntryNode();
1400 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDOperand());
1401 InFlag = Chain.getValue(1);
1405 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
1406 LoReg, N0, SDOperand()).getValue(1);
1408 // Sign extend the low part into the high part.
1410 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1412 // Zero out the high part, effectively zero extending the input.
1413 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
1414 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1415 ClrNode, InFlag).getValue(1);
1420 AddToISelQueue(N1.getOperand(0));
1421 AddToISelQueue(Tmp0);
1422 AddToISelQueue(Tmp1);
1423 AddToISelQueue(Tmp2);
1424 AddToISelQueue(Tmp3);
1425 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
1427 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
1428 InFlag = SDOperand(CNode, 1);
1429 // Update the chain.
1430 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
1434 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1437 // Copy the division (low) result, if it is needed.
1438 if (!N.getValue(0).use_empty()) {
1439 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1440 LoReg, NVT, InFlag);
1441 InFlag = Result.getValue(2);
1442 ReplaceUses(N.getValue(0), Result);
1444 DOUT << std::string(Indent-2, ' ') << "=> ";
1445 DEBUG(Result.Val->dump(CurDAG));
1449 // Copy the remainder (high) result, if it is needed.
1450 if (!N.getValue(1).use_empty()) {
1452 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1453 // Prevent use of AH in a REX instruction by referencing AX instead.
1454 // Shift it down 8 bits.
1455 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1456 X86::AX, MVT::i16, InFlag);
1457 InFlag = Result.getValue(2);
1458 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1459 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1460 // Then truncate it down to i8.
1461 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1462 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1463 MVT::i8, Result, SRIdx), 0);
1465 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1466 HiReg, NVT, InFlag);
1467 InFlag = Result.getValue(2);
1469 ReplaceUses(N.getValue(1), Result);
1471 DOUT << std::string(Indent-2, ' ') << "=> ";
1472 DEBUG(Result.Val->dump(CurDAG));
1484 case ISD::ANY_EXTEND: {
1485 SDOperand N0 = Node->getOperand(0);
1487 if (NVT == MVT::i64 || NVT == MVT::i32 || NVT == MVT::i16) {
1489 switch(N0.getValueType()) {
1491 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1494 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1497 if (Subtarget->is64Bit())
1498 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1500 default: assert(0 && "Unknown any_extend!");
1503 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
1507 DOUT << std::string(Indent-2, ' ') << "=> ";
1508 DEBUG(ResNode->dump(CurDAG));
1513 } // Otherwise let generated ISel handle it.
1518 case ISD::SIGN_EXTEND_INREG: {
1519 SDOperand N0 = Node->getOperand(0);
1522 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1523 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
1527 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1528 else assert(0 && "Unknown sign_extend_inreg!");
1532 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1533 case MVT::i16: Opc = X86::MOVSX32rr16; break;
1534 default: assert(0 && "Unknown sign_extend_inreg!");
1539 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1540 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1541 case MVT::i32: Opc = X86::MOVSX64rr32; break;
1542 default: assert(0 && "Unknown sign_extend_inreg!");
1545 default: assert(0 && "Unknown sign_extend_inreg!");
1548 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1551 DOUT << std::string(Indent-2, ' ') << "=> ";
1552 DEBUG(TruncOp.Val->dump(CurDAG));
1554 DOUT << std::string(Indent-2, ' ') << "=> ";
1555 DEBUG(ResNode->dump(CurDAG));
1563 case ISD::TRUNCATE: {
1564 SDOperand Input = Node->getOperand(0);
1565 AddToISelQueue(Node->getOperand(0));
1566 SDNode *ResNode = getTruncate(Input, NVT);
1569 DOUT << std::string(Indent-2, ' ') << "=> ";
1570 DEBUG(ResNode->dump(CurDAG));
1579 SDNode *ResNode = SelectCode(N);
1582 DOUT << std::string(Indent-2, ' ') << "=> ";
1583 if (ResNode == NULL || ResNode == N.Val)
1584 DEBUG(N.Val->dump(CurDAG));
1586 DEBUG(ResNode->dump(CurDAG));
1594 bool X86DAGToDAGISel::
1595 SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1596 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1597 SDOperand Op0, Op1, Op2, Op3;
1598 switch (ConstraintCode) {
1599 case 'o': // offsetable ??
1600 case 'v': // not offsetable ??
1601 default: return true;
1603 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1608 OutOps.push_back(Op0);
1609 OutOps.push_back(Op1);
1610 OutOps.push_back(Op2);
1611 OutOps.push_back(Op3);
1612 AddToISelQueue(Op0);
1613 AddToISelQueue(Op1);
1614 AddToISelQueue(Op2);
1615 AddToISelQueue(Op3);
1619 /// createX86ISelDag - This pass converts a legalized DAG into a
1620 /// X86-specific DAG, ready for instruction scheduling.
1622 FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1623 return new X86DAGToDAGISel(TM, Fast);