Bug Summary

File:llvm/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 -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-11/lib/clang/11.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/lib/CodeGen/GlobalISel -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/GlobalISel -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/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/local/include -internal-isystem /usr/lib/llvm-11/lib/clang/11.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++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/lib/CodeGen/GlobalISel -fdebug-prefix-map=/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-03-09-184146-41876-1 -x c++ /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/GlobalISel/Legalizer.cpp

/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/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/InitializePasses.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/Error.h"
32#include "llvm/Target/TargetMachine.h"
33
34#include <iterator>
35
36#define DEBUG_TYPE"legalizer" "legalizer"
37
38using namespace llvm;
39
40static cl::opt<bool>
41 EnableCSEInLegalizer("enable-cse-in-legalizer",
42 cl::desc("Should enable CSE in Legalizer"),
43 cl::Optional, cl::init(false));
44
45char Legalizer::ID = 0;
46INITIALIZE_PASS_BEGIN(Legalizer, DEBUG_TYPE,static void *initializeLegalizerPassOnce(PassRegistry &Registry
) {
47 "Legalize the Machine IR a function's Machine IR", false,static void *initializeLegalizerPassOnce(PassRegistry &Registry
) {
48 false)static void *initializeLegalizerPassOnce(PassRegistry &Registry
) {
49INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)initializeTargetPassConfigPass(Registry);
50INITIALIZE_PASS_DEPENDENCY(GISelCSEAnalysisWrapperPass)initializeGISelCSEAnalysisWrapperPassPass(Registry);
51INITIALIZE_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)); }
52 "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)); }
53 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)); }
54
55Legalizer::Legalizer() : MachineFunctionPass(ID) { }
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
144Legalizer::MFResult
145Legalizer::legalizeMachineFunction(MachineFunction &MF, const LegalizerInfo &LI,
146 ArrayRef<GISelChangeObserver *> AuxObservers,
147 MachineIRBuilder &MIRBuilder) {
148 MachineRegisterInfo &MRI = MF.getRegInfo();
149
150 // Populate worklists.
151 InstListTy InstList;
152 ArtifactListTy ArtifactList;
153 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
154 // Perform legalization bottom up so we can DCE as we legalize.
155 // Traverse BB in RPOT and within each basic block, add insts top down,
156 // so when we pop_back_val in the legalization process, we traverse bottom-up.
157 for (auto *MBB : RPOT) {
158 if (MBB->empty())
159 continue;
160 for (MachineInstr &MI : *MBB) {
161 // Only legalize pre-isel generic instructions: others don't have types
162 // and are assumed to be legal.
163 if (!isPreISelGenericOpcode(MI.getOpcode()))
164 continue;
165 if (isArtifact(MI))
166 ArtifactList.deferred_insert(&MI);
167 else
168 InstList.deferred_insert(&MI);
169 }
170 }
171 ArtifactList.finalize();
172 InstList.finalize();
173
174 // This observer keeps the worklists updated.
175 LegalizerWorkListManager WorkListObserver(InstList, ArtifactList);
176 // We want both WorkListObserver as well as all the auxiliary observers (e.g.
177 // CSEInfo) to observe all changes. Use the wrapper observer.
178 GISelObserverWrapper WrapperObserver(&WorkListObserver);
179 for (GISelChangeObserver *Observer : AuxObservers)
180 WrapperObserver.addObserver(Observer);
181
182 // Now install the observer as the delegate to MF.
183 // This will keep all the observers notified about new insertions/deletions.
184 RAIIMFObsDelInstaller Installer(MF, WrapperObserver);
185 LegalizerHelper Helper(MF, LI, WrapperObserver, MIRBuilder);
186 LegalizationArtifactCombiner ArtCombiner(MIRBuilder, MRI, LI);
187 auto RemoveDeadInstFromLists = [&WrapperObserver](MachineInstr *DeadMI) {
188 WrapperObserver.erasingInstr(*DeadMI);
189 };
190 bool Changed = false;
191 SmallVector<MachineInstr *, 128> RetryList;
192 do {
193 LLVM_DEBUG(dbgs() << "=== New Iteration ===\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << "=== New Iteration ===\n"; }
} while (false)
;
194 assert(RetryList.empty() && "Expected no instructions in RetryList")((RetryList.empty() && "Expected no instructions in RetryList"
) ? static_cast<void> (0) : __assert_fail ("RetryList.empty() && \"Expected no instructions in RetryList\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 194, __PRETTY_FUNCTION__))
;
195 unsigned NumArtifacts = ArtifactList.size();
196 while (!InstList.empty()) {
197 MachineInstr &MI = *InstList.pop_back_val();
198 assert(isPreISelGenericOpcode(MI.getOpcode()) &&((isPreISelGenericOpcode(MI.getOpcode()) && "Expecting generic opcode"
) ? static_cast<void> (0) : __assert_fail ("isPreISelGenericOpcode(MI.getOpcode()) && \"Expecting generic opcode\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 199, __PRETTY_FUNCTION__))
199 "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-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 199, __PRETTY_FUNCTION__))
;
200 if (isTriviallyDead(MI, MRI)) {
201 LLVM_DEBUG(dbgs() << MI << "Is dead; erasing.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << MI << "Is dead; erasing.\n"
; } } while (false)
;
202 MI.eraseFromParentAndMarkDBGValuesForRemoval();
203 continue;
204 }
205
206 // Do the legalization for this instruction.
207 auto Res = Helper.legalizeInstrStep(MI);
208 // Error out if we couldn't legalize this instruction. We may want to
209 // fall back to DAG ISel instead in the future.
210 if (Res == LegalizerHelper::UnableToLegalize) {
211 // Move illegal artifacts to RetryList instead of aborting because
212 // legalizing InstList may generate artifacts that allow
213 // ArtifactCombiner to combine away them.
214 if (isArtifact(MI)) {
215 LLVM_DEBUG(dbgs() << ".. Not legalized, moving to artifacts retry\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << ".. Not legalized, moving to artifacts retry\n"
; } } while (false)
;
216 assert(NumArtifacts == 0 &&((NumArtifacts == 0 && "Artifacts are only expected in instruction list starting the "
"second iteration, but each iteration starting second must "
"start with an empty artifacts list") ? static_cast<void>
(0) : __assert_fail ("NumArtifacts == 0 && \"Artifacts are only expected in instruction list starting the \" \"second iteration, but each iteration starting second must \" \"start with an empty artifacts list\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 219, __PRETTY_FUNCTION__))
217 "Artifacts are only expected in instruction list starting the "((NumArtifacts == 0 && "Artifacts are only expected in instruction list starting the "
"second iteration, but each iteration starting second must "
"start with an empty artifacts list") ? static_cast<void>
(0) : __assert_fail ("NumArtifacts == 0 && \"Artifacts are only expected in instruction list starting the \" \"second iteration, but each iteration starting second must \" \"start with an empty artifacts list\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 219, __PRETTY_FUNCTION__))
218 "second iteration, but each iteration starting second must "((NumArtifacts == 0 && "Artifacts are only expected in instruction list starting the "
"second iteration, but each iteration starting second must "
"start with an empty artifacts list") ? static_cast<void>
(0) : __assert_fail ("NumArtifacts == 0 && \"Artifacts are only expected in instruction list starting the \" \"second iteration, but each iteration starting second must \" \"start with an empty artifacts list\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 219, __PRETTY_FUNCTION__))
219 "start with an empty artifacts list")((NumArtifacts == 0 && "Artifacts are only expected in instruction list starting the "
"second iteration, but each iteration starting second must "
"start with an empty artifacts list") ? static_cast<void>
(0) : __assert_fail ("NumArtifacts == 0 && \"Artifacts are only expected in instruction list starting the \" \"second iteration, but each iteration starting second must \" \"start with an empty artifacts list\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 219, __PRETTY_FUNCTION__))
;
220 (void)NumArtifacts;
221 RetryList.push_back(&MI);
222 continue;
223 }
224 Helper.MIRBuilder.stopObservingChanges();
225 return {Changed, &MI};
226 }
227 WorkListObserver.printNewInstrs();
228 Changed |= Res == LegalizerHelper::Legalized;
229 }
230 // Try to combine the instructions in RetryList again if there
231 // are new artifacts. If not, stop legalizing.
232 if (!RetryList.empty()) {
233 if (!ArtifactList.empty()) {
234 while (!RetryList.empty())
235 ArtifactList.insert(RetryList.pop_back_val());
236 } else {
237 LLVM_DEBUG(dbgs() << "No new artifacts created, not retrying!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << "No new artifacts created, not retrying!\n"
; } } while (false)
;
238 Helper.MIRBuilder.stopObservingChanges();
239 return {Changed, RetryList.front()};
240 }
241 }
242 while (!ArtifactList.empty()) {
243 MachineInstr &MI = *ArtifactList.pop_back_val();
244 assert(isPreISelGenericOpcode(MI.getOpcode()) &&((isPreISelGenericOpcode(MI.getOpcode()) && "Expecting generic opcode"
) ? static_cast<void> (0) : __assert_fail ("isPreISelGenericOpcode(MI.getOpcode()) && \"Expecting generic opcode\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 245, __PRETTY_FUNCTION__))
245 "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-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 245, __PRETTY_FUNCTION__))
;
246 if (isTriviallyDead(MI, MRI)) {
247 LLVM_DEBUG(dbgs() << MI << "Is dead\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << MI << "Is dead\n"; } }
while (false)
;
248 RemoveDeadInstFromLists(&MI);
249 MI.eraseFromParentAndMarkDBGValuesForRemoval();
250 continue;
251 }
252 SmallVector<MachineInstr *, 4> DeadInstructions;
253 LLVM_DEBUG(dbgs() << "Trying to combine: " << MI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << "Trying to combine: " <<
MI; } } while (false)
;
254 if (ArtCombiner.tryCombineInstruction(MI, DeadInstructions,
255 WrapperObserver)) {
256 WorkListObserver.printNewInstrs();
257 for (auto *DeadMI : DeadInstructions) {
258 LLVM_DEBUG(dbgs() << *DeadMI << "Is dead\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << *DeadMI << "Is dead\n"
; } } while (false)
;
259 RemoveDeadInstFromLists(DeadMI);
260 DeadMI->eraseFromParentAndMarkDBGValuesForRemoval();
261 }
262 Changed = true;
263 continue;
264 }
265 // If this was not an artifact (that could be combined away), this might
266 // need special handling. Add it to InstList, so when it's processed
267 // there, it has to be legal or specially handled.
268 else {
269 LLVM_DEBUG(dbgs() << ".. Not combined, moving to instructions list\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << ".. Not combined, moving to instructions list\n"
; } } while (false)
;
270 InstList.insert(&MI);
271 }
272 }
273 } while (!InstList.empty());
274
275 return {Changed, /*FailedOn*/ nullptr};
276}
277
278bool Legalizer::runOnMachineFunction(MachineFunction &MF) {
279 // If the ISel pipeline failed, do not bother running that pass.
280 if (MF.getProperties().hasProperty(
1
Taking false branch
281 MachineFunctionProperties::Property::FailedISel))
282 return false;
283 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 false
3
Loop condition is false. Exiting loop
284 init(MF);
285 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
286 GISelCSEAnalysisWrapper &Wrapper =
287 getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
288 MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
289
290 const size_t NumBlocks = MF.size();
291
292 std::unique_ptr<MachineIRBuilder> MIRBuilder;
293 GISelCSEInfo *CSEInfo = nullptr;
294 bool EnableCSE = EnableCSEInLegalizer.getNumOccurrences()
4
Assuming the condition is false
5
'?' condition is false
295 ? EnableCSEInLegalizer
296 : TPC.isGISelCSEEnabled();
297 if (EnableCSE) {
6
Assuming 'EnableCSE' is false
7
Taking false branch
298 MIRBuilder = std::make_unique<CSEMIRBuilder>();
299 CSEInfo = &Wrapper.get(TPC.getCSEConfig());
300 MIRBuilder->setCSEInfo(CSEInfo);
301 } else
302 MIRBuilder = std::make_unique<MachineIRBuilder>();
303
304 SmallVector<GISelChangeObserver *, 1> AuxObservers;
305 if (EnableCSE
7.1
'EnableCSE' is false
7.1
'EnableCSE' is false
&& CSEInfo) {
306 // We want CSEInfo in addition to WorkListObserver to observe all changes.
307 AuxObservers.push_back(CSEInfo);
308 }
309 assert(!CSEInfo || !errorToBool(CSEInfo->verify()))((!CSEInfo || !errorToBool(CSEInfo->verify())) ? static_cast
<void> (0) : __assert_fail ("!CSEInfo || !errorToBool(CSEInfo->verify())"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 309, __PRETTY_FUNCTION__))
;
310
311 const LegalizerInfo &LI = *MF.getSubtarget().getLegalizerInfo();
312 MFResult Result = legalizeMachineFunction(MF, LI, AuxObservers, *MIRBuilder);
313
314 if (Result.FailedOn) {
8
Assuming field 'FailedOn' is null
9
Taking false branch
315 reportGISelFailure(MF, TPC, MORE, "gisel-legalize",
316 "unable to legalize instruction", *Result.FailedOn);
317 return false;
318 }
319 // For now don't support if new blocks are inserted - we would need to fix the
320 // outer loop for that.
321 if (MF.size() != NumBlocks) {
10
Assuming the condition is true
11
Taking true branch
322 MachineOptimizationRemarkMissed R("gisel-legalize", "GISelFailure",
13
Calling constructor for 'MachineOptimizationRemarkMissed'
323 MF.getFunction().getSubprogram(),
324 /*MBB=*/nullptr);
12
Passing null pointer value via 4th parameter 'MBB'
325 R << "inserting blocks is not supported yet";
326 reportGISelFailure(MF, TPC, MORE, R);
327 return false;
328 }
329 // If for some reason CSE was not enabled, make sure that we invalidate the
330 // CSEInfo object (as we currently declare that the analysis is preserved).
331 // The next time get on the wrapper is called, it will force it to recompute
332 // the analysis.
333 if (!EnableCSE)
334 Wrapper.setComputed(false);
335 return Result.Changed;
336}

/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/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),
16
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,
15
Calling constructor for 'DiagnosticInfoMIROptimization'
93 PassName, RemarkName, Loc, MBB) {}
14
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().getLLVMRemarkStreamer() ||
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().getLLVMRemarkStreamer() ||
176 MF.getFunction()
177 .getContext()
178 .getDiagHandlerPtr()
179 ->isAnyRemarkEnabled()) {
180 auto R = RemarkBuilder();
181 emit((DiagnosticInfoOptimizationBase &)R);
182 }
183 }
184
185 MachineBlockFrequencyInfo *getBFI() {
186 return MBFI;
187 }
188
189private:
190 MachineFunction &MF;
191
192 /// MBFI is only set if hotness is requested.
193 MachineBlockFrequencyInfo *MBFI;
194
195 /// Compute hotness from IR value (currently assumed to be a block) if PGO is
196 /// available.
197 Optional<uint64_t> computeHotness(const MachineBasicBlock &MBB);
198
199 /// Similar but use value from \p OptDiag and update hotness there.
200 void computeHotness(DiagnosticInfoMIROptimization &Remark);
201
202 /// Only allow verbose messages if we know we're filtering by hotness
203 /// (BFI is only set in this case).
204 bool shouldEmitVerbose() { return MBFI != nullptr; }
205};
206
207/// The analysis pass
208///
209/// Note that this pass shouldn't generally be marked as preserved by other
210/// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
211/// could be freed.
212class MachineOptimizationRemarkEmitterPass : public MachineFunctionPass {
213 std::unique_ptr<MachineOptimizationRemarkEmitter> ORE;
214
215public:
216 MachineOptimizationRemarkEmitterPass();
217
218 bool runOnMachineFunction(MachineFunction &MF) override;
219
220 void getAnalysisUsage(AnalysisUsage &AU) const override;
221
222 MachineOptimizationRemarkEmitter &getORE() {
223 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-11~++20200309111110+2c36c23f347/llvm/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
, 223, __PRETTY_FUNCTION__))
;
224 return *ORE;
225 }
226
227 static char ID;
228};
229}
230
231#endif