Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/Analysis.cpp
35232 views
//===-- Analysis.cpp - CodeGen LLVM IR Analysis Utilities -----------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines several CodeGen-specific LLVM IR analysis utilities.9//10//===----------------------------------------------------------------------===//1112#include "llvm/CodeGen/Analysis.h"13#include "llvm/Analysis/ValueTracking.h"14#include "llvm/CodeGen/MachineFunction.h"15#include "llvm/CodeGen/TargetInstrInfo.h"16#include "llvm/CodeGen/TargetLowering.h"17#include "llvm/CodeGen/TargetSubtargetInfo.h"18#include "llvm/IR/DataLayout.h"19#include "llvm/IR/DerivedTypes.h"20#include "llvm/IR/Function.h"21#include "llvm/IR/Instructions.h"22#include "llvm/IR/IntrinsicInst.h"23#include "llvm/IR/Module.h"24#include "llvm/Support/ErrorHandling.h"25#include "llvm/Target/TargetMachine.h"2627using namespace llvm;2829/// Compute the linearized index of a member in a nested aggregate/struct/array30/// by recursing and accumulating CurIndex as long as there are indices in the31/// index list.32unsigned llvm::ComputeLinearIndex(Type *Ty,33const unsigned *Indices,34const unsigned *IndicesEnd,35unsigned CurIndex) {36// Base case: We're done.37if (Indices && Indices == IndicesEnd)38return CurIndex;3940// Given a struct type, recursively traverse the elements.41if (StructType *STy = dyn_cast<StructType>(Ty)) {42for (auto I : llvm::enumerate(STy->elements())) {43Type *ET = I.value();44if (Indices && *Indices == I.index())45return ComputeLinearIndex(ET, Indices + 1, IndicesEnd, CurIndex);46CurIndex = ComputeLinearIndex(ET, nullptr, nullptr, CurIndex);47}48assert(!Indices && "Unexpected out of bound");49return CurIndex;50}51// Given an array type, recursively traverse the elements.52else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {53Type *EltTy = ATy->getElementType();54unsigned NumElts = ATy->getNumElements();55// Compute the Linear offset when jumping one element of the array56unsigned EltLinearOffset = ComputeLinearIndex(EltTy, nullptr, nullptr, 0);57if (Indices) {58assert(*Indices < NumElts && "Unexpected out of bound");59// If the indice is inside the array, compute the index to the requested60// elt and recurse inside the element with the end of the indices list61CurIndex += EltLinearOffset* *Indices;62return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex);63}64CurIndex += EltLinearOffset*NumElts;65return CurIndex;66}67// We haven't found the type we're looking for, so keep searching.68return CurIndex + 1;69}7071/// ComputeValueVTs - Given an LLVM IR type, compute a sequence of72/// EVTs that represent all the individual underlying73/// non-aggregate types that comprise it.74///75/// If Offsets is non-null, it points to a vector to be filled in76/// with the in-memory offsets of each of the individual values.77///78void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,79Type *Ty, SmallVectorImpl<EVT> &ValueVTs,80SmallVectorImpl<EVT> *MemVTs,81SmallVectorImpl<TypeSize> *Offsets,82TypeSize StartingOffset) {83assert((Ty->isScalableTy() == StartingOffset.isScalable() ||84StartingOffset.isZero()) &&85"Offset/TypeSize mismatch!");86// Given a struct type, recursively traverse the elements.87if (StructType *STy = dyn_cast<StructType>(Ty)) {88// If the Offsets aren't needed, don't query the struct layout. This allows89// us to support structs with scalable vectors for operations that don't90// need offsets.91const StructLayout *SL = Offsets ? DL.getStructLayout(STy) : nullptr;92for (StructType::element_iterator EB = STy->element_begin(),93EI = EB,94EE = STy->element_end();95EI != EE; ++EI) {96// Don't compute the element offset if we didn't get a StructLayout above.97TypeSize EltOffset =98SL ? SL->getElementOffset(EI - EB) : TypeSize::getZero();99ComputeValueVTs(TLI, DL, *EI, ValueVTs, MemVTs, Offsets,100StartingOffset + EltOffset);101}102return;103}104// Given an array type, recursively traverse the elements.105if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {106Type *EltTy = ATy->getElementType();107TypeSize EltSize = DL.getTypeAllocSize(EltTy);108for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)109ComputeValueVTs(TLI, DL, EltTy, ValueVTs, MemVTs, Offsets,110StartingOffset + i * EltSize);111return;112}113// Interpret void as zero return values.114if (Ty->isVoidTy())115return;116// Base case: we can get an EVT for this LLVM IR type.117ValueVTs.push_back(TLI.getValueType(DL, Ty));118if (MemVTs)119MemVTs->push_back(TLI.getMemValueType(DL, Ty));120if (Offsets)121Offsets->push_back(StartingOffset);122}123124void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,125Type *Ty, SmallVectorImpl<EVT> &ValueVTs,126SmallVectorImpl<EVT> *MemVTs,127SmallVectorImpl<uint64_t> *FixedOffsets,128uint64_t StartingOffset) {129TypeSize Offset = TypeSize::getFixed(StartingOffset);130if (FixedOffsets) {131SmallVector<TypeSize, 4> Offsets;132ComputeValueVTs(TLI, DL, Ty, ValueVTs, MemVTs, &Offsets, Offset);133for (TypeSize Offset : Offsets)134FixedOffsets->push_back(Offset.getFixedValue());135} else {136ComputeValueVTs(TLI, DL, Ty, ValueVTs, MemVTs, nullptr, Offset);137}138}139140void llvm::computeValueLLTs(const DataLayout &DL, Type &Ty,141SmallVectorImpl<LLT> &ValueTys,142SmallVectorImpl<uint64_t> *Offsets,143uint64_t StartingOffset) {144// Given a struct type, recursively traverse the elements.145if (StructType *STy = dyn_cast<StructType>(&Ty)) {146// If the Offsets aren't needed, don't query the struct layout. This allows147// us to support structs with scalable vectors for operations that don't148// need offsets.149const StructLayout *SL = Offsets ? DL.getStructLayout(STy) : nullptr;150for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I) {151uint64_t EltOffset = SL ? SL->getElementOffset(I) : 0;152computeValueLLTs(DL, *STy->getElementType(I), ValueTys, Offsets,153StartingOffset + EltOffset);154}155return;156}157// Given an array type, recursively traverse the elements.158if (ArrayType *ATy = dyn_cast<ArrayType>(&Ty)) {159Type *EltTy = ATy->getElementType();160uint64_t EltSize = DL.getTypeAllocSize(EltTy).getFixedValue();161for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)162computeValueLLTs(DL, *EltTy, ValueTys, Offsets,163StartingOffset + i * EltSize);164return;165}166// Interpret void as zero return values.167if (Ty.isVoidTy())168return;169// Base case: we can get an LLT for this LLVM IR type.170ValueTys.push_back(getLLTForType(Ty, DL));171if (Offsets != nullptr)172Offsets->push_back(StartingOffset * 8);173}174175/// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.176GlobalValue *llvm::ExtractTypeInfo(Value *V) {177V = V->stripPointerCasts();178GlobalValue *GV = dyn_cast<GlobalValue>(V);179GlobalVariable *Var = dyn_cast<GlobalVariable>(V);180181if (Var && Var->getName() == "llvm.eh.catch.all.value") {182assert(Var->hasInitializer() &&183"The EH catch-all value must have an initializer");184Value *Init = Var->getInitializer();185GV = dyn_cast<GlobalValue>(Init);186if (!GV) V = cast<ConstantPointerNull>(Init);187}188189assert((GV || isa<ConstantPointerNull>(V)) &&190"TypeInfo must be a global variable or NULL");191return GV;192}193194/// getFCmpCondCode - Return the ISD condition code corresponding to195/// the given LLVM IR floating-point condition code. This includes196/// consideration of global floating-point math flags.197///198ISD::CondCode llvm::getFCmpCondCode(FCmpInst::Predicate Pred) {199switch (Pred) {200case FCmpInst::FCMP_FALSE: return ISD::SETFALSE;201case FCmpInst::FCMP_OEQ: return ISD::SETOEQ;202case FCmpInst::FCMP_OGT: return ISD::SETOGT;203case FCmpInst::FCMP_OGE: return ISD::SETOGE;204case FCmpInst::FCMP_OLT: return ISD::SETOLT;205case FCmpInst::FCMP_OLE: return ISD::SETOLE;206case FCmpInst::FCMP_ONE: return ISD::SETONE;207case FCmpInst::FCMP_ORD: return ISD::SETO;208case FCmpInst::FCMP_UNO: return ISD::SETUO;209case FCmpInst::FCMP_UEQ: return ISD::SETUEQ;210case FCmpInst::FCMP_UGT: return ISD::SETUGT;211case FCmpInst::FCMP_UGE: return ISD::SETUGE;212case FCmpInst::FCMP_ULT: return ISD::SETULT;213case FCmpInst::FCMP_ULE: return ISD::SETULE;214case FCmpInst::FCMP_UNE: return ISD::SETUNE;215case FCmpInst::FCMP_TRUE: return ISD::SETTRUE;216default: llvm_unreachable("Invalid FCmp predicate opcode!");217}218}219220ISD::CondCode llvm::getFCmpCodeWithoutNaN(ISD::CondCode CC) {221switch (CC) {222case ISD::SETOEQ: case ISD::SETUEQ: return ISD::SETEQ;223case ISD::SETONE: case ISD::SETUNE: return ISD::SETNE;224case ISD::SETOLT: case ISD::SETULT: return ISD::SETLT;225case ISD::SETOLE: case ISD::SETULE: return ISD::SETLE;226case ISD::SETOGT: case ISD::SETUGT: return ISD::SETGT;227case ISD::SETOGE: case ISD::SETUGE: return ISD::SETGE;228default: return CC;229}230}231232ISD::CondCode llvm::getICmpCondCode(ICmpInst::Predicate Pred) {233switch (Pred) {234case ICmpInst::ICMP_EQ: return ISD::SETEQ;235case ICmpInst::ICMP_NE: return ISD::SETNE;236case ICmpInst::ICMP_SLE: return ISD::SETLE;237case ICmpInst::ICMP_ULE: return ISD::SETULE;238case ICmpInst::ICMP_SGE: return ISD::SETGE;239case ICmpInst::ICMP_UGE: return ISD::SETUGE;240case ICmpInst::ICMP_SLT: return ISD::SETLT;241case ICmpInst::ICMP_ULT: return ISD::SETULT;242case ICmpInst::ICMP_SGT: return ISD::SETGT;243case ICmpInst::ICMP_UGT: return ISD::SETUGT;244default:245llvm_unreachable("Invalid ICmp predicate opcode!");246}247}248249ICmpInst::Predicate llvm::getICmpCondCode(ISD::CondCode Pred) {250switch (Pred) {251case ISD::SETEQ:252return ICmpInst::ICMP_EQ;253case ISD::SETNE:254return ICmpInst::ICMP_NE;255case ISD::SETLE:256return ICmpInst::ICMP_SLE;257case ISD::SETULE:258return ICmpInst::ICMP_ULE;259case ISD::SETGE:260return ICmpInst::ICMP_SGE;261case ISD::SETUGE:262return ICmpInst::ICMP_UGE;263case ISD::SETLT:264return ICmpInst::ICMP_SLT;265case ISD::SETULT:266return ICmpInst::ICMP_ULT;267case ISD::SETGT:268return ICmpInst::ICMP_SGT;269case ISD::SETUGT:270return ICmpInst::ICMP_UGT;271default:272llvm_unreachable("Invalid ISD integer condition code!");273}274}275276static bool isNoopBitcast(Type *T1, Type *T2,277const TargetLoweringBase& TLI) {278return T1 == T2 || (T1->isPointerTy() && T2->isPointerTy()) ||279(isa<VectorType>(T1) && isa<VectorType>(T2) &&280TLI.isTypeLegal(EVT::getEVT(T1)) && TLI.isTypeLegal(EVT::getEVT(T2)));281}282283/// Look through operations that will be free to find the earliest source of284/// this value.285///286/// @param ValLoc If V has aggregate type, we will be interested in a particular287/// scalar component. This records its address; the reverse of this list gives a288/// sequence of indices appropriate for an extractvalue to locate the important289/// value. This value is updated during the function and on exit will indicate290/// similar information for the Value returned.291///292/// @param DataBits If this function looks through truncate instructions, this293/// will record the smallest size attained.294static const Value *getNoopInput(const Value *V,295SmallVectorImpl<unsigned> &ValLoc,296unsigned &DataBits,297const TargetLoweringBase &TLI,298const DataLayout &DL) {299while (true) {300// Try to look through V1; if V1 is not an instruction, it can't be looked301// through.302const Instruction *I = dyn_cast<Instruction>(V);303if (!I || I->getNumOperands() == 0) return V;304const Value *NoopInput = nullptr;305306Value *Op = I->getOperand(0);307if (isa<BitCastInst>(I)) {308// Look through truly no-op bitcasts.309if (isNoopBitcast(Op->getType(), I->getType(), TLI))310NoopInput = Op;311} else if (isa<GetElementPtrInst>(I)) {312// Look through getelementptr313if (cast<GetElementPtrInst>(I)->hasAllZeroIndices())314NoopInput = Op;315} else if (isa<IntToPtrInst>(I)) {316// Look through inttoptr.317// Make sure this isn't a truncating or extending cast. We could318// support this eventually, but don't bother for now.319if (!isa<VectorType>(I->getType()) &&320DL.getPointerSizeInBits() ==321cast<IntegerType>(Op->getType())->getBitWidth())322NoopInput = Op;323} else if (isa<PtrToIntInst>(I)) {324// Look through ptrtoint.325// Make sure this isn't a truncating or extending cast. We could326// support this eventually, but don't bother for now.327if (!isa<VectorType>(I->getType()) &&328DL.getPointerSizeInBits() ==329cast<IntegerType>(I->getType())->getBitWidth())330NoopInput = Op;331} else if (isa<TruncInst>(I) &&332TLI.allowTruncateForTailCall(Op->getType(), I->getType())) {333DataBits =334std::min((uint64_t)DataBits,335I->getType()->getPrimitiveSizeInBits().getFixedValue());336NoopInput = Op;337} else if (auto *CB = dyn_cast<CallBase>(I)) {338const Value *ReturnedOp = CB->getReturnedArgOperand();339if (ReturnedOp && isNoopBitcast(ReturnedOp->getType(), I->getType(), TLI))340NoopInput = ReturnedOp;341} else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(V)) {342// Value may come from either the aggregate or the scalar343ArrayRef<unsigned> InsertLoc = IVI->getIndices();344if (ValLoc.size() >= InsertLoc.size() &&345std::equal(InsertLoc.begin(), InsertLoc.end(), ValLoc.rbegin())) {346// The type being inserted is a nested sub-type of the aggregate; we347// have to remove those initial indices to get the location we're348// interested in for the operand.349ValLoc.resize(ValLoc.size() - InsertLoc.size());350NoopInput = IVI->getInsertedValueOperand();351} else {352// The struct we're inserting into has the value we're interested in, no353// change of address.354NoopInput = Op;355}356} else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {357// The part we're interested in will inevitably be some sub-section of the358// previous aggregate. Combine the two paths to obtain the true address of359// our element.360ArrayRef<unsigned> ExtractLoc = EVI->getIndices();361ValLoc.append(ExtractLoc.rbegin(), ExtractLoc.rend());362NoopInput = Op;363}364// Terminate if we couldn't find anything to look through.365if (!NoopInput)366return V;367368V = NoopInput;369}370}371372/// Return true if this scalar return value only has bits discarded on its path373/// from the "tail call" to the "ret". This includes the obvious noop374/// instructions handled by getNoopInput above as well as free truncations (or375/// extensions prior to the call).376static bool slotOnlyDiscardsData(const Value *RetVal, const Value *CallVal,377SmallVectorImpl<unsigned> &RetIndices,378SmallVectorImpl<unsigned> &CallIndices,379bool AllowDifferingSizes,380const TargetLoweringBase &TLI,381const DataLayout &DL) {382383// Trace the sub-value needed by the return value as far back up the graph as384// possible, in the hope that it will intersect with the value produced by the385// call. In the simple case with no "returned" attribute, the hope is actually386// that we end up back at the tail call instruction itself.387unsigned BitsRequired = UINT_MAX;388RetVal = getNoopInput(RetVal, RetIndices, BitsRequired, TLI, DL);389390// If this slot in the value returned is undef, it doesn't matter what the391// call puts there, it'll be fine.392if (isa<UndefValue>(RetVal))393return true;394395// Now do a similar search up through the graph to find where the value396// actually returned by the "tail call" comes from. In the simple case without397// a "returned" attribute, the search will be blocked immediately and the loop398// a Noop.399unsigned BitsProvided = UINT_MAX;400CallVal = getNoopInput(CallVal, CallIndices, BitsProvided, TLI, DL);401402// There's no hope if we can't actually trace them to (the same part of!) the403// same value.404if (CallVal != RetVal || CallIndices != RetIndices)405return false;406407// However, intervening truncates may have made the call non-tail. Make sure408// all the bits that are needed by the "ret" have been provided by the "tail409// call". FIXME: with sufficiently cunning bit-tracking, we could look through410// extensions too.411if (BitsProvided < BitsRequired ||412(!AllowDifferingSizes && BitsProvided != BitsRequired))413return false;414415return true;416}417418/// For an aggregate type, determine whether a given index is within bounds or419/// not.420static bool indexReallyValid(Type *T, unsigned Idx) {421if (ArrayType *AT = dyn_cast<ArrayType>(T))422return Idx < AT->getNumElements();423424return Idx < cast<StructType>(T)->getNumElements();425}426427/// Move the given iterators to the next leaf type in depth first traversal.428///429/// Performs a depth-first traversal of the type as specified by its arguments,430/// stopping at the next leaf node (which may be a legitimate scalar type or an431/// empty struct or array).432///433/// @param SubTypes List of the partial components making up the type from434/// outermost to innermost non-empty aggregate. The element currently435/// represented is SubTypes.back()->getTypeAtIndex(Path.back() - 1).436///437/// @param Path Set of extractvalue indices leading from the outermost type438/// (SubTypes[0]) to the leaf node currently represented.439///440/// @returns true if a new type was found, false otherwise. Calling this441/// function again on a finished iterator will repeatedly return442/// false. SubTypes.back()->getTypeAtIndex(Path.back()) is either an empty443/// aggregate or a non-aggregate444static bool advanceToNextLeafType(SmallVectorImpl<Type *> &SubTypes,445SmallVectorImpl<unsigned> &Path) {446// First march back up the tree until we can successfully increment one of the447// coordinates in Path.448while (!Path.empty() && !indexReallyValid(SubTypes.back(), Path.back() + 1)) {449Path.pop_back();450SubTypes.pop_back();451}452453// If we reached the top, then the iterator is done.454if (Path.empty())455return false;456457// We know there's *some* valid leaf now, so march back down the tree picking458// out the left-most element at each node.459++Path.back();460Type *DeeperType =461ExtractValueInst::getIndexedType(SubTypes.back(), Path.back());462while (DeeperType->isAggregateType()) {463if (!indexReallyValid(DeeperType, 0))464return true;465466SubTypes.push_back(DeeperType);467Path.push_back(0);468469DeeperType = ExtractValueInst::getIndexedType(DeeperType, 0);470}471472return true;473}474475/// Find the first non-empty, scalar-like type in Next and setup the iterator476/// components.477///478/// Assuming Next is an aggregate of some kind, this function will traverse the479/// tree from left to right (i.e. depth-first) looking for the first480/// non-aggregate type which will play a role in function return.481///482/// For example, if Next was {[0 x i64], {{}, i32, {}}, i32} then we would setup483/// Path as [1, 1] and SubTypes as [Next, {{}, i32, {}}] to represent the first484/// i32 in that type.485static bool firstRealType(Type *Next, SmallVectorImpl<Type *> &SubTypes,486SmallVectorImpl<unsigned> &Path) {487// First initialise the iterator components to the first "leaf" node488// (i.e. node with no valid sub-type at any index, so {} does count as a leaf489// despite nominally being an aggregate).490while (Type *FirstInner = ExtractValueInst::getIndexedType(Next, 0)) {491SubTypes.push_back(Next);492Path.push_back(0);493Next = FirstInner;494}495496// If there's no Path now, Next was originally scalar already (or empty497// leaf). We're done.498if (Path.empty())499return true;500501// Otherwise, use normal iteration to keep looking through the tree until we502// find a non-aggregate type.503while (ExtractValueInst::getIndexedType(SubTypes.back(), Path.back())504->isAggregateType()) {505if (!advanceToNextLeafType(SubTypes, Path))506return false;507}508509return true;510}511512/// Set the iterator data-structures to the next non-empty, non-aggregate513/// subtype.514static bool nextRealType(SmallVectorImpl<Type *> &SubTypes,515SmallVectorImpl<unsigned> &Path) {516do {517if (!advanceToNextLeafType(SubTypes, Path))518return false;519520assert(!Path.empty() && "found a leaf but didn't set the path?");521} while (ExtractValueInst::getIndexedType(SubTypes.back(), Path.back())522->isAggregateType());523524return true;525}526527528/// Test if the given instruction is in a position to be optimized529/// with a tail-call. This roughly means that it's in a block with530/// a return and there's nothing that needs to be scheduled531/// between it and the return.532///533/// This function only tests target-independent requirements.534bool llvm::isInTailCallPosition(const CallBase &Call, const TargetMachine &TM,535bool ReturnsFirstArg) {536const BasicBlock *ExitBB = Call.getParent();537const Instruction *Term = ExitBB->getTerminator();538const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);539540// The block must end in a return statement or unreachable.541//542// FIXME: Decline tailcall if it's not guaranteed and if the block ends in543// an unreachable, for now. The way tailcall optimization is currently544// implemented means it will add an epilogue followed by a jump. That is545// not profitable. Also, if the callee is a special function (e.g.546// longjmp on x86), it can end up causing miscompilation that has not547// been fully understood.548if (!Ret && ((!TM.Options.GuaranteedTailCallOpt &&549Call.getCallingConv() != CallingConv::Tail &&550Call.getCallingConv() != CallingConv::SwiftTail) ||551!isa<UnreachableInst>(Term)))552return false;553554// If I will have a chain, make sure no other instruction that will have a555// chain interposes between I and the return.556// Check for all calls including speculatable functions.557for (BasicBlock::const_iterator BBI = std::prev(ExitBB->end(), 2);; --BBI) {558if (&*BBI == &Call)559break;560// Debug info intrinsics do not get in the way of tail call optimization.561// Pseudo probe intrinsics do not block tail call optimization either.562if (BBI->isDebugOrPseudoInst())563continue;564// A lifetime end, assume or noalias.decl intrinsic should not stop tail565// call optimization.566if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(BBI))567if (II->getIntrinsicID() == Intrinsic::lifetime_end ||568II->getIntrinsicID() == Intrinsic::assume ||569II->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl)570continue;571if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||572!isSafeToSpeculativelyExecute(&*BBI))573return false;574}575576const Function *F = ExitBB->getParent();577return returnTypeIsEligibleForTailCall(578F, &Call, Ret, *TM.getSubtargetImpl(*F)->getTargetLowering(),579ReturnsFirstArg);580}581582bool llvm::attributesPermitTailCall(const Function *F, const Instruction *I,583const ReturnInst *Ret,584const TargetLoweringBase &TLI,585bool *AllowDifferingSizes) {586// ADS may be null, so don't write to it directly.587bool DummyADS;588bool &ADS = AllowDifferingSizes ? *AllowDifferingSizes : DummyADS;589ADS = true;590591AttrBuilder CallerAttrs(F->getContext(), F->getAttributes().getRetAttrs());592AttrBuilder CalleeAttrs(F->getContext(),593cast<CallInst>(I)->getAttributes().getRetAttrs());594595// Following attributes are completely benign as far as calling convention596// goes, they shouldn't affect whether the call is a tail call.597for (const auto &Attr :598{Attribute::Alignment, Attribute::Dereferenceable,599Attribute::DereferenceableOrNull, Attribute::NoAlias,600Attribute::NonNull, Attribute::NoUndef, Attribute::Range}) {601CallerAttrs.removeAttribute(Attr);602CalleeAttrs.removeAttribute(Attr);603}604605if (CallerAttrs.contains(Attribute::ZExt)) {606if (!CalleeAttrs.contains(Attribute::ZExt))607return false;608609ADS = false;610CallerAttrs.removeAttribute(Attribute::ZExt);611CalleeAttrs.removeAttribute(Attribute::ZExt);612} else if (CallerAttrs.contains(Attribute::SExt)) {613if (!CalleeAttrs.contains(Attribute::SExt))614return false;615616ADS = false;617CallerAttrs.removeAttribute(Attribute::SExt);618CalleeAttrs.removeAttribute(Attribute::SExt);619}620621// Drop sext and zext return attributes if the result is not used.622// This enables tail calls for code like:623//624// define void @caller() {625// entry:626// %unused_result = tail call zeroext i1 @callee()627// br label %retlabel628// retlabel:629// ret void630// }631if (I->use_empty()) {632CalleeAttrs.removeAttribute(Attribute::SExt);633CalleeAttrs.removeAttribute(Attribute::ZExt);634}635636// If they're still different, there's some facet we don't understand637// (currently only "inreg", but in future who knows). It may be OK but the638// only safe option is to reject the tail call.639return CallerAttrs == CalleeAttrs;640}641642bool llvm::returnTypeIsEligibleForTailCall(const Function *F,643const Instruction *I,644const ReturnInst *Ret,645const TargetLoweringBase &TLI,646bool ReturnsFirstArg) {647// If the block ends with a void return or unreachable, it doesn't matter648// what the call's return type is.649if (!Ret || Ret->getNumOperands() == 0) return true;650651// If the return value is undef, it doesn't matter what the call's652// return type is.653if (isa<UndefValue>(Ret->getOperand(0))) return true;654655// Make sure the attributes attached to each return are compatible.656bool AllowDifferingSizes;657if (!attributesPermitTailCall(F, I, Ret, TLI, &AllowDifferingSizes))658return false;659660// If the return value is the first argument of the call.661if (ReturnsFirstArg)662return true;663664const Value *RetVal = Ret->getOperand(0), *CallVal = I;665SmallVector<unsigned, 4> RetPath, CallPath;666SmallVector<Type *, 4> RetSubTypes, CallSubTypes;667668bool RetEmpty = !firstRealType(RetVal->getType(), RetSubTypes, RetPath);669bool CallEmpty = !firstRealType(CallVal->getType(), CallSubTypes, CallPath);670671// Nothing's actually returned, it doesn't matter what the callee put there672// it's a valid tail call.673if (RetEmpty)674return true;675676// Iterate pairwise through each of the value types making up the tail call677// and the corresponding return. For each one we want to know whether it's678// essentially going directly from the tail call to the ret, via operations679// that end up not generating any code.680//681// We allow a certain amount of covariance here. For example it's permitted682// for the tail call to define more bits than the ret actually cares about683// (e.g. via a truncate).684do {685if (CallEmpty) {686// We've exhausted the values produced by the tail call instruction, the687// rest are essentially undef. The type doesn't really matter, but we need688// *something*.689Type *SlotType =690ExtractValueInst::getIndexedType(RetSubTypes.back(), RetPath.back());691CallVal = UndefValue::get(SlotType);692}693694// The manipulations performed when we're looking through an insertvalue or695// an extractvalue would happen at the front of the RetPath list, so since696// we have to copy it anyway it's more efficient to create a reversed copy.697SmallVector<unsigned, 4> TmpRetPath(llvm::reverse(RetPath));698SmallVector<unsigned, 4> TmpCallPath(llvm::reverse(CallPath));699700// Finally, we can check whether the value produced by the tail call at this701// index is compatible with the value we return.702if (!slotOnlyDiscardsData(RetVal, CallVal, TmpRetPath, TmpCallPath,703AllowDifferingSizes, TLI,704F->getDataLayout()))705return false;706707CallEmpty = !nextRealType(CallSubTypes, CallPath);708} while(nextRealType(RetSubTypes, RetPath));709710return true;711}712713bool llvm::funcReturnsFirstArgOfCall(const CallInst &CI) {714const ReturnInst *Ret = dyn_cast<ReturnInst>(CI.getParent()->getTerminator());715Value *RetVal = Ret ? Ret->getReturnValue() : nullptr;716bool ReturnsFirstArg = false;717if (RetVal && ((RetVal == CI.getArgOperand(0))))718ReturnsFirstArg = true;719return ReturnsFirstArg;720}721722static void collectEHScopeMembers(723DenseMap<const MachineBasicBlock *, int> &EHScopeMembership, int EHScope,724const MachineBasicBlock *MBB) {725SmallVector<const MachineBasicBlock *, 16> Worklist = {MBB};726while (!Worklist.empty()) {727const MachineBasicBlock *Visiting = Worklist.pop_back_val();728// Don't follow blocks which start new scopes.729if (Visiting->isEHPad() && Visiting != MBB)730continue;731732// Add this MBB to our scope.733auto P = EHScopeMembership.insert(std::make_pair(Visiting, EHScope));734735// Don't revisit blocks.736if (!P.second) {737assert(P.first->second == EHScope && "MBB is part of two scopes!");738continue;739}740741// Returns are boundaries where scope transfer can occur, don't follow742// successors.743if (Visiting->isEHScopeReturnBlock())744continue;745746append_range(Worklist, Visiting->successors());747}748}749750DenseMap<const MachineBasicBlock *, int>751llvm::getEHScopeMembership(const MachineFunction &MF) {752DenseMap<const MachineBasicBlock *, int> EHScopeMembership;753754// We don't have anything to do if there aren't any EH pads.755if (!MF.hasEHScopes())756return EHScopeMembership;757758int EntryBBNumber = MF.front().getNumber();759bool IsSEH = isAsynchronousEHPersonality(760classifyEHPersonality(MF.getFunction().getPersonalityFn()));761762const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();763SmallVector<const MachineBasicBlock *, 16> EHScopeBlocks;764SmallVector<const MachineBasicBlock *, 16> UnreachableBlocks;765SmallVector<const MachineBasicBlock *, 16> SEHCatchPads;766SmallVector<std::pair<const MachineBasicBlock *, int>, 16> CatchRetSuccessors;767for (const MachineBasicBlock &MBB : MF) {768if (MBB.isEHScopeEntry()) {769EHScopeBlocks.push_back(&MBB);770} else if (IsSEH && MBB.isEHPad()) {771SEHCatchPads.push_back(&MBB);772} else if (MBB.pred_empty()) {773UnreachableBlocks.push_back(&MBB);774}775776MachineBasicBlock::const_iterator MBBI = MBB.getFirstTerminator();777778// CatchPads are not scopes for SEH so do not consider CatchRet to779// transfer control to another scope.780if (MBBI == MBB.end() || MBBI->getOpcode() != TII->getCatchReturnOpcode())781continue;782783// FIXME: SEH CatchPads are not necessarily in the parent function:784// they could be inside a finally block.785const MachineBasicBlock *Successor = MBBI->getOperand(0).getMBB();786const MachineBasicBlock *SuccessorColor = MBBI->getOperand(1).getMBB();787CatchRetSuccessors.push_back(788{Successor, IsSEH ? EntryBBNumber : SuccessorColor->getNumber()});789}790791// We don't have anything to do if there aren't any EH pads.792if (EHScopeBlocks.empty())793return EHScopeMembership;794795// Identify all the basic blocks reachable from the function entry.796collectEHScopeMembers(EHScopeMembership, EntryBBNumber, &MF.front());797// All blocks not part of a scope are in the parent function.798for (const MachineBasicBlock *MBB : UnreachableBlocks)799collectEHScopeMembers(EHScopeMembership, EntryBBNumber, MBB);800// Next, identify all the blocks inside the scopes.801for (const MachineBasicBlock *MBB : EHScopeBlocks)802collectEHScopeMembers(EHScopeMembership, MBB->getNumber(), MBB);803// SEH CatchPads aren't really scopes, handle them separately.804for (const MachineBasicBlock *MBB : SEHCatchPads)805collectEHScopeMembers(EHScopeMembership, EntryBBNumber, MBB);806// Finally, identify all the targets of a catchret.807for (std::pair<const MachineBasicBlock *, int> CatchRetPair :808CatchRetSuccessors)809collectEHScopeMembers(EHScopeMembership, CatchRetPair.second,810CatchRetPair.first);811return EHScopeMembership;812}813814815