LLVM  15.0.0git
CGProfile.cpp
Go to the documentation of this file.
1 //===-- CGProfile.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
11 #include "llvm/ADT/MapVector.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/MDBuilder.h"
17 #include "llvm/IR/PassManager.h"
18 #include "llvm/InitializePasses.h"
21 
22 using namespace llvm;
23 
24 static bool
26  MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) {
27  if (Counts.empty())
28  return false;
29 
30  LLVMContext &Context = M.getContext();
31  MDBuilder MDB(Context);
32  std::vector<Metadata *> Nodes;
33 
34  for (auto E : Counts) {
35  Metadata *Vals[] = {ValueAsMetadata::get(E.first.first),
36  ValueAsMetadata::get(E.first.second),
38  Type::getInt64Ty(Context), E.second))};
39  Nodes.push_back(MDNode::get(Context, Vals));
40  }
41 
42  M.addModuleFlag(Module::Append, "CG Profile", MDNode::get(Context, Nodes));
43  return true;
44 }
45 
46 static bool runCGProfilePass(
48  function_ref<TargetTransformInfo &(Function &)> GetTTI, bool LazyBFI) {
50  InstrProfSymtab Symtab;
51  auto UpdateCounts = [&](TargetTransformInfo &TTI, Function *F,
52  Function *CalledF, uint64_t NewCount) {
53  if (NewCount == 0)
54  return;
55  if (!CalledF || !TTI.isLoweredToCall(CalledF) ||
56  CalledF->hasDLLImportStorageClass())
57  return;
58  uint64_t &Count = Counts[std::make_pair(F, CalledF)];
59  Count = SaturatingAdd(Count, NewCount);
60  };
61  // Ignore error here. Indirect calls are ignored if this fails.
62  (void)(bool) Symtab.create(M);
63  for (auto &F : M) {
64  // Avoid extra cost of running passes for BFI when the function doesn't have
65  // entry count. Since LazyBlockFrequencyInfoPass only exists in LPM, check
66  // if using LazyBlockFrequencyInfoPass.
67  // TODO: Remove LazyBFI when LazyBlockFrequencyInfoPass is available in NPM.
68  if (F.isDeclaration() || (LazyBFI && !F.getEntryCount()))
69  continue;
70  auto &BFI = GetBFI(F);
71  if (BFI.getEntryFreq() == 0)
72  continue;
73  TargetTransformInfo &TTI = GetTTI(F);
74  for (auto &BB : F) {
75  Optional<uint64_t> BBCount = BFI.getBlockProfileCount(&BB);
76  if (!BBCount)
77  continue;
78  for (auto &I : BB) {
79  CallBase *CB = dyn_cast<CallBase>(&I);
80  if (!CB)
81  continue;
82  if (CB->isIndirectCall()) {
83  InstrProfValueData ValueData[8];
84  uint32_t ActualNumValueData;
85  uint64_t TotalC;
86  if (!getValueProfDataFromInst(*CB, IPVK_IndirectCallTarget, 8,
87  ValueData, ActualNumValueData, TotalC))
88  continue;
89  for (const auto &VD :
90  ArrayRef<InstrProfValueData>(ValueData, ActualNumValueData)) {
91  UpdateCounts(TTI, &F, Symtab.getFunction(VD.Value), VD.Count);
92  }
93  continue;
94  }
95  UpdateCounts(TTI, &F, CB->getCalledFunction(), *BBCount);
96  }
97  }
98  }
99 
100  return addModuleFlags(M, Counts);
101 }
102 
103 namespace {
104 struct CGProfileLegacyPass final : public ModulePass {
105  static char ID;
106  CGProfileLegacyPass() : ModulePass(ID) {
108  }
109 
110  void getAnalysisUsage(AnalysisUsage &AU) const override {
111  AU.setPreservesCFG();
114  }
115 
116  bool runOnModule(Module &M) override {
117  auto GetBFI = [this](Function &F) -> BlockFrequencyInfo & {
118  return this->getAnalysis<LazyBlockFrequencyInfoPass>(F).getBFI();
119  };
120  auto GetTTI = [this](Function &F) -> TargetTransformInfo & {
121  return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
122  };
123 
124  return runCGProfilePass(M, GetBFI, GetTTI, true);
125  }
126 };
127 
128 } // namespace
129 
130 char CGProfileLegacyPass::ID = 0;
131 
132 INITIALIZE_PASS(CGProfileLegacyPass, "cg-profile", "Call Graph Profile", false,
133  false)
134 
136  return new CGProfileLegacyPass();
137 }
138 
142  auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & {
144  };
145  auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & {
146  return FAM.getResult<TargetIRAnalysis>(F);
147  };
148 
149  runCGProfilePass(M, GetBFI, GetTTI, false);
150 
151  return PreservedAnalyses::all();
152 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
Instrumentation.h
llvm::TargetIRAnalysis
Analysis pass providing the TargetTransformInfo.
Definition: TargetTransformInfo.h:2458
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::MDBuilder::createConstant
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:24
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::SaturatingAdd
std::enable_if_t< std::is_unsigned< T >::value, T > SaturatingAdd(T X, T Y, bool *ResultOverflowed=nullptr)
Add two unsigned integers, X and Y, of type T.
Definition: MathExtras.h:830
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:780
addModuleFlags
static bool addModuleFlags(Module &M, MapVector< std::pair< Function *, Function * >, uint64_t > &Counts)
Definition: CGProfile.cpp:25
runCGProfilePass
static bool runCGProfilePass(Module &M, function_ref< BlockFrequencyInfo &(Function &)> GetBFI, function_ref< TargetTransformInfo &(Function &)> GetTTI, bool LazyBFI)
Definition: CGProfile.cpp:46
llvm::Function
Definition: Function.h:60
llvm::InstrProfSymtab::create
Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
llvm::TargetTransformInfo
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Definition: TargetTransformInfo.h:167
LazyBlockFrequencyInfo.h
MapVector.h
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
CGProfile.h
llvm::Optional< uint64_t >
llvm::MapVector
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:37
llvm::ValueAsMetadata::get
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:394
llvm::createCGProfileLegacyPass
ModulePass * createCGProfileLegacyPass()
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1289
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::LazyBlockFrequencyInfoPass
This is an alternative analysis pass to BlockFrequencyInfoWrapperPass.
Definition: LazyBlockFrequencyInfo.h:98
llvm::initializeCGProfileLegacyPassPass
void initializeCGProfileLegacyPassPass(PassRegistry &)
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::BlockFrequencyInfo
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Definition: BlockFrequencyInfo.h:37
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
Constants.h
llvm::Module::Append
@ Append
Appends the two values, which are required to be metadata nodes.
Definition: Module.h:139
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::CallBase::getCalledFunction
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1396
MAM
ModuleAnalysisManager MAM
Definition: PassBuilderBindings.cpp:61
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::InstrProfSymtab::getFunction
Function * getFunction(uint64_t FuncMD5Hash)
Return function from the name's md5 hash. Return nullptr if not found.
Definition: InstrProf.h:609
InstrProf.h
MDBuilder.h
llvm::ConstantInt::get
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:919
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::BlockFrequencyAnalysis
Analysis pass which computes BlockFrequencyInfo.
Definition: BlockFrequencyInfo.h:112
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::CGProfilePass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: CGProfile.cpp:139
uint64_t
llvm::TargetTransformInfoWrapperPass
Wrapper pass for TargetTransformInfo.
Definition: TargetTransformInfo.h:2514
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::TTI
TargetTransformInfo TTI
Definition: TargetTransformInfo.h:162
llvm::TargetTransformInfo::isLoweredToCall
bool isLoweredToCall(const Function *F) const
Test whether calls to a function lower to actual program function calls.
Definition: TargetTransformInfo.cpp:279
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
uint32_t
BlockFrequencyInfo.h
llvm::AMDGPUISD::BFI
@ BFI
Definition: AMDGPUISelLowering.h:429
llvm::Type::getInt64Ty
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:240
llvm::InstrProfSymtab
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:441
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
INITIALIZE_PASS
INITIALIZE_PASS(CGProfileLegacyPass, "cg-profile", "Call Graph Profile", false, false) ModulePass *llvm
Definition: CGProfile.cpp:132
llvm::CallBase::isIndirectCall
bool isIndirectCall() const
Return true if the callsite is an indirect call.
Definition: Instructions.cpp:289
PassManager.h
llvm::MDBuilder
Definition: MDBuilder.h:35
TargetTransformInfo.h
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1174
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::InnerAnalysisManagerProxy
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:937
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::getValueProfDataFromInst
bool getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind, uint32_t MaxNumValueData, InstrProfValueData ValueData[], uint32_t &ActualNumValueData, uint64_t &TotalC, bool GetNoICPValue=false)
Extract the value profile data from Inst which is annotated with value profile meta data.
Definition: InstrProf.cpp:1039
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
InitializePasses.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37