1dff0c46cSDimitry Andric //===-- Analysis.cpp - CodeGen LLVM IR Analysis Utilities -----------------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
1091bc56edSDimitry Andric // This file defines several CodeGen-specific LLVM IR analysis utilities.
11f22ef01cSRoman Divacky //
12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
13f22ef01cSRoman Divacky
14f22ef01cSRoman Divacky #include "llvm/CodeGen/Analysis.h"
15dff0c46cSDimitry Andric #include "llvm/Analysis/ValueTracking.h"
16f22ef01cSRoman Divacky #include "llvm/CodeGen/MachineFunction.h"
172cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
182cab237bSDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
192cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
20139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h"
21139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h"
22139f7f9bSDimitry Andric #include "llvm/IR/Function.h"
23139f7f9bSDimitry Andric #include "llvm/IR/Instructions.h"
24139f7f9bSDimitry Andric #include "llvm/IR/IntrinsicInst.h"
25139f7f9bSDimitry Andric #include "llvm/IR/LLVMContext.h"
26139f7f9bSDimitry Andric #include "llvm/IR/Module.h"
27f22ef01cSRoman Divacky #include "llvm/Support/ErrorHandling.h"
28f22ef01cSRoman Divacky #include "llvm/Support/MathExtras.h"
2939d628a0SDimitry Andric #include "llvm/Transforms/Utils/GlobalStatus.h"
3039d628a0SDimitry Andric
31f22ef01cSRoman Divacky using namespace llvm;
32f22ef01cSRoman Divacky
3339d628a0SDimitry Andric /// Compute the linearized index of a member in a nested aggregate/struct/array
3439d628a0SDimitry Andric /// by recursing and accumulating CurIndex as long as there are indices in the
3539d628a0SDimitry Andric /// index list.
ComputeLinearIndex(Type * Ty,const unsigned * Indices,const unsigned * IndicesEnd,unsigned CurIndex)366122f3e6SDimitry Andric unsigned llvm::ComputeLinearIndex(Type *Ty,
37f22ef01cSRoman Divacky const unsigned *Indices,
38f22ef01cSRoman Divacky const unsigned *IndicesEnd,
39f22ef01cSRoman Divacky unsigned CurIndex) {
40f22ef01cSRoman Divacky // Base case: We're done.
41f22ef01cSRoman Divacky if (Indices && Indices == IndicesEnd)
42f22ef01cSRoman Divacky return CurIndex;
43f22ef01cSRoman Divacky
44f22ef01cSRoman Divacky // Given a struct type, recursively traverse the elements.
456122f3e6SDimitry Andric if (StructType *STy = dyn_cast<StructType>(Ty)) {
46f22ef01cSRoman Divacky for (StructType::element_iterator EB = STy->element_begin(),
47f22ef01cSRoman Divacky EI = EB,
48f22ef01cSRoman Divacky EE = STy->element_end();
49f22ef01cSRoman Divacky EI != EE; ++EI) {
50f22ef01cSRoman Divacky if (Indices && *Indices == unsigned(EI - EB))
512754fe60SDimitry Andric return ComputeLinearIndex(*EI, Indices+1, IndicesEnd, CurIndex);
5291bc56edSDimitry Andric CurIndex = ComputeLinearIndex(*EI, nullptr, nullptr, CurIndex);
53f22ef01cSRoman Divacky }
5439d628a0SDimitry Andric assert(!Indices && "Unexpected out of bound");
55f22ef01cSRoman Divacky return CurIndex;
56f22ef01cSRoman Divacky }
57f22ef01cSRoman Divacky // Given an array type, recursively traverse the elements.
586122f3e6SDimitry Andric else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
596122f3e6SDimitry Andric Type *EltTy = ATy->getElementType();
6039d628a0SDimitry Andric unsigned NumElts = ATy->getNumElements();
6139d628a0SDimitry Andric // Compute the Linear offset when jumping one element of the array
6239d628a0SDimitry Andric unsigned EltLinearOffset = ComputeLinearIndex(EltTy, nullptr, nullptr, 0);
6339d628a0SDimitry Andric if (Indices) {
6439d628a0SDimitry Andric assert(*Indices < NumElts && "Unexpected out of bound");
6539d628a0SDimitry Andric // If the indice is inside the array, compute the index to the requested
6639d628a0SDimitry Andric // elt and recurse inside the element with the end of the indices list
6739d628a0SDimitry Andric CurIndex += EltLinearOffset* *Indices;
682754fe60SDimitry Andric return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex);
69f22ef01cSRoman Divacky }
7039d628a0SDimitry Andric CurIndex += EltLinearOffset*NumElts;
71f22ef01cSRoman Divacky return CurIndex;
72f22ef01cSRoman Divacky }
73f22ef01cSRoman Divacky // We haven't found the type we're looking for, so keep searching.
74f22ef01cSRoman Divacky return CurIndex + 1;
75f22ef01cSRoman Divacky }
76f22ef01cSRoman Divacky
77f22ef01cSRoman Divacky /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
78f22ef01cSRoman Divacky /// EVTs that represent all the individual underlying
79f22ef01cSRoman Divacky /// non-aggregate types that comprise it.
80f22ef01cSRoman Divacky ///
81f22ef01cSRoman Divacky /// If Offsets is non-null, it points to a vector to be filled in
82f22ef01cSRoman Divacky /// with the in-memory offsets of each of the individual values.
83f22ef01cSRoman Divacky ///
ComputeValueVTs(const TargetLowering & TLI,const DataLayout & DL,Type * Ty,SmallVectorImpl<EVT> & ValueVTs,SmallVectorImpl<uint64_t> * Offsets,uint64_t StartingOffset)84875ed548SDimitry Andric void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
85875ed548SDimitry Andric Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
86f22ef01cSRoman Divacky SmallVectorImpl<uint64_t> *Offsets,
87f22ef01cSRoman Divacky uint64_t StartingOffset) {
88f22ef01cSRoman Divacky // Given a struct type, recursively traverse the elements.
896122f3e6SDimitry Andric if (StructType *STy = dyn_cast<StructType>(Ty)) {
90875ed548SDimitry Andric const StructLayout *SL = DL.getStructLayout(STy);
91f22ef01cSRoman Divacky for (StructType::element_iterator EB = STy->element_begin(),
92f22ef01cSRoman Divacky EI = EB,
93f22ef01cSRoman Divacky EE = STy->element_end();
94f22ef01cSRoman Divacky EI != EE; ++EI)
95875ed548SDimitry Andric ComputeValueVTs(TLI, DL, *EI, ValueVTs, Offsets,
96f22ef01cSRoman Divacky StartingOffset + SL->getElementOffset(EI - EB));
97f22ef01cSRoman Divacky return;
98f22ef01cSRoman Divacky }
99f22ef01cSRoman Divacky // Given an array type, recursively traverse the elements.
1006122f3e6SDimitry Andric if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1016122f3e6SDimitry Andric Type *EltTy = ATy->getElementType();
102875ed548SDimitry Andric uint64_t EltSize = DL.getTypeAllocSize(EltTy);
103f22ef01cSRoman Divacky for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
104875ed548SDimitry Andric ComputeValueVTs(TLI, DL, EltTy, ValueVTs, Offsets,
105f22ef01cSRoman Divacky StartingOffset + i * EltSize);
106f22ef01cSRoman Divacky return;
107f22ef01cSRoman Divacky }
108f22ef01cSRoman Divacky // Interpret void as zero return values.
109f22ef01cSRoman Divacky if (Ty->isVoidTy())
110f22ef01cSRoman Divacky return;
111f22ef01cSRoman Divacky // Base case: we can get an EVT for this LLVM IR type.
112875ed548SDimitry Andric ValueVTs.push_back(TLI.getValueType(DL, Ty));
113f22ef01cSRoman Divacky if (Offsets)
114f22ef01cSRoman Divacky Offsets->push_back(StartingOffset);
115f22ef01cSRoman Divacky }
116f22ef01cSRoman Divacky
117f22ef01cSRoman Divacky /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
ExtractTypeInfo(Value * V)11839d628a0SDimitry Andric GlobalValue *llvm::ExtractTypeInfo(Value *V) {
119f22ef01cSRoman Divacky V = V->stripPointerCasts();
12039d628a0SDimitry Andric GlobalValue *GV = dyn_cast<GlobalValue>(V);
12139d628a0SDimitry Andric GlobalVariable *Var = dyn_cast<GlobalVariable>(V);
122f22ef01cSRoman Divacky
12339d628a0SDimitry Andric if (Var && Var->getName() == "llvm.eh.catch.all.value") {
12439d628a0SDimitry Andric assert(Var->hasInitializer() &&
125f22ef01cSRoman Divacky "The EH catch-all value must have an initializer");
12639d628a0SDimitry Andric Value *Init = Var->getInitializer();
12739d628a0SDimitry Andric GV = dyn_cast<GlobalValue>(Init);
128f22ef01cSRoman Divacky if (!GV) V = cast<ConstantPointerNull>(Init);
129f22ef01cSRoman Divacky }
130f22ef01cSRoman Divacky
131f22ef01cSRoman Divacky assert((GV || isa<ConstantPointerNull>(V)) &&
132f22ef01cSRoman Divacky "TypeInfo must be a global variable or NULL");
133f22ef01cSRoman Divacky return GV;
134f22ef01cSRoman Divacky }
135f22ef01cSRoman Divacky
136f22ef01cSRoman Divacky /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
137f22ef01cSRoman Divacky /// processed uses a memory 'm' constraint.
138f22ef01cSRoman Divacky bool
hasInlineAsmMemConstraint(InlineAsm::ConstraintInfoVector & CInfos,const TargetLowering & TLI)1392754fe60SDimitry Andric llvm::hasInlineAsmMemConstraint(InlineAsm::ConstraintInfoVector &CInfos,
140f22ef01cSRoman Divacky const TargetLowering &TLI) {
141f22ef01cSRoman Divacky for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
142f22ef01cSRoman Divacky InlineAsm::ConstraintInfo &CI = CInfos[i];
143f22ef01cSRoman Divacky for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
144f22ef01cSRoman Divacky TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
145f22ef01cSRoman Divacky if (CType == TargetLowering::C_Memory)
146f22ef01cSRoman Divacky return true;
147f22ef01cSRoman Divacky }
148f22ef01cSRoman Divacky
149f22ef01cSRoman Divacky // Indirect operand accesses access memory.
150f22ef01cSRoman Divacky if (CI.isIndirect)
151f22ef01cSRoman Divacky return true;
152f22ef01cSRoman Divacky }
153f22ef01cSRoman Divacky
154f22ef01cSRoman Divacky return false;
155f22ef01cSRoman Divacky }
156f22ef01cSRoman Divacky
157f22ef01cSRoman Divacky /// getFCmpCondCode - Return the ISD condition code corresponding to
158f22ef01cSRoman Divacky /// the given LLVM IR floating-point condition code. This includes
159f22ef01cSRoman Divacky /// consideration of global floating-point math flags.
160f22ef01cSRoman Divacky ///
getFCmpCondCode(FCmpInst::Predicate Pred)161f22ef01cSRoman Divacky ISD::CondCode llvm::getFCmpCondCode(FCmpInst::Predicate Pred) {
162f22ef01cSRoman Divacky switch (Pred) {
163dff0c46cSDimitry Andric case FCmpInst::FCMP_FALSE: return ISD::SETFALSE;
164dff0c46cSDimitry Andric case FCmpInst::FCMP_OEQ: return ISD::SETOEQ;
165dff0c46cSDimitry Andric case FCmpInst::FCMP_OGT: return ISD::SETOGT;
166dff0c46cSDimitry Andric case FCmpInst::FCMP_OGE: return ISD::SETOGE;
167dff0c46cSDimitry Andric case FCmpInst::FCMP_OLT: return ISD::SETOLT;
168dff0c46cSDimitry Andric case FCmpInst::FCMP_OLE: return ISD::SETOLE;
169dff0c46cSDimitry Andric case FCmpInst::FCMP_ONE: return ISD::SETONE;
170dff0c46cSDimitry Andric case FCmpInst::FCMP_ORD: return ISD::SETO;
171dff0c46cSDimitry Andric case FCmpInst::FCMP_UNO: return ISD::SETUO;
172dff0c46cSDimitry Andric case FCmpInst::FCMP_UEQ: return ISD::SETUEQ;
173dff0c46cSDimitry Andric case FCmpInst::FCMP_UGT: return ISD::SETUGT;
174dff0c46cSDimitry Andric case FCmpInst::FCMP_UGE: return ISD::SETUGE;
175dff0c46cSDimitry Andric case FCmpInst::FCMP_ULT: return ISD::SETULT;
176dff0c46cSDimitry Andric case FCmpInst::FCMP_ULE: return ISD::SETULE;
177dff0c46cSDimitry Andric case FCmpInst::FCMP_UNE: return ISD::SETUNE;
178dff0c46cSDimitry Andric case FCmpInst::FCMP_TRUE: return ISD::SETTRUE;
179dff0c46cSDimitry Andric default: llvm_unreachable("Invalid FCmp predicate opcode!");
180f22ef01cSRoman Divacky }
181dff0c46cSDimitry Andric }
182dff0c46cSDimitry Andric
getFCmpCodeWithoutNaN(ISD::CondCode CC)183dff0c46cSDimitry Andric ISD::CondCode llvm::getFCmpCodeWithoutNaN(ISD::CondCode CC) {
184dff0c46cSDimitry Andric switch (CC) {
185dff0c46cSDimitry Andric case ISD::SETOEQ: case ISD::SETUEQ: return ISD::SETEQ;
186dff0c46cSDimitry Andric case ISD::SETONE: case ISD::SETUNE: return ISD::SETNE;
187dff0c46cSDimitry Andric case ISD::SETOLT: case ISD::SETULT: return ISD::SETLT;
188dff0c46cSDimitry Andric case ISD::SETOLE: case ISD::SETULE: return ISD::SETLE;
189dff0c46cSDimitry Andric case ISD::SETOGT: case ISD::SETUGT: return ISD::SETGT;
190dff0c46cSDimitry Andric case ISD::SETOGE: case ISD::SETUGE: return ISD::SETGE;
191dff0c46cSDimitry Andric default: return CC;
192dff0c46cSDimitry Andric }
193f22ef01cSRoman Divacky }
194f22ef01cSRoman Divacky
195f22ef01cSRoman Divacky /// getICmpCondCode - Return the ISD condition code corresponding to
196f22ef01cSRoman Divacky /// the given LLVM IR integer condition code.
197f22ef01cSRoman Divacky ///
getICmpCondCode(ICmpInst::Predicate Pred)198f22ef01cSRoman Divacky ISD::CondCode llvm::getICmpCondCode(ICmpInst::Predicate Pred) {
199f22ef01cSRoman Divacky switch (Pred) {
200f22ef01cSRoman Divacky case ICmpInst::ICMP_EQ: return ISD::SETEQ;
201f22ef01cSRoman Divacky case ICmpInst::ICMP_NE: return ISD::SETNE;
202f22ef01cSRoman Divacky case ICmpInst::ICMP_SLE: return ISD::SETLE;
203f22ef01cSRoman Divacky case ICmpInst::ICMP_ULE: return ISD::SETULE;
204f22ef01cSRoman Divacky case ICmpInst::ICMP_SGE: return ISD::SETGE;
205f22ef01cSRoman Divacky case ICmpInst::ICMP_UGE: return ISD::SETUGE;
206f22ef01cSRoman Divacky case ICmpInst::ICMP_SLT: return ISD::SETLT;
207f22ef01cSRoman Divacky case ICmpInst::ICMP_ULT: return ISD::SETULT;
208f22ef01cSRoman Divacky case ICmpInst::ICMP_SGT: return ISD::SETGT;
209f22ef01cSRoman Divacky case ICmpInst::ICMP_UGT: return ISD::SETUGT;
210f22ef01cSRoman Divacky default:
211f22ef01cSRoman Divacky llvm_unreachable("Invalid ICmp predicate opcode!");
212f22ef01cSRoman Divacky }
213f22ef01cSRoman Divacky }
214f22ef01cSRoman Divacky
isNoopBitcast(Type * T1,Type * T2,const TargetLoweringBase & TLI)215284c1978SDimitry Andric static bool isNoopBitcast(Type *T1, Type *T2,
216f785676fSDimitry Andric const TargetLoweringBase& TLI) {
217284c1978SDimitry Andric return T1 == T2 || (T1->isPointerTy() && T2->isPointerTy()) ||
218284c1978SDimitry Andric (isa<VectorType>(T1) && isa<VectorType>(T2) &&
219284c1978SDimitry Andric TLI.isTypeLegal(EVT::getEVT(T1)) && TLI.isTypeLegal(EVT::getEVT(T2)));
220284c1978SDimitry Andric }
2217ae0e2c9SDimitry Andric
222f785676fSDimitry Andric /// Look through operations that will be free to find the earliest source of
223f785676fSDimitry Andric /// this value.
224f785676fSDimitry Andric ///
225f785676fSDimitry Andric /// @param ValLoc If V has aggegate type, we will be interested in a particular
226f785676fSDimitry Andric /// scalar component. This records its address; the reverse of this list gives a
227f785676fSDimitry Andric /// sequence of indices appropriate for an extractvalue to locate the important
228f785676fSDimitry Andric /// value. This value is updated during the function and on exit will indicate
229f785676fSDimitry Andric /// similar information for the Value returned.
230f785676fSDimitry Andric ///
231f785676fSDimitry Andric /// @param DataBits If this function looks through truncate instructions, this
232f785676fSDimitry Andric /// will record the smallest size attained.
getNoopInput(const Value * V,SmallVectorImpl<unsigned> & ValLoc,unsigned & DataBits,const TargetLoweringBase & TLI,const DataLayout & DL)233f785676fSDimitry Andric static const Value *getNoopInput(const Value *V,
234f785676fSDimitry Andric SmallVectorImpl<unsigned> &ValLoc,
235f785676fSDimitry Andric unsigned &DataBits,
236875ed548SDimitry Andric const TargetLoweringBase &TLI,
237875ed548SDimitry Andric const DataLayout &DL) {
238284c1978SDimitry Andric while (true) {
239284c1978SDimitry Andric // Try to look through V1; if V1 is not an instruction, it can't be looked
240284c1978SDimitry Andric // through.
241f785676fSDimitry Andric const Instruction *I = dyn_cast<Instruction>(V);
242f785676fSDimitry Andric if (!I || I->getNumOperands() == 0) return V;
24391bc56edSDimitry Andric const Value *NoopInput = nullptr;
244f785676fSDimitry Andric
2457ae0e2c9SDimitry Andric Value *Op = I->getOperand(0);
246f785676fSDimitry Andric if (isa<BitCastInst>(I)) {
2477ae0e2c9SDimitry Andric // Look through truly no-op bitcasts.
248284c1978SDimitry Andric if (isNoopBitcast(Op->getType(), I->getType(), TLI))
249284c1978SDimitry Andric NoopInput = Op;
250284c1978SDimitry Andric } else if (isa<GetElementPtrInst>(I)) {
251284c1978SDimitry Andric // Look through getelementptr
252284c1978SDimitry Andric if (cast<GetElementPtrInst>(I)->hasAllZeroIndices())
253284c1978SDimitry Andric NoopInput = Op;
254284c1978SDimitry Andric } else if (isa<IntToPtrInst>(I)) {
2557ae0e2c9SDimitry Andric // Look through inttoptr.
256284c1978SDimitry Andric // Make sure this isn't a truncating or extending cast. We could
257284c1978SDimitry Andric // support this eventually, but don't bother for now.
258284c1978SDimitry Andric if (!isa<VectorType>(I->getType()) &&
259875ed548SDimitry Andric DL.getPointerSizeInBits() ==
2607ae0e2c9SDimitry Andric cast<IntegerType>(Op->getType())->getBitWidth())
261284c1978SDimitry Andric NoopInput = Op;
262284c1978SDimitry Andric } else if (isa<PtrToIntInst>(I)) {
2637ae0e2c9SDimitry Andric // Look through ptrtoint.
264284c1978SDimitry Andric // Make sure this isn't a truncating or extending cast. We could
265284c1978SDimitry Andric // support this eventually, but don't bother for now.
266284c1978SDimitry Andric if (!isa<VectorType>(I->getType()) &&
267875ed548SDimitry Andric DL.getPointerSizeInBits() ==
2687ae0e2c9SDimitry Andric cast<IntegerType>(I->getType())->getBitWidth())
269284c1978SDimitry Andric NoopInput = Op;
270f785676fSDimitry Andric } else if (isa<TruncInst>(I) &&
271f785676fSDimitry Andric TLI.allowTruncateForTailCall(Op->getType(), I->getType())) {
272f785676fSDimitry Andric DataBits = std::min(DataBits, I->getType()->getPrimitiveSizeInBits());
273f785676fSDimitry Andric NoopInput = Op;
2748e0f8b8cSDimitry Andric } else if (auto CS = ImmutableCallSite(I)) {
2758e0f8b8cSDimitry Andric const Value *ReturnedOp = CS.getReturnedArgOperand();
2768e0f8b8cSDimitry Andric if (ReturnedOp && isNoopBitcast(ReturnedOp->getType(), I->getType(), TLI))
2778e0f8b8cSDimitry Andric NoopInput = ReturnedOp;
278f785676fSDimitry Andric } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(V)) {
279f785676fSDimitry Andric // Value may come from either the aggregate or the scalar
280f785676fSDimitry Andric ArrayRef<unsigned> InsertLoc = IVI->getIndices();
281ff0cc061SDimitry Andric if (ValLoc.size() >= InsertLoc.size() &&
282ff0cc061SDimitry Andric std::equal(InsertLoc.begin(), InsertLoc.end(), ValLoc.rbegin())) {
283f785676fSDimitry Andric // The type being inserted is a nested sub-type of the aggregate; we
284f785676fSDimitry Andric // have to remove those initial indices to get the location we're
285f785676fSDimitry Andric // interested in for the operand.
286f785676fSDimitry Andric ValLoc.resize(ValLoc.size() - InsertLoc.size());
287f785676fSDimitry Andric NoopInput = IVI->getInsertedValueOperand();
288f785676fSDimitry Andric } else {
289f785676fSDimitry Andric // The struct we're inserting into has the value we're interested in, no
290f785676fSDimitry Andric // change of address.
291f785676fSDimitry Andric NoopInput = Op;
292f785676fSDimitry Andric }
293f785676fSDimitry Andric } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {
294f785676fSDimitry Andric // The part we're interested in will inevitably be some sub-section of the
295f785676fSDimitry Andric // previous aggregate. Combine the two paths to obtain the true address of
296f785676fSDimitry Andric // our element.
297f785676fSDimitry Andric ArrayRef<unsigned> ExtractLoc = EVI->getIndices();
298ff0cc061SDimitry Andric ValLoc.append(ExtractLoc.rbegin(), ExtractLoc.rend());
299f785676fSDimitry Andric NoopInput = Op;
300f785676fSDimitry Andric }
301f785676fSDimitry Andric // Terminate if we couldn't find anything to look through.
302f785676fSDimitry Andric if (!NoopInput)
303f785676fSDimitry Andric return V;
304f785676fSDimitry Andric
305f785676fSDimitry Andric V = NoopInput;
306284c1978SDimitry Andric }
3077ae0e2c9SDimitry Andric }
3087ae0e2c9SDimitry Andric
309f785676fSDimitry Andric /// Return true if this scalar return value only has bits discarded on its path
310f785676fSDimitry Andric /// from the "tail call" to the "ret". This includes the obvious noop
311f785676fSDimitry Andric /// instructions handled by getNoopInput above as well as free truncations (or
312f785676fSDimitry Andric /// 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)313f785676fSDimitry Andric static bool slotOnlyDiscardsData(const Value *RetVal, const Value *CallVal,
314f785676fSDimitry Andric SmallVectorImpl<unsigned> &RetIndices,
315f785676fSDimitry Andric SmallVectorImpl<unsigned> &CallIndices,
316f785676fSDimitry Andric bool AllowDifferingSizes,
317875ed548SDimitry Andric const TargetLoweringBase &TLI,
318875ed548SDimitry Andric const DataLayout &DL) {
3197ae0e2c9SDimitry Andric
320f785676fSDimitry Andric // Trace the sub-value needed by the return value as far back up the graph as
321f785676fSDimitry Andric // possible, in the hope that it will intersect with the value produced by the
322f785676fSDimitry Andric // call. In the simple case with no "returned" attribute, the hope is actually
323f785676fSDimitry Andric // that we end up back at the tail call instruction itself.
324f785676fSDimitry Andric unsigned BitsRequired = UINT_MAX;
325875ed548SDimitry Andric RetVal = getNoopInput(RetVal, RetIndices, BitsRequired, TLI, DL);
326284c1978SDimitry Andric
327f785676fSDimitry Andric // If this slot in the value returned is undef, it doesn't matter what the
328f785676fSDimitry Andric // call puts there, it'll be fine.
329f785676fSDimitry Andric if (isa<UndefValue>(RetVal))
330f785676fSDimitry Andric return true;
331284c1978SDimitry Andric
332f785676fSDimitry Andric // Now do a similar search up through the graph to find where the value
333f785676fSDimitry Andric // actually returned by the "tail call" comes from. In the simple case without
334f785676fSDimitry Andric // a "returned" attribute, the search will be blocked immediately and the loop
335f785676fSDimitry Andric // a Noop.
336f785676fSDimitry Andric unsigned BitsProvided = UINT_MAX;
337875ed548SDimitry Andric CallVal = getNoopInput(CallVal, CallIndices, BitsProvided, TLI, DL);
338f785676fSDimitry Andric
339f785676fSDimitry Andric // There's no hope if we can't actually trace them to (the same part of!) the
340f785676fSDimitry Andric // same value.
341f785676fSDimitry Andric if (CallVal != RetVal || CallIndices != RetIndices)
342f785676fSDimitry Andric return false;
343f785676fSDimitry Andric
344f785676fSDimitry Andric // However, intervening truncates may have made the call non-tail. Make sure
345f785676fSDimitry Andric // all the bits that are needed by the "ret" have been provided by the "tail
346f785676fSDimitry Andric // call". FIXME: with sufficiently cunning bit-tracking, we could look through
347f785676fSDimitry Andric // extensions too.
348f785676fSDimitry Andric if (BitsProvided < BitsRequired ||
349f785676fSDimitry Andric (!AllowDifferingSizes && BitsProvided != BitsRequired))
350f785676fSDimitry Andric return false;
351f785676fSDimitry Andric
352284c1978SDimitry Andric return true;
353284c1978SDimitry Andric }
354f785676fSDimitry Andric
355f785676fSDimitry Andric /// For an aggregate type, determine whether a given index is within bounds or
356f785676fSDimitry Andric /// not.
indexReallyValid(CompositeType * T,unsigned Idx)357f785676fSDimitry Andric static bool indexReallyValid(CompositeType *T, unsigned Idx) {
358f785676fSDimitry Andric if (ArrayType *AT = dyn_cast<ArrayType>(T))
359f785676fSDimitry Andric return Idx < AT->getNumElements();
360f785676fSDimitry Andric
361f785676fSDimitry Andric return Idx < cast<StructType>(T)->getNumElements();
362284c1978SDimitry Andric }
363f785676fSDimitry Andric
364f785676fSDimitry Andric /// Move the given iterators to the next leaf type in depth first traversal.
365f785676fSDimitry Andric ///
366f785676fSDimitry Andric /// Performs a depth-first traversal of the type as specified by its arguments,
367f785676fSDimitry Andric /// stopping at the next leaf node (which may be a legitimate scalar type or an
368f785676fSDimitry Andric /// empty struct or array).
369f785676fSDimitry Andric ///
370f785676fSDimitry Andric /// @param SubTypes List of the partial components making up the type from
371f785676fSDimitry Andric /// outermost to innermost non-empty aggregate. The element currently
372f785676fSDimitry Andric /// represented is SubTypes.back()->getTypeAtIndex(Path.back() - 1).
373f785676fSDimitry Andric ///
374f785676fSDimitry Andric /// @param Path Set of extractvalue indices leading from the outermost type
375f785676fSDimitry Andric /// (SubTypes[0]) to the leaf node currently represented.
376f785676fSDimitry Andric ///
377f785676fSDimitry Andric /// @returns true if a new type was found, false otherwise. Calling this
378f785676fSDimitry Andric /// function again on a finished iterator will repeatedly return
379f785676fSDimitry Andric /// false. SubTypes.back()->getTypeAtIndex(Path.back()) is either an empty
380f785676fSDimitry Andric /// aggregate or a non-aggregate
advanceToNextLeafType(SmallVectorImpl<CompositeType * > & SubTypes,SmallVectorImpl<unsigned> & Path)381f785676fSDimitry Andric static bool advanceToNextLeafType(SmallVectorImpl<CompositeType *> &SubTypes,
382f785676fSDimitry Andric SmallVectorImpl<unsigned> &Path) {
383f785676fSDimitry Andric // First march back up the tree until we can successfully increment one of the
384f785676fSDimitry Andric // coordinates in Path.
385f785676fSDimitry Andric while (!Path.empty() && !indexReallyValid(SubTypes.back(), Path.back() + 1)) {
386f785676fSDimitry Andric Path.pop_back();
387f785676fSDimitry Andric SubTypes.pop_back();
388f785676fSDimitry Andric }
389f785676fSDimitry Andric
390f785676fSDimitry Andric // If we reached the top, then the iterator is done.
391f785676fSDimitry Andric if (Path.empty())
392f785676fSDimitry Andric return false;
393f785676fSDimitry Andric
394f785676fSDimitry Andric // We know there's *some* valid leaf now, so march back down the tree picking
395f785676fSDimitry Andric // out the left-most element at each node.
396f785676fSDimitry Andric ++Path.back();
397f785676fSDimitry Andric Type *DeeperType = SubTypes.back()->getTypeAtIndex(Path.back());
398f785676fSDimitry Andric while (DeeperType->isAggregateType()) {
399f785676fSDimitry Andric CompositeType *CT = cast<CompositeType>(DeeperType);
400f785676fSDimitry Andric if (!indexReallyValid(CT, 0))
401f785676fSDimitry Andric return true;
402f785676fSDimitry Andric
403f785676fSDimitry Andric SubTypes.push_back(CT);
404f785676fSDimitry Andric Path.push_back(0);
405f785676fSDimitry Andric
406f785676fSDimitry Andric DeeperType = CT->getTypeAtIndex(0U);
407f785676fSDimitry Andric }
408f785676fSDimitry Andric
409284c1978SDimitry Andric return true;
410284c1978SDimitry Andric }
411f785676fSDimitry Andric
412f785676fSDimitry Andric /// Find the first non-empty, scalar-like type in Next and setup the iterator
413f785676fSDimitry Andric /// components.
414f785676fSDimitry Andric ///
415f785676fSDimitry Andric /// Assuming Next is an aggregate of some kind, this function will traverse the
416f785676fSDimitry Andric /// tree from left to right (i.e. depth-first) looking for the first
417f785676fSDimitry Andric /// non-aggregate type which will play a role in function return.
418f785676fSDimitry Andric ///
419f785676fSDimitry Andric /// For example, if Next was {[0 x i64], {{}, i32, {}}, i32} then we would setup
420f785676fSDimitry Andric /// Path as [1, 1] and SubTypes as [Next, {{}, i32, {}}] to represent the first
421f785676fSDimitry Andric /// i32 in that type.
firstRealType(Type * Next,SmallVectorImpl<CompositeType * > & SubTypes,SmallVectorImpl<unsigned> & Path)422f785676fSDimitry Andric static bool firstRealType(Type *Next,
423f785676fSDimitry Andric SmallVectorImpl<CompositeType *> &SubTypes,
424f785676fSDimitry Andric SmallVectorImpl<unsigned> &Path) {
425f785676fSDimitry Andric // First initialise the iterator components to the first "leaf" node
426f785676fSDimitry Andric // (i.e. node with no valid sub-type at any index, so {} does count as a leaf
427f785676fSDimitry Andric // despite nominally being an aggregate).
428f785676fSDimitry Andric while (Next->isAggregateType() &&
429f785676fSDimitry Andric indexReallyValid(cast<CompositeType>(Next), 0)) {
430f785676fSDimitry Andric SubTypes.push_back(cast<CompositeType>(Next));
431f785676fSDimitry Andric Path.push_back(0);
432f785676fSDimitry Andric Next = cast<CompositeType>(Next)->getTypeAtIndex(0U);
433284c1978SDimitry Andric }
434284c1978SDimitry Andric
435f785676fSDimitry Andric // If there's no Path now, Next was originally scalar already (or empty
436f785676fSDimitry Andric // leaf). We're done.
437f785676fSDimitry Andric if (Path.empty())
438f785676fSDimitry Andric return true;
439284c1978SDimitry Andric
440f785676fSDimitry Andric // Otherwise, use normal iteration to keep looking through the tree until we
441f785676fSDimitry Andric // find a non-aggregate type.
442f785676fSDimitry Andric while (SubTypes.back()->getTypeAtIndex(Path.back())->isAggregateType()) {
443f785676fSDimitry Andric if (!advanceToNextLeafType(SubTypes, Path))
444284c1978SDimitry Andric return false;
445284c1978SDimitry Andric }
4467ae0e2c9SDimitry Andric
447f785676fSDimitry Andric return true;
448f785676fSDimitry Andric }
449f785676fSDimitry Andric
450f785676fSDimitry Andric /// Set the iterator data-structures to the next non-empty, non-aggregate
451f785676fSDimitry Andric /// subtype.
nextRealType(SmallVectorImpl<CompositeType * > & SubTypes,SmallVectorImpl<unsigned> & Path)452f785676fSDimitry Andric static bool nextRealType(SmallVectorImpl<CompositeType *> &SubTypes,
453f785676fSDimitry Andric SmallVectorImpl<unsigned> &Path) {
454f785676fSDimitry Andric do {
455f785676fSDimitry Andric if (!advanceToNextLeafType(SubTypes, Path))
456f785676fSDimitry Andric return false;
457f785676fSDimitry Andric
458f785676fSDimitry Andric assert(!Path.empty() && "found a leaf but didn't set the path?");
459f785676fSDimitry Andric } while (SubTypes.back()->getTypeAtIndex(Path.back())->isAggregateType());
460f785676fSDimitry Andric
461f785676fSDimitry Andric return true;
462f785676fSDimitry Andric }
463f785676fSDimitry Andric
464f785676fSDimitry Andric
465f22ef01cSRoman Divacky /// Test if the given instruction is in a position to be optimized
466f22ef01cSRoman Divacky /// with a tail-call. This roughly means that it's in a block with
467f22ef01cSRoman Divacky /// a return and there's nothing that needs to be scheduled
468f22ef01cSRoman Divacky /// between it and the return.
469f22ef01cSRoman Divacky ///
470f22ef01cSRoman Divacky /// This function only tests target-independent requirements.
isInTailCallPosition(ImmutableCallSite CS,const TargetMachine & TM)47191bc56edSDimitry Andric bool llvm::isInTailCallPosition(ImmutableCallSite CS, const TargetMachine &TM) {
472f22ef01cSRoman Divacky const Instruction *I = CS.getInstruction();
473f22ef01cSRoman Divacky const BasicBlock *ExitBB = I->getParent();
474*b5893f02SDimitry Andric const Instruction *Term = ExitBB->getTerminator();
475f22ef01cSRoman Divacky const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
476f22ef01cSRoman Divacky
477f22ef01cSRoman Divacky // The block must end in a return statement or unreachable.
478f22ef01cSRoman Divacky //
479f22ef01cSRoman Divacky // FIXME: Decline tailcall if it's not guaranteed and if the block ends in
480f22ef01cSRoman Divacky // an unreachable, for now. The way tailcall optimization is currently
481f22ef01cSRoman Divacky // implemented means it will add an epilogue followed by a jump. That is
482f22ef01cSRoman Divacky // not profitable. Also, if the callee is a special function (e.g.
483f22ef01cSRoman Divacky // longjmp on x86), it can end up causing miscompilation that has not
484f22ef01cSRoman Divacky // been fully understood.
485f22ef01cSRoman Divacky if (!Ret &&
48691bc56edSDimitry Andric (!TM.Options.GuaranteedTailCallOpt || !isa<UnreachableInst>(Term)))
4877ae0e2c9SDimitry Andric return false;
488f22ef01cSRoman Divacky
489f22ef01cSRoman Divacky // If I will have a chain, make sure no other instruction that will have a
490f22ef01cSRoman Divacky // chain interposes between I and the return.
491f22ef01cSRoman Divacky if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
492dff0c46cSDimitry Andric !isSafeToSpeculativelyExecute(I))
49391bc56edSDimitry Andric for (BasicBlock::const_iterator BBI = std::prev(ExitBB->end(), 2);; --BBI) {
494f22ef01cSRoman Divacky if (&*BBI == I)
495f22ef01cSRoman Divacky break;
496f22ef01cSRoman Divacky // Debug info intrinsics do not get in the way of tail call optimization.
497f22ef01cSRoman Divacky if (isa<DbgInfoIntrinsic>(BBI))
498f22ef01cSRoman Divacky continue;
499*b5893f02SDimitry Andric // A lifetime end intrinsic should not stop tail call optimization.
500*b5893f02SDimitry Andric if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(BBI))
501*b5893f02SDimitry Andric if (II->getIntrinsicID() == Intrinsic::lifetime_end)
502*b5893f02SDimitry Andric continue;
503f22ef01cSRoman Divacky if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
5047d523365SDimitry Andric !isSafeToSpeculativelyExecute(&*BBI))
505f22ef01cSRoman Divacky return false;
506f22ef01cSRoman Divacky }
507f22ef01cSRoman Divacky
508ff0cc061SDimitry Andric const Function *F = ExitBB->getParent();
50939d628a0SDimitry Andric return returnTypeIsEligibleForTailCall(
510ff0cc061SDimitry Andric F, I, Ret, *TM.getSubtargetImpl(*F)->getTargetLowering());
511f785676fSDimitry Andric }
512f785676fSDimitry Andric
attributesPermitTailCall(const Function * F,const Instruction * I,const ReturnInst * Ret,const TargetLoweringBase & TLI,bool * AllowDifferingSizes)513d88c1a5aSDimitry Andric bool llvm::attributesPermitTailCall(const Function *F, const Instruction *I,
514d88c1a5aSDimitry Andric const ReturnInst *Ret,
515d88c1a5aSDimitry Andric const TargetLoweringBase &TLI,
516d88c1a5aSDimitry Andric bool *AllowDifferingSizes) {
517d88c1a5aSDimitry Andric // ADS may be null, so don't write to it directly.
518d88c1a5aSDimitry Andric bool DummyADS;
519d88c1a5aSDimitry Andric bool &ADS = AllowDifferingSizes ? *AllowDifferingSizes : DummyADS;
520d88c1a5aSDimitry Andric ADS = true;
521d88c1a5aSDimitry Andric
5227a7e6055SDimitry Andric AttrBuilder CallerAttrs(F->getAttributes(), AttributeList::ReturnIndex);
523d88c1a5aSDimitry Andric AttrBuilder CalleeAttrs(cast<CallInst>(I)->getAttributes(),
5247a7e6055SDimitry Andric AttributeList::ReturnIndex);
525d88c1a5aSDimitry Andric
526*b5893f02SDimitry Andric // NoAlias and NonNull are completely benign as far as calling convention
527*b5893f02SDimitry Andric // goes, they shouldn't affect whether the call is a tail call.
528d88c1a5aSDimitry Andric CallerAttrs.removeAttribute(Attribute::NoAlias);
529d88c1a5aSDimitry Andric CalleeAttrs.removeAttribute(Attribute::NoAlias);
530*b5893f02SDimitry Andric CallerAttrs.removeAttribute(Attribute::NonNull);
531*b5893f02SDimitry Andric CalleeAttrs.removeAttribute(Attribute::NonNull);
532d88c1a5aSDimitry Andric
533d88c1a5aSDimitry Andric if (CallerAttrs.contains(Attribute::ZExt)) {
534d88c1a5aSDimitry Andric if (!CalleeAttrs.contains(Attribute::ZExt))
535d88c1a5aSDimitry Andric return false;
536d88c1a5aSDimitry Andric
537d88c1a5aSDimitry Andric ADS = false;
538d88c1a5aSDimitry Andric CallerAttrs.removeAttribute(Attribute::ZExt);
539d88c1a5aSDimitry Andric CalleeAttrs.removeAttribute(Attribute::ZExt);
540d88c1a5aSDimitry Andric } else if (CallerAttrs.contains(Attribute::SExt)) {
541d88c1a5aSDimitry Andric if (!CalleeAttrs.contains(Attribute::SExt))
542d88c1a5aSDimitry Andric return false;
543d88c1a5aSDimitry Andric
544d88c1a5aSDimitry Andric ADS = false;
545d88c1a5aSDimitry Andric CallerAttrs.removeAttribute(Attribute::SExt);
546d88c1a5aSDimitry Andric CalleeAttrs.removeAttribute(Attribute::SExt);
547d88c1a5aSDimitry Andric }
548d88c1a5aSDimitry Andric
549*b5893f02SDimitry Andric // Drop sext and zext return attributes if the result is not used.
550*b5893f02SDimitry Andric // This enables tail calls for code like:
551*b5893f02SDimitry Andric //
552*b5893f02SDimitry Andric // define void @caller() {
553*b5893f02SDimitry Andric // entry:
554*b5893f02SDimitry Andric // %unused_result = tail call zeroext i1 @callee()
555*b5893f02SDimitry Andric // br label %retlabel
556*b5893f02SDimitry Andric // retlabel:
557*b5893f02SDimitry Andric // ret void
558*b5893f02SDimitry Andric // }
559*b5893f02SDimitry Andric if (I->use_empty()) {
560*b5893f02SDimitry Andric CalleeAttrs.removeAttribute(Attribute::SExt);
561*b5893f02SDimitry Andric CalleeAttrs.removeAttribute(Attribute::ZExt);
562*b5893f02SDimitry Andric }
563*b5893f02SDimitry Andric
564d88c1a5aSDimitry Andric // If they're still different, there's some facet we don't understand
565d88c1a5aSDimitry Andric // (currently only "inreg", but in future who knows). It may be OK but the
566d88c1a5aSDimitry Andric // only safe option is to reject the tail call.
567d88c1a5aSDimitry Andric return CallerAttrs == CalleeAttrs;
568d88c1a5aSDimitry Andric }
569d88c1a5aSDimitry Andric
returnTypeIsEligibleForTailCall(const Function * F,const Instruction * I,const ReturnInst * Ret,const TargetLoweringBase & TLI)570f785676fSDimitry Andric bool llvm::returnTypeIsEligibleForTailCall(const Function *F,
571f785676fSDimitry Andric const Instruction *I,
572f785676fSDimitry Andric const ReturnInst *Ret,
573f785676fSDimitry Andric const TargetLoweringBase &TLI) {
574f22ef01cSRoman Divacky // If the block ends with a void return or unreachable, it doesn't matter
575f22ef01cSRoman Divacky // what the call's return type is.
576f22ef01cSRoman Divacky if (!Ret || Ret->getNumOperands() == 0) return true;
577f22ef01cSRoman Divacky
578f22ef01cSRoman Divacky // If the return value is undef, it doesn't matter what the call's
579f22ef01cSRoman Divacky // return type is.
580f22ef01cSRoman Divacky if (isa<UndefValue>(Ret->getOperand(0))) return true;
581f22ef01cSRoman Divacky
582f785676fSDimitry Andric // Make sure the attributes attached to each return are compatible.
583d88c1a5aSDimitry Andric bool AllowDifferingSizes;
584d88c1a5aSDimitry Andric if (!attributesPermitTailCall(F, I, Ret, TLI, &AllowDifferingSizes))
585f785676fSDimitry Andric return false;
586f785676fSDimitry Andric
587f785676fSDimitry Andric const Value *RetVal = Ret->getOperand(0), *CallVal = I;
5882cab237bSDimitry Andric // Intrinsic like llvm.memcpy has no return value, but the expanded
5892cab237bSDimitry Andric // libcall may or may not have return value. On most platforms, it
5902cab237bSDimitry Andric // will be expanded as memcpy in libc, which returns the first
5912cab237bSDimitry Andric // argument. On other platforms like arm-none-eabi, memcpy may be
5922cab237bSDimitry Andric // expanded as library call without return value, like __aeabi_memcpy.
5932cab237bSDimitry Andric const CallInst *Call = cast<CallInst>(I);
5942cab237bSDimitry Andric if (Function *F = Call->getCalledFunction()) {
5952cab237bSDimitry Andric Intrinsic::ID IID = F->getIntrinsicID();
5962cab237bSDimitry Andric if (((IID == Intrinsic::memcpy &&
5972cab237bSDimitry Andric TLI.getLibcallName(RTLIB::MEMCPY) == StringRef("memcpy")) ||
5982cab237bSDimitry Andric (IID == Intrinsic::memmove &&
5992cab237bSDimitry Andric TLI.getLibcallName(RTLIB::MEMMOVE) == StringRef("memmove")) ||
6002cab237bSDimitry Andric (IID == Intrinsic::memset &&
6012cab237bSDimitry Andric TLI.getLibcallName(RTLIB::MEMSET) == StringRef("memset"))) &&
6022cab237bSDimitry Andric RetVal == Call->getArgOperand(0))
6032cab237bSDimitry Andric return true;
6042cab237bSDimitry Andric }
6052cab237bSDimitry Andric
606f785676fSDimitry Andric SmallVector<unsigned, 4> RetPath, CallPath;
607f785676fSDimitry Andric SmallVector<CompositeType *, 4> RetSubTypes, CallSubTypes;
608f785676fSDimitry Andric
609f785676fSDimitry Andric bool RetEmpty = !firstRealType(RetVal->getType(), RetSubTypes, RetPath);
610f785676fSDimitry Andric bool CallEmpty = !firstRealType(CallVal->getType(), CallSubTypes, CallPath);
611f785676fSDimitry Andric
612f785676fSDimitry Andric // Nothing's actually returned, it doesn't matter what the callee put there
613f785676fSDimitry Andric // it's a valid tail call.
614f785676fSDimitry Andric if (RetEmpty)
615f785676fSDimitry Andric return true;
616f785676fSDimitry Andric
617f785676fSDimitry Andric // Iterate pairwise through each of the value types making up the tail call
618f785676fSDimitry Andric // and the corresponding return. For each one we want to know whether it's
619f785676fSDimitry Andric // essentially going directly from the tail call to the ret, via operations
620f785676fSDimitry Andric // that end up not generating any code.
621f785676fSDimitry Andric //
622f785676fSDimitry Andric // We allow a certain amount of covariance here. For example it's permitted
623f785676fSDimitry Andric // for the tail call to define more bits than the ret actually cares about
624f785676fSDimitry Andric // (e.g. via a truncate).
625f785676fSDimitry Andric do {
626f785676fSDimitry Andric if (CallEmpty) {
627f785676fSDimitry Andric // We've exhausted the values produced by the tail call instruction, the
628f785676fSDimitry Andric // rest are essentially undef. The type doesn't really matter, but we need
629f785676fSDimitry Andric // *something*.
630f785676fSDimitry Andric Type *SlotType = RetSubTypes.back()->getTypeAtIndex(RetPath.back());
631f785676fSDimitry Andric CallVal = UndefValue::get(SlotType);
632f785676fSDimitry Andric }
633f785676fSDimitry Andric
634f785676fSDimitry Andric // The manipulations performed when we're looking through an insertvalue or
635f785676fSDimitry Andric // an extractvalue would happen at the front of the RetPath list, so since
636f785676fSDimitry Andric // we have to copy it anyway it's more efficient to create a reversed copy.
637ff0cc061SDimitry Andric SmallVector<unsigned, 4> TmpRetPath(RetPath.rbegin(), RetPath.rend());
638ff0cc061SDimitry Andric SmallVector<unsigned, 4> TmpCallPath(CallPath.rbegin(), CallPath.rend());
639f785676fSDimitry Andric
640f785676fSDimitry Andric // Finally, we can check whether the value produced by the tail call at this
641f785676fSDimitry Andric // index is compatible with the value we return.
642f785676fSDimitry Andric if (!slotOnlyDiscardsData(RetVal, CallVal, TmpRetPath, TmpCallPath,
643875ed548SDimitry Andric AllowDifferingSizes, TLI,
644875ed548SDimitry Andric F->getParent()->getDataLayout()))
645f785676fSDimitry Andric return false;
646f785676fSDimitry Andric
647f785676fSDimitry Andric CallEmpty = !nextRealType(CallSubTypes, CallPath);
648f785676fSDimitry Andric } while(nextRealType(RetSubTypes, RetPath));
649f785676fSDimitry Andric
650f785676fSDimitry Andric return true;
651f22ef01cSRoman Divacky }
65239d628a0SDimitry Andric
collectEHScopeMembers(DenseMap<const MachineBasicBlock *,int> & EHScopeMembership,int EHScope,const MachineBasicBlock * MBB)6534ba319b5SDimitry Andric static void collectEHScopeMembers(
6544ba319b5SDimitry Andric DenseMap<const MachineBasicBlock *, int> &EHScopeMembership, int EHScope,
6557d523365SDimitry Andric const MachineBasicBlock *MBB) {
6563ca95b02SDimitry Andric SmallVector<const MachineBasicBlock *, 16> Worklist = {MBB};
6573ca95b02SDimitry Andric while (!Worklist.empty()) {
6583ca95b02SDimitry Andric const MachineBasicBlock *Visiting = Worklist.pop_back_val();
6594ba319b5SDimitry Andric // Don't follow blocks which start new scopes.
6603ca95b02SDimitry Andric if (Visiting->isEHPad() && Visiting != MBB)
6613ca95b02SDimitry Andric continue;
6623ca95b02SDimitry Andric
6634ba319b5SDimitry Andric // Add this MBB to our scope.
6644ba319b5SDimitry Andric auto P = EHScopeMembership.insert(std::make_pair(Visiting, EHScope));
6657d523365SDimitry Andric
6667d523365SDimitry Andric // Don't revisit blocks.
6677d523365SDimitry Andric if (!P.second) {
6684ba319b5SDimitry Andric assert(P.first->second == EHScope && "MBB is part of two scopes!");
6693ca95b02SDimitry Andric continue;
6707d523365SDimitry Andric }
6717d523365SDimitry Andric
6724ba319b5SDimitry Andric // Returns are boundaries where scope transfer can occur, don't follow
6737d523365SDimitry Andric // successors.
674*b5893f02SDimitry Andric if (Visiting->isEHScopeReturnBlock())
6753ca95b02SDimitry Andric continue;
6767d523365SDimitry Andric
6773ca95b02SDimitry Andric for (const MachineBasicBlock *Succ : Visiting->successors())
6783ca95b02SDimitry Andric Worklist.push_back(Succ);
6793ca95b02SDimitry Andric }
6807d523365SDimitry Andric }
6817d523365SDimitry Andric
6827d523365SDimitry Andric DenseMap<const MachineBasicBlock *, int>
getEHScopeMembership(const MachineFunction & MF)6834ba319b5SDimitry Andric llvm::getEHScopeMembership(const MachineFunction &MF) {
6844ba319b5SDimitry Andric DenseMap<const MachineBasicBlock *, int> EHScopeMembership;
6857d523365SDimitry Andric
6867d523365SDimitry Andric // We don't have anything to do if there aren't any EH pads.
6874ba319b5SDimitry Andric if (!MF.hasEHScopes())
6884ba319b5SDimitry Andric return EHScopeMembership;
6897d523365SDimitry Andric
6907d523365SDimitry Andric int EntryBBNumber = MF.front().getNumber();
6917d523365SDimitry Andric bool IsSEH = isAsynchronousEHPersonality(
6922cab237bSDimitry Andric classifyEHPersonality(MF.getFunction().getPersonalityFn()));
6937d523365SDimitry Andric
6947d523365SDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
6954ba319b5SDimitry Andric SmallVector<const MachineBasicBlock *, 16> EHScopeBlocks;
6967d523365SDimitry Andric SmallVector<const MachineBasicBlock *, 16> UnreachableBlocks;
6977d523365SDimitry Andric SmallVector<const MachineBasicBlock *, 16> SEHCatchPads;
6987d523365SDimitry Andric SmallVector<std::pair<const MachineBasicBlock *, int>, 16> CatchRetSuccessors;
6997d523365SDimitry Andric for (const MachineBasicBlock &MBB : MF) {
7004ba319b5SDimitry Andric if (MBB.isEHScopeEntry()) {
7014ba319b5SDimitry Andric EHScopeBlocks.push_back(&MBB);
7027d523365SDimitry Andric } else if (IsSEH && MBB.isEHPad()) {
7037d523365SDimitry Andric SEHCatchPads.push_back(&MBB);
7047d523365SDimitry Andric } else if (MBB.pred_empty()) {
7057d523365SDimitry Andric UnreachableBlocks.push_back(&MBB);
7067d523365SDimitry Andric }
7077d523365SDimitry Andric
7087d523365SDimitry Andric MachineBasicBlock::const_iterator MBBI = MBB.getFirstTerminator();
709d88c1a5aSDimitry Andric
7104ba319b5SDimitry Andric // CatchPads are not scopes for SEH so do not consider CatchRet to
7114ba319b5SDimitry Andric // transfer control to another scope.
712d88c1a5aSDimitry Andric if (MBBI == MBB.end() || MBBI->getOpcode() != TII->getCatchReturnOpcode())
7137d523365SDimitry Andric continue;
7147d523365SDimitry Andric
7157d523365SDimitry Andric // FIXME: SEH CatchPads are not necessarily in the parent function:
7167d523365SDimitry Andric // they could be inside a finally block.
7177d523365SDimitry Andric const MachineBasicBlock *Successor = MBBI->getOperand(0).getMBB();
7187d523365SDimitry Andric const MachineBasicBlock *SuccessorColor = MBBI->getOperand(1).getMBB();
7197d523365SDimitry Andric CatchRetSuccessors.push_back(
7207d523365SDimitry Andric {Successor, IsSEH ? EntryBBNumber : SuccessorColor->getNumber()});
7217d523365SDimitry Andric }
7227d523365SDimitry Andric
7237d523365SDimitry Andric // We don't have anything to do if there aren't any EH pads.
7244ba319b5SDimitry Andric if (EHScopeBlocks.empty())
7254ba319b5SDimitry Andric return EHScopeMembership;
7267d523365SDimitry Andric
7277d523365SDimitry Andric // Identify all the basic blocks reachable from the function entry.
7284ba319b5SDimitry Andric collectEHScopeMembers(EHScopeMembership, EntryBBNumber, &MF.front());
7294ba319b5SDimitry Andric // All blocks not part of a scope are in the parent function.
7307d523365SDimitry Andric for (const MachineBasicBlock *MBB : UnreachableBlocks)
7314ba319b5SDimitry Andric collectEHScopeMembers(EHScopeMembership, EntryBBNumber, MBB);
7324ba319b5SDimitry Andric // Next, identify all the blocks inside the scopes.
7334ba319b5SDimitry Andric for (const MachineBasicBlock *MBB : EHScopeBlocks)
7344ba319b5SDimitry Andric collectEHScopeMembers(EHScopeMembership, MBB->getNumber(), MBB);
7354ba319b5SDimitry Andric // SEH CatchPads aren't really scopes, handle them separately.
7367d523365SDimitry Andric for (const MachineBasicBlock *MBB : SEHCatchPads)
7374ba319b5SDimitry Andric collectEHScopeMembers(EHScopeMembership, EntryBBNumber, MBB);
7387d523365SDimitry Andric // Finally, identify all the targets of a catchret.
7397d523365SDimitry Andric for (std::pair<const MachineBasicBlock *, int> CatchRetPair :
7407d523365SDimitry Andric CatchRetSuccessors)
7414ba319b5SDimitry Andric collectEHScopeMembers(EHScopeMembership, CatchRetPair.second,
7427d523365SDimitry Andric CatchRetPair.first);
7434ba319b5SDimitry Andric return EHScopeMembership;
7447d523365SDimitry Andric }
745