LLVM  12.0.0git
AnnotationRemarks.cpp
Go to the documentation of this file.
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 
14 #include "llvm/ADT/MapVector.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 runImpl(Function &F) {
32  return;
33 
35  // For now, just generate a summary of the annotated instructions.
37  for (Instruction &I : instructions(F)) {
38  if (!I.hasMetadata(LLVMContext::MD_annotation))
39  continue;
40  for (const MDOperand &Op :
41  I.getMetadata(LLVMContext::MD_annotation)->operands()) {
42  auto Iter = Mapping.insert({cast<MDString>(Op.get())->getString(), 0});
43  Iter.first->second++;
44  }
45  }
46 
47  Instruction *IP = &*F.begin()->begin();
48  for (const auto &KV : Mapping)
49  ORE.emit(OptimizationRemarkAnalysis(REMARK_PASS, "AnnotationSummary", IP)
50  << "Annotated " << NV("count", KV.second) << " instructions with "
51  << NV("type", KV.first));
52 }
53 
54 namespace {
55 
56 struct AnnotationRemarksLegacy : public FunctionPass {
57  static char ID;
58 
59  AnnotationRemarksLegacy() : FunctionPass(ID) {
61  }
62 
63  bool runOnFunction(Function &F) override {
64  runImpl(F);
65  return false;
66  }
67 
68  void getAnalysisUsage(AnalysisUsage &AU) const override {
69  AU.setPreservesAll();
70  }
71 };
72 
73 } // end anonymous namespace
74 
76 
77 INITIALIZE_PASS_BEGIN(AnnotationRemarksLegacy, "annotation-remarks",
78  "Annotation Remarks", false, false)
79 INITIALIZE_PASS_END(AnnotationRemarksLegacy, "annotation-remarks",
80  "Annotation Remarks", false, false)
81 
83  return new AnnotationRemarksLegacy();
84 }
85 
88  runImpl(F);
89  return PreservedAnalyses::all();
90 }
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:717
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
DiagnosticInfoOptimizationBase::Argument NV
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static void runImpl(Function &F)
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:37
F(f)
Diagnostic information for optimization analysis remarks.
INITIALIZE_PASS_BEGIN(AnnotationRemarksLegacy, "annotation-remarks", "Annotation Remarks", false, false) INITIALIZE_PASS_END(AnnotationRemarksLegacy
annotation remarks
void initializeAnnotationRemarksLegacyPass(PassRegistry &)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
annotation Annotation Remarks
static bool runOnFunction(Function &F, bool PostInlining)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
Used in the streaming interface as the general argument type.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: MapVector.h:117
bool allowExtraAnalysis(StringRef PassName) const
Whether we allow for extra compile-time budget to perform more analysis to produce fewer false positi...
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
void setPreservesAll()
Set by analyses that do not transform their input at all.
#define I(x, y, z)
Definition: MD5.cpp:59
inst_range instructions(Function *F)
Definition: InstIterator.h:133
A container for analyses that lazily runs them and caches their results.
#define REMARK_PASS
The optimization diagnostic interface.
FunctionPass * createAnnotationRemarksLegacyPass()
Add a small namespace to avoid name clashes with the classes used in the streaming interface.