Bug Summary

File:include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h
Warning:line 35, column 40
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Legalizer.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-9/lib/clang/9.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-9~svn361465/build-llvm/lib/CodeGen/GlobalISel -I /build/llvm-toolchain-snapshot-9~svn361465/lib/CodeGen/GlobalISel -I /build/llvm-toolchain-snapshot-9~svn361465/build-llvm/include -I /build/llvm-toolchain-snapshot-9~svn361465/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/9.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-9/lib/clang/9.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-9~svn361465/build-llvm/lib/CodeGen/GlobalISel -fdebug-prefix-map=/build/llvm-toolchain-snapshot-9~svn361465=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2019-05-24-031927-21217-1 -x c++ /build/llvm-toolchain-snapshot-9~svn361465/lib/CodeGen/GlobalISel/Legalizer.cpp -faddrsig

/build/llvm-toolchain-snapshot-9~svn361465/lib/CodeGen/GlobalISel/Legalizer.cpp

1//===-- llvm/CodeGen/GlobalISel/Legalizer.cpp -----------------------------===//
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/// \file This file implements the LegalizerHelper class to legalize individual
10/// instructions and the LegalizePass wrapper pass for the primary
11/// legalization.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/GlobalISel/Legalizer.h"
16#include "llvm/ADT/PostOrderIterator.h"
17#include "llvm/ADT/SetVector.h"
18#include "llvm/CodeGen/GlobalISel/CSEInfo.h"
19#include "llvm/CodeGen/GlobalISel/CSEMIRBuilder.h"
20#include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
21#include "llvm/CodeGen/GlobalISel/GISelWorkList.h"
22#include "llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h"
23#include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
24#include "llvm/CodeGen/GlobalISel/Utils.h"
25#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/CodeGen/TargetPassConfig.h"
28#include "llvm/CodeGen/TargetSubtargetInfo.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Target/TargetMachine.h"
31
32#include <iterator>
33
34#define DEBUG_TYPE"legalizer" "legalizer"
35
36using namespace llvm;
37
38static cl::opt<bool>
39 EnableCSEInLegalizer("enable-cse-in-legalizer",
40 cl::desc("Should enable CSE in Legalizer"),
41 cl::Optional, cl::init(false));
42
43char Legalizer::ID = 0;
44INITIALIZE_PASS_BEGIN(Legalizer, DEBUG_TYPE,static void *initializeLegalizerPassOnce(PassRegistry &Registry
) {
45 "Legalize the Machine IR a function's Machine IR", false,static void *initializeLegalizerPassOnce(PassRegistry &Registry
) {
46 false)static void *initializeLegalizerPassOnce(PassRegistry &Registry
) {
47INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)initializeTargetPassConfigPass(Registry);
48INITIALIZE_PASS_DEPENDENCY(GISelCSEAnalysisWrapperPass)initializeGISelCSEAnalysisWrapperPassPass(Registry);
49INITIALIZE_PASS_END(Legalizer, DEBUG_TYPE,PassInfo *PI = new PassInfo( "Legalize the Machine IR a function's Machine IR"
, "legalizer", &Legalizer::ID, PassInfo::NormalCtor_t(callDefaultCtor
<Legalizer>), false, false); Registry.registerPass(*PI,
true); return PI; } static llvm::once_flag InitializeLegalizerPassFlag
; void llvm::initializeLegalizerPass(PassRegistry &Registry
) { llvm::call_once(InitializeLegalizerPassFlag, initializeLegalizerPassOnce
, std::ref(Registry)); }
50 "Legalize the Machine IR a function's Machine IR", false,PassInfo *PI = new PassInfo( "Legalize the Machine IR a function's Machine IR"
, "legalizer", &Legalizer::ID, PassInfo::NormalCtor_t(callDefaultCtor
<Legalizer>), false, false); Registry.registerPass(*PI,
true); return PI; } static llvm::once_flag InitializeLegalizerPassFlag
; void llvm::initializeLegalizerPass(PassRegistry &Registry
) { llvm::call_once(InitializeLegalizerPassFlag, initializeLegalizerPassOnce
, std::ref(Registry)); }
51 false)PassInfo *PI = new PassInfo( "Legalize the Machine IR a function's Machine IR"
, "legalizer", &Legalizer::ID, PassInfo::NormalCtor_t(callDefaultCtor
<Legalizer>), false, false); Registry.registerPass(*PI,
true); return PI; } static llvm::once_flag InitializeLegalizerPassFlag
; void llvm::initializeLegalizerPass(PassRegistry &Registry
) { llvm::call_once(InitializeLegalizerPassFlag, initializeLegalizerPassOnce
, std::ref(Registry)); }
52
53Legalizer::Legalizer() : MachineFunctionPass(ID) {
54 initializeLegalizerPass(*PassRegistry::getPassRegistry());
55}
56
57void Legalizer::getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.addRequired<TargetPassConfig>();
59 AU.addRequired<GISelCSEAnalysisWrapperPass>();
60 AU.addPreserved<GISelCSEAnalysisWrapperPass>();
61 getSelectionDAGFallbackAnalysisUsage(AU);
62 MachineFunctionPass::getAnalysisUsage(AU);
63}
64
65void Legalizer::init(MachineFunction &MF) {
66}
67
68static bool isArtifact(const MachineInstr &MI) {
69 switch (MI.getOpcode()) {
70 default:
71 return false;
72 case TargetOpcode::G_TRUNC:
73 case TargetOpcode::G_ZEXT:
74 case TargetOpcode::G_ANYEXT:
75 case TargetOpcode::G_SEXT:
76 case TargetOpcode::G_MERGE_VALUES:
77 case TargetOpcode::G_UNMERGE_VALUES:
78 case TargetOpcode::G_CONCAT_VECTORS:
79 case TargetOpcode::G_BUILD_VECTOR:
80 case TargetOpcode::G_EXTRACT:
81 return true;
82 }
83}
84using InstListTy = GISelWorkList<256>;
85using ArtifactListTy = GISelWorkList<128>;
86
87namespace {
88class LegalizerWorkListManager : public GISelChangeObserver {
89 InstListTy &InstList;
90 ArtifactListTy &ArtifactList;
91#ifndef NDEBUG
92 SmallVector<MachineInstr *, 4> NewMIs;
93#endif
94
95public:
96 LegalizerWorkListManager(InstListTy &Insts, ArtifactListTy &Arts)
97 : InstList(Insts), ArtifactList(Arts) {}
98
99 void createdOrChangedInstr(MachineInstr &MI) {
100 // Only legalize pre-isel generic instructions.
101 // Legalization process could generate Target specific pseudo
102 // instructions with generic types. Don't record them
103 if (isPreISelGenericOpcode(MI.getOpcode())) {
104 if (isArtifact(MI))
105 ArtifactList.insert(&MI);
106 else
107 InstList.insert(&MI);
108 }
109 }
110
111 void createdInstr(MachineInstr &MI) override {
112 LLVM_DEBUG(dbgs() << ".. .. New MI: " << MI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << ".. .. New MI: " << MI
; } } while (false)
;
113 LLVM_DEBUG(NewMIs.push_back(&MI))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { NewMIs.push_back(&MI); } } while (false)
;
114 createdOrChangedInstr(MI);
115 }
116
117 void printNewInstrs() {
118 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { { for (const auto *MI : NewMIs) dbgs() <<
".. .. New MI: " << *MI; NewMIs.clear(); }; } } while (
false)
119 for (const auto *MI : NewMIs)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { { for (const auto *MI : NewMIs) dbgs() <<
".. .. New MI: " << *MI; NewMIs.clear(); }; } } while (
false)
120 dbgs() << ".. .. New MI: " << *MI;do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { { for (const auto *MI : NewMIs) dbgs() <<
".. .. New MI: " << *MI; NewMIs.clear(); }; } } while (
false)
121 NewMIs.clear();do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { { for (const auto *MI : NewMIs) dbgs() <<
".. .. New MI: " << *MI; NewMIs.clear(); }; } } while (
false)
122 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { { for (const auto *MI : NewMIs) dbgs() <<
".. .. New MI: " << *MI; NewMIs.clear(); }; } } while (
false)
;
123 }
124
125 void erasingInstr(MachineInstr &MI) override {
126 LLVM_DEBUG(dbgs() << ".. .. Erasing: " << MI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << ".. .. Erasing: " << MI
; } } while (false)
;
127 InstList.remove(&MI);
128 ArtifactList.remove(&MI);
129 }
130
131 void changingInstr(MachineInstr &MI) override {
132 LLVM_DEBUG(dbgs() << ".. .. Changing MI: " << MI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << ".. .. Changing MI: " <<
MI; } } while (false)
;
133 }
134
135 void changedInstr(MachineInstr &MI) override {
136 // When insts change, we want to revisit them to legalize them again.
137 // We'll consider them the same as created.
138 LLVM_DEBUG(dbgs() << ".. .. Changed MI: " << MI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << ".. .. Changed MI: " <<
MI; } } while (false)
;
139 createdOrChangedInstr(MI);
140 }
141};
142} // namespace
143
144bool Legalizer::runOnMachineFunction(MachineFunction &MF) {
145 // If the ISel pipeline failed, do not bother running that pass.
146 if (MF.getProperties().hasProperty(
1
Taking false branch
147 MachineFunctionProperties::Property::FailedISel))
148 return false;
149 LLVM_DEBUG(dbgs() << "Legalize Machine IR for: " << MF.getName() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << "Legalize Machine IR for: " <<
MF.getName() << '\n'; } } while (false)
;
2
Assuming 'DebugFlag' is 0
3
Loop condition is false. Exiting loop
150 init(MF);
151 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
152 GISelCSEAnalysisWrapper &Wrapper =
153 getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
154 MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
155
156 const size_t NumBlocks = MF.size();
157 MachineRegisterInfo &MRI = MF.getRegInfo();
158
159 // Populate Insts
160 InstListTy InstList;
161 ArtifactListTy ArtifactList;
162 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
163 // Perform legalization bottom up so we can DCE as we legalize.
164 // Traverse BB in RPOT and within each basic block, add insts top down,
165 // so when we pop_back_val in the legalization process, we traverse bottom-up.
166 for (auto *MBB : RPOT) {
167 if (MBB->empty())
168 continue;
169 for (MachineInstr &MI : *MBB) {
170 // Only legalize pre-isel generic instructions: others don't have types
171 // and are assumed to be legal.
172 if (!isPreISelGenericOpcode(MI.getOpcode()))
173 continue;
174 if (isArtifact(MI))
175 ArtifactList.deferred_insert(&MI);
176 else
177 InstList.deferred_insert(&MI);
178 }
179 }
180 ArtifactList.finalize();
181 InstList.finalize();
182 std::unique_ptr<MachineIRBuilder> MIRBuilder;
183 GISelCSEInfo *CSEInfo = nullptr;
184 bool EnableCSE = EnableCSEInLegalizer.getNumOccurrences()
4
Assuming the condition is false
5
'?' condition is false
185 ? EnableCSEInLegalizer
186 : TPC.isGISelCSEEnabled();
187
188 if (EnableCSE) {
6
Assuming 'EnableCSE' is 0
7
Taking false branch
189 MIRBuilder = make_unique<CSEMIRBuilder>();
190 CSEInfo = &Wrapper.get(TPC.getCSEConfig());
191 MIRBuilder->setCSEInfo(CSEInfo);
192 } else
193 MIRBuilder = make_unique<MachineIRBuilder>();
194 // This observer keeps the worklist updated.
195 LegalizerWorkListManager WorkListObserver(InstList, ArtifactList);
196 // We want both WorkListObserver as well as CSEInfo to observe all changes.
197 // Use the wrapper observer.
198 GISelObserverWrapper WrapperObserver(&WorkListObserver);
199 if (EnableCSE && CSEInfo)
200 WrapperObserver.addObserver(CSEInfo);
201 // Now install the observer as the delegate to MF.
202 // This will keep all the observers notified about new insertions/deletions.
203 RAIIDelegateInstaller DelInstall(MF, &WrapperObserver);
204 LegalizerHelper Helper(MF, WrapperObserver, *MIRBuilder.get());
205 const LegalizerInfo &LInfo(Helper.getLegalizerInfo());
206 LegalizationArtifactCombiner ArtCombiner(*MIRBuilder.get(), MF.getRegInfo(),
207 LInfo);
208 auto RemoveDeadInstFromLists = [&WrapperObserver](MachineInstr *DeadMI) {
209 WrapperObserver.erasingInstr(*DeadMI);
210 };
211 bool Changed = false;
212 do {
13
Loop condition is false. Exiting loop
213 while (!InstList.empty()) {
8
Assuming the condition is false
9
Loop condition is false. Execution continues on line 235
214 MachineInstr &MI = *InstList.pop_back_val();
215 assert(isPreISelGenericOpcode(MI.getOpcode()) && "Expecting generic opcode")((isPreISelGenericOpcode(MI.getOpcode()) && "Expecting generic opcode"
) ? static_cast<void> (0) : __assert_fail ("isPreISelGenericOpcode(MI.getOpcode()) && \"Expecting generic opcode\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 215, __PRETTY_FUNCTION__))
;
216 if (isTriviallyDead(MI, MRI)) {
217 LLVM_DEBUG(dbgs() << MI << "Is dead; erasing.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << MI << "Is dead; erasing.\n"
; } } while (false)
;
218 MI.eraseFromParentAndMarkDBGValuesForRemoval();
219 continue;
220 }
221
222 // Do the legalization for this instruction.
223 auto Res = Helper.legalizeInstrStep(MI);
224 // Error out if we couldn't legalize this instruction. We may want to
225 // fall back to DAG ISel instead in the future.
226 if (Res == LegalizerHelper::UnableToLegalize) {
227 Helper.MIRBuilder.stopObservingChanges();
228 reportGISelFailure(MF, TPC, MORE, "gisel-legalize",
229 "unable to legalize instruction", MI);
230 return false;
231 }
232 WorkListObserver.printNewInstrs();
233 Changed |= Res == LegalizerHelper::Legalized;
234 }
235 while (!ArtifactList.empty()) {
10
Assuming the condition is false
11
Loop condition is false. Execution continues on line 262
236 MachineInstr &MI = *ArtifactList.pop_back_val();
237 assert(isPreISelGenericOpcode(MI.getOpcode()) && "Expecting generic opcode")((isPreISelGenericOpcode(MI.getOpcode()) && "Expecting generic opcode"
) ? static_cast<void> (0) : __assert_fail ("isPreISelGenericOpcode(MI.getOpcode()) && \"Expecting generic opcode\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 237, __PRETTY_FUNCTION__))
;
238 if (isTriviallyDead(MI, MRI)) {
239 LLVM_DEBUG(dbgs() << MI << "Is dead\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << MI << "Is dead\n"; } }
while (false)
;
240 RemoveDeadInstFromLists(&MI);
241 MI.eraseFromParentAndMarkDBGValuesForRemoval();
242 continue;
243 }
244 SmallVector<MachineInstr *, 4> DeadInstructions;
245 if (ArtCombiner.tryCombineInstruction(MI, DeadInstructions,
246 WrapperObserver)) {
247 WorkListObserver.printNewInstrs();
248 for (auto *DeadMI : DeadInstructions) {
249 LLVM_DEBUG(dbgs() << *DeadMI << "Is dead\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << *DeadMI << "Is dead\n"
; } } while (false)
;
250 RemoveDeadInstFromLists(DeadMI);
251 DeadMI->eraseFromParentAndMarkDBGValuesForRemoval();
252 }
253 Changed = true;
254 continue;
255 }
256 // If this was not an artifact (that could be combined away), this might
257 // need special handling. Add it to InstList, so when it's processed
258 // there, it has to be legal or specially handled.
259 else
260 InstList.insert(&MI);
261 }
262 } while (!InstList.empty());
12
Assuming the condition is false
263
264 // For now don't support if new blocks are inserted - we would need to fix the
265 // outerloop for that.
266 if (MF.size() != NumBlocks) {
14
Assuming the condition is true
15
Taking true branch
267 MachineOptimizationRemarkMissed R("gisel-legalize", "GISelFailure",
17
Calling constructor for 'MachineOptimizationRemarkMissed'
268 MF.getFunction().getSubprogram(),
269 /*MBB=*/nullptr);
16
Passing null pointer value via 4th parameter 'MBB'
270 R << "inserting blocks is not supported yet";
271 reportGISelFailure(MF, TPC, MORE, R);
272 return false;
273 }
274
275 return Changed;
276}

/build/llvm-toolchain-snapshot-9~svn361465/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h

1///===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*----===//
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/// \file
9/// Optimization diagnostic interfaces for machine passes. It's packaged as an
10/// analysis pass so that by using this service passes become dependent on MBFI
11/// as well. MBFI is used to compute the "hotness" of the diagnostic message.
12///
13///===---------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_MACHINEOPTIMIZATIONREMARKEMITTER_H
16#define LLVM_CODEGEN_MACHINEOPTIMIZATIONREMARKEMITTER_H
17
18#include "llvm/Analysis/OptimizationRemarkEmitter.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
20
21namespace llvm {
22class MachineBasicBlock;
23class MachineBlockFrequencyInfo;
24class MachineInstr;
25
26/// Common features for diagnostics dealing with optimization remarks
27/// that are used by machine passes.
28class DiagnosticInfoMIROptimization : public DiagnosticInfoOptimizationBase {
29public:
30 DiagnosticInfoMIROptimization(enum DiagnosticKind Kind, const char *PassName,
31 StringRef RemarkName,
32 const DiagnosticLocation &Loc,
33 const MachineBasicBlock *MBB)
34 : DiagnosticInfoOptimizationBase(Kind, DS_Remark, PassName, RemarkName,
35 MBB->getParent()->getFunction(), Loc),
20
Called C++ object pointer is null
36 MBB(MBB) {}
37
38 /// MI-specific kinds of diagnostic Arguments.
39 struct MachineArgument : public DiagnosticInfoOptimizationBase::Argument {
40 /// Print an entire MachineInstr.
41 MachineArgument(StringRef Key, const MachineInstr &MI);
42 };
43
44 static bool classof(const DiagnosticInfo *DI) {
45 return DI->getKind() >= DK_FirstMachineRemark &&
46 DI->getKind() <= DK_LastMachineRemark;
47 }
48
49 const MachineBasicBlock *getBlock() const { return MBB; }
50
51private:
52 const MachineBasicBlock *MBB;
53};
54
55/// Diagnostic information for applied optimization remarks.
56class MachineOptimizationRemark : public DiagnosticInfoMIROptimization {
57public:
58 /// \p PassName is the name of the pass emitting this diagnostic. If this name
59 /// matches the regular expression given in -Rpass=, then the diagnostic will
60 /// be emitted. \p RemarkName is a textual identifier for the remark. \p
61 /// Loc is the debug location and \p MBB is the block that the optimization
62 /// operates in.
63 MachineOptimizationRemark(const char *PassName, StringRef RemarkName,
64 const DiagnosticLocation &Loc,
65 const MachineBasicBlock *MBB)
66 : DiagnosticInfoMIROptimization(DK_MachineOptimizationRemark, PassName,
67 RemarkName, Loc, MBB) {}
68
69 static bool classof(const DiagnosticInfo *DI) {
70 return DI->getKind() == DK_MachineOptimizationRemark;
71 }
72
73 /// \see DiagnosticInfoOptimizationBase::isEnabled.
74 bool isEnabled() const override {
75 const Function &Fn = getFunction();
76 LLVMContext &Ctx = Fn.getContext();
77 return Ctx.getDiagHandlerPtr()->isPassedOptRemarkEnabled(getPassName());
78 }
79};
80
81/// Diagnostic information for missed-optimization remarks.
82class MachineOptimizationRemarkMissed : public DiagnosticInfoMIROptimization {
83public:
84 /// \p PassName is the name of the pass emitting this diagnostic. If this name
85 /// matches the regular expression given in -Rpass-missed=, then the
86 /// diagnostic will be emitted. \p RemarkName is a textual identifier for the
87 /// remark. \p Loc is the debug location and \p MBB is the block that the
88 /// optimization operates in.
89 MachineOptimizationRemarkMissed(const char *PassName, StringRef RemarkName,
90 const DiagnosticLocation &Loc,
91 const MachineBasicBlock *MBB)
92 : DiagnosticInfoMIROptimization(DK_MachineOptimizationRemarkMissed,
19
Calling constructor for 'DiagnosticInfoMIROptimization'
93 PassName, RemarkName, Loc, MBB) {}
18
Passing null pointer value via 5th parameter 'MBB'
94
95 static bool classof(const DiagnosticInfo *DI) {
96 return DI->getKind() == DK_MachineOptimizationRemarkMissed;
97 }
98
99 /// \see DiagnosticInfoOptimizationBase::isEnabled.
100 bool isEnabled() const override {
101 const Function &Fn = getFunction();
102 LLVMContext &Ctx = Fn.getContext();
103 return Ctx.getDiagHandlerPtr()->isMissedOptRemarkEnabled(getPassName());
104 }
105};
106
107/// Diagnostic information for optimization analysis remarks.
108class MachineOptimizationRemarkAnalysis : public DiagnosticInfoMIROptimization {
109public:
110 /// \p PassName is the name of the pass emitting this diagnostic. If this name
111 /// matches the regular expression given in -Rpass-analysis=, then the
112 /// diagnostic will be emitted. \p RemarkName is a textual identifier for the
113 /// remark. \p Loc is the debug location and \p MBB is the block that the
114 /// optimization operates in.
115 MachineOptimizationRemarkAnalysis(const char *PassName, StringRef RemarkName,
116 const DiagnosticLocation &Loc,
117 const MachineBasicBlock *MBB)
118 : DiagnosticInfoMIROptimization(DK_MachineOptimizationRemarkAnalysis,
119 PassName, RemarkName, Loc, MBB) {}
120
121 static bool classof(const DiagnosticInfo *DI) {
122 return DI->getKind() == DK_MachineOptimizationRemarkAnalysis;
123 }
124
125 /// \see DiagnosticInfoOptimizationBase::isEnabled.
126 bool isEnabled() const override {
127 const Function &Fn = getFunction();
128 LLVMContext &Ctx = Fn.getContext();
129 return Ctx.getDiagHandlerPtr()->isAnalysisRemarkEnabled(getPassName());
130 }
131};
132
133/// Extend llvm::ore:: with MI-specific helper names.
134namespace ore {
135using MNV = DiagnosticInfoMIROptimization::MachineArgument;
136}
137
138/// The optimization diagnostic interface.
139///
140/// It allows reporting when optimizations are performed and when they are not
141/// along with the reasons for it. Hotness information of the corresponding
142/// code region can be included in the remark if DiagnosticsHotnessRequested is
143/// enabled in the LLVM context.
144class MachineOptimizationRemarkEmitter {
145public:
146 MachineOptimizationRemarkEmitter(MachineFunction &MF,
147 MachineBlockFrequencyInfo *MBFI)
148 : MF(MF), MBFI(MBFI) {}
149
150 /// Emit an optimization remark.
151 void emit(DiagnosticInfoOptimizationBase &OptDiag);
152
153 /// Whether we allow for extra compile-time budget to perform more
154 /// analysis to be more informative.
155 ///
156 /// This is useful to enable additional missed optimizations to be reported
157 /// that are normally too noisy. In this mode, we can use the extra analysis
158 /// (1) to filter trivial false positives or (2) to provide more context so
159 /// that non-trivial false positives can be quickly detected by the user.
160 bool allowExtraAnalysis(StringRef PassName) const {
161 return (
162 MF.getFunction().getContext().getRemarkStreamer() ||
163 MF.getFunction().getContext().getDiagHandlerPtr()->isAnyRemarkEnabled(
164 PassName));
165 }
166
167 /// Take a lambda that returns a remark which will be emitted. Second
168 /// argument is only used to restrict this to functions.
169 template <typename T>
170 void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
171 // Avoid building the remark unless we know there are at least *some*
172 // remarks enabled. We can't currently check whether remarks are requested
173 // for the calling pass since that requires actually building the remark.
174
175 if (MF.getFunction().getContext().getRemarkStreamer() ||
176 MF.getFunction()
177 .getContext()
178 .getDiagHandlerPtr()
179 ->isAnyRemarkEnabled()) {
180 auto R = RemarkBuilder();
181 emit((DiagnosticInfoOptimizationBase &)R);
182 }
183 }
184
185private:
186 MachineFunction &MF;
187
188 /// MBFI is only set if hotness is requested.
189 MachineBlockFrequencyInfo *MBFI;
190
191 /// Compute hotness from IR value (currently assumed to be a block) if PGO is
192 /// available.
193 Optional<uint64_t> computeHotness(const MachineBasicBlock &MBB);
194
195 /// Similar but use value from \p OptDiag and update hotness there.
196 void computeHotness(DiagnosticInfoMIROptimization &Remark);
197
198 /// Only allow verbose messages if we know we're filtering by hotness
199 /// (BFI is only set in this case).
200 bool shouldEmitVerbose() { return MBFI != nullptr; }
201};
202
203/// The analysis pass
204///
205/// Note that this pass shouldn't generally be marked as preserved by other
206/// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
207/// could be freed.
208class MachineOptimizationRemarkEmitterPass : public MachineFunctionPass {
209 std::unique_ptr<MachineOptimizationRemarkEmitter> ORE;
210
211public:
212 MachineOptimizationRemarkEmitterPass();
213
214 bool runOnMachineFunction(MachineFunction &MF) override;
215
216 void getAnalysisUsage(AnalysisUsage &AU) const override;
217
218 MachineOptimizationRemarkEmitter &getORE() {
219 assert(ORE && "pass not run yet")((ORE && "pass not run yet") ? static_cast<void>
(0) : __assert_fail ("ORE && \"pass not run yet\"", "/build/llvm-toolchain-snapshot-9~svn361465/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
, 219, __PRETTY_FUNCTION__))
;
220 return *ORE;
221 }
222
223 static char ID;
224};
225}
226
227#endif