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