Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/IPO/MergeFunctions.cpp
35266 views
//===- MergeFunctions.cpp - Merge identical functions ---------------------===//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 looks for equivalent functions that are mergable and folds them.9//10// Order relation is defined on set of functions. It was made through11// special function comparison procedure that returns12// 0 when functions are equal,13// -1 when Left function is less than right function, and14// 1 for opposite case. We need total-ordering, so we need to maintain15// four properties on the functions set:16// a <= a (reflexivity)17// if a <= b and b <= a then a = b (antisymmetry)18// if a <= b and b <= c then a <= c (transitivity).19// for all a and b: a <= b or b <= a (totality).20//21// Comparison iterates through each instruction in each basic block.22// Functions are kept on binary tree. For each new function F we perform23// lookup in binary tree.24// In practice it works the following way:25// -- We define Function* container class with custom "operator<" (FunctionPtr).26// -- "FunctionPtr" instances are stored in std::set collection, so every27// std::set::insert operation will give you result in log(N) time.28//29// As an optimization, a hash of the function structure is calculated first, and30// two functions are only compared if they have the same hash. This hash is31// cheap to compute, and has the property that if function F == G according to32// the comparison function, then hash(F) == hash(G). This consistency property33// is critical to ensuring all possible merging opportunities are exploited.34// Collisions in the hash affect the speed of the pass but not the correctness35// or determinism of the resulting transformation.36//37// When a match is found the functions are folded. If both functions are38// overridable, we move the functionality into a new internal function and39// leave two overridable thunks to it.40//41//===----------------------------------------------------------------------===//42//43// Future work:44//45// * virtual functions.46//47// Many functions have their address taken by the virtual function table for48// the object they belong to. However, as long as it's only used for a lookup49// and call, this is irrelevant, and we'd like to fold such functions.50//51// * be smarter about bitcasts.52//53// In order to fold functions, we will sometimes add either bitcast instructions54// or bitcast constant expressions. Unfortunately, this can confound further55// analysis since the two functions differ where one has a bitcast and the56// other doesn't. We should learn to look through bitcasts.57//58// * Compare complex types with pointer types inside.59// * Compare cross-reference cases.60// * Compare complex expressions.61//62// All the three issues above could be described as ability to prove that63// fA == fB == fC == fE == fF == fG in example below:64//65// void fA() {66// fB();67// }68// void fB() {69// fA();70// }71//72// void fE() {73// fF();74// }75// void fF() {76// fG();77// }78// void fG() {79// fE();80// }81//82// Simplest cross-reference case (fA <--> fB) was implemented in previous83// versions of MergeFunctions, though it presented only in two function pairs84// in test-suite (that counts >50k functions)85// Though possibility to detect complex cross-referencing (e.g.: A->B->C->D->A)86// could cover much more cases.87//88//===----------------------------------------------------------------------===//8990#include "llvm/Transforms/IPO/MergeFunctions.h"91#include "llvm/ADT/ArrayRef.h"92#include "llvm/ADT/SmallVector.h"93#include "llvm/ADT/Statistic.h"94#include "llvm/IR/Argument.h"95#include "llvm/IR/BasicBlock.h"96#include "llvm/IR/Constant.h"97#include "llvm/IR/Constants.h"98#include "llvm/IR/DebugInfoMetadata.h"99#include "llvm/IR/DebugLoc.h"100#include "llvm/IR/DerivedTypes.h"101#include "llvm/IR/Function.h"102#include "llvm/IR/GlobalValue.h"103#include "llvm/IR/IRBuilder.h"104#include "llvm/IR/InstrTypes.h"105#include "llvm/IR/Instruction.h"106#include "llvm/IR/Instructions.h"107#include "llvm/IR/IntrinsicInst.h"108#include "llvm/IR/Module.h"109#include "llvm/IR/StructuralHash.h"110#include "llvm/IR/Type.h"111#include "llvm/IR/Use.h"112#include "llvm/IR/User.h"113#include "llvm/IR/Value.h"114#include "llvm/IR/ValueHandle.h"115#include "llvm/Support/Casting.h"116#include "llvm/Support/CommandLine.h"117#include "llvm/Support/Debug.h"118#include "llvm/Support/raw_ostream.h"119#include "llvm/Transforms/IPO.h"120#include "llvm/Transforms/Utils/FunctionComparator.h"121#include "llvm/Transforms/Utils/ModuleUtils.h"122#include <algorithm>123#include <cassert>124#include <iterator>125#include <set>126#include <utility>127#include <vector>128129using namespace llvm;130131#define DEBUG_TYPE "mergefunc"132133STATISTIC(NumFunctionsMerged, "Number of functions merged");134STATISTIC(NumThunksWritten, "Number of thunks generated");135STATISTIC(NumAliasesWritten, "Number of aliases generated");136STATISTIC(NumDoubleWeak, "Number of new functions created");137138static cl::opt<unsigned> NumFunctionsForVerificationCheck(139"mergefunc-verify",140cl::desc("How many functions in a module could be used for "141"MergeFunctions to pass a basic correctness check. "142"'0' disables this check. Works only with '-debug' key."),143cl::init(0), cl::Hidden);144145// Under option -mergefunc-preserve-debug-info we:146// - Do not create a new function for a thunk.147// - Retain the debug info for a thunk's parameters (and associated148// instructions for the debug info) from the entry block.149// Note: -debug will display the algorithm at work.150// - Create debug-info for the call (to the shared implementation) made by151// a thunk and its return value.152// - Erase the rest of the function, retaining the (minimally sized) entry153// block to create a thunk.154// - Preserve a thunk's call site to point to the thunk even when both occur155// within the same translation unit, to aid debugability. Note that this156// behaviour differs from the underlying -mergefunc implementation which157// modifies the thunk's call site to point to the shared implementation158// when both occur within the same translation unit.159static cl::opt<bool>160MergeFunctionsPDI("mergefunc-preserve-debug-info", cl::Hidden,161cl::init(false),162cl::desc("Preserve debug info in thunk when mergefunc "163"transformations are made."));164165static cl::opt<bool>166MergeFunctionsAliases("mergefunc-use-aliases", cl::Hidden,167cl::init(false),168cl::desc("Allow mergefunc to create aliases"));169170namespace {171172class FunctionNode {173mutable AssertingVH<Function> F;174IRHash Hash;175176public:177// Note the hash is recalculated potentially multiple times, but it is cheap.178FunctionNode(Function *F) : F(F), Hash(StructuralHash(*F)) {}179180Function *getFunc() const { return F; }181IRHash getHash() const { return Hash; }182183/// Replace the reference to the function F by the function G, assuming their184/// implementations are equal.185void replaceBy(Function *G) const {186F = G;187}188};189190/// MergeFunctions finds functions which will generate identical machine code,191/// by considering all pointer types to be equivalent. Once identified,192/// MergeFunctions will fold them by replacing a call to one to a call to a193/// bitcast of the other.194class MergeFunctions {195public:196MergeFunctions() : FnTree(FunctionNodeCmp(&GlobalNumbers)) {197}198199bool runOnModule(Module &M);200201private:202// The function comparison operator is provided here so that FunctionNodes do203// not need to become larger with another pointer.204class FunctionNodeCmp {205GlobalNumberState* GlobalNumbers;206207public:208FunctionNodeCmp(GlobalNumberState* GN) : GlobalNumbers(GN) {}209210bool operator()(const FunctionNode &LHS, const FunctionNode &RHS) const {211// Order first by hashes, then full function comparison.212if (LHS.getHash() != RHS.getHash())213return LHS.getHash() < RHS.getHash();214FunctionComparator FCmp(LHS.getFunc(), RHS.getFunc(), GlobalNumbers);215return FCmp.compare() < 0;216}217};218using FnTreeType = std::set<FunctionNode, FunctionNodeCmp>;219220GlobalNumberState GlobalNumbers;221222/// A work queue of functions that may have been modified and should be223/// analyzed again.224std::vector<WeakTrackingVH> Deferred;225226/// Set of values marked as used in llvm.used and llvm.compiler.used.227SmallPtrSet<GlobalValue *, 4> Used;228229#ifndef NDEBUG230/// Checks the rules of order relation introduced among functions set.231/// Returns true, if check has been passed, and false if failed.232bool doFunctionalCheck(std::vector<WeakTrackingVH> &Worklist);233#endif234235/// Insert a ComparableFunction into the FnTree, or merge it away if it's236/// equal to one that's already present.237bool insert(Function *NewFunction);238239/// Remove a Function from the FnTree and queue it up for a second sweep of240/// analysis.241void remove(Function *F);242243/// Find the functions that use this Value and remove them from FnTree and244/// queue the functions.245void removeUsers(Value *V);246247/// Replace all direct calls of Old with calls of New. Will bitcast New if248/// necessary to make types match.249void replaceDirectCallers(Function *Old, Function *New);250251/// Merge two equivalent functions. Upon completion, G may be deleted, or may252/// be converted into a thunk. In either case, it should never be visited253/// again.254void mergeTwoFunctions(Function *F, Function *G);255256/// Fill PDIUnrelatedWL with instructions from the entry block that are257/// unrelated to parameter related debug info.258/// \param PDVRUnrelatedWL The equivalent non-intrinsic debug records.259void260filterInstsUnrelatedToPDI(BasicBlock *GEntryBlock,261std::vector<Instruction *> &PDIUnrelatedWL,262std::vector<DbgVariableRecord *> &PDVRUnrelatedWL);263264/// Erase the rest of the CFG (i.e. barring the entry block).265void eraseTail(Function *G);266267/// Erase the instructions in PDIUnrelatedWL as they are unrelated to the268/// parameter debug info, from the entry block.269/// \param PDVRUnrelatedWL contains the equivalent set of non-instruction270/// debug-info records.271void272eraseInstsUnrelatedToPDI(std::vector<Instruction *> &PDIUnrelatedWL,273std::vector<DbgVariableRecord *> &PDVRUnrelatedWL);274275/// Replace G with a simple tail call to bitcast(F). Also (unless276/// MergeFunctionsPDI holds) replace direct uses of G with bitcast(F),277/// delete G.278void writeThunk(Function *F, Function *G);279280// Replace G with an alias to F (deleting function G)281void writeAlias(Function *F, Function *G);282283// Replace G with an alias to F if possible, or a thunk to F if possible.284// Returns false if neither is the case.285bool writeThunkOrAlias(Function *F, Function *G);286287/// Replace function F with function G in the function tree.288void replaceFunctionInTree(const FunctionNode &FN, Function *G);289290/// The set of all distinct functions. Use the insert() and remove() methods291/// to modify it. The map allows efficient lookup and deferring of Functions.292FnTreeType FnTree;293294// Map functions to the iterators of the FunctionNode which contains them295// in the FnTree. This must be updated carefully whenever the FnTree is296// modified, i.e. in insert(), remove(), and replaceFunctionInTree(), to avoid297// dangling iterators into FnTree. The invariant that preserves this is that298// there is exactly one mapping F -> FN for each FunctionNode FN in FnTree.299DenseMap<AssertingVH<Function>, FnTreeType::iterator> FNodesInTree;300};301} // end anonymous namespace302303PreservedAnalyses MergeFunctionsPass::run(Module &M,304ModuleAnalysisManager &AM) {305MergeFunctions MF;306if (!MF.runOnModule(M))307return PreservedAnalyses::all();308return PreservedAnalyses::none();309}310311#ifndef NDEBUG312bool MergeFunctions::doFunctionalCheck(std::vector<WeakTrackingVH> &Worklist) {313if (const unsigned Max = NumFunctionsForVerificationCheck) {314unsigned TripleNumber = 0;315bool Valid = true;316317dbgs() << "MERGEFUNC-VERIFY: Started for first " << Max << " functions.\n";318319unsigned i = 0;320for (std::vector<WeakTrackingVH>::iterator I = Worklist.begin(),321E = Worklist.end();322I != E && i < Max; ++I, ++i) {323unsigned j = i;324for (std::vector<WeakTrackingVH>::iterator J = I; J != E && j < Max;325++J, ++j) {326Function *F1 = cast<Function>(*I);327Function *F2 = cast<Function>(*J);328int Res1 = FunctionComparator(F1, F2, &GlobalNumbers).compare();329int Res2 = FunctionComparator(F2, F1, &GlobalNumbers).compare();330331// If F1 <= F2, then F2 >= F1, otherwise report failure.332if (Res1 != -Res2) {333dbgs() << "MERGEFUNC-VERIFY: Non-symmetric; triple: " << TripleNumber334<< "\n";335dbgs() << *F1 << '\n' << *F2 << '\n';336Valid = false;337}338339if (Res1 == 0)340continue;341342unsigned k = j;343for (std::vector<WeakTrackingVH>::iterator K = J; K != E && k < Max;344++k, ++K, ++TripleNumber) {345if (K == J)346continue;347348Function *F3 = cast<Function>(*K);349int Res3 = FunctionComparator(F1, F3, &GlobalNumbers).compare();350int Res4 = FunctionComparator(F2, F3, &GlobalNumbers).compare();351352bool Transitive = true;353354if (Res1 != 0 && Res1 == Res4) {355// F1 > F2, F2 > F3 => F1 > F3356Transitive = Res3 == Res1;357} else if (Res3 != 0 && Res3 == -Res4) {358// F1 > F3, F3 > F2 => F1 > F2359Transitive = Res3 == Res1;360} else if (Res4 != 0 && -Res3 == Res4) {361// F2 > F3, F3 > F1 => F2 > F1362Transitive = Res4 == -Res1;363}364365if (!Transitive) {366dbgs() << "MERGEFUNC-VERIFY: Non-transitive; triple: "367<< TripleNumber << "\n";368dbgs() << "Res1, Res3, Res4: " << Res1 << ", " << Res3 << ", "369<< Res4 << "\n";370dbgs() << *F1 << '\n' << *F2 << '\n' << *F3 << '\n';371Valid = false;372}373}374}375}376377dbgs() << "MERGEFUNC-VERIFY: " << (Valid ? "Passed." : "Failed.") << "\n";378return Valid;379}380return true;381}382#endif383384/// Check whether \p F has an intrinsic which references385/// distinct metadata as an operand. The most common386/// instance of this would be CFI checks for function-local types.387static bool hasDistinctMetadataIntrinsic(const Function &F) {388for (const BasicBlock &BB : F) {389for (const Instruction &I : BB.instructionsWithoutDebug()) {390if (!isa<IntrinsicInst>(&I))391continue;392393for (Value *Op : I.operands()) {394auto *MDL = dyn_cast<MetadataAsValue>(Op);395if (!MDL)396continue;397if (MDNode *N = dyn_cast<MDNode>(MDL->getMetadata()))398if (N->isDistinct())399return true;400}401}402}403return false;404}405406/// Check whether \p F is eligible for function merging.407static bool isEligibleForMerging(Function &F) {408return !F.isDeclaration() && !F.hasAvailableExternallyLinkage() &&409!hasDistinctMetadataIntrinsic(F);410}411412bool MergeFunctions::runOnModule(Module &M) {413bool Changed = false;414415SmallVector<GlobalValue *, 4> UsedV;416collectUsedGlobalVariables(M, UsedV, /*CompilerUsed=*/false);417collectUsedGlobalVariables(M, UsedV, /*CompilerUsed=*/true);418Used.insert(UsedV.begin(), UsedV.end());419420// All functions in the module, ordered by hash. Functions with a unique421// hash value are easily eliminated.422std::vector<std::pair<IRHash, Function *>> HashedFuncs;423for (Function &Func : M) {424if (isEligibleForMerging(Func)) {425HashedFuncs.push_back({StructuralHash(Func), &Func});426}427}428429llvm::stable_sort(HashedFuncs, less_first());430431auto S = HashedFuncs.begin();432for (auto I = HashedFuncs.begin(), IE = HashedFuncs.end(); I != IE; ++I) {433// If the hash value matches the previous value or the next one, we must434// consider merging it. Otherwise it is dropped and never considered again.435if ((I != S && std::prev(I)->first == I->first) ||436(std::next(I) != IE && std::next(I)->first == I->first) ) {437Deferred.push_back(WeakTrackingVH(I->second));438}439}440441do {442std::vector<WeakTrackingVH> Worklist;443Deferred.swap(Worklist);444445LLVM_DEBUG(doFunctionalCheck(Worklist));446447LLVM_DEBUG(dbgs() << "size of module: " << M.size() << '\n');448LLVM_DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n');449450// Insert functions and merge them.451for (WeakTrackingVH &I : Worklist) {452if (!I)453continue;454Function *F = cast<Function>(I);455if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) {456Changed |= insert(F);457}458}459LLVM_DEBUG(dbgs() << "size of FnTree: " << FnTree.size() << '\n');460} while (!Deferred.empty());461462FnTree.clear();463FNodesInTree.clear();464GlobalNumbers.clear();465Used.clear();466467return Changed;468}469470// Replace direct callers of Old with New.471void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) {472for (Use &U : llvm::make_early_inc_range(Old->uses())) {473CallBase *CB = dyn_cast<CallBase>(U.getUser());474if (CB && CB->isCallee(&U)) {475// Do not copy attributes from the called function to the call-site.476// Function comparison ensures that the attributes are the same up to477// type congruences in byval(), in which case we need to keep the byval478// type of the call-site, not the callee function.479remove(CB->getFunction());480U.set(New);481}482}483}484485// Helper for writeThunk,486// Selects proper bitcast operation,487// but a bit simpler then CastInst::getCastOpcode.488static Value *createCast(IRBuilder<> &Builder, Value *V, Type *DestTy) {489Type *SrcTy = V->getType();490if (SrcTy->isStructTy()) {491assert(DestTy->isStructTy());492assert(SrcTy->getStructNumElements() == DestTy->getStructNumElements());493Value *Result = PoisonValue::get(DestTy);494for (unsigned int I = 0, E = SrcTy->getStructNumElements(); I < E; ++I) {495Value *Element =496createCast(Builder, Builder.CreateExtractValue(V, ArrayRef(I)),497DestTy->getStructElementType(I));498499Result = Builder.CreateInsertValue(Result, Element, ArrayRef(I));500}501return Result;502}503assert(!DestTy->isStructTy());504if (SrcTy->isIntegerTy() && DestTy->isPointerTy())505return Builder.CreateIntToPtr(V, DestTy);506else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())507return Builder.CreatePtrToInt(V, DestTy);508else509return Builder.CreateBitCast(V, DestTy);510}511512// Erase the instructions in PDIUnrelatedWL as they are unrelated to the513// parameter debug info, from the entry block.514void MergeFunctions::eraseInstsUnrelatedToPDI(515std::vector<Instruction *> &PDIUnrelatedWL,516std::vector<DbgVariableRecord *> &PDVRUnrelatedWL) {517LLVM_DEBUG(518dbgs() << " Erasing instructions (in reverse order of appearance in "519"entry block) unrelated to parameter debug info from entry "520"block: {\n");521while (!PDIUnrelatedWL.empty()) {522Instruction *I = PDIUnrelatedWL.back();523LLVM_DEBUG(dbgs() << " Deleting Instruction: ");524LLVM_DEBUG(I->print(dbgs()));525LLVM_DEBUG(dbgs() << "\n");526I->eraseFromParent();527PDIUnrelatedWL.pop_back();528}529530while (!PDVRUnrelatedWL.empty()) {531DbgVariableRecord *DVR = PDVRUnrelatedWL.back();532LLVM_DEBUG(dbgs() << " Deleting DbgVariableRecord ");533LLVM_DEBUG(DVR->print(dbgs()));534LLVM_DEBUG(dbgs() << "\n");535DVR->eraseFromParent();536PDVRUnrelatedWL.pop_back();537}538539LLVM_DEBUG(dbgs() << " } // Done erasing instructions unrelated to parameter "540"debug info from entry block. \n");541}542543// Reduce G to its entry block.544void MergeFunctions::eraseTail(Function *G) {545std::vector<BasicBlock *> WorklistBB;546for (BasicBlock &BB : drop_begin(*G)) {547BB.dropAllReferences();548WorklistBB.push_back(&BB);549}550while (!WorklistBB.empty()) {551BasicBlock *BB = WorklistBB.back();552BB->eraseFromParent();553WorklistBB.pop_back();554}555}556557// We are interested in the following instructions from the entry block as being558// related to parameter debug info:559// - @llvm.dbg.declare560// - stores from the incoming parameters to locations on the stack-frame561// - allocas that create these locations on the stack-frame562// - @llvm.dbg.value563// - the entry block's terminator564// The rest are unrelated to debug info for the parameters; fill up565// PDIUnrelatedWL with such instructions.566void MergeFunctions::filterInstsUnrelatedToPDI(567BasicBlock *GEntryBlock, std::vector<Instruction *> &PDIUnrelatedWL,568std::vector<DbgVariableRecord *> &PDVRUnrelatedWL) {569std::set<Instruction *> PDIRelated;570std::set<DbgVariableRecord *> PDVRRelated;571572// Work out whether a dbg.value intrinsic or an equivalent DbgVariableRecord573// is a parameter to be preserved.574auto ExamineDbgValue = [](auto *DbgVal, auto &Container) {575LLVM_DEBUG(dbgs() << " Deciding: ");576LLVM_DEBUG(DbgVal->print(dbgs()));577LLVM_DEBUG(dbgs() << "\n");578DILocalVariable *DILocVar = DbgVal->getVariable();579if (DILocVar->isParameter()) {580LLVM_DEBUG(dbgs() << " Include (parameter): ");581LLVM_DEBUG(DbgVal->print(dbgs()));582LLVM_DEBUG(dbgs() << "\n");583Container.insert(DbgVal);584} else {585LLVM_DEBUG(dbgs() << " Delete (!parameter): ");586LLVM_DEBUG(DbgVal->print(dbgs()));587LLVM_DEBUG(dbgs() << "\n");588}589};590591auto ExamineDbgDeclare = [&PDIRelated](auto *DbgDecl, auto &Container) {592LLVM_DEBUG(dbgs() << " Deciding: ");593LLVM_DEBUG(DbgDecl->print(dbgs()));594LLVM_DEBUG(dbgs() << "\n");595DILocalVariable *DILocVar = DbgDecl->getVariable();596if (DILocVar->isParameter()) {597LLVM_DEBUG(dbgs() << " Parameter: ");598LLVM_DEBUG(DILocVar->print(dbgs()));599AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DbgDecl->getAddress());600if (AI) {601LLVM_DEBUG(dbgs() << " Processing alloca users: ");602LLVM_DEBUG(dbgs() << "\n");603for (User *U : AI->users()) {604if (StoreInst *SI = dyn_cast<StoreInst>(U)) {605if (Value *Arg = SI->getValueOperand()) {606if (isa<Argument>(Arg)) {607LLVM_DEBUG(dbgs() << " Include: ");608LLVM_DEBUG(AI->print(dbgs()));609LLVM_DEBUG(dbgs() << "\n");610PDIRelated.insert(AI);611LLVM_DEBUG(dbgs() << " Include (parameter): ");612LLVM_DEBUG(SI->print(dbgs()));613LLVM_DEBUG(dbgs() << "\n");614PDIRelated.insert(SI);615LLVM_DEBUG(dbgs() << " Include: ");616LLVM_DEBUG(DbgDecl->print(dbgs()));617LLVM_DEBUG(dbgs() << "\n");618Container.insert(DbgDecl);619} else {620LLVM_DEBUG(dbgs() << " Delete (!parameter): ");621LLVM_DEBUG(SI->print(dbgs()));622LLVM_DEBUG(dbgs() << "\n");623}624}625} else {626LLVM_DEBUG(dbgs() << " Defer: ");627LLVM_DEBUG(U->print(dbgs()));628LLVM_DEBUG(dbgs() << "\n");629}630}631} else {632LLVM_DEBUG(dbgs() << " Delete (alloca NULL): ");633LLVM_DEBUG(DbgDecl->print(dbgs()));634LLVM_DEBUG(dbgs() << "\n");635}636} else {637LLVM_DEBUG(dbgs() << " Delete (!parameter): ");638LLVM_DEBUG(DbgDecl->print(dbgs()));639LLVM_DEBUG(dbgs() << "\n");640}641};642643for (BasicBlock::iterator BI = GEntryBlock->begin(), BIE = GEntryBlock->end();644BI != BIE; ++BI) {645// Examine DbgVariableRecords as they happen "before" the instruction. Are646// they connected to parameters?647for (DbgVariableRecord &DVR : filterDbgVars(BI->getDbgRecordRange())) {648if (DVR.isDbgValue() || DVR.isDbgAssign()) {649ExamineDbgValue(&DVR, PDVRRelated);650} else {651assert(DVR.isDbgDeclare());652ExamineDbgDeclare(&DVR, PDVRRelated);653}654}655656if (auto *DVI = dyn_cast<DbgValueInst>(&*BI)) {657ExamineDbgValue(DVI, PDIRelated);658} else if (auto *DDI = dyn_cast<DbgDeclareInst>(&*BI)) {659ExamineDbgDeclare(DDI, PDIRelated);660} else if (BI->isTerminator() && &*BI == GEntryBlock->getTerminator()) {661LLVM_DEBUG(dbgs() << " Will Include Terminator: ");662LLVM_DEBUG(BI->print(dbgs()));663LLVM_DEBUG(dbgs() << "\n");664PDIRelated.insert(&*BI);665} else {666LLVM_DEBUG(dbgs() << " Defer: ");667LLVM_DEBUG(BI->print(dbgs()));668LLVM_DEBUG(dbgs() << "\n");669}670}671LLVM_DEBUG(672dbgs()673<< " Report parameter debug info related/related instructions: {\n");674675auto IsPDIRelated = [](auto *Rec, auto &Container, auto &UnrelatedCont) {676if (Container.find(Rec) == Container.end()) {677LLVM_DEBUG(dbgs() << " !PDIRelated: ");678LLVM_DEBUG(Rec->print(dbgs()));679LLVM_DEBUG(dbgs() << "\n");680UnrelatedCont.push_back(Rec);681} else {682LLVM_DEBUG(dbgs() << " PDIRelated: ");683LLVM_DEBUG(Rec->print(dbgs()));684LLVM_DEBUG(dbgs() << "\n");685}686};687688// Collect the set of unrelated instructions and debug records.689for (Instruction &I : *GEntryBlock) {690for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange()))691IsPDIRelated(&DVR, PDVRRelated, PDVRUnrelatedWL);692IsPDIRelated(&I, PDIRelated, PDIUnrelatedWL);693}694LLVM_DEBUG(dbgs() << " }\n");695}696697/// Whether this function may be replaced by a forwarding thunk.698static bool canCreateThunkFor(Function *F) {699if (F->isVarArg())700return false;701702// Don't merge tiny functions using a thunk, since it can just end up703// making the function larger.704if (F->size() == 1) {705if (F->front().sizeWithoutDebug() < 2) {706LLVM_DEBUG(dbgs() << "canCreateThunkFor: " << F->getName()707<< " is too small to bother creating a thunk for\n");708return false;709}710}711return true;712}713714/// Copy all metadata of a specific kind from one function to another.715static void copyMetadataIfPresent(Function *From, Function *To,716StringRef Kind) {717SmallVector<MDNode *, 4> MDs;718From->getMetadata(Kind, MDs);719for (MDNode *MD : MDs)720To->addMetadata(Kind, *MD);721}722723// Replace G with a simple tail call to bitcast(F). Also (unless724// MergeFunctionsPDI holds) replace direct uses of G with bitcast(F),725// delete G. Under MergeFunctionsPDI, we use G itself for creating726// the thunk as we preserve the debug info (and associated instructions)727// from G's entry block pertaining to G's incoming arguments which are728// passed on as corresponding arguments in the call that G makes to F.729// For better debugability, under MergeFunctionsPDI, we do not modify G's730// call sites to point to F even when within the same translation unit.731void MergeFunctions::writeThunk(Function *F, Function *G) {732BasicBlock *GEntryBlock = nullptr;733std::vector<Instruction *> PDIUnrelatedWL;734std::vector<DbgVariableRecord *> PDVRUnrelatedWL;735BasicBlock *BB = nullptr;736Function *NewG = nullptr;737if (MergeFunctionsPDI) {738LLVM_DEBUG(dbgs() << "writeThunk: (MergeFunctionsPDI) Do not create a new "739"function as thunk; retain original: "740<< G->getName() << "()\n");741GEntryBlock = &G->getEntryBlock();742LLVM_DEBUG(743dbgs() << "writeThunk: (MergeFunctionsPDI) filter parameter related "744"debug info for "745<< G->getName() << "() {\n");746filterInstsUnrelatedToPDI(GEntryBlock, PDIUnrelatedWL, PDVRUnrelatedWL);747GEntryBlock->getTerminator()->eraseFromParent();748BB = GEntryBlock;749} else {750NewG = Function::Create(G->getFunctionType(), G->getLinkage(),751G->getAddressSpace(), "", G->getParent());752NewG->setComdat(G->getComdat());753NewG->IsNewDbgInfoFormat = G->IsNewDbgInfoFormat;754BB = BasicBlock::Create(F->getContext(), "", NewG);755}756757IRBuilder<> Builder(BB);758Function *H = MergeFunctionsPDI ? G : NewG;759SmallVector<Value *, 16> Args;760unsigned i = 0;761FunctionType *FFTy = F->getFunctionType();762for (Argument &AI : H->args()) {763Args.push_back(createCast(Builder, &AI, FFTy->getParamType(i)));764++i;765}766767CallInst *CI = Builder.CreateCall(F, Args);768ReturnInst *RI = nullptr;769bool isSwiftTailCall = F->getCallingConv() == CallingConv::SwiftTail &&770G->getCallingConv() == CallingConv::SwiftTail;771CI->setTailCallKind(isSwiftTailCall ? llvm::CallInst::TCK_MustTail772: llvm::CallInst::TCK_Tail);773CI->setCallingConv(F->getCallingConv());774CI->setAttributes(F->getAttributes());775if (H->getReturnType()->isVoidTy()) {776RI = Builder.CreateRetVoid();777} else {778RI = Builder.CreateRet(createCast(Builder, CI, H->getReturnType()));779}780781if (MergeFunctionsPDI) {782DISubprogram *DIS = G->getSubprogram();783if (DIS) {784DebugLoc CIDbgLoc =785DILocation::get(DIS->getContext(), DIS->getScopeLine(), 0, DIS);786DebugLoc RIDbgLoc =787DILocation::get(DIS->getContext(), DIS->getScopeLine(), 0, DIS);788CI->setDebugLoc(CIDbgLoc);789RI->setDebugLoc(RIDbgLoc);790} else {791LLVM_DEBUG(792dbgs() << "writeThunk: (MergeFunctionsPDI) No DISubprogram for "793<< G->getName() << "()\n");794}795eraseTail(G);796eraseInstsUnrelatedToPDI(PDIUnrelatedWL, PDVRUnrelatedWL);797LLVM_DEBUG(798dbgs() << "} // End of parameter related debug info filtering for: "799<< G->getName() << "()\n");800} else {801NewG->copyAttributesFrom(G);802NewG->takeName(G);803// Ensure CFI type metadata is propagated to the new function.804copyMetadataIfPresent(G, NewG, "type");805copyMetadataIfPresent(G, NewG, "kcfi_type");806removeUsers(G);807G->replaceAllUsesWith(NewG);808G->eraseFromParent();809}810811LLVM_DEBUG(dbgs() << "writeThunk: " << H->getName() << '\n');812++NumThunksWritten;813}814815// Whether this function may be replaced by an alias816static bool canCreateAliasFor(Function *F) {817if (!MergeFunctionsAliases || !F->hasGlobalUnnamedAddr())818return false;819820// We should only see linkages supported by aliases here821assert(F->hasLocalLinkage() || F->hasExternalLinkage()822|| F->hasWeakLinkage() || F->hasLinkOnceLinkage());823return true;824}825826// Replace G with an alias to F (deleting function G)827void MergeFunctions::writeAlias(Function *F, Function *G) {828PointerType *PtrType = G->getType();829auto *GA = GlobalAlias::create(G->getValueType(), PtrType->getAddressSpace(),830G->getLinkage(), "", F, G->getParent());831832const MaybeAlign FAlign = F->getAlign();833const MaybeAlign GAlign = G->getAlign();834if (FAlign || GAlign)835F->setAlignment(std::max(FAlign.valueOrOne(), GAlign.valueOrOne()));836else837F->setAlignment(std::nullopt);838GA->takeName(G);839GA->setVisibility(G->getVisibility());840GA->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);841842removeUsers(G);843G->replaceAllUsesWith(GA);844G->eraseFromParent();845846LLVM_DEBUG(dbgs() << "writeAlias: " << GA->getName() << '\n');847++NumAliasesWritten;848}849850// Replace G with an alias to F if possible, or a thunk to F if851// profitable. Returns false if neither is the case.852bool MergeFunctions::writeThunkOrAlias(Function *F, Function *G) {853if (canCreateAliasFor(G)) {854writeAlias(F, G);855return true;856}857if (canCreateThunkFor(F)) {858writeThunk(F, G);859return true;860}861return false;862}863864// Merge two equivalent functions. Upon completion, Function G is deleted.865void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {866if (F->isInterposable()) {867assert(G->isInterposable());868869// Both writeThunkOrAlias() calls below must succeed, either because we can870// create aliases for G and NewF, or because a thunk for F is profitable.871// F here has the same signature as NewF below, so that's what we check.872if (!canCreateThunkFor(F) &&873(!canCreateAliasFor(F) || !canCreateAliasFor(G)))874return;875876// Make them both thunks to the same internal function.877Function *NewF = Function::Create(F->getFunctionType(), F->getLinkage(),878F->getAddressSpace(), "", F->getParent());879NewF->copyAttributesFrom(F);880NewF->takeName(F);881NewF->IsNewDbgInfoFormat = F->IsNewDbgInfoFormat;882// Ensure CFI type metadata is propagated to the new function.883copyMetadataIfPresent(F, NewF, "type");884copyMetadataIfPresent(F, NewF, "kcfi_type");885removeUsers(F);886F->replaceAllUsesWith(NewF);887888// We collect alignment before writeThunkOrAlias that overwrites NewF and889// G's content.890const MaybeAlign NewFAlign = NewF->getAlign();891const MaybeAlign GAlign = G->getAlign();892893writeThunkOrAlias(F, G);894writeThunkOrAlias(F, NewF);895896if (NewFAlign || GAlign)897F->setAlignment(std::max(NewFAlign.valueOrOne(), GAlign.valueOrOne()));898else899F->setAlignment(std::nullopt);900F->setLinkage(GlobalValue::PrivateLinkage);901++NumDoubleWeak;902++NumFunctionsMerged;903} else {904// For better debugability, under MergeFunctionsPDI, we do not modify G's905// call sites to point to F even when within the same translation unit.906if (!G->isInterposable() && !MergeFunctionsPDI) {907// Functions referred to by llvm.used/llvm.compiler.used are special:908// there are uses of the symbol name that are not visible to LLVM,909// usually from inline asm.910if (G->hasGlobalUnnamedAddr() && !Used.contains(G)) {911// G might have been a key in our GlobalNumberState, and it's illegal912// to replace a key in ValueMap<GlobalValue *> with a non-global.913GlobalNumbers.erase(G);914// If G's address is not significant, replace it entirely.915removeUsers(G);916G->replaceAllUsesWith(F);917} else {918// Redirect direct callers of G to F. (See note on MergeFunctionsPDI919// above).920replaceDirectCallers(G, F);921}922}923924// If G was internal then we may have replaced all uses of G with F. If so,925// stop here and delete G. There's no need for a thunk. (See note on926// MergeFunctionsPDI above).927if (G->isDiscardableIfUnused() && G->use_empty() && !MergeFunctionsPDI) {928G->eraseFromParent();929++NumFunctionsMerged;930return;931}932933if (writeThunkOrAlias(F, G)) {934++NumFunctionsMerged;935}936}937}938939/// Replace function F by function G.940void MergeFunctions::replaceFunctionInTree(const FunctionNode &FN,941Function *G) {942Function *F = FN.getFunc();943assert(FunctionComparator(F, G, &GlobalNumbers).compare() == 0 &&944"The two functions must be equal");945946auto I = FNodesInTree.find(F);947assert(I != FNodesInTree.end() && "F should be in FNodesInTree");948assert(FNodesInTree.count(G) == 0 && "FNodesInTree should not contain G");949950FnTreeType::iterator IterToFNInFnTree = I->second;951assert(&(*IterToFNInFnTree) == &FN && "F should map to FN in FNodesInTree.");952// Remove F -> FN and insert G -> FN953FNodesInTree.erase(I);954FNodesInTree.insert({G, IterToFNInFnTree});955// Replace F with G in FN, which is stored inside the FnTree.956FN.replaceBy(G);957}958959// Ordering for functions that are equal under FunctionComparator960static bool isFuncOrderCorrect(const Function *F, const Function *G) {961if (F->isInterposable() != G->isInterposable()) {962// Strong before weak, because the weak function may call the strong963// one, but not the other way around.964return !F->isInterposable();965}966if (F->hasLocalLinkage() != G->hasLocalLinkage()) {967// External before local, because we definitely have to keep the external968// function, but may be able to drop the local one.969return !F->hasLocalLinkage();970}971// Impose a total order (by name) on the replacement of functions. This is972// important when operating on more than one module independently to prevent973// cycles of thunks calling each other when the modules are linked together.974return F->getName() <= G->getName();975}976977// Insert a ComparableFunction into the FnTree, or merge it away if equal to one978// that was already inserted.979bool MergeFunctions::insert(Function *NewFunction) {980std::pair<FnTreeType::iterator, bool> Result =981FnTree.insert(FunctionNode(NewFunction));982983if (Result.second) {984assert(FNodesInTree.count(NewFunction) == 0);985FNodesInTree.insert({NewFunction, Result.first});986LLVM_DEBUG(dbgs() << "Inserting as unique: " << NewFunction->getName()987<< '\n');988return false;989}990991const FunctionNode &OldF = *Result.first;992993if (!isFuncOrderCorrect(OldF.getFunc(), NewFunction)) {994// Swap the two functions.995Function *F = OldF.getFunc();996replaceFunctionInTree(*Result.first, NewFunction);997NewFunction = F;998assert(OldF.getFunc() != F && "Must have swapped the functions.");999}10001001LLVM_DEBUG(dbgs() << " " << OldF.getFunc()->getName()1002<< " == " << NewFunction->getName() << '\n');10031004Function *DeleteF = NewFunction;1005mergeTwoFunctions(OldF.getFunc(), DeleteF);1006return true;1007}10081009// Remove a function from FnTree. If it was already in FnTree, add1010// it to Deferred so that we'll look at it in the next round.1011void MergeFunctions::remove(Function *F) {1012auto I = FNodesInTree.find(F);1013if (I != FNodesInTree.end()) {1014LLVM_DEBUG(dbgs() << "Deferred " << F->getName() << ".\n");1015FnTree.erase(I->second);1016// I->second has been invalidated, remove it from the FNodesInTree map to1017// preserve the invariant.1018FNodesInTree.erase(I);1019Deferred.emplace_back(F);1020}1021}10221023// For each instruction used by the value, remove() the function that contains1024// the instruction. This should happen right before a call to RAUW.1025void MergeFunctions::removeUsers(Value *V) {1026for (User *U : V->users())1027if (auto *I = dyn_cast<Instruction>(U))1028remove(I->getFunction());1029}103010311032