LLVM  13.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 (!CalledF || !TTI.isLoweredToCall(CalledF) ||
57  CalledF->hasDLLImportStorageClass())
58  return;
59  uint64_t &Count = Counts[std::make_pair(F, CalledF)];
60  Count = SaturatingAdd(Count, NewCount);
61  };
62  // Ignore error here. Indirect calls are ignored if this fails.
63  (void)(bool) Symtab.create(M);
64  for (auto &F : M) {
65  // Avoid extra cost of running passes for BFI when the function doesn't have
66  // entry count. Since LazyBlockFrequencyInfoPass only exists in LPM, check
67  // if using LazyBlockFrequencyInfoPass.
68  // TODO: Remove LazyBFI when LazyBlockFrequencyInfoPass is available in NPM.
69  if (F.isDeclaration() || (LazyBFI && !F.getEntryCount()))
70  continue;
71  auto &BFI = GetBFI(F);
72  if (BFI.getEntryFreq() == 0)
73  continue;
74  TargetTransformInfo &TTI = GetTTI(F);
75  for (auto &BB : F) {
76  Optional<uint64_t> BBCount = BFI.getBlockProfileCount(&BB);
77  if (!BBCount)
78  continue;
79  for (auto &I : BB) {
80  CallBase *CB = dyn_cast<CallBase>(&I);
81  if (!CB)
82  continue;
83  if (CB->isIndirectCall()) {
84  InstrProfValueData ValueData[8];
85  uint32_t ActualNumValueData;
86  uint64_t TotalC;
87  if (!getValueProfDataFromInst(*CB, IPVK_IndirectCallTarget, 8,
88  ValueData, ActualNumValueData, TotalC))
89  continue;
90  for (const auto &VD :
91  ArrayRef<InstrProfValueData>(ValueData, ActualNumValueData)) {
92  UpdateCounts(TTI, &F, Symtab.getFunction(VD.Value), VD.Count);
93  }
94  continue;
95  }
96  UpdateCounts(TTI, &F, CB->getCalledFunction(), *BBCount);
97  }
98  }
99  }
100 
101  return addModuleFlags(M, Counts);
102 }
103 
104 namespace {
105 struct CGProfileLegacyPass final : public ModulePass {
106  static char ID;
107  CGProfileLegacyPass() : ModulePass(ID) {
109  }
110 
111  void getAnalysisUsage(AnalysisUsage &AU) const override {
112  AU.setPreservesCFG();
115  }
116 
117  bool runOnModule(Module &M) override {
118  auto GetBFI = [this](Function &F) -> BlockFrequencyInfo & {
119  return this->getAnalysis<LazyBlockFrequencyInfoPass>(F).getBFI();
120  };
121  auto GetTTI = [this](Function &F) -> TargetTransformInfo & {
122  return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
123  };
124 
125  return runCGProfilePass(M, GetBFI, GetTTI, true);
126  }
127 };
128 
129 } // namespace
130 
131 char CGProfileLegacyPass::ID = 0;
132 
133 INITIALIZE_PASS(CGProfileLegacyPass, "cg-profile", "Call Graph Profile", false,
134  false)
135 
137  return new CGProfileLegacyPass();
138 }
139 
143  auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & {
144  return FAM.getResult<BlockFrequencyAnalysis>(F);
145  };
146  auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & {
147  return FAM.getResult<TargetIRAnalysis>(F);
148  };
149 
150  runCGProfilePass(M, GetBFI, GetTTI, false);
151 
152  return PreservedAnalyses::all();
153 }
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:2265
llvm
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:785
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:61
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:168
LazyBlockFrequencyInfo.h
MapVector.h
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:362
llvm::createCGProfileLegacyPass
ModulePass * createCGProfileLegacyPass()
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1198
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::LazyBlockFrequencyInfoPass
This is an alternative analysis pass to BlockFrequencyInfoWrapperPass.
Definition: LazyBlockFrequencyInfo.h:100
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: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:1396
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:575
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:885
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:176
llvm::CGProfilePass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: CGProfile.cpp:140
llvm::TargetTransformInfoWrapperPass
Wrapper pass for TargetTransformInfo.
Definition: TargetTransformInfo.h:2321
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:163
llvm::TargetTransformInfo::isLoweredToCall
bool isLoweredToCall(const Function *F) const
Test whether calls to a function lower to actual program function calls.
Definition: TargetTransformInfo.cpp:275
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:33
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:419
llvm::Type::getInt64Ty
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:205
llvm::InstrProfSymtab
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:414
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:133
llvm::CallBase::isIndirectCall
bool isIndirectCall() const
Return true if the callsite is an indirect call.
Definition: Instructions.cpp:285
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:1164
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:945
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:999
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
InitializePasses.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38