150f02cb2SNick Lewycky //===-- Analysis.cpp - CodeGen LLVM IR Analysis Utilities -----------------===//
2450aa64fSDan Gohman //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6450aa64fSDan Gohman //
7450aa64fSDan Gohman //===----------------------------------------------------------------------===//
8450aa64fSDan Gohman //
9db5028bdSEric Christopher // This file defines several CodeGen-specific LLVM IR analysis utilities.
10450aa64fSDan Gohman //
11450aa64fSDan Gohman //===----------------------------------------------------------------------===//
12450aa64fSDan Gohman
1309fc276dSEric Christopher #include "llvm/CodeGen/Analysis.h"
14dda00098SEric Christopher #include "llvm/Analysis/ValueTracking.h"
15ed0881b2SChandler Carruth #include "llvm/CodeGen/MachineFunction.h"
163f833edcSDavid Blaikie #include "llvm/CodeGen/TargetInstrInfo.h"
17b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetLowering.h"
18b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetSubtargetInfo.h"
199fb823bbSChandler Carruth #include "llvm/IR/DataLayout.h"
209fb823bbSChandler Carruth #include "llvm/IR/DerivedTypes.h"
219fb823bbSChandler Carruth #include "llvm/IR/Function.h"
229fb823bbSChandler Carruth #include "llvm/IR/Instructions.h"
239fb823bbSChandler Carruth #include "llvm/IR/IntrinsicInst.h"
249fb823bbSChandler Carruth #include "llvm/IR/Module.h"
25450aa64fSDan Gohman #include "llvm/Support/ErrorHandling.h"
26fe0006c8SSimon Pilgrim #include "llvm/Target/TargetMachine.h"
27d913448bSEric Christopher
28450aa64fSDan Gohman using namespace llvm;
29450aa64fSDan Gohman
308923cc54SMehdi Amini /// Compute the linearized index of a member in a nested aggregate/struct/array
318923cc54SMehdi Amini /// by recursing and accumulating CurIndex as long as there are indices in the
328923cc54SMehdi Amini /// index list.
ComputeLinearIndex(Type * Ty,const unsigned * Indices,const unsigned * IndicesEnd,unsigned CurIndex)33229907cdSChris Lattner unsigned llvm::ComputeLinearIndex(Type *Ty,
34450aa64fSDan Gohman const unsigned *Indices,
35450aa64fSDan Gohman const unsigned *IndicesEnd,
36450aa64fSDan Gohman unsigned CurIndex) {
37450aa64fSDan Gohman // Base case: We're done.
38450aa64fSDan Gohman if (Indices && Indices == IndicesEnd)
39450aa64fSDan Gohman return CurIndex;
40450aa64fSDan Gohman
41450aa64fSDan Gohman // Given a struct type, recursively traverse the elements.
42229907cdSChris Lattner if (StructType *STy = dyn_cast<StructType>(Ty)) {
43ffba9e59SKazu Hirata for (auto I : llvm::enumerate(STy->elements())) {
44ffba9e59SKazu Hirata Type *ET = I.value();
45ffba9e59SKazu Hirata if (Indices && *Indices == I.index())
46ffba9e59SKazu Hirata return ComputeLinearIndex(ET, Indices + 1, IndicesEnd, CurIndex);
47ffba9e59SKazu Hirata CurIndex = ComputeLinearIndex(ET, nullptr, nullptr, CurIndex);
48450aa64fSDan Gohman }
497b068f6bSMehdi Amini assert(!Indices && "Unexpected out of bound");
50450aa64fSDan Gohman return CurIndex;
51450aa64fSDan Gohman }
52450aa64fSDan Gohman // Given an array type, recursively traverse the elements.
53229907cdSChris Lattner else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
54229907cdSChris Lattner Type *EltTy = ATy->getElementType();
558923cc54SMehdi Amini unsigned NumElts = ATy->getNumElements();
568923cc54SMehdi Amini // Compute the Linear offset when jumping one element of the array
578923cc54SMehdi Amini unsigned EltLinearOffset = ComputeLinearIndex(EltTy, nullptr, nullptr, 0);
587b068f6bSMehdi Amini if (Indices) {
597b068f6bSMehdi Amini assert(*Indices < NumElts && "Unexpected out of bound");
608923cc54SMehdi Amini // If the indice is inside the array, compute the index to the requested
618923cc54SMehdi Amini // elt and recurse inside the element with the end of the indices list
628923cc54SMehdi Amini CurIndex += EltLinearOffset* *Indices;
63aadc5596SDan Gohman return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex);
64450aa64fSDan Gohman }
658923cc54SMehdi Amini CurIndex += EltLinearOffset*NumElts;
66450aa64fSDan Gohman return CurIndex;
67450aa64fSDan Gohman }
68450aa64fSDan Gohman // We haven't found the type we're looking for, so keep searching.
69450aa64fSDan Gohman return CurIndex + 1;
70450aa64fSDan Gohman }
71450aa64fSDan Gohman
72450aa64fSDan Gohman /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
73450aa64fSDan Gohman /// EVTs that represent all the individual underlying
74450aa64fSDan Gohman /// non-aggregate types that comprise it.
75450aa64fSDan Gohman ///
76450aa64fSDan Gohman /// If Offsets is non-null, it points to a vector to be filled in
77450aa64fSDan Gohman /// with the in-memory offsets of each of the individual values.
78450aa64fSDan Gohman ///
ComputeValueVTs(const TargetLowering & TLI,const DataLayout & DL,Type * Ty,SmallVectorImpl<EVT> & ValueVTs,SmallVectorImpl<EVT> * MemVTs,SmallVectorImpl<uint64_t> * Offsets,uint64_t StartingOffset)7956228dabSMehdi Amini void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
8056228dabSMehdi Amini Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
81ee2474dfSTim Northover SmallVectorImpl<EVT> *MemVTs,
82450aa64fSDan Gohman SmallVectorImpl<uint64_t> *Offsets,
83450aa64fSDan Gohman uint64_t StartingOffset) {
84450aa64fSDan Gohman // Given a struct type, recursively traverse the elements.
85229907cdSChris Lattner if (StructType *STy = dyn_cast<StructType>(Ty)) {
86cfec6cd5SCraig Topper // If the Offsets aren't needed, don't query the struct layout. This allows
87cfec6cd5SCraig Topper // us to support structs with scalable vectors for operations that don't
88cfec6cd5SCraig Topper // need offsets.
89cfec6cd5SCraig Topper const StructLayout *SL = Offsets ? DL.getStructLayout(STy) : nullptr;
90450aa64fSDan Gohman for (StructType::element_iterator EB = STy->element_begin(),
91450aa64fSDan Gohman EI = EB,
92450aa64fSDan Gohman EE = STy->element_end();
93cfec6cd5SCraig Topper EI != EE; ++EI) {
94cfec6cd5SCraig Topper // Don't compute the element offset if we didn't get a StructLayout above.
95cfec6cd5SCraig Topper uint64_t EltOffset = SL ? SL->getElementOffset(EI - EB) : 0;
96ee2474dfSTim Northover ComputeValueVTs(TLI, DL, *EI, ValueVTs, MemVTs, Offsets,
97cfec6cd5SCraig Topper StartingOffset + EltOffset);
98cfec6cd5SCraig Topper }
99450aa64fSDan Gohman return;
100450aa64fSDan Gohman }
101450aa64fSDan Gohman // Given an array type, recursively traverse the elements.
102229907cdSChris Lattner if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
103229907cdSChris Lattner Type *EltTy = ATy->getElementType();
104cfec6cd5SCraig Topper uint64_t EltSize = DL.getTypeAllocSize(EltTy).getFixedValue();
105450aa64fSDan Gohman for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
106ee2474dfSTim Northover ComputeValueVTs(TLI, DL, EltTy, ValueVTs, MemVTs, Offsets,
107450aa64fSDan Gohman StartingOffset + i * EltSize);
108450aa64fSDan Gohman return;
109450aa64fSDan Gohman }
110450aa64fSDan Gohman // Interpret void as zero return values.
111450aa64fSDan Gohman if (Ty->isVoidTy())
112450aa64fSDan Gohman return;
113450aa64fSDan Gohman // Base case: we can get an EVT for this LLVM IR type.
11444ede33aSMehdi Amini ValueVTs.push_back(TLI.getValueType(DL, Ty));
115ee2474dfSTim Northover if (MemVTs)
116ee2474dfSTim Northover MemVTs->push_back(TLI.getMemValueType(DL, Ty));
117450aa64fSDan Gohman if (Offsets)
118450aa64fSDan Gohman Offsets->push_back(StartingOffset);
119450aa64fSDan Gohman }
120450aa64fSDan Gohman
ComputeValueVTs(const TargetLowering & TLI,const DataLayout & DL,Type * Ty,SmallVectorImpl<EVT> & ValueVTs,SmallVectorImpl<uint64_t> * Offsets,uint64_t StartingOffset)121ee2474dfSTim Northover void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
122ee2474dfSTim Northover Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
123ee2474dfSTim Northover SmallVectorImpl<uint64_t> *Offsets,
124ee2474dfSTim Northover uint64_t StartingOffset) {
125ee2474dfSTim Northover return ComputeValueVTs(TLI, DL, Ty, ValueVTs, /*MemVTs=*/nullptr, Offsets,
126ee2474dfSTim Northover StartingOffset);
127ee2474dfSTim Northover }
128ee2474dfSTim Northover
computeValueLLTs(const DataLayout & DL,Type & Ty,SmallVectorImpl<LLT> & ValueTys,SmallVectorImpl<uint64_t> * Offsets,uint64_t StartingOffset)1292064e45cSMatt Arsenault void llvm::computeValueLLTs(const DataLayout &DL, Type &Ty,
1302064e45cSMatt Arsenault SmallVectorImpl<LLT> &ValueTys,
1312064e45cSMatt Arsenault SmallVectorImpl<uint64_t> *Offsets,
1322064e45cSMatt Arsenault uint64_t StartingOffset) {
1332064e45cSMatt Arsenault // Given a struct type, recursively traverse the elements.
1342064e45cSMatt Arsenault if (StructType *STy = dyn_cast<StructType>(&Ty)) {
135cfec6cd5SCraig Topper // If the Offsets aren't needed, don't query the struct layout. This allows
136cfec6cd5SCraig Topper // us to support structs with scalable vectors for operations that don't
137cfec6cd5SCraig Topper // need offsets.
138cfec6cd5SCraig Topper const StructLayout *SL = Offsets ? DL.getStructLayout(STy) : nullptr;
139cfec6cd5SCraig Topper for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I) {
140cfec6cd5SCraig Topper uint64_t EltOffset = SL ? SL->getElementOffset(I) : 0;
1412064e45cSMatt Arsenault computeValueLLTs(DL, *STy->getElementType(I), ValueTys, Offsets,
142cfec6cd5SCraig Topper StartingOffset + EltOffset);
143cfec6cd5SCraig Topper }
1442064e45cSMatt Arsenault return;
1452064e45cSMatt Arsenault }
1462064e45cSMatt Arsenault // Given an array type, recursively traverse the elements.
1472064e45cSMatt Arsenault if (ArrayType *ATy = dyn_cast<ArrayType>(&Ty)) {
1482064e45cSMatt Arsenault Type *EltTy = ATy->getElementType();
149cfec6cd5SCraig Topper uint64_t EltSize = DL.getTypeAllocSize(EltTy).getFixedValue();
1502064e45cSMatt Arsenault for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
1512064e45cSMatt Arsenault computeValueLLTs(DL, *EltTy, ValueTys, Offsets,
1522064e45cSMatt Arsenault StartingOffset + i * EltSize);
1532064e45cSMatt Arsenault return;
1542064e45cSMatt Arsenault }
1552064e45cSMatt Arsenault // Interpret void as zero return values.
1562064e45cSMatt Arsenault if (Ty.isVoidTy())
1572064e45cSMatt Arsenault return;
1582064e45cSMatt Arsenault // Base case: we can get an LLT for this LLVM IR type.
1592064e45cSMatt Arsenault ValueTys.push_back(getLLTForType(Ty, DL));
1602064e45cSMatt Arsenault if (Offsets != nullptr)
1612064e45cSMatt Arsenault Offsets->push_back(StartingOffset * 8);
1622064e45cSMatt Arsenault }
1632064e45cSMatt Arsenault
164450aa64fSDan Gohman /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
ExtractTypeInfo(Value * V)165283bc2edSReid Kleckner GlobalValue *llvm::ExtractTypeInfo(Value *V) {
1662452d703SPeter Collingbourne V = V->stripPointerCasts();
167283bc2edSReid Kleckner GlobalValue *GV = dyn_cast<GlobalValue>(V);
168283bc2edSReid Kleckner GlobalVariable *Var = dyn_cast<GlobalVariable>(V);
169450aa64fSDan Gohman
170283bc2edSReid Kleckner if (Var && Var->getName() == "llvm.eh.catch.all.value") {
171283bc2edSReid Kleckner assert(Var->hasInitializer() &&
172450aa64fSDan Gohman "The EH catch-all value must have an initializer");
173283bc2edSReid Kleckner Value *Init = Var->getInitializer();
174283bc2edSReid Kleckner GV = dyn_cast<GlobalValue>(Init);
175450aa64fSDan Gohman if (!GV) V = cast<ConstantPointerNull>(Init);
176450aa64fSDan Gohman }
177450aa64fSDan Gohman
178450aa64fSDan Gohman assert((GV || isa<ConstantPointerNull>(V)) &&
179450aa64fSDan Gohman "TypeInfo must be a global variable or NULL");
180450aa64fSDan Gohman return GV;
181450aa64fSDan Gohman }
182450aa64fSDan Gohman
183450aa64fSDan Gohman /// getFCmpCondCode - Return the ISD condition code corresponding to
184450aa64fSDan Gohman /// the given LLVM IR floating-point condition code. This includes
185450aa64fSDan Gohman /// consideration of global floating-point math flags.
186450aa64fSDan Gohman ///
getFCmpCondCode(FCmpInst::Predicate Pred)187450aa64fSDan Gohman ISD::CondCode llvm::getFCmpCondCode(FCmpInst::Predicate Pred) {
188450aa64fSDan Gohman switch (Pred) {
18950f02cb2SNick Lewycky case FCmpInst::FCMP_FALSE: return ISD::SETFALSE;
19050f02cb2SNick Lewycky case FCmpInst::FCMP_OEQ: return ISD::SETOEQ;
19150f02cb2SNick Lewycky case FCmpInst::FCMP_OGT: return ISD::SETOGT;
19250f02cb2SNick Lewycky case FCmpInst::FCMP_OGE: return ISD::SETOGE;
19350f02cb2SNick Lewycky case FCmpInst::FCMP_OLT: return ISD::SETOLT;
19450f02cb2SNick Lewycky case FCmpInst::FCMP_OLE: return ISD::SETOLE;
19550f02cb2SNick Lewycky case FCmpInst::FCMP_ONE: return ISD::SETONE;
19650f02cb2SNick Lewycky case FCmpInst::FCMP_ORD: return ISD::SETO;
19750f02cb2SNick Lewycky case FCmpInst::FCMP_UNO: return ISD::SETUO;
19850f02cb2SNick Lewycky case FCmpInst::FCMP_UEQ: return ISD::SETUEQ;
19950f02cb2SNick Lewycky case FCmpInst::FCMP_UGT: return ISD::SETUGT;
20050f02cb2SNick Lewycky case FCmpInst::FCMP_UGE: return ISD::SETUGE;
20150f02cb2SNick Lewycky case FCmpInst::FCMP_ULT: return ISD::SETULT;
20250f02cb2SNick Lewycky case FCmpInst::FCMP_ULE: return ISD::SETULE;
20350f02cb2SNick Lewycky case FCmpInst::FCMP_UNE: return ISD::SETUNE;
20450f02cb2SNick Lewycky case FCmpInst::FCMP_TRUE: return ISD::SETTRUE;
20546a9f016SDavid Blaikie default: llvm_unreachable("Invalid FCmp predicate opcode!");
206450aa64fSDan Gohman }
20750f02cb2SNick Lewycky }
20850f02cb2SNick Lewycky
getFCmpCodeWithoutNaN(ISD::CondCode CC)20950f02cb2SNick Lewycky ISD::CondCode llvm::getFCmpCodeWithoutNaN(ISD::CondCode CC) {
21050f02cb2SNick Lewycky switch (CC) {
21150f02cb2SNick Lewycky case ISD::SETOEQ: case ISD::SETUEQ: return ISD::SETEQ;
21250f02cb2SNick Lewycky case ISD::SETONE: case ISD::SETUNE: return ISD::SETNE;
21350f02cb2SNick Lewycky case ISD::SETOLT: case ISD::SETULT: return ISD::SETLT;
21450f02cb2SNick Lewycky case ISD::SETOLE: case ISD::SETULE: return ISD::SETLE;
21550f02cb2SNick Lewycky case ISD::SETOGT: case ISD::SETUGT: return ISD::SETGT;
21650f02cb2SNick Lewycky case ISD::SETOGE: case ISD::SETUGE: return ISD::SETGE;
21746a9f016SDavid Blaikie default: return CC;
21850f02cb2SNick Lewycky }
219450aa64fSDan Gohman }
220450aa64fSDan Gohman
getICmpCondCode(ICmpInst::Predicate Pred)221450aa64fSDan Gohman ISD::CondCode llvm::getICmpCondCode(ICmpInst::Predicate Pred) {
222450aa64fSDan Gohman switch (Pred) {
223450aa64fSDan Gohman case ICmpInst::ICMP_EQ: return ISD::SETEQ;
224450aa64fSDan Gohman case ICmpInst::ICMP_NE: return ISD::SETNE;
225450aa64fSDan Gohman case ICmpInst::ICMP_SLE: return ISD::SETLE;
226450aa64fSDan Gohman case ICmpInst::ICMP_ULE: return ISD::SETULE;
227450aa64fSDan Gohman case ICmpInst::ICMP_SGE: return ISD::SETGE;
228450aa64fSDan Gohman case ICmpInst::ICMP_UGE: return ISD::SETUGE;
229450aa64fSDan Gohman case ICmpInst::ICMP_SLT: return ISD::SETLT;
230450aa64fSDan Gohman case ICmpInst::ICMP_ULT: return ISD::SETULT;
231450aa64fSDan Gohman case ICmpInst::ICMP_SGT: return ISD::SETGT;
232450aa64fSDan Gohman case ICmpInst::ICMP_UGT: return ISD::SETUGT;
233450aa64fSDan Gohman default:
234450aa64fSDan Gohman llvm_unreachable("Invalid ICmp predicate opcode!");
235450aa64fSDan Gohman }
236450aa64fSDan Gohman }
237450aa64fSDan Gohman
getICmpCondCode(ISD::CondCode Pred)23825043c82SRoman Lebedev ICmpInst::Predicate llvm::getICmpCondCode(ISD::CondCode Pred) {
23925043c82SRoman Lebedev switch (Pred) {
24025043c82SRoman Lebedev case ISD::SETEQ:
24125043c82SRoman Lebedev return ICmpInst::ICMP_EQ;
24225043c82SRoman Lebedev case ISD::SETNE:
24325043c82SRoman Lebedev return ICmpInst::ICMP_NE;
24425043c82SRoman Lebedev case ISD::SETLE:
24525043c82SRoman Lebedev return ICmpInst::ICMP_SLE;
24625043c82SRoman Lebedev case ISD::SETULE:
24725043c82SRoman Lebedev return ICmpInst::ICMP_ULE;
24825043c82SRoman Lebedev case ISD::SETGE:
24925043c82SRoman Lebedev return ICmpInst::ICMP_SGE;
25025043c82SRoman Lebedev case ISD::SETUGE:
25125043c82SRoman Lebedev return ICmpInst::ICMP_UGE;
25225043c82SRoman Lebedev case ISD::SETLT:
25325043c82SRoman Lebedev return ICmpInst::ICMP_SLT;
25425043c82SRoman Lebedev case ISD::SETULT:
25525043c82SRoman Lebedev return ICmpInst::ICMP_ULT;
25625043c82SRoman Lebedev case ISD::SETGT:
25725043c82SRoman Lebedev return ICmpInst::ICMP_SGT;
25825043c82SRoman Lebedev case ISD::SETUGT:
25925043c82SRoman Lebedev return ICmpInst::ICMP_UGT;
26025043c82SRoman Lebedev default:
26125043c82SRoman Lebedev llvm_unreachable("Invalid ISD integer condition code!");
26225043c82SRoman Lebedev }
26325043c82SRoman Lebedev }
26425043c82SRoman Lebedev
isNoopBitcast(Type * T1,Type * T2,const TargetLoweringBase & TLI)265ffc44549SStephen Lin static bool isNoopBitcast(Type *T1, Type *T2,
266c0659fadSMichael Gottesman const TargetLoweringBase& TLI) {
267ffc44549SStephen Lin return T1 == T2 || (T1->isPointerTy() && T2->isPointerTy()) ||
268ffc44549SStephen Lin (isa<VectorType>(T1) && isa<VectorType>(T2) &&
269ffc44549SStephen Lin TLI.isTypeLegal(EVT::getEVT(T1)) && TLI.isTypeLegal(EVT::getEVT(T2)));
270ffc44549SStephen Lin }
2714f3615deSChris Lattner
272a4415854STim Northover /// Look through operations that will be free to find the earliest source of
273a4415854STim Northover /// this value.
274a4415854STim Northover ///
275d68904f9SJames Henderson /// @param ValLoc If V has aggregate type, we will be interested in a particular
276a4415854STim Northover /// scalar component. This records its address; the reverse of this list gives a
277a4415854STim Northover /// sequence of indices appropriate for an extractvalue to locate the important
278a4415854STim Northover /// value. This value is updated during the function and on exit will indicate
279a4415854STim Northover /// similar information for the Value returned.
280a4415854STim Northover ///
281a4415854STim Northover /// @param DataBits If this function looks through truncate instructions, this
282a4415854STim Northover /// will record the smallest size attained.
getNoopInput(const Value * V,SmallVectorImpl<unsigned> & ValLoc,unsigned & DataBits,const TargetLoweringBase & TLI,const DataLayout & DL)283a4415854STim Northover static const Value *getNoopInput(const Value *V,
284a4415854STim Northover SmallVectorImpl<unsigned> &ValLoc,
285a4415854STim Northover unsigned &DataBits,
28644ede33aSMehdi Amini const TargetLoweringBase &TLI,
28744ede33aSMehdi Amini const DataLayout &DL) {
288ffc44549SStephen Lin while (true) {
289ffc44549SStephen Lin // Try to look through V1; if V1 is not an instruction, it can't be looked
290ffc44549SStephen Lin // through.
291a4415854STim Northover const Instruction *I = dyn_cast<Instruction>(V);
292a4415854STim Northover if (!I || I->getNumOperands() == 0) return V;
293c0196b1bSCraig Topper const Value *NoopInput = nullptr;
294a4415854STim Northover
295182fe3eeSChris Lattner Value *Op = I->getOperand(0);
296a4415854STim Northover if (isa<BitCastInst>(I)) {
2974f3615deSChris Lattner // Look through truly no-op bitcasts.
298ffc44549SStephen Lin if (isNoopBitcast(Op->getType(), I->getType(), TLI))
299ffc44549SStephen Lin NoopInput = Op;
300ffc44549SStephen Lin } else if (isa<GetElementPtrInst>(I)) {
301ffc44549SStephen Lin // Look through getelementptr
302ffc44549SStephen Lin if (cast<GetElementPtrInst>(I)->hasAllZeroIndices())
303ffc44549SStephen Lin NoopInput = Op;
304ffc44549SStephen Lin } else if (isa<IntToPtrInst>(I)) {
305182fe3eeSChris Lattner // Look through inttoptr.
306ffc44549SStephen Lin // Make sure this isn't a truncating or extending cast. We could
307ffc44549SStephen Lin // support this eventually, but don't bother for now.
308ffc44549SStephen Lin if (!isa<VectorType>(I->getType()) &&
30944ede33aSMehdi Amini DL.getPointerSizeInBits() ==
310182fe3eeSChris Lattner cast<IntegerType>(Op->getType())->getBitWidth())
311ffc44549SStephen Lin NoopInput = Op;
312ffc44549SStephen Lin } else if (isa<PtrToIntInst>(I)) {
313182fe3eeSChris Lattner // Look through ptrtoint.
314ffc44549SStephen Lin // Make sure this isn't a truncating or extending cast. We could
315ffc44549SStephen Lin // support this eventually, but don't bother for now.
316ffc44549SStephen Lin if (!isa<VectorType>(I->getType()) &&
31744ede33aSMehdi Amini DL.getPointerSizeInBits() ==
318182fe3eeSChris Lattner cast<IntegerType>(I->getType())->getBitWidth())
319ffc44549SStephen Lin NoopInput = Op;
320a4415854STim Northover } else if (isa<TruncInst>(I) &&
321a4415854STim Northover TLI.allowTruncateForTailCall(Op->getType(), I->getType())) {
322b302561bSGraham Hunter DataBits = std::min((uint64_t)DataBits,
323b302561bSGraham Hunter I->getType()->getPrimitiveSizeInBits().getFixedSize());
324a4415854STim Northover NoopInput = Op;
325f06cf9daSCraig Topper } else if (auto *CB = dyn_cast<CallBase>(I)) {
326f06cf9daSCraig Topper const Value *ReturnedOp = CB->getReturnedArgOperand();
3276aff744eSAhmed Bougacha if (ReturnedOp && isNoopBitcast(ReturnedOp->getType(), I->getType(), TLI))
3286aff744eSAhmed Bougacha NoopInput = ReturnedOp;
329a4415854STim Northover } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(V)) {
330a4415854STim Northover // Value may come from either the aggregate or the scalar
331a4415854STim Northover ArrayRef<unsigned> InsertLoc = IVI->getIndices();
332e4310fe9STim Northover if (ValLoc.size() >= InsertLoc.size() &&
333e4310fe9STim Northover std::equal(InsertLoc.begin(), InsertLoc.end(), ValLoc.rbegin())) {
334a4415854STim Northover // The type being inserted is a nested sub-type of the aggregate; we
335a4415854STim Northover // have to remove those initial indices to get the location we're
336a4415854STim Northover // interested in for the operand.
337a4415854STim Northover ValLoc.resize(ValLoc.size() - InsertLoc.size());
338a4415854STim Northover NoopInput = IVI->getInsertedValueOperand();
339a4415854STim Northover } else {
340a4415854STim Northover // The struct we're inserting into has the value we're interested in, no
341a4415854STim Northover // change of address.
342a4415854STim Northover NoopInput = Op;
343a4415854STim Northover }
344a4415854STim Northover } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {
345a4415854STim Northover // The part we're interested in will inevitably be some sub-section of the
346a4415854STim Northover // previous aggregate. Combine the two paths to obtain the true address of
347a4415854STim Northover // our element.
348a4415854STim Northover ArrayRef<unsigned> ExtractLoc = EVI->getIndices();
3494f6ac162SBenjamin Kramer ValLoc.append(ExtractLoc.rbegin(), ExtractLoc.rend());
350a4415854STim Northover NoopInput = Op;
351a4415854STim Northover }
352a4415854STim Northover // Terminate if we couldn't find anything to look through.
353a4415854STim Northover if (!NoopInput)
354a4415854STim Northover return V;
355a4415854STim Northover
356a4415854STim Northover V = NoopInput;
357ffc44549SStephen Lin }
358182fe3eeSChris Lattner }
359182fe3eeSChris Lattner
360a4415854STim Northover /// Return true if this scalar return value only has bits discarded on its path
361a4415854STim Northover /// from the "tail call" to the "ret". This includes the obvious noop
362a4415854STim Northover /// instructions handled by getNoopInput above as well as free truncations (or
363a4415854STim Northover /// 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)364a4415854STim Northover static bool slotOnlyDiscardsData(const Value *RetVal, const Value *CallVal,
365a4415854STim Northover SmallVectorImpl<unsigned> &RetIndices,
366a4415854STim Northover SmallVectorImpl<unsigned> &CallIndices,
367707d68f0STim Northover bool AllowDifferingSizes,
36844ede33aSMehdi Amini const TargetLoweringBase &TLI,
36944ede33aSMehdi Amini const DataLayout &DL) {
3704f3615deSChris Lattner
371a4415854STim Northover // Trace the sub-value needed by the return value as far back up the graph as
372a4415854STim Northover // possible, in the hope that it will intersect with the value produced by the
373a4415854STim Northover // call. In the simple case with no "returned" attribute, the hope is actually
374a4415854STim Northover // that we end up back at the tail call instruction itself.
375a4415854STim Northover unsigned BitsRequired = UINT_MAX;
37644ede33aSMehdi Amini RetVal = getNoopInput(RetVal, RetIndices, BitsRequired, TLI, DL);
377ffc44549SStephen Lin
378a4415854STim Northover // If this slot in the value returned is undef, it doesn't matter what the
379a4415854STim Northover // call puts there, it'll be fine.
380a4415854STim Northover if (isa<UndefValue>(RetVal))
381a4415854STim Northover return true;
382ffc44549SStephen Lin
383a4415854STim Northover // Now do a similar search up through the graph to find where the value
384a4415854STim Northover // actually returned by the "tail call" comes from. In the simple case without
385a4415854STim Northover // a "returned" attribute, the search will be blocked immediately and the loop
386a4415854STim Northover // a Noop.
387a4415854STim Northover unsigned BitsProvided = UINT_MAX;
38844ede33aSMehdi Amini CallVal = getNoopInput(CallVal, CallIndices, BitsProvided, TLI, DL);
389a4415854STim Northover
390a4415854STim Northover // There's no hope if we can't actually trace them to (the same part of!) the
391a4415854STim Northover // same value.
392a4415854STim Northover if (CallVal != RetVal || CallIndices != RetIndices)
393a4415854STim Northover return false;
394a4415854STim Northover
395a4415854STim Northover // However, intervening truncates may have made the call non-tail. Make sure
396a4415854STim Northover // all the bits that are needed by the "ret" have been provided by the "tail
397a4415854STim Northover // call". FIXME: with sufficiently cunning bit-tracking, we could look through
398a4415854STim Northover // extensions too.
399707d68f0STim Northover if (BitsProvided < BitsRequired ||
400707d68f0STim Northover (!AllowDifferingSizes && BitsProvided != BitsRequired))
401a4415854STim Northover return false;
402a4415854STim Northover
403ffc44549SStephen Lin return true;
404ffc44549SStephen Lin }
405a4415854STim Northover
406a4415854STim Northover /// For an aggregate type, determine whether a given index is within bounds or
407a4415854STim Northover /// not.
indexReallyValid(Type * T,unsigned Idx)408e24e95feSEli Friedman static bool indexReallyValid(Type *T, unsigned Idx) {
409a4415854STim Northover if (ArrayType *AT = dyn_cast<ArrayType>(T))
410a4415854STim Northover return Idx < AT->getNumElements();
411a4415854STim Northover
412a4415854STim Northover return Idx < cast<StructType>(T)->getNumElements();
413ffc44549SStephen Lin }
414a4415854STim Northover
415a4415854STim Northover /// Move the given iterators to the next leaf type in depth first traversal.
416a4415854STim Northover ///
417a4415854STim Northover /// Performs a depth-first traversal of the type as specified by its arguments,
418a4415854STim Northover /// stopping at the next leaf node (which may be a legitimate scalar type or an
419a4415854STim Northover /// empty struct or array).
420a4415854STim Northover ///
421a4415854STim Northover /// @param SubTypes List of the partial components making up the type from
422a4415854STim Northover /// outermost to innermost non-empty aggregate. The element currently
423a4415854STim Northover /// represented is SubTypes.back()->getTypeAtIndex(Path.back() - 1).
424a4415854STim Northover ///
425a4415854STim Northover /// @param Path Set of extractvalue indices leading from the outermost type
426a4415854STim Northover /// (SubTypes[0]) to the leaf node currently represented.
427a4415854STim Northover ///
428a4415854STim Northover /// @returns true if a new type was found, false otherwise. Calling this
429a4415854STim Northover /// function again on a finished iterator will repeatedly return
430a4415854STim Northover /// false. SubTypes.back()->getTypeAtIndex(Path.back()) is either an empty
431a4415854STim Northover /// aggregate or a non-aggregate
advanceToNextLeafType(SmallVectorImpl<Type * > & SubTypes,SmallVectorImpl<unsigned> & Path)432e24e95feSEli Friedman static bool advanceToNextLeafType(SmallVectorImpl<Type *> &SubTypes,
433a4415854STim Northover SmallVectorImpl<unsigned> &Path) {
434a4415854STim Northover // First march back up the tree until we can successfully increment one of the
435a4415854STim Northover // coordinates in Path.
436a4415854STim Northover while (!Path.empty() && !indexReallyValid(SubTypes.back(), Path.back() + 1)) {
437a4415854STim Northover Path.pop_back();
438a4415854STim Northover SubTypes.pop_back();
439a4415854STim Northover }
440a4415854STim Northover
441a4415854STim Northover // If we reached the top, then the iterator is done.
442a4415854STim Northover if (Path.empty())
443a4415854STim Northover return false;
444a4415854STim Northover
445a4415854STim Northover // We know there's *some* valid leaf now, so march back down the tree picking
446a4415854STim Northover // out the left-most element at each node.
447a4415854STim Northover ++Path.back();
448e24e95feSEli Friedman Type *DeeperType =
449e24e95feSEli Friedman ExtractValueInst::getIndexedType(SubTypes.back(), Path.back());
450a4415854STim Northover while (DeeperType->isAggregateType()) {
451e24e95feSEli Friedman if (!indexReallyValid(DeeperType, 0))
452a4415854STim Northover return true;
453a4415854STim Northover
454e24e95feSEli Friedman SubTypes.push_back(DeeperType);
455a4415854STim Northover Path.push_back(0);
456a4415854STim Northover
457e24e95feSEli Friedman DeeperType = ExtractValueInst::getIndexedType(DeeperType, 0);
458a4415854STim Northover }
459a4415854STim Northover
460ffc44549SStephen Lin return true;
461ffc44549SStephen Lin }
462a4415854STim Northover
463a4415854STim Northover /// Find the first non-empty, scalar-like type in Next and setup the iterator
464a4415854STim Northover /// components.
465a4415854STim Northover ///
466a4415854STim Northover /// Assuming Next is an aggregate of some kind, this function will traverse the
467a4415854STim Northover /// tree from left to right (i.e. depth-first) looking for the first
468a4415854STim Northover /// non-aggregate type which will play a role in function return.
469a4415854STim Northover ///
470a4415854STim Northover /// For example, if Next was {[0 x i64], {{}, i32, {}}, i32} then we would setup
471a4415854STim Northover /// Path as [1, 1] and SubTypes as [Next, {{}, i32, {}}] to represent the first
472a4415854STim Northover /// i32 in that type.
firstRealType(Type * Next,SmallVectorImpl<Type * > & SubTypes,SmallVectorImpl<unsigned> & Path)473e24e95feSEli Friedman static bool firstRealType(Type *Next, SmallVectorImpl<Type *> &SubTypes,
474a4415854STim Northover SmallVectorImpl<unsigned> &Path) {
475a4415854STim Northover // First initialise the iterator components to the first "leaf" node
476a4415854STim Northover // (i.e. node with no valid sub-type at any index, so {} does count as a leaf
477a4415854STim Northover // despite nominally being an aggregate).
478e24e95feSEli Friedman while (Type *FirstInner = ExtractValueInst::getIndexedType(Next, 0)) {
479e24e95feSEli Friedman SubTypes.push_back(Next);
480a4415854STim Northover Path.push_back(0);
481e24e95feSEli Friedman Next = FirstInner;
482ffc44549SStephen Lin }
483ffc44549SStephen Lin
484a4415854STim Northover // If there's no Path now, Next was originally scalar already (or empty
485a4415854STim Northover // leaf). We're done.
486a4415854STim Northover if (Path.empty())
487a4415854STim Northover return true;
488ffc44549SStephen Lin
489a4415854STim Northover // Otherwise, use normal iteration to keep looking through the tree until we
490a4415854STim Northover // find a non-aggregate type.
491e24e95feSEli Friedman while (ExtractValueInst::getIndexedType(SubTypes.back(), Path.back())
492e24e95feSEli Friedman ->isAggregateType()) {
493a4415854STim Northover if (!advanceToNextLeafType(SubTypes, Path))
494ffc44549SStephen Lin return false;
495ffc44549SStephen Lin }
4964f3615deSChris Lattner
497a4415854STim Northover return true;
498a4415854STim Northover }
499a4415854STim Northover
500a4415854STim Northover /// Set the iterator data-structures to the next non-empty, non-aggregate
501a4415854STim Northover /// subtype.
nextRealType(SmallVectorImpl<Type * > & SubTypes,SmallVectorImpl<unsigned> & Path)502e24e95feSEli Friedman static bool nextRealType(SmallVectorImpl<Type *> &SubTypes,
503a4415854STim Northover SmallVectorImpl<unsigned> &Path) {
504a4415854STim Northover do {
505a4415854STim Northover if (!advanceToNextLeafType(SubTypes, Path))
506a4415854STim Northover return false;
507a4415854STim Northover
508a4415854STim Northover assert(!Path.empty() && "found a leaf but didn't set the path?");
509e24e95feSEli Friedman } while (ExtractValueInst::getIndexedType(SubTypes.back(), Path.back())
510e24e95feSEli Friedman ->isAggregateType());
511a4415854STim Northover
512a4415854STim Northover return true;
513a4415854STim Northover }
514a4415854STim Northover
515a4415854STim Northover
516450aa64fSDan Gohman /// Test if the given instruction is in a position to be optimized
517450aa64fSDan Gohman /// with a tail-call. This roughly means that it's in a block with
518450aa64fSDan Gohman /// a return and there's nothing that needs to be scheduled
519450aa64fSDan Gohman /// between it and the return.
520450aa64fSDan Gohman ///
521450aa64fSDan Gohman /// This function only tests target-independent requirements.
isInTailCallPosition(const CallBase & Call,const TargetMachine & TM)52230430938SCraig Topper bool llvm::isInTailCallPosition(const CallBase &Call, const TargetMachine &TM) {
52330430938SCraig Topper const BasicBlock *ExitBB = Call.getParent();
524edb12a83SChandler Carruth const Instruction *Term = ExitBB->getTerminator();
525450aa64fSDan Gohman const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
526450aa64fSDan Gohman
527450aa64fSDan Gohman // The block must end in a return statement or unreachable.
528450aa64fSDan Gohman //
529450aa64fSDan Gohman // FIXME: Decline tailcall if it's not guaranteed and if the block ends in
530450aa64fSDan Gohman // an unreachable, for now. The way tailcall optimization is currently
531450aa64fSDan Gohman // implemented means it will add an epilogue followed by a jump. That is
532450aa64fSDan Gohman // not profitable. Also, if the callee is a special function (e.g.
533450aa64fSDan Gohman // longjmp on x86), it can end up causing miscompilation that has not
534450aa64fSDan Gohman // been fully understood.
53582a0e808STim Northover if (!Ret && ((!TM.Options.GuaranteedTailCallOpt &&
53682a0e808STim Northover Call.getCallingConv() != CallingConv::Tail &&
53782a0e808STim Northover Call.getCallingConv() != CallingConv::SwiftTail) ||
53882a0e808STim Northover !isa<UnreachableInst>(Term)))
5394f3615deSChris Lattner return false;
540450aa64fSDan Gohman
541450aa64fSDan Gohman // If I will have a chain, make sure no other instruction that will have a
542450aa64fSDan Gohman // chain interposes between I and the return.
5433abe7acaSVictor Huang // Check for all calls including speculatable functions.
544b6d0bd48SBenjamin Kramer for (BasicBlock::const_iterator BBI = std::prev(ExitBB->end(), 2);; --BBI) {
54530430938SCraig Topper if (&*BBI == &Call)
546450aa64fSDan Gohman break;
547450aa64fSDan Gohman // Debug info intrinsics do not get in the way of tail call optimization.
548f3c44569SHongtao Yu // Pseudo probe intrinsics do not block tail call optimization either.
549098a0d8fSHongtao Yu if (BBI->isDebugOrPseudoInst())
550f3c44569SHongtao Yu continue;
551121cac01SJeroen Dobbelaere // A lifetime end, assume or noalias.decl intrinsic should not stop tail
552121cac01SJeroen Dobbelaere // call optimization.
55318bfb3a5SRobert Lougher if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(BBI))
554e03f6a16SGuozhi Wei if (II->getIntrinsicID() == Intrinsic::lifetime_end ||
555121cac01SJeroen Dobbelaere II->getIntrinsicID() == Intrinsic::assume ||
556121cac01SJeroen Dobbelaere II->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl)
55718bfb3a5SRobert Lougher continue;
558450aa64fSDan Gohman if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
559980f8f26SDuncan P. N. Exon Smith !isSafeToSpeculativelyExecute(&*BBI))
560450aa64fSDan Gohman return false;
561450aa64fSDan Gohman }
562450aa64fSDan Gohman
563f734a8baSEric Christopher const Function *F = ExitBB->getParent();
564d913448bSEric Christopher return returnTypeIsEligibleForTailCall(
56530430938SCraig Topper F, &Call, Ret, *TM.getSubtargetImpl(*F)->getTargetLowering());
566ce0e4c26SMichael Gottesman }
567ce0e4c26SMichael Gottesman
attributesPermitTailCall(const Function * F,const Instruction * I,const ReturnInst * Ret,const TargetLoweringBase & TLI,bool * AllowDifferingSizes)568f79af6f8SMichael Kuperstein bool llvm::attributesPermitTailCall(const Function *F, const Instruction *I,
569f79af6f8SMichael Kuperstein const ReturnInst *Ret,
570f79af6f8SMichael Kuperstein const TargetLoweringBase &TLI,
571f79af6f8SMichael Kuperstein bool *AllowDifferingSizes) {
572f79af6f8SMichael Kuperstein // ADS may be null, so don't write to it directly.
573f79af6f8SMichael Kuperstein bool DummyADS;
574f79af6f8SMichael Kuperstein bool &ADS = AllowDifferingSizes ? *AllowDifferingSizes : DummyADS;
575f79af6f8SMichael Kuperstein ADS = true;
576f79af6f8SMichael Kuperstein
577c63a3175SNikita Popov AttrBuilder CallerAttrs(F->getContext(), F->getAttributes().getRetAttrs());
578c63a3175SNikita Popov AttrBuilder CalleeAttrs(F->getContext(),
579c63a3175SNikita Popov cast<CallInst>(I)->getAttributes().getRetAttrs());
580f79af6f8SMichael Kuperstein
58162ad2128SDávid Bolvanský // Following attributes are completely benign as far as calling convention
582353cb3d4SDavid Green // goes, they shouldn't affect whether the call is a tail call.
583ef2dc7edSDávid Bolvanský for (const auto &Attr : {Attribute::Alignment, Attribute::Dereferenceable,
584ef2dc7edSDávid Bolvanský Attribute::DereferenceableOrNull, Attribute::NoAlias,
585*ae990a3cSDávid Bolvanský Attribute::NonNull, Attribute::NoUndef}) {
586ef2dc7edSDávid Bolvanský CallerAttrs.removeAttribute(Attr);
587ef2dc7edSDávid Bolvanský CalleeAttrs.removeAttribute(Attr);
588ef2dc7edSDávid Bolvanský }
589f79af6f8SMichael Kuperstein
590f79af6f8SMichael Kuperstein if (CallerAttrs.contains(Attribute::ZExt)) {
591f79af6f8SMichael Kuperstein if (!CalleeAttrs.contains(Attribute::ZExt))
592f79af6f8SMichael Kuperstein return false;
593f79af6f8SMichael Kuperstein
594f79af6f8SMichael Kuperstein ADS = false;
595f79af6f8SMichael Kuperstein CallerAttrs.removeAttribute(Attribute::ZExt);
596f79af6f8SMichael Kuperstein CalleeAttrs.removeAttribute(Attribute::ZExt);
597f79af6f8SMichael Kuperstein } else if (CallerAttrs.contains(Attribute::SExt)) {
598f79af6f8SMichael Kuperstein if (!CalleeAttrs.contains(Attribute::SExt))
599f79af6f8SMichael Kuperstein return false;
600f79af6f8SMichael Kuperstein
601f79af6f8SMichael Kuperstein ADS = false;
602f79af6f8SMichael Kuperstein CallerAttrs.removeAttribute(Attribute::SExt);
603f79af6f8SMichael Kuperstein CalleeAttrs.removeAttribute(Attribute::SExt);
604f79af6f8SMichael Kuperstein }
605f79af6f8SMichael Kuperstein
606ac6454a7SFrancis Visoiu Mistrih // Drop sext and zext return attributes if the result is not used.
607ac6454a7SFrancis Visoiu Mistrih // This enables tail calls for code like:
608ac6454a7SFrancis Visoiu Mistrih //
609ac6454a7SFrancis Visoiu Mistrih // define void @caller() {
610ac6454a7SFrancis Visoiu Mistrih // entry:
611ac6454a7SFrancis Visoiu Mistrih // %unused_result = tail call zeroext i1 @callee()
612ac6454a7SFrancis Visoiu Mistrih // br label %retlabel
613ac6454a7SFrancis Visoiu Mistrih // retlabel:
614ac6454a7SFrancis Visoiu Mistrih // ret void
615ac6454a7SFrancis Visoiu Mistrih // }
616ac6454a7SFrancis Visoiu Mistrih if (I->use_empty()) {
617ac6454a7SFrancis Visoiu Mistrih CalleeAttrs.removeAttribute(Attribute::SExt);
618ac6454a7SFrancis Visoiu Mistrih CalleeAttrs.removeAttribute(Attribute::ZExt);
619ac6454a7SFrancis Visoiu Mistrih }
620ac6454a7SFrancis Visoiu Mistrih
621f79af6f8SMichael Kuperstein // If they're still different, there's some facet we don't understand
622f79af6f8SMichael Kuperstein // (currently only "inreg", but in future who knows). It may be OK but the
623f79af6f8SMichael Kuperstein // only safe option is to reject the tail call.
624f79af6f8SMichael Kuperstein return CallerAttrs == CalleeAttrs;
625f79af6f8SMichael Kuperstein }
626f79af6f8SMichael Kuperstein
627f2cb9c0eSSanne Wouda /// Check whether B is a bitcast of a pointer type to another pointer type,
628f2cb9c0eSSanne Wouda /// which is equal to A.
isPointerBitcastEqualTo(const Value * A,const Value * B)629f2cb9c0eSSanne Wouda static bool isPointerBitcastEqualTo(const Value *A, const Value *B) {
630f2cb9c0eSSanne Wouda assert(A && B && "Expected non-null inputs!");
631f2cb9c0eSSanne Wouda
632f2cb9c0eSSanne Wouda auto *BitCastIn = dyn_cast<BitCastInst>(B);
633f2cb9c0eSSanne Wouda
634f2cb9c0eSSanne Wouda if (!BitCastIn)
635f2cb9c0eSSanne Wouda return false;
636f2cb9c0eSSanne Wouda
637f2cb9c0eSSanne Wouda if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
638f2cb9c0eSSanne Wouda return false;
639f2cb9c0eSSanne Wouda
640f2cb9c0eSSanne Wouda return A == BitCastIn->getOperand(0);
641f2cb9c0eSSanne Wouda }
642f2cb9c0eSSanne Wouda
returnTypeIsEligibleForTailCall(const Function * F,const Instruction * I,const ReturnInst * Ret,const TargetLoweringBase & TLI)643ce0e4c26SMichael Gottesman bool llvm::returnTypeIsEligibleForTailCall(const Function *F,
644ce0e4c26SMichael Gottesman const Instruction *I,
645ce0e4c26SMichael Gottesman const ReturnInst *Ret,
646ce0e4c26SMichael Gottesman const TargetLoweringBase &TLI) {
647450aa64fSDan Gohman // If the block ends with a void return or unreachable, it doesn't matter
648450aa64fSDan Gohman // what the call's return type is.
649450aa64fSDan Gohman if (!Ret || Ret->getNumOperands() == 0) return true;
650450aa64fSDan Gohman
651450aa64fSDan Gohman // If the return value is undef, it doesn't matter what the call's
652450aa64fSDan Gohman // return type is.
653450aa64fSDan Gohman if (isa<UndefValue>(Ret->getOperand(0))) return true;
654450aa64fSDan Gohman
655707d68f0STim Northover // Make sure the attributes attached to each return are compatible.
656f79af6f8SMichael Kuperstein bool AllowDifferingSizes;
657f79af6f8SMichael Kuperstein if (!attributesPermitTailCall(F, I, Ret, TLI, &AllowDifferingSizes))
658450aa64fSDan Gohman return false;
659450aa64fSDan Gohman
660a4415854STim Northover const Value *RetVal = Ret->getOperand(0), *CallVal = I;
6615d84d9b3SWei Mi // Intrinsic like llvm.memcpy has no return value, but the expanded
6625d84d9b3SWei Mi // libcall may or may not have return value. On most platforms, it
6635d84d9b3SWei Mi // will be expanded as memcpy in libc, which returns the first
6645d84d9b3SWei Mi // argument. On other platforms like arm-none-eabi, memcpy may be
6655d84d9b3SWei Mi // expanded as library call without return value, like __aeabi_memcpy.
666818d50a9SWei Mi const CallInst *Call = cast<CallInst>(I);
667818d50a9SWei Mi if (Function *F = Call->getCalledFunction()) {
668818d50a9SWei Mi Intrinsic::ID IID = F->getIntrinsicID();
6695d84d9b3SWei Mi if (((IID == Intrinsic::memcpy &&
6705d84d9b3SWei Mi TLI.getLibcallName(RTLIB::MEMCPY) == StringRef("memcpy")) ||
6715d84d9b3SWei Mi (IID == Intrinsic::memmove &&
6725d84d9b3SWei Mi TLI.getLibcallName(RTLIB::MEMMOVE) == StringRef("memmove")) ||
6735d84d9b3SWei Mi (IID == Intrinsic::memset &&
6745d84d9b3SWei Mi TLI.getLibcallName(RTLIB::MEMSET) == StringRef("memset"))) &&
675f2cb9c0eSSanne Wouda (RetVal == Call->getArgOperand(0) ||
676f2cb9c0eSSanne Wouda isPointerBitcastEqualTo(RetVal, Call->getArgOperand(0))))
677818d50a9SWei Mi return true;
678818d50a9SWei Mi }
679818d50a9SWei Mi
680a4415854STim Northover SmallVector<unsigned, 4> RetPath, CallPath;
681e24e95feSEli Friedman SmallVector<Type *, 4> RetSubTypes, CallSubTypes;
682a4415854STim Northover
683a4415854STim Northover bool RetEmpty = !firstRealType(RetVal->getType(), RetSubTypes, RetPath);
684a4415854STim Northover bool CallEmpty = !firstRealType(CallVal->getType(), CallSubTypes, CallPath);
685a4415854STim Northover
686a4415854STim Northover // Nothing's actually returned, it doesn't matter what the callee put there
687a4415854STim Northover // it's a valid tail call.
688a4415854STim Northover if (RetEmpty)
689a4415854STim Northover return true;
690a4415854STim Northover
691a4415854STim Northover // Iterate pairwise through each of the value types making up the tail call
692a4415854STim Northover // and the corresponding return. For each one we want to know whether it's
693a4415854STim Northover // essentially going directly from the tail call to the ret, via operations
694a4415854STim Northover // that end up not generating any code.
695a4415854STim Northover //
696a4415854STim Northover // We allow a certain amount of covariance here. For example it's permitted
697a4415854STim Northover // for the tail call to define more bits than the ret actually cares about
698a4415854STim Northover // (e.g. via a truncate).
699a4415854STim Northover do {
700a4415854STim Northover if (CallEmpty) {
701a4415854STim Northover // We've exhausted the values produced by the tail call instruction, the
702a4415854STim Northover // rest are essentially undef. The type doesn't really matter, but we need
703a4415854STim Northover // *something*.
704e24e95feSEli Friedman Type *SlotType =
705e24e95feSEli Friedman ExtractValueInst::getIndexedType(RetSubTypes.back(), RetPath.back());
706a4415854STim Northover CallVal = UndefValue::get(SlotType);
707a4415854STim Northover }
708a4415854STim Northover
709a4415854STim Northover // The manipulations performed when we're looking through an insertvalue or
710a4415854STim Northover // an extractvalue would happen at the front of the RetPath list, so since
711a4415854STim Northover // we have to copy it anyway it's more efficient to create a reversed copy.
712500c4b68SKazu Hirata SmallVector<unsigned, 4> TmpRetPath(llvm::reverse(RetPath));
713500c4b68SKazu Hirata SmallVector<unsigned, 4> TmpCallPath(llvm::reverse(CallPath));
714a4415854STim Northover
715a4415854STim Northover // Finally, we can check whether the value produced by the tail call at this
716a4415854STim Northover // index is compatible with the value we return.
717707d68f0STim Northover if (!slotOnlyDiscardsData(RetVal, CallVal, TmpRetPath, TmpCallPath,
71844ede33aSMehdi Amini AllowDifferingSizes, TLI,
71944ede33aSMehdi Amini F->getParent()->getDataLayout()))
720a4415854STim Northover return false;
721a4415854STim Northover
722a4415854STim Northover CallEmpty = !nextRealType(CallSubTypes, CallPath);
723a4415854STim Northover } while(nextRealType(RetSubTypes, RetPath));
724a4415854STim Northover
725a4415854STim Northover return true;
726450aa64fSDan Gohman }
727f21434ccSRafael Espindola
collectEHScopeMembers(DenseMap<const MachineBasicBlock *,int> & EHScopeMembership,int EHScope,const MachineBasicBlock * MBB)728d69acf3bSHeejin Ahn static void collectEHScopeMembers(
729d69acf3bSHeejin Ahn DenseMap<const MachineBasicBlock *, int> &EHScopeMembership, int EHScope,
730d69acf3bSHeejin Ahn const MachineBasicBlock *MBB) {
731734d7c32SDavid Majnemer SmallVector<const MachineBasicBlock *, 16> Worklist = {MBB};
732734d7c32SDavid Majnemer while (!Worklist.empty()) {
733734d7c32SDavid Majnemer const MachineBasicBlock *Visiting = Worklist.pop_back_val();
7341e4d3504SHeejin Ahn // Don't follow blocks which start new scopes.
735734d7c32SDavid Majnemer if (Visiting->isEHPad() && Visiting != MBB)
736734d7c32SDavid Majnemer continue;
737734d7c32SDavid Majnemer
7381e4d3504SHeejin Ahn // Add this MBB to our scope.
739d69acf3bSHeejin Ahn auto P = EHScopeMembership.insert(std::make_pair(Visiting, EHScope));
7407b54b525SDavid Blaikie
74116193552SDavid Majnemer // Don't revisit blocks.
7427b54b525SDavid Blaikie if (!P.second) {
743d69acf3bSHeejin Ahn assert(P.first->second == EHScope && "MBB is part of two scopes!");
744734d7c32SDavid Majnemer continue;
74516193552SDavid Majnemer }
74616193552SDavid Majnemer
7471e4d3504SHeejin Ahn // Returns are boundaries where scope transfer can occur, don't follow
74816193552SDavid Majnemer // successors.
749ed5e06b0SHeejin Ahn if (Visiting->isEHScopeReturnBlock())
750734d7c32SDavid Majnemer continue;
75116193552SDavid Majnemer
752c5c4dbd2SKazu Hirata append_range(Worklist, Visiting->successors());
753734d7c32SDavid Majnemer }
75416193552SDavid Majnemer }
75516193552SDavid Majnemer
75616193552SDavid Majnemer DenseMap<const MachineBasicBlock *, int>
getEHScopeMembership(const MachineFunction & MF)7571e4d3504SHeejin Ahn llvm::getEHScopeMembership(const MachineFunction &MF) {
758d69acf3bSHeejin Ahn DenseMap<const MachineBasicBlock *, int> EHScopeMembership;
75916193552SDavid Majnemer
76016193552SDavid Majnemer // We don't have anything to do if there aren't any EH pads.
7611e4d3504SHeejin Ahn if (!MF.hasEHScopes())
762d69acf3bSHeejin Ahn return EHScopeMembership;
76316193552SDavid Majnemer
764e4f9b09bSDavid Majnemer int EntryBBNumber = MF.front().getNumber();
76516193552SDavid Majnemer bool IsSEH = isAsynchronousEHPersonality(
766f1caa283SMatthias Braun classifyEHPersonality(MF.getFunction().getPersonalityFn()));
76716193552SDavid Majnemer
76816193552SDavid Majnemer const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
769d69acf3bSHeejin Ahn SmallVector<const MachineBasicBlock *, 16> EHScopeBlocks;
770e4f9b09bSDavid Majnemer SmallVector<const MachineBasicBlock *, 16> UnreachableBlocks;
771e4f9b09bSDavid Majnemer SmallVector<const MachineBasicBlock *, 16> SEHCatchPads;
77216193552SDavid Majnemer SmallVector<std::pair<const MachineBasicBlock *, int>, 16> CatchRetSuccessors;
77316193552SDavid Majnemer for (const MachineBasicBlock &MBB : MF) {
7741e4d3504SHeejin Ahn if (MBB.isEHScopeEntry()) {
775d69acf3bSHeejin Ahn EHScopeBlocks.push_back(&MBB);
776e4f9b09bSDavid Majnemer } else if (IsSEH && MBB.isEHPad()) {
777e4f9b09bSDavid Majnemer SEHCatchPads.push_back(&MBB);
778e4f9b09bSDavid Majnemer } else if (MBB.pred_empty()) {
779e4f9b09bSDavid Majnemer UnreachableBlocks.push_back(&MBB);
780e4f9b09bSDavid Majnemer }
78116193552SDavid Majnemer
78216193552SDavid Majnemer MachineBasicBlock::const_iterator MBBI = MBB.getFirstTerminator();
7832e7af979SDuncan P. N. Exon Smith
7841e4d3504SHeejin Ahn // CatchPads are not scopes for SEH so do not consider CatchRet to
7851e4d3504SHeejin Ahn // transfer control to another scope.
78626f9e9ebSReid Kleckner if (MBBI == MBB.end() || MBBI->getOpcode() != TII->getCatchReturnOpcode())
78716193552SDavid Majnemer continue;
78816193552SDavid Majnemer
789e4f9b09bSDavid Majnemer // FIXME: SEH CatchPads are not necessarily in the parent function:
790e4f9b09bSDavid Majnemer // they could be inside a finally block.
79116193552SDavid Majnemer const MachineBasicBlock *Successor = MBBI->getOperand(0).getMBB();
79216193552SDavid Majnemer const MachineBasicBlock *SuccessorColor = MBBI->getOperand(1).getMBB();
793e4f9b09bSDavid Majnemer CatchRetSuccessors.push_back(
794e4f9b09bSDavid Majnemer {Successor, IsSEH ? EntryBBNumber : SuccessorColor->getNumber()});
79516193552SDavid Majnemer }
79616193552SDavid Majnemer
79716193552SDavid Majnemer // We don't have anything to do if there aren't any EH pads.
798d69acf3bSHeejin Ahn if (EHScopeBlocks.empty())
799d69acf3bSHeejin Ahn return EHScopeMembership;
80016193552SDavid Majnemer
80116193552SDavid Majnemer // Identify all the basic blocks reachable from the function entry.
802d69acf3bSHeejin Ahn collectEHScopeMembers(EHScopeMembership, EntryBBNumber, &MF.front());
8031e4d3504SHeejin Ahn // All blocks not part of a scope are in the parent function.
804e4f9b09bSDavid Majnemer for (const MachineBasicBlock *MBB : UnreachableBlocks)
805d69acf3bSHeejin Ahn collectEHScopeMembers(EHScopeMembership, EntryBBNumber, MBB);
8061e4d3504SHeejin Ahn // Next, identify all the blocks inside the scopes.
807d69acf3bSHeejin Ahn for (const MachineBasicBlock *MBB : EHScopeBlocks)
808d69acf3bSHeejin Ahn collectEHScopeMembers(EHScopeMembership, MBB->getNumber(), MBB);
8091e4d3504SHeejin Ahn // SEH CatchPads aren't really scopes, handle them separately.
810e4f9b09bSDavid Majnemer for (const MachineBasicBlock *MBB : SEHCatchPads)
811d69acf3bSHeejin Ahn collectEHScopeMembers(EHScopeMembership, EntryBBNumber, MBB);
81216193552SDavid Majnemer // Finally, identify all the targets of a catchret.
81316193552SDavid Majnemer for (std::pair<const MachineBasicBlock *, int> CatchRetPair :
81416193552SDavid Majnemer CatchRetSuccessors)
815d69acf3bSHeejin Ahn collectEHScopeMembers(EHScopeMembership, CatchRetPair.second,
81616193552SDavid Majnemer CatchRetPair.first);
817d69acf3bSHeejin Ahn return EHScopeMembership;
81816193552SDavid Majnemer }
819