ValueProfilePlugins.inc 3.3 KB

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