LLVM API Documentation

SelectionDAGBuilder.h
Go to the documentation of this file.
00001 //===-- SelectionDAGBuilder.h - Selection-DAG building --------*- C++ -*---===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This implements routines for translating from LLVM IR into SelectionDAG IR.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H
00015 #define LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H
00016 
00017 #include "llvm/ADT/APInt.h"
00018 #include "llvm/ADT/DenseMap.h"
00019 #include "llvm/CodeGen/SelectionDAG.h"
00020 #include "llvm/CodeGen/SelectionDAGNodes.h"
00021 #include "llvm/IR/CallSite.h"
00022 #include "llvm/IR/Constants.h"
00023 #include "llvm/Support/ErrorHandling.h"
00024 #include <vector>
00025 
00026 namespace llvm {
00027 
00028 class AddrSpaceCastInst;
00029 class AliasAnalysis;
00030 class AllocaInst;
00031 class BasicBlock;
00032 class BitCastInst;
00033 class BranchInst;
00034 class CallInst;
00035 class DbgValueInst;
00036 class ExtractElementInst;
00037 class ExtractValueInst;
00038 class FCmpInst;
00039 class FPExtInst;
00040 class FPToSIInst;
00041 class FPToUIInst;
00042 class FPTruncInst;
00043 class Function;
00044 class FunctionLoweringInfo;
00045 class GetElementPtrInst;
00046 class GCFunctionInfo;
00047 class ICmpInst;
00048 class IntToPtrInst;
00049 class IndirectBrInst;
00050 class InvokeInst;
00051 class InsertElementInst;
00052 class InsertValueInst;
00053 class Instruction;
00054 class LoadInst;
00055 class MachineBasicBlock;
00056 class MachineInstr;
00057 class MachineRegisterInfo;
00058 class MDNode;
00059 class MVT;
00060 class PHINode;
00061 class PtrToIntInst;
00062 class ReturnInst;
00063 class SDDbgValue;
00064 class SExtInst;
00065 class SelectInst;
00066 class ShuffleVectorInst;
00067 class SIToFPInst;
00068 class StoreInst;
00069 class SwitchInst;
00070 class DataLayout;
00071 class TargetLibraryInfo;
00072 class TargetLowering;
00073 class TruncInst;
00074 class UIToFPInst;
00075 class UnreachableInst;
00076 class VAArgInst;
00077 class ZExtInst;
00078 
00079 //===----------------------------------------------------------------------===//
00080 /// SelectionDAGBuilder - This is the common target-independent lowering
00081 /// implementation that is parameterized by a TargetLowering object.
00082 ///
00083 class SelectionDAGBuilder {
00084   /// CurInst - The current instruction being visited
00085   const Instruction *CurInst;
00086 
00087   DenseMap<const Value*, SDValue> NodeMap;
00088 
00089   /// UnusedArgNodeMap - Maps argument value for unused arguments. This is used
00090   /// to preserve debug information for incoming arguments.
00091   DenseMap<const Value*, SDValue> UnusedArgNodeMap;
00092 
00093   /// DanglingDebugInfo - Helper type for DanglingDebugInfoMap.
00094   class DanglingDebugInfo {
00095     const DbgValueInst* DI;
00096     DebugLoc dl;
00097     unsigned SDNodeOrder;
00098   public:
00099     DanglingDebugInfo() : DI(nullptr), dl(DebugLoc()), SDNodeOrder(0) { }
00100     DanglingDebugInfo(const DbgValueInst *di, DebugLoc DL, unsigned SDNO) :
00101       DI(di), dl(DL), SDNodeOrder(SDNO) { }
00102     const DbgValueInst* getDI() { return DI; }
00103     DebugLoc getdl() { return dl; }
00104     unsigned getSDNodeOrder() { return SDNodeOrder; }
00105   };
00106 
00107   /// DanglingDebugInfoMap - Keeps track of dbg_values for which we have not
00108   /// yet seen the referent.  We defer handling these until we do see it.
00109   DenseMap<const Value*, DanglingDebugInfo> DanglingDebugInfoMap;
00110 
00111 public:
00112   /// PendingLoads - Loads are not emitted to the program immediately.  We bunch
00113   /// them up and then emit token factor nodes when possible.  This allows us to
00114   /// get simple disambiguation between loads without worrying about alias
00115   /// analysis.
00116   SmallVector<SDValue, 8> PendingLoads;
00117 private:
00118 
00119   /// PendingExports - CopyToReg nodes that copy values to virtual registers
00120   /// for export to other blocks need to be emitted before any terminator
00121   /// instruction, but they have no other ordering requirements. We bunch them
00122   /// up and the emit a single tokenfactor for them just before terminator
00123   /// instructions.
00124   SmallVector<SDValue, 8> PendingExports;
00125 
00126   /// SDNodeOrder - A unique monotonically increasing number used to order the
00127   /// SDNodes we create.
00128   unsigned SDNodeOrder;
00129 
00130   /// Case - A struct to record the Value for a switch case, and the
00131   /// case's target basic block.
00132   struct Case {
00133     const Constant *Low;
00134     const Constant *High;
00135     MachineBasicBlock* BB;
00136     uint32_t ExtraWeight;
00137 
00138     Case() : Low(nullptr), High(nullptr), BB(nullptr), ExtraWeight(0) { }
00139     Case(const Constant *low, const Constant *high, MachineBasicBlock *bb,
00140          uint32_t extraweight) : Low(low), High(high), BB(bb),
00141          ExtraWeight(extraweight) { }
00142 
00143     APInt size() const {
00144       const APInt &rHigh = cast<ConstantInt>(High)->getValue();
00145       const APInt &rLow  = cast<ConstantInt>(Low)->getValue();
00146       return (rHigh - rLow + 1ULL);
00147     }
00148   };
00149 
00150   struct CaseBits {
00151     uint64_t Mask;
00152     MachineBasicBlock* BB;
00153     unsigned Bits;
00154     uint32_t ExtraWeight;
00155 
00156     CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits,
00157              uint32_t Weight):
00158       Mask(mask), BB(bb), Bits(bits), ExtraWeight(Weight) { }
00159   };
00160 
00161   typedef std::vector<Case>           CaseVector;
00162   typedef std::vector<CaseBits>       CaseBitsVector;
00163   typedef CaseVector::iterator        CaseItr;
00164   typedef std::pair<CaseItr, CaseItr> CaseRange;
00165 
00166   /// CaseRec - A struct with ctor used in lowering switches to a binary tree
00167   /// of conditional branches.
00168   struct CaseRec {
00169     CaseRec(MachineBasicBlock *bb, const Constant *lt, const Constant *ge,
00170             CaseRange r) :
00171     CaseBB(bb), LT(lt), GE(ge), Range(r) {}
00172 
00173     /// CaseBB - The MBB in which to emit the compare and branch
00174     MachineBasicBlock *CaseBB;
00175     /// LT, GE - If nonzero, we know the current case value must be less-than or
00176     /// greater-than-or-equal-to these Constants.
00177     const Constant *LT;
00178     const Constant *GE;
00179     /// Range - A pair of iterators representing the range of case values to be
00180     /// processed at this point in the binary search tree.
00181     CaseRange Range;
00182   };
00183 
00184   typedef std::vector<CaseRec> CaseRecVector;
00185 
00186   /// The comparison function for sorting the switch case values in the vector.
00187   /// WARNING: Case ranges should be disjoint!
00188   struct CaseCmp {
00189     bool operator()(const Case &C1, const Case &C2) {
00190       assert(isa<ConstantInt>(C1.Low) && isa<ConstantInt>(C2.High));
00191       const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low);
00192       const ConstantInt* CI2 = cast<const ConstantInt>(C2.High);
00193       return CI1->getValue().slt(CI2->getValue());
00194     }
00195   };
00196 
00197   struct CaseBitsCmp {
00198     bool operator()(const CaseBits &C1, const CaseBits &C2) {
00199       return C1.Bits > C2.Bits;
00200     }
00201   };
00202 
00203   size_t Clusterify(CaseVector &Cases, const SwitchInst &SI);
00204 
00205   /// CaseBlock - This structure is used to communicate between
00206   /// SelectionDAGBuilder and SDISel for the code generation of additional basic
00207   /// blocks needed by multi-case switch statements.
00208   struct CaseBlock {
00209     CaseBlock(ISD::CondCode cc, const Value *cmplhs, const Value *cmprhs,
00210               const Value *cmpmiddle,
00211               MachineBasicBlock *truebb, MachineBasicBlock *falsebb,
00212               MachineBasicBlock *me,
00213               uint32_t trueweight = 0, uint32_t falseweight = 0)
00214       : CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs),
00215         TrueBB(truebb), FalseBB(falsebb), ThisBB(me),
00216         TrueWeight(trueweight), FalseWeight(falseweight) { }
00217 
00218     // CC - the condition code to use for the case block's setcc node
00219     ISD::CondCode CC;
00220 
00221     // CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit.
00222     // Emit by default LHS op RHS. MHS is used for range comparisons:
00223     // If MHS is not null: (LHS <= MHS) and (MHS <= RHS).
00224     const Value *CmpLHS, *CmpMHS, *CmpRHS;
00225 
00226     // TrueBB/FalseBB - the block to branch to if the setcc is true/false.
00227     MachineBasicBlock *TrueBB, *FalseBB;
00228 
00229     // ThisBB - the block into which to emit the code for the setcc and branches
00230     MachineBasicBlock *ThisBB;
00231 
00232     // TrueWeight/FalseWeight - branch weights.
00233     uint32_t TrueWeight, FalseWeight;
00234   };
00235 
00236   struct JumpTable {
00237     JumpTable(unsigned R, unsigned J, MachineBasicBlock *M,
00238               MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {}
00239 
00240     /// Reg - the virtual register containing the index of the jump table entry
00241     //. to jump to.
00242     unsigned Reg;
00243     /// JTI - the JumpTableIndex for this jump table in the function.
00244     unsigned JTI;
00245     /// MBB - the MBB into which to emit the code for the indirect jump.
00246     MachineBasicBlock *MBB;
00247     /// Default - the MBB of the default bb, which is a successor of the range
00248     /// check MBB.  This is when updating PHI nodes in successors.
00249     MachineBasicBlock *Default;
00250   };
00251   struct JumpTableHeader {
00252     JumpTableHeader(APInt F, APInt L, const Value *SV, MachineBasicBlock *H,
00253                     bool E = false):
00254       First(F), Last(L), SValue(SV), HeaderBB(H), Emitted(E) {}
00255     APInt First;
00256     APInt Last;
00257     const Value *SValue;
00258     MachineBasicBlock *HeaderBB;
00259     bool Emitted;
00260   };
00261   typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock;
00262 
00263   struct BitTestCase {
00264     BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr,
00265                 uint32_t Weight):
00266       Mask(M), ThisBB(T), TargetBB(Tr), ExtraWeight(Weight) { }
00267     uint64_t Mask;
00268     MachineBasicBlock *ThisBB;
00269     MachineBasicBlock *TargetBB;
00270     uint32_t ExtraWeight;
00271   };
00272 
00273   typedef SmallVector<BitTestCase, 3> BitTestInfo;
00274 
00275   struct BitTestBlock {
00276     BitTestBlock(APInt F, APInt R, const Value* SV,
00277                  unsigned Rg, MVT RgVT, bool E,
00278                  MachineBasicBlock* P, MachineBasicBlock* D,
00279                  const BitTestInfo& C):
00280       First(F), Range(R), SValue(SV), Reg(Rg), RegVT(RgVT), Emitted(E),
00281       Parent(P), Default(D), Cases(C) { }
00282     APInt First;
00283     APInt Range;
00284     const Value *SValue;
00285     unsigned Reg;
00286     MVT RegVT;
00287     bool Emitted;
00288     MachineBasicBlock *Parent;
00289     MachineBasicBlock *Default;
00290     BitTestInfo Cases;
00291   };
00292 
00293   /// A class which encapsulates all of the information needed to generate a
00294   /// stack protector check and signals to isel via its state being initialized
00295   /// that a stack protector needs to be generated.
00296   ///
00297   /// *NOTE* The following is a high level documentation of SelectionDAG Stack
00298   /// Protector Generation. The reason that it is placed here is for a lack of
00299   /// other good places to stick it.
00300   ///
00301   /// High Level Overview of SelectionDAG Stack Protector Generation:
00302   ///
00303   /// Previously, generation of stack protectors was done exclusively in the
00304   /// pre-SelectionDAG Codegen LLVM IR Pass "Stack Protector". This necessitated
00305   /// splitting basic blocks at the IR level to create the success/failure basic
00306   /// blocks in the tail of the basic block in question. As a result of this,
00307   /// calls that would have qualified for the sibling call optimization were no
00308   /// longer eligible for optimization since said calls were no longer right in
00309   /// the "tail position" (i.e. the immediate predecessor of a ReturnInst
00310   /// instruction).
00311   ///
00312   /// Then it was noticed that since the sibling call optimization causes the
00313   /// callee to reuse the caller's stack, if we could delay the generation of
00314   /// the stack protector check until later in CodeGen after the sibling call
00315   /// decision was made, we get both the tail call optimization and the stack
00316   /// protector check!
00317   ///
00318   /// A few goals in solving this problem were:
00319   ///
00320   ///   1. Preserve the architecture independence of stack protector generation.
00321   ///
00322   ///   2. Preserve the normal IR level stack protector check for platforms like
00323   ///      OpenBSD for which we support platform-specific stack protector
00324   ///      generation.
00325   ///
00326   /// The main problem that guided the present solution is that one can not
00327   /// solve this problem in an architecture independent manner at the IR level
00328   /// only. This is because:
00329   ///
00330   ///   1. The decision on whether or not to perform a sibling call on certain
00331   ///      platforms (for instance i386) requires lower level information
00332   ///      related to available registers that can not be known at the IR level.
00333   ///
00334   ///   2. Even if the previous point were not true, the decision on whether to
00335   ///      perform a tail call is done in LowerCallTo in SelectionDAG which
00336   ///      occurs after the Stack Protector Pass. As a result, one would need to
00337   ///      put the relevant callinst into the stack protector check success
00338   ///      basic block (where the return inst is placed) and then move it back
00339   ///      later at SelectionDAG/MI time before the stack protector check if the
00340   ///      tail call optimization failed. The MI level option was nixed
00341   ///      immediately since it would require platform-specific pattern
00342   ///      matching. The SelectionDAG level option was nixed because
00343   ///      SelectionDAG only processes one IR level basic block at a time
00344   ///      implying one could not create a DAG Combine to move the callinst.
00345   ///
00346   /// To get around this problem a few things were realized:
00347   ///
00348   ///   1. While one can not handle multiple IR level basic blocks at the
00349   ///      SelectionDAG Level, one can generate multiple machine basic blocks
00350   ///      for one IR level basic block. This is how we handle bit tests and
00351   ///      switches.
00352   ///
00353   ///   2. At the MI level, tail calls are represented via a special return
00354   ///      MIInst called "tcreturn". Thus if we know the basic block in which we
00355   ///      wish to insert the stack protector check, we get the correct behavior
00356   ///      by always inserting the stack protector check right before the return
00357   ///      statement. This is a "magical transformation" since no matter where
00358   ///      the stack protector check intrinsic is, we always insert the stack
00359   ///      protector check code at the end of the BB.
00360   ///
00361   /// Given the aforementioned constraints, the following solution was devised:
00362   ///
00363   ///   1. On platforms that do not support SelectionDAG stack protector check
00364   ///      generation, allow for the normal IR level stack protector check
00365   ///      generation to continue.
00366   ///
00367   ///   2. On platforms that do support SelectionDAG stack protector check
00368   ///      generation:
00369   ///
00370   ///     a. Use the IR level stack protector pass to decide if a stack
00371   ///        protector is required/which BB we insert the stack protector check
00372   ///        in by reusing the logic already therein. If we wish to generate a
00373   ///        stack protector check in a basic block, we place a special IR
00374   ///        intrinsic called llvm.stackprotectorcheck right before the BB's
00375   ///        returninst or if there is a callinst that could potentially be
00376   ///        sibling call optimized, before the call inst.
00377   ///
00378   ///     b. Then when a BB with said intrinsic is processed, we codegen the BB
00379   ///        normally via SelectBasicBlock. In said process, when we visit the
00380   ///        stack protector check, we do not actually emit anything into the
00381   ///        BB. Instead, we just initialize the stack protector descriptor
00382   ///        class (which involves stashing information/creating the success
00383   ///        mbbb and the failure mbb if we have not created one for this
00384   ///        function yet) and export the guard variable that we are going to
00385   ///        compare.
00386   ///
00387   ///     c. After we finish selecting the basic block, in FinishBasicBlock if
00388   ///        the StackProtectorDescriptor attached to the SelectionDAGBuilder is
00389   ///        initialized, we first find a splice point in the parent basic block
00390   ///        before the terminator and then splice the terminator of said basic
00391   ///        block into the success basic block. Then we code-gen a new tail for
00392   ///        the parent basic block consisting of the two loads, the comparison,
00393   ///        and finally two branches to the success/failure basic blocks. We
00394   ///        conclude by code-gening the failure basic block if we have not
00395   ///        code-gened it already (all stack protector checks we generate in
00396   ///        the same function, use the same failure basic block).
00397   class StackProtectorDescriptor {
00398   public:
00399     StackProtectorDescriptor() : ParentMBB(nullptr), SuccessMBB(nullptr),
00400                                  FailureMBB(nullptr), Guard(nullptr),
00401                                  GuardReg(0) { }
00402     ~StackProtectorDescriptor() { }
00403 
00404     /// Returns true if all fields of the stack protector descriptor are
00405     /// initialized implying that we should/are ready to emit a stack protector.
00406     bool shouldEmitStackProtector() const {
00407       return ParentMBB && SuccessMBB && FailureMBB && Guard;
00408     }
00409 
00410     /// Initialize the stack protector descriptor structure for a new basic
00411     /// block.
00412     void initialize(const BasicBlock *BB,
00413                     MachineBasicBlock *MBB,
00414                     const CallInst &StackProtCheckCall) {
00415       // Make sure we are not initialized yet.
00416       assert(!shouldEmitStackProtector() && "Stack Protector Descriptor is "
00417              "already initialized!");
00418       ParentMBB = MBB;
00419       SuccessMBB = AddSuccessorMBB(BB, MBB);
00420       FailureMBB = AddSuccessorMBB(BB, MBB, FailureMBB);
00421       if (!Guard)
00422         Guard = StackProtCheckCall.getArgOperand(0);
00423     }
00424 
00425     /// Reset state that changes when we handle different basic blocks.
00426     ///
00427     /// This currently includes:
00428     ///
00429     /// 1. The specific basic block we are generating a
00430     /// stack protector for (ParentMBB).
00431     ///
00432     /// 2. The successor machine basic block that will contain the tail of
00433     /// parent mbb after we create the stack protector check (SuccessMBB). This
00434     /// BB is visited only on stack protector check success.
00435     void resetPerBBState() {
00436       ParentMBB = nullptr;
00437       SuccessMBB = nullptr;
00438     }
00439 
00440     /// Reset state that only changes when we switch functions.
00441     ///
00442     /// This currently includes:
00443     ///
00444     /// 1. FailureMBB since we reuse the failure code path for all stack
00445     /// protector checks created in an individual function.
00446     ///
00447     /// 2.The guard variable since the guard variable we are checking against is
00448     /// always the same.
00449     void resetPerFunctionState() {
00450       FailureMBB = nullptr;
00451       Guard = nullptr;
00452     }
00453 
00454     MachineBasicBlock *getParentMBB() { return ParentMBB; }
00455     MachineBasicBlock *getSuccessMBB() { return SuccessMBB; }
00456     MachineBasicBlock *getFailureMBB() { return FailureMBB; }
00457     const Value *getGuard() { return Guard; }
00458 
00459     unsigned getGuardReg() const { return GuardReg; }
00460     void setGuardReg(unsigned R) { GuardReg = R; }
00461 
00462   private:
00463     /// The basic block for which we are generating the stack protector.
00464     ///
00465     /// As a result of stack protector generation, we will splice the
00466     /// terminators of this basic block into the successor mbb SuccessMBB and
00467     /// replace it with a compare/branch to the successor mbbs
00468     /// SuccessMBB/FailureMBB depending on whether or not the stack protector
00469     /// was violated.
00470     MachineBasicBlock *ParentMBB;
00471 
00472     /// A basic block visited on stack protector check success that contains the
00473     /// terminators of ParentMBB.
00474     MachineBasicBlock *SuccessMBB;
00475 
00476     /// This basic block visited on stack protector check failure that will
00477     /// contain a call to __stack_chk_fail().
00478     MachineBasicBlock *FailureMBB;
00479 
00480     /// The guard variable which we will compare against the stored value in the
00481     /// stack protector stack slot.
00482     const Value *Guard;
00483 
00484     /// The virtual register holding the stack guard value.
00485     unsigned GuardReg;
00486 
00487     /// Add a successor machine basic block to ParentMBB. If the successor mbb
00488     /// has not been created yet (i.e. if SuccMBB = 0), then the machine basic
00489     /// block will be created.
00490     MachineBasicBlock *AddSuccessorMBB(const BasicBlock *BB,
00491                                        MachineBasicBlock *ParentMBB,
00492                                        MachineBasicBlock *SuccMBB = nullptr);
00493   };
00494 
00495 private:
00496   const TargetMachine &TM;
00497 public:
00498   /// Lowest valid SDNodeOrder. The special case 0 is reserved for scheduling
00499   /// nodes without a corresponding SDNode.
00500   static const unsigned LowestSDNodeOrder = 1;
00501 
00502   SelectionDAG &DAG;
00503   const DataLayout *DL;
00504   AliasAnalysis *AA;
00505   const TargetLibraryInfo *LibInfo;
00506 
00507   /// SwitchCases - Vector of CaseBlock structures used to communicate
00508   /// SwitchInst code generation information.
00509   std::vector<CaseBlock> SwitchCases;
00510   /// JTCases - Vector of JumpTable structures used to communicate
00511   /// SwitchInst code generation information.
00512   std::vector<JumpTableBlock> JTCases;
00513   /// BitTestCases - Vector of BitTestBlock structures used to communicate
00514   /// SwitchInst code generation information.
00515   std::vector<BitTestBlock> BitTestCases;
00516   /// A StackProtectorDescriptor structure used to communicate stack protector
00517   /// information in between SelectBasicBlock and FinishBasicBlock.
00518   StackProtectorDescriptor SPDescriptor;
00519 
00520   // Emit PHI-node-operand constants only once even if used by multiple
00521   // PHI nodes.
00522   DenseMap<const Constant *, unsigned> ConstantsOut;
00523 
00524   /// FuncInfo - Information about the function as a whole.
00525   ///
00526   FunctionLoweringInfo &FuncInfo;
00527 
00528   /// OptLevel - What optimization level we're generating code for.
00529   ///
00530   CodeGenOpt::Level OptLevel;
00531 
00532   /// GFI - Garbage collection metadata for the function.
00533   GCFunctionInfo *GFI;
00534 
00535   /// LPadToCallSiteMap - Map a landing pad to the call site indexes.
00536   DenseMap<MachineBasicBlock*, SmallVector<unsigned, 4> > LPadToCallSiteMap;
00537 
00538   /// HasTailCall - This is set to true if a call in the current
00539   /// block has been translated as a tail call. In this case,
00540   /// no subsequent DAG nodes should be created.
00541   ///
00542   bool HasTailCall;
00543 
00544   LLVMContext *Context;
00545 
00546   SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo,
00547                       CodeGenOpt::Level ol)
00548     : CurInst(nullptr), SDNodeOrder(LowestSDNodeOrder), TM(dag.getTarget()),
00549       DAG(dag), FuncInfo(funcinfo), OptLevel(ol),
00550       HasTailCall(false) {
00551   }
00552 
00553   void init(GCFunctionInfo *gfi, AliasAnalysis &aa,
00554             const TargetLibraryInfo *li);
00555 
00556   /// clear - Clear out the current SelectionDAG and the associated
00557   /// state and prepare this SelectionDAGBuilder object to be used
00558   /// for a new block. This doesn't clear out information about
00559   /// additional blocks that are needed to complete switch lowering
00560   /// or PHI node updating; that information is cleared out as it is
00561   /// consumed.
00562   void clear();
00563 
00564   /// clearDanglingDebugInfo - Clear the dangling debug information
00565   /// map. This function is separated from the clear so that debug
00566   /// information that is dangling in a basic block can be properly
00567   /// resolved in a different basic block. This allows the
00568   /// SelectionDAG to resolve dangling debug information attached
00569   /// to PHI nodes.
00570   void clearDanglingDebugInfo();
00571 
00572   /// getRoot - Return the current virtual root of the Selection DAG,
00573   /// flushing any PendingLoad items. This must be done before emitting
00574   /// a store or any other node that may need to be ordered after any
00575   /// prior load instructions.
00576   ///
00577   SDValue getRoot();
00578 
00579   /// getControlRoot - Similar to getRoot, but instead of flushing all the
00580   /// PendingLoad items, flush all the PendingExports items. It is necessary
00581   /// to do this before emitting a terminator instruction.
00582   ///
00583   SDValue getControlRoot();
00584 
00585   SDLoc getCurSDLoc() const {
00586     return SDLoc(CurInst, SDNodeOrder);
00587   }
00588 
00589   DebugLoc getCurDebugLoc() const {
00590     return CurInst ? CurInst->getDebugLoc() : DebugLoc();
00591   }
00592 
00593   unsigned getSDNodeOrder() const { return SDNodeOrder; }
00594 
00595   void CopyValueToVirtualRegister(const Value *V, unsigned Reg);
00596 
00597   void visit(const Instruction &I);
00598 
00599   void visit(unsigned Opcode, const User &I);
00600 
00601   // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
00602   // generate the debug data structures now that we've seen its definition.
00603   void resolveDanglingDebugInfo(const Value *V, SDValue Val);
00604   SDValue getValue(const Value *V);
00605   SDValue getNonRegisterValue(const Value *V);
00606   SDValue getValueImpl(const Value *V);
00607 
00608   void setValue(const Value *V, SDValue NewN) {
00609     SDValue &N = NodeMap[V];
00610     assert(!N.getNode() && "Already set a value for this node!");
00611     N = NewN;
00612   }
00613 
00614   void setUnusedArgValue(const Value *V, SDValue NewN) {
00615     SDValue &N = UnusedArgNodeMap[V];
00616     assert(!N.getNode() && "Already set a value for this node!");
00617     N = NewN;
00618   }
00619 
00620   void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB,
00621                             MachineBasicBlock *FBB, MachineBasicBlock *CurBB,
00622                             MachineBasicBlock *SwitchBB, unsigned Opc,
00623                             uint32_t TW, uint32_t FW);
00624   void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB,
00625                                     MachineBasicBlock *FBB,
00626                                     MachineBasicBlock *CurBB,
00627                                     MachineBasicBlock *SwitchBB,
00628                                     uint32_t TW, uint32_t FW);
00629   bool ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases);
00630   bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB);
00631   void CopyToExportRegsIfNeeded(const Value *V);
00632   void ExportFromCurrentBlock(const Value *V);
00633   void LowerCallTo(ImmutableCallSite CS, SDValue Callee, bool IsTailCall,
00634                    MachineBasicBlock *LandingPad = nullptr);
00635 
00636   std::pair<SDValue, SDValue> LowerCallOperands(const CallInst &CI,
00637                                                 unsigned ArgIdx,
00638                                                 unsigned NumArgs,
00639                                                 SDValue Callee,
00640                                                 bool useVoidTy = false);
00641 
00642   /// UpdateSplitBlock - When an MBB was split during scheduling, update the
00643   /// references that need to refer to the last resulting block.
00644   void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last);
00645 
00646 private:
00647   // Terminator instructions.
00648   void visitRet(const ReturnInst &I);
00649   void visitBr(const BranchInst &I);
00650   void visitSwitch(const SwitchInst &I);
00651   void visitIndirectBr(const IndirectBrInst &I);
00652   void visitUnreachable(const UnreachableInst &I);
00653 
00654   // Helpers for visitSwitch
00655   bool handleSmallSwitchRange(CaseRec& CR,
00656                               CaseRecVector& WorkList,
00657                               const Value* SV,
00658                               MachineBasicBlock* Default,
00659                               MachineBasicBlock *SwitchBB);
00660   bool handleJTSwitchCase(CaseRec& CR,
00661                           CaseRecVector& WorkList,
00662                           const Value* SV,
00663                           MachineBasicBlock* Default,
00664                           MachineBasicBlock *SwitchBB);
00665   bool handleBTSplitSwitchCase(CaseRec& CR,
00666                                CaseRecVector& WorkList,
00667                                const Value* SV,
00668                                MachineBasicBlock* Default,
00669                                MachineBasicBlock *SwitchBB);
00670   bool handleBitTestsSwitchCase(CaseRec& CR,
00671                                 CaseRecVector& WorkList,
00672                                 const Value* SV,
00673                                 MachineBasicBlock* Default,
00674                                 MachineBasicBlock *SwitchBB);
00675 
00676   uint32_t getEdgeWeight(const MachineBasicBlock *Src,
00677                          const MachineBasicBlock *Dst) const;
00678   void addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst,
00679                               uint32_t Weight = 0);
00680 public:
00681   void visitSwitchCase(CaseBlock &CB,
00682                        MachineBasicBlock *SwitchBB);
00683   void visitSPDescriptorParent(StackProtectorDescriptor &SPD,
00684                                MachineBasicBlock *ParentBB);
00685   void visitSPDescriptorFailure(StackProtectorDescriptor &SPD);
00686   void visitBitTestHeader(BitTestBlock &B, MachineBasicBlock *SwitchBB);
00687   void visitBitTestCase(BitTestBlock &BB,
00688                         MachineBasicBlock* NextMBB,
00689                         uint32_t BranchWeightToNext,
00690                         unsigned Reg,
00691                         BitTestCase &B,
00692                         MachineBasicBlock *SwitchBB);
00693   void visitJumpTable(JumpTable &JT);
00694   void visitJumpTableHeader(JumpTable &JT, JumpTableHeader &JTH,
00695                             MachineBasicBlock *SwitchBB);
00696 
00697 private:
00698   // These all get lowered before this pass.
00699   void visitInvoke(const InvokeInst &I);
00700   void visitResume(const ResumeInst &I);
00701 
00702   void visitBinary(const User &I, unsigned OpCode);
00703   void visitShift(const User &I, unsigned Opcode);
00704   void visitAdd(const User &I)  { visitBinary(I, ISD::ADD); }
00705   void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); }
00706   void visitSub(const User &I)  { visitBinary(I, ISD::SUB); }
00707   void visitFSub(const User &I);
00708   void visitMul(const User &I)  { visitBinary(I, ISD::MUL); }
00709   void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); }
00710   void visitURem(const User &I) { visitBinary(I, ISD::UREM); }
00711   void visitSRem(const User &I) { visitBinary(I, ISD::SREM); }
00712   void visitFRem(const User &I) { visitBinary(I, ISD::FREM); }
00713   void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); }
00714   void visitSDiv(const User &I);
00715   void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); }
00716   void visitAnd (const User &I) { visitBinary(I, ISD::AND); }
00717   void visitOr  (const User &I) { visitBinary(I, ISD::OR); }
00718   void visitXor (const User &I) { visitBinary(I, ISD::XOR); }
00719   void visitShl (const User &I) { visitShift(I, ISD::SHL); }
00720   void visitLShr(const User &I) { visitShift(I, ISD::SRL); }
00721   void visitAShr(const User &I) { visitShift(I, ISD::SRA); }
00722   void visitICmp(const User &I);
00723   void visitFCmp(const User &I);
00724   // Visit the conversion instructions
00725   void visitTrunc(const User &I);
00726   void visitZExt(const User &I);
00727   void visitSExt(const User &I);
00728   void visitFPTrunc(const User &I);
00729   void visitFPExt(const User &I);
00730   void visitFPToUI(const User &I);
00731   void visitFPToSI(const User &I);
00732   void visitUIToFP(const User &I);
00733   void visitSIToFP(const User &I);
00734   void visitPtrToInt(const User &I);
00735   void visitIntToPtr(const User &I);
00736   void visitBitCast(const User &I);
00737   void visitAddrSpaceCast(const User &I);
00738 
00739   void visitExtractElement(const User &I);
00740   void visitInsertElement(const User &I);
00741   void visitShuffleVector(const User &I);
00742 
00743   void visitExtractValue(const ExtractValueInst &I);
00744   void visitInsertValue(const InsertValueInst &I);
00745   void visitLandingPad(const LandingPadInst &I);
00746 
00747   void visitGetElementPtr(const User &I);
00748   void visitSelect(const User &I);
00749 
00750   void visitAlloca(const AllocaInst &I);
00751   void visitLoad(const LoadInst &I);
00752   void visitStore(const StoreInst &I);
00753   void visitAtomicCmpXchg(const AtomicCmpXchgInst &I);
00754   void visitAtomicRMW(const AtomicRMWInst &I);
00755   void visitFence(const FenceInst &I);
00756   void visitPHI(const PHINode &I);
00757   void visitCall(const CallInst &I);
00758   bool visitMemCmpCall(const CallInst &I);
00759   bool visitMemChrCall(const CallInst &I);
00760   bool visitStrCpyCall(const CallInst &I, bool isStpcpy);
00761   bool visitStrCmpCall(const CallInst &I);
00762   bool visitStrLenCall(const CallInst &I);
00763   bool visitStrNLenCall(const CallInst &I);
00764   bool visitUnaryFloatCall(const CallInst &I, unsigned Opcode);
00765   void visitAtomicLoad(const LoadInst &I);
00766   void visitAtomicStore(const StoreInst &I);
00767 
00768   void visitInlineAsm(ImmutableCallSite CS);
00769   const char *visitIntrinsicCall(const CallInst &I, unsigned Intrinsic);
00770   void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic);
00771 
00772   void visitVAStart(const CallInst &I);
00773   void visitVAArg(const VAArgInst &I);
00774   void visitVAEnd(const CallInst &I);
00775   void visitVACopy(const CallInst &I);
00776   void visitStackmap(const CallInst &I);
00777   void visitPatchpoint(const CallInst &I);
00778 
00779   void visitUserOp1(const Instruction &I) {
00780     llvm_unreachable("UserOp1 should not exist at instruction selection time!");
00781   }
00782   void visitUserOp2(const Instruction &I) {
00783     llvm_unreachable("UserOp2 should not exist at instruction selection time!");
00784   }
00785 
00786   void processIntegerCallValue(const Instruction &I,
00787                                SDValue Value, bool IsSigned);
00788 
00789   void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
00790 
00791   /// EmitFuncArgumentDbgValue - If V is an function argument then create
00792   /// corresponding DBG_VALUE machine instruction for it now. At the end of
00793   /// instruction selection, they will be inserted to the entry BB.
00794   bool EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
00795                                 int64_t Offset, bool IsIndirect,
00796                                 const SDValue &N);
00797 };
00798 
00799 } // end namespace llvm
00800 
00801 #endif