Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Instrumentation/PGOForceFunctionAttrs.cpp
35266 views
//===----------------------------------------------------------------------===//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//===----------------------------------------------------------------------===//78#include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h"9#include "llvm/Analysis/BlockFrequencyInfo.h"10#include "llvm/Analysis/ProfileSummaryInfo.h"11#include "llvm/IR/Module.h"12#include "llvm/IR/PassManager.h"13#include "llvm/Support/ErrorHandling.h"1415using namespace llvm;1617static bool shouldRunOnFunction(Function &F, ProfileSummaryInfo &PSI,18FunctionAnalysisManager &FAM) {19if (F.isDeclaration())20return false;21// Respect existing attributes.22if (F.hasOptNone() || F.hasOptSize() || F.hasMinSize())23return false;24if (F.hasFnAttribute(Attribute::Cold))25return true;26if (!PSI.hasProfileSummary())27return false;28BlockFrequencyInfo &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);29return PSI.isFunctionColdInCallGraph(&F, BFI);30}3132PreservedAnalyses PGOForceFunctionAttrsPass::run(Module &M,33ModuleAnalysisManager &AM) {34if (ColdType == PGOOptions::ColdFuncOpt::Default)35return PreservedAnalyses::all();36ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);37FunctionAnalysisManager &FAM =38AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();39bool MadeChange = false;40for (Function &F : M) {41if (!shouldRunOnFunction(F, PSI, FAM))42continue;43switch (ColdType) {44case PGOOptions::ColdFuncOpt::Default:45llvm_unreachable("bailed out for default above");46break;47case PGOOptions::ColdFuncOpt::OptSize:48F.addFnAttr(Attribute::OptimizeForSize);49break;50case PGOOptions::ColdFuncOpt::MinSize:51F.addFnAttr(Attribute::MinSize);52break;53case PGOOptions::ColdFuncOpt::OptNone:54// alwaysinline is incompatible with optnone.55if (F.hasFnAttribute(Attribute::AlwaysInline))56continue;57F.addFnAttr(Attribute::OptimizeNone);58F.addFnAttr(Attribute::NoInline);59break;60}61MadeChange = true;62}63return MadeChange ? PreservedAnalyses::none() : PreservedAnalyses::all();64}656667