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/AutoInitRemark.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
tryEmitAutoInitRemark(ArrayRef<Instruction * > Instructions,OptimizationRemarkEmitter & ORE,const TargetLibraryInfo & TLI)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 (!I->hasMetadata(LLVMContext::MD_annotation))
39 continue;
40 for (const MDOperand &Op :
41 I->getMetadata(LLVMContext::MD_annotation)->operands()) {
42 if (cast<MDString>(Op.get())->getString() != "auto-init")
43 continue;
44
45 Function &F = *I->getParent()->getParent();
46 const DataLayout &DL = F.getParent()->getDataLayout();
47 AutoInitRemark Remark(ORE, REMARK_PASS, DL, TLI);
48 // For some of them, we can provide more information:
49
50 // For stores:
51 // * size
52 // * volatile / atomic
53 if (auto *SI = dyn_cast<StoreInst>(I)) {
54 Remark.inspectStore(*SI);
55 continue;
56 }
57
58 // For intrinsics:
59 // * user-friendly name
60 // * size
61 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
62 Remark.inspectIntrinsicCall(*II);
63 continue;
64 }
65
66 // For calls:
67 // * known/unknown function (e.g. the compiler knows bzero, but it doesn't
68 // know my_bzero)
69 // * memory operation size
70 if (auto *CI = dyn_cast<CallInst>(I)) {
71 Remark.inspectCall(*CI);
72 continue;
73 }
74
75 Remark.inspectUnknown(*I);
76 }
77 }
78 }
79
runImpl(Function & F,const TargetLibraryInfo & TLI)80 static void runImpl(Function &F, const TargetLibraryInfo &TLI) {
81 if (!OptimizationRemarkEmitter::allowExtraAnalysis(F, REMARK_PASS))
82 return;
83
84 // Track all annotated instructions aggregated based on their debug location.
85 DenseMap<MDNode *, SmallVector<Instruction *, 4>> DebugLoc2Annotated;
86
87 OptimizationRemarkEmitter ORE(&F);
88 // First, generate a summary of the annotated instructions.
89 MapVector<StringRef, unsigned> Mapping;
90 for (Instruction &I : instructions(F)) {
91 if (!I.hasMetadata(LLVMContext::MD_annotation))
92 continue;
93 auto Iter = DebugLoc2Annotated.insert({I.getDebugLoc().getAsMDNode(), {}});
94 Iter.first->second.push_back(&I);
95
96 for (const MDOperand &Op :
97 I.getMetadata(LLVMContext::MD_annotation)->operands()) {
98 auto Iter = Mapping.insert({cast<MDString>(Op.get())->getString(), 0});
99 Iter.first->second++;
100 }
101 }
102
103 for (const auto &KV : Mapping)
104 ORE.emit(OptimizationRemarkAnalysis(REMARK_PASS, "AnnotationSummary",
105 F.getSubprogram(), &F.front())
106 << "Annotated " << NV("count", KV.second) << " instructions with "
107 << NV("type", KV.first));
108
109 // For each debug location, look for all the instructions with annotations and
110 // generate more detailed remarks to be displayed at that location.
111 for (auto &KV : DebugLoc2Annotated) {
112 // Don't generate remarks with no debug location.
113 if (!KV.first)
114 continue;
115
116 tryEmitAutoInitRemark(KV.second, ORE, TLI);
117 }
118 }
119
120 namespace {
121
122 struct AnnotationRemarksLegacy : public FunctionPass {
123 static char ID;
124
AnnotationRemarksLegacy__anon625dfa2e0111::AnnotationRemarksLegacy125 AnnotationRemarksLegacy() : FunctionPass(ID) {
126 initializeAnnotationRemarksLegacyPass(*PassRegistry::getPassRegistry());
127 }
128
runOnFunction__anon625dfa2e0111::AnnotationRemarksLegacy129 bool runOnFunction(Function &F) override {
130 const TargetLibraryInfo &TLI =
131 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
132 runImpl(F, TLI);
133 return false;
134 }
135
getAnalysisUsage__anon625dfa2e0111::AnnotationRemarksLegacy136 void getAnalysisUsage(AnalysisUsage &AU) const override {
137 AU.setPreservesAll();
138 AU.addRequired<TargetLibraryInfoWrapperPass>();
139 }
140 };
141
142 } // end anonymous namespace
143
144 char AnnotationRemarksLegacy::ID = 0;
145
146 INITIALIZE_PASS_BEGIN(AnnotationRemarksLegacy, "annotation-remarks",
147 "Annotation Remarks", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)148 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
149 INITIALIZE_PASS_END(AnnotationRemarksLegacy, "annotation-remarks",
150 "Annotation Remarks", false, false)
151
152 FunctionPass *llvm::createAnnotationRemarksLegacyPass() {
153 return new AnnotationRemarksLegacy();
154 }
155
run(Function & F,FunctionAnalysisManager & AM)156 PreservedAnalyses AnnotationRemarksPass::run(Function &F,
157 FunctionAnalysisManager &AM) {
158 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
159 runImpl(F, TLI);
160 return PreservedAnalyses::all();
161 }
162