LLVM  9.0.0svn
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"
14 #include "llvm/IR/CallSite.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"
21 
22 #include <array>
23 
24 using namespace llvm;
25 
29  MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
30  InstrProfSymtab Symtab;
31  auto UpdateCounts = [&](TargetTransformInfo &TTI, Function *F,
32  Function *CalledF, uint64_t NewCount) {
33  if (!CalledF || !TTI.isLoweredToCall(CalledF))
34  return;
35  uint64_t &Count = Counts[std::make_pair(F, CalledF)];
36  Count = SaturatingAdd(Count, NewCount);
37  };
38  // Ignore error here. Indirect calls are ignored if this fails.
39  (void)(bool)Symtab.create(M);
40  for (auto &F : M) {
41  if (F.isDeclaration())
42  continue;
43  auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);
44  if (BFI.getEntryFreq() == 0)
45  continue;
47  for (auto &BB : F) {
48  Optional<uint64_t> BBCount = BFI.getBlockProfileCount(&BB);
49  if (!BBCount)
50  continue;
51  for (auto &I : BB) {
52  CallSite CS(&I);
53  if (!CS)
54  continue;
55  if (CS.isIndirectCall()) {
56  InstrProfValueData ValueData[8];
57  uint32_t ActualNumValueData;
58  uint64_t TotalC;
60  IPVK_IndirectCallTarget, 8, ValueData,
61  ActualNumValueData, TotalC))
62  continue;
63  for (const auto &VD :
64  ArrayRef<InstrProfValueData>(ValueData, ActualNumValueData)) {
65  UpdateCounts(TTI, &F, Symtab.getFunction(VD.Value), VD.Count);
66  }
67  continue;
68  }
69  UpdateCounts(TTI, &F, CS.getCalledFunction(), *BBCount);
70  }
71  }
72  }
73 
74  addModuleFlags(M, Counts);
75 
76  return PreservedAnalyses::all();
77 }
78 
79 void CGProfilePass::addModuleFlags(
80  Module &M,
81  MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) const {
82  if (Counts.empty())
83  return;
84 
86  MDBuilder MDB(Context);
87  std::vector<Metadata *> Nodes;
88 
89  for (auto E : Counts) {
90  Metadata *Vals[] = {ValueAsMetadata::get(E.first.first),
91  ValueAsMetadata::get(E.first.second),
93  Type::getInt64Ty(Context), E.second))};
94  Nodes.push_back(MDNode::get(Context, Vals));
95  }
96 
97  M.addModuleFlag(Module::Append, "CG Profile", MDNode::get(Context, Nodes));
98 }
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:410
LLVMContext & Context
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:769
This class represents lattice values for constants.
Definition: AllocatorList.h:23
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
Function * getFunction(uint64_t FuncMD5Hash)
Return function from the name&#39;s md5 hash. Return nullptr if not found.
Definition: InstrProf.h:572
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:37
F(f)
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:111
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:176
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:775
Appends the two values, which are required to be metadata nodes.
Definition: Module.h:136
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:243
InstrTy * getInstruction() const
Definition: CallSite.h:96
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1165
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:857
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
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:337
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: CGProfile.cpp:26
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:348
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:621
bool isIndirectCall() const
Return true if the callsite is an indirect call.
Definition: CallSite.h:116
#define I(x, y, z)
Definition: MD5.cpp:58
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:57
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:1037