150f02cb2SNick Lewycky //===-- Analysis.cpp - CodeGen LLVM IR Analysis Utilities -----------------===// 2450aa64fSDan Gohman // 3450aa64fSDan Gohman // The LLVM Compiler Infrastructure 4450aa64fSDan Gohman // 5450aa64fSDan Gohman // This file is distributed under the University of Illinois Open Source 6450aa64fSDan Gohman // License. See LICENSE.TXT for details. 7450aa64fSDan Gohman // 8450aa64fSDan Gohman //===----------------------------------------------------------------------===// 9450aa64fSDan Gohman // 10db5028bdSEric Christopher // This file defines several CodeGen-specific LLVM IR analysis utilities. 11450aa64fSDan Gohman // 12450aa64fSDan Gohman //===----------------------------------------------------------------------===// 13450aa64fSDan Gohman 1409fc276dSEric Christopher #include "llvm/CodeGen/Analysis.h" 15dda00098SEric Christopher #include "llvm/Analysis/ValueTracking.h" 16ed0881b2SChandler Carruth #include "llvm/CodeGen/MachineFunction.h" 1716193552SDavid Majnemer #include "llvm/CodeGen/MachineModuleInfo.h" 189fb823bbSChandler Carruth #include "llvm/IR/DataLayout.h" 199fb823bbSChandler Carruth #include "llvm/IR/DerivedTypes.h" 209fb823bbSChandler Carruth #include "llvm/IR/Function.h" 219fb823bbSChandler Carruth #include "llvm/IR/Instructions.h" 229fb823bbSChandler Carruth #include "llvm/IR/IntrinsicInst.h" 239fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h" 249fb823bbSChandler Carruth #include "llvm/IR/Module.h" 25450aa64fSDan Gohman #include "llvm/Support/ErrorHandling.h" 26450aa64fSDan Gohman #include "llvm/Support/MathExtras.h" 27ed0881b2SChandler Carruth #include "llvm/Target/TargetLowering.h" 2816193552SDavid Majnemer #include "llvm/Target/TargetInstrInfo.h" 29d913448bSEric Christopher #include "llvm/Target/TargetSubtargetInfo.h" 30f21434ccSRafael Espindola #include "llvm/Transforms/Utils/GlobalStatus.h" 31d913448bSEric Christopher 32450aa64fSDan Gohman using namespace llvm; 33450aa64fSDan Gohman 348923cc54SMehdi Amini /// Compute the linearized index of a member in a nested aggregate/struct/array 358923cc54SMehdi Amini /// by recursing and accumulating CurIndex as long as there are indices in the 368923cc54SMehdi Amini /// index list. 37229907cdSChris Lattner unsigned llvm::ComputeLinearIndex(Type *Ty, 38450aa64fSDan Gohman const unsigned *Indices, 39450aa64fSDan Gohman const unsigned *IndicesEnd, 40450aa64fSDan Gohman unsigned CurIndex) { 41450aa64fSDan Gohman // Base case: We're done. 42450aa64fSDan Gohman if (Indices && Indices == IndicesEnd) 43450aa64fSDan Gohman return CurIndex; 44450aa64fSDan Gohman 45450aa64fSDan Gohman // Given a struct type, recursively traverse the elements. 46229907cdSChris Lattner if (StructType *STy = dyn_cast<StructType>(Ty)) { 47450aa64fSDan Gohman for (StructType::element_iterator EB = STy->element_begin(), 48450aa64fSDan Gohman EI = EB, 49450aa64fSDan Gohman EE = STy->element_end(); 50450aa64fSDan Gohman EI != EE; ++EI) { 51450aa64fSDan Gohman if (Indices && *Indices == unsigned(EI - EB)) 52aadc5596SDan Gohman return ComputeLinearIndex(*EI, Indices+1, IndicesEnd, CurIndex); 53c0196b1bSCraig Topper CurIndex = ComputeLinearIndex(*EI, nullptr, nullptr, CurIndex); 54450aa64fSDan Gohman } 557b068f6bSMehdi Amini assert(!Indices && "Unexpected out of bound"); 56450aa64fSDan Gohman return CurIndex; 57450aa64fSDan Gohman } 58450aa64fSDan Gohman // Given an array type, recursively traverse the elements. 59229907cdSChris Lattner else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 60229907cdSChris Lattner Type *EltTy = ATy->getElementType(); 618923cc54SMehdi Amini unsigned NumElts = ATy->getNumElements(); 628923cc54SMehdi Amini // Compute the Linear offset when jumping one element of the array 638923cc54SMehdi Amini unsigned EltLinearOffset = ComputeLinearIndex(EltTy, nullptr, nullptr, 0); 647b068f6bSMehdi Amini if (Indices) { 657b068f6bSMehdi Amini assert(*Indices < NumElts && "Unexpected out of bound"); 668923cc54SMehdi Amini // If the indice is inside the array, compute the index to the requested 678923cc54SMehdi Amini // elt and recurse inside the element with the end of the indices list 688923cc54SMehdi Amini CurIndex += EltLinearOffset* *Indices; 69aadc5596SDan Gohman return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex); 70450aa64fSDan Gohman } 718923cc54SMehdi Amini CurIndex += EltLinearOffset*NumElts; 72450aa64fSDan Gohman return CurIndex; 73450aa64fSDan Gohman } 74450aa64fSDan Gohman // We haven't found the type we're looking for, so keep searching. 75450aa64fSDan Gohman return CurIndex + 1; 76450aa64fSDan Gohman } 77450aa64fSDan Gohman 78450aa64fSDan Gohman /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of 79450aa64fSDan Gohman /// EVTs that represent all the individual underlying 80450aa64fSDan Gohman /// non-aggregate types that comprise it. 81450aa64fSDan Gohman /// 82450aa64fSDan Gohman /// If Offsets is non-null, it points to a vector to be filled in 83450aa64fSDan Gohman /// with the in-memory offsets of each of the individual values. 84450aa64fSDan Gohman /// 8556228dabSMehdi Amini void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, 8656228dabSMehdi Amini Type *Ty, SmallVectorImpl<EVT> &ValueVTs, 87450aa64fSDan Gohman SmallVectorImpl<uint64_t> *Offsets, 88450aa64fSDan Gohman uint64_t StartingOffset) { 89450aa64fSDan Gohman // Given a struct type, recursively traverse the elements. 90229907cdSChris Lattner if (StructType *STy = dyn_cast<StructType>(Ty)) { 9156228dabSMehdi Amini const StructLayout *SL = DL.getStructLayout(STy); 92450aa64fSDan Gohman for (StructType::element_iterator EB = STy->element_begin(), 93450aa64fSDan Gohman EI = EB, 94450aa64fSDan Gohman EE = STy->element_end(); 95450aa64fSDan Gohman EI != EE; ++EI) 9656228dabSMehdi Amini ComputeValueVTs(TLI, DL, *EI, ValueVTs, Offsets, 97450aa64fSDan Gohman StartingOffset + SL->getElementOffset(EI - EB)); 98450aa64fSDan Gohman return; 99450aa64fSDan Gohman } 100450aa64fSDan Gohman // Given an array type, recursively traverse the elements. 101229907cdSChris Lattner if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 102229907cdSChris Lattner Type *EltTy = ATy->getElementType(); 10356228dabSMehdi Amini uint64_t EltSize = DL.getTypeAllocSize(EltTy); 104450aa64fSDan Gohman for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) 10556228dabSMehdi Amini ComputeValueVTs(TLI, DL, EltTy, ValueVTs, Offsets, 106450aa64fSDan Gohman StartingOffset + i * EltSize); 107450aa64fSDan Gohman return; 108450aa64fSDan Gohman } 109450aa64fSDan Gohman // Interpret void as zero return values. 110450aa64fSDan Gohman if (Ty->isVoidTy()) 111450aa64fSDan Gohman return; 112450aa64fSDan Gohman // Base case: we can get an EVT for this LLVM IR type. 11344ede33aSMehdi Amini ValueVTs.push_back(TLI.getValueType(DL, Ty)); 114450aa64fSDan Gohman if (Offsets) 115450aa64fSDan Gohman Offsets->push_back(StartingOffset); 116450aa64fSDan Gohman } 117450aa64fSDan Gohman 118450aa64fSDan Gohman /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V. 119283bc2edSReid Kleckner GlobalValue *llvm::ExtractTypeInfo(Value *V) { 120450aa64fSDan Gohman V = V->stripPointerCasts(); 121283bc2edSReid Kleckner GlobalValue *GV = dyn_cast<GlobalValue>(V); 122283bc2edSReid Kleckner GlobalVariable *Var = dyn_cast<GlobalVariable>(V); 123450aa64fSDan Gohman 124283bc2edSReid Kleckner if (Var && Var->getName() == "llvm.eh.catch.all.value") { 125283bc2edSReid Kleckner assert(Var->hasInitializer() && 126450aa64fSDan Gohman "The EH catch-all value must have an initializer"); 127283bc2edSReid Kleckner Value *Init = Var->getInitializer(); 128283bc2edSReid Kleckner GV = dyn_cast<GlobalValue>(Init); 129450aa64fSDan Gohman if (!GV) V = cast<ConstantPointerNull>(Init); 130450aa64fSDan Gohman } 131450aa64fSDan Gohman 132450aa64fSDan Gohman assert((GV || isa<ConstantPointerNull>(V)) && 133450aa64fSDan Gohman "TypeInfo must be a global variable or NULL"); 134450aa64fSDan Gohman return GV; 135450aa64fSDan Gohman } 136450aa64fSDan Gohman 137450aa64fSDan Gohman /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being 138450aa64fSDan Gohman /// processed uses a memory 'm' constraint. 139450aa64fSDan Gohman bool 140e8360b71SJohn Thompson llvm::hasInlineAsmMemConstraint(InlineAsm::ConstraintInfoVector &CInfos, 141450aa64fSDan Gohman const TargetLowering &TLI) { 142450aa64fSDan Gohman for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { 143450aa64fSDan Gohman InlineAsm::ConstraintInfo &CI = CInfos[i]; 144450aa64fSDan Gohman for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { 145450aa64fSDan Gohman TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); 146450aa64fSDan Gohman if (CType == TargetLowering::C_Memory) 147450aa64fSDan Gohman return true; 148450aa64fSDan Gohman } 149450aa64fSDan Gohman 150450aa64fSDan Gohman // Indirect operand accesses access memory. 151450aa64fSDan Gohman if (CI.isIndirect) 152450aa64fSDan Gohman return true; 153450aa64fSDan Gohman } 154450aa64fSDan Gohman 155450aa64fSDan Gohman return false; 156450aa64fSDan Gohman } 157450aa64fSDan Gohman 158450aa64fSDan Gohman /// getFCmpCondCode - Return the ISD condition code corresponding to 159450aa64fSDan Gohman /// the given LLVM IR floating-point condition code. This includes 160450aa64fSDan Gohman /// consideration of global floating-point math flags. 161450aa64fSDan Gohman /// 162450aa64fSDan Gohman ISD::CondCode llvm::getFCmpCondCode(FCmpInst::Predicate Pred) { 163450aa64fSDan Gohman switch (Pred) { 16450f02cb2SNick Lewycky case FCmpInst::FCMP_FALSE: return ISD::SETFALSE; 16550f02cb2SNick Lewycky case FCmpInst::FCMP_OEQ: return ISD::SETOEQ; 16650f02cb2SNick Lewycky case FCmpInst::FCMP_OGT: return ISD::SETOGT; 16750f02cb2SNick Lewycky case FCmpInst::FCMP_OGE: return ISD::SETOGE; 16850f02cb2SNick Lewycky case FCmpInst::FCMP_OLT: return ISD::SETOLT; 16950f02cb2SNick Lewycky case FCmpInst::FCMP_OLE: return ISD::SETOLE; 17050f02cb2SNick Lewycky case FCmpInst::FCMP_ONE: return ISD::SETONE; 17150f02cb2SNick Lewycky case FCmpInst::FCMP_ORD: return ISD::SETO; 17250f02cb2SNick Lewycky case FCmpInst::FCMP_UNO: return ISD::SETUO; 17350f02cb2SNick Lewycky case FCmpInst::FCMP_UEQ: return ISD::SETUEQ; 17450f02cb2SNick Lewycky case FCmpInst::FCMP_UGT: return ISD::SETUGT; 17550f02cb2SNick Lewycky case FCmpInst::FCMP_UGE: return ISD::SETUGE; 17650f02cb2SNick Lewycky case FCmpInst::FCMP_ULT: return ISD::SETULT; 17750f02cb2SNick Lewycky case FCmpInst::FCMP_ULE: return ISD::SETULE; 17850f02cb2SNick Lewycky case FCmpInst::FCMP_UNE: return ISD::SETUNE; 17950f02cb2SNick Lewycky case FCmpInst::FCMP_TRUE: return ISD::SETTRUE; 18046a9f016SDavid Blaikie default: llvm_unreachable("Invalid FCmp predicate opcode!"); 181450aa64fSDan Gohman } 18250f02cb2SNick Lewycky } 18350f02cb2SNick Lewycky 18450f02cb2SNick Lewycky ISD::CondCode llvm::getFCmpCodeWithoutNaN(ISD::CondCode CC) { 18550f02cb2SNick Lewycky switch (CC) { 18650f02cb2SNick Lewycky case ISD::SETOEQ: case ISD::SETUEQ: return ISD::SETEQ; 18750f02cb2SNick Lewycky case ISD::SETONE: case ISD::SETUNE: return ISD::SETNE; 18850f02cb2SNick Lewycky case ISD::SETOLT: case ISD::SETULT: return ISD::SETLT; 18950f02cb2SNick Lewycky case ISD::SETOLE: case ISD::SETULE: return ISD::SETLE; 19050f02cb2SNick Lewycky case ISD::SETOGT: case ISD::SETUGT: return ISD::SETGT; 19150f02cb2SNick Lewycky case ISD::SETOGE: case ISD::SETUGE: return ISD::SETGE; 19246a9f016SDavid Blaikie default: return CC; 19350f02cb2SNick Lewycky } 194450aa64fSDan Gohman } 195450aa64fSDan Gohman 196450aa64fSDan Gohman /// getICmpCondCode - Return the ISD condition code corresponding to 197450aa64fSDan Gohman /// the given LLVM IR integer condition code. 198450aa64fSDan Gohman /// 199450aa64fSDan Gohman ISD::CondCode llvm::getICmpCondCode(ICmpInst::Predicate Pred) { 200450aa64fSDan Gohman switch (Pred) { 201450aa64fSDan Gohman case ICmpInst::ICMP_EQ: return ISD::SETEQ; 202450aa64fSDan Gohman case ICmpInst::ICMP_NE: return ISD::SETNE; 203450aa64fSDan Gohman case ICmpInst::ICMP_SLE: return ISD::SETLE; 204450aa64fSDan Gohman case ICmpInst::ICMP_ULE: return ISD::SETULE; 205450aa64fSDan Gohman case ICmpInst::ICMP_SGE: return ISD::SETGE; 206450aa64fSDan Gohman case ICmpInst::ICMP_UGE: return ISD::SETUGE; 207450aa64fSDan Gohman case ICmpInst::ICMP_SLT: return ISD::SETLT; 208450aa64fSDan Gohman case ICmpInst::ICMP_ULT: return ISD::SETULT; 209450aa64fSDan Gohman case ICmpInst::ICMP_SGT: return ISD::SETGT; 210450aa64fSDan Gohman case ICmpInst::ICMP_UGT: return ISD::SETUGT; 211450aa64fSDan Gohman default: 212450aa64fSDan Gohman llvm_unreachable("Invalid ICmp predicate opcode!"); 213450aa64fSDan Gohman } 214450aa64fSDan Gohman } 215450aa64fSDan Gohman 216ffc44549SStephen Lin static bool isNoopBitcast(Type *T1, Type *T2, 217c0659fadSMichael Gottesman const TargetLoweringBase& TLI) { 218ffc44549SStephen Lin return T1 == T2 || (T1->isPointerTy() && T2->isPointerTy()) || 219ffc44549SStephen Lin (isa<VectorType>(T1) && isa<VectorType>(T2) && 220ffc44549SStephen Lin TLI.isTypeLegal(EVT::getEVT(T1)) && TLI.isTypeLegal(EVT::getEVT(T2))); 221ffc44549SStephen Lin } 2224f3615deSChris Lattner 223a4415854STim Northover /// Look through operations that will be free to find the earliest source of 224a4415854STim Northover /// this value. 225a4415854STim Northover /// 226a4415854STim Northover /// @param ValLoc If V has aggegate type, we will be interested in a particular 227a4415854STim Northover /// scalar component. This records its address; the reverse of this list gives a 228a4415854STim Northover /// sequence of indices appropriate for an extractvalue to locate the important 229a4415854STim Northover /// value. This value is updated during the function and on exit will indicate 230a4415854STim Northover /// similar information for the Value returned. 231a4415854STim Northover /// 232a4415854STim Northover /// @param DataBits If this function looks through truncate instructions, this 233a4415854STim Northover /// will record the smallest size attained. 234a4415854STim Northover static const Value *getNoopInput(const Value *V, 235a4415854STim Northover SmallVectorImpl<unsigned> &ValLoc, 236a4415854STim Northover unsigned &DataBits, 23744ede33aSMehdi Amini const TargetLoweringBase &TLI, 23844ede33aSMehdi Amini const DataLayout &DL) { 239ffc44549SStephen Lin while (true) { 240ffc44549SStephen Lin // Try to look through V1; if V1 is not an instruction, it can't be looked 241ffc44549SStephen Lin // through. 242a4415854STim Northover const Instruction *I = dyn_cast<Instruction>(V); 243a4415854STim Northover if (!I || I->getNumOperands() == 0) return V; 244c0196b1bSCraig Topper const Value *NoopInput = nullptr; 245a4415854STim Northover 246182fe3eeSChris Lattner Value *Op = I->getOperand(0); 247a4415854STim Northover if (isa<BitCastInst>(I)) { 2484f3615deSChris Lattner // Look through truly no-op bitcasts. 249ffc44549SStephen Lin if (isNoopBitcast(Op->getType(), I->getType(), TLI)) 250ffc44549SStephen Lin NoopInput = Op; 251ffc44549SStephen Lin } else if (isa<GetElementPtrInst>(I)) { 252ffc44549SStephen Lin // Look through getelementptr 253ffc44549SStephen Lin if (cast<GetElementPtrInst>(I)->hasAllZeroIndices()) 254ffc44549SStephen Lin NoopInput = Op; 255ffc44549SStephen Lin } else if (isa<IntToPtrInst>(I)) { 256182fe3eeSChris Lattner // Look through inttoptr. 257ffc44549SStephen Lin // Make sure this isn't a truncating or extending cast. We could 258ffc44549SStephen Lin // support this eventually, but don't bother for now. 259ffc44549SStephen Lin if (!isa<VectorType>(I->getType()) && 26044ede33aSMehdi Amini DL.getPointerSizeInBits() == 261182fe3eeSChris Lattner cast<IntegerType>(Op->getType())->getBitWidth()) 262ffc44549SStephen Lin NoopInput = Op; 263ffc44549SStephen Lin } else if (isa<PtrToIntInst>(I)) { 264182fe3eeSChris Lattner // Look through ptrtoint. 265ffc44549SStephen Lin // Make sure this isn't a truncating or extending cast. We could 266ffc44549SStephen Lin // support this eventually, but don't bother for now. 267ffc44549SStephen Lin if (!isa<VectorType>(I->getType()) && 26844ede33aSMehdi Amini DL.getPointerSizeInBits() == 269182fe3eeSChris Lattner cast<IntegerType>(I->getType())->getBitWidth()) 270ffc44549SStephen Lin NoopInput = Op; 271a4415854STim Northover } else if (isa<TruncInst>(I) && 272a4415854STim Northover TLI.allowTruncateForTailCall(Op->getType(), I->getType())) { 273a4415854STim Northover DataBits = std::min(DataBits, I->getType()->getPrimitiveSizeInBits()); 274a4415854STim Northover NoopInput = Op; 275b8bd232aSStephen Lin } else if (isa<CallInst>(I)) { 276a4415854STim Northover // Look through call (skipping callee) 277a4415854STim Northover for (User::const_op_iterator i = I->op_begin(), e = I->op_end() - 1; 278b8bd232aSStephen Lin i != e; ++i) { 279b8bd232aSStephen Lin unsigned attrInd = i - I->op_begin() + 1; 280b8bd232aSStephen Lin if (cast<CallInst>(I)->paramHasAttr(attrInd, Attribute::Returned) && 281b8bd232aSStephen Lin isNoopBitcast((*i)->getType(), I->getType(), TLI)) { 282b8bd232aSStephen Lin NoopInput = *i; 283b8bd232aSStephen Lin break; 284b8bd232aSStephen Lin } 285b8bd232aSStephen Lin } 286b8bd232aSStephen Lin } else if (isa<InvokeInst>(I)) { 287a4415854STim Northover // Look through invoke (skipping BB, BB, Callee) 288a4415854STim Northover for (User::const_op_iterator i = I->op_begin(), e = I->op_end() - 3; 289b8bd232aSStephen Lin i != e; ++i) { 290b8bd232aSStephen Lin unsigned attrInd = i - I->op_begin() + 1; 291b8bd232aSStephen Lin if (cast<InvokeInst>(I)->paramHasAttr(attrInd, Attribute::Returned) && 292b8bd232aSStephen Lin isNoopBitcast((*i)->getType(), I->getType(), TLI)) { 293b8bd232aSStephen Lin NoopInput = *i; 294b8bd232aSStephen Lin break; 295b8bd232aSStephen Lin } 296b8bd232aSStephen Lin } 297a4415854STim Northover } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(V)) { 298a4415854STim Northover // Value may come from either the aggregate or the scalar 299a4415854STim Northover ArrayRef<unsigned> InsertLoc = IVI->getIndices(); 300e4310fe9STim Northover if (ValLoc.size() >= InsertLoc.size() && 301e4310fe9STim Northover std::equal(InsertLoc.begin(), InsertLoc.end(), ValLoc.rbegin())) { 302a4415854STim Northover // The type being inserted is a nested sub-type of the aggregate; we 303a4415854STim Northover // have to remove those initial indices to get the location we're 304a4415854STim Northover // interested in for the operand. 305a4415854STim Northover ValLoc.resize(ValLoc.size() - InsertLoc.size()); 306a4415854STim Northover NoopInput = IVI->getInsertedValueOperand(); 307a4415854STim Northover } else { 308a4415854STim Northover // The struct we're inserting into has the value we're interested in, no 309a4415854STim Northover // change of address. 310a4415854STim Northover NoopInput = Op; 311a4415854STim Northover } 312a4415854STim Northover } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) { 313a4415854STim Northover // The part we're interested in will inevitably be some sub-section of the 314a4415854STim Northover // previous aggregate. Combine the two paths to obtain the true address of 315a4415854STim Northover // our element. 316a4415854STim Northover ArrayRef<unsigned> ExtractLoc = EVI->getIndices(); 3174f6ac162SBenjamin Kramer ValLoc.append(ExtractLoc.rbegin(), ExtractLoc.rend()); 318a4415854STim Northover NoopInput = Op; 319a4415854STim Northover } 320a4415854STim Northover // Terminate if we couldn't find anything to look through. 321a4415854STim Northover if (!NoopInput) 322a4415854STim Northover return V; 323a4415854STim Northover 324a4415854STim Northover V = NoopInput; 325ffc44549SStephen Lin } 326182fe3eeSChris Lattner } 327182fe3eeSChris Lattner 328a4415854STim Northover /// Return true if this scalar return value only has bits discarded on its path 329a4415854STim Northover /// from the "tail call" to the "ret". This includes the obvious noop 330a4415854STim Northover /// instructions handled by getNoopInput above as well as free truncations (or 331a4415854STim Northover /// extensions prior to the call). 332a4415854STim Northover static bool slotOnlyDiscardsData(const Value *RetVal, const Value *CallVal, 333a4415854STim Northover SmallVectorImpl<unsigned> &RetIndices, 334a4415854STim Northover SmallVectorImpl<unsigned> &CallIndices, 335707d68f0STim Northover bool AllowDifferingSizes, 33644ede33aSMehdi Amini const TargetLoweringBase &TLI, 33744ede33aSMehdi Amini const DataLayout &DL) { 3384f3615deSChris Lattner 339a4415854STim Northover // Trace the sub-value needed by the return value as far back up the graph as 340a4415854STim Northover // possible, in the hope that it will intersect with the value produced by the 341a4415854STim Northover // call. In the simple case with no "returned" attribute, the hope is actually 342a4415854STim Northover // that we end up back at the tail call instruction itself. 343a4415854STim Northover unsigned BitsRequired = UINT_MAX; 34444ede33aSMehdi Amini RetVal = getNoopInput(RetVal, RetIndices, BitsRequired, TLI, DL); 345ffc44549SStephen Lin 346a4415854STim Northover // If this slot in the value returned is undef, it doesn't matter what the 347a4415854STim Northover // call puts there, it'll be fine. 348a4415854STim Northover if (isa<UndefValue>(RetVal)) 349a4415854STim Northover return true; 350ffc44549SStephen Lin 351a4415854STim Northover // Now do a similar search up through the graph to find where the value 352a4415854STim Northover // actually returned by the "tail call" comes from. In the simple case without 353a4415854STim Northover // a "returned" attribute, the search will be blocked immediately and the loop 354a4415854STim Northover // a Noop. 355a4415854STim Northover unsigned BitsProvided = UINT_MAX; 35644ede33aSMehdi Amini CallVal = getNoopInput(CallVal, CallIndices, BitsProvided, TLI, DL); 357a4415854STim Northover 358a4415854STim Northover // There's no hope if we can't actually trace them to (the same part of!) the 359a4415854STim Northover // same value. 360a4415854STim Northover if (CallVal != RetVal || CallIndices != RetIndices) 361a4415854STim Northover return false; 362a4415854STim Northover 363a4415854STim Northover // However, intervening truncates may have made the call non-tail. Make sure 364a4415854STim Northover // all the bits that are needed by the "ret" have been provided by the "tail 365a4415854STim Northover // call". FIXME: with sufficiently cunning bit-tracking, we could look through 366a4415854STim Northover // extensions too. 367707d68f0STim Northover if (BitsProvided < BitsRequired || 368707d68f0STim Northover (!AllowDifferingSizes && BitsProvided != BitsRequired)) 369a4415854STim Northover return false; 370a4415854STim Northover 371ffc44549SStephen Lin return true; 372ffc44549SStephen Lin } 373a4415854STim Northover 374a4415854STim Northover /// For an aggregate type, determine whether a given index is within bounds or 375a4415854STim Northover /// not. 376a4415854STim Northover static bool indexReallyValid(CompositeType *T, unsigned Idx) { 377a4415854STim Northover if (ArrayType *AT = dyn_cast<ArrayType>(T)) 378a4415854STim Northover return Idx < AT->getNumElements(); 379a4415854STim Northover 380a4415854STim Northover return Idx < cast<StructType>(T)->getNumElements(); 381ffc44549SStephen Lin } 382a4415854STim Northover 383a4415854STim Northover /// Move the given iterators to the next leaf type in depth first traversal. 384a4415854STim Northover /// 385a4415854STim Northover /// Performs a depth-first traversal of the type as specified by its arguments, 386a4415854STim Northover /// stopping at the next leaf node (which may be a legitimate scalar type or an 387a4415854STim Northover /// empty struct or array). 388a4415854STim Northover /// 389a4415854STim Northover /// @param SubTypes List of the partial components making up the type from 390a4415854STim Northover /// outermost to innermost non-empty aggregate. The element currently 391a4415854STim Northover /// represented is SubTypes.back()->getTypeAtIndex(Path.back() - 1). 392a4415854STim Northover /// 393a4415854STim Northover /// @param Path Set of extractvalue indices leading from the outermost type 394a4415854STim Northover /// (SubTypes[0]) to the leaf node currently represented. 395a4415854STim Northover /// 396a4415854STim Northover /// @returns true if a new type was found, false otherwise. Calling this 397a4415854STim Northover /// function again on a finished iterator will repeatedly return 398a4415854STim Northover /// false. SubTypes.back()->getTypeAtIndex(Path.back()) is either an empty 399a4415854STim Northover /// aggregate or a non-aggregate 400df03449aSBenjamin Kramer static bool advanceToNextLeafType(SmallVectorImpl<CompositeType *> &SubTypes, 401a4415854STim Northover SmallVectorImpl<unsigned> &Path) { 402a4415854STim Northover // First march back up the tree until we can successfully increment one of the 403a4415854STim Northover // coordinates in Path. 404a4415854STim Northover while (!Path.empty() && !indexReallyValid(SubTypes.back(), Path.back() + 1)) { 405a4415854STim Northover Path.pop_back(); 406a4415854STim Northover SubTypes.pop_back(); 407a4415854STim Northover } 408a4415854STim Northover 409a4415854STim Northover // If we reached the top, then the iterator is done. 410a4415854STim Northover if (Path.empty()) 411a4415854STim Northover return false; 412a4415854STim Northover 413a4415854STim Northover // We know there's *some* valid leaf now, so march back down the tree picking 414a4415854STim Northover // out the left-most element at each node. 415a4415854STim Northover ++Path.back(); 416a4415854STim Northover Type *DeeperType = SubTypes.back()->getTypeAtIndex(Path.back()); 417a4415854STim Northover while (DeeperType->isAggregateType()) { 418a4415854STim Northover CompositeType *CT = cast<CompositeType>(DeeperType); 419a4415854STim Northover if (!indexReallyValid(CT, 0)) 420a4415854STim Northover return true; 421a4415854STim Northover 422a4415854STim Northover SubTypes.push_back(CT); 423a4415854STim Northover Path.push_back(0); 424a4415854STim Northover 425a4415854STim Northover DeeperType = CT->getTypeAtIndex(0U); 426a4415854STim Northover } 427a4415854STim Northover 428ffc44549SStephen Lin return true; 429ffc44549SStephen Lin } 430a4415854STim Northover 431a4415854STim Northover /// Find the first non-empty, scalar-like type in Next and setup the iterator 432a4415854STim Northover /// components. 433a4415854STim Northover /// 434a4415854STim Northover /// Assuming Next is an aggregate of some kind, this function will traverse the 435a4415854STim Northover /// tree from left to right (i.e. depth-first) looking for the first 436a4415854STim Northover /// non-aggregate type which will play a role in function return. 437a4415854STim Northover /// 438a4415854STim Northover /// For example, if Next was {[0 x i64], {{}, i32, {}}, i32} then we would setup 439a4415854STim Northover /// Path as [1, 1] and SubTypes as [Next, {{}, i32, {}}] to represent the first 440a4415854STim Northover /// i32 in that type. 441a4415854STim Northover static bool firstRealType(Type *Next, 442a4415854STim Northover SmallVectorImpl<CompositeType *> &SubTypes, 443a4415854STim Northover SmallVectorImpl<unsigned> &Path) { 444a4415854STim Northover // First initialise the iterator components to the first "leaf" node 445a4415854STim Northover // (i.e. node with no valid sub-type at any index, so {} does count as a leaf 446a4415854STim Northover // despite nominally being an aggregate). 447a4415854STim Northover while (Next->isAggregateType() && 448a4415854STim Northover indexReallyValid(cast<CompositeType>(Next), 0)) { 449a4415854STim Northover SubTypes.push_back(cast<CompositeType>(Next)); 450a4415854STim Northover Path.push_back(0); 451a4415854STim Northover Next = cast<CompositeType>(Next)->getTypeAtIndex(0U); 452ffc44549SStephen Lin } 453ffc44549SStephen Lin 454a4415854STim Northover // If there's no Path now, Next was originally scalar already (or empty 455a4415854STim Northover // leaf). We're done. 456a4415854STim Northover if (Path.empty()) 457a4415854STim Northover return true; 458ffc44549SStephen Lin 459a4415854STim Northover // Otherwise, use normal iteration to keep looking through the tree until we 460a4415854STim Northover // find a non-aggregate type. 461a4415854STim Northover while (SubTypes.back()->getTypeAtIndex(Path.back())->isAggregateType()) { 462a4415854STim Northover if (!advanceToNextLeafType(SubTypes, Path)) 463ffc44549SStephen Lin return false; 464ffc44549SStephen Lin } 4654f3615deSChris Lattner 466a4415854STim Northover return true; 467a4415854STim Northover } 468a4415854STim Northover 469a4415854STim Northover /// Set the iterator data-structures to the next non-empty, non-aggregate 470a4415854STim Northover /// subtype. 471df03449aSBenjamin Kramer static bool nextRealType(SmallVectorImpl<CompositeType *> &SubTypes, 472a4415854STim Northover SmallVectorImpl<unsigned> &Path) { 473a4415854STim Northover do { 474a4415854STim Northover if (!advanceToNextLeafType(SubTypes, Path)) 475a4415854STim Northover return false; 476a4415854STim Northover 477a4415854STim Northover assert(!Path.empty() && "found a leaf but didn't set the path?"); 478a4415854STim Northover } while (SubTypes.back()->getTypeAtIndex(Path.back())->isAggregateType()); 479a4415854STim Northover 480a4415854STim Northover return true; 481a4415854STim Northover } 482a4415854STim Northover 483a4415854STim Northover 484450aa64fSDan Gohman /// Test if the given instruction is in a position to be optimized 485450aa64fSDan Gohman /// with a tail-call. This roughly means that it's in a block with 486450aa64fSDan Gohman /// a return and there's nothing that needs to be scheduled 487450aa64fSDan Gohman /// between it and the return. 488450aa64fSDan Gohman /// 489450aa64fSDan Gohman /// This function only tests target-independent requirements. 490480872b4SJuergen Ributzka bool llvm::isInTailCallPosition(ImmutableCallSite CS, const TargetMachine &TM) { 491450aa64fSDan Gohman const Instruction *I = CS.getInstruction(); 492450aa64fSDan Gohman const BasicBlock *ExitBB = I->getParent(); 493450aa64fSDan Gohman const TerminatorInst *Term = ExitBB->getTerminator(); 494450aa64fSDan Gohman const ReturnInst *Ret = dyn_cast<ReturnInst>(Term); 495450aa64fSDan Gohman 496450aa64fSDan Gohman // The block must end in a return statement or unreachable. 497450aa64fSDan Gohman // 498450aa64fSDan Gohman // FIXME: Decline tailcall if it's not guaranteed and if the block ends in 499450aa64fSDan Gohman // an unreachable, for now. The way tailcall optimization is currently 500450aa64fSDan Gohman // implemented means it will add an epilogue followed by a jump. That is 501450aa64fSDan Gohman // not profitable. Also, if the callee is a special function (e.g. 502450aa64fSDan Gohman // longjmp on x86), it can end up causing miscompilation that has not 503450aa64fSDan Gohman // been fully understood. 504450aa64fSDan Gohman if (!Ret && 5054ce9863dSJuergen Ributzka (!TM.Options.GuaranteedTailCallOpt || !isa<UnreachableInst>(Term))) 5064f3615deSChris Lattner return false; 507450aa64fSDan Gohman 508450aa64fSDan Gohman // If I will have a chain, make sure no other instruction that will have a 509450aa64fSDan Gohman // chain interposes between I and the return. 5100a92f86fSDavid Majnemer if (I->mayHaveSideEffects() || I->mayReadFromMemory() || 5110a92f86fSDavid Majnemer !isSafeToSpeculativelyExecute(I)) 512b6d0bd48SBenjamin Kramer for (BasicBlock::const_iterator BBI = std::prev(ExitBB->end(), 2);; --BBI) { 513450aa64fSDan Gohman if (&*BBI == I) 514450aa64fSDan Gohman break; 515450aa64fSDan Gohman // Debug info intrinsics do not get in the way of tail call optimization. 516450aa64fSDan Gohman if (isa<DbgInfoIntrinsic>(BBI)) 517450aa64fSDan Gohman continue; 518450aa64fSDan Gohman if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() || 519980f8f26SDuncan P. N. Exon Smith !isSafeToSpeculativelyExecute(&*BBI)) 520450aa64fSDan Gohman return false; 521450aa64fSDan Gohman } 522450aa64fSDan Gohman 523f734a8baSEric Christopher const Function *F = ExitBB->getParent(); 524d913448bSEric Christopher return returnTypeIsEligibleForTailCall( 525f734a8baSEric Christopher F, I, Ret, *TM.getSubtargetImpl(*F)->getTargetLowering()); 526ce0e4c26SMichael Gottesman } 527ce0e4c26SMichael Gottesman 528ce0e4c26SMichael Gottesman bool llvm::returnTypeIsEligibleForTailCall(const Function *F, 529ce0e4c26SMichael Gottesman const Instruction *I, 530ce0e4c26SMichael Gottesman const ReturnInst *Ret, 531ce0e4c26SMichael Gottesman const TargetLoweringBase &TLI) { 532450aa64fSDan Gohman // If the block ends with a void return or unreachable, it doesn't matter 533450aa64fSDan Gohman // what the call's return type is. 534450aa64fSDan Gohman if (!Ret || Ret->getNumOperands() == 0) return true; 535450aa64fSDan Gohman 536450aa64fSDan Gohman // If the return value is undef, it doesn't matter what the call's 537450aa64fSDan Gohman // return type is. 538450aa64fSDan Gohman if (isa<UndefValue>(Ret->getOperand(0))) return true; 539450aa64fSDan Gohman 540707d68f0STim Northover // Make sure the attributes attached to each return are compatible. 541ce0e4c26SMichael Gottesman AttrBuilder CallerAttrs(F->getAttributes(), 542707d68f0STim Northover AttributeSet::ReturnIndex); 543707d68f0STim Northover AttrBuilder CalleeAttrs(cast<CallInst>(I)->getAttributes(), 544707d68f0STim Northover AttributeSet::ReturnIndex); 545707d68f0STim Northover 546707d68f0STim Northover // Noalias is completely benign as far as calling convention goes, it 547707d68f0STim Northover // shouldn't affect whether the call is a tail call. 548707d68f0STim Northover CallerAttrs = CallerAttrs.removeAttribute(Attribute::NoAlias); 549707d68f0STim Northover CalleeAttrs = CalleeAttrs.removeAttribute(Attribute::NoAlias); 550707d68f0STim Northover 551707d68f0STim Northover bool AllowDifferingSizes = true; 552707d68f0STim Northover if (CallerAttrs.contains(Attribute::ZExt)) { 553707d68f0STim Northover if (!CalleeAttrs.contains(Attribute::ZExt)) 554450aa64fSDan Gohman return false; 555450aa64fSDan Gohman 556707d68f0STim Northover AllowDifferingSizes = false; 557707d68f0STim Northover CallerAttrs.removeAttribute(Attribute::ZExt); 558707d68f0STim Northover CalleeAttrs.removeAttribute(Attribute::ZExt); 559707d68f0STim Northover } else if (CallerAttrs.contains(Attribute::SExt)) { 560707d68f0STim Northover if (!CalleeAttrs.contains(Attribute::SExt)) 561707d68f0STim Northover return false; 562707d68f0STim Northover 563707d68f0STim Northover AllowDifferingSizes = false; 564707d68f0STim Northover CallerAttrs.removeAttribute(Attribute::SExt); 565707d68f0STim Northover CalleeAttrs.removeAttribute(Attribute::SExt); 566707d68f0STim Northover } 567707d68f0STim Northover 568707d68f0STim Northover // If they're still different, there's some facet we don't understand 569707d68f0STim Northover // (currently only "inreg", but in future who knows). It may be OK but the 570707d68f0STim Northover // only safe option is to reject the tail call. 571707d68f0STim Northover if (CallerAttrs != CalleeAttrs) 572450aa64fSDan Gohman return false; 573450aa64fSDan Gohman 574a4415854STim Northover const Value *RetVal = Ret->getOperand(0), *CallVal = I; 575a4415854STim Northover SmallVector<unsigned, 4> RetPath, CallPath; 576a4415854STim Northover SmallVector<CompositeType *, 4> RetSubTypes, CallSubTypes; 577a4415854STim Northover 578a4415854STim Northover bool RetEmpty = !firstRealType(RetVal->getType(), RetSubTypes, RetPath); 579a4415854STim Northover bool CallEmpty = !firstRealType(CallVal->getType(), CallSubTypes, CallPath); 580a4415854STim Northover 581a4415854STim Northover // Nothing's actually returned, it doesn't matter what the callee put there 582a4415854STim Northover // it's a valid tail call. 583a4415854STim Northover if (RetEmpty) 584a4415854STim Northover return true; 585a4415854STim Northover 586a4415854STim Northover // Iterate pairwise through each of the value types making up the tail call 587a4415854STim Northover // and the corresponding return. For each one we want to know whether it's 588a4415854STim Northover // essentially going directly from the tail call to the ret, via operations 589a4415854STim Northover // that end up not generating any code. 590a4415854STim Northover // 591a4415854STim Northover // We allow a certain amount of covariance here. For example it's permitted 592a4415854STim Northover // for the tail call to define more bits than the ret actually cares about 593a4415854STim Northover // (e.g. via a truncate). 594a4415854STim Northover do { 595a4415854STim Northover if (CallEmpty) { 596a4415854STim Northover // We've exhausted the values produced by the tail call instruction, the 597a4415854STim Northover // rest are essentially undef. The type doesn't really matter, but we need 598a4415854STim Northover // *something*. 599a4415854STim Northover Type *SlotType = RetSubTypes.back()->getTypeAtIndex(RetPath.back()); 600a4415854STim Northover CallVal = UndefValue::get(SlotType); 601a4415854STim Northover } 602a4415854STim Northover 603a4415854STim Northover // The manipulations performed when we're looking through an insertvalue or 604a4415854STim Northover // an extractvalue would happen at the front of the RetPath list, so since 605a4415854STim Northover // we have to copy it anyway it's more efficient to create a reversed copy. 6064f6ac162SBenjamin Kramer SmallVector<unsigned, 4> TmpRetPath(RetPath.rbegin(), RetPath.rend()); 6074f6ac162SBenjamin Kramer SmallVector<unsigned, 4> TmpCallPath(CallPath.rbegin(), CallPath.rend()); 608a4415854STim Northover 609a4415854STim Northover // Finally, we can check whether the value produced by the tail call at this 610a4415854STim Northover // index is compatible with the value we return. 611707d68f0STim Northover if (!slotOnlyDiscardsData(RetVal, CallVal, TmpRetPath, TmpCallPath, 61244ede33aSMehdi Amini AllowDifferingSizes, TLI, 61344ede33aSMehdi Amini F->getParent()->getDataLayout())) 614a4415854STim Northover return false; 615a4415854STim Northover 616a4415854STim Northover CallEmpty = !nextRealType(CallSubTypes, CallPath); 617a4415854STim Northover } while(nextRealType(RetSubTypes, RetPath)); 618a4415854STim Northover 619a4415854STim Northover return true; 620450aa64fSDan Gohman } 621f21434ccSRafael Espindola 622f21434ccSRafael Espindola bool llvm::canBeOmittedFromSymbolTable(const GlobalValue *GV) { 623f21434ccSRafael Espindola if (!GV->hasLinkOnceODRLinkage()) 624f21434ccSRafael Espindola return false; 625f21434ccSRafael Espindola 626*96efdd61SPeter Collingbourne // We assume that anyone who sets global unnamed_addr on a non-constant knows 627*96efdd61SPeter Collingbourne // what they're doing. 628*96efdd61SPeter Collingbourne if (GV->hasGlobalUnnamedAddr()) 629f21434ccSRafael Espindola return true; 630f21434ccSRafael Espindola 631f21434ccSRafael Espindola // If it is a non constant variable, it needs to be uniqued across shared 632f21434ccSRafael Espindola // objects. 633f21434ccSRafael Espindola if (const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV)) { 634f21434ccSRafael Espindola if (!Var->isConstant()) 635f21434ccSRafael Espindola return false; 636f21434ccSRafael Espindola } 637f21434ccSRafael Espindola 638*96efdd61SPeter Collingbourne return GV->hasAtLeastLocalUnnamedAddr(); 639f21434ccSRafael Espindola } 64016193552SDavid Majnemer 641a224de06SRafael Espindola // FIXME: make this a proper option 642a224de06SRafael Espindola static bool CanUseCopyRelocWithPIE = false; 643a224de06SRafael Espindola 644a224de06SRafael Espindola bool llvm::shouldAssumeDSOLocal(Reloc::Model RM, const Triple &TT, 645a224de06SRafael Espindola const Module &M, const GlobalValue *GV) { 646a224de06SRafael Espindola // DLLImport explicitly marks the GV as external. 647a224de06SRafael Espindola if (GV && GV->hasDLLImportStorageClass()) 648a224de06SRafael Espindola return false; 649a224de06SRafael Espindola 650a224de06SRafael Espindola // Every other GV is local on COFF 651a224de06SRafael Espindola if (TT.isOSBinFormatCOFF()) 652a224de06SRafael Espindola return true; 653a224de06SRafael Espindola 654a224de06SRafael Espindola if (RM == Reloc::Static) 655a224de06SRafael Espindola return true; 656a224de06SRafael Espindola 657a224de06SRafael Espindola if (GV && (GV->hasLocalLinkage() || !GV->hasDefaultVisibility())) 658a224de06SRafael Espindola return true; 659a224de06SRafael Espindola 660a224de06SRafael Espindola if (TT.isOSBinFormatELF()) { 661a224de06SRafael Espindola assert(RM != Reloc::DynamicNoPIC); 662a224de06SRafael Espindola // Some linkers can use copy relocations with pie executables. 663a224de06SRafael Espindola if (M.getPIELevel() != PIELevel::Default) { 664a224de06SRafael Espindola if (CanUseCopyRelocWithPIE) 665a224de06SRafael Espindola return true; 666a224de06SRafael Espindola 667a224de06SRafael Espindola // If the symbol is defined, it cannot be preempted. 668a224de06SRafael Espindola if (GV && !GV->isDeclarationForLinker()) 669a224de06SRafael Espindola return true; 670a224de06SRafael Espindola return false; 671a224de06SRafael Espindola } 672a224de06SRafael Espindola 673a224de06SRafael Espindola // ELF supports preemption of other symbols. 674a224de06SRafael Espindola return false; 675a224de06SRafael Espindola } 676a224de06SRafael Espindola 677a224de06SRafael Espindola assert(TT.isOSBinFormatMachO()); 678a224de06SRafael Espindola if (GV && GV->isStrongDefinitionForLinker()) 679a224de06SRafael Espindola return true; 680a224de06SRafael Espindola 681a224de06SRafael Espindola return false; 682a224de06SRafael Espindola } 683a224de06SRafael Espindola 68416193552SDavid Majnemer static void collectFuncletMembers( 68516193552SDavid Majnemer DenseMap<const MachineBasicBlock *, int> &FuncletMembership, int Funclet, 68616193552SDavid Majnemer const MachineBasicBlock *MBB) { 687734d7c32SDavid Majnemer SmallVector<const MachineBasicBlock *, 16> Worklist = {MBB}; 688734d7c32SDavid Majnemer while (!Worklist.empty()) { 689734d7c32SDavid Majnemer const MachineBasicBlock *Visiting = Worklist.pop_back_val(); 690734d7c32SDavid Majnemer // Don't follow blocks which start new funclets. 691734d7c32SDavid Majnemer if (Visiting->isEHPad() && Visiting != MBB) 692734d7c32SDavid Majnemer continue; 693734d7c32SDavid Majnemer 6947b54b525SDavid Blaikie // Add this MBB to our funclet. 695734d7c32SDavid Majnemer auto P = FuncletMembership.insert(std::make_pair(Visiting, Funclet)); 6967b54b525SDavid Blaikie 69716193552SDavid Majnemer // Don't revisit blocks. 6987b54b525SDavid Blaikie if (!P.second) { 6997b54b525SDavid Blaikie assert(P.first->second == Funclet && "MBB is part of two funclets!"); 700734d7c32SDavid Majnemer continue; 70116193552SDavid Majnemer } 70216193552SDavid Majnemer 70316193552SDavid Majnemer // Returns are boundaries where funclet transfer can occur, don't follow 70416193552SDavid Majnemer // successors. 705734d7c32SDavid Majnemer if (Visiting->isReturnBlock()) 706734d7c32SDavid Majnemer continue; 70716193552SDavid Majnemer 708734d7c32SDavid Majnemer for (const MachineBasicBlock *Succ : Visiting->successors()) 709734d7c32SDavid Majnemer Worklist.push_back(Succ); 710734d7c32SDavid Majnemer } 71116193552SDavid Majnemer } 71216193552SDavid Majnemer 71316193552SDavid Majnemer DenseMap<const MachineBasicBlock *, int> 71416193552SDavid Majnemer llvm::getFuncletMembership(const MachineFunction &MF) { 71516193552SDavid Majnemer DenseMap<const MachineBasicBlock *, int> FuncletMembership; 71616193552SDavid Majnemer 71716193552SDavid Majnemer // We don't have anything to do if there aren't any EH pads. 71816193552SDavid Majnemer if (!MF.getMMI().hasEHFunclets()) 71916193552SDavid Majnemer return FuncletMembership; 72016193552SDavid Majnemer 721e4f9b09bSDavid Majnemer int EntryBBNumber = MF.front().getNumber(); 72216193552SDavid Majnemer bool IsSEH = isAsynchronousEHPersonality( 72316193552SDavid Majnemer classifyEHPersonality(MF.getFunction()->getPersonalityFn())); 72416193552SDavid Majnemer 72516193552SDavid Majnemer const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 72616193552SDavid Majnemer SmallVector<const MachineBasicBlock *, 16> FuncletBlocks; 727e4f9b09bSDavid Majnemer SmallVector<const MachineBasicBlock *, 16> UnreachableBlocks; 728e4f9b09bSDavid Majnemer SmallVector<const MachineBasicBlock *, 16> SEHCatchPads; 72916193552SDavid Majnemer SmallVector<std::pair<const MachineBasicBlock *, int>, 16> CatchRetSuccessors; 73016193552SDavid Majnemer for (const MachineBasicBlock &MBB : MF) { 731e4f9b09bSDavid Majnemer if (MBB.isEHFuncletEntry()) { 73216193552SDavid Majnemer FuncletBlocks.push_back(&MBB); 733e4f9b09bSDavid Majnemer } else if (IsSEH && MBB.isEHPad()) { 734e4f9b09bSDavid Majnemer SEHCatchPads.push_back(&MBB); 735e4f9b09bSDavid Majnemer } else if (MBB.pred_empty()) { 736e4f9b09bSDavid Majnemer UnreachableBlocks.push_back(&MBB); 737e4f9b09bSDavid Majnemer } 73816193552SDavid Majnemer 73916193552SDavid Majnemer MachineBasicBlock::const_iterator MBBI = MBB.getFirstTerminator(); 74016193552SDavid Majnemer // CatchPads are not funclets for SEH so do not consider CatchRet to 74116193552SDavid Majnemer // transfer control to another funclet. 742e4f9b09bSDavid Majnemer if (MBBI->getOpcode() != TII->getCatchReturnOpcode()) 74316193552SDavid Majnemer continue; 74416193552SDavid Majnemer 745e4f9b09bSDavid Majnemer // FIXME: SEH CatchPads are not necessarily in the parent function: 746e4f9b09bSDavid Majnemer // they could be inside a finally block. 74716193552SDavid Majnemer const MachineBasicBlock *Successor = MBBI->getOperand(0).getMBB(); 74816193552SDavid Majnemer const MachineBasicBlock *SuccessorColor = MBBI->getOperand(1).getMBB(); 749e4f9b09bSDavid Majnemer CatchRetSuccessors.push_back( 750e4f9b09bSDavid Majnemer {Successor, IsSEH ? EntryBBNumber : SuccessorColor->getNumber()}); 75116193552SDavid Majnemer } 75216193552SDavid Majnemer 75316193552SDavid Majnemer // We don't have anything to do if there aren't any EH pads. 75416193552SDavid Majnemer if (FuncletBlocks.empty()) 75516193552SDavid Majnemer return FuncletMembership; 75616193552SDavid Majnemer 75716193552SDavid Majnemer // Identify all the basic blocks reachable from the function entry. 758980f8f26SDuncan P. N. Exon Smith collectFuncletMembers(FuncletMembership, EntryBBNumber, &MF.front()); 759e4f9b09bSDavid Majnemer // All blocks not part of a funclet are in the parent function. 760e4f9b09bSDavid Majnemer for (const MachineBasicBlock *MBB : UnreachableBlocks) 761e4f9b09bSDavid Majnemer collectFuncletMembers(FuncletMembership, EntryBBNumber, MBB); 76216193552SDavid Majnemer // Next, identify all the blocks inside the funclets. 76316193552SDavid Majnemer for (const MachineBasicBlock *MBB : FuncletBlocks) 76416193552SDavid Majnemer collectFuncletMembers(FuncletMembership, MBB->getNumber(), MBB); 765e4f9b09bSDavid Majnemer // SEH CatchPads aren't really funclets, handle them separately. 766e4f9b09bSDavid Majnemer for (const MachineBasicBlock *MBB : SEHCatchPads) 767e4f9b09bSDavid Majnemer collectFuncletMembers(FuncletMembership, EntryBBNumber, MBB); 76816193552SDavid Majnemer // Finally, identify all the targets of a catchret. 76916193552SDavid Majnemer for (std::pair<const MachineBasicBlock *, int> CatchRetPair : 77016193552SDavid Majnemer CatchRetSuccessors) 77116193552SDavid Majnemer collectFuncletMembers(FuncletMembership, CatchRetPair.second, 77216193552SDavid Majnemer CatchRetPair.first); 77316193552SDavid Majnemer return FuncletMembership; 77416193552SDavid Majnemer } 775