LLVM 17.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"
21#include <optional>
22
23using namespace llvm;
24
25static bool
27 MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) {
28 if (Counts.empty())
29 return false;
30
31 LLVMContext &Context = M.getContext();
32 MDBuilder MDB(Context);
33 std::vector<Metadata *> Nodes;
34
35 for (auto E : Counts) {
36 Metadata *Vals[] = {ValueAsMetadata::get(E.first.first),
37 ValueAsMetadata::get(E.first.second),
39 Type::getInt64Ty(Context), E.second))};
40 Nodes.push_back(MDNode::get(Context, Vals));
41 }
42
43 M.addModuleFlag(Module::Append, "CG Profile",
45 return true;
46}
47
48static bool runCGProfilePass(
51 InstrProfSymtab Symtab;
52 auto UpdateCounts = [&](TargetTransformInfo &TTI, Function *F,
53 Function *CalledF, uint64_t NewCount) {
54 if (NewCount == 0)
55 return;
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.
67 if (F.isDeclaration() || !F.getEntryCount())
68 continue;
70 if (BFI.getEntryFreq() == 0)
71 continue;
73 for (auto &BB : F) {
74 std::optional<uint64_t> BBCount = BFI.getBlockProfileCount(&BB);
75 if (!BBCount)
76 continue;
77 for (auto &I : BB) {
78 CallBase *CB = dyn_cast<CallBase>(&I);
79 if (!CB)
80 continue;
81 if (CB->isIndirectCall()) {
82 InstrProfValueData ValueData[8];
83 uint32_t ActualNumValueData;
84 uint64_t TotalC;
85 if (!getValueProfDataFromInst(*CB, IPVK_IndirectCallTarget, 8,
86 ValueData, ActualNumValueData, TotalC))
87 continue;
88 for (const auto &VD :
89 ArrayRef<InstrProfValueData>(ValueData, ActualNumValueData)) {
90 UpdateCounts(TTI, &F, Symtab.getFunction(VD.Value), VD.Count);
91 }
92 continue;
93 }
94 UpdateCounts(TTI, &F, CB->getCalledFunction(), *BBCount);
95 }
96 }
97 }
98
99 return addModuleFlags(M, Counts);
100}
101
106
107 return PreservedAnalyses::all();
108}
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static bool addModuleFlags(Module &M, MapVector< std::pair< Function *, Function * >, uint64_t > &Counts)
Definition: CGProfile.cpp:26
static bool runCGProfilePass(Module &M, FunctionAnalysisManager &FAM)
Definition: CGProfile.cpp:48
This file provides the interface for LLVM's Call Graph Profile pass.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file implements a map that provides insertion order iteration.
LLVMContext & Context
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
This header defines various interfaces for pass management in LLVM.
This pass exposes codegen information to IR-level passes.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:774
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Analysis pass which computes BlockFrequencyInfo.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: CGProfile.cpp:102
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1186
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1408
bool isIndirectCall() const
Return true if the callsite is an indirect call.
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:888
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:933
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:446
Function * getFunction(uint64_t FuncMD5Hash)
Return function from the name's md5 hash. Return nullptr if not found.
Definition: InstrProf.h:614
Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:24
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1399
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a distinct node.
Definition: Metadata.h:1367
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:37
Root of the metadata hierarchy.
Definition: Metadata.h:61
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
@ Append
Appends the two values, which are required to be metadata nodes.
Definition: Module.h:139
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
Analysis pass providing the TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
bool isLoweredToCall(const Function *F) const
Test whether calls to a function lower to actual program function calls.
static IntegerType * getInt64Ty(LLVMContext &C)
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:392
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1063
TargetTransformInfo TTI
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:583