LLVM  8.0.1
CGProfile.cpp
Go to the documentation of this file.
1 //===-- CGProfile.cpp -----------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 
12 #include "llvm/ADT/MapVector.h"
15 #include "llvm/IR/CallSite.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/MDBuilder.h"
19 #include "llvm/IR/PassManager.h"
22 
23 #include <array>
24 
25 using namespace llvm;
26 
30  MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
31  InstrProfSymtab Symtab;
32  auto UpdateCounts = [&](TargetTransformInfo &TTI, Function *F,
33  Function *CalledF, uint64_t NewCount) {
34  if (!CalledF || !TTI.isLoweredToCall(CalledF))
35  return;
36  uint64_t &Count = Counts[std::make_pair(F, CalledF)];
37  Count = SaturatingAdd(Count, NewCount);
38  };
39  // Ignore error here. Indirect calls are ignored if this fails.
40  (void)(bool)Symtab.create(M);
41  for (auto &F : M) {
42  if (F.isDeclaration())
43  continue;
44  auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);
45  if (BFI.getEntryFreq() == 0)
46  continue;
48  for (auto &BB : F) {
49  Optional<uint64_t> BBCount = BFI.getBlockProfileCount(&BB);
50  if (!BBCount)
51  continue;
52  for (auto &I : BB) {
53  CallSite CS(&I);
54  if (!CS)
55  continue;
56  if (CS.isIndirectCall()) {
57  InstrProfValueData ValueData[8];
58  uint32_t ActualNumValueData;
59  uint64_t TotalC;
61  IPVK_IndirectCallTarget, 8, ValueData,
62  ActualNumValueData, TotalC))
63  continue;
64  for (const auto &VD :
65  ArrayRef<InstrProfValueData>(ValueData, ActualNumValueData)) {
66  UpdateCounts(TTI, &F, Symtab.getFunction(VD.Value), VD.Count);
67  }
68  continue;
69  }
70  UpdateCounts(TTI, &F, CS.getCalledFunction(), *BBCount);
71  }
72  }
73  }
74 
75  addModuleFlags(M, Counts);
76 
77  return PreservedAnalyses::all();
78 }
79 
80 void CGProfilePass::addModuleFlags(
81  Module &M,
82  MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) const {
83  if (Counts.empty())
84  return;
85 
87  MDBuilder MDB(Context);
88  std::vector<Metadata *> Nodes;
89 
90  for (auto E : Counts) {
91  Metadata *Vals[] = {ValueAsMetadata::get(E.first.first),
92  ValueAsMetadata::get(E.first.second),
94  Type::getInt64Ty(Context), E.second))};
95  Nodes.push_back(MDNode::get(Context, Vals));
96  }
97 
98  M.addModuleFlag(Module::Append, "CG Profile", MDNode::get(Context, Nodes));
99 }
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:411
LLVMContext & Context
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:770
This class represents lattice values for constants.
Definition: AllocatorList.h:24
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:25
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
Function * getFunction(uint64_t FuncMD5Hash)
Return function from the name&#39;s md5 hash. Return nullptr if not found.
Definition: InstrProf.h:573
Analysis pass providing the TargetTransformInfo.
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:38
F(f)
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:177
bool isIndirectCall() const
Return true if the callsite is an indirect call.
Definition: CallSite.h:112
std::enable_if< std::is_unsigned< T >::value, T >::type SaturatingAdd(T X, T Y, bool *ResultOverflowed=nullptr)
Add two unsigned integers, X and Y, of type T.
Definition: MathExtras.h:776
Appends the two values, which are required to be metadata nodes.
Definition: Module.h:137
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:244
InstrTy * getInstruction() const
Definition: CallSite.h:92
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1166
bool getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind, uint32_t MaxNumValueData, InstrProfValueData ValueData[], uint32_t &ActualNumValueData, uint64_t &TotalC)
Extract the value profile data from Inst which is annotated with value profile meta data...
Definition: InstrProf.cpp:858
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Metadata *Val)
Add a module-level flag to the module-level flags metadata.
Definition: Module.cpp:339
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: CGProfile.cpp:27
Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
Analysis pass which computes BlockFrequencyInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:349
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:622
#define I(x, y, z)
Definition: MD5.cpp:58
FunTy * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it&#39;s an indirect...
Definition: CallSite.h:107
This file provides the interface for LLVM&#39;s Call Graph Profile pass.
bool isLoweredToCall(const Function *F) const
Test whether calls to a function lower to actual program function calls.
A container for analyses that lazily runs them and caches their results.
This pass exposes codegen information to IR-level passes.
This header defines various interfaces for pass management in LLVM.
Root of the metadata hierarchy.
Definition: Metadata.h:58
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:1038