LLVM 24.0.0git
ValueProfilePlugins.inc
Go to the documentation of this file.
1//=== ValueProfilePlugins.inc - set of plugins used by ValueProfileCollector =//
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//
9// This file contains a set of plugin classes used in ValueProfileCollectorImpl.
10// Each plugin is responsible for collecting Value Profiling candidates for a
11// particular optimization.
12// Each plugin must satisfy the interface described in ValueProfileCollector.cpp
13//
14//===----------------------------------------------------------------------===//
15
20#include "llvm/IR/InstVisitor.h"
21
22using namespace llvm;
23using CandidateInfo = ValueProfileCollector::CandidateInfo;
24
25///--------------------------- MemIntrinsicPlugin ------------------------------
26class MemIntrinsicPlugin : public InstVisitor<MemIntrinsicPlugin> {
27 Function &F;
28 TargetLibraryInfo &TLI;
29 std::vector<CandidateInfo> *Candidates;
30
31public:
32 static constexpr InstrProfValueKind Kind = IPVK_MemOPSize;
33
34 MemIntrinsicPlugin(Function &Fn, TargetLibraryInfo &TLI)
35 : F(Fn), TLI(TLI), Candidates(nullptr) {}
36
37 void run(std::vector<CandidateInfo> &Cs) {
38 Candidates = &Cs;
39 visit(F);
40 Candidates = nullptr;
41 }
42 void visitMemIntrinsic(MemIntrinsic &MI) {
43 Value *Length = MI.getLength();
44 // Not instrument constant length calls.
45 if (isa<ConstantInt>(Length))
46 return;
47
48 Instruction *InsertPt = &MI;
49 Instruction *AnnotatedInst = &MI;
50 Candidates->emplace_back(CandidateInfo{Length, InsertPt, AnnotatedInst});
51 }
52 void visitCallInst(CallInst &CI) {
53 if (!InstrumentationOptions::Global.pgo_memop_optimize_memcmp_bcmp)
54 return;
55 auto *F = CI.getCalledFunction();
56 if (!F)
57 return;
58 LibFunc Func = TLI.getLibFunc(CI);
59 if (Func == LibFunc_memcmp || Func == LibFunc_bcmp) {
60 Value *Length = CI.getArgOperand(2);
61 // Not instrument constant length calls.
62 if (isa<ConstantInt>(Length))
63 return;
64 Instruction *InsertPt = &CI;
65 Instruction *AnnotatedInst = &CI;
66 Candidates->emplace_back(CandidateInfo{Length, InsertPt, AnnotatedInst});
67 }
68 }
69};
70
71///------------------------ IndirectCallPromotionPlugin ------------------------
72class IndirectCallPromotionPlugin {
73 Function &F;
74
75public:
76 static constexpr InstrProfValueKind Kind = IPVK_IndirectCallTarget;
77
78 IndirectCallPromotionPlugin(Function &Fn, TargetLibraryInfo &TLI) : F(Fn) {}
79
80 void run(std::vector<CandidateInfo> &Candidates) {
81 std::vector<CallBase *> Result = findIndirectCalls(F);
82 for (Instruction *I : Result) {
83 Value *Callee = cast<CallBase>(I)->getCalledOperand();
84 Instruction *InsertPt = I;
85 Instruction *AnnotatedInst = I;
86 Candidates.emplace_back(CandidateInfo{Callee, InsertPt, AnnotatedInst});
87 }
88 }
89};
90
91///--------------------- VirtualTableValueProfilingPlugin --------------------
92class VTableProfilingPlugin {
93 Function &F;
94
95public:
96 static constexpr InstrProfValueKind Kind = IPVK_VTableTarget;
97
98 VTableProfilingPlugin(Function &Fn, TargetLibraryInfo &TLI) : F(Fn) {}
99
100 void run(std::vector<CandidateInfo> &Candidates) {
101 std::vector<Instruction *> Result = findVTableAddrs(F);
102 for (Instruction *I : Result) {
103 Instruction *InsertPt = I->getNextNode();
104 // When finding an insertion point, keep PHI and EH pad instructions
105 // before vp intrinsics. This is similar to
106 // `BasicBlock::getFirstInsertionPt`.
107 while (InsertPt && (dyn_cast<PHINode>(InsertPt) || InsertPt->isEHPad()))
108 InsertPt = InsertPt->getNextNode();
109 // Skip instrumentating the value if InsertPt is the last instruction.
110 // FIXME: Set InsertPt to the end of basic block to instrument the value
111 // if InsertPt is the last instruction.
112 if (InsertPt == nullptr)
113 continue;
114
115 Instruction *AnnotatedInst = I;
116 Candidates.emplace_back(CandidateInfo{I, InsertPt, AnnotatedInst});
117 }
118 }
119};
120
121///----------------------- Registration of the plugins -------------------------
122/// For now, registering a plugin with the ValueProfileCollector is done by
123/// adding the plugin type to the VP_PLUGIN_LIST macro.
124#define VP_PLUGIN_LIST \
125 MemIntrinsicPlugin, IndirectCallPromotionPlugin, VTableProfilingPlugin
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static void visit(BasicBlock &Start, std::function< bool(BasicBlock *)> op)
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Value * getArgOperand(unsigned i) const
Base class for instruction visitors.
Definition InstVisitor.h:78
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
LibFunc getLibFunc(StringRef funcName) const
Searches for a particular function name.
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
@ Length
Definition DWP.cpp:577
std::vector< CallBase * > findIndirectCalls(Function &F)
InstrProfValueKind
Definition InstrProf.h:324
std::vector< Instruction * > findVTableAddrs(Function &F)