LLVM 19.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"
20
21using namespace llvm;
22using namespace llvm::ore;
23
24#define DEBUG_TYPE "annotation-remarks"
25#define REMARK_PASS DEBUG_TYPE
26
29 const TargetLibraryInfo &TLI) {
30 // For every auto-init annotation generate a separate remark.
31 for (Instruction *I : Instructions) {
33 continue;
34
35 Function &F = *I->getParent()->getParent();
36 const DataLayout &DL = F.getParent()->getDataLayout();
38 Remark.visit(I);
39 }
40}
41
42static void runImpl(Function &F, const TargetLibraryInfo &TLI) {
44 return;
45
46 // Track all annotated instructions aggregated based on their debug location.
48
50 // First, generate a summary of the annotated instructions.
52 for (Instruction &I : instructions(F)) {
53 if (!I.hasMetadata(LLVMContext::MD_annotation))
54 continue;
55 auto Iter = DebugLoc2Annotated.insert({I.getDebugLoc().getAsMDNode(), {}});
56 Iter.first->second.push_back(&I);
57
58 for (const MDOperand &Op :
59 I.getMetadata(LLVMContext::MD_annotation)->operands()) {
60 StringRef AnnotationStr =
61 isa<MDString>(Op.get())
62 ? cast<MDString>(Op.get())->getString()
63 : cast<MDString>(cast<MDTuple>(Op.get())->getOperand(0).get())
64 ->getString();
65 auto Iter = Mapping.insert({AnnotationStr, 0});
66 Iter.first->second++;
67 }
68 }
69
70 for (const auto &KV : Mapping)
71 ORE.emit(OptimizationRemarkAnalysis(REMARK_PASS, "AnnotationSummary",
72 F.getSubprogram(), &F.front())
73 << "Annotated " << NV("count", KV.second) << " instructions with "
74 << NV("type", KV.first));
75
76 // For each debug location, look for all the instructions with annotations and
77 // generate more detailed remarks to be displayed at that location.
78 for (auto &KV : DebugLoc2Annotated) {
79 // Don't generate remarks with no debug location.
80 if (!KV.first)
81 continue;
82
83 tryEmitAutoInitRemark(KV.second, ORE, TLI);
84 }
85}
86
89 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
90 runImpl(F, TLI);
92}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define REMARK_PASS
static void tryEmitAutoInitRemark(ArrayRef< Instruction * > Instructions, OptimizationRemarkEmitter &ORE, const TargetLibraryInfo &TLI)
static void runImpl(Function &F, const TargetLibraryInfo &TLI)
Expand Atomic instructions
static bool runImpl(Function &F, const TargetLowering &TLI)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file implements a map that provides insertion order iteration.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:348
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:500
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:889
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: MapVector.h:141
Diagnostic information for optimization analysis remarks.
The optimization diagnostic interface.
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.
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
Add a small namespace to avoid name clashes with the classes used in the streaming interface.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Special case for -ftrivial-auto-var-init remarks.
static bool canHandle(const Instruction *I)
Used in the streaming interface as the general argument type.