LLVM  14.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/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"
26 
27 using namespace llvm;
28 using namespace llvm::ore;
29 
30 #define DEBUG_TYPE "annotation-remarks"
31 #define REMARK_PASS DEBUG_TYPE
32 
35  const TargetLibraryInfo &TLI) {
36  // For every auto-init annotation generate a separate remark.
37  for (Instruction *I : Instructions) {
39  continue;
40 
41  Function &F = *I->getParent()->getParent();
42  const DataLayout &DL = F.getParent()->getDataLayout();
43  AutoInitRemark Remark(ORE, REMARK_PASS, DL, TLI);
44  Remark.visit(I);
45  }
46 }
47 
48 static void runImpl(Function &F, const TargetLibraryInfo &TLI) {
50  return;
51 
52  // Track all annotated instructions aggregated based on their debug location.
54 
56  // First, generate a summary of the annotated instructions.
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  for (const auto &KV : Mapping)
72  ORE.emit(OptimizationRemarkAnalysis(REMARK_PASS, "AnnotationSummary",
73  F.getSubprogram(), &F.front())
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, TLI);
85  }
86 }
87 
88 namespace {
89 
90 struct AnnotationRemarksLegacy : public FunctionPass {
91  static char ID;
92 
93  AnnotationRemarksLegacy() : FunctionPass(ID) {
95  }
96 
97  bool runOnFunction(Function &F) override {
98  const TargetLibraryInfo &TLI =
99  getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
100  runImpl(F, TLI);
101  return false;
102  }
103 
104  void getAnalysisUsage(AnalysisUsage &AU) const override {
105  AU.setPreservesAll();
107  }
108 };
109 
110 } // end anonymous namespace
111 
113 
114 INITIALIZE_PASS_BEGIN(AnnotationRemarksLegacy, "annotation-remarks",
115  "Annotation Remarks", false, false)
117 INITIALIZE_PASS_END(AnnotationRemarksLegacy, "annotation-remarks",
118  "Annotation Remarks", false, false)
119 
121  return new AnnotationRemarksLegacy();
122 }
123 
126  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
127  runImpl(F, TLI);
128  return PreservedAnalyses::all();
129 }
runImpl
static void runImpl(Function &F, const TargetLibraryInfo &TLI)
Definition: AnnotationRemarks.cpp:48
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
IntrinsicInst.h
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:783
Scalar.h
InstIterator.h
llvm::Function
Definition: Function.h:62
Pass.h
llvm::AnnotationRemarksPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: AnnotationRemarks.cpp:124
llvm::AutoInitRemark
Special case for -ftrivial-auto-var-init remarks.
Definition: MemoryOpRemark.h:98
MapVector.h
OptimizationRemarkEmitter.h
llvm::OptimizationRemarkEmitter::allowExtraAnalysis
bool allowExtraAnalysis(StringRef PassName) const
Whether we allow for extra compile-time budget to perform more analysis to produce fewer false positi...
Definition: OptimizationRemarkEmitter.h:98
Instructions
Code Generation Notes for reduce the size of the ISel and reduce repetition in the implementation In a small number of this can cause even when no optimisation has taken place Instructions
Definition: MSA.txt:11
llvm::MapVector
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:37
llvm::ore::NV
DiagnosticInfoOptimizationBase::Argument NV
Definition: OptimizationRemarkEmitter.h:136
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(AnnotationRemarksLegacy, "annotation-remarks", "Annotation Remarks", false, false) INITIALIZE_PASS_END(AnnotationRemarksLegacy
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
TargetLibraryInfo.h
false
Definition: StackSlotColoring.cpp:142
remarks
annotation remarks
Definition: AnnotationRemarks.cpp:117
llvm::Instruction
Definition: Instruction.h:45
llvm::createAnnotationRemarksLegacyPass
FunctionPass * createAnnotationRemarksLegacyPass()
Definition: AnnotationRemarks.cpp:120
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
llvm::HighlightColor::Remark
@ Remark
llvm::instructions
inst_range instructions(Function *F)
Definition: InstIterator.h:133
llvm::TargetLibraryInfoWrapperPass
Definition: TargetLibraryInfo.h:465
llvm::AutoInitRemark::canHandle
static bool canHandle(const Instruction *I)
Definition: MemoryOpRemark.cpp:383
INITIALIZE_PASS_DEPENDENCY
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Remarks
annotation Annotation Remarks
Definition: AnnotationRemarks.cpp:118
llvm::DenseMap
Definition: DenseMap.h:714
I
#define I(x, y, z)
Definition: MD5.cpp:59
MemoryOpRemark.h
llvm::OptimizationRemarkEmitter::emit
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Definition: OptimizationRemarkEmitter.cpp:77
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::OptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: OptimizationRemarkEmitter.h:33
llvm::MapVector::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: MapVector.h:118
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::OptimizationRemarkAnalysis
Diagnostic information for optimization analysis remarks.
Definition: DiagnosticInfo.h:776
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:207
AnnotationRemarks.h
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:324
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
Function.h
llvm::initializeAnnotationRemarksLegacyPass
void initializeAnnotationRemarksLegacyPass(PassRegistry &)
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:221
tryEmitAutoInitRemark
static void tryEmitAutoInitRemark(ArrayRef< Instruction * > Instructions, OptimizationRemarkEmitter &ORE, const TargetLibraryInfo &TLI)
Definition: AnnotationRemarks.cpp:33
REMARK_PASS
#define REMARK_PASS
Definition: AnnotationRemarks.cpp:31
Instructions.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::ore
Add a small namespace to avoid name clashes with the classes used in the streaming interface.
Definition: OptimizationRemarkEmitter.h:135
InitializePasses.h
Debug.h
llvm::TargetLibraryAnalysis
Analysis pass providing the TargetLibraryInfo.
Definition: TargetLibraryInfo.h:440
llvm::MDOperand
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:753
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38