Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
35269 views
//===- AlwaysInliner.cpp - Code to inline always_inline 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 file implements a custom inliner that handles only functions that9// are marked as "always inline".10//11//===----------------------------------------------------------------------===//1213#include "llvm/Transforms/IPO/AlwaysInliner.h"14#include "llvm/ADT/SetVector.h"15#include "llvm/Analysis/AliasAnalysis.h"16#include "llvm/Analysis/AssumptionCache.h"17#include "llvm/Analysis/InlineAdvisor.h"18#include "llvm/Analysis/InlineCost.h"19#include "llvm/Analysis/OptimizationRemarkEmitter.h"20#include "llvm/Analysis/ProfileSummaryInfo.h"21#include "llvm/IR/Module.h"22#include "llvm/InitializePasses.h"23#include "llvm/Transforms/Utils/Cloning.h"24#include "llvm/Transforms/Utils/ModuleUtils.h"2526using namespace llvm;2728#define DEBUG_TYPE "inline"2930namespace {3132bool AlwaysInlineImpl(33Module &M, bool InsertLifetime, ProfileSummaryInfo &PSI,34function_ref<AssumptionCache &(Function &)> GetAssumptionCache,35function_ref<AAResults &(Function &)> GetAAR,36function_ref<BlockFrequencyInfo &(Function &)> GetBFI) {37SmallSetVector<CallBase *, 16> Calls;38bool Changed = false;39SmallVector<Function *, 16> InlinedComdatFunctions;4041for (Function &F : make_early_inc_range(M)) {42if (F.isPresplitCoroutine())43continue;4445if (F.isDeclaration() || !isInlineViable(F).isSuccess())46continue;4748Calls.clear();4950for (User *U : F.users())51if (auto *CB = dyn_cast<CallBase>(U))52if (CB->getCalledFunction() == &F &&53CB->hasFnAttr(Attribute::AlwaysInline) &&54!CB->getAttributes().hasFnAttr(Attribute::NoInline))55Calls.insert(CB);5657for (CallBase *CB : Calls) {58Function *Caller = CB->getCaller();59OptimizationRemarkEmitter ORE(Caller);60DebugLoc DLoc = CB->getDebugLoc();61BasicBlock *Block = CB->getParent();6263InlineFunctionInfo IFI(GetAssumptionCache, &PSI,64GetBFI ? &GetBFI(*Caller) : nullptr,65GetBFI ? &GetBFI(F) : nullptr);6667InlineResult Res = InlineFunction(*CB, IFI, /*MergeAttributes=*/true,68&GetAAR(F), InsertLifetime);69if (!Res.isSuccess()) {70ORE.emit([&]() {71return OptimizationRemarkMissed(DEBUG_TYPE, "NotInlined", DLoc, Block)72<< "'" << ore::NV("Callee", &F) << "' is not inlined into '"73<< ore::NV("Caller", Caller)74<< "': " << ore::NV("Reason", Res.getFailureReason());75});76continue;77}7879emitInlinedIntoBasedOnCost(80ORE, DLoc, Block, F, *Caller,81InlineCost::getAlways("always inline attribute"),82/*ForProfileContext=*/false, DEBUG_TYPE);8384Changed = true;85}8687F.removeDeadConstantUsers();88if (F.hasFnAttribute(Attribute::AlwaysInline) && F.isDefTriviallyDead()) {89// Remember to try and delete this function afterward. This allows to call90// filterDeadComdatFunctions() only once.91if (F.hasComdat()) {92InlinedComdatFunctions.push_back(&F);93} else {94M.getFunctionList().erase(F);95Changed = true;96}97}98}99100if (!InlinedComdatFunctions.empty()) {101// Now we just have the comdat functions. Filter out the ones whose comdats102// are not actually dead.103filterDeadComdatFunctions(InlinedComdatFunctions);104// The remaining functions are actually dead.105for (Function *F : InlinedComdatFunctions) {106M.getFunctionList().erase(F);107Changed = true;108}109}110111return Changed;112}113114struct AlwaysInlinerLegacyPass : public ModulePass {115bool InsertLifetime;116117AlwaysInlinerLegacyPass()118: AlwaysInlinerLegacyPass(/*InsertLifetime*/ true) {}119120AlwaysInlinerLegacyPass(bool InsertLifetime)121: ModulePass(ID), InsertLifetime(InsertLifetime) {122initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry());123}124125/// Main run interface method. We override here to avoid calling skipSCC().126bool runOnModule(Module &M) override {127128auto &PSI = getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();129auto GetAAR = [&](Function &F) -> AAResults & {130return getAnalysis<AAResultsWrapperPass>(F).getAAResults();131};132auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {133return getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);134};135136return AlwaysInlineImpl(M, InsertLifetime, PSI, GetAssumptionCache, GetAAR,137/*GetBFI*/ nullptr);138}139140static char ID; // Pass identification, replacement for typeid141142void getAnalysisUsage(AnalysisUsage &AU) const override {143AU.addRequired<AssumptionCacheTracker>();144AU.addRequired<AAResultsWrapperPass>();145AU.addRequired<ProfileSummaryInfoWrapperPass>();146}147};148149} // namespace150151char AlwaysInlinerLegacyPass::ID = 0;152INITIALIZE_PASS_BEGIN(AlwaysInlinerLegacyPass, "always-inline",153"Inliner for always_inline functions", false, false)154INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)155INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)156INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)157INITIALIZE_PASS_END(AlwaysInlinerLegacyPass, "always-inline",158"Inliner for always_inline functions", false, false)159160Pass *llvm::createAlwaysInlinerLegacyPass(bool InsertLifetime) {161return new AlwaysInlinerLegacyPass(InsertLifetime);162}163164PreservedAnalyses AlwaysInlinerPass::run(Module &M,165ModuleAnalysisManager &MAM) {166FunctionAnalysisManager &FAM =167MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();168auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {169return FAM.getResult<AssumptionAnalysis>(F);170};171auto GetBFI = [&](Function &F) -> BlockFrequencyInfo & {172return FAM.getResult<BlockFrequencyAnalysis>(F);173};174auto GetAAR = [&](Function &F) -> AAResults & {175return FAM.getResult<AAManager>(F);176};177auto &PSI = MAM.getResult<ProfileSummaryAnalysis>(M);178179bool Changed = AlwaysInlineImpl(M, InsertLifetime, PSI, GetAssumptionCache,180GetAAR, GetBFI);181182return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();183}184185186