LLVM  14.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 
16 #include "ValueProfileCollector.h"
18 #include "llvm/IR/InstVisitor.h"
19 
20 using namespace llvm;
21 using CandidateInfo = ValueProfileCollector::CandidateInfo;
22 
24 
25 ///--------------------------- MemIntrinsicPlugin ------------------------------
26 class MemIntrinsicPlugin : public InstVisitor<MemIntrinsicPlugin> {
27  Function &F;
28  TargetLibraryInfo &TLI;
29  std::vector<CandidateInfo> *Candidates;
30 
31 public:
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 (!MemOPOptMemcmpBcmp)
54  return;
55  auto *F = CI.getCalledFunction();
56  if (!F)
57  return;
58  LibFunc Func;
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))
64  return;
65  Instruction *InsertPt = &CI;
66  Instruction *AnnotatedInst = &CI;
67  Candidates->emplace_back(CandidateInfo{Length, InsertPt, AnnotatedInst});
68  }
69  }
70 };
71 
72 ///------------------------ IndirectCallPromotionPlugin ------------------------
73 class IndirectCallPromotionPlugin {
74  Function &F;
75 
76 public:
77  static constexpr InstrProfValueKind Kind = IPVK_IndirectCallTarget;
78 
79  IndirectCallPromotionPlugin(Function &Fn, TargetLibraryInfo &TLI) : F(Fn) {}
80 
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});
88  }
89  }
90 };
91 
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 \
96  MemIntrinsicPlugin, \
97  IndirectCallPromotionPlugin
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:105
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::Function
Definition: Function.h:62
llvm::MemIntrinsic
This is the common base class for memset/memcpy/memmove.
Definition: IntrinsicInst.h:874
F
#define F(x, y, z)
Definition: MD5.cpp:56
IndirectCallVisitor.h
llvm::LibFunc
LibFunc
Definition: TargetLibraryInfo.h:34
llvm::CallBase::getCalledFunction
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1398
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
llvm::TargetLibraryInfo::getLibFunc
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
Definition: TargetLibraryInfo.h:291
llvm::Instruction
Definition: Instruction.h:45
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::cl::opt< bool >
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::InstrProfValueKind
InstrProfValueKind
Definition: InstrProf.h:238
InstVisitor.h
llvm::InstVisitor
Base class for instruction visitors.
Definition: InstVisitor.h:79
llvm::ifs::IFSSymbolType::Func
@ Func
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:206
llvm::findIndirectCalls
std::vector< CallBase * > findIndirectCalls(Function &F)
Definition: IndirectCallVisitor.h:31
ValueProfileCollector.h
llvm::ValueProfileCollector::CandidateInfo
Definition: ValueProfileCollector.h:59
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:221
llvm::CallBase::getArgOperand
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1343
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1487
MemOPOptMemcmpBcmp
cl::opt< bool > MemOPOptMemcmpBcmp("pgo-memop-optimize-memcmp-bcmp", cl::init(true), cl::Hidden, cl::desc("Size-specialize memcmp and bcmp calls"))
llvm::Value
LLVM Value Representation.
Definition: Value.h:74