AnnotationRemarks.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/Transforms/Scalar.h"
  19. #include "llvm/Transforms/Utils/MemoryOpRemark.h"
  20. using namespace llvm;
  21. using namespace llvm::ore;
  22. #define DEBUG_TYPE "annotation-remarks"
  23. #define REMARK_PASS DEBUG_TYPE
  24. static void tryEmitAutoInitRemark(ArrayRef<Instruction *> Instructions,
  25. OptimizationRemarkEmitter &ORE,
  26. const TargetLibraryInfo &TLI) {
  27. // For every auto-init annotation generate a separate remark.
  28. for (Instruction *I : Instructions) {
  29. if (!AutoInitRemark::canHandle(I))
  30. continue;
  31. Function &F = *I->getParent()->getParent();
  32. const DataLayout &DL = F.getParent()->getDataLayout();
  33. AutoInitRemark Remark(ORE, REMARK_PASS, DL, TLI);
  34. Remark.visit(I);
  35. }
  36. }
  37. static void runImpl(Function &F, const TargetLibraryInfo &TLI) {
  38. if (!OptimizationRemarkEmitter::allowExtraAnalysis(F, REMARK_PASS))
  39. return;
  40. // Track all annotated instructions aggregated based on their debug location.
  41. DenseMap<MDNode *, SmallVector<Instruction *, 4>> DebugLoc2Annotated;
  42. OptimizationRemarkEmitter ORE(&F);
  43. // First, generate a summary of the annotated instructions.
  44. MapVector<StringRef, unsigned> Mapping;
  45. for (Instruction &I : instructions(F)) {
  46. if (!I.hasMetadata(LLVMContext::MD_annotation))
  47. continue;
  48. auto Iter = DebugLoc2Annotated.insert({I.getDebugLoc().getAsMDNode(), {}});
  49. Iter.first->second.push_back(&I);
  50. for (const MDOperand &Op :
  51. I.getMetadata(LLVMContext::MD_annotation)->operands()) {
  52. auto Iter = Mapping.insert({cast<MDString>(Op.get())->getString(), 0});
  53. Iter.first->second++;
  54. }
  55. }
  56. for (const auto &KV : Mapping)
  57. ORE.emit(OptimizationRemarkAnalysis(REMARK_PASS, "AnnotationSummary",
  58. F.getSubprogram(), &F.front())
  59. << "Annotated " << NV("count", KV.second) << " instructions with "
  60. << NV("type", KV.first));
  61. // For each debug location, look for all the instructions with annotations and
  62. // generate more detailed remarks to be displayed at that location.
  63. for (auto &KV : DebugLoc2Annotated) {
  64. // Don't generate remarks with no debug location.
  65. if (!KV.first)
  66. continue;
  67. tryEmitAutoInitRemark(KV.second, ORE, TLI);
  68. }
  69. }
  70. PreservedAnalyses AnnotationRemarksPass::run(Function &F,
  71. FunctionAnalysisManager &AM) {
  72. auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
  73. runImpl(F, TLI);
  74. return PreservedAnalyses::all();
  75. }