ValueProfilePlugins.inc 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "ValueProfileCollector.h"
  16. #include "llvm/Analysis/IndirectCallVisitor.h"
  17. #include "llvm/IR/InstVisitor.h"
  18. using namespace llvm;
  19. using CandidateInfo = ValueProfileCollector::CandidateInfo;
  20. extern cl::opt<bool> MemOPOptMemcmpBcmp;
  21. ///--------------------------- MemIntrinsicPlugin ------------------------------
  22. class MemIntrinsicPlugin : public InstVisitor<MemIntrinsicPlugin> {
  23. Function &F;
  24. TargetLibraryInfo &TLI;
  25. std::vector<CandidateInfo> *Candidates;
  26. public:
  27. static constexpr InstrProfValueKind Kind = IPVK_MemOPSize;
  28. MemIntrinsicPlugin(Function &Fn, TargetLibraryInfo &TLI)
  29. : F(Fn), TLI(TLI), Candidates(nullptr) {}
  30. void run(std::vector<CandidateInfo> &Cs) {
  31. Candidates = &Cs;
  32. visit(F);
  33. Candidates = nullptr;
  34. }
  35. void visitMemIntrinsic(MemIntrinsic &MI) {
  36. Value *Length = MI.getLength();
  37. // Not instrument constant length calls.
  38. if (isa<ConstantInt>(Length))
  39. return;
  40. Instruction *InsertPt = &MI;
  41. Instruction *AnnotatedInst = &MI;
  42. Candidates->emplace_back(CandidateInfo{Length, InsertPt, AnnotatedInst});
  43. }
  44. void visitCallInst(CallInst &CI) {
  45. if (!MemOPOptMemcmpBcmp)
  46. return;
  47. auto *F = CI.getCalledFunction();
  48. if (!F)
  49. return;
  50. LibFunc Func;
  51. if (TLI.getLibFunc(CI, Func) &&
  52. (Func == LibFunc_memcmp || Func == LibFunc_bcmp)) {
  53. Value *Length = CI.getArgOperand(2);
  54. // Not instrument constant length calls.
  55. if (isa<ConstantInt>(Length))
  56. return;
  57. Instruction *InsertPt = &CI;
  58. Instruction *AnnotatedInst = &CI;
  59. Candidates->emplace_back(CandidateInfo{Length, InsertPt, AnnotatedInst});
  60. }
  61. }
  62. };
  63. ///------------------------ IndirectCallPromotionPlugin ------------------------
  64. class IndirectCallPromotionPlugin {
  65. Function &F;
  66. public:
  67. static constexpr InstrProfValueKind Kind = IPVK_IndirectCallTarget;
  68. IndirectCallPromotionPlugin(Function &Fn, TargetLibraryInfo &TLI) : F(Fn) {}
  69. void run(std::vector<CandidateInfo> &Candidates) {
  70. std::vector<CallBase *> Result = findIndirectCalls(F);
  71. for (Instruction *I : Result) {
  72. Value *Callee = cast<CallBase>(I)->getCalledOperand();
  73. Instruction *InsertPt = I;
  74. Instruction *AnnotatedInst = I;
  75. Candidates.emplace_back(CandidateInfo{Callee, InsertPt, AnnotatedInst});
  76. }
  77. }
  78. };
  79. ///----------------------- Registration of the plugins -------------------------
  80. /// For now, registering a plugin with the ValueProfileCollector is done by
  81. /// adding the plugin type to the VP_PLUGIN_LIST macro.
  82. #define VP_PLUGIN_LIST \
  83. MemIntrinsicPlugin, \
  84. IndirectCallPromotionPlugin