]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/lib/Transforms/Instrumentation/ValueProfilePlugins.inc
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / lib / Transforms / Instrumentation / ValueProfilePlugins.inc
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"
17 #include "llvm/Analysis/IndirectCallVisitor.h"
18 #include "llvm/IR/InstVisitor.h"
19
20 using namespace llvm;
21 using CandidateInfo = ValueProfileCollector::CandidateInfo;
22
23 ///--------------------------- MemIntrinsicPlugin ------------------------------
24 class MemIntrinsicPlugin : public InstVisitor<MemIntrinsicPlugin> {
25   Function &F;
26   std::vector<CandidateInfo> *Candidates;
27
28 public:
29   static constexpr InstrProfValueKind Kind = IPVK_MemOPSize;
30
31   MemIntrinsicPlugin(Function &Fn) : F(Fn), Candidates(nullptr) {}
32
33   void run(std::vector<CandidateInfo> &Cs) {
34     Candidates = &Cs;
35     visit(F);
36     Candidates = nullptr;
37   }
38   void visitMemIntrinsic(MemIntrinsic &MI) {
39     Value *Length = MI.getLength();
40     // Not instrument constant length calls.
41     if (dyn_cast<ConstantInt>(Length))
42       return;
43
44     Instruction *InsertPt = &MI;
45     Instruction *AnnotatedInst = &MI;
46     Candidates->emplace_back(CandidateInfo{Length, InsertPt, AnnotatedInst});
47   }
48 };
49
50 ///------------------------ IndirectCallPromotionPlugin ------------------------
51 class IndirectCallPromotionPlugin {
52   Function &F;
53
54 public:
55   static constexpr InstrProfValueKind Kind = IPVK_IndirectCallTarget;
56
57   IndirectCallPromotionPlugin(Function &Fn) : F(Fn) {}
58
59   void run(std::vector<CandidateInfo> &Candidates) {
60     std::vector<Instruction *> Result = findIndirectCalls(F);
61     for (Instruction *I : Result) {
62       Value *Callee = CallSite(I).getCalledValue();
63       Instruction *InsertPt = I;
64       Instruction *AnnotatedInst = I;
65       Candidates.emplace_back(CandidateInfo{Callee, InsertPt, AnnotatedInst});
66     }
67   }
68 };
69
70 ///----------------------- Registration of the plugins -------------------------
71 /// For now, registering a plugin with the ValueProfileCollector is done by
72 /// adding the plugin type to the VP_PLUGIN_LIST macro.
73 #define VP_PLUGIN_LIST           \
74     MemIntrinsicPlugin,          \
75     IndirectCallPromotionPlugin