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/Module.h"
2509467b48Spatrick #include "llvm/Support/ErrorHandling.h"
26097a140dSpatrick #include "llvm/Target/TargetMachine.h"
2709467b48Spatrick
2809467b48Spatrick using namespace llvm;
2909467b48Spatrick
3009467b48Spatrick /// Compute the linearized index of a member in a nested aggregate/struct/array
3109467b48Spatrick /// by recursing and accumulating CurIndex as long as there are indices in the
3209467b48Spatrick /// index list.
ComputeLinearIndex(Type * Ty,const unsigned * Indices,const unsigned * IndicesEnd,unsigned CurIndex)3309467b48Spatrick unsigned llvm::ComputeLinearIndex(Type *Ty,
3409467b48Spatrick const unsigned *Indices,
3509467b48Spatrick const unsigned *IndicesEnd,
3609467b48Spatrick unsigned CurIndex) {
3709467b48Spatrick // Base case: We're done.
3809467b48Spatrick if (Indices && Indices == IndicesEnd)
3909467b48Spatrick return CurIndex;
4009467b48Spatrick
4109467b48Spatrick // Given a struct type, recursively traverse the elements.
4209467b48Spatrick if (StructType *STy = dyn_cast<StructType>(Ty)) {
4373471bf0Spatrick for (auto I : llvm::enumerate(STy->elements())) {
4473471bf0Spatrick Type *ET = I.value();
4573471bf0Spatrick if (Indices && *Indices == I.index())
4673471bf0Spatrick return ComputeLinearIndex(ET, Indices + 1, IndicesEnd, CurIndex);
4773471bf0Spatrick CurIndex = ComputeLinearIndex(ET, nullptr, nullptr, CurIndex);
4809467b48Spatrick }
4909467b48Spatrick assert(!Indices && "Unexpected out of bound");
5009467b48Spatrick return CurIndex;
5109467b48Spatrick }
5209467b48Spatrick // Given an array type, recursively traverse the elements.
5309467b48Spatrick else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
5409467b48Spatrick Type *EltTy = ATy->getElementType();
5509467b48Spatrick unsigned NumElts = ATy->getNumElements();
5609467b48Spatrick // Compute the Linear offset when jumping one element of the array
5709467b48Spatrick unsigned EltLinearOffset = ComputeLinearIndex(EltTy, nullptr, nullptr, 0);
5809467b48Spatrick if (Indices) {
5909467b48Spatrick assert(*Indices < NumElts && "Unexpected out of bound");
6009467b48Spatrick // If the indice is inside the array, compute the index to the requested
6109467b48Spatrick // elt and recurse inside the element with the end of the indices list
6209467b48Spatrick CurIndex += EltLinearOffset* *Indices;
6309467b48Spatrick return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex);
6409467b48Spatrick }
6509467b48Spatrick CurIndex += EltLinearOffset*NumElts;
6609467b48Spatrick return CurIndex;
6709467b48Spatrick }
6809467b48Spatrick // We haven't found the type we're looking for, so keep searching.
6909467b48Spatrick return CurIndex + 1;
7009467b48Spatrick }
7109467b48Spatrick
7209467b48Spatrick /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
7309467b48Spatrick /// EVTs that represent all the individual underlying
7409467b48Spatrick /// non-aggregate types that comprise it.
7509467b48Spatrick ///
7609467b48Spatrick /// If Offsets is non-null, it points to a vector to be filled in
7709467b48Spatrick /// with the in-memory offsets of each of the individual values.
7809467b48Spatrick ///
ComputeValueVTs(const TargetLowering & TLI,const DataLayout & DL,Type * Ty,SmallVectorImpl<EVT> & ValueVTs,SmallVectorImpl<EVT> * MemVTs,SmallVectorImpl<uint64_t> * Offsets,uint64_t StartingOffset)7909467b48Spatrick void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
8009467b48Spatrick Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
8109467b48Spatrick SmallVectorImpl<EVT> *MemVTs,
8209467b48Spatrick SmallVectorImpl<uint64_t> *Offsets,
8309467b48Spatrick uint64_t StartingOffset) {
8409467b48Spatrick // Given a struct type, recursively traverse the elements.
8509467b48Spatrick if (StructType *STy = dyn_cast<StructType>(Ty)) {
8673471bf0Spatrick // If the Offsets aren't needed, don't query the struct layout. This allows
8773471bf0Spatrick // us to support structs with scalable vectors for operations that don't
8873471bf0Spatrick // need offsets.
8973471bf0Spatrick const StructLayout *SL = Offsets ? DL.getStructLayout(STy) : nullptr;
9009467b48Spatrick for (StructType::element_iterator EB = STy->element_begin(),
9109467b48Spatrick EI = EB,
9209467b48Spatrick EE = STy->element_end();
9373471bf0Spatrick EI != EE; ++EI) {
9473471bf0Spatrick // Don't compute the element offset if we didn't get a StructLayout above.
9573471bf0Spatrick uint64_t EltOffset = SL ? SL->getElementOffset(EI - EB) : 0;
9609467b48Spatrick ComputeValueVTs(TLI, DL, *EI, ValueVTs, MemVTs, Offsets,
9773471bf0Spatrick StartingOffset + EltOffset);
9873471bf0Spatrick }
9909467b48Spatrick return;
10009467b48Spatrick }
10109467b48Spatrick // Given an array type, recursively traverse the elements.
10209467b48Spatrick if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
10309467b48Spatrick Type *EltTy = ATy->getElementType();
10473471bf0Spatrick uint64_t EltSize = DL.getTypeAllocSize(EltTy).getFixedValue();
10509467b48Spatrick for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
10609467b48Spatrick ComputeValueVTs(TLI, DL, EltTy, ValueVTs, MemVTs, Offsets,
10709467b48Spatrick StartingOffset + i * EltSize);
10809467b48Spatrick return;
10909467b48Spatrick }
11009467b48Spatrick // Interpret void as zero return values.
11109467b48Spatrick if (Ty->isVoidTy())
11209467b48Spatrick return;
11309467b48Spatrick // Base case: we can get an EVT for this LLVM IR type.
11409467b48Spatrick ValueVTs.push_back(TLI.getValueType(DL, Ty));
11509467b48Spatrick if (MemVTs)
11609467b48Spatrick MemVTs->push_back(TLI.getMemValueType(DL, Ty));
11709467b48Spatrick if (Offsets)
11809467b48Spatrick Offsets->push_back(StartingOffset);
11909467b48Spatrick }
12009467b48Spatrick
ComputeValueVTs(const TargetLowering & TLI,const DataLayout & DL,Type * Ty,SmallVectorImpl<EVT> & ValueVTs,SmallVectorImpl<uint64_t> * Offsets,uint64_t StartingOffset)12109467b48Spatrick void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
12209467b48Spatrick Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
12309467b48Spatrick SmallVectorImpl<uint64_t> *Offsets,
12409467b48Spatrick uint64_t StartingOffset) {
12509467b48Spatrick return ComputeValueVTs(TLI, DL, Ty, ValueVTs, /*MemVTs=*/nullptr, Offsets,
12609467b48Spatrick StartingOffset);
12709467b48Spatrick }
12809467b48Spatrick
computeValueLLTs(const DataLayout & DL,Type & Ty,SmallVectorImpl<LLT> & ValueTys,SmallVectorImpl<uint64_t> * Offsets,uint64_t StartingOffset)12909467b48Spatrick void llvm::computeValueLLTs(const DataLayout &DL, Type &Ty,
13009467b48Spatrick SmallVectorImpl<LLT> &ValueTys,
13109467b48Spatrick SmallVectorImpl<uint64_t> *Offsets,
13209467b48Spatrick uint64_t StartingOffset) {
13309467b48Spatrick // Given a struct type, recursively traverse the elements.
13409467b48Spatrick if (StructType *STy = dyn_cast<StructType>(&Ty)) {
13573471bf0Spatrick // If the Offsets aren't needed, don't query the struct layout. This allows
13673471bf0Spatrick // us to support structs with scalable vectors for operations that don't
13773471bf0Spatrick // need offsets.
13873471bf0Spatrick const StructLayout *SL = Offsets ? DL.getStructLayout(STy) : nullptr;
13973471bf0Spatrick for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I) {
14073471bf0Spatrick uint64_t EltOffset = SL ? SL->getElementOffset(I) : 0;
14109467b48Spatrick computeValueLLTs(DL, *STy->getElementType(I), ValueTys, Offsets,
14273471bf0Spatrick StartingOffset + EltOffset);
14373471bf0Spatrick }
14409467b48Spatrick return;
14509467b48Spatrick }
14609467b48Spatrick // Given an array type, recursively traverse the elements.
14709467b48Spatrick if (ArrayType *ATy = dyn_cast<ArrayType>(&Ty)) {
14809467b48Spatrick Type *EltTy = ATy->getElementType();
14973471bf0Spatrick uint64_t EltSize = DL.getTypeAllocSize(EltTy).getFixedValue();
15009467b48Spatrick for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
15109467b48Spatrick computeValueLLTs(DL, *EltTy, ValueTys, Offsets,
15209467b48Spatrick StartingOffset + i * EltSize);
15309467b48Spatrick return;
15409467b48Spatrick }
15509467b48Spatrick // Interpret void as zero return values.
15609467b48Spatrick if (Ty.isVoidTy())
15709467b48Spatrick return;
15809467b48Spatrick // Base case: we can get an LLT for this LLVM IR type.
15909467b48Spatrick ValueTys.push_back(getLLTForType(Ty, DL));
16009467b48Spatrick if (Offsets != nullptr)
16109467b48Spatrick Offsets->push_back(StartingOffset * 8);
16209467b48Spatrick }
16309467b48Spatrick
16409467b48Spatrick /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
ExtractTypeInfo(Value * V)16509467b48Spatrick GlobalValue *llvm::ExtractTypeInfo(Value *V) {
16609467b48Spatrick V = V->stripPointerCasts();
16709467b48Spatrick GlobalValue *GV = dyn_cast<GlobalValue>(V);
16809467b48Spatrick GlobalVariable *Var = dyn_cast<GlobalVariable>(V);
16909467b48Spatrick
17009467b48Spatrick if (Var && Var->getName() == "llvm.eh.catch.all.value") {
17109467b48Spatrick assert(Var->hasInitializer() &&
17209467b48Spatrick "The EH catch-all value must have an initializer");
17309467b48Spatrick Value *Init = Var->getInitializer();
17409467b48Spatrick GV = dyn_cast<GlobalValue>(Init);
17509467b48Spatrick if (!GV) V = cast<ConstantPointerNull>(Init);
17609467b48Spatrick }
17709467b48Spatrick
17809467b48Spatrick assert((GV || isa<ConstantPointerNull>(V)) &&
17909467b48Spatrick "TypeInfo must be a global variable or NULL");
18009467b48Spatrick return GV;
18109467b48Spatrick }
18209467b48Spatrick
18309467b48Spatrick /// getFCmpCondCode - Return the ISD condition code corresponding to
18409467b48Spatrick /// the given LLVM IR floating-point condition code. This includes
18509467b48Spatrick /// consideration of global floating-point math flags.
18609467b48Spatrick ///
getFCmpCondCode(FCmpInst::Predicate Pred)18709467b48Spatrick ISD::CondCode llvm::getFCmpCondCode(FCmpInst::Predicate Pred) {
18809467b48Spatrick switch (Pred) {
18909467b48Spatrick case FCmpInst::FCMP_FALSE: return ISD::SETFALSE;
19009467b48Spatrick case FCmpInst::FCMP_OEQ: return ISD::SETOEQ;
19109467b48Spatrick case FCmpInst::FCMP_OGT: return ISD::SETOGT;
19209467b48Spatrick case FCmpInst::FCMP_OGE: return ISD::SETOGE;
19309467b48Spatrick case FCmpInst::FCMP_OLT: return ISD::SETOLT;
19409467b48Spatrick case FCmpInst::FCMP_OLE: return ISD::SETOLE;
19509467b48Spatrick case FCmpInst::FCMP_ONE: return ISD::SETONE;
19609467b48Spatrick case FCmpInst::FCMP_ORD: return ISD::SETO;
19709467b48Spatrick case FCmpInst::FCMP_UNO: return ISD::SETUO;
19809467b48Spatrick case FCmpInst::FCMP_UEQ: return ISD::SETUEQ;
19909467b48Spatrick case FCmpInst::FCMP_UGT: return ISD::SETUGT;
20009467b48Spatrick case FCmpInst::FCMP_UGE: return ISD::SETUGE;
20109467b48Spatrick case FCmpInst::FCMP_ULT: return ISD::SETULT;
20209467b48Spatrick case FCmpInst::FCMP_ULE: return ISD::SETULE;
20309467b48Spatrick case FCmpInst::FCMP_UNE: return ISD::SETUNE;
20409467b48Spatrick case FCmpInst::FCMP_TRUE: return ISD::SETTRUE;
20509467b48Spatrick default: llvm_unreachable("Invalid FCmp predicate opcode!");
20609467b48Spatrick }
20709467b48Spatrick }
20809467b48Spatrick
getFCmpCodeWithoutNaN(ISD::CondCode CC)20909467b48Spatrick ISD::CondCode llvm::getFCmpCodeWithoutNaN(ISD::CondCode CC) {
21009467b48Spatrick switch (CC) {
21109467b48Spatrick case ISD::SETOEQ: case ISD::SETUEQ: return ISD::SETEQ;
21209467b48Spatrick case ISD::SETONE: case ISD::SETUNE: return ISD::SETNE;
21309467b48Spatrick case ISD::SETOLT: case ISD::SETULT: return ISD::SETLT;
21409467b48Spatrick case ISD::SETOLE: case ISD::SETULE: return ISD::SETLE;
21509467b48Spatrick case ISD::SETOGT: case ISD::SETUGT: return ISD::SETGT;
21609467b48Spatrick case ISD::SETOGE: case ISD::SETUGE: return ISD::SETGE;
21709467b48Spatrick default: return CC;
21809467b48Spatrick }
21909467b48Spatrick }
22009467b48Spatrick
getICmpCondCode(ICmpInst::Predicate Pred)22109467b48Spatrick ISD::CondCode llvm::getICmpCondCode(ICmpInst::Predicate Pred) {
22209467b48Spatrick switch (Pred) {
22309467b48Spatrick case ICmpInst::ICMP_EQ: return ISD::SETEQ;
22409467b48Spatrick case ICmpInst::ICMP_NE: return ISD::SETNE;
22509467b48Spatrick case ICmpInst::ICMP_SLE: return ISD::SETLE;
22609467b48Spatrick case ICmpInst::ICMP_ULE: return ISD::SETULE;
22709467b48Spatrick case ICmpInst::ICMP_SGE: return ISD::SETGE;
22809467b48Spatrick case ICmpInst::ICMP_UGE: return ISD::SETUGE;
22909467b48Spatrick case ICmpInst::ICMP_SLT: return ISD::SETLT;
23009467b48Spatrick case ICmpInst::ICMP_ULT: return ISD::SETULT;
23109467b48Spatrick case ICmpInst::ICMP_SGT: return ISD::SETGT;
23209467b48Spatrick case ICmpInst::ICMP_UGT: return ISD::SETUGT;
23309467b48Spatrick default:
23409467b48Spatrick llvm_unreachable("Invalid ICmp predicate opcode!");
23509467b48Spatrick }
23609467b48Spatrick }
23709467b48Spatrick
getICmpCondCode(ISD::CondCode Pred)238*d415bd75Srobert ICmpInst::Predicate llvm::getICmpCondCode(ISD::CondCode Pred) {
239*d415bd75Srobert switch (Pred) {
240*d415bd75Srobert case ISD::SETEQ:
241*d415bd75Srobert return ICmpInst::ICMP_EQ;
242*d415bd75Srobert case ISD::SETNE:
243*d415bd75Srobert return ICmpInst::ICMP_NE;
244*d415bd75Srobert case ISD::SETLE:
245*d415bd75Srobert return ICmpInst::ICMP_SLE;
246*d415bd75Srobert case ISD::SETULE:
247*d415bd75Srobert return ICmpInst::ICMP_ULE;
248*d415bd75Srobert case ISD::SETGE:
249*d415bd75Srobert return ICmpInst::ICMP_SGE;
250*d415bd75Srobert case ISD::SETUGE:
251*d415bd75Srobert return ICmpInst::ICMP_UGE;
252*d415bd75Srobert case ISD::SETLT:
253*d415bd75Srobert return ICmpInst::ICMP_SLT;
254*d415bd75Srobert case ISD::SETULT:
255*d415bd75Srobert return ICmpInst::ICMP_ULT;
256*d415bd75Srobert case ISD::SETGT:
257*d415bd75Srobert return ICmpInst::ICMP_SGT;
258*d415bd75Srobert case ISD::SETUGT:
259*d415bd75Srobert return ICmpInst::ICMP_UGT;
260*d415bd75Srobert default:
261*d415bd75Srobert llvm_unreachable("Invalid ISD integer condition code!");
262*d415bd75Srobert }
263*d415bd75Srobert }
264*d415bd75Srobert
isNoopBitcast(Type * T1,Type * T2,const TargetLoweringBase & TLI)26509467b48Spatrick static bool isNoopBitcast(Type *T1, Type *T2,
26609467b48Spatrick const TargetLoweringBase& TLI) {
26709467b48Spatrick return T1 == T2 || (T1->isPointerTy() && T2->isPointerTy()) ||
26809467b48Spatrick (isa<VectorType>(T1) && isa<VectorType>(T2) &&
26909467b48Spatrick TLI.isTypeLegal(EVT::getEVT(T1)) && TLI.isTypeLegal(EVT::getEVT(T2)));
27009467b48Spatrick }
27109467b48Spatrick
27209467b48Spatrick /// Look through operations that will be free to find the earliest source of
27309467b48Spatrick /// this value.
27409467b48Spatrick ///
27509467b48Spatrick /// @param ValLoc If V has aggregate type, we will be interested in a particular
27609467b48Spatrick /// scalar component. This records its address; the reverse of this list gives a
27709467b48Spatrick /// sequence of indices appropriate for an extractvalue to locate the important
27809467b48Spatrick /// value. This value is updated during the function and on exit will indicate
27909467b48Spatrick /// similar information for the Value returned.
28009467b48Spatrick ///
28109467b48Spatrick /// @param DataBits If this function looks through truncate instructions, this
28209467b48Spatrick /// will record the smallest size attained.
getNoopInput(const Value * V,SmallVectorImpl<unsigned> & ValLoc,unsigned & DataBits,const TargetLoweringBase & TLI,const DataLayout & DL)28309467b48Spatrick static const Value *getNoopInput(const Value *V,
28409467b48Spatrick SmallVectorImpl<unsigned> &ValLoc,
28509467b48Spatrick unsigned &DataBits,
28609467b48Spatrick const TargetLoweringBase &TLI,
28709467b48Spatrick const DataLayout &DL) {
28809467b48Spatrick while (true) {
28909467b48Spatrick // Try to look through V1; if V1 is not an instruction, it can't be looked
29009467b48Spatrick // through.
29109467b48Spatrick const Instruction *I = dyn_cast<Instruction>(V);
29209467b48Spatrick if (!I || I->getNumOperands() == 0) return V;
29309467b48Spatrick const Value *NoopInput = nullptr;
29409467b48Spatrick
29509467b48Spatrick Value *Op = I->getOperand(0);
29609467b48Spatrick if (isa<BitCastInst>(I)) {
29709467b48Spatrick // Look through truly no-op bitcasts.
29809467b48Spatrick if (isNoopBitcast(Op->getType(), I->getType(), TLI))
29909467b48Spatrick NoopInput = Op;
30009467b48Spatrick } else if (isa<GetElementPtrInst>(I)) {
30109467b48Spatrick // Look through getelementptr
30209467b48Spatrick if (cast<GetElementPtrInst>(I)->hasAllZeroIndices())
30309467b48Spatrick NoopInput = Op;
30409467b48Spatrick } else if (isa<IntToPtrInst>(I)) {
30509467b48Spatrick // Look through inttoptr.
30609467b48Spatrick // Make sure this isn't a truncating or extending cast. We could
30709467b48Spatrick // support this eventually, but don't bother for now.
30809467b48Spatrick if (!isa<VectorType>(I->getType()) &&
30909467b48Spatrick DL.getPointerSizeInBits() ==
31009467b48Spatrick cast<IntegerType>(Op->getType())->getBitWidth())
31109467b48Spatrick NoopInput = Op;
31209467b48Spatrick } else if (isa<PtrToIntInst>(I)) {
31309467b48Spatrick // Look through ptrtoint.
31409467b48Spatrick // Make sure this isn't a truncating or extending cast. We could
31509467b48Spatrick // support this eventually, but don't bother for now.
31609467b48Spatrick if (!isa<VectorType>(I->getType()) &&
31709467b48Spatrick DL.getPointerSizeInBits() ==
31809467b48Spatrick cast<IntegerType>(I->getType())->getBitWidth())
31909467b48Spatrick NoopInput = Op;
32009467b48Spatrick } else if (isa<TruncInst>(I) &&
32109467b48Spatrick TLI.allowTruncateForTailCall(Op->getType(), I->getType())) {
322*d415bd75Srobert DataBits =
323*d415bd75Srobert std::min((uint64_t)DataBits,
324*d415bd75Srobert I->getType()->getPrimitiveSizeInBits().getFixedValue());
32509467b48Spatrick NoopInput = Op;
326097a140dSpatrick } else if (auto *CB = dyn_cast<CallBase>(I)) {
327097a140dSpatrick const Value *ReturnedOp = CB->getReturnedArgOperand();
32809467b48Spatrick if (ReturnedOp && isNoopBitcast(ReturnedOp->getType(), I->getType(), TLI))
32909467b48Spatrick NoopInput = ReturnedOp;
33009467b48Spatrick } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(V)) {
33109467b48Spatrick // Value may come from either the aggregate or the scalar
33209467b48Spatrick ArrayRef<unsigned> InsertLoc = IVI->getIndices();
33309467b48Spatrick if (ValLoc.size() >= InsertLoc.size() &&
33409467b48Spatrick std::equal(InsertLoc.begin(), InsertLoc.end(), ValLoc.rbegin())) {
33509467b48Spatrick // The type being inserted is a nested sub-type of the aggregate; we
33609467b48Spatrick // have to remove those initial indices to get the location we're
33709467b48Spatrick // interested in for the operand.
33809467b48Spatrick ValLoc.resize(ValLoc.size() - InsertLoc.size());
33909467b48Spatrick NoopInput = IVI->getInsertedValueOperand();
34009467b48Spatrick } else {
34109467b48Spatrick // The struct we're inserting into has the value we're interested in, no
34209467b48Spatrick // change of address.
34309467b48Spatrick NoopInput = Op;
34409467b48Spatrick }
34509467b48Spatrick } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {
34609467b48Spatrick // The part we're interested in will inevitably be some sub-section of the
34709467b48Spatrick // previous aggregate. Combine the two paths to obtain the true address of
34809467b48Spatrick // our element.
34909467b48Spatrick ArrayRef<unsigned> ExtractLoc = EVI->getIndices();
35009467b48Spatrick ValLoc.append(ExtractLoc.rbegin(), ExtractLoc.rend());
35109467b48Spatrick NoopInput = Op;
35209467b48Spatrick }
35309467b48Spatrick // Terminate if we couldn't find anything to look through.
35409467b48Spatrick if (!NoopInput)
35509467b48Spatrick return V;
35609467b48Spatrick
35709467b48Spatrick V = NoopInput;
35809467b48Spatrick }
35909467b48Spatrick }
36009467b48Spatrick
36109467b48Spatrick /// Return true if this scalar return value only has bits discarded on its path
36209467b48Spatrick /// from the "tail call" to the "ret". This includes the obvious noop
36309467b48Spatrick /// instructions handled by getNoopInput above as well as free truncations (or
36409467b48Spatrick /// extensions prior to the call).
slotOnlyDiscardsData(const Value * RetVal,const Value * CallVal,SmallVectorImpl<unsigned> & RetIndices,SmallVectorImpl<unsigned> & CallIndices,bool AllowDifferingSizes,const TargetLoweringBase & TLI,const DataLayout & DL)36509467b48Spatrick static bool slotOnlyDiscardsData(const Value *RetVal, const Value *CallVal,
36609467b48Spatrick SmallVectorImpl<unsigned> &RetIndices,
36709467b48Spatrick SmallVectorImpl<unsigned> &CallIndices,
36809467b48Spatrick bool AllowDifferingSizes,
36909467b48Spatrick const TargetLoweringBase &TLI,
37009467b48Spatrick const DataLayout &DL) {
37109467b48Spatrick
37209467b48Spatrick // Trace the sub-value needed by the return value as far back up the graph as
37309467b48Spatrick // possible, in the hope that it will intersect with the value produced by the
37409467b48Spatrick // call. In the simple case with no "returned" attribute, the hope is actually
37509467b48Spatrick // that we end up back at the tail call instruction itself.
37609467b48Spatrick unsigned BitsRequired = UINT_MAX;
37709467b48Spatrick RetVal = getNoopInput(RetVal, RetIndices, BitsRequired, TLI, DL);
37809467b48Spatrick
37909467b48Spatrick // If this slot in the value returned is undef, it doesn't matter what the
38009467b48Spatrick // call puts there, it'll be fine.
38109467b48Spatrick if (isa<UndefValue>(RetVal))
38209467b48Spatrick return true;
38309467b48Spatrick
38409467b48Spatrick // Now do a similar search up through the graph to find where the value
38509467b48Spatrick // actually returned by the "tail call" comes from. In the simple case without
38609467b48Spatrick // a "returned" attribute, the search will be blocked immediately and the loop
38709467b48Spatrick // a Noop.
38809467b48Spatrick unsigned BitsProvided = UINT_MAX;
38909467b48Spatrick CallVal = getNoopInput(CallVal, CallIndices, BitsProvided, TLI, DL);
39009467b48Spatrick
39109467b48Spatrick // There's no hope if we can't actually trace them to (the same part of!) the
39209467b48Spatrick // same value.
39309467b48Spatrick if (CallVal != RetVal || CallIndices != RetIndices)
39409467b48Spatrick return false;
39509467b48Spatrick
39609467b48Spatrick // However, intervening truncates may have made the call non-tail. Make sure
39709467b48Spatrick // all the bits that are needed by the "ret" have been provided by the "tail
39809467b48Spatrick // call". FIXME: with sufficiently cunning bit-tracking, we could look through
39909467b48Spatrick // extensions too.
40009467b48Spatrick if (BitsProvided < BitsRequired ||
40109467b48Spatrick (!AllowDifferingSizes && BitsProvided != BitsRequired))
40209467b48Spatrick return false;
40309467b48Spatrick
40409467b48Spatrick return true;
40509467b48Spatrick }
40609467b48Spatrick
40709467b48Spatrick /// For an aggregate type, determine whether a given index is within bounds or
40809467b48Spatrick /// not.
indexReallyValid(Type * T,unsigned Idx)409097a140dSpatrick static bool indexReallyValid(Type *T, unsigned Idx) {
41009467b48Spatrick if (ArrayType *AT = dyn_cast<ArrayType>(T))
41109467b48Spatrick return Idx < AT->getNumElements();
41209467b48Spatrick
41309467b48Spatrick return Idx < cast<StructType>(T)->getNumElements();
41409467b48Spatrick }
41509467b48Spatrick
41609467b48Spatrick /// Move the given iterators to the next leaf type in depth first traversal.
41709467b48Spatrick ///
41809467b48Spatrick /// Performs a depth-first traversal of the type as specified by its arguments,
41909467b48Spatrick /// stopping at the next leaf node (which may be a legitimate scalar type or an
42009467b48Spatrick /// empty struct or array).
42109467b48Spatrick ///
42209467b48Spatrick /// @param SubTypes List of the partial components making up the type from
42309467b48Spatrick /// outermost to innermost non-empty aggregate. The element currently
42409467b48Spatrick /// represented is SubTypes.back()->getTypeAtIndex(Path.back() - 1).
42509467b48Spatrick ///
42609467b48Spatrick /// @param Path Set of extractvalue indices leading from the outermost type
42709467b48Spatrick /// (SubTypes[0]) to the leaf node currently represented.
42809467b48Spatrick ///
42909467b48Spatrick /// @returns true if a new type was found, false otherwise. Calling this
43009467b48Spatrick /// function again on a finished iterator will repeatedly return
43109467b48Spatrick /// false. SubTypes.back()->getTypeAtIndex(Path.back()) is either an empty
43209467b48Spatrick /// aggregate or a non-aggregate
advanceToNextLeafType(SmallVectorImpl<Type * > & SubTypes,SmallVectorImpl<unsigned> & Path)433097a140dSpatrick static bool advanceToNextLeafType(SmallVectorImpl<Type *> &SubTypes,
43409467b48Spatrick SmallVectorImpl<unsigned> &Path) {
43509467b48Spatrick // First march back up the tree until we can successfully increment one of the
43609467b48Spatrick // coordinates in Path.
43709467b48Spatrick while (!Path.empty() && !indexReallyValid(SubTypes.back(), Path.back() + 1)) {
43809467b48Spatrick Path.pop_back();
43909467b48Spatrick SubTypes.pop_back();
44009467b48Spatrick }
44109467b48Spatrick
44209467b48Spatrick // If we reached the top, then the iterator is done.
44309467b48Spatrick if (Path.empty())
44409467b48Spatrick return false;
44509467b48Spatrick
44609467b48Spatrick // We know there's *some* valid leaf now, so march back down the tree picking
44709467b48Spatrick // out the left-most element at each node.
44809467b48Spatrick ++Path.back();
449097a140dSpatrick Type *DeeperType =
450097a140dSpatrick ExtractValueInst::getIndexedType(SubTypes.back(), Path.back());
45109467b48Spatrick while (DeeperType->isAggregateType()) {
452097a140dSpatrick if (!indexReallyValid(DeeperType, 0))
45309467b48Spatrick return true;
45409467b48Spatrick
455097a140dSpatrick SubTypes.push_back(DeeperType);
45609467b48Spatrick Path.push_back(0);
45709467b48Spatrick
458097a140dSpatrick DeeperType = ExtractValueInst::getIndexedType(DeeperType, 0);
45909467b48Spatrick }
46009467b48Spatrick
46109467b48Spatrick return true;
46209467b48Spatrick }
46309467b48Spatrick
46409467b48Spatrick /// Find the first non-empty, scalar-like type in Next and setup the iterator
46509467b48Spatrick /// components.
46609467b48Spatrick ///
46709467b48Spatrick /// Assuming Next is an aggregate of some kind, this function will traverse the
46809467b48Spatrick /// tree from left to right (i.e. depth-first) looking for the first
46909467b48Spatrick /// non-aggregate type which will play a role in function return.
47009467b48Spatrick ///
47109467b48Spatrick /// For example, if Next was {[0 x i64], {{}, i32, {}}, i32} then we would setup
47209467b48Spatrick /// Path as [1, 1] and SubTypes as [Next, {{}, i32, {}}] to represent the first
47309467b48Spatrick /// i32 in that type.
firstRealType(Type * Next,SmallVectorImpl<Type * > & SubTypes,SmallVectorImpl<unsigned> & Path)474097a140dSpatrick static bool firstRealType(Type *Next, SmallVectorImpl<Type *> &SubTypes,
47509467b48Spatrick SmallVectorImpl<unsigned> &Path) {
47609467b48Spatrick // First initialise the iterator components to the first "leaf" node
47709467b48Spatrick // (i.e. node with no valid sub-type at any index, so {} does count as a leaf
47809467b48Spatrick // despite nominally being an aggregate).
479097a140dSpatrick while (Type *FirstInner = ExtractValueInst::getIndexedType(Next, 0)) {
480097a140dSpatrick SubTypes.push_back(Next);
48109467b48Spatrick Path.push_back(0);
482097a140dSpatrick Next = FirstInner;
48309467b48Spatrick }
48409467b48Spatrick
48509467b48Spatrick // If there's no Path now, Next was originally scalar already (or empty
48609467b48Spatrick // leaf). We're done.
48709467b48Spatrick if (Path.empty())
48809467b48Spatrick return true;
48909467b48Spatrick
49009467b48Spatrick // Otherwise, use normal iteration to keep looking through the tree until we
49109467b48Spatrick // find a non-aggregate type.
492097a140dSpatrick while (ExtractValueInst::getIndexedType(SubTypes.back(), Path.back())
493097a140dSpatrick ->isAggregateType()) {
49409467b48Spatrick if (!advanceToNextLeafType(SubTypes, Path))
49509467b48Spatrick return false;
49609467b48Spatrick }
49709467b48Spatrick
49809467b48Spatrick return true;
49909467b48Spatrick }
50009467b48Spatrick
50109467b48Spatrick /// Set the iterator data-structures to the next non-empty, non-aggregate
50209467b48Spatrick /// subtype.
nextRealType(SmallVectorImpl<Type * > & SubTypes,SmallVectorImpl<unsigned> & Path)503097a140dSpatrick static bool nextRealType(SmallVectorImpl<Type *> &SubTypes,
50409467b48Spatrick SmallVectorImpl<unsigned> &Path) {
50509467b48Spatrick do {
50609467b48Spatrick if (!advanceToNextLeafType(SubTypes, Path))
50709467b48Spatrick return false;
50809467b48Spatrick
50909467b48Spatrick assert(!Path.empty() && "found a leaf but didn't set the path?");
510097a140dSpatrick } while (ExtractValueInst::getIndexedType(SubTypes.back(), Path.back())
511097a140dSpatrick ->isAggregateType());
51209467b48Spatrick
51309467b48Spatrick return true;
51409467b48Spatrick }
51509467b48Spatrick
51609467b48Spatrick
51709467b48Spatrick /// Test if the given instruction is in a position to be optimized
51809467b48Spatrick /// with a tail-call. This roughly means that it's in a block with
51909467b48Spatrick /// a return and there's nothing that needs to be scheduled
52009467b48Spatrick /// between it and the return.
52109467b48Spatrick ///
52209467b48Spatrick /// This function only tests target-independent requirements.
isInTailCallPosition(const CallBase & Call,const TargetMachine & TM)523097a140dSpatrick bool llvm::isInTailCallPosition(const CallBase &Call, const TargetMachine &TM) {
524097a140dSpatrick const BasicBlock *ExitBB = Call.getParent();
52509467b48Spatrick const Instruction *Term = ExitBB->getTerminator();
52609467b48Spatrick const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
52709467b48Spatrick
52809467b48Spatrick // The block must end in a return statement or unreachable.
52909467b48Spatrick //
53009467b48Spatrick // FIXME: Decline tailcall if it's not guaranteed and if the block ends in
53109467b48Spatrick // an unreachable, for now. The way tailcall optimization is currently
53209467b48Spatrick // implemented means it will add an epilogue followed by a jump. That is
53309467b48Spatrick // not profitable. Also, if the callee is a special function (e.g.
53409467b48Spatrick // longjmp on x86), it can end up causing miscompilation that has not
53509467b48Spatrick // been fully understood.
53673471bf0Spatrick if (!Ret && ((!TM.Options.GuaranteedTailCallOpt &&
53773471bf0Spatrick Call.getCallingConv() != CallingConv::Tail &&
53873471bf0Spatrick Call.getCallingConv() != CallingConv::SwiftTail) ||
53973471bf0Spatrick !isa<UnreachableInst>(Term)))
54009467b48Spatrick return false;
54109467b48Spatrick
54209467b48Spatrick // If I will have a chain, make sure no other instruction that will have a
54309467b48Spatrick // chain interposes between I and the return.
544097a140dSpatrick // Check for all calls including speculatable functions.
54509467b48Spatrick for (BasicBlock::const_iterator BBI = std::prev(ExitBB->end(), 2);; --BBI) {
546097a140dSpatrick if (&*BBI == &Call)
54709467b48Spatrick break;
54809467b48Spatrick // Debug info intrinsics do not get in the way of tail call optimization.
54973471bf0Spatrick // Pseudo probe intrinsics do not block tail call optimization either.
550*d415bd75Srobert if (BBI->isDebugOrPseudoInst())
55173471bf0Spatrick continue;
55273471bf0Spatrick // A lifetime end, assume or noalias.decl intrinsic should not stop tail
55373471bf0Spatrick // call optimization.
55409467b48Spatrick if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(BBI))
55509467b48Spatrick if (II->getIntrinsicID() == Intrinsic::lifetime_end ||
55673471bf0Spatrick II->getIntrinsicID() == Intrinsic::assume ||
55773471bf0Spatrick II->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl)
55809467b48Spatrick continue;
55909467b48Spatrick if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
56009467b48Spatrick !isSafeToSpeculativelyExecute(&*BBI))
56109467b48Spatrick return false;
56209467b48Spatrick }
56309467b48Spatrick
56409467b48Spatrick const Function *F = ExitBB->getParent();
56509467b48Spatrick return returnTypeIsEligibleForTailCall(
566097a140dSpatrick F, &Call, Ret, *TM.getSubtargetImpl(*F)->getTargetLowering());
56709467b48Spatrick }
56809467b48Spatrick
attributesPermitTailCall(const Function * F,const Instruction * I,const ReturnInst * Ret,const TargetLoweringBase & TLI,bool * AllowDifferingSizes)56909467b48Spatrick bool llvm::attributesPermitTailCall(const Function *F, const Instruction *I,
57009467b48Spatrick const ReturnInst *Ret,
57109467b48Spatrick const TargetLoweringBase &TLI,
57209467b48Spatrick bool *AllowDifferingSizes) {
57309467b48Spatrick // ADS may be null, so don't write to it directly.
57409467b48Spatrick bool DummyADS;
57509467b48Spatrick bool &ADS = AllowDifferingSizes ? *AllowDifferingSizes : DummyADS;
57609467b48Spatrick ADS = true;
57709467b48Spatrick
578*d415bd75Srobert AttrBuilder CallerAttrs(F->getContext(), F->getAttributes().getRetAttrs());
579*d415bd75Srobert AttrBuilder CalleeAttrs(F->getContext(),
580*d415bd75Srobert cast<CallInst>(I)->getAttributes().getRetAttrs());
58109467b48Spatrick
58209467b48Spatrick // Following attributes are completely benign as far as calling convention
58309467b48Spatrick // goes, they shouldn't affect whether the call is a tail call.
58473471bf0Spatrick for (const auto &Attr : {Attribute::Alignment, Attribute::Dereferenceable,
58573471bf0Spatrick Attribute::DereferenceableOrNull, Attribute::NoAlias,
586*d415bd75Srobert Attribute::NonNull, Attribute::NoUndef}) {
58773471bf0Spatrick CallerAttrs.removeAttribute(Attr);
58873471bf0Spatrick CalleeAttrs.removeAttribute(Attr);
58973471bf0Spatrick }
59009467b48Spatrick
59109467b48Spatrick if (CallerAttrs.contains(Attribute::ZExt)) {
59209467b48Spatrick if (!CalleeAttrs.contains(Attribute::ZExt))
59309467b48Spatrick return false;
59409467b48Spatrick
59509467b48Spatrick ADS = false;
59609467b48Spatrick CallerAttrs.removeAttribute(Attribute::ZExt);
59709467b48Spatrick CalleeAttrs.removeAttribute(Attribute::ZExt);
59809467b48Spatrick } else if (CallerAttrs.contains(Attribute::SExt)) {
59909467b48Spatrick if (!CalleeAttrs.contains(Attribute::SExt))
60009467b48Spatrick return false;
60109467b48Spatrick
60209467b48Spatrick ADS = false;
60309467b48Spatrick CallerAttrs.removeAttribute(Attribute::SExt);
60409467b48Spatrick CalleeAttrs.removeAttribute(Attribute::SExt);
60509467b48Spatrick }
60609467b48Spatrick
60709467b48Spatrick // Drop sext and zext return attributes if the result is not used.
60809467b48Spatrick // This enables tail calls for code like:
60909467b48Spatrick //
61009467b48Spatrick // define void @caller() {
61109467b48Spatrick // entry:
61209467b48Spatrick // %unused_result = tail call zeroext i1 @callee()
61309467b48Spatrick // br label %retlabel
61409467b48Spatrick // retlabel:
61509467b48Spatrick // ret void
61609467b48Spatrick // }
61709467b48Spatrick if (I->use_empty()) {
61809467b48Spatrick CalleeAttrs.removeAttribute(Attribute::SExt);
61909467b48Spatrick CalleeAttrs.removeAttribute(Attribute::ZExt);
62009467b48Spatrick }
62109467b48Spatrick
62209467b48Spatrick // If they're still different, there's some facet we don't understand
62309467b48Spatrick // (currently only "inreg", but in future who knows). It may be OK but the
62409467b48Spatrick // only safe option is to reject the tail call.
62509467b48Spatrick return CallerAttrs == CalleeAttrs;
62609467b48Spatrick }
62709467b48Spatrick
62809467b48Spatrick /// Check whether B is a bitcast of a pointer type to another pointer type,
62909467b48Spatrick /// which is equal to A.
isPointerBitcastEqualTo(const Value * A,const Value * B)63009467b48Spatrick static bool isPointerBitcastEqualTo(const Value *A, const Value *B) {
63109467b48Spatrick assert(A && B && "Expected non-null inputs!");
63209467b48Spatrick
63309467b48Spatrick auto *BitCastIn = dyn_cast<BitCastInst>(B);
63409467b48Spatrick
63509467b48Spatrick if (!BitCastIn)
63609467b48Spatrick return false;
63709467b48Spatrick
63809467b48Spatrick if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
63909467b48Spatrick return false;
64009467b48Spatrick
64109467b48Spatrick return A == BitCastIn->getOperand(0);
64209467b48Spatrick }
64309467b48Spatrick
returnTypeIsEligibleForTailCall(const Function * F,const Instruction * I,const ReturnInst * Ret,const TargetLoweringBase & TLI)64409467b48Spatrick bool llvm::returnTypeIsEligibleForTailCall(const Function *F,
64509467b48Spatrick const Instruction *I,
64609467b48Spatrick const ReturnInst *Ret,
64709467b48Spatrick const TargetLoweringBase &TLI) {
64809467b48Spatrick // If the block ends with a void return or unreachable, it doesn't matter
64909467b48Spatrick // what the call's return type is.
65009467b48Spatrick if (!Ret || Ret->getNumOperands() == 0) return true;
65109467b48Spatrick
65209467b48Spatrick // If the return value is undef, it doesn't matter what the call's
65309467b48Spatrick // return type is.
65409467b48Spatrick if (isa<UndefValue>(Ret->getOperand(0))) return true;
65509467b48Spatrick
65609467b48Spatrick // Make sure the attributes attached to each return are compatible.
65709467b48Spatrick bool AllowDifferingSizes;
65809467b48Spatrick if (!attributesPermitTailCall(F, I, Ret, TLI, &AllowDifferingSizes))
65909467b48Spatrick return false;
66009467b48Spatrick
66109467b48Spatrick const Value *RetVal = Ret->getOperand(0), *CallVal = I;
66209467b48Spatrick // Intrinsic like llvm.memcpy has no return value, but the expanded
66309467b48Spatrick // libcall may or may not have return value. On most platforms, it
66409467b48Spatrick // will be expanded as memcpy in libc, which returns the first
66509467b48Spatrick // argument. On other platforms like arm-none-eabi, memcpy may be
66609467b48Spatrick // expanded as library call without return value, like __aeabi_memcpy.
66709467b48Spatrick const CallInst *Call = cast<CallInst>(I);
66809467b48Spatrick if (Function *F = Call->getCalledFunction()) {
66909467b48Spatrick Intrinsic::ID IID = F->getIntrinsicID();
67009467b48Spatrick if (((IID == Intrinsic::memcpy &&
67109467b48Spatrick TLI.getLibcallName(RTLIB::MEMCPY) == StringRef("memcpy")) ||
67209467b48Spatrick (IID == Intrinsic::memmove &&
67309467b48Spatrick TLI.getLibcallName(RTLIB::MEMMOVE) == StringRef("memmove")) ||
67409467b48Spatrick (IID == Intrinsic::memset &&
67509467b48Spatrick TLI.getLibcallName(RTLIB::MEMSET) == StringRef("memset"))) &&
67609467b48Spatrick (RetVal == Call->getArgOperand(0) ||
67709467b48Spatrick isPointerBitcastEqualTo(RetVal, Call->getArgOperand(0))))
67809467b48Spatrick return true;
67909467b48Spatrick }
68009467b48Spatrick
68109467b48Spatrick SmallVector<unsigned, 4> RetPath, CallPath;
682097a140dSpatrick SmallVector<Type *, 4> RetSubTypes, CallSubTypes;
68309467b48Spatrick
68409467b48Spatrick bool RetEmpty = !firstRealType(RetVal->getType(), RetSubTypes, RetPath);
68509467b48Spatrick bool CallEmpty = !firstRealType(CallVal->getType(), CallSubTypes, CallPath);
68609467b48Spatrick
68709467b48Spatrick // Nothing's actually returned, it doesn't matter what the callee put there
68809467b48Spatrick // it's a valid tail call.
68909467b48Spatrick if (RetEmpty)
69009467b48Spatrick return true;
69109467b48Spatrick
69209467b48Spatrick // Iterate pairwise through each of the value types making up the tail call
69309467b48Spatrick // and the corresponding return. For each one we want to know whether it's
69409467b48Spatrick // essentially going directly from the tail call to the ret, via operations
69509467b48Spatrick // that end up not generating any code.
69609467b48Spatrick //
69709467b48Spatrick // We allow a certain amount of covariance here. For example it's permitted
69809467b48Spatrick // for the tail call to define more bits than the ret actually cares about
69909467b48Spatrick // (e.g. via a truncate).
70009467b48Spatrick do {
70109467b48Spatrick if (CallEmpty) {
70209467b48Spatrick // We've exhausted the values produced by the tail call instruction, the
70309467b48Spatrick // rest are essentially undef. The type doesn't really matter, but we need
70409467b48Spatrick // *something*.
705097a140dSpatrick Type *SlotType =
706097a140dSpatrick ExtractValueInst::getIndexedType(RetSubTypes.back(), RetPath.back());
70709467b48Spatrick CallVal = UndefValue::get(SlotType);
70809467b48Spatrick }
70909467b48Spatrick
71009467b48Spatrick // The manipulations performed when we're looking through an insertvalue or
71109467b48Spatrick // an extractvalue would happen at the front of the RetPath list, so since
71209467b48Spatrick // we have to copy it anyway it's more efficient to create a reversed copy.
713*d415bd75Srobert SmallVector<unsigned, 4> TmpRetPath(llvm::reverse(RetPath));
714*d415bd75Srobert SmallVector<unsigned, 4> TmpCallPath(llvm::reverse(CallPath));
71509467b48Spatrick
71609467b48Spatrick // Finally, we can check whether the value produced by the tail call at this
71709467b48Spatrick // index is compatible with the value we return.
71809467b48Spatrick if (!slotOnlyDiscardsData(RetVal, CallVal, TmpRetPath, TmpCallPath,
71909467b48Spatrick AllowDifferingSizes, TLI,
72009467b48Spatrick F->getParent()->getDataLayout()))
72109467b48Spatrick return false;
72209467b48Spatrick
72309467b48Spatrick CallEmpty = !nextRealType(CallSubTypes, CallPath);
72409467b48Spatrick } while(nextRealType(RetSubTypes, RetPath));
72509467b48Spatrick
72609467b48Spatrick return true;
72709467b48Spatrick }
72809467b48Spatrick
collectEHScopeMembers(DenseMap<const MachineBasicBlock *,int> & EHScopeMembership,int EHScope,const MachineBasicBlock * MBB)72909467b48Spatrick static void collectEHScopeMembers(
73009467b48Spatrick DenseMap<const MachineBasicBlock *, int> &EHScopeMembership, int EHScope,
73109467b48Spatrick const MachineBasicBlock *MBB) {
73209467b48Spatrick SmallVector<const MachineBasicBlock *, 16> Worklist = {MBB};
73309467b48Spatrick while (!Worklist.empty()) {
73409467b48Spatrick const MachineBasicBlock *Visiting = Worklist.pop_back_val();
73509467b48Spatrick // Don't follow blocks which start new scopes.
73609467b48Spatrick if (Visiting->isEHPad() && Visiting != MBB)
73709467b48Spatrick continue;
73809467b48Spatrick
73909467b48Spatrick // Add this MBB to our scope.
74009467b48Spatrick auto P = EHScopeMembership.insert(std::make_pair(Visiting, EHScope));
74109467b48Spatrick
74209467b48Spatrick // Don't revisit blocks.
74309467b48Spatrick if (!P.second) {
74409467b48Spatrick assert(P.first->second == EHScope && "MBB is part of two scopes!");
74509467b48Spatrick continue;
74609467b48Spatrick }
74709467b48Spatrick
74809467b48Spatrick // Returns are boundaries where scope transfer can occur, don't follow
74909467b48Spatrick // successors.
75009467b48Spatrick if (Visiting->isEHScopeReturnBlock())
75109467b48Spatrick continue;
75209467b48Spatrick
75373471bf0Spatrick append_range(Worklist, Visiting->successors());
75409467b48Spatrick }
75509467b48Spatrick }
75609467b48Spatrick
75709467b48Spatrick DenseMap<const MachineBasicBlock *, int>
getEHScopeMembership(const MachineFunction & MF)75809467b48Spatrick llvm::getEHScopeMembership(const MachineFunction &MF) {
75909467b48Spatrick DenseMap<const MachineBasicBlock *, int> EHScopeMembership;
76009467b48Spatrick
76109467b48Spatrick // We don't have anything to do if there aren't any EH pads.
76209467b48Spatrick if (!MF.hasEHScopes())
76309467b48Spatrick return EHScopeMembership;
76409467b48Spatrick
76509467b48Spatrick int EntryBBNumber = MF.front().getNumber();
76609467b48Spatrick bool IsSEH = isAsynchronousEHPersonality(
76709467b48Spatrick classifyEHPersonality(MF.getFunction().getPersonalityFn()));
76809467b48Spatrick
76909467b48Spatrick const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
77009467b48Spatrick SmallVector<const MachineBasicBlock *, 16> EHScopeBlocks;
77109467b48Spatrick SmallVector<const MachineBasicBlock *, 16> UnreachableBlocks;
77209467b48Spatrick SmallVector<const MachineBasicBlock *, 16> SEHCatchPads;
77309467b48Spatrick SmallVector<std::pair<const MachineBasicBlock *, int>, 16> CatchRetSuccessors;
77409467b48Spatrick for (const MachineBasicBlock &MBB : MF) {
77509467b48Spatrick if (MBB.isEHScopeEntry()) {
77609467b48Spatrick EHScopeBlocks.push_back(&MBB);
77709467b48Spatrick } else if (IsSEH && MBB.isEHPad()) {
77809467b48Spatrick SEHCatchPads.push_back(&MBB);
77909467b48Spatrick } else if (MBB.pred_empty()) {
78009467b48Spatrick UnreachableBlocks.push_back(&MBB);
78109467b48Spatrick }
78209467b48Spatrick
78309467b48Spatrick MachineBasicBlock::const_iterator MBBI = MBB.getFirstTerminator();
78409467b48Spatrick
78509467b48Spatrick // CatchPads are not scopes for SEH so do not consider CatchRet to
78609467b48Spatrick // transfer control to another scope.
78709467b48Spatrick if (MBBI == MBB.end() || MBBI->getOpcode() != TII->getCatchReturnOpcode())
78809467b48Spatrick continue;
78909467b48Spatrick
79009467b48Spatrick // FIXME: SEH CatchPads are not necessarily in the parent function:
79109467b48Spatrick // they could be inside a finally block.
79209467b48Spatrick const MachineBasicBlock *Successor = MBBI->getOperand(0).getMBB();
79309467b48Spatrick const MachineBasicBlock *SuccessorColor = MBBI->getOperand(1).getMBB();
79409467b48Spatrick CatchRetSuccessors.push_back(
79509467b48Spatrick {Successor, IsSEH ? EntryBBNumber : SuccessorColor->getNumber()});
79609467b48Spatrick }
79709467b48Spatrick
79809467b48Spatrick // We don't have anything to do if there aren't any EH pads.
79909467b48Spatrick if (EHScopeBlocks.empty())
80009467b48Spatrick return EHScopeMembership;
80109467b48Spatrick
80209467b48Spatrick // Identify all the basic blocks reachable from the function entry.
80309467b48Spatrick collectEHScopeMembers(EHScopeMembership, EntryBBNumber, &MF.front());
80409467b48Spatrick // All blocks not part of a scope are in the parent function.
80509467b48Spatrick for (const MachineBasicBlock *MBB : UnreachableBlocks)
80609467b48Spatrick collectEHScopeMembers(EHScopeMembership, EntryBBNumber, MBB);
80709467b48Spatrick // Next, identify all the blocks inside the scopes.
80809467b48Spatrick for (const MachineBasicBlock *MBB : EHScopeBlocks)
80909467b48Spatrick collectEHScopeMembers(EHScopeMembership, MBB->getNumber(), MBB);
81009467b48Spatrick // SEH CatchPads aren't really scopes, handle them separately.
81109467b48Spatrick for (const MachineBasicBlock *MBB : SEHCatchPads)
81209467b48Spatrick collectEHScopeMembers(EHScopeMembership, EntryBBNumber, MBB);
81309467b48Spatrick // Finally, identify all the targets of a catchret.
81409467b48Spatrick for (std::pair<const MachineBasicBlock *, int> CatchRetPair :
81509467b48Spatrick CatchRetSuccessors)
81609467b48Spatrick collectEHScopeMembers(EHScopeMembership, CatchRetPair.second,
81709467b48Spatrick CatchRetPair.first);
81809467b48Spatrick return EHScopeMembership;
81909467b48Spatrick }
820