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