AnnotationRemarks.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //===-- AnnotationRemarks.cpp - Generate remarks for annotated instrs. ----===//
  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. // Generate remarks for instructions marked with !annotation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Transforms/Scalar/AnnotationRemarks.h"
  13. #include "llvm/ADT/MapVector.h"
  14. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  15. #include "llvm/Analysis/TargetLibraryInfo.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/InstIterator.h"
  18. #include "llvm/IR/Instructions.h"
  19. #include "llvm/IR/IntrinsicInst.h"
  20. #include "llvm/InitializePasses.h"
  21. #include "llvm/Pass.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Transforms/Scalar.h"
  24. #include "llvm/Transforms/Utils/MemoryOpRemark.h"
  25. using namespace llvm;
  26. using namespace llvm::ore;
  27. #define DEBUG_TYPE "annotation-remarks"
  28. #define REMARK_PASS DEBUG_TYPE
  29. static void tryEmitAutoInitRemark(ArrayRef<Instruction *> Instructions,
  30. OptimizationRemarkEmitter &ORE,
  31. const TargetLibraryInfo &TLI) {
  32. // For every auto-init annotation generate a separate remark.
  33. for (Instruction *I : Instructions) {
  34. if (!AutoInitRemark::canHandle(I))
  35. continue;
  36. Function &F = *I->getParent()->getParent();
  37. const DataLayout &DL = F.getParent()->getDataLayout();
  38. AutoInitRemark Remark(ORE, REMARK_PASS, DL, TLI);
  39. Remark.visit(I);
  40. }
  41. }
  42. static void runImpl(Function &F, const TargetLibraryInfo &TLI) {
  43. if (!OptimizationRemarkEmitter::allowExtraAnalysis(F, REMARK_PASS))
  44. return;
  45. // Track all annotated instructions aggregated based on their debug location.
  46. DenseMap<MDNode *, SmallVector<Instruction *, 4>> DebugLoc2Annotated;
  47. OptimizationRemarkEmitter ORE(&F);
  48. // First, generate a summary of the annotated instructions.
  49. MapVector<StringRef, unsigned> Mapping;
  50. for (Instruction &I : instructions(F)) {
  51. if (!I.hasMetadata(LLVMContext::MD_annotation))
  52. continue;
  53. auto Iter = DebugLoc2Annotated.insert({I.getDebugLoc().getAsMDNode(), {}});
  54. Iter.first->second.push_back(&I);
  55. for (const MDOperand &Op :
  56. I.getMetadata(LLVMContext::MD_annotation)->operands()) {
  57. auto Iter = Mapping.insert({cast<MDString>(Op.get())->getString(), 0});
  58. Iter.first->second++;
  59. }
  60. }
  61. for (const auto &KV : Mapping)
  62. ORE.emit(OptimizationRemarkAnalysis(REMARK_PASS, "AnnotationSummary",
  63. F.getSubprogram(), &F.front())
  64. << "Annotated " << NV("count", KV.second) << " instructions with "
  65. << NV("type", KV.first));
  66. // For each debug location, look for all the instructions with annotations and
  67. // generate more detailed remarks to be displayed at that location.
  68. for (auto &KV : DebugLoc2Annotated) {
  69. // Don't generate remarks with no debug location.
  70. if (!KV.first)
  71. continue;
  72. tryEmitAutoInitRemark(KV.second, ORE, TLI);
  73. }
  74. }
  75. namespace {
  76. struct AnnotationRemarksLegacy : public FunctionPass {
  77. static char ID;
  78. AnnotationRemarksLegacy() : FunctionPass(ID) {
  79. initializeAnnotationRemarksLegacyPass(*PassRegistry::getPassRegistry());
  80. }
  81. bool runOnFunction(Function &F) override {
  82. const TargetLibraryInfo &TLI =
  83. getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  84. runImpl(F, TLI);
  85. return false;
  86. }
  87. void getAnalysisUsage(AnalysisUsage &AU) const override {
  88. AU.setPreservesAll();
  89. AU.addRequired<TargetLibraryInfoWrapperPass>();
  90. }
  91. };
  92. } // end anonymous namespace
  93. char AnnotationRemarksLegacy::ID = 0;
  94. INITIALIZE_PASS_BEGIN(AnnotationRemarksLegacy, "annotation-remarks",
  95. "Annotation Remarks", false, false)
  96. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  97. INITIALIZE_PASS_END(AnnotationRemarksLegacy, "annotation-remarks",
  98. "Annotation Remarks", false, false)
  99. FunctionPass *llvm::createAnnotationRemarksLegacyPass() {
  100. return new AnnotationRemarksLegacy();
  101. }
  102. PreservedAnalyses AnnotationRemarksPass::run(Function &F,
  103. FunctionAnalysisManager &AM) {
  104. auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
  105. runImpl(F, TLI);
  106. return PreservedAnalyses::all();
  107. }