LLVM  13.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) {
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 
80 static void runImpl(Function &F, const TargetLibraryInfo &TLI) {
82  return;
83 
84  // Track all annotated instructions aggregated based on their debug location.
86 
88  // First, generate a summary of the annotated instructions.
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 
125  AnnotationRemarksLegacy() : FunctionPass(ID) {
127  }
128 
129  bool runOnFunction(Function &F) override {
130  const TargetLibraryInfo &TLI =
131  getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
132  runImpl(F, TLI);
133  return false;
134  }
135 
136  void getAnalysisUsage(AnalysisUsage &AU) const override {
137  AU.setPreservesAll();
139  }
140 };
141 
142 } // end anonymous namespace
143 
145 
146 INITIALIZE_PASS_BEGIN(AnnotationRemarksLegacy, "annotation-remarks",
147  "Annotation Remarks", false, false)
149 INITIALIZE_PASS_END(AnnotationRemarksLegacy, "annotation-remarks",
150  "Annotation Remarks", false, false)
151 
153  return new AnnotationRemarksLegacy();
154 }
155 
158  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
159  runImpl(F, TLI);
160  return PreservedAnalyses::all();
161 }
runImpl
static void runImpl(Function &F, const TargetLibraryInfo &TLI)
Definition: AnnotationRemarks.cpp:80
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
Definition: AllocatorList.h:23
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
AutoInitRemark.h
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:785
Scalar.h
InstIterator.h
llvm::Function
Definition: Function.h:61
Pass.h
llvm::AnnotationRemarksPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: AnnotationRemarks.cpp:156
llvm::AutoInitRemark
Definition: AutoInitRemark.h:34
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:90
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:128
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
SI
@ SI
Definition: SIInstrInfo.cpp:7463
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:149
llvm::Instruction
Definition: Instruction.h:45
llvm::createAnnotationRemarksLegacyPass
FunctionPass * createAnnotationRemarksLegacyPass()
Definition: AnnotationRemarks.cpp:152
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:446
INITIALIZE_PASS_DEPENDENCY
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Remarks
annotation Annotation Remarks
Definition: AnnotationRemarks.cpp:150
llvm::DenseMap
Definition: DenseMap.h:714
I
#define I(x, y, z)
Definition: MD5.cpp:59
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:117
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::OptimizationRemarkAnalysis
Diagnostic information for optimization analysis remarks.
Definition: DiagnosticInfo.h:770
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:314
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:207
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:127
InitializePasses.h
Debug.h
llvm::TargetLibraryAnalysis
Analysis pass providing the TargetLibraryInfo.
Definition: TargetLibraryInfo.h:421
llvm::MDOperand
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:744
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38