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/Transforms/Scalar.h"
20 #include "llvm/Transforms/Utils/MemoryOpRemark.h"
21
22 using namespace llvm;
23 using namespace llvm::ore;
24
25 #define DEBUG_TYPE "annotation-remarks"
26 #define REMARK_PASS DEBUG_TYPE
27
tryEmitAutoInitRemark(ArrayRef<Instruction * > Instructions,OptimizationRemarkEmitter & ORE,const TargetLibraryInfo & TLI)28 static void tryEmitAutoInitRemark(ArrayRef<Instruction *> Instructions,
29 OptimizationRemarkEmitter &ORE,
30 const TargetLibraryInfo &TLI) {
31 // For every auto-init annotation generate a separate remark.
32 for (Instruction *I : Instructions) {
33 if (!AutoInitRemark::canHandle(I))
34 continue;
35
36 Function &F = *I->getParent()->getParent();
37 const DataLayout &DL = F.getParent()->getDataLayout();
38 AutoInitRemark Remark(ORE, REMARK_PASS, DL, TLI);
39 Remark.visit(I);
40 }
41 }
42
runImpl(Function & F,const TargetLibraryInfo & TLI)43 static void runImpl(Function &F, const TargetLibraryInfo &TLI) {
44 if (!OptimizationRemarkEmitter::allowExtraAnalysis(F, REMARK_PASS))
45 return;
46
47 // Track all annotated instructions aggregated based on their debug location.
48 DenseMap<MDNode *, SmallVector<Instruction *, 4>> DebugLoc2Annotated;
49
50 OptimizationRemarkEmitter ORE(&F);
51 // First, generate a summary of the annotated instructions.
52 MapVector<StringRef, unsigned> Mapping;
53 for (Instruction &I : instructions(F)) {
54 if (!I.hasMetadata(LLVMContext::MD_annotation))
55 continue;
56 auto Iter = DebugLoc2Annotated.insert({I.getDebugLoc().getAsMDNode(), {}});
57 Iter.first->second.push_back(&I);
58
59 for (const MDOperand &Op :
60 I.getMetadata(LLVMContext::MD_annotation)->operands()) {
61 auto Iter = Mapping.insert({cast<MDString>(Op.get())->getString(), 0});
62 Iter.first->second++;
63 }
64 }
65
66 for (const auto &KV : Mapping)
67 ORE.emit(OptimizationRemarkAnalysis(REMARK_PASS, "AnnotationSummary",
68 F.getSubprogram(), &F.front())
69 << "Annotated " << NV("count", KV.second) << " instructions with "
70 << NV("type", KV.first));
71
72 // For each debug location, look for all the instructions with annotations and
73 // generate more detailed remarks to be displayed at that location.
74 for (auto &KV : DebugLoc2Annotated) {
75 // Don't generate remarks with no debug location.
76 if (!KV.first)
77 continue;
78
79 tryEmitAutoInitRemark(KV.second, ORE, TLI);
80 }
81 }
82
run(Function & F,FunctionAnalysisManager & AM)83 PreservedAnalyses AnnotationRemarksPass::run(Function &F,
84 FunctionAnalysisManager &AM) {
85 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
86 runImpl(F, TLI);
87 return PreservedAnalyses::all();
88 }
89