1 //=== ValueProfilePlugins.inc - set of plugins used by ValueProfileCollector =//
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
7 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #include "ValueProfileCollector.h"
17 #include "llvm/Analysis/IndirectCallVisitor.h"
18 #include "llvm/Analysis/TargetLibraryInfo.h"
19 #include "llvm/IR/InstVisitor.h"
22 using CandidateInfo = ValueProfileCollector::CandidateInfo;
24 extern cl::opt<bool> MemOPOptMemcmpBcmp;
26 ///--------------------------- MemIntrinsicPlugin ------------------------------
27 class MemIntrinsicPlugin : public InstVisitor<MemIntrinsicPlugin> {
29 TargetLibraryInfo &TLI;
30 std::vector<CandidateInfo> *Candidates;
33 static constexpr InstrProfValueKind Kind = IPVK_MemOPSize;
35 MemIntrinsicPlugin(Function &Fn, TargetLibraryInfo &TLI)
36 : F(Fn), TLI(TLI), Candidates(nullptr) {}
38 void run(std::vector<CandidateInfo> &Cs) {
43 void visitMemIntrinsic(MemIntrinsic &MI) {
44 Value *Length = MI.getLength();
45 // Not instrument constant length calls.
46 if (isa<ConstantInt>(Length))
49 Instruction *InsertPt = &MI;
50 Instruction *AnnotatedInst = &MI;
51 Candidates->emplace_back(CandidateInfo{Length, InsertPt, AnnotatedInst});
53 void visitCallInst(CallInst &CI) {
54 if (!MemOPOptMemcmpBcmp)
56 auto *F = CI.getCalledFunction();
60 if (TLI.getLibFunc(CI, Func) &&
61 (Func == LibFunc_memcmp || Func == LibFunc_bcmp)) {
62 Value *Length = CI.getArgOperand(2);
63 // Not instrument constant length calls.
64 if (isa<ConstantInt>(Length))
66 Instruction *InsertPt = &CI;
67 Instruction *AnnotatedInst = &CI;
68 Candidates->emplace_back(CandidateInfo{Length, InsertPt, AnnotatedInst});
73 ///------------------------ IndirectCallPromotionPlugin ------------------------
74 class IndirectCallPromotionPlugin {
78 static constexpr InstrProfValueKind Kind = IPVK_IndirectCallTarget;
80 IndirectCallPromotionPlugin(Function &Fn, TargetLibraryInfo &TLI) : F(Fn) {}
82 void run(std::vector<CandidateInfo> &Candidates) {
83 std::vector<CallBase *> Result = findIndirectCalls(F);
84 for (Instruction *I : Result) {
85 Value *Callee = cast<CallBase>(I)->getCalledOperand();
86 Instruction *InsertPt = I;
87 Instruction *AnnotatedInst = I;
88 Candidates.emplace_back(CandidateInfo{Callee, InsertPt, AnnotatedInst});
93 ///----------------------- Registration of the plugins -------------------------
94 /// For now, registering a plugin with the ValueProfileCollector is done by
95 /// adding the plugin type to the VP_PLUGIN_LIST macro.
96 #define VP_PLUGIN_LIST \
98 IndirectCallPromotionPlugin