Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
35266 views
//===- DeadArgumentElimination.cpp - Eliminate dead arguments -------------===//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 pass deletes dead arguments from internal functions. Dead argument9// elimination removes arguments which are directly dead, as well as arguments10// only passed into function calls as dead arguments of other functions. This11// pass also deletes dead return values in a similar way.12//13// This pass is often useful as a cleanup pass to run after aggressive14// interprocedural passes, which add possibly-dead arguments or return values.15//16//===----------------------------------------------------------------------===//1718#include "llvm/Transforms/IPO/DeadArgumentElimination.h"19#include "llvm/ADT/SmallVector.h"20#include "llvm/ADT/Statistic.h"21#include "llvm/IR/Argument.h"22#include "llvm/IR/AttributeMask.h"23#include "llvm/IR/Attributes.h"24#include "llvm/IR/BasicBlock.h"25#include "llvm/IR/Constants.h"26#include "llvm/IR/DIBuilder.h"27#include "llvm/IR/DerivedTypes.h"28#include "llvm/IR/Function.h"29#include "llvm/IR/IRBuilder.h"30#include "llvm/IR/InstrTypes.h"31#include "llvm/IR/Instructions.h"32#include "llvm/IR/IntrinsicInst.h"33#include "llvm/IR/Intrinsics.h"34#include "llvm/IR/Module.h"35#include "llvm/IR/NoFolder.h"36#include "llvm/IR/PassManager.h"37#include "llvm/IR/Type.h"38#include "llvm/IR/Use.h"39#include "llvm/IR/User.h"40#include "llvm/IR/Value.h"41#include "llvm/InitializePasses.h"42#include "llvm/Pass.h"43#include "llvm/Support/Casting.h"44#include "llvm/Support/Debug.h"45#include "llvm/Support/raw_ostream.h"46#include "llvm/Transforms/IPO.h"47#include "llvm/Transforms/Utils/BasicBlockUtils.h"48#include <cassert>49#include <utility>50#include <vector>5152using namespace llvm;5354#define DEBUG_TYPE "deadargelim"5556STATISTIC(NumArgumentsEliminated, "Number of unread args removed");57STATISTIC(NumRetValsEliminated, "Number of unused return values removed");58STATISTIC(NumArgumentsReplacedWithPoison,59"Number of unread args replaced with poison");6061namespace {6263/// The dead argument elimination pass.64class DAE : public ModulePass {65protected:66// DAH uses this to specify a different ID.67explicit DAE(char &ID) : ModulePass(ID) {}6869public:70static char ID; // Pass identification, replacement for typeid7172DAE() : ModulePass(ID) {73initializeDAEPass(*PassRegistry::getPassRegistry());74}7576bool runOnModule(Module &M) override {77if (skipModule(M))78return false;79DeadArgumentEliminationPass DAEP(shouldHackArguments());80ModuleAnalysisManager DummyMAM;81PreservedAnalyses PA = DAEP.run(M, DummyMAM);82return !PA.areAllPreserved();83}8485virtual bool shouldHackArguments() const { return false; }86};8788bool isMustTailCalleeAnalyzable(const CallBase &CB) {89assert(CB.isMustTailCall());90return CB.getCalledFunction() && !CB.getCalledFunction()->isDeclaration();91}9293} // end anonymous namespace9495char DAE::ID = 0;9697INITIALIZE_PASS(DAE, "deadargelim", "Dead Argument Elimination", false, false)9899namespace {100101/// The DeadArgumentHacking pass, same as dead argument elimination, but deletes102/// arguments to functions which are external. This is only for use by bugpoint.103struct DAH : public DAE {104static char ID;105106DAH() : DAE(ID) {}107108bool shouldHackArguments() const override { return true; }109};110111} // end anonymous namespace112113char DAH::ID = 0;114115INITIALIZE_PASS(DAH, "deadarghaX0r",116"Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)", false,117false)118119/// This pass removes arguments from functions which are not used by the body of120/// the function.121ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); }122123ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); }124125/// If this is an function that takes a ... list, and if llvm.vastart is never126/// called, the varargs list is dead for the function.127bool DeadArgumentEliminationPass::deleteDeadVarargs(Function &F) {128assert(F.getFunctionType()->isVarArg() && "Function isn't varargs!");129if (F.isDeclaration() || !F.hasLocalLinkage())130return false;131132// Ensure that the function is only directly called.133if (F.hasAddressTaken())134return false;135136// Don't touch naked functions. The assembly might be using an argument, or137// otherwise rely on the frame layout in a way that this analysis will not138// see.139if (F.hasFnAttribute(Attribute::Naked)) {140return false;141}142143// Okay, we know we can transform this function if safe. Scan its body144// looking for calls marked musttail or calls to llvm.vastart.145for (BasicBlock &BB : F) {146for (Instruction &I : BB) {147CallInst *CI = dyn_cast<CallInst>(&I);148if (!CI)149continue;150if (CI->isMustTailCall())151return false;152if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {153if (II->getIntrinsicID() == Intrinsic::vastart)154return false;155}156}157}158159// If we get here, there are no calls to llvm.vastart in the function body,160// remove the "..." and adjust all the calls.161162// Start by computing a new prototype for the function, which is the same as163// the old function, but doesn't have isVarArg set.164FunctionType *FTy = F.getFunctionType();165166std::vector<Type *> Params(FTy->param_begin(), FTy->param_end());167FunctionType *NFTy = FunctionType::get(FTy->getReturnType(), Params, false);168unsigned NumArgs = Params.size();169170// Create the new function body and insert it into the module...171Function *NF = Function::Create(NFTy, F.getLinkage(), F.getAddressSpace());172NF->copyAttributesFrom(&F);173NF->setComdat(F.getComdat());174F.getParent()->getFunctionList().insert(F.getIterator(), NF);175NF->takeName(&F);176NF->IsNewDbgInfoFormat = F.IsNewDbgInfoFormat;177178// Loop over all the callers of the function, transforming the call sites179// to pass in a smaller number of arguments into the new function.180//181std::vector<Value *> Args;182for (User *U : llvm::make_early_inc_range(F.users())) {183CallBase *CB = dyn_cast<CallBase>(U);184if (!CB)185continue;186187// Pass all the same arguments.188Args.assign(CB->arg_begin(), CB->arg_begin() + NumArgs);189190// Drop any attributes that were on the vararg arguments.191AttributeList PAL = CB->getAttributes();192if (!PAL.isEmpty()) {193SmallVector<AttributeSet, 8> ArgAttrs;194for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)195ArgAttrs.push_back(PAL.getParamAttrs(ArgNo));196PAL = AttributeList::get(F.getContext(), PAL.getFnAttrs(),197PAL.getRetAttrs(), ArgAttrs);198}199200SmallVector<OperandBundleDef, 1> OpBundles;201CB->getOperandBundlesAsDefs(OpBundles);202203CallBase *NewCB = nullptr;204if (InvokeInst *II = dyn_cast<InvokeInst>(CB)) {205NewCB = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),206Args, OpBundles, "", CB->getIterator());207} else {208NewCB = CallInst::Create(NF, Args, OpBundles, "", CB->getIterator());209cast<CallInst>(NewCB)->setTailCallKind(210cast<CallInst>(CB)->getTailCallKind());211}212NewCB->setCallingConv(CB->getCallingConv());213NewCB->setAttributes(PAL);214NewCB->copyMetadata(*CB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});215216Args.clear();217218if (!CB->use_empty())219CB->replaceAllUsesWith(NewCB);220221NewCB->takeName(CB);222223// Finally, remove the old call from the program, reducing the use-count of224// F.225CB->eraseFromParent();226}227228// Since we have now created the new function, splice the body of the old229// function right into the new function, leaving the old rotting hulk of the230// function empty.231NF->splice(NF->begin(), &F);232233// Loop over the argument list, transferring uses of the old arguments over to234// the new arguments, also transferring over the names as well. While we're235// at it, remove the dead arguments from the DeadArguments list.236for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(),237I2 = NF->arg_begin();238I != E; ++I, ++I2) {239// Move the name and users over to the new version.240I->replaceAllUsesWith(&*I2);241I2->takeName(&*I);242}243244// Clone metadata from the old function, including debug info descriptor.245SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;246F.getAllMetadata(MDs);247for (auto [KindID, Node] : MDs)248NF->addMetadata(KindID, *Node);249250// Fix up any BlockAddresses that refer to the function.251F.replaceAllUsesWith(NF);252// Delete the bitcast that we just created, so that NF does not253// appear to be address-taken.254NF->removeDeadConstantUsers();255// Finally, nuke the old function.256F.eraseFromParent();257return true;258}259260/// Checks if the given function has any arguments that are unused, and changes261/// the caller parameters to be poison instead.262bool DeadArgumentEliminationPass::removeDeadArgumentsFromCallers(Function &F) {263// We cannot change the arguments if this TU does not define the function or264// if the linker may choose a function body from another TU, even if the265// nominal linkage indicates that other copies of the function have the same266// semantics. In the below example, the dead load from %p may not have been267// eliminated from the linker-chosen copy of f, so replacing %p with poison268// in callers may introduce undefined behavior.269//270// define linkonce_odr void @f(i32* %p) {271// %v = load i32 %p272// ret void273// }274if (!F.hasExactDefinition())275return false;276277// Functions with local linkage should already have been handled, except if278// they are fully alive (e.g., called indirectly) and except for the fragile279// (variadic) ones. In these cases, we may still be able to improve their280// statically known call sites.281if ((F.hasLocalLinkage() && !LiveFunctions.count(&F)) &&282!F.getFunctionType()->isVarArg())283return false;284285// Don't touch naked functions. The assembly might be using an argument, or286// otherwise rely on the frame layout in a way that this analysis will not287// see.288if (F.hasFnAttribute(Attribute::Naked))289return false;290291if (F.use_empty())292return false;293294SmallVector<unsigned, 8> UnusedArgs;295bool Changed = false;296297AttributeMask UBImplyingAttributes =298AttributeFuncs::getUBImplyingAttributes();299for (Argument &Arg : F.args()) {300if (!Arg.hasSwiftErrorAttr() && Arg.use_empty() &&301!Arg.hasPassPointeeByValueCopyAttr()) {302if (Arg.isUsedByMetadata()) {303Arg.replaceAllUsesWith(PoisonValue::get(Arg.getType()));304Changed = true;305}306UnusedArgs.push_back(Arg.getArgNo());307F.removeParamAttrs(Arg.getArgNo(), UBImplyingAttributes);308}309}310311if (UnusedArgs.empty())312return false;313314for (Use &U : F.uses()) {315CallBase *CB = dyn_cast<CallBase>(U.getUser());316if (!CB || !CB->isCallee(&U) ||317CB->getFunctionType() != F.getFunctionType())318continue;319320// Now go through all unused args and replace them with poison.321for (unsigned ArgNo : UnusedArgs) {322Value *Arg = CB->getArgOperand(ArgNo);323CB->setArgOperand(ArgNo, PoisonValue::get(Arg->getType()));324CB->removeParamAttrs(ArgNo, UBImplyingAttributes);325326++NumArgumentsReplacedWithPoison;327Changed = true;328}329}330331return Changed;332}333334/// Convenience function that returns the number of return values. It returns 0335/// for void functions and 1 for functions not returning a struct. It returns336/// the number of struct elements for functions returning a struct.337static unsigned numRetVals(const Function *F) {338Type *RetTy = F->getReturnType();339if (RetTy->isVoidTy())340return 0;341if (StructType *STy = dyn_cast<StructType>(RetTy))342return STy->getNumElements();343if (ArrayType *ATy = dyn_cast<ArrayType>(RetTy))344return ATy->getNumElements();345return 1;346}347348/// Returns the sub-type a function will return at a given Idx. Should349/// correspond to the result type of an ExtractValue instruction executed with350/// just that one Idx (i.e. only top-level structure is considered).351static Type *getRetComponentType(const Function *F, unsigned Idx) {352Type *RetTy = F->getReturnType();353assert(!RetTy->isVoidTy() && "void type has no subtype");354355if (StructType *STy = dyn_cast<StructType>(RetTy))356return STy->getElementType(Idx);357if (ArrayType *ATy = dyn_cast<ArrayType>(RetTy))358return ATy->getElementType();359return RetTy;360}361362/// Checks Use for liveness in LiveValues. If Use is not live, it adds Use to363/// the MaybeLiveUses argument. Returns the determined liveness of Use.364DeadArgumentEliminationPass::Liveness365DeadArgumentEliminationPass::markIfNotLive(RetOrArg Use,366UseVector &MaybeLiveUses) {367// We're live if our use or its Function is already marked as live.368if (isLive(Use))369return Live;370371// We're maybe live otherwise, but remember that we must become live if372// Use becomes live.373MaybeLiveUses.push_back(Use);374return MaybeLive;375}376377/// Looks at a single use of an argument or return value and determines if it378/// should be alive or not. Adds this use to MaybeLiveUses if it causes the379/// used value to become MaybeLive.380///381/// RetValNum is the return value number to use when this use is used in a382/// return instruction. This is used in the recursion, you should always leave383/// it at 0.384DeadArgumentEliminationPass::Liveness385DeadArgumentEliminationPass::surveyUse(const Use *U, UseVector &MaybeLiveUses,386unsigned RetValNum) {387const User *V = U->getUser();388if (const ReturnInst *RI = dyn_cast<ReturnInst>(V)) {389// The value is returned from a function. It's only live when the390// function's return value is live. We use RetValNum here, for the case391// that U is really a use of an insertvalue instruction that uses the392// original Use.393const Function *F = RI->getParent()->getParent();394if (RetValNum != -1U) {395RetOrArg Use = createRet(F, RetValNum);396// We might be live, depending on the liveness of Use.397return markIfNotLive(Use, MaybeLiveUses);398}399400DeadArgumentEliminationPass::Liveness Result = MaybeLive;401for (unsigned Ri = 0; Ri < numRetVals(F); ++Ri) {402RetOrArg Use = createRet(F, Ri);403// We might be live, depending on the liveness of Use. If any404// sub-value is live, then the entire value is considered live. This405// is a conservative choice, and better tracking is possible.406DeadArgumentEliminationPass::Liveness SubResult =407markIfNotLive(Use, MaybeLiveUses);408if (Result != Live)409Result = SubResult;410}411return Result;412}413414if (const InsertValueInst *IV = dyn_cast<InsertValueInst>(V)) {415if (U->getOperandNo() != InsertValueInst::getAggregateOperandIndex() &&416IV->hasIndices())417// The use we are examining is inserted into an aggregate. Our liveness418// depends on all uses of that aggregate, but if it is used as a return419// value, only index at which we were inserted counts.420RetValNum = *IV->idx_begin();421422// Note that if we are used as the aggregate operand to the insertvalue,423// we don't change RetValNum, but do survey all our uses.424425Liveness Result = MaybeLive;426for (const Use &UU : IV->uses()) {427Result = surveyUse(&UU, MaybeLiveUses, RetValNum);428if (Result == Live)429break;430}431return Result;432}433434if (const auto *CB = dyn_cast<CallBase>(V)) {435const Function *F = CB->getCalledFunction();436if (F) {437// Used in a direct call.438439// The function argument is live if it is used as a bundle operand.440if (CB->isBundleOperand(U))441return Live;442443// Find the argument number. We know for sure that this use is an444// argument, since if it was the function argument this would be an445// indirect call and that we know can't be looking at a value of the446// label type (for the invoke instruction).447unsigned ArgNo = CB->getArgOperandNo(U);448449if (ArgNo >= F->getFunctionType()->getNumParams())450// The value is passed in through a vararg! Must be live.451return Live;452453assert(CB->getArgOperand(ArgNo) == CB->getOperand(U->getOperandNo()) &&454"Argument is not where we expected it");455456// Value passed to a normal call. It's only live when the corresponding457// argument to the called function turns out live.458RetOrArg Use = createArg(F, ArgNo);459return markIfNotLive(Use, MaybeLiveUses);460}461}462// Used in any other way? Value must be live.463return Live;464}465466/// Looks at all the uses of the given value467/// Returns the Liveness deduced from the uses of this value.468///469/// Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses. If470/// the result is Live, MaybeLiveUses might be modified but its content should471/// be ignored (since it might not be complete).472DeadArgumentEliminationPass::Liveness473DeadArgumentEliminationPass::surveyUses(const Value *V,474UseVector &MaybeLiveUses) {475// Assume it's dead (which will only hold if there are no uses at all..).476Liveness Result = MaybeLive;477// Check each use.478for (const Use &U : V->uses()) {479Result = surveyUse(&U, MaybeLiveUses);480if (Result == Live)481break;482}483return Result;484}485486/// Performs the initial survey of the specified function, checking out whether487/// it uses any of its incoming arguments or whether any callers use the return488/// value. This fills in the LiveValues set and Uses map.489///490/// We consider arguments of non-internal functions to be intrinsically alive as491/// well as arguments to functions which have their "address taken".492void DeadArgumentEliminationPass::surveyFunction(const Function &F) {493// Functions with inalloca/preallocated parameters are expecting args in a494// particular register and memory layout.495if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca) ||496F.getAttributes().hasAttrSomewhere(Attribute::Preallocated)) {497markLive(F);498return;499}500501// Don't touch naked functions. The assembly might be using an argument, or502// otherwise rely on the frame layout in a way that this analysis will not503// see.504if (F.hasFnAttribute(Attribute::Naked)) {505markLive(F);506return;507}508509unsigned RetCount = numRetVals(&F);510511// Assume all return values are dead512using RetVals = SmallVector<Liveness, 5>;513514RetVals RetValLiveness(RetCount, MaybeLive);515516using RetUses = SmallVector<UseVector, 5>;517518// These vectors map each return value to the uses that make it MaybeLive, so519// we can add those to the Uses map if the return value really turns out to be520// MaybeLive. Initialized to a list of RetCount empty lists.521RetUses MaybeLiveRetUses(RetCount);522523bool HasMustTailCalls = false;524for (const BasicBlock &BB : F) {525// If we have any returns of `musttail` results - the signature can't526// change527if (const auto *TC = BB.getTerminatingMustTailCall()) {528HasMustTailCalls = true;529// In addition, if the called function is not locally defined (or unknown,530// if this is an indirect call), we can't change the callsite and thus531// can't change this function's signature either.532if (!isMustTailCalleeAnalyzable(*TC)) {533markLive(F);534return;535}536}537}538539if (HasMustTailCalls) {540LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - " << F.getName()541<< " has musttail calls\n");542}543544if (!F.hasLocalLinkage() && (!ShouldHackArguments || F.isIntrinsic())) {545markLive(F);546return;547}548549LLVM_DEBUG(550dbgs() << "DeadArgumentEliminationPass - Inspecting callers for fn: "551<< F.getName() << "\n");552// Keep track of the number of live retvals, so we can skip checks once all553// of them turn out to be live.554unsigned NumLiveRetVals = 0;555556bool HasMustTailCallers = false;557558// Loop all uses of the function.559for (const Use &U : F.uses()) {560// If the function is PASSED IN as an argument, its address has been561// taken.562const auto *CB = dyn_cast<CallBase>(U.getUser());563if (!CB || !CB->isCallee(&U) ||564CB->getFunctionType() != F.getFunctionType()) {565markLive(F);566return;567}568569// The number of arguments for `musttail` call must match the number of570// arguments of the caller571if (CB->isMustTailCall())572HasMustTailCallers = true;573574// If we end up here, we are looking at a direct call to our function.575576// Now, check how our return value(s) is/are used in this caller. Don't577// bother checking return values if all of them are live already.578if (NumLiveRetVals == RetCount)579continue;580581// Check all uses of the return value.582for (const Use &UU : CB->uses()) {583if (ExtractValueInst *Ext = dyn_cast<ExtractValueInst>(UU.getUser())) {584// This use uses a part of our return value, survey the uses of585// that part and store the results for this index only.586unsigned Idx = *Ext->idx_begin();587if (RetValLiveness[Idx] != Live) {588RetValLiveness[Idx] = surveyUses(Ext, MaybeLiveRetUses[Idx]);589if (RetValLiveness[Idx] == Live)590NumLiveRetVals++;591}592} else {593// Used by something else than extractvalue. Survey, but assume that the594// result applies to all sub-values.595UseVector MaybeLiveAggregateUses;596if (surveyUse(&UU, MaybeLiveAggregateUses) == Live) {597NumLiveRetVals = RetCount;598RetValLiveness.assign(RetCount, Live);599break;600}601602for (unsigned Ri = 0; Ri != RetCount; ++Ri) {603if (RetValLiveness[Ri] != Live)604MaybeLiveRetUses[Ri].append(MaybeLiveAggregateUses.begin(),605MaybeLiveAggregateUses.end());606}607}608}609}610611if (HasMustTailCallers) {612LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - " << F.getName()613<< " has musttail callers\n");614}615616// Now we've inspected all callers, record the liveness of our return values.617for (unsigned Ri = 0; Ri != RetCount; ++Ri)618markValue(createRet(&F, Ri), RetValLiveness[Ri], MaybeLiveRetUses[Ri]);619620LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Inspecting args for fn: "621<< F.getName() << "\n");622623// Now, check all of our arguments.624unsigned ArgI = 0;625UseVector MaybeLiveArgUses;626for (Function::const_arg_iterator AI = F.arg_begin(), E = F.arg_end();627AI != E; ++AI, ++ArgI) {628Liveness Result;629if (F.getFunctionType()->isVarArg() || HasMustTailCallers ||630HasMustTailCalls) {631// Variadic functions will already have a va_arg function expanded inside632// them, making them potentially very sensitive to ABI changes resulting633// from removing arguments entirely, so don't. For example AArch64 handles634// register and stack HFAs very differently, and this is reflected in the635// IR which has already been generated.636//637// `musttail` calls to this function restrict argument removal attempts.638// The signature of the caller must match the signature of the function.639//640// `musttail` calls in this function prevents us from changing its641// signature642Result = Live;643} else {644// See what the effect of this use is (recording any uses that cause645// MaybeLive in MaybeLiveArgUses).646Result = surveyUses(&*AI, MaybeLiveArgUses);647}648649// Mark the result.650markValue(createArg(&F, ArgI), Result, MaybeLiveArgUses);651// Clear the vector again for the next iteration.652MaybeLiveArgUses.clear();653}654}655656/// Marks the liveness of RA depending on L. If L is MaybeLive, it also takes657/// all uses in MaybeLiveUses and records them in Uses, such that RA will be658/// marked live if any use in MaybeLiveUses gets marked live later on.659void DeadArgumentEliminationPass::markValue(const RetOrArg &RA, Liveness L,660const UseVector &MaybeLiveUses) {661switch (L) {662case Live:663markLive(RA);664break;665case MaybeLive:666assert(!isLive(RA) && "Use is already live!");667for (const auto &MaybeLiveUse : MaybeLiveUses) {668if (isLive(MaybeLiveUse)) {669// A use is live, so this value is live.670markLive(RA);671break;672}673// Note any uses of this value, so this value can be674// marked live whenever one of the uses becomes live.675Uses.emplace(MaybeLiveUse, RA);676}677break;678}679}680681/// Mark the given Function as alive, meaning that it cannot be changed in any682/// way. Additionally, mark any values that are used as this function's683/// parameters or by its return values (according to Uses) live as well.684void DeadArgumentEliminationPass::markLive(const Function &F) {685LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Intrinsically live fn: "686<< F.getName() << "\n");687// Mark the function as live.688LiveFunctions.insert(&F);689// Mark all arguments as live.690for (unsigned ArgI = 0, E = F.arg_size(); ArgI != E; ++ArgI)691propagateLiveness(createArg(&F, ArgI));692// Mark all return values as live.693for (unsigned Ri = 0, E = numRetVals(&F); Ri != E; ++Ri)694propagateLiveness(createRet(&F, Ri));695}696697/// Mark the given return value or argument as live. Additionally, mark any698/// values that are used by this value (according to Uses) live as well.699void DeadArgumentEliminationPass::markLive(const RetOrArg &RA) {700if (isLive(RA))701return; // Already marked Live.702703LiveValues.insert(RA);704705LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Marking "706<< RA.getDescription() << " live\n");707propagateLiveness(RA);708}709710bool DeadArgumentEliminationPass::isLive(const RetOrArg &RA) {711return LiveFunctions.count(RA.F) || LiveValues.count(RA);712}713714/// Given that RA is a live value, propagate it's liveness to any other values715/// it uses (according to Uses).716void DeadArgumentEliminationPass::propagateLiveness(const RetOrArg &RA) {717// We don't use upper_bound (or equal_range) here, because our recursive call718// to ourselves is likely to cause the upper_bound (which is the first value719// not belonging to RA) to become erased and the iterator invalidated.720UseMap::iterator Begin = Uses.lower_bound(RA);721UseMap::iterator E = Uses.end();722UseMap::iterator I;723for (I = Begin; I != E && I->first == RA; ++I)724markLive(I->second);725726// Erase RA from the Uses map (from the lower bound to wherever we ended up727// after the loop).728Uses.erase(Begin, I);729}730731/// Remove any arguments and return values from F that are not in LiveValues.732/// Transform the function and all the callees of the function to not have these733/// arguments and return values.734bool DeadArgumentEliminationPass::removeDeadStuffFromFunction(Function *F) {735// Don't modify fully live functions736if (LiveFunctions.count(F))737return false;738739// Start by computing a new prototype for the function, which is the same as740// the old function, but has fewer arguments and a different return type.741FunctionType *FTy = F->getFunctionType();742std::vector<Type *> Params;743744// Keep track of if we have a live 'returned' argument745bool HasLiveReturnedArg = false;746747// Set up to build a new list of parameter attributes.748SmallVector<AttributeSet, 8> ArgAttrVec;749const AttributeList &PAL = F->getAttributes();750751// Remember which arguments are still alive.752SmallVector<bool, 10> ArgAlive(FTy->getNumParams(), false);753// Construct the new parameter list from non-dead arguments. Also construct754// a new set of parameter attributes to correspond. Skip the first parameter755// attribute, since that belongs to the return value.756unsigned ArgI = 0;757for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;758++I, ++ArgI) {759RetOrArg Arg = createArg(F, ArgI);760if (LiveValues.erase(Arg)) {761Params.push_back(I->getType());762ArgAlive[ArgI] = true;763ArgAttrVec.push_back(PAL.getParamAttrs(ArgI));764HasLiveReturnedArg |= PAL.hasParamAttr(ArgI, Attribute::Returned);765} else {766++NumArgumentsEliminated;767LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Removing argument "768<< ArgI << " (" << I->getName() << ") from "769<< F->getName() << "\n");770}771}772773// Find out the new return value.774Type *RetTy = FTy->getReturnType();775Type *NRetTy = nullptr;776unsigned RetCount = numRetVals(F);777778// -1 means unused, other numbers are the new index779SmallVector<int, 5> NewRetIdxs(RetCount, -1);780std::vector<Type *> RetTypes;781782// If there is a function with a live 'returned' argument but a dead return783// value, then there are two possible actions:784// 1) Eliminate the return value and take off the 'returned' attribute on the785// argument.786// 2) Retain the 'returned' attribute and treat the return value (but not the787// entire function) as live so that it is not eliminated.788//789// It's not clear in the general case which option is more profitable because,790// even in the absence of explicit uses of the return value, code generation791// is free to use the 'returned' attribute to do things like eliding792// save/restores of registers across calls. Whether this happens is target and793// ABI-specific as well as depending on the amount of register pressure, so794// there's no good way for an IR-level pass to figure this out.795//796// Fortunately, the only places where 'returned' is currently generated by797// the FE are places where 'returned' is basically free and almost always a798// performance win, so the second option can just be used always for now.799//800// This should be revisited if 'returned' is ever applied more liberally.801if (RetTy->isVoidTy() || HasLiveReturnedArg) {802NRetTy = RetTy;803} else {804// Look at each of the original return values individually.805for (unsigned Ri = 0; Ri != RetCount; ++Ri) {806RetOrArg Ret = createRet(F, Ri);807if (LiveValues.erase(Ret)) {808RetTypes.push_back(getRetComponentType(F, Ri));809NewRetIdxs[Ri] = RetTypes.size() - 1;810} else {811++NumRetValsEliminated;812LLVM_DEBUG(813dbgs() << "DeadArgumentEliminationPass - Removing return value "814<< Ri << " from " << F->getName() << "\n");815}816}817if (RetTypes.size() > 1) {818// More than one return type? Reduce it down to size.819if (StructType *STy = dyn_cast<StructType>(RetTy)) {820// Make the new struct packed if we used to return a packed struct821// already.822NRetTy = StructType::get(STy->getContext(), RetTypes, STy->isPacked());823} else {824assert(isa<ArrayType>(RetTy) && "unexpected multi-value return");825NRetTy = ArrayType::get(RetTypes[0], RetTypes.size());826}827} else if (RetTypes.size() == 1)828// One return type? Just a simple value then, but only if we didn't use to829// return a struct with that simple value before.830NRetTy = RetTypes.front();831else if (RetTypes.empty())832// No return types? Make it void, but only if we didn't use to return {}.833NRetTy = Type::getVoidTy(F->getContext());834}835836assert(NRetTy && "No new return type found?");837838// The existing function return attributes.839AttrBuilder RAttrs(F->getContext(), PAL.getRetAttrs());840841// Remove any incompatible attributes, but only if we removed all return842// values. Otherwise, ensure that we don't have any conflicting attributes843// here. Currently, this should not be possible, but special handling might be844// required when new return value attributes are added.845if (NRetTy->isVoidTy())846RAttrs.remove(AttributeFuncs::typeIncompatible(NRetTy));847else848assert(!RAttrs.overlaps(AttributeFuncs::typeIncompatible(NRetTy)) &&849"Return attributes no longer compatible?");850851AttributeSet RetAttrs = AttributeSet::get(F->getContext(), RAttrs);852853// Strip allocsize attributes. They might refer to the deleted arguments.854AttributeSet FnAttrs =855PAL.getFnAttrs().removeAttribute(F->getContext(), Attribute::AllocSize);856857// Reconstruct the AttributesList based on the vector we constructed.858assert(ArgAttrVec.size() == Params.size());859AttributeList NewPAL =860AttributeList::get(F->getContext(), FnAttrs, RetAttrs, ArgAttrVec);861862// Create the new function type based on the recomputed parameters.863FunctionType *NFTy = FunctionType::get(NRetTy, Params, FTy->isVarArg());864865// No change?866if (NFTy == FTy)867return false;868869// Create the new function body and insert it into the module...870Function *NF = Function::Create(NFTy, F->getLinkage(), F->getAddressSpace());871NF->copyAttributesFrom(F);872NF->setComdat(F->getComdat());873NF->setAttributes(NewPAL);874// Insert the new function before the old function, so we won't be processing875// it again.876F->getParent()->getFunctionList().insert(F->getIterator(), NF);877NF->takeName(F);878NF->IsNewDbgInfoFormat = F->IsNewDbgInfoFormat;879880// Loop over all the callers of the function, transforming the call sites to881// pass in a smaller number of arguments into the new function.882std::vector<Value *> Args;883while (!F->use_empty()) {884CallBase &CB = cast<CallBase>(*F->user_back());885886ArgAttrVec.clear();887const AttributeList &CallPAL = CB.getAttributes();888889// Adjust the call return attributes in case the function was changed to890// return void.891AttrBuilder RAttrs(F->getContext(), CallPAL.getRetAttrs());892RAttrs.remove(AttributeFuncs::typeIncompatible(NRetTy));893AttributeSet RetAttrs = AttributeSet::get(F->getContext(), RAttrs);894895// Declare these outside of the loops, so we can reuse them for the second896// loop, which loops the varargs.897auto *I = CB.arg_begin();898unsigned Pi = 0;899// Loop over those operands, corresponding to the normal arguments to the900// original function, and add those that are still alive.901for (unsigned E = FTy->getNumParams(); Pi != E; ++I, ++Pi)902if (ArgAlive[Pi]) {903Args.push_back(*I);904// Get original parameter attributes, but skip return attributes.905AttributeSet Attrs = CallPAL.getParamAttrs(Pi);906if (NRetTy != RetTy && Attrs.hasAttribute(Attribute::Returned)) {907// If the return type has changed, then get rid of 'returned' on the908// call site. The alternative is to make all 'returned' attributes on909// call sites keep the return value alive just like 'returned'910// attributes on function declaration, but it's less clearly a win and911// this is not an expected case anyway912ArgAttrVec.push_back(AttributeSet::get(913F->getContext(), AttrBuilder(F->getContext(), Attrs)914.removeAttribute(Attribute::Returned)));915} else {916// Otherwise, use the original attributes.917ArgAttrVec.push_back(Attrs);918}919}920921// Push any varargs arguments on the list. Don't forget their attributes.922for (auto *E = CB.arg_end(); I != E; ++I, ++Pi) {923Args.push_back(*I);924ArgAttrVec.push_back(CallPAL.getParamAttrs(Pi));925}926927// Reconstruct the AttributesList based on the vector we constructed.928assert(ArgAttrVec.size() == Args.size());929930// Again, be sure to remove any allocsize attributes, since their indices931// may now be incorrect.932AttributeSet FnAttrs = CallPAL.getFnAttrs().removeAttribute(933F->getContext(), Attribute::AllocSize);934935AttributeList NewCallPAL =936AttributeList::get(F->getContext(), FnAttrs, RetAttrs, ArgAttrVec);937938SmallVector<OperandBundleDef, 1> OpBundles;939CB.getOperandBundlesAsDefs(OpBundles);940941CallBase *NewCB = nullptr;942if (InvokeInst *II = dyn_cast<InvokeInst>(&CB)) {943NewCB = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),944Args, OpBundles, "", CB.getParent());945} else {946NewCB = CallInst::Create(NFTy, NF, Args, OpBundles, "", CB.getIterator());947cast<CallInst>(NewCB)->setTailCallKind(948cast<CallInst>(&CB)->getTailCallKind());949}950NewCB->setCallingConv(CB.getCallingConv());951NewCB->setAttributes(NewCallPAL);952NewCB->copyMetadata(CB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});953Args.clear();954ArgAttrVec.clear();955956if (!CB.use_empty() || CB.isUsedByMetadata()) {957if (NewCB->getType() == CB.getType()) {958// Return type not changed? Just replace users then.959CB.replaceAllUsesWith(NewCB);960NewCB->takeName(&CB);961} else if (NewCB->getType()->isVoidTy()) {962// If the return value is dead, replace any uses of it with poison963// (any non-debug value uses will get removed later on).964if (!CB.getType()->isX86_MMXTy())965CB.replaceAllUsesWith(PoisonValue::get(CB.getType()));966} else {967assert((RetTy->isStructTy() || RetTy->isArrayTy()) &&968"Return type changed, but not into a void. The old return type"969" must have been a struct or an array!");970Instruction *InsertPt = &CB;971if (InvokeInst *II = dyn_cast<InvokeInst>(&CB)) {972BasicBlock *NewEdge =973SplitEdge(NewCB->getParent(), II->getNormalDest());974InsertPt = &*NewEdge->getFirstInsertionPt();975}976977// We used to return a struct or array. Instead of doing smart stuff978// with all the uses, we will just rebuild it using extract/insertvalue979// chaining and let instcombine clean that up.980//981// Start out building up our return value from poison982Value *RetVal = PoisonValue::get(RetTy);983for (unsigned Ri = 0; Ri != RetCount; ++Ri)984if (NewRetIdxs[Ri] != -1) {985Value *V;986IRBuilder<NoFolder> IRB(InsertPt);987if (RetTypes.size() > 1)988// We are still returning a struct, so extract the value from our989// return value990V = IRB.CreateExtractValue(NewCB, NewRetIdxs[Ri], "newret");991else992// We are now returning a single element, so just insert that993V = NewCB;994// Insert the value at the old position995RetVal = IRB.CreateInsertValue(RetVal, V, Ri, "oldret");996}997// Now, replace all uses of the old call instruction with the return998// struct we built999CB.replaceAllUsesWith(RetVal);1000NewCB->takeName(&CB);1001}1002}10031004// Finally, remove the old call from the program, reducing the use-count of1005// F.1006CB.eraseFromParent();1007}10081009// Since we have now created the new function, splice the body of the old1010// function right into the new function, leaving the old rotting hulk of the1011// function empty.1012NF->splice(NF->begin(), F);10131014// Loop over the argument list, transferring uses of the old arguments over to1015// the new arguments, also transferring over the names as well.1016ArgI = 0;1017for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),1018I2 = NF->arg_begin();1019I != E; ++I, ++ArgI)1020if (ArgAlive[ArgI]) {1021// If this is a live argument, move the name and users over to the new1022// version.1023I->replaceAllUsesWith(&*I2);1024I2->takeName(&*I);1025++I2;1026} else {1027// If this argument is dead, replace any uses of it with poison1028// (any non-debug value uses will get removed later on).1029if (!I->getType()->isX86_MMXTy())1030I->replaceAllUsesWith(PoisonValue::get(I->getType()));1031}10321033// If we change the return value of the function we must rewrite any return1034// instructions. Check this now.1035if (F->getReturnType() != NF->getReturnType())1036for (BasicBlock &BB : *NF)1037if (ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator())) {1038IRBuilder<NoFolder> IRB(RI);1039Value *RetVal = nullptr;10401041if (!NFTy->getReturnType()->isVoidTy()) {1042assert(RetTy->isStructTy() || RetTy->isArrayTy());1043// The original return value was a struct or array, insert1044// extractvalue/insertvalue chains to extract only the values we need1045// to return and insert them into our new result.1046// This does generate messy code, but we'll let it to instcombine to1047// clean that up.1048Value *OldRet = RI->getOperand(0);1049// Start out building up our return value from poison1050RetVal = PoisonValue::get(NRetTy);1051for (unsigned RetI = 0; RetI != RetCount; ++RetI)1052if (NewRetIdxs[RetI] != -1) {1053Value *EV = IRB.CreateExtractValue(OldRet, RetI, "oldret");10541055if (RetTypes.size() > 1) {1056// We're still returning a struct, so reinsert the value into1057// our new return value at the new index10581059RetVal = IRB.CreateInsertValue(RetVal, EV, NewRetIdxs[RetI],1060"newret");1061} else {1062// We are now only returning a simple value, so just return the1063// extracted value.1064RetVal = EV;1065}1066}1067}1068// Replace the return instruction with one returning the new return1069// value (possibly 0 if we became void).1070auto *NewRet =1071ReturnInst::Create(F->getContext(), RetVal, RI->getIterator());1072NewRet->setDebugLoc(RI->getDebugLoc());1073RI->eraseFromParent();1074}10751076// Clone metadata from the old function, including debug info descriptor.1077SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;1078F->getAllMetadata(MDs);1079for (auto [KindID, Node] : MDs)1080NF->addMetadata(KindID, *Node);10811082// If either the return value(s) or argument(s) are removed, then probably the1083// function does not follow standard calling conventions anymore. Hence, add1084// DW_CC_nocall to DISubroutineType to inform debugger that it may not be safe1085// to call this function or try to interpret the return value.1086if (NFTy != FTy && NF->getSubprogram()) {1087DISubprogram *SP = NF->getSubprogram();1088auto Temp = SP->getType()->cloneWithCC(llvm::dwarf::DW_CC_nocall);1089SP->replaceType(MDNode::replaceWithPermanent(std::move(Temp)));1090}10911092// Now that the old function is dead, delete it.1093F->eraseFromParent();10941095return true;1096}10971098void DeadArgumentEliminationPass::propagateVirtMustcallLiveness(1099const Module &M) {1100// If a function was marked "live", and it has musttail callers, they in turn1101// can't change either.1102LiveFuncSet NewLiveFuncs(LiveFunctions);1103while (!NewLiveFuncs.empty()) {1104LiveFuncSet Temp;1105for (const auto *F : NewLiveFuncs)1106for (const auto *U : F->users())1107if (const auto *CB = dyn_cast<CallBase>(U))1108if (CB->isMustTailCall())1109if (!LiveFunctions.count(CB->getParent()->getParent()))1110Temp.insert(CB->getParent()->getParent());1111NewLiveFuncs.clear();1112NewLiveFuncs.insert(Temp.begin(), Temp.end());1113for (const auto *F : Temp)1114markLive(*F);1115}1116}11171118PreservedAnalyses DeadArgumentEliminationPass::run(Module &M,1119ModuleAnalysisManager &) {1120bool Changed = false;11211122// First pass: Do a simple check to see if any functions can have their "..."1123// removed. We can do this if they never call va_start. This loop cannot be1124// fused with the next loop, because deleting a function invalidates1125// information computed while surveying other functions.1126LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Deleting dead varargs\n");1127for (Function &F : llvm::make_early_inc_range(M))1128if (F.getFunctionType()->isVarArg())1129Changed |= deleteDeadVarargs(F);11301131// Second phase: Loop through the module, determining which arguments are1132// live. We assume all arguments are dead unless proven otherwise (allowing us1133// to determine that dead arguments passed into recursive functions are dead).1134LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Determining liveness\n");1135for (auto &F : M)1136surveyFunction(F);11371138propagateVirtMustcallLiveness(M);11391140// Now, remove all dead arguments and return values from each function in1141// turn. We use make_early_inc_range here because functions will probably get1142// removed (i.e. replaced by new ones).1143for (Function &F : llvm::make_early_inc_range(M))1144Changed |= removeDeadStuffFromFunction(&F);11451146// Finally, look for any unused parameters in functions with non-local1147// linkage and replace the passed in parameters with poison.1148for (auto &F : M)1149Changed |= removeDeadArgumentsFromCallers(F);11501151if (!Changed)1152return PreservedAnalyses::all();1153return PreservedAnalyses::none();1154}115511561157