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-10/lib/clang/10.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/lib/CodeGen/GlobalISel -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/CodeGen/GlobalISel -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/include -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/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-10/lib/clang/10.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-10~++20200112100611+7fa5290d5bd/build-llvm/lib/CodeGen/GlobalISel -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd=. -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-01-13-084841-49055-1 -x c++ /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/CodeGen/GlobalISel/Legalizer.cpp

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

/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/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().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
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-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
, 223, __PRETTY_FUNCTION__))
;
224 return *ORE;
225 }
226
227 static char ID;
228};
229}
230
231#endif