1f4a2713aSLionel Sambuc //===-- SelectionDAGBuilder.h - Selection-DAG building --------*- C++ -*---===// 2f4a2713aSLionel Sambuc // 3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4f4a2713aSLionel Sambuc // 5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7f4a2713aSLionel Sambuc // 8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9f4a2713aSLionel Sambuc // 10f4a2713aSLionel Sambuc // This implements routines for translating from LLVM IR into SelectionDAG IR. 11f4a2713aSLionel Sambuc // 12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 13f4a2713aSLionel Sambuc 14*0a6a1f1dSLionel Sambuc #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H 15*0a6a1f1dSLionel Sambuc #define LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H 16f4a2713aSLionel Sambuc 17*0a6a1f1dSLionel Sambuc #include "StatepointLowering.h" 18f4a2713aSLionel Sambuc #include "llvm/ADT/APInt.h" 19f4a2713aSLionel Sambuc #include "llvm/ADT/DenseMap.h" 20f4a2713aSLionel Sambuc #include "llvm/CodeGen/SelectionDAG.h" 21f4a2713aSLionel Sambuc #include "llvm/CodeGen/SelectionDAGNodes.h" 22*0a6a1f1dSLionel Sambuc #include "llvm/IR/CallSite.h" 23f4a2713aSLionel Sambuc #include "llvm/IR/Constants.h" 24f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h" 25*0a6a1f1dSLionel Sambuc #include "llvm/Target/TargetLowering.h" 26f4a2713aSLionel Sambuc #include <vector> 27f4a2713aSLionel Sambuc 28f4a2713aSLionel Sambuc namespace llvm { 29f4a2713aSLionel Sambuc 30f4a2713aSLionel Sambuc class AddrSpaceCastInst; 31f4a2713aSLionel Sambuc class AliasAnalysis; 32f4a2713aSLionel Sambuc class AllocaInst; 33f4a2713aSLionel Sambuc class BasicBlock; 34f4a2713aSLionel Sambuc class BitCastInst; 35f4a2713aSLionel Sambuc class BranchInst; 36f4a2713aSLionel Sambuc class CallInst; 37f4a2713aSLionel Sambuc class DbgValueInst; 38f4a2713aSLionel Sambuc class ExtractElementInst; 39f4a2713aSLionel Sambuc class ExtractValueInst; 40f4a2713aSLionel Sambuc class FCmpInst; 41f4a2713aSLionel Sambuc class FPExtInst; 42f4a2713aSLionel Sambuc class FPToSIInst; 43f4a2713aSLionel Sambuc class FPToUIInst; 44f4a2713aSLionel Sambuc class FPTruncInst; 45f4a2713aSLionel Sambuc class Function; 46f4a2713aSLionel Sambuc class FunctionLoweringInfo; 47f4a2713aSLionel Sambuc class GetElementPtrInst; 48f4a2713aSLionel Sambuc class GCFunctionInfo; 49f4a2713aSLionel Sambuc class ICmpInst; 50f4a2713aSLionel Sambuc class IntToPtrInst; 51f4a2713aSLionel Sambuc class IndirectBrInst; 52f4a2713aSLionel Sambuc class InvokeInst; 53f4a2713aSLionel Sambuc class InsertElementInst; 54f4a2713aSLionel Sambuc class InsertValueInst; 55f4a2713aSLionel Sambuc class Instruction; 56f4a2713aSLionel Sambuc class LoadInst; 57f4a2713aSLionel Sambuc class MachineBasicBlock; 58f4a2713aSLionel Sambuc class MachineInstr; 59f4a2713aSLionel Sambuc class MachineRegisterInfo; 60f4a2713aSLionel Sambuc class MDNode; 61*0a6a1f1dSLionel Sambuc class MVT; 62f4a2713aSLionel Sambuc class PHINode; 63f4a2713aSLionel Sambuc class PtrToIntInst; 64f4a2713aSLionel Sambuc class ReturnInst; 65f4a2713aSLionel Sambuc class SDDbgValue; 66f4a2713aSLionel Sambuc class SExtInst; 67f4a2713aSLionel Sambuc class SelectInst; 68f4a2713aSLionel Sambuc class ShuffleVectorInst; 69f4a2713aSLionel Sambuc class SIToFPInst; 70f4a2713aSLionel Sambuc class StoreInst; 71f4a2713aSLionel Sambuc class SwitchInst; 72f4a2713aSLionel Sambuc class DataLayout; 73f4a2713aSLionel Sambuc class TargetLibraryInfo; 74f4a2713aSLionel Sambuc class TargetLowering; 75f4a2713aSLionel Sambuc class TruncInst; 76f4a2713aSLionel Sambuc class UIToFPInst; 77f4a2713aSLionel Sambuc class UnreachableInst; 78f4a2713aSLionel Sambuc class VAArgInst; 79f4a2713aSLionel Sambuc class ZExtInst; 80f4a2713aSLionel Sambuc 81f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 82f4a2713aSLionel Sambuc /// SelectionDAGBuilder - This is the common target-independent lowering 83f4a2713aSLionel Sambuc /// implementation that is parameterized by a TargetLowering object. 84f4a2713aSLionel Sambuc /// 85f4a2713aSLionel Sambuc class SelectionDAGBuilder { 86f4a2713aSLionel Sambuc /// CurInst - The current instruction being visited 87f4a2713aSLionel Sambuc const Instruction *CurInst; 88f4a2713aSLionel Sambuc 89f4a2713aSLionel Sambuc DenseMap<const Value*, SDValue> NodeMap; 90f4a2713aSLionel Sambuc 91f4a2713aSLionel Sambuc /// UnusedArgNodeMap - Maps argument value for unused arguments. This is used 92f4a2713aSLionel Sambuc /// to preserve debug information for incoming arguments. 93f4a2713aSLionel Sambuc DenseMap<const Value*, SDValue> UnusedArgNodeMap; 94f4a2713aSLionel Sambuc 95f4a2713aSLionel Sambuc /// DanglingDebugInfo - Helper type for DanglingDebugInfoMap. 96f4a2713aSLionel Sambuc class DanglingDebugInfo { 97f4a2713aSLionel Sambuc const DbgValueInst* DI; 98f4a2713aSLionel Sambuc DebugLoc dl; 99f4a2713aSLionel Sambuc unsigned SDNodeOrder; 100f4a2713aSLionel Sambuc public: DanglingDebugInfo()101*0a6a1f1dSLionel Sambuc DanglingDebugInfo() : DI(nullptr), dl(DebugLoc()), SDNodeOrder(0) { } DanglingDebugInfo(const DbgValueInst * di,DebugLoc DL,unsigned SDNO)102f4a2713aSLionel Sambuc DanglingDebugInfo(const DbgValueInst *di, DebugLoc DL, unsigned SDNO) : 103f4a2713aSLionel Sambuc DI(di), dl(DL), SDNodeOrder(SDNO) { } getDI()104f4a2713aSLionel Sambuc const DbgValueInst* getDI() { return DI; } getdl()105f4a2713aSLionel Sambuc DebugLoc getdl() { return dl; } getSDNodeOrder()106f4a2713aSLionel Sambuc unsigned getSDNodeOrder() { return SDNodeOrder; } 107f4a2713aSLionel Sambuc }; 108f4a2713aSLionel Sambuc 109f4a2713aSLionel Sambuc /// DanglingDebugInfoMap - Keeps track of dbg_values for which we have not 110f4a2713aSLionel Sambuc /// yet seen the referent. We defer handling these until we do see it. 111f4a2713aSLionel Sambuc DenseMap<const Value*, DanglingDebugInfo> DanglingDebugInfoMap; 112f4a2713aSLionel Sambuc 113f4a2713aSLionel Sambuc public: 114f4a2713aSLionel Sambuc /// PendingLoads - Loads are not emitted to the program immediately. We bunch 115f4a2713aSLionel Sambuc /// them up and then emit token factor nodes when possible. This allows us to 116f4a2713aSLionel Sambuc /// get simple disambiguation between loads without worrying about alias 117f4a2713aSLionel Sambuc /// analysis. 118f4a2713aSLionel Sambuc SmallVector<SDValue, 8> PendingLoads; 119*0a6a1f1dSLionel Sambuc 120*0a6a1f1dSLionel Sambuc /// State used while lowering a statepoint sequence (gc_statepoint, 121*0a6a1f1dSLionel Sambuc /// gc_relocate, and gc_result). See StatepointLowering.hpp/cpp for details. 122*0a6a1f1dSLionel Sambuc StatepointLoweringState StatepointLowering; 123f4a2713aSLionel Sambuc private: 124f4a2713aSLionel Sambuc 125f4a2713aSLionel Sambuc /// PendingExports - CopyToReg nodes that copy values to virtual registers 126f4a2713aSLionel Sambuc /// for export to other blocks need to be emitted before any terminator 127f4a2713aSLionel Sambuc /// instruction, but they have no other ordering requirements. We bunch them 128f4a2713aSLionel Sambuc /// up and the emit a single tokenfactor for them just before terminator 129f4a2713aSLionel Sambuc /// instructions. 130f4a2713aSLionel Sambuc SmallVector<SDValue, 8> PendingExports; 131f4a2713aSLionel Sambuc 132f4a2713aSLionel Sambuc /// SDNodeOrder - A unique monotonically increasing number used to order the 133f4a2713aSLionel Sambuc /// SDNodes we create. 134f4a2713aSLionel Sambuc unsigned SDNodeOrder; 135f4a2713aSLionel Sambuc 136f4a2713aSLionel Sambuc /// Case - A struct to record the Value for a switch case, and the 137f4a2713aSLionel Sambuc /// case's target basic block. 138f4a2713aSLionel Sambuc struct Case { 139f4a2713aSLionel Sambuc const Constant *Low; 140f4a2713aSLionel Sambuc const Constant *High; 141f4a2713aSLionel Sambuc MachineBasicBlock* BB; 142f4a2713aSLionel Sambuc uint32_t ExtraWeight; 143f4a2713aSLionel Sambuc CaseCase144*0a6a1f1dSLionel Sambuc Case() : Low(nullptr), High(nullptr), BB(nullptr), ExtraWeight(0) { } CaseCase145f4a2713aSLionel Sambuc Case(const Constant *low, const Constant *high, MachineBasicBlock *bb, 146f4a2713aSLionel Sambuc uint32_t extraweight) : Low(low), High(high), BB(bb), 147f4a2713aSLionel Sambuc ExtraWeight(extraweight) { } 148f4a2713aSLionel Sambuc sizeCase149f4a2713aSLionel Sambuc APInt size() const { 150f4a2713aSLionel Sambuc const APInt &rHigh = cast<ConstantInt>(High)->getValue(); 151f4a2713aSLionel Sambuc const APInt &rLow = cast<ConstantInt>(Low)->getValue(); 152f4a2713aSLionel Sambuc return (rHigh - rLow + 1ULL); 153f4a2713aSLionel Sambuc } 154f4a2713aSLionel Sambuc }; 155f4a2713aSLionel Sambuc 156f4a2713aSLionel Sambuc struct CaseBits { 157f4a2713aSLionel Sambuc uint64_t Mask; 158f4a2713aSLionel Sambuc MachineBasicBlock* BB; 159f4a2713aSLionel Sambuc unsigned Bits; 160f4a2713aSLionel Sambuc uint32_t ExtraWeight; 161f4a2713aSLionel Sambuc CaseBitsCaseBits162f4a2713aSLionel Sambuc CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits, 163f4a2713aSLionel Sambuc uint32_t Weight): 164f4a2713aSLionel Sambuc Mask(mask), BB(bb), Bits(bits), ExtraWeight(Weight) { } 165f4a2713aSLionel Sambuc }; 166f4a2713aSLionel Sambuc 167f4a2713aSLionel Sambuc typedef std::vector<Case> CaseVector; 168f4a2713aSLionel Sambuc typedef std::vector<CaseBits> CaseBitsVector; 169f4a2713aSLionel Sambuc typedef CaseVector::iterator CaseItr; 170f4a2713aSLionel Sambuc typedef std::pair<CaseItr, CaseItr> CaseRange; 171f4a2713aSLionel Sambuc 172f4a2713aSLionel Sambuc /// CaseRec - A struct with ctor used in lowering switches to a binary tree 173f4a2713aSLionel Sambuc /// of conditional branches. 174f4a2713aSLionel Sambuc struct CaseRec { CaseRecCaseRec175f4a2713aSLionel Sambuc CaseRec(MachineBasicBlock *bb, const Constant *lt, const Constant *ge, 176f4a2713aSLionel Sambuc CaseRange r) : 177f4a2713aSLionel Sambuc CaseBB(bb), LT(lt), GE(ge), Range(r) {} 178f4a2713aSLionel Sambuc 179f4a2713aSLionel Sambuc /// CaseBB - The MBB in which to emit the compare and branch 180f4a2713aSLionel Sambuc MachineBasicBlock *CaseBB; 181f4a2713aSLionel Sambuc /// LT, GE - If nonzero, we know the current case value must be less-than or 182f4a2713aSLionel Sambuc /// greater-than-or-equal-to these Constants. 183f4a2713aSLionel Sambuc const Constant *LT; 184f4a2713aSLionel Sambuc const Constant *GE; 185f4a2713aSLionel Sambuc /// Range - A pair of iterators representing the range of case values to be 186f4a2713aSLionel Sambuc /// processed at this point in the binary search tree. 187f4a2713aSLionel Sambuc CaseRange Range; 188f4a2713aSLionel Sambuc }; 189f4a2713aSLionel Sambuc 190f4a2713aSLionel Sambuc typedef std::vector<CaseRec> CaseRecVector; 191f4a2713aSLionel Sambuc 192f4a2713aSLionel Sambuc /// The comparison function for sorting the switch case values in the vector. 193f4a2713aSLionel Sambuc /// WARNING: Case ranges should be disjoint! 194f4a2713aSLionel Sambuc struct CaseCmp { operatorCaseCmp195f4a2713aSLionel Sambuc bool operator()(const Case &C1, const Case &C2) { 196f4a2713aSLionel Sambuc assert(isa<ConstantInt>(C1.Low) && isa<ConstantInt>(C2.High)); 197f4a2713aSLionel Sambuc const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low); 198f4a2713aSLionel Sambuc const ConstantInt* CI2 = cast<const ConstantInt>(C2.High); 199f4a2713aSLionel Sambuc return CI1->getValue().slt(CI2->getValue()); 200f4a2713aSLionel Sambuc } 201f4a2713aSLionel Sambuc }; 202f4a2713aSLionel Sambuc 203f4a2713aSLionel Sambuc struct CaseBitsCmp { operatorCaseBitsCmp204f4a2713aSLionel Sambuc bool operator()(const CaseBits &C1, const CaseBits &C2) { 205f4a2713aSLionel Sambuc return C1.Bits > C2.Bits; 206f4a2713aSLionel Sambuc } 207f4a2713aSLionel Sambuc }; 208f4a2713aSLionel Sambuc 209*0a6a1f1dSLionel Sambuc void Clusterify(CaseVector &Cases, const SwitchInst &SI); 210f4a2713aSLionel Sambuc 211f4a2713aSLionel Sambuc /// CaseBlock - This structure is used to communicate between 212f4a2713aSLionel Sambuc /// SelectionDAGBuilder and SDISel for the code generation of additional basic 213f4a2713aSLionel Sambuc /// blocks needed by multi-case switch statements. 214f4a2713aSLionel Sambuc struct CaseBlock { 215f4a2713aSLionel Sambuc CaseBlock(ISD::CondCode cc, const Value *cmplhs, const Value *cmprhs, 216f4a2713aSLionel Sambuc const Value *cmpmiddle, 217f4a2713aSLionel Sambuc MachineBasicBlock *truebb, MachineBasicBlock *falsebb, 218f4a2713aSLionel Sambuc MachineBasicBlock *me, 219f4a2713aSLionel Sambuc uint32_t trueweight = 0, uint32_t falseweight = 0) CCCaseBlock220f4a2713aSLionel Sambuc : CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs), 221f4a2713aSLionel Sambuc TrueBB(truebb), FalseBB(falsebb), ThisBB(me), 222f4a2713aSLionel Sambuc TrueWeight(trueweight), FalseWeight(falseweight) { } 223f4a2713aSLionel Sambuc 224f4a2713aSLionel Sambuc // CC - the condition code to use for the case block's setcc node 225f4a2713aSLionel Sambuc ISD::CondCode CC; 226f4a2713aSLionel Sambuc 227f4a2713aSLionel Sambuc // CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit. 228f4a2713aSLionel Sambuc // Emit by default LHS op RHS. MHS is used for range comparisons: 229f4a2713aSLionel Sambuc // If MHS is not null: (LHS <= MHS) and (MHS <= RHS). 230f4a2713aSLionel Sambuc const Value *CmpLHS, *CmpMHS, *CmpRHS; 231f4a2713aSLionel Sambuc 232f4a2713aSLionel Sambuc // TrueBB/FalseBB - the block to branch to if the setcc is true/false. 233f4a2713aSLionel Sambuc MachineBasicBlock *TrueBB, *FalseBB; 234f4a2713aSLionel Sambuc 235f4a2713aSLionel Sambuc // ThisBB - the block into which to emit the code for the setcc and branches 236f4a2713aSLionel Sambuc MachineBasicBlock *ThisBB; 237f4a2713aSLionel Sambuc 238f4a2713aSLionel Sambuc // TrueWeight/FalseWeight - branch weights. 239f4a2713aSLionel Sambuc uint32_t TrueWeight, FalseWeight; 240f4a2713aSLionel Sambuc }; 241f4a2713aSLionel Sambuc 242f4a2713aSLionel Sambuc struct JumpTable { JumpTableJumpTable243f4a2713aSLionel Sambuc JumpTable(unsigned R, unsigned J, MachineBasicBlock *M, 244f4a2713aSLionel Sambuc MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {} 245f4a2713aSLionel Sambuc 246f4a2713aSLionel Sambuc /// Reg - the virtual register containing the index of the jump table entry 247f4a2713aSLionel Sambuc //. to jump to. 248f4a2713aSLionel Sambuc unsigned Reg; 249f4a2713aSLionel Sambuc /// JTI - the JumpTableIndex for this jump table in the function. 250f4a2713aSLionel Sambuc unsigned JTI; 251f4a2713aSLionel Sambuc /// MBB - the MBB into which to emit the code for the indirect jump. 252f4a2713aSLionel Sambuc MachineBasicBlock *MBB; 253f4a2713aSLionel Sambuc /// Default - the MBB of the default bb, which is a successor of the range 254f4a2713aSLionel Sambuc /// check MBB. This is when updating PHI nodes in successors. 255f4a2713aSLionel Sambuc MachineBasicBlock *Default; 256f4a2713aSLionel Sambuc }; 257f4a2713aSLionel Sambuc struct JumpTableHeader { 258f4a2713aSLionel Sambuc JumpTableHeader(APInt F, APInt L, const Value *SV, MachineBasicBlock *H, 259f4a2713aSLionel Sambuc bool E = false): FirstJumpTableHeader260f4a2713aSLionel Sambuc First(F), Last(L), SValue(SV), HeaderBB(H), Emitted(E) {} 261f4a2713aSLionel Sambuc APInt First; 262f4a2713aSLionel Sambuc APInt Last; 263f4a2713aSLionel Sambuc const Value *SValue; 264f4a2713aSLionel Sambuc MachineBasicBlock *HeaderBB; 265f4a2713aSLionel Sambuc bool Emitted; 266f4a2713aSLionel Sambuc }; 267f4a2713aSLionel Sambuc typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock; 268f4a2713aSLionel Sambuc 269f4a2713aSLionel Sambuc struct BitTestCase { BitTestCaseBitTestCase270f4a2713aSLionel Sambuc BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr, 271f4a2713aSLionel Sambuc uint32_t Weight): 272f4a2713aSLionel Sambuc Mask(M), ThisBB(T), TargetBB(Tr), ExtraWeight(Weight) { } 273f4a2713aSLionel Sambuc uint64_t Mask; 274f4a2713aSLionel Sambuc MachineBasicBlock *ThisBB; 275f4a2713aSLionel Sambuc MachineBasicBlock *TargetBB; 276f4a2713aSLionel Sambuc uint32_t ExtraWeight; 277f4a2713aSLionel Sambuc }; 278f4a2713aSLionel Sambuc 279f4a2713aSLionel Sambuc typedef SmallVector<BitTestCase, 3> BitTestInfo; 280f4a2713aSLionel Sambuc 281f4a2713aSLionel Sambuc struct BitTestBlock { BitTestBlockBitTestBlock282f4a2713aSLionel Sambuc BitTestBlock(APInt F, APInt R, const Value* SV, 283f4a2713aSLionel Sambuc unsigned Rg, MVT RgVT, bool E, 284f4a2713aSLionel Sambuc MachineBasicBlock* P, MachineBasicBlock* D, 285*0a6a1f1dSLionel Sambuc BitTestInfo C): 286f4a2713aSLionel Sambuc First(F), Range(R), SValue(SV), Reg(Rg), RegVT(RgVT), Emitted(E), 287*0a6a1f1dSLionel Sambuc Parent(P), Default(D), Cases(std::move(C)) { } 288f4a2713aSLionel Sambuc APInt First; 289f4a2713aSLionel Sambuc APInt Range; 290f4a2713aSLionel Sambuc const Value *SValue; 291f4a2713aSLionel Sambuc unsigned Reg; 292f4a2713aSLionel Sambuc MVT RegVT; 293f4a2713aSLionel Sambuc bool Emitted; 294f4a2713aSLionel Sambuc MachineBasicBlock *Parent; 295f4a2713aSLionel Sambuc MachineBasicBlock *Default; 296f4a2713aSLionel Sambuc BitTestInfo Cases; 297f4a2713aSLionel Sambuc }; 298f4a2713aSLionel Sambuc 299f4a2713aSLionel Sambuc /// A class which encapsulates all of the information needed to generate a 300f4a2713aSLionel Sambuc /// stack protector check and signals to isel via its state being initialized 301f4a2713aSLionel Sambuc /// that a stack protector needs to be generated. 302f4a2713aSLionel Sambuc /// 303f4a2713aSLionel Sambuc /// *NOTE* The following is a high level documentation of SelectionDAG Stack 304f4a2713aSLionel Sambuc /// Protector Generation. The reason that it is placed here is for a lack of 305f4a2713aSLionel Sambuc /// other good places to stick it. 306f4a2713aSLionel Sambuc /// 307f4a2713aSLionel Sambuc /// High Level Overview of SelectionDAG Stack Protector Generation: 308f4a2713aSLionel Sambuc /// 309f4a2713aSLionel Sambuc /// Previously, generation of stack protectors was done exclusively in the 310f4a2713aSLionel Sambuc /// pre-SelectionDAG Codegen LLVM IR Pass "Stack Protector". This necessitated 311f4a2713aSLionel Sambuc /// splitting basic blocks at the IR level to create the success/failure basic 312f4a2713aSLionel Sambuc /// blocks in the tail of the basic block in question. As a result of this, 313f4a2713aSLionel Sambuc /// calls that would have qualified for the sibling call optimization were no 314f4a2713aSLionel Sambuc /// longer eligible for optimization since said calls were no longer right in 315f4a2713aSLionel Sambuc /// the "tail position" (i.e. the immediate predecessor of a ReturnInst 316f4a2713aSLionel Sambuc /// instruction). 317f4a2713aSLionel Sambuc /// 318f4a2713aSLionel Sambuc /// Then it was noticed that since the sibling call optimization causes the 319f4a2713aSLionel Sambuc /// callee to reuse the caller's stack, if we could delay the generation of 320f4a2713aSLionel Sambuc /// the stack protector check until later in CodeGen after the sibling call 321f4a2713aSLionel Sambuc /// decision was made, we get both the tail call optimization and the stack 322f4a2713aSLionel Sambuc /// protector check! 323f4a2713aSLionel Sambuc /// 324f4a2713aSLionel Sambuc /// A few goals in solving this problem were: 325f4a2713aSLionel Sambuc /// 326f4a2713aSLionel Sambuc /// 1. Preserve the architecture independence of stack protector generation. 327f4a2713aSLionel Sambuc /// 328f4a2713aSLionel Sambuc /// 2. Preserve the normal IR level stack protector check for platforms like 329*0a6a1f1dSLionel Sambuc /// OpenBSD for which we support platform-specific stack protector 330f4a2713aSLionel Sambuc /// generation. 331f4a2713aSLionel Sambuc /// 332f4a2713aSLionel Sambuc /// The main problem that guided the present solution is that one can not 333f4a2713aSLionel Sambuc /// solve this problem in an architecture independent manner at the IR level 334f4a2713aSLionel Sambuc /// only. This is because: 335f4a2713aSLionel Sambuc /// 336f4a2713aSLionel Sambuc /// 1. The decision on whether or not to perform a sibling call on certain 337f4a2713aSLionel Sambuc /// platforms (for instance i386) requires lower level information 338f4a2713aSLionel Sambuc /// related to available registers that can not be known at the IR level. 339f4a2713aSLionel Sambuc /// 340f4a2713aSLionel Sambuc /// 2. Even if the previous point were not true, the decision on whether to 341f4a2713aSLionel Sambuc /// perform a tail call is done in LowerCallTo in SelectionDAG which 342f4a2713aSLionel Sambuc /// occurs after the Stack Protector Pass. As a result, one would need to 343f4a2713aSLionel Sambuc /// put the relevant callinst into the stack protector check success 344f4a2713aSLionel Sambuc /// basic block (where the return inst is placed) and then move it back 345f4a2713aSLionel Sambuc /// later at SelectionDAG/MI time before the stack protector check if the 346f4a2713aSLionel Sambuc /// tail call optimization failed. The MI level option was nixed 347*0a6a1f1dSLionel Sambuc /// immediately since it would require platform-specific pattern 348f4a2713aSLionel Sambuc /// matching. The SelectionDAG level option was nixed because 349f4a2713aSLionel Sambuc /// SelectionDAG only processes one IR level basic block at a time 350f4a2713aSLionel Sambuc /// implying one could not create a DAG Combine to move the callinst. 351f4a2713aSLionel Sambuc /// 352f4a2713aSLionel Sambuc /// To get around this problem a few things were realized: 353f4a2713aSLionel Sambuc /// 354f4a2713aSLionel Sambuc /// 1. While one can not handle multiple IR level basic blocks at the 355f4a2713aSLionel Sambuc /// SelectionDAG Level, one can generate multiple machine basic blocks 356f4a2713aSLionel Sambuc /// for one IR level basic block. This is how we handle bit tests and 357f4a2713aSLionel Sambuc /// switches. 358f4a2713aSLionel Sambuc /// 359f4a2713aSLionel Sambuc /// 2. At the MI level, tail calls are represented via a special return 360f4a2713aSLionel Sambuc /// MIInst called "tcreturn". Thus if we know the basic block in which we 361f4a2713aSLionel Sambuc /// wish to insert the stack protector check, we get the correct behavior 362f4a2713aSLionel Sambuc /// by always inserting the stack protector check right before the return 363f4a2713aSLionel Sambuc /// statement. This is a "magical transformation" since no matter where 364f4a2713aSLionel Sambuc /// the stack protector check intrinsic is, we always insert the stack 365f4a2713aSLionel Sambuc /// protector check code at the end of the BB. 366f4a2713aSLionel Sambuc /// 367f4a2713aSLionel Sambuc /// Given the aforementioned constraints, the following solution was devised: 368f4a2713aSLionel Sambuc /// 369f4a2713aSLionel Sambuc /// 1. On platforms that do not support SelectionDAG stack protector check 370f4a2713aSLionel Sambuc /// generation, allow for the normal IR level stack protector check 371f4a2713aSLionel Sambuc /// generation to continue. 372f4a2713aSLionel Sambuc /// 373f4a2713aSLionel Sambuc /// 2. On platforms that do support SelectionDAG stack protector check 374f4a2713aSLionel Sambuc /// generation: 375f4a2713aSLionel Sambuc /// 376f4a2713aSLionel Sambuc /// a. Use the IR level stack protector pass to decide if a stack 377f4a2713aSLionel Sambuc /// protector is required/which BB we insert the stack protector check 378f4a2713aSLionel Sambuc /// in by reusing the logic already therein. If we wish to generate a 379f4a2713aSLionel Sambuc /// stack protector check in a basic block, we place a special IR 380f4a2713aSLionel Sambuc /// intrinsic called llvm.stackprotectorcheck right before the BB's 381f4a2713aSLionel Sambuc /// returninst or if there is a callinst that could potentially be 382f4a2713aSLionel Sambuc /// sibling call optimized, before the call inst. 383f4a2713aSLionel Sambuc /// 384f4a2713aSLionel Sambuc /// b. Then when a BB with said intrinsic is processed, we codegen the BB 385f4a2713aSLionel Sambuc /// normally via SelectBasicBlock. In said process, when we visit the 386f4a2713aSLionel Sambuc /// stack protector check, we do not actually emit anything into the 387f4a2713aSLionel Sambuc /// BB. Instead, we just initialize the stack protector descriptor 388f4a2713aSLionel Sambuc /// class (which involves stashing information/creating the success 389f4a2713aSLionel Sambuc /// mbbb and the failure mbb if we have not created one for this 390f4a2713aSLionel Sambuc /// function yet) and export the guard variable that we are going to 391f4a2713aSLionel Sambuc /// compare. 392f4a2713aSLionel Sambuc /// 393f4a2713aSLionel Sambuc /// c. After we finish selecting the basic block, in FinishBasicBlock if 394f4a2713aSLionel Sambuc /// the StackProtectorDescriptor attached to the SelectionDAGBuilder is 395f4a2713aSLionel Sambuc /// initialized, we first find a splice point in the parent basic block 396f4a2713aSLionel Sambuc /// before the terminator and then splice the terminator of said basic 397f4a2713aSLionel Sambuc /// block into the success basic block. Then we code-gen a new tail for 398f4a2713aSLionel Sambuc /// the parent basic block consisting of the two loads, the comparison, 399f4a2713aSLionel Sambuc /// and finally two branches to the success/failure basic blocks. We 400f4a2713aSLionel Sambuc /// conclude by code-gening the failure basic block if we have not 401f4a2713aSLionel Sambuc /// code-gened it already (all stack protector checks we generate in 402f4a2713aSLionel Sambuc /// the same function, use the same failure basic block). 403f4a2713aSLionel Sambuc class StackProtectorDescriptor { 404f4a2713aSLionel Sambuc public: StackProtectorDescriptor()405*0a6a1f1dSLionel Sambuc StackProtectorDescriptor() : ParentMBB(nullptr), SuccessMBB(nullptr), 406*0a6a1f1dSLionel Sambuc FailureMBB(nullptr), Guard(nullptr), 407*0a6a1f1dSLionel Sambuc GuardReg(0) { } ~StackProtectorDescriptor()408f4a2713aSLionel Sambuc ~StackProtectorDescriptor() { } 409f4a2713aSLionel Sambuc 410f4a2713aSLionel Sambuc /// Returns true if all fields of the stack protector descriptor are 411f4a2713aSLionel Sambuc /// initialized implying that we should/are ready to emit a stack protector. shouldEmitStackProtector()412f4a2713aSLionel Sambuc bool shouldEmitStackProtector() const { 413f4a2713aSLionel Sambuc return ParentMBB && SuccessMBB && FailureMBB && Guard; 414f4a2713aSLionel Sambuc } 415f4a2713aSLionel Sambuc 416f4a2713aSLionel Sambuc /// Initialize the stack protector descriptor structure for a new basic 417f4a2713aSLionel Sambuc /// block. initialize(const BasicBlock * BB,MachineBasicBlock * MBB,const CallInst & StackProtCheckCall)418f4a2713aSLionel Sambuc void initialize(const BasicBlock *BB, 419f4a2713aSLionel Sambuc MachineBasicBlock *MBB, 420f4a2713aSLionel Sambuc const CallInst &StackProtCheckCall) { 421f4a2713aSLionel Sambuc // Make sure we are not initialized yet. 422f4a2713aSLionel Sambuc assert(!shouldEmitStackProtector() && "Stack Protector Descriptor is " 423f4a2713aSLionel Sambuc "already initialized!"); 424f4a2713aSLionel Sambuc ParentMBB = MBB; 425*0a6a1f1dSLionel Sambuc SuccessMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ true); 426*0a6a1f1dSLionel Sambuc FailureMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ false, FailureMBB); 427f4a2713aSLionel Sambuc if (!Guard) 428f4a2713aSLionel Sambuc Guard = StackProtCheckCall.getArgOperand(0); 429f4a2713aSLionel Sambuc } 430f4a2713aSLionel Sambuc 431f4a2713aSLionel Sambuc /// Reset state that changes when we handle different basic blocks. 432f4a2713aSLionel Sambuc /// 433f4a2713aSLionel Sambuc /// This currently includes: 434f4a2713aSLionel Sambuc /// 435f4a2713aSLionel Sambuc /// 1. The specific basic block we are generating a 436f4a2713aSLionel Sambuc /// stack protector for (ParentMBB). 437f4a2713aSLionel Sambuc /// 438f4a2713aSLionel Sambuc /// 2. The successor machine basic block that will contain the tail of 439f4a2713aSLionel Sambuc /// parent mbb after we create the stack protector check (SuccessMBB). This 440f4a2713aSLionel Sambuc /// BB is visited only on stack protector check success. resetPerBBState()441f4a2713aSLionel Sambuc void resetPerBBState() { 442*0a6a1f1dSLionel Sambuc ParentMBB = nullptr; 443*0a6a1f1dSLionel Sambuc SuccessMBB = nullptr; 444f4a2713aSLionel Sambuc } 445f4a2713aSLionel Sambuc 446f4a2713aSLionel Sambuc /// Reset state that only changes when we switch functions. 447f4a2713aSLionel Sambuc /// 448f4a2713aSLionel Sambuc /// This currently includes: 449f4a2713aSLionel Sambuc /// 450f4a2713aSLionel Sambuc /// 1. FailureMBB since we reuse the failure code path for all stack 451f4a2713aSLionel Sambuc /// protector checks created in an individual function. 452f4a2713aSLionel Sambuc /// 453f4a2713aSLionel Sambuc /// 2.The guard variable since the guard variable we are checking against is 454f4a2713aSLionel Sambuc /// always the same. resetPerFunctionState()455f4a2713aSLionel Sambuc void resetPerFunctionState() { 456*0a6a1f1dSLionel Sambuc FailureMBB = nullptr; 457*0a6a1f1dSLionel Sambuc Guard = nullptr; 458f4a2713aSLionel Sambuc } 459f4a2713aSLionel Sambuc getParentMBB()460f4a2713aSLionel Sambuc MachineBasicBlock *getParentMBB() { return ParentMBB; } getSuccessMBB()461f4a2713aSLionel Sambuc MachineBasicBlock *getSuccessMBB() { return SuccessMBB; } getFailureMBB()462f4a2713aSLionel Sambuc MachineBasicBlock *getFailureMBB() { return FailureMBB; } getGuard()463f4a2713aSLionel Sambuc const Value *getGuard() { return Guard; } 464f4a2713aSLionel Sambuc getGuardReg()465*0a6a1f1dSLionel Sambuc unsigned getGuardReg() const { return GuardReg; } setGuardReg(unsigned R)466*0a6a1f1dSLionel Sambuc void setGuardReg(unsigned R) { GuardReg = R; } 467*0a6a1f1dSLionel Sambuc 468f4a2713aSLionel Sambuc private: 469f4a2713aSLionel Sambuc /// The basic block for which we are generating the stack protector. 470f4a2713aSLionel Sambuc /// 471f4a2713aSLionel Sambuc /// As a result of stack protector generation, we will splice the 472f4a2713aSLionel Sambuc /// terminators of this basic block into the successor mbb SuccessMBB and 473f4a2713aSLionel Sambuc /// replace it with a compare/branch to the successor mbbs 474f4a2713aSLionel Sambuc /// SuccessMBB/FailureMBB depending on whether or not the stack protector 475f4a2713aSLionel Sambuc /// was violated. 476f4a2713aSLionel Sambuc MachineBasicBlock *ParentMBB; 477f4a2713aSLionel Sambuc 478f4a2713aSLionel Sambuc /// A basic block visited on stack protector check success that contains the 479f4a2713aSLionel Sambuc /// terminators of ParentMBB. 480f4a2713aSLionel Sambuc MachineBasicBlock *SuccessMBB; 481f4a2713aSLionel Sambuc 482f4a2713aSLionel Sambuc /// This basic block visited on stack protector check failure that will 483f4a2713aSLionel Sambuc /// contain a call to __stack_chk_fail(). 484f4a2713aSLionel Sambuc MachineBasicBlock *FailureMBB; 485f4a2713aSLionel Sambuc 486f4a2713aSLionel Sambuc /// The guard variable which we will compare against the stored value in the 487f4a2713aSLionel Sambuc /// stack protector stack slot. 488f4a2713aSLionel Sambuc const Value *Guard; 489f4a2713aSLionel Sambuc 490*0a6a1f1dSLionel Sambuc /// The virtual register holding the stack guard value. 491*0a6a1f1dSLionel Sambuc unsigned GuardReg; 492*0a6a1f1dSLionel Sambuc 493f4a2713aSLionel Sambuc /// Add a successor machine basic block to ParentMBB. If the successor mbb 494f4a2713aSLionel Sambuc /// has not been created yet (i.e. if SuccMBB = 0), then the machine basic 495*0a6a1f1dSLionel Sambuc /// block will be created. Assign a large weight if IsLikely is true. 496f4a2713aSLionel Sambuc MachineBasicBlock *AddSuccessorMBB(const BasicBlock *BB, 497f4a2713aSLionel Sambuc MachineBasicBlock *ParentMBB, 498*0a6a1f1dSLionel Sambuc bool IsLikely, 499*0a6a1f1dSLionel Sambuc MachineBasicBlock *SuccMBB = nullptr); 500f4a2713aSLionel Sambuc }; 501f4a2713aSLionel Sambuc 502f4a2713aSLionel Sambuc private: 503f4a2713aSLionel Sambuc const TargetMachine &TM; 504f4a2713aSLionel Sambuc public: 505*0a6a1f1dSLionel Sambuc /// Lowest valid SDNodeOrder. The special case 0 is reserved for scheduling 506*0a6a1f1dSLionel Sambuc /// nodes without a corresponding SDNode. 507*0a6a1f1dSLionel Sambuc static const unsigned LowestSDNodeOrder = 1; 508*0a6a1f1dSLionel Sambuc 509f4a2713aSLionel Sambuc SelectionDAG &DAG; 510*0a6a1f1dSLionel Sambuc const DataLayout *DL; 511f4a2713aSLionel Sambuc AliasAnalysis *AA; 512f4a2713aSLionel Sambuc const TargetLibraryInfo *LibInfo; 513f4a2713aSLionel Sambuc 514f4a2713aSLionel Sambuc /// SwitchCases - Vector of CaseBlock structures used to communicate 515f4a2713aSLionel Sambuc /// SwitchInst code generation information. 516f4a2713aSLionel Sambuc std::vector<CaseBlock> SwitchCases; 517f4a2713aSLionel Sambuc /// JTCases - Vector of JumpTable structures used to communicate 518f4a2713aSLionel Sambuc /// SwitchInst code generation information. 519f4a2713aSLionel Sambuc std::vector<JumpTableBlock> JTCases; 520f4a2713aSLionel Sambuc /// BitTestCases - Vector of BitTestBlock structures used to communicate 521f4a2713aSLionel Sambuc /// SwitchInst code generation information. 522f4a2713aSLionel Sambuc std::vector<BitTestBlock> BitTestCases; 523f4a2713aSLionel Sambuc /// A StackProtectorDescriptor structure used to communicate stack protector 524f4a2713aSLionel Sambuc /// information in between SelectBasicBlock and FinishBasicBlock. 525f4a2713aSLionel Sambuc StackProtectorDescriptor SPDescriptor; 526f4a2713aSLionel Sambuc 527f4a2713aSLionel Sambuc // Emit PHI-node-operand constants only once even if used by multiple 528f4a2713aSLionel Sambuc // PHI nodes. 529f4a2713aSLionel Sambuc DenseMap<const Constant *, unsigned> ConstantsOut; 530f4a2713aSLionel Sambuc 531f4a2713aSLionel Sambuc /// FuncInfo - Information about the function as a whole. 532f4a2713aSLionel Sambuc /// 533f4a2713aSLionel Sambuc FunctionLoweringInfo &FuncInfo; 534f4a2713aSLionel Sambuc 535f4a2713aSLionel Sambuc /// OptLevel - What optimization level we're generating code for. 536f4a2713aSLionel Sambuc /// 537f4a2713aSLionel Sambuc CodeGenOpt::Level OptLevel; 538f4a2713aSLionel Sambuc 539f4a2713aSLionel Sambuc /// GFI - Garbage collection metadata for the function. 540f4a2713aSLionel Sambuc GCFunctionInfo *GFI; 541f4a2713aSLionel Sambuc 542f4a2713aSLionel Sambuc /// LPadToCallSiteMap - Map a landing pad to the call site indexes. 543f4a2713aSLionel Sambuc DenseMap<MachineBasicBlock*, SmallVector<unsigned, 4> > LPadToCallSiteMap; 544f4a2713aSLionel Sambuc 545f4a2713aSLionel Sambuc /// HasTailCall - This is set to true if a call in the current 546f4a2713aSLionel Sambuc /// block has been translated as a tail call. In this case, 547f4a2713aSLionel Sambuc /// no subsequent DAG nodes should be created. 548f4a2713aSLionel Sambuc /// 549f4a2713aSLionel Sambuc bool HasTailCall; 550f4a2713aSLionel Sambuc 551f4a2713aSLionel Sambuc LLVMContext *Context; 552f4a2713aSLionel Sambuc SelectionDAGBuilder(SelectionDAG & dag,FunctionLoweringInfo & funcinfo,CodeGenOpt::Level ol)553f4a2713aSLionel Sambuc SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo, 554f4a2713aSLionel Sambuc CodeGenOpt::Level ol) 555*0a6a1f1dSLionel Sambuc : CurInst(nullptr), SDNodeOrder(LowestSDNodeOrder), TM(dag.getTarget()), 556f4a2713aSLionel Sambuc DAG(dag), FuncInfo(funcinfo), OptLevel(ol), 557f4a2713aSLionel Sambuc HasTailCall(false) { 558f4a2713aSLionel Sambuc } 559f4a2713aSLionel Sambuc 560f4a2713aSLionel Sambuc void init(GCFunctionInfo *gfi, AliasAnalysis &aa, 561f4a2713aSLionel Sambuc const TargetLibraryInfo *li); 562f4a2713aSLionel Sambuc 563f4a2713aSLionel Sambuc /// clear - Clear out the current SelectionDAG and the associated 564f4a2713aSLionel Sambuc /// state and prepare this SelectionDAGBuilder object to be used 565f4a2713aSLionel Sambuc /// for a new block. This doesn't clear out information about 566f4a2713aSLionel Sambuc /// additional blocks that are needed to complete switch lowering 567f4a2713aSLionel Sambuc /// or PHI node updating; that information is cleared out as it is 568f4a2713aSLionel Sambuc /// consumed. 569f4a2713aSLionel Sambuc void clear(); 570f4a2713aSLionel Sambuc 571f4a2713aSLionel Sambuc /// clearDanglingDebugInfo - Clear the dangling debug information 572f4a2713aSLionel Sambuc /// map. This function is separated from the clear so that debug 573f4a2713aSLionel Sambuc /// information that is dangling in a basic block can be properly 574f4a2713aSLionel Sambuc /// resolved in a different basic block. This allows the 575f4a2713aSLionel Sambuc /// SelectionDAG to resolve dangling debug information attached 576f4a2713aSLionel Sambuc /// to PHI nodes. 577f4a2713aSLionel Sambuc void clearDanglingDebugInfo(); 578f4a2713aSLionel Sambuc 579f4a2713aSLionel Sambuc /// getRoot - Return the current virtual root of the Selection DAG, 580f4a2713aSLionel Sambuc /// flushing any PendingLoad items. This must be done before emitting 581f4a2713aSLionel Sambuc /// a store or any other node that may need to be ordered after any 582f4a2713aSLionel Sambuc /// prior load instructions. 583f4a2713aSLionel Sambuc /// 584f4a2713aSLionel Sambuc SDValue getRoot(); 585f4a2713aSLionel Sambuc 586f4a2713aSLionel Sambuc /// getControlRoot - Similar to getRoot, but instead of flushing all the 587f4a2713aSLionel Sambuc /// PendingLoad items, flush all the PendingExports items. It is necessary 588f4a2713aSLionel Sambuc /// to do this before emitting a terminator instruction. 589f4a2713aSLionel Sambuc /// 590f4a2713aSLionel Sambuc SDValue getControlRoot(); 591f4a2713aSLionel Sambuc getCurSDLoc()592f4a2713aSLionel Sambuc SDLoc getCurSDLoc() const { 593f4a2713aSLionel Sambuc return SDLoc(CurInst, SDNodeOrder); 594f4a2713aSLionel Sambuc } 595f4a2713aSLionel Sambuc getCurDebugLoc()596f4a2713aSLionel Sambuc DebugLoc getCurDebugLoc() const { 597f4a2713aSLionel Sambuc return CurInst ? CurInst->getDebugLoc() : DebugLoc(); 598f4a2713aSLionel Sambuc } 599f4a2713aSLionel Sambuc getSDNodeOrder()600f4a2713aSLionel Sambuc unsigned getSDNodeOrder() const { return SDNodeOrder; } 601f4a2713aSLionel Sambuc 602f4a2713aSLionel Sambuc void CopyValueToVirtualRegister(const Value *V, unsigned Reg); 603f4a2713aSLionel Sambuc 604f4a2713aSLionel Sambuc void visit(const Instruction &I); 605f4a2713aSLionel Sambuc 606f4a2713aSLionel Sambuc void visit(unsigned Opcode, const User &I); 607f4a2713aSLionel Sambuc 608f4a2713aSLionel Sambuc // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V, 609f4a2713aSLionel Sambuc // generate the debug data structures now that we've seen its definition. 610f4a2713aSLionel Sambuc void resolveDanglingDebugInfo(const Value *V, SDValue Val); 611f4a2713aSLionel Sambuc SDValue getValue(const Value *V); 612f4a2713aSLionel Sambuc SDValue getNonRegisterValue(const Value *V); 613f4a2713aSLionel Sambuc SDValue getValueImpl(const Value *V); 614f4a2713aSLionel Sambuc setValue(const Value * V,SDValue NewN)615f4a2713aSLionel Sambuc void setValue(const Value *V, SDValue NewN) { 616f4a2713aSLionel Sambuc SDValue &N = NodeMap[V]; 617*0a6a1f1dSLionel Sambuc assert(!N.getNode() && "Already set a value for this node!"); 618f4a2713aSLionel Sambuc N = NewN; 619f4a2713aSLionel Sambuc } 620f4a2713aSLionel Sambuc removeValue(const Value * V)621*0a6a1f1dSLionel Sambuc void removeValue(const Value *V) { 622*0a6a1f1dSLionel Sambuc // This is to support hack in lowerCallFromStatepoint 623*0a6a1f1dSLionel Sambuc // Should be removed when hack is resolved 624*0a6a1f1dSLionel Sambuc if (NodeMap.count(V)) 625*0a6a1f1dSLionel Sambuc NodeMap.erase(V); 626*0a6a1f1dSLionel Sambuc } 627*0a6a1f1dSLionel Sambuc setUnusedArgValue(const Value * V,SDValue NewN)628f4a2713aSLionel Sambuc void setUnusedArgValue(const Value *V, SDValue NewN) { 629f4a2713aSLionel Sambuc SDValue &N = UnusedArgNodeMap[V]; 630*0a6a1f1dSLionel Sambuc assert(!N.getNode() && "Already set a value for this node!"); 631f4a2713aSLionel Sambuc N = NewN; 632f4a2713aSLionel Sambuc } 633f4a2713aSLionel Sambuc 634f4a2713aSLionel Sambuc void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, 635f4a2713aSLionel Sambuc MachineBasicBlock *FBB, MachineBasicBlock *CurBB, 636*0a6a1f1dSLionel Sambuc MachineBasicBlock *SwitchBB, unsigned Opc, 637*0a6a1f1dSLionel Sambuc uint32_t TW, uint32_t FW); 638f4a2713aSLionel Sambuc void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, 639f4a2713aSLionel Sambuc MachineBasicBlock *FBB, 640f4a2713aSLionel Sambuc MachineBasicBlock *CurBB, 641*0a6a1f1dSLionel Sambuc MachineBasicBlock *SwitchBB, 642*0a6a1f1dSLionel Sambuc uint32_t TW, uint32_t FW); 643f4a2713aSLionel Sambuc bool ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases); 644f4a2713aSLionel Sambuc bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB); 645f4a2713aSLionel Sambuc void CopyToExportRegsIfNeeded(const Value *V); 646f4a2713aSLionel Sambuc void ExportFromCurrentBlock(const Value *V); 647f4a2713aSLionel Sambuc void LowerCallTo(ImmutableCallSite CS, SDValue Callee, bool IsTailCall, 648*0a6a1f1dSLionel Sambuc MachineBasicBlock *LandingPad = nullptr); 649f4a2713aSLionel Sambuc 650*0a6a1f1dSLionel Sambuc std::pair<SDValue, SDValue> lowerCallOperands( 651*0a6a1f1dSLionel Sambuc ImmutableCallSite CS, 652f4a2713aSLionel Sambuc unsigned ArgIdx, 653f4a2713aSLionel Sambuc unsigned NumArgs, 654f4a2713aSLionel Sambuc SDValue Callee, 655*0a6a1f1dSLionel Sambuc bool UseVoidTy = false, 656*0a6a1f1dSLionel Sambuc MachineBasicBlock *LandingPad = nullptr, 657*0a6a1f1dSLionel Sambuc bool IsPatchPoint = false); 658f4a2713aSLionel Sambuc 659f4a2713aSLionel Sambuc /// UpdateSplitBlock - When an MBB was split during scheduling, update the 660*0a6a1f1dSLionel Sambuc /// references that need to refer to the last resulting block. 661f4a2713aSLionel Sambuc void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last); 662f4a2713aSLionel Sambuc 663f4a2713aSLionel Sambuc private: 664*0a6a1f1dSLionel Sambuc std::pair<SDValue, SDValue> lowerInvokable( 665*0a6a1f1dSLionel Sambuc TargetLowering::CallLoweringInfo &CLI, 666*0a6a1f1dSLionel Sambuc MachineBasicBlock *LandingPad); 667*0a6a1f1dSLionel Sambuc 668f4a2713aSLionel Sambuc // Terminator instructions. 669f4a2713aSLionel Sambuc void visitRet(const ReturnInst &I); 670f4a2713aSLionel Sambuc void visitBr(const BranchInst &I); 671f4a2713aSLionel Sambuc void visitSwitch(const SwitchInst &I); 672f4a2713aSLionel Sambuc void visitIndirectBr(const IndirectBrInst &I); 673*0a6a1f1dSLionel Sambuc void visitUnreachable(const UnreachableInst &I); 674f4a2713aSLionel Sambuc 675f4a2713aSLionel Sambuc // Helpers for visitSwitch 676f4a2713aSLionel Sambuc bool handleSmallSwitchRange(CaseRec& CR, 677f4a2713aSLionel Sambuc CaseRecVector& WorkList, 678f4a2713aSLionel Sambuc const Value* SV, 679f4a2713aSLionel Sambuc MachineBasicBlock* Default, 680f4a2713aSLionel Sambuc MachineBasicBlock *SwitchBB); 681f4a2713aSLionel Sambuc bool handleJTSwitchCase(CaseRec& CR, 682f4a2713aSLionel Sambuc CaseRecVector& WorkList, 683f4a2713aSLionel Sambuc const Value* SV, 684f4a2713aSLionel Sambuc MachineBasicBlock* Default, 685f4a2713aSLionel Sambuc MachineBasicBlock *SwitchBB); 686f4a2713aSLionel Sambuc bool handleBTSplitSwitchCase(CaseRec& CR, 687f4a2713aSLionel Sambuc CaseRecVector& WorkList, 688f4a2713aSLionel Sambuc const Value* SV, 689f4a2713aSLionel Sambuc MachineBasicBlock *SwitchBB); 690f4a2713aSLionel Sambuc bool handleBitTestsSwitchCase(CaseRec& CR, 691f4a2713aSLionel Sambuc CaseRecVector& WorkList, 692f4a2713aSLionel Sambuc const Value* SV, 693f4a2713aSLionel Sambuc MachineBasicBlock* Default, 694f4a2713aSLionel Sambuc MachineBasicBlock *SwitchBB); 695f4a2713aSLionel Sambuc 696f4a2713aSLionel Sambuc uint32_t getEdgeWeight(const MachineBasicBlock *Src, 697f4a2713aSLionel Sambuc const MachineBasicBlock *Dst) const; 698f4a2713aSLionel Sambuc void addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst, 699f4a2713aSLionel Sambuc uint32_t Weight = 0); 700f4a2713aSLionel Sambuc public: 701f4a2713aSLionel Sambuc void visitSwitchCase(CaseBlock &CB, 702f4a2713aSLionel Sambuc MachineBasicBlock *SwitchBB); 703f4a2713aSLionel Sambuc void visitSPDescriptorParent(StackProtectorDescriptor &SPD, 704f4a2713aSLionel Sambuc MachineBasicBlock *ParentBB); 705f4a2713aSLionel Sambuc void visitSPDescriptorFailure(StackProtectorDescriptor &SPD); 706f4a2713aSLionel Sambuc void visitBitTestHeader(BitTestBlock &B, MachineBasicBlock *SwitchBB); 707f4a2713aSLionel Sambuc void visitBitTestCase(BitTestBlock &BB, 708f4a2713aSLionel Sambuc MachineBasicBlock* NextMBB, 709f4a2713aSLionel Sambuc uint32_t BranchWeightToNext, 710f4a2713aSLionel Sambuc unsigned Reg, 711f4a2713aSLionel Sambuc BitTestCase &B, 712f4a2713aSLionel Sambuc MachineBasicBlock *SwitchBB); 713f4a2713aSLionel Sambuc void visitJumpTable(JumpTable &JT); 714f4a2713aSLionel Sambuc void visitJumpTableHeader(JumpTable &JT, JumpTableHeader &JTH, 715f4a2713aSLionel Sambuc MachineBasicBlock *SwitchBB); 716f4a2713aSLionel Sambuc 717f4a2713aSLionel Sambuc private: 718f4a2713aSLionel Sambuc // These all get lowered before this pass. 719f4a2713aSLionel Sambuc void visitInvoke(const InvokeInst &I); 720f4a2713aSLionel Sambuc void visitResume(const ResumeInst &I); 721f4a2713aSLionel Sambuc 722f4a2713aSLionel Sambuc void visitBinary(const User &I, unsigned OpCode); 723f4a2713aSLionel Sambuc void visitShift(const User &I, unsigned Opcode); visitAdd(const User & I)724f4a2713aSLionel Sambuc void visitAdd(const User &I) { visitBinary(I, ISD::ADD); } visitFAdd(const User & I)725f4a2713aSLionel Sambuc void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); } visitSub(const User & I)726f4a2713aSLionel Sambuc void visitSub(const User &I) { visitBinary(I, ISD::SUB); } 727f4a2713aSLionel Sambuc void visitFSub(const User &I); visitMul(const User & I)728f4a2713aSLionel Sambuc void visitMul(const User &I) { visitBinary(I, ISD::MUL); } visitFMul(const User & I)729f4a2713aSLionel Sambuc void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); } visitURem(const User & I)730f4a2713aSLionel Sambuc void visitURem(const User &I) { visitBinary(I, ISD::UREM); } visitSRem(const User & I)731f4a2713aSLionel Sambuc void visitSRem(const User &I) { visitBinary(I, ISD::SREM); } visitFRem(const User & I)732f4a2713aSLionel Sambuc void visitFRem(const User &I) { visitBinary(I, ISD::FREM); } visitUDiv(const User & I)733f4a2713aSLionel Sambuc void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); } 734f4a2713aSLionel Sambuc void visitSDiv(const User &I); visitFDiv(const User & I)735f4a2713aSLionel Sambuc void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); } visitAnd(const User & I)736f4a2713aSLionel Sambuc void visitAnd (const User &I) { visitBinary(I, ISD::AND); } visitOr(const User & I)737f4a2713aSLionel Sambuc void visitOr (const User &I) { visitBinary(I, ISD::OR); } visitXor(const User & I)738f4a2713aSLionel Sambuc void visitXor (const User &I) { visitBinary(I, ISD::XOR); } visitShl(const User & I)739f4a2713aSLionel Sambuc void visitShl (const User &I) { visitShift(I, ISD::SHL); } visitLShr(const User & I)740f4a2713aSLionel Sambuc void visitLShr(const User &I) { visitShift(I, ISD::SRL); } visitAShr(const User & I)741f4a2713aSLionel Sambuc void visitAShr(const User &I) { visitShift(I, ISD::SRA); } 742f4a2713aSLionel Sambuc void visitICmp(const User &I); 743f4a2713aSLionel Sambuc void visitFCmp(const User &I); 744f4a2713aSLionel Sambuc // Visit the conversion instructions 745f4a2713aSLionel Sambuc void visitTrunc(const User &I); 746f4a2713aSLionel Sambuc void visitZExt(const User &I); 747f4a2713aSLionel Sambuc void visitSExt(const User &I); 748f4a2713aSLionel Sambuc void visitFPTrunc(const User &I); 749f4a2713aSLionel Sambuc void visitFPExt(const User &I); 750f4a2713aSLionel Sambuc void visitFPToUI(const User &I); 751f4a2713aSLionel Sambuc void visitFPToSI(const User &I); 752f4a2713aSLionel Sambuc void visitUIToFP(const User &I); 753f4a2713aSLionel Sambuc void visitSIToFP(const User &I); 754f4a2713aSLionel Sambuc void visitPtrToInt(const User &I); 755f4a2713aSLionel Sambuc void visitIntToPtr(const User &I); 756f4a2713aSLionel Sambuc void visitBitCast(const User &I); 757f4a2713aSLionel Sambuc void visitAddrSpaceCast(const User &I); 758f4a2713aSLionel Sambuc 759f4a2713aSLionel Sambuc void visitExtractElement(const User &I); 760f4a2713aSLionel Sambuc void visitInsertElement(const User &I); 761f4a2713aSLionel Sambuc void visitShuffleVector(const User &I); 762f4a2713aSLionel Sambuc 763f4a2713aSLionel Sambuc void visitExtractValue(const ExtractValueInst &I); 764f4a2713aSLionel Sambuc void visitInsertValue(const InsertValueInst &I); 765f4a2713aSLionel Sambuc void visitLandingPad(const LandingPadInst &I); 766f4a2713aSLionel Sambuc 767f4a2713aSLionel Sambuc void visitGetElementPtr(const User &I); 768f4a2713aSLionel Sambuc void visitSelect(const User &I); 769f4a2713aSLionel Sambuc 770f4a2713aSLionel Sambuc void visitAlloca(const AllocaInst &I); 771f4a2713aSLionel Sambuc void visitLoad(const LoadInst &I); 772f4a2713aSLionel Sambuc void visitStore(const StoreInst &I); 773*0a6a1f1dSLionel Sambuc void visitMaskedLoad(const CallInst &I); 774*0a6a1f1dSLionel Sambuc void visitMaskedStore(const CallInst &I); 775f4a2713aSLionel Sambuc void visitAtomicCmpXchg(const AtomicCmpXchgInst &I); 776f4a2713aSLionel Sambuc void visitAtomicRMW(const AtomicRMWInst &I); 777f4a2713aSLionel Sambuc void visitFence(const FenceInst &I); 778f4a2713aSLionel Sambuc void visitPHI(const PHINode &I); 779f4a2713aSLionel Sambuc void visitCall(const CallInst &I); 780f4a2713aSLionel Sambuc bool visitMemCmpCall(const CallInst &I); 781f4a2713aSLionel Sambuc bool visitMemChrCall(const CallInst &I); 782f4a2713aSLionel Sambuc bool visitStrCpyCall(const CallInst &I, bool isStpcpy); 783f4a2713aSLionel Sambuc bool visitStrCmpCall(const CallInst &I); 784f4a2713aSLionel Sambuc bool visitStrLenCall(const CallInst &I); 785f4a2713aSLionel Sambuc bool visitStrNLenCall(const CallInst &I); 786f4a2713aSLionel Sambuc bool visitUnaryFloatCall(const CallInst &I, unsigned Opcode); 787*0a6a1f1dSLionel Sambuc bool visitBinaryFloatCall(const CallInst &I, unsigned Opcode); 788f4a2713aSLionel Sambuc void visitAtomicLoad(const LoadInst &I); 789f4a2713aSLionel Sambuc void visitAtomicStore(const StoreInst &I); 790f4a2713aSLionel Sambuc 791f4a2713aSLionel Sambuc void visitInlineAsm(ImmutableCallSite CS); 792f4a2713aSLionel Sambuc const char *visitIntrinsicCall(const CallInst &I, unsigned Intrinsic); 793f4a2713aSLionel Sambuc void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic); 794f4a2713aSLionel Sambuc 795f4a2713aSLionel Sambuc void visitVAStart(const CallInst &I); 796f4a2713aSLionel Sambuc void visitVAArg(const VAArgInst &I); 797f4a2713aSLionel Sambuc void visitVAEnd(const CallInst &I); 798f4a2713aSLionel Sambuc void visitVACopy(const CallInst &I); 799f4a2713aSLionel Sambuc void visitStackmap(const CallInst &I); 800*0a6a1f1dSLionel Sambuc void visitPatchpoint(ImmutableCallSite CS, 801*0a6a1f1dSLionel Sambuc MachineBasicBlock *LandingPad = nullptr); 802*0a6a1f1dSLionel Sambuc 803*0a6a1f1dSLionel Sambuc // These three are implemented in StatepointLowering.cpp 804*0a6a1f1dSLionel Sambuc void visitStatepoint(const CallInst &I); 805*0a6a1f1dSLionel Sambuc void visitGCRelocate(const CallInst &I); 806*0a6a1f1dSLionel Sambuc void visitGCResult(const CallInst &I); 807f4a2713aSLionel Sambuc visitUserOp1(const Instruction & I)808f4a2713aSLionel Sambuc void visitUserOp1(const Instruction &I) { 809f4a2713aSLionel Sambuc llvm_unreachable("UserOp1 should not exist at instruction selection time!"); 810f4a2713aSLionel Sambuc } visitUserOp2(const Instruction & I)811f4a2713aSLionel Sambuc void visitUserOp2(const Instruction &I) { 812f4a2713aSLionel Sambuc llvm_unreachable("UserOp2 should not exist at instruction selection time!"); 813f4a2713aSLionel Sambuc } 814f4a2713aSLionel Sambuc 815f4a2713aSLionel Sambuc void processIntegerCallValue(const Instruction &I, 816f4a2713aSLionel Sambuc SDValue Value, bool IsSigned); 817f4a2713aSLionel Sambuc 818f4a2713aSLionel Sambuc void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB); 819f4a2713aSLionel Sambuc 820f4a2713aSLionel Sambuc /// EmitFuncArgumentDbgValue - If V is an function argument then create 821f4a2713aSLionel Sambuc /// corresponding DBG_VALUE machine instruction for it now. At the end of 822f4a2713aSLionel Sambuc /// instruction selection, they will be inserted to the entry BB. 823*0a6a1f1dSLionel Sambuc bool EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable, MDNode *Expr, 824*0a6a1f1dSLionel Sambuc int64_t Offset, bool IsIndirect, 825*0a6a1f1dSLionel Sambuc const SDValue &N); 826f4a2713aSLionel Sambuc }; 827f4a2713aSLionel Sambuc 828f4a2713aSLionel Sambuc } // end namespace llvm 829f4a2713aSLionel Sambuc 830f4a2713aSLionel Sambuc #endif 831