xref: /openbsd-src/gnu/llvm/llvm/lib/CodeGen/Analysis.cpp (revision 73471bf04ceb096474c7f0fa83b1b65c70a787a1)
109467b48Spatrick //===-- Analysis.cpp - CodeGen LLVM IR Analysis Utilities -----------------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This file defines several CodeGen-specific LLVM IR analysis utilities.
1009467b48Spatrick //
1109467b48Spatrick //===----------------------------------------------------------------------===//
1209467b48Spatrick 
1309467b48Spatrick #include "llvm/CodeGen/Analysis.h"
1409467b48Spatrick #include "llvm/Analysis/ValueTracking.h"
1509467b48Spatrick #include "llvm/CodeGen/MachineFunction.h"
1609467b48Spatrick #include "llvm/CodeGen/TargetInstrInfo.h"
1709467b48Spatrick #include "llvm/CodeGen/TargetLowering.h"
1809467b48Spatrick #include "llvm/CodeGen/TargetSubtargetInfo.h"
1909467b48Spatrick #include "llvm/IR/DataLayout.h"
2009467b48Spatrick #include "llvm/IR/DerivedTypes.h"
2109467b48Spatrick #include "llvm/IR/Function.h"
2209467b48Spatrick #include "llvm/IR/Instructions.h"
2309467b48Spatrick #include "llvm/IR/IntrinsicInst.h"
2409467b48Spatrick #include "llvm/IR/LLVMContext.h"
2509467b48Spatrick #include "llvm/IR/Module.h"
2609467b48Spatrick #include "llvm/Support/ErrorHandling.h"
2709467b48Spatrick #include "llvm/Support/MathExtras.h"
28097a140dSpatrick #include "llvm/Target/TargetMachine.h"
2909467b48Spatrick #include "llvm/Transforms/Utils/GlobalStatus.h"
3009467b48Spatrick 
3109467b48Spatrick using namespace llvm;
3209467b48Spatrick 
3309467b48Spatrick /// Compute the linearized index of a member in a nested aggregate/struct/array
3409467b48Spatrick /// by recursing and accumulating CurIndex as long as there are indices in the
3509467b48Spatrick /// index list.
3609467b48Spatrick unsigned llvm::ComputeLinearIndex(Type *Ty,
3709467b48Spatrick                                   const unsigned *Indices,
3809467b48Spatrick                                   const unsigned *IndicesEnd,
3909467b48Spatrick                                   unsigned CurIndex) {
4009467b48Spatrick   // Base case: We're done.
4109467b48Spatrick   if (Indices && Indices == IndicesEnd)
4209467b48Spatrick     return CurIndex;
4309467b48Spatrick 
4409467b48Spatrick   // Given a struct type, recursively traverse the elements.
4509467b48Spatrick   if (StructType *STy = dyn_cast<StructType>(Ty)) {
46*73471bf0Spatrick     for (auto I : llvm::enumerate(STy->elements())) {
47*73471bf0Spatrick       Type *ET = I.value();
48*73471bf0Spatrick       if (Indices && *Indices == I.index())
49*73471bf0Spatrick         return ComputeLinearIndex(ET, Indices + 1, IndicesEnd, CurIndex);
50*73471bf0Spatrick       CurIndex = ComputeLinearIndex(ET, nullptr, nullptr, CurIndex);
5109467b48Spatrick     }
5209467b48Spatrick     assert(!Indices && "Unexpected out of bound");
5309467b48Spatrick     return CurIndex;
5409467b48Spatrick   }
5509467b48Spatrick   // Given an array type, recursively traverse the elements.
5609467b48Spatrick   else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
5709467b48Spatrick     Type *EltTy = ATy->getElementType();
5809467b48Spatrick     unsigned NumElts = ATy->getNumElements();
5909467b48Spatrick     // Compute the Linear offset when jumping one element of the array
6009467b48Spatrick     unsigned EltLinearOffset = ComputeLinearIndex(EltTy, nullptr, nullptr, 0);
6109467b48Spatrick     if (Indices) {
6209467b48Spatrick       assert(*Indices < NumElts && "Unexpected out of bound");
6309467b48Spatrick       // If the indice is inside the array, compute the index to the requested
6409467b48Spatrick       // elt and recurse inside the element with the end of the indices list
6509467b48Spatrick       CurIndex += EltLinearOffset* *Indices;
6609467b48Spatrick       return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex);
6709467b48Spatrick     }
6809467b48Spatrick     CurIndex += EltLinearOffset*NumElts;
6909467b48Spatrick     return CurIndex;
7009467b48Spatrick   }
7109467b48Spatrick   // We haven't found the type we're looking for, so keep searching.
7209467b48Spatrick   return CurIndex + 1;
7309467b48Spatrick }
7409467b48Spatrick 
7509467b48Spatrick /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
7609467b48Spatrick /// EVTs that represent all the individual underlying
7709467b48Spatrick /// non-aggregate types that comprise it.
7809467b48Spatrick ///
7909467b48Spatrick /// If Offsets is non-null, it points to a vector to be filled in
8009467b48Spatrick /// with the in-memory offsets of each of the individual values.
8109467b48Spatrick ///
8209467b48Spatrick void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
8309467b48Spatrick                            Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
8409467b48Spatrick                            SmallVectorImpl<EVT> *MemVTs,
8509467b48Spatrick                            SmallVectorImpl<uint64_t> *Offsets,
8609467b48Spatrick                            uint64_t StartingOffset) {
8709467b48Spatrick   // Given a struct type, recursively traverse the elements.
8809467b48Spatrick   if (StructType *STy = dyn_cast<StructType>(Ty)) {
89*73471bf0Spatrick     // If the Offsets aren't needed, don't query the struct layout. This allows
90*73471bf0Spatrick     // us to support structs with scalable vectors for operations that don't
91*73471bf0Spatrick     // need offsets.
92*73471bf0Spatrick     const StructLayout *SL = Offsets ? DL.getStructLayout(STy) : nullptr;
9309467b48Spatrick     for (StructType::element_iterator EB = STy->element_begin(),
9409467b48Spatrick                                       EI = EB,
9509467b48Spatrick                                       EE = STy->element_end();
96*73471bf0Spatrick          EI != EE; ++EI) {
97*73471bf0Spatrick       // Don't compute the element offset if we didn't get a StructLayout above.
98*73471bf0Spatrick       uint64_t EltOffset = SL ? SL->getElementOffset(EI - EB) : 0;
9909467b48Spatrick       ComputeValueVTs(TLI, DL, *EI, ValueVTs, MemVTs, Offsets,
100*73471bf0Spatrick                       StartingOffset + EltOffset);
101*73471bf0Spatrick     }
10209467b48Spatrick     return;
10309467b48Spatrick   }
10409467b48Spatrick   // Given an array type, recursively traverse the elements.
10509467b48Spatrick   if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
10609467b48Spatrick     Type *EltTy = ATy->getElementType();
107*73471bf0Spatrick     uint64_t EltSize = DL.getTypeAllocSize(EltTy).getFixedValue();
10809467b48Spatrick     for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
10909467b48Spatrick       ComputeValueVTs(TLI, DL, EltTy, ValueVTs, MemVTs, Offsets,
11009467b48Spatrick                       StartingOffset + i * EltSize);
11109467b48Spatrick     return;
11209467b48Spatrick   }
11309467b48Spatrick   // Interpret void as zero return values.
11409467b48Spatrick   if (Ty->isVoidTy())
11509467b48Spatrick     return;
11609467b48Spatrick   // Base case: we can get an EVT for this LLVM IR type.
11709467b48Spatrick   ValueVTs.push_back(TLI.getValueType(DL, Ty));
11809467b48Spatrick   if (MemVTs)
11909467b48Spatrick     MemVTs->push_back(TLI.getMemValueType(DL, Ty));
12009467b48Spatrick   if (Offsets)
12109467b48Spatrick     Offsets->push_back(StartingOffset);
12209467b48Spatrick }
12309467b48Spatrick 
12409467b48Spatrick void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
12509467b48Spatrick                            Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
12609467b48Spatrick                            SmallVectorImpl<uint64_t> *Offsets,
12709467b48Spatrick                            uint64_t StartingOffset) {
12809467b48Spatrick   return ComputeValueVTs(TLI, DL, Ty, ValueVTs, /*MemVTs=*/nullptr, Offsets,
12909467b48Spatrick                          StartingOffset);
13009467b48Spatrick }
13109467b48Spatrick 
13209467b48Spatrick void llvm::computeValueLLTs(const DataLayout &DL, Type &Ty,
13309467b48Spatrick                             SmallVectorImpl<LLT> &ValueTys,
13409467b48Spatrick                             SmallVectorImpl<uint64_t> *Offsets,
13509467b48Spatrick                             uint64_t StartingOffset) {
13609467b48Spatrick   // Given a struct type, recursively traverse the elements.
13709467b48Spatrick   if (StructType *STy = dyn_cast<StructType>(&Ty)) {
138*73471bf0Spatrick     // If the Offsets aren't needed, don't query the struct layout. This allows
139*73471bf0Spatrick     // us to support structs with scalable vectors for operations that don't
140*73471bf0Spatrick     // need offsets.
141*73471bf0Spatrick     const StructLayout *SL = Offsets ? DL.getStructLayout(STy) : nullptr;
142*73471bf0Spatrick     for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I) {
143*73471bf0Spatrick       uint64_t EltOffset = SL ? SL->getElementOffset(I) : 0;
14409467b48Spatrick       computeValueLLTs(DL, *STy->getElementType(I), ValueTys, Offsets,
145*73471bf0Spatrick                        StartingOffset + EltOffset);
146*73471bf0Spatrick     }
14709467b48Spatrick     return;
14809467b48Spatrick   }
14909467b48Spatrick   // Given an array type, recursively traverse the elements.
15009467b48Spatrick   if (ArrayType *ATy = dyn_cast<ArrayType>(&Ty)) {
15109467b48Spatrick     Type *EltTy = ATy->getElementType();
152*73471bf0Spatrick     uint64_t EltSize = DL.getTypeAllocSize(EltTy).getFixedValue();
15309467b48Spatrick     for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
15409467b48Spatrick       computeValueLLTs(DL, *EltTy, ValueTys, Offsets,
15509467b48Spatrick                        StartingOffset + i * EltSize);
15609467b48Spatrick     return;
15709467b48Spatrick   }
15809467b48Spatrick   // Interpret void as zero return values.
15909467b48Spatrick   if (Ty.isVoidTy())
16009467b48Spatrick     return;
16109467b48Spatrick   // Base case: we can get an LLT for this LLVM IR type.
16209467b48Spatrick   ValueTys.push_back(getLLTForType(Ty, DL));
16309467b48Spatrick   if (Offsets != nullptr)
16409467b48Spatrick     Offsets->push_back(StartingOffset * 8);
16509467b48Spatrick }
16609467b48Spatrick 
16709467b48Spatrick /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
16809467b48Spatrick GlobalValue *llvm::ExtractTypeInfo(Value *V) {
16909467b48Spatrick   V = V->stripPointerCasts();
17009467b48Spatrick   GlobalValue *GV = dyn_cast<GlobalValue>(V);
17109467b48Spatrick   GlobalVariable *Var = dyn_cast<GlobalVariable>(V);
17209467b48Spatrick 
17309467b48Spatrick   if (Var && Var->getName() == "llvm.eh.catch.all.value") {
17409467b48Spatrick     assert(Var->hasInitializer() &&
17509467b48Spatrick            "The EH catch-all value must have an initializer");
17609467b48Spatrick     Value *Init = Var->getInitializer();
17709467b48Spatrick     GV = dyn_cast<GlobalValue>(Init);
17809467b48Spatrick     if (!GV) V = cast<ConstantPointerNull>(Init);
17909467b48Spatrick   }
18009467b48Spatrick 
18109467b48Spatrick   assert((GV || isa<ConstantPointerNull>(V)) &&
18209467b48Spatrick          "TypeInfo must be a global variable or NULL");
18309467b48Spatrick   return GV;
18409467b48Spatrick }
18509467b48Spatrick 
18609467b48Spatrick /// getFCmpCondCode - Return the ISD condition code corresponding to
18709467b48Spatrick /// the given LLVM IR floating-point condition code.  This includes
18809467b48Spatrick /// consideration of global floating-point math flags.
18909467b48Spatrick ///
19009467b48Spatrick ISD::CondCode llvm::getFCmpCondCode(FCmpInst::Predicate Pred) {
19109467b48Spatrick   switch (Pred) {
19209467b48Spatrick   case FCmpInst::FCMP_FALSE: return ISD::SETFALSE;
19309467b48Spatrick   case FCmpInst::FCMP_OEQ:   return ISD::SETOEQ;
19409467b48Spatrick   case FCmpInst::FCMP_OGT:   return ISD::SETOGT;
19509467b48Spatrick   case FCmpInst::FCMP_OGE:   return ISD::SETOGE;
19609467b48Spatrick   case FCmpInst::FCMP_OLT:   return ISD::SETOLT;
19709467b48Spatrick   case FCmpInst::FCMP_OLE:   return ISD::SETOLE;
19809467b48Spatrick   case FCmpInst::FCMP_ONE:   return ISD::SETONE;
19909467b48Spatrick   case FCmpInst::FCMP_ORD:   return ISD::SETO;
20009467b48Spatrick   case FCmpInst::FCMP_UNO:   return ISD::SETUO;
20109467b48Spatrick   case FCmpInst::FCMP_UEQ:   return ISD::SETUEQ;
20209467b48Spatrick   case FCmpInst::FCMP_UGT:   return ISD::SETUGT;
20309467b48Spatrick   case FCmpInst::FCMP_UGE:   return ISD::SETUGE;
20409467b48Spatrick   case FCmpInst::FCMP_ULT:   return ISD::SETULT;
20509467b48Spatrick   case FCmpInst::FCMP_ULE:   return ISD::SETULE;
20609467b48Spatrick   case FCmpInst::FCMP_UNE:   return ISD::SETUNE;
20709467b48Spatrick   case FCmpInst::FCMP_TRUE:  return ISD::SETTRUE;
20809467b48Spatrick   default: llvm_unreachable("Invalid FCmp predicate opcode!");
20909467b48Spatrick   }
21009467b48Spatrick }
21109467b48Spatrick 
21209467b48Spatrick ISD::CondCode llvm::getFCmpCodeWithoutNaN(ISD::CondCode CC) {
21309467b48Spatrick   switch (CC) {
21409467b48Spatrick     case ISD::SETOEQ: case ISD::SETUEQ: return ISD::SETEQ;
21509467b48Spatrick     case ISD::SETONE: case ISD::SETUNE: return ISD::SETNE;
21609467b48Spatrick     case ISD::SETOLT: case ISD::SETULT: return ISD::SETLT;
21709467b48Spatrick     case ISD::SETOLE: case ISD::SETULE: return ISD::SETLE;
21809467b48Spatrick     case ISD::SETOGT: case ISD::SETUGT: return ISD::SETGT;
21909467b48Spatrick     case ISD::SETOGE: case ISD::SETUGE: return ISD::SETGE;
22009467b48Spatrick     default: return CC;
22109467b48Spatrick   }
22209467b48Spatrick }
22309467b48Spatrick 
22409467b48Spatrick /// getICmpCondCode - Return the ISD condition code corresponding to
22509467b48Spatrick /// the given LLVM IR integer condition code.
22609467b48Spatrick ///
22709467b48Spatrick ISD::CondCode llvm::getICmpCondCode(ICmpInst::Predicate Pred) {
22809467b48Spatrick   switch (Pred) {
22909467b48Spatrick   case ICmpInst::ICMP_EQ:  return ISD::SETEQ;
23009467b48Spatrick   case ICmpInst::ICMP_NE:  return ISD::SETNE;
23109467b48Spatrick   case ICmpInst::ICMP_SLE: return ISD::SETLE;
23209467b48Spatrick   case ICmpInst::ICMP_ULE: return ISD::SETULE;
23309467b48Spatrick   case ICmpInst::ICMP_SGE: return ISD::SETGE;
23409467b48Spatrick   case ICmpInst::ICMP_UGE: return ISD::SETUGE;
23509467b48Spatrick   case ICmpInst::ICMP_SLT: return ISD::SETLT;
23609467b48Spatrick   case ICmpInst::ICMP_ULT: return ISD::SETULT;
23709467b48Spatrick   case ICmpInst::ICMP_SGT: return ISD::SETGT;
23809467b48Spatrick   case ICmpInst::ICMP_UGT: return ISD::SETUGT;
23909467b48Spatrick   default:
24009467b48Spatrick     llvm_unreachable("Invalid ICmp predicate opcode!");
24109467b48Spatrick   }
24209467b48Spatrick }
24309467b48Spatrick 
24409467b48Spatrick static bool isNoopBitcast(Type *T1, Type *T2,
24509467b48Spatrick                           const TargetLoweringBase& TLI) {
24609467b48Spatrick   return T1 == T2 || (T1->isPointerTy() && T2->isPointerTy()) ||
24709467b48Spatrick          (isa<VectorType>(T1) && isa<VectorType>(T2) &&
24809467b48Spatrick           TLI.isTypeLegal(EVT::getEVT(T1)) && TLI.isTypeLegal(EVT::getEVT(T2)));
24909467b48Spatrick }
25009467b48Spatrick 
25109467b48Spatrick /// Look through operations that will be free to find the earliest source of
25209467b48Spatrick /// this value.
25309467b48Spatrick ///
25409467b48Spatrick /// @param ValLoc If V has aggregate type, we will be interested in a particular
25509467b48Spatrick /// scalar component. This records its address; the reverse of this list gives a
25609467b48Spatrick /// sequence of indices appropriate for an extractvalue to locate the important
25709467b48Spatrick /// value. This value is updated during the function and on exit will indicate
25809467b48Spatrick /// similar information for the Value returned.
25909467b48Spatrick ///
26009467b48Spatrick /// @param DataBits If this function looks through truncate instructions, this
26109467b48Spatrick /// will record the smallest size attained.
26209467b48Spatrick static const Value *getNoopInput(const Value *V,
26309467b48Spatrick                                  SmallVectorImpl<unsigned> &ValLoc,
26409467b48Spatrick                                  unsigned &DataBits,
26509467b48Spatrick                                  const TargetLoweringBase &TLI,
26609467b48Spatrick                                  const DataLayout &DL) {
26709467b48Spatrick   while (true) {
26809467b48Spatrick     // Try to look through V1; if V1 is not an instruction, it can't be looked
26909467b48Spatrick     // through.
27009467b48Spatrick     const Instruction *I = dyn_cast<Instruction>(V);
27109467b48Spatrick     if (!I || I->getNumOperands() == 0) return V;
27209467b48Spatrick     const Value *NoopInput = nullptr;
27309467b48Spatrick 
27409467b48Spatrick     Value *Op = I->getOperand(0);
27509467b48Spatrick     if (isa<BitCastInst>(I)) {
27609467b48Spatrick       // Look through truly no-op bitcasts.
27709467b48Spatrick       if (isNoopBitcast(Op->getType(), I->getType(), TLI))
27809467b48Spatrick         NoopInput = Op;
27909467b48Spatrick     } else if (isa<GetElementPtrInst>(I)) {
28009467b48Spatrick       // Look through getelementptr
28109467b48Spatrick       if (cast<GetElementPtrInst>(I)->hasAllZeroIndices())
28209467b48Spatrick         NoopInput = Op;
28309467b48Spatrick     } else if (isa<IntToPtrInst>(I)) {
28409467b48Spatrick       // Look through inttoptr.
28509467b48Spatrick       // Make sure this isn't a truncating or extending cast.  We could
28609467b48Spatrick       // support this eventually, but don't bother for now.
28709467b48Spatrick       if (!isa<VectorType>(I->getType()) &&
28809467b48Spatrick           DL.getPointerSizeInBits() ==
28909467b48Spatrick               cast<IntegerType>(Op->getType())->getBitWidth())
29009467b48Spatrick         NoopInput = Op;
29109467b48Spatrick     } else if (isa<PtrToIntInst>(I)) {
29209467b48Spatrick       // Look through ptrtoint.
29309467b48Spatrick       // Make sure this isn't a truncating or extending cast.  We could
29409467b48Spatrick       // support this eventually, but don't bother for now.
29509467b48Spatrick       if (!isa<VectorType>(I->getType()) &&
29609467b48Spatrick           DL.getPointerSizeInBits() ==
29709467b48Spatrick               cast<IntegerType>(I->getType())->getBitWidth())
29809467b48Spatrick         NoopInput = Op;
29909467b48Spatrick     } else if (isa<TruncInst>(I) &&
30009467b48Spatrick                TLI.allowTruncateForTailCall(Op->getType(), I->getType())) {
30109467b48Spatrick       DataBits = std::min((uint64_t)DataBits,
30209467b48Spatrick                          I->getType()->getPrimitiveSizeInBits().getFixedSize());
30309467b48Spatrick       NoopInput = Op;
304097a140dSpatrick     } else if (auto *CB = dyn_cast<CallBase>(I)) {
305097a140dSpatrick       const Value *ReturnedOp = CB->getReturnedArgOperand();
30609467b48Spatrick       if (ReturnedOp && isNoopBitcast(ReturnedOp->getType(), I->getType(), TLI))
30709467b48Spatrick         NoopInput = ReturnedOp;
30809467b48Spatrick     } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(V)) {
30909467b48Spatrick       // Value may come from either the aggregate or the scalar
31009467b48Spatrick       ArrayRef<unsigned> InsertLoc = IVI->getIndices();
31109467b48Spatrick       if (ValLoc.size() >= InsertLoc.size() &&
31209467b48Spatrick           std::equal(InsertLoc.begin(), InsertLoc.end(), ValLoc.rbegin())) {
31309467b48Spatrick         // The type being inserted is a nested sub-type of the aggregate; we
31409467b48Spatrick         // have to remove those initial indices to get the location we're
31509467b48Spatrick         // interested in for the operand.
31609467b48Spatrick         ValLoc.resize(ValLoc.size() - InsertLoc.size());
31709467b48Spatrick         NoopInput = IVI->getInsertedValueOperand();
31809467b48Spatrick       } else {
31909467b48Spatrick         // The struct we're inserting into has the value we're interested in, no
32009467b48Spatrick         // change of address.
32109467b48Spatrick         NoopInput = Op;
32209467b48Spatrick       }
32309467b48Spatrick     } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {
32409467b48Spatrick       // The part we're interested in will inevitably be some sub-section of the
32509467b48Spatrick       // previous aggregate. Combine the two paths to obtain the true address of
32609467b48Spatrick       // our element.
32709467b48Spatrick       ArrayRef<unsigned> ExtractLoc = EVI->getIndices();
32809467b48Spatrick       ValLoc.append(ExtractLoc.rbegin(), ExtractLoc.rend());
32909467b48Spatrick       NoopInput = Op;
33009467b48Spatrick     }
33109467b48Spatrick     // Terminate if we couldn't find anything to look through.
33209467b48Spatrick     if (!NoopInput)
33309467b48Spatrick       return V;
33409467b48Spatrick 
33509467b48Spatrick     V = NoopInput;
33609467b48Spatrick   }
33709467b48Spatrick }
33809467b48Spatrick 
33909467b48Spatrick /// Return true if this scalar return value only has bits discarded on its path
34009467b48Spatrick /// from the "tail call" to the "ret". This includes the obvious noop
34109467b48Spatrick /// instructions handled by getNoopInput above as well as free truncations (or
34209467b48Spatrick /// extensions prior to the call).
34309467b48Spatrick static bool slotOnlyDiscardsData(const Value *RetVal, const Value *CallVal,
34409467b48Spatrick                                  SmallVectorImpl<unsigned> &RetIndices,
34509467b48Spatrick                                  SmallVectorImpl<unsigned> &CallIndices,
34609467b48Spatrick                                  bool AllowDifferingSizes,
34709467b48Spatrick                                  const TargetLoweringBase &TLI,
34809467b48Spatrick                                  const DataLayout &DL) {
34909467b48Spatrick 
35009467b48Spatrick   // Trace the sub-value needed by the return value as far back up the graph as
35109467b48Spatrick   // possible, in the hope that it will intersect with the value produced by the
35209467b48Spatrick   // call. In the simple case with no "returned" attribute, the hope is actually
35309467b48Spatrick   // that we end up back at the tail call instruction itself.
35409467b48Spatrick   unsigned BitsRequired = UINT_MAX;
35509467b48Spatrick   RetVal = getNoopInput(RetVal, RetIndices, BitsRequired, TLI, DL);
35609467b48Spatrick 
35709467b48Spatrick   // If this slot in the value returned is undef, it doesn't matter what the
35809467b48Spatrick   // call puts there, it'll be fine.
35909467b48Spatrick   if (isa<UndefValue>(RetVal))
36009467b48Spatrick     return true;
36109467b48Spatrick 
36209467b48Spatrick   // Now do a similar search up through the graph to find where the value
36309467b48Spatrick   // actually returned by the "tail call" comes from. In the simple case without
36409467b48Spatrick   // a "returned" attribute, the search will be blocked immediately and the loop
36509467b48Spatrick   // a Noop.
36609467b48Spatrick   unsigned BitsProvided = UINT_MAX;
36709467b48Spatrick   CallVal = getNoopInput(CallVal, CallIndices, BitsProvided, TLI, DL);
36809467b48Spatrick 
36909467b48Spatrick   // There's no hope if we can't actually trace them to (the same part of!) the
37009467b48Spatrick   // same value.
37109467b48Spatrick   if (CallVal != RetVal || CallIndices != RetIndices)
37209467b48Spatrick     return false;
37309467b48Spatrick 
37409467b48Spatrick   // However, intervening truncates may have made the call non-tail. Make sure
37509467b48Spatrick   // all the bits that are needed by the "ret" have been provided by the "tail
37609467b48Spatrick   // call". FIXME: with sufficiently cunning bit-tracking, we could look through
37709467b48Spatrick   // extensions too.
37809467b48Spatrick   if (BitsProvided < BitsRequired ||
37909467b48Spatrick       (!AllowDifferingSizes && BitsProvided != BitsRequired))
38009467b48Spatrick     return false;
38109467b48Spatrick 
38209467b48Spatrick   return true;
38309467b48Spatrick }
38409467b48Spatrick 
38509467b48Spatrick /// For an aggregate type, determine whether a given index is within bounds or
38609467b48Spatrick /// not.
387097a140dSpatrick static bool indexReallyValid(Type *T, unsigned Idx) {
38809467b48Spatrick   if (ArrayType *AT = dyn_cast<ArrayType>(T))
38909467b48Spatrick     return Idx < AT->getNumElements();
39009467b48Spatrick 
39109467b48Spatrick   return Idx < cast<StructType>(T)->getNumElements();
39209467b48Spatrick }
39309467b48Spatrick 
39409467b48Spatrick /// Move the given iterators to the next leaf type in depth first traversal.
39509467b48Spatrick ///
39609467b48Spatrick /// Performs a depth-first traversal of the type as specified by its arguments,
39709467b48Spatrick /// stopping at the next leaf node (which may be a legitimate scalar type or an
39809467b48Spatrick /// empty struct or array).
39909467b48Spatrick ///
40009467b48Spatrick /// @param SubTypes List of the partial components making up the type from
40109467b48Spatrick /// outermost to innermost non-empty aggregate. The element currently
40209467b48Spatrick /// represented is SubTypes.back()->getTypeAtIndex(Path.back() - 1).
40309467b48Spatrick ///
40409467b48Spatrick /// @param Path Set of extractvalue indices leading from the outermost type
40509467b48Spatrick /// (SubTypes[0]) to the leaf node currently represented.
40609467b48Spatrick ///
40709467b48Spatrick /// @returns true if a new type was found, false otherwise. Calling this
40809467b48Spatrick /// function again on a finished iterator will repeatedly return
40909467b48Spatrick /// false. SubTypes.back()->getTypeAtIndex(Path.back()) is either an empty
41009467b48Spatrick /// aggregate or a non-aggregate
411097a140dSpatrick static bool advanceToNextLeafType(SmallVectorImpl<Type *> &SubTypes,
41209467b48Spatrick                                   SmallVectorImpl<unsigned> &Path) {
41309467b48Spatrick   // First march back up the tree until we can successfully increment one of the
41409467b48Spatrick   // coordinates in Path.
41509467b48Spatrick   while (!Path.empty() && !indexReallyValid(SubTypes.back(), Path.back() + 1)) {
41609467b48Spatrick     Path.pop_back();
41709467b48Spatrick     SubTypes.pop_back();
41809467b48Spatrick   }
41909467b48Spatrick 
42009467b48Spatrick   // If we reached the top, then the iterator is done.
42109467b48Spatrick   if (Path.empty())
42209467b48Spatrick     return false;
42309467b48Spatrick 
42409467b48Spatrick   // We know there's *some* valid leaf now, so march back down the tree picking
42509467b48Spatrick   // out the left-most element at each node.
42609467b48Spatrick   ++Path.back();
427097a140dSpatrick   Type *DeeperType =
428097a140dSpatrick       ExtractValueInst::getIndexedType(SubTypes.back(), Path.back());
42909467b48Spatrick   while (DeeperType->isAggregateType()) {
430097a140dSpatrick     if (!indexReallyValid(DeeperType, 0))
43109467b48Spatrick       return true;
43209467b48Spatrick 
433097a140dSpatrick     SubTypes.push_back(DeeperType);
43409467b48Spatrick     Path.push_back(0);
43509467b48Spatrick 
436097a140dSpatrick     DeeperType = ExtractValueInst::getIndexedType(DeeperType, 0);
43709467b48Spatrick   }
43809467b48Spatrick 
43909467b48Spatrick   return true;
44009467b48Spatrick }
44109467b48Spatrick 
44209467b48Spatrick /// Find the first non-empty, scalar-like type in Next and setup the iterator
44309467b48Spatrick /// components.
44409467b48Spatrick ///
44509467b48Spatrick /// Assuming Next is an aggregate of some kind, this function will traverse the
44609467b48Spatrick /// tree from left to right (i.e. depth-first) looking for the first
44709467b48Spatrick /// non-aggregate type which will play a role in function return.
44809467b48Spatrick ///
44909467b48Spatrick /// For example, if Next was {[0 x i64], {{}, i32, {}}, i32} then we would setup
45009467b48Spatrick /// Path as [1, 1] and SubTypes as [Next, {{}, i32, {}}] to represent the first
45109467b48Spatrick /// i32 in that type.
452097a140dSpatrick static bool firstRealType(Type *Next, SmallVectorImpl<Type *> &SubTypes,
45309467b48Spatrick                           SmallVectorImpl<unsigned> &Path) {
45409467b48Spatrick   // First initialise the iterator components to the first "leaf" node
45509467b48Spatrick   // (i.e. node with no valid sub-type at any index, so {} does count as a leaf
45609467b48Spatrick   // despite nominally being an aggregate).
457097a140dSpatrick   while (Type *FirstInner = ExtractValueInst::getIndexedType(Next, 0)) {
458097a140dSpatrick     SubTypes.push_back(Next);
45909467b48Spatrick     Path.push_back(0);
460097a140dSpatrick     Next = FirstInner;
46109467b48Spatrick   }
46209467b48Spatrick 
46309467b48Spatrick   // If there's no Path now, Next was originally scalar already (or empty
46409467b48Spatrick   // leaf). We're done.
46509467b48Spatrick   if (Path.empty())
46609467b48Spatrick     return true;
46709467b48Spatrick 
46809467b48Spatrick   // Otherwise, use normal iteration to keep looking through the tree until we
46909467b48Spatrick   // find a non-aggregate type.
470097a140dSpatrick   while (ExtractValueInst::getIndexedType(SubTypes.back(), Path.back())
471097a140dSpatrick              ->isAggregateType()) {
47209467b48Spatrick     if (!advanceToNextLeafType(SubTypes, Path))
47309467b48Spatrick       return false;
47409467b48Spatrick   }
47509467b48Spatrick 
47609467b48Spatrick   return true;
47709467b48Spatrick }
47809467b48Spatrick 
47909467b48Spatrick /// Set the iterator data-structures to the next non-empty, non-aggregate
48009467b48Spatrick /// subtype.
481097a140dSpatrick static bool nextRealType(SmallVectorImpl<Type *> &SubTypes,
48209467b48Spatrick                          SmallVectorImpl<unsigned> &Path) {
48309467b48Spatrick   do {
48409467b48Spatrick     if (!advanceToNextLeafType(SubTypes, Path))
48509467b48Spatrick       return false;
48609467b48Spatrick 
48709467b48Spatrick     assert(!Path.empty() && "found a leaf but didn't set the path?");
488097a140dSpatrick   } while (ExtractValueInst::getIndexedType(SubTypes.back(), Path.back())
489097a140dSpatrick                ->isAggregateType());
49009467b48Spatrick 
49109467b48Spatrick   return true;
49209467b48Spatrick }
49309467b48Spatrick 
49409467b48Spatrick 
49509467b48Spatrick /// Test if the given instruction is in a position to be optimized
49609467b48Spatrick /// with a tail-call. This roughly means that it's in a block with
49709467b48Spatrick /// a return and there's nothing that needs to be scheduled
49809467b48Spatrick /// between it and the return.
49909467b48Spatrick ///
50009467b48Spatrick /// This function only tests target-independent requirements.
501097a140dSpatrick bool llvm::isInTailCallPosition(const CallBase &Call, const TargetMachine &TM) {
502097a140dSpatrick   const BasicBlock *ExitBB = Call.getParent();
50309467b48Spatrick   const Instruction *Term = ExitBB->getTerminator();
50409467b48Spatrick   const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
50509467b48Spatrick 
50609467b48Spatrick   // The block must end in a return statement or unreachable.
50709467b48Spatrick   //
50809467b48Spatrick   // FIXME: Decline tailcall if it's not guaranteed and if the block ends in
50909467b48Spatrick   // an unreachable, for now. The way tailcall optimization is currently
51009467b48Spatrick   // implemented means it will add an epilogue followed by a jump. That is
51109467b48Spatrick   // not profitable. Also, if the callee is a special function (e.g.
51209467b48Spatrick   // longjmp on x86), it can end up causing miscompilation that has not
51309467b48Spatrick   // been fully understood.
514*73471bf0Spatrick   if (!Ret && ((!TM.Options.GuaranteedTailCallOpt &&
515*73471bf0Spatrick                 Call.getCallingConv() != CallingConv::Tail &&
516*73471bf0Spatrick                 Call.getCallingConv() != CallingConv::SwiftTail) ||
517*73471bf0Spatrick                !isa<UnreachableInst>(Term)))
51809467b48Spatrick     return false;
51909467b48Spatrick 
52009467b48Spatrick   // If I will have a chain, make sure no other instruction that will have a
52109467b48Spatrick   // chain interposes between I and the return.
522097a140dSpatrick   // Check for all calls including speculatable functions.
52309467b48Spatrick   for (BasicBlock::const_iterator BBI = std::prev(ExitBB->end(), 2);; --BBI) {
524097a140dSpatrick     if (&*BBI == &Call)
52509467b48Spatrick       break;
52609467b48Spatrick     // Debug info intrinsics do not get in the way of tail call optimization.
52709467b48Spatrick     if (isa<DbgInfoIntrinsic>(BBI))
52809467b48Spatrick       continue;
529*73471bf0Spatrick     // Pseudo probe intrinsics do not block tail call optimization either.
530*73471bf0Spatrick     if (isa<PseudoProbeInst>(BBI))
531*73471bf0Spatrick       continue;
532*73471bf0Spatrick     // A lifetime end, assume or noalias.decl intrinsic should not stop tail
533*73471bf0Spatrick     // call optimization.
53409467b48Spatrick     if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(BBI))
53509467b48Spatrick       if (II->getIntrinsicID() == Intrinsic::lifetime_end ||
536*73471bf0Spatrick           II->getIntrinsicID() == Intrinsic::assume ||
537*73471bf0Spatrick           II->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl)
53809467b48Spatrick         continue;
53909467b48Spatrick     if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
54009467b48Spatrick         !isSafeToSpeculativelyExecute(&*BBI))
54109467b48Spatrick       return false;
54209467b48Spatrick   }
54309467b48Spatrick 
54409467b48Spatrick   const Function *F = ExitBB->getParent();
54509467b48Spatrick   return returnTypeIsEligibleForTailCall(
546097a140dSpatrick       F, &Call, Ret, *TM.getSubtargetImpl(*F)->getTargetLowering());
54709467b48Spatrick }
54809467b48Spatrick 
54909467b48Spatrick bool llvm::attributesPermitTailCall(const Function *F, const Instruction *I,
55009467b48Spatrick                                     const ReturnInst *Ret,
55109467b48Spatrick                                     const TargetLoweringBase &TLI,
55209467b48Spatrick                                     bool *AllowDifferingSizes) {
55309467b48Spatrick   // ADS may be null, so don't write to it directly.
55409467b48Spatrick   bool DummyADS;
55509467b48Spatrick   bool &ADS = AllowDifferingSizes ? *AllowDifferingSizes : DummyADS;
55609467b48Spatrick   ADS = true;
55709467b48Spatrick 
55809467b48Spatrick   AttrBuilder CallerAttrs(F->getAttributes(), AttributeList::ReturnIndex);
55909467b48Spatrick   AttrBuilder CalleeAttrs(cast<CallInst>(I)->getAttributes(),
56009467b48Spatrick                           AttributeList::ReturnIndex);
56109467b48Spatrick 
56209467b48Spatrick   // Following attributes are completely benign as far as calling convention
56309467b48Spatrick   // goes, they shouldn't affect whether the call is a tail call.
564*73471bf0Spatrick   for (const auto &Attr : {Attribute::Alignment, Attribute::Dereferenceable,
565*73471bf0Spatrick                            Attribute::DereferenceableOrNull, Attribute::NoAlias,
566*73471bf0Spatrick                            Attribute::NonNull}) {
567*73471bf0Spatrick     CallerAttrs.removeAttribute(Attr);
568*73471bf0Spatrick     CalleeAttrs.removeAttribute(Attr);
569*73471bf0Spatrick   }
57009467b48Spatrick 
57109467b48Spatrick   if (CallerAttrs.contains(Attribute::ZExt)) {
57209467b48Spatrick     if (!CalleeAttrs.contains(Attribute::ZExt))
57309467b48Spatrick       return false;
57409467b48Spatrick 
57509467b48Spatrick     ADS = false;
57609467b48Spatrick     CallerAttrs.removeAttribute(Attribute::ZExt);
57709467b48Spatrick     CalleeAttrs.removeAttribute(Attribute::ZExt);
57809467b48Spatrick   } else if (CallerAttrs.contains(Attribute::SExt)) {
57909467b48Spatrick     if (!CalleeAttrs.contains(Attribute::SExt))
58009467b48Spatrick       return false;
58109467b48Spatrick 
58209467b48Spatrick     ADS = false;
58309467b48Spatrick     CallerAttrs.removeAttribute(Attribute::SExt);
58409467b48Spatrick     CalleeAttrs.removeAttribute(Attribute::SExt);
58509467b48Spatrick   }
58609467b48Spatrick 
58709467b48Spatrick   // Drop sext and zext return attributes if the result is not used.
58809467b48Spatrick   // This enables tail calls for code like:
58909467b48Spatrick   //
59009467b48Spatrick   // define void @caller() {
59109467b48Spatrick   // entry:
59209467b48Spatrick   //   %unused_result = tail call zeroext i1 @callee()
59309467b48Spatrick   //   br label %retlabel
59409467b48Spatrick   // retlabel:
59509467b48Spatrick   //   ret void
59609467b48Spatrick   // }
59709467b48Spatrick   if (I->use_empty()) {
59809467b48Spatrick     CalleeAttrs.removeAttribute(Attribute::SExt);
59909467b48Spatrick     CalleeAttrs.removeAttribute(Attribute::ZExt);
60009467b48Spatrick   }
60109467b48Spatrick 
60209467b48Spatrick   // If they're still different, there's some facet we don't understand
60309467b48Spatrick   // (currently only "inreg", but in future who knows). It may be OK but the
60409467b48Spatrick   // only safe option is to reject the tail call.
60509467b48Spatrick   return CallerAttrs == CalleeAttrs;
60609467b48Spatrick }
60709467b48Spatrick 
60809467b48Spatrick /// Check whether B is a bitcast of a pointer type to another pointer type,
60909467b48Spatrick /// which is equal to A.
61009467b48Spatrick static bool isPointerBitcastEqualTo(const Value *A, const Value *B) {
61109467b48Spatrick   assert(A && B && "Expected non-null inputs!");
61209467b48Spatrick 
61309467b48Spatrick   auto *BitCastIn = dyn_cast<BitCastInst>(B);
61409467b48Spatrick 
61509467b48Spatrick   if (!BitCastIn)
61609467b48Spatrick     return false;
61709467b48Spatrick 
61809467b48Spatrick   if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
61909467b48Spatrick     return false;
62009467b48Spatrick 
62109467b48Spatrick   return A == BitCastIn->getOperand(0);
62209467b48Spatrick }
62309467b48Spatrick 
62409467b48Spatrick bool llvm::returnTypeIsEligibleForTailCall(const Function *F,
62509467b48Spatrick                                            const Instruction *I,
62609467b48Spatrick                                            const ReturnInst *Ret,
62709467b48Spatrick                                            const TargetLoweringBase &TLI) {
62809467b48Spatrick   // If the block ends with a void return or unreachable, it doesn't matter
62909467b48Spatrick   // what the call's return type is.
63009467b48Spatrick   if (!Ret || Ret->getNumOperands() == 0) return true;
63109467b48Spatrick 
63209467b48Spatrick   // If the return value is undef, it doesn't matter what the call's
63309467b48Spatrick   // return type is.
63409467b48Spatrick   if (isa<UndefValue>(Ret->getOperand(0))) return true;
63509467b48Spatrick 
63609467b48Spatrick   // Make sure the attributes attached to each return are compatible.
63709467b48Spatrick   bool AllowDifferingSizes;
63809467b48Spatrick   if (!attributesPermitTailCall(F, I, Ret, TLI, &AllowDifferingSizes))
63909467b48Spatrick     return false;
64009467b48Spatrick 
64109467b48Spatrick   const Value *RetVal = Ret->getOperand(0), *CallVal = I;
64209467b48Spatrick   // Intrinsic like llvm.memcpy has no return value, but the expanded
64309467b48Spatrick   // libcall may or may not have return value. On most platforms, it
64409467b48Spatrick   // will be expanded as memcpy in libc, which returns the first
64509467b48Spatrick   // argument. On other platforms like arm-none-eabi, memcpy may be
64609467b48Spatrick   // expanded as library call without return value, like __aeabi_memcpy.
64709467b48Spatrick   const CallInst *Call = cast<CallInst>(I);
64809467b48Spatrick   if (Function *F = Call->getCalledFunction()) {
64909467b48Spatrick     Intrinsic::ID IID = F->getIntrinsicID();
65009467b48Spatrick     if (((IID == Intrinsic::memcpy &&
65109467b48Spatrick           TLI.getLibcallName(RTLIB::MEMCPY) == StringRef("memcpy")) ||
65209467b48Spatrick          (IID == Intrinsic::memmove &&
65309467b48Spatrick           TLI.getLibcallName(RTLIB::MEMMOVE) == StringRef("memmove")) ||
65409467b48Spatrick          (IID == Intrinsic::memset &&
65509467b48Spatrick           TLI.getLibcallName(RTLIB::MEMSET) == StringRef("memset"))) &&
65609467b48Spatrick         (RetVal == Call->getArgOperand(0) ||
65709467b48Spatrick          isPointerBitcastEqualTo(RetVal, Call->getArgOperand(0))))
65809467b48Spatrick       return true;
65909467b48Spatrick   }
66009467b48Spatrick 
66109467b48Spatrick   SmallVector<unsigned, 4> RetPath, CallPath;
662097a140dSpatrick   SmallVector<Type *, 4> RetSubTypes, CallSubTypes;
66309467b48Spatrick 
66409467b48Spatrick   bool RetEmpty = !firstRealType(RetVal->getType(), RetSubTypes, RetPath);
66509467b48Spatrick   bool CallEmpty = !firstRealType(CallVal->getType(), CallSubTypes, CallPath);
66609467b48Spatrick 
66709467b48Spatrick   // Nothing's actually returned, it doesn't matter what the callee put there
66809467b48Spatrick   // it's a valid tail call.
66909467b48Spatrick   if (RetEmpty)
67009467b48Spatrick     return true;
67109467b48Spatrick 
67209467b48Spatrick   // Iterate pairwise through each of the value types making up the tail call
67309467b48Spatrick   // and the corresponding return. For each one we want to know whether it's
67409467b48Spatrick   // essentially going directly from the tail call to the ret, via operations
67509467b48Spatrick   // that end up not generating any code.
67609467b48Spatrick   //
67709467b48Spatrick   // We allow a certain amount of covariance here. For example it's permitted
67809467b48Spatrick   // for the tail call to define more bits than the ret actually cares about
67909467b48Spatrick   // (e.g. via a truncate).
68009467b48Spatrick   do {
68109467b48Spatrick     if (CallEmpty) {
68209467b48Spatrick       // We've exhausted the values produced by the tail call instruction, the
68309467b48Spatrick       // rest are essentially undef. The type doesn't really matter, but we need
68409467b48Spatrick       // *something*.
685097a140dSpatrick       Type *SlotType =
686097a140dSpatrick           ExtractValueInst::getIndexedType(RetSubTypes.back(), RetPath.back());
68709467b48Spatrick       CallVal = UndefValue::get(SlotType);
68809467b48Spatrick     }
68909467b48Spatrick 
69009467b48Spatrick     // The manipulations performed when we're looking through an insertvalue or
69109467b48Spatrick     // an extractvalue would happen at the front of the RetPath list, so since
69209467b48Spatrick     // we have to copy it anyway it's more efficient to create a reversed copy.
69309467b48Spatrick     SmallVector<unsigned, 4> TmpRetPath(RetPath.rbegin(), RetPath.rend());
69409467b48Spatrick     SmallVector<unsigned, 4> TmpCallPath(CallPath.rbegin(), CallPath.rend());
69509467b48Spatrick 
69609467b48Spatrick     // Finally, we can check whether the value produced by the tail call at this
69709467b48Spatrick     // index is compatible with the value we return.
69809467b48Spatrick     if (!slotOnlyDiscardsData(RetVal, CallVal, TmpRetPath, TmpCallPath,
69909467b48Spatrick                               AllowDifferingSizes, TLI,
70009467b48Spatrick                               F->getParent()->getDataLayout()))
70109467b48Spatrick       return false;
70209467b48Spatrick 
70309467b48Spatrick     CallEmpty  = !nextRealType(CallSubTypes, CallPath);
70409467b48Spatrick   } while(nextRealType(RetSubTypes, RetPath));
70509467b48Spatrick 
70609467b48Spatrick   return true;
70709467b48Spatrick }
70809467b48Spatrick 
70909467b48Spatrick static void collectEHScopeMembers(
71009467b48Spatrick     DenseMap<const MachineBasicBlock *, int> &EHScopeMembership, int EHScope,
71109467b48Spatrick     const MachineBasicBlock *MBB) {
71209467b48Spatrick   SmallVector<const MachineBasicBlock *, 16> Worklist = {MBB};
71309467b48Spatrick   while (!Worklist.empty()) {
71409467b48Spatrick     const MachineBasicBlock *Visiting = Worklist.pop_back_val();
71509467b48Spatrick     // Don't follow blocks which start new scopes.
71609467b48Spatrick     if (Visiting->isEHPad() && Visiting != MBB)
71709467b48Spatrick       continue;
71809467b48Spatrick 
71909467b48Spatrick     // Add this MBB to our scope.
72009467b48Spatrick     auto P = EHScopeMembership.insert(std::make_pair(Visiting, EHScope));
72109467b48Spatrick 
72209467b48Spatrick     // Don't revisit blocks.
72309467b48Spatrick     if (!P.second) {
72409467b48Spatrick       assert(P.first->second == EHScope && "MBB is part of two scopes!");
72509467b48Spatrick       continue;
72609467b48Spatrick     }
72709467b48Spatrick 
72809467b48Spatrick     // Returns are boundaries where scope transfer can occur, don't follow
72909467b48Spatrick     // successors.
73009467b48Spatrick     if (Visiting->isEHScopeReturnBlock())
73109467b48Spatrick       continue;
73209467b48Spatrick 
733*73471bf0Spatrick     append_range(Worklist, Visiting->successors());
73409467b48Spatrick   }
73509467b48Spatrick }
73609467b48Spatrick 
73709467b48Spatrick DenseMap<const MachineBasicBlock *, int>
73809467b48Spatrick llvm::getEHScopeMembership(const MachineFunction &MF) {
73909467b48Spatrick   DenseMap<const MachineBasicBlock *, int> EHScopeMembership;
74009467b48Spatrick 
74109467b48Spatrick   // We don't have anything to do if there aren't any EH pads.
74209467b48Spatrick   if (!MF.hasEHScopes())
74309467b48Spatrick     return EHScopeMembership;
74409467b48Spatrick 
74509467b48Spatrick   int EntryBBNumber = MF.front().getNumber();
74609467b48Spatrick   bool IsSEH = isAsynchronousEHPersonality(
74709467b48Spatrick       classifyEHPersonality(MF.getFunction().getPersonalityFn()));
74809467b48Spatrick 
74909467b48Spatrick   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
75009467b48Spatrick   SmallVector<const MachineBasicBlock *, 16> EHScopeBlocks;
75109467b48Spatrick   SmallVector<const MachineBasicBlock *, 16> UnreachableBlocks;
75209467b48Spatrick   SmallVector<const MachineBasicBlock *, 16> SEHCatchPads;
75309467b48Spatrick   SmallVector<std::pair<const MachineBasicBlock *, int>, 16> CatchRetSuccessors;
75409467b48Spatrick   for (const MachineBasicBlock &MBB : MF) {
75509467b48Spatrick     if (MBB.isEHScopeEntry()) {
75609467b48Spatrick       EHScopeBlocks.push_back(&MBB);
75709467b48Spatrick     } else if (IsSEH && MBB.isEHPad()) {
75809467b48Spatrick       SEHCatchPads.push_back(&MBB);
75909467b48Spatrick     } else if (MBB.pred_empty()) {
76009467b48Spatrick       UnreachableBlocks.push_back(&MBB);
76109467b48Spatrick     }
76209467b48Spatrick 
76309467b48Spatrick     MachineBasicBlock::const_iterator MBBI = MBB.getFirstTerminator();
76409467b48Spatrick 
76509467b48Spatrick     // CatchPads are not scopes for SEH so do not consider CatchRet to
76609467b48Spatrick     // transfer control to another scope.
76709467b48Spatrick     if (MBBI == MBB.end() || MBBI->getOpcode() != TII->getCatchReturnOpcode())
76809467b48Spatrick       continue;
76909467b48Spatrick 
77009467b48Spatrick     // FIXME: SEH CatchPads are not necessarily in the parent function:
77109467b48Spatrick     // they could be inside a finally block.
77209467b48Spatrick     const MachineBasicBlock *Successor = MBBI->getOperand(0).getMBB();
77309467b48Spatrick     const MachineBasicBlock *SuccessorColor = MBBI->getOperand(1).getMBB();
77409467b48Spatrick     CatchRetSuccessors.push_back(
77509467b48Spatrick         {Successor, IsSEH ? EntryBBNumber : SuccessorColor->getNumber()});
77609467b48Spatrick   }
77709467b48Spatrick 
77809467b48Spatrick   // We don't have anything to do if there aren't any EH pads.
77909467b48Spatrick   if (EHScopeBlocks.empty())
78009467b48Spatrick     return EHScopeMembership;
78109467b48Spatrick 
78209467b48Spatrick   // Identify all the basic blocks reachable from the function entry.
78309467b48Spatrick   collectEHScopeMembers(EHScopeMembership, EntryBBNumber, &MF.front());
78409467b48Spatrick   // All blocks not part of a scope are in the parent function.
78509467b48Spatrick   for (const MachineBasicBlock *MBB : UnreachableBlocks)
78609467b48Spatrick     collectEHScopeMembers(EHScopeMembership, EntryBBNumber, MBB);
78709467b48Spatrick   // Next, identify all the blocks inside the scopes.
78809467b48Spatrick   for (const MachineBasicBlock *MBB : EHScopeBlocks)
78909467b48Spatrick     collectEHScopeMembers(EHScopeMembership, MBB->getNumber(), MBB);
79009467b48Spatrick   // SEH CatchPads aren't really scopes, handle them separately.
79109467b48Spatrick   for (const MachineBasicBlock *MBB : SEHCatchPads)
79209467b48Spatrick     collectEHScopeMembers(EHScopeMembership, EntryBBNumber, MBB);
79309467b48Spatrick   // Finally, identify all the targets of a catchret.
79409467b48Spatrick   for (std::pair<const MachineBasicBlock *, int> CatchRetPair :
79509467b48Spatrick        CatchRetSuccessors)
79609467b48Spatrick     collectEHScopeMembers(EHScopeMembership, CatchRetPair.second,
79709467b48Spatrick                           CatchRetPair.first);
79809467b48Spatrick   return EHScopeMembership;
79909467b48Spatrick }
800