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