LLVM  14.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/Instructions.h"
17 #include "llvm/IR/MDBuilder.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/InitializePasses.h"
22 
23 #include <array>
24 
25 using namespace llvm;
26 
27 static bool
29  MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) {
30  if (Counts.empty())
31  return false;
32 
33  LLVMContext &Context = M.getContext();
34  MDBuilder MDB(Context);
35  std::vector<Metadata *> Nodes;
36 
37  for (auto E : Counts) {
38  Metadata *Vals[] = {ValueAsMetadata::get(E.first.first),
39  ValueAsMetadata::get(E.first.second),
41  Type::getInt64Ty(Context), E.second))};
42  Nodes.push_back(MDNode::get(Context, Vals));
43  }
44 
45  M.addModuleFlag(Module::Append, "CG Profile", MDNode::get(Context, Nodes));
46  return true;
47 }
48 
49 static bool runCGProfilePass(
51  function_ref<TargetTransformInfo &(Function &)> GetTTI, bool LazyBFI) {
53  InstrProfSymtab Symtab;
54  auto UpdateCounts = [&](TargetTransformInfo &TTI, Function *F,
55  Function *CalledF, uint64_t NewCount) {
56  if (NewCount == 0)
57  return;
58  if (!CalledF || !TTI.isLoweredToCall(CalledF) ||
59  CalledF->hasDLLImportStorageClass())
60  return;
61  uint64_t &Count = Counts[std::make_pair(F, CalledF)];
62  Count = SaturatingAdd(Count, NewCount);
63  };
64  // Ignore error here. Indirect calls are ignored if this fails.
65  (void)(bool) Symtab.create(M);
66  for (auto &F : M) {
67  // Avoid extra cost of running passes for BFI when the function doesn't have
68  // entry count. Since LazyBlockFrequencyInfoPass only exists in LPM, check
69  // if using LazyBlockFrequencyInfoPass.
70  // TODO: Remove LazyBFI when LazyBlockFrequencyInfoPass is available in NPM.
71  if (F.isDeclaration() || (LazyBFI && !F.getEntryCount()))
72  continue;
73  auto &BFI = GetBFI(F);
74  if (BFI.getEntryFreq() == 0)
75  continue;
76  TargetTransformInfo &TTI = GetTTI(F);
77  for (auto &BB : F) {
78  Optional<uint64_t> BBCount = BFI.getBlockProfileCount(&BB);
79  if (!BBCount)
80  continue;
81  for (auto &I : BB) {
82  CallBase *CB = dyn_cast<CallBase>(&I);
83  if (!CB)
84  continue;
85  if (CB->isIndirectCall()) {
86  InstrProfValueData ValueData[8];
87  uint32_t ActualNumValueData;
88  uint64_t TotalC;
89  if (!getValueProfDataFromInst(*CB, IPVK_IndirectCallTarget, 8,
90  ValueData, ActualNumValueData, TotalC))
91  continue;
92  for (const auto &VD :
93  ArrayRef<InstrProfValueData>(ValueData, ActualNumValueData)) {
94  UpdateCounts(TTI, &F, Symtab.getFunction(VD.Value), VD.Count);
95  }
96  continue;
97  }
98  UpdateCounts(TTI, &F, CB->getCalledFunction(), *BBCount);
99  }
100  }
101  }
102 
103  return addModuleFlags(M, Counts);
104 }
105 
106 namespace {
107 struct CGProfileLegacyPass final : public ModulePass {
108  static char ID;
109  CGProfileLegacyPass() : ModulePass(ID) {
111  }
112 
113  void getAnalysisUsage(AnalysisUsage &AU) const override {
114  AU.setPreservesCFG();
117  }
118 
119  bool runOnModule(Module &M) override {
120  auto GetBFI = [this](Function &F) -> BlockFrequencyInfo & {
121  return this->getAnalysis<LazyBlockFrequencyInfoPass>(F).getBFI();
122  };
123  auto GetTTI = [this](Function &F) -> TargetTransformInfo & {
124  return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
125  };
126 
127  return runCGProfilePass(M, GetBFI, GetTTI, true);
128  }
129 };
130 
131 } // namespace
132 
133 char CGProfileLegacyPass::ID = 0;
134 
135 INITIALIZE_PASS(CGProfileLegacyPass, "cg-profile", "Call Graph Profile", false,
136  false)
137 
139  return new CGProfileLegacyPass();
140 }
141 
145  auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & {
147  };
148  auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & {
149  return FAM.getResult<TargetIRAnalysis>(F);
150  };
151 
152  runCGProfilePass(M, GetBFI, GetTTI, false);
153 
154  return PreservedAnalyses::all();
155 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
Instrumentation.h
llvm::TargetIRAnalysis
Analysis pass providing the TargetTransformInfo.
Definition: TargetTransformInfo.h:2372
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
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:803
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
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:783
addModuleFlags
static bool addModuleFlags(Module &M, MapVector< std::pair< Function *, Function * >, uint64_t > &Counts)
Definition: CGProfile.cpp:28
runCGProfilePass
static bool runCGProfilePass(Module &M, function_ref< BlockFrequencyInfo &(Function &)> GetBFI, function_ref< TargetTransformInfo &(Function &)> GetTTI, bool LazyBFI)
Definition: CGProfile.cpp:49
llvm::Function
Definition: Function.h:62
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:169
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:368
llvm::createCGProfileLegacyPass
ModulePass * createCGProfileLegacyPass()
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1233
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::LazyBlockFrequencyInfoPass
This is an alternative analysis pass to BlockFrequencyInfoWrapperPass.
Definition: LazyBlockFrequencyInfo.h:100
Context
ManagedStatic< detail::RecordContext > Context
Definition: Record.cpp:96
llvm::initializeCGProfileLegacyPassPass
void initializeCGProfileLegacyPassPass(PassRegistry &)
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:141
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.
Definition: InstrTypes.h:1398
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:579
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:925
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::BlockFrequencyAnalysis
Analysis pass which computes BlockFrequencyInfo.
Definition: BlockFrequencyInfo.h:112
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:168
llvm::CGProfilePass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: CGProfile.cpp:142
uint64_t
llvm::TargetTransformInfoWrapperPass
Wrapper pass for TargetTransformInfo.
Definition: TargetTransformInfo.h:2428
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:59
llvm::TTI
TargetTransformInfo TTI
Definition: TargetTransformInfo.h:164
llvm::TargetTransformInfo::isLoweredToCall
bool isLoweredToCall(const Function *F) const
Test whether calls to a function lower to actual program function calls.
Definition: TargetTransformInfo.cpp:281
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
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:253
uint32_t
BlockFrequencyInfo.h
llvm::AMDGPUISD::BFI
@ BFI
Definition: AMDGPUISelLowering.h:431
llvm::Type::getInt64Ty
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:242
llvm::InstrProfSymtab
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:417
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
INITIALIZE_PASS
INITIALIZE_PASS(CGProfileLegacyPass, "cg-profile", "Call Graph Profile", false, false) ModulePass *llvm
Definition: CGProfile.cpp:135
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
Instructions.h
TargetTransformInfo.h
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1176
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::InnerAnalysisManagerProxy
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:940
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:1033
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
InitializePasses.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38