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/Support/Debug.h" 22 #include "llvm/Transforms/Scalar.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 // For every auto-init annotation generate a separate remark. 33 for (Instruction *I : Instructions) { 34 if (!I->hasMetadata(LLVMContext::MD_annotation)) 35 continue; 36 for (const MDOperand &Op : 37 I->getMetadata(LLVMContext::MD_annotation)->operands()) { 38 if (cast<MDString>(Op.get())->getString() != "auto-init") 39 continue; 40 41 ORE.emit( 42 OptimizationRemarkMissed(REMARK_PASS, "AutoInitUnknownInstruction", I) 43 << "Initialization inserted by -ftrivial-auto-var-init."); 44 } 45 } 46 } 47 48 static void runImpl(Function &F) { 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 Instruction *IP = &*F.begin()->begin(); 72 for (const auto &KV : Mapping) 73 ORE.emit(OptimizationRemarkAnalysis(REMARK_PASS, "AnnotationSummary", IP) 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); 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 runImpl(F); 99 return false; 100 } 101 102 void getAnalysisUsage(AnalysisUsage &AU) const override { 103 AU.setPreservesAll(); 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_END(AnnotationRemarksLegacy, "annotation-remarks", 114 "Annotation Remarks", false, false) 115 116 FunctionPass *llvm::createAnnotationRemarksLegacyPass() { 117 return new AnnotationRemarksLegacy(); 118 } 119 120 PreservedAnalyses AnnotationRemarksPass::run(Function &F, 121 FunctionAnalysisManager &AM) { 122 runImpl(F); 123 return PreservedAnalyses::all(); 124 } 125