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/IR/InstVisitor.h"
21 using CandidateInfo = ValueProfileCollector::CandidateInfo;
23 extern cl::opt<bool> MemOPOptMemcmpBcmp;
25 ///--------------------------- MemIntrinsicPlugin ------------------------------
26 class MemIntrinsicPlugin : public InstVisitor<MemIntrinsicPlugin> {
28 TargetLibraryInfo &TLI;
29 std::vector<CandidateInfo> *Candidates;
32 static constexpr InstrProfValueKind Kind = IPVK_MemOPSize;
34 MemIntrinsicPlugin(Function &Fn, TargetLibraryInfo &TLI)
35 : F(Fn), TLI(TLI), Candidates(nullptr) {}
37 void run(std::vector<CandidateInfo> &Cs) {
42 void visitMemIntrinsic(MemIntrinsic &MI) {
43 Value *Length = MI.getLength();
44 // Not instrument constant length calls.
45 if (isa<ConstantInt>(Length))
48 Instruction *InsertPt = &MI;
49 Instruction *AnnotatedInst = &MI;
50 Candidates->emplace_back(CandidateInfo{Length, InsertPt, AnnotatedInst});
52 void visitCallInst(CallInst &CI) {
53 if (!MemOPOptMemcmpBcmp)
55 auto *F = CI.getCalledFunction();
59 if (TLI.getLibFunc(CI, Func) &&
60 (Func == LibFunc_memcmp || Func == LibFunc_bcmp)) {
61 Value *Length = CI.getArgOperand(2);
62 // Not instrument constant length calls.
63 if (isa<ConstantInt>(Length))
65 Instruction *InsertPt = &CI;
66 Instruction *AnnotatedInst = &CI;
67 Candidates->emplace_back(CandidateInfo{Length, InsertPt, AnnotatedInst});
72 ///------------------------ IndirectCallPromotionPlugin ------------------------
73 class IndirectCallPromotionPlugin {
77 static constexpr InstrProfValueKind Kind = IPVK_IndirectCallTarget;
79 IndirectCallPromotionPlugin(Function &Fn, TargetLibraryInfo &TLI) : F(Fn) {}
81 void run(std::vector<CandidateInfo> &Candidates) {
82 std::vector<CallBase *> Result = findIndirectCalls(F);
83 for (Instruction *I : Result) {
84 Value *Callee = cast<CallBase>(I)->getCalledOperand();
85 Instruction *InsertPt = I;
86 Instruction *AnnotatedInst = I;
87 Candidates.emplace_back(CandidateInfo{Callee, InsertPt, AnnotatedInst});
92 ///----------------------- Registration of the plugins -------------------------
93 /// For now, registering a plugin with the ValueProfileCollector is done by
94 /// adding the plugin type to the VP_PLUGIN_LIST macro.
95 #define VP_PLUGIN_LIST \
97 IndirectCallPromotionPlugin