Bug Summary

File:include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h
Warning:line 36, 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-8/lib/clang/8.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-8~svn350071/build-llvm/lib/CodeGen/GlobalISel -I /build/llvm-toolchain-snapshot-8~svn350071/lib/CodeGen/GlobalISel -I /build/llvm-toolchain-snapshot-8~svn350071/build-llvm/include -I /build/llvm-toolchain-snapshot-8~svn350071/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/8.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-8/lib/clang/8.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-8~svn350071/build-llvm/lib/CodeGen/GlobalISel -fdebug-prefix-map=/build/llvm-toolchain-snapshot-8~svn350071=. -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-2018-12-27-042839-1215-1 -x c++ /build/llvm-toolchain-snapshot-8~svn350071/lib/CodeGen/GlobalISel/Legalizer.cpp -faddrsig

/build/llvm-toolchain-snapshot-8~svn350071/lib/CodeGen/GlobalISel/Legalizer.cpp

1//===-- llvm/CodeGen/GlobalISel/Legalizer.cpp -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file This file implements the LegalizerHelper class to legalize individual
11/// instructions and the LegalizePass wrapper pass for the primary
12/// legalization.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/CodeGen/GlobalISel/Legalizer.h"
17#include "llvm/ADT/PostOrderIterator.h"
18#include "llvm/ADT/SetVector.h"
19#include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
20#include "llvm/CodeGen/GlobalISel/GISelWorkList.h"
21#include "llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h"
22#include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
23#include "llvm/CodeGen/GlobalISel/Utils.h"
24#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/TargetPassConfig.h"
27#include "llvm/CodeGen/TargetSubtargetInfo.h"
28#include "llvm/Support/Debug.h"
29
30#include <iterator>
31
32#define DEBUG_TYPE"legalizer" "legalizer"
33
34using namespace llvm;
35
36char Legalizer::ID = 0;
37INITIALIZE_PASS_BEGIN(Legalizer, DEBUG_TYPE,static void *initializeLegalizerPassOnce(PassRegistry &Registry
) {
38 "Legalize the Machine IR a function's Machine IR", false,static void *initializeLegalizerPassOnce(PassRegistry &Registry
) {
39 false)static void *initializeLegalizerPassOnce(PassRegistry &Registry
) {
40INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)initializeTargetPassConfigPass(Registry);
41INITIALIZE_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)); }
42 "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)); }
43 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)); }
44
45Legalizer::Legalizer() : MachineFunctionPass(ID) {
46 initializeLegalizerPass(*PassRegistry::getPassRegistry());
47}
48
49void Legalizer::getAnalysisUsage(AnalysisUsage &AU) const {
50 AU.addRequired<TargetPassConfig>();
51 getSelectionDAGFallbackAnalysisUsage(AU);
52 MachineFunctionPass::getAnalysisUsage(AU);
53}
54
55void Legalizer::init(MachineFunction &MF) {
56}
57
58static bool isArtifact(const MachineInstr &MI) {
59 switch (MI.getOpcode()) {
60 default:
61 return false;
62 case TargetOpcode::G_TRUNC:
63 case TargetOpcode::G_ZEXT:
64 case TargetOpcode::G_ANYEXT:
65 case TargetOpcode::G_SEXT:
66 case TargetOpcode::G_MERGE_VALUES:
67 case TargetOpcode::G_UNMERGE_VALUES:
68 case TargetOpcode::G_CONCAT_VECTORS:
69 case TargetOpcode::G_BUILD_VECTOR:
70 return true;
71 }
72}
73using InstListTy = GISelWorkList<256>;
74using ArtifactListTy = GISelWorkList<128>;
75
76class LegalizerWorkListManager : public GISelChangeObserver {
77 InstListTy &InstList;
78 ArtifactListTy &ArtifactList;
79
80public:
81 LegalizerWorkListManager(InstListTy &Insts, ArtifactListTy &Arts)
82 : InstList(Insts), ArtifactList(Arts) {}
83
84 void createdInstr(const MachineInstr &MI) override {
85 // Only legalize pre-isel generic instructions.
86 // Legalization process could generate Target specific pseudo
87 // instructions with generic types. Don't record them
88 if (isPreISelGenericOpcode(MI.getOpcode())) {
89 if (isArtifact(MI))
90 ArtifactList.insert(&MI);
91 else
92 InstList.insert(&MI);
93 }
94 LLVM_DEBUG(dbgs() << ".. .. New MI: " << MI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << ".. .. New MI: " << MI
; } } while (false)
;
95 }
96
97 void erasingInstr(const MachineInstr &MI) override {
98 LLVM_DEBUG(dbgs() << ".. .. Erasing: " << MI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << ".. .. Erasing: " << MI
; } } while (false)
;
99 InstList.remove(&MI);
100 ArtifactList.remove(&MI);
101 }
102
103 void changingInstr(const MachineInstr &MI) override {
104 LLVM_DEBUG(dbgs() << ".. .. Changing MI: " << MI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << ".. .. Changing MI: " <<
MI; } } while (false)
;
105 }
106
107 void changedInstr(const MachineInstr &MI) override {
108 // When insts change, we want to revisit them to legalize them again.
109 // We'll consider them the same as created.
110 LLVM_DEBUG(dbgs() << ".. .. Changed MI: " << MI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << ".. .. Changed MI: " <<
MI; } } while (false)
;
111 createdInstr(MI);
112 }
113};
114
115bool Legalizer::runOnMachineFunction(MachineFunction &MF) {
116 // If the ISel pipeline failed, do not bother running that pass.
117 if (MF.getProperties().hasProperty(
1
Taking false branch
118 MachineFunctionProperties::Property::FailedISel))
119 return false;
120 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
121 init(MF);
122 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
123 MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
124
125 const size_t NumBlocks = MF.size();
126 MachineRegisterInfo &MRI = MF.getRegInfo();
127
128 // Populate Insts
129 InstListTy InstList(&MF);
130 ArtifactListTy ArtifactList(&MF);
131 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
132 // Perform legalization bottom up so we can DCE as we legalize.
133 // Traverse BB in RPOT and within each basic block, add insts top down,
134 // so when we pop_back_val in the legalization process, we traverse bottom-up.
135 for (auto *MBB : RPOT) {
136 if (MBB->empty())
137 continue;
138 for (MachineInstr &MI : *MBB) {
139 // Only legalize pre-isel generic instructions: others don't have types
140 // and are assumed to be legal.
141 if (!isPreISelGenericOpcode(MI.getOpcode()))
142 continue;
143 if (isArtifact(MI))
144 ArtifactList.insert(&MI);
145 else
146 InstList.insert(&MI);
147 }
148 }
149 LegalizerWorkListManager WorkListObserver(InstList, ArtifactList);
150 LegalizerHelper Helper(MF, WorkListObserver);
151 const LegalizerInfo &LInfo(Helper.getLegalizerInfo());
152 LegalizationArtifactCombiner ArtCombiner(Helper.MIRBuilder, MF.getRegInfo(), LInfo);
153 auto RemoveDeadInstFromLists = [&WorkListObserver](MachineInstr *DeadMI) {
154 WorkListObserver.erasingInstr(*DeadMI);
155 };
156 bool Changed = false;
157 do {
9
Loop condition is false. Exiting loop
158 while (!InstList.empty()) {
4
Assuming the condition is false
5
Loop condition is false. Execution continues on line 179
159 MachineInstr &MI = *InstList.pop_back_val();
160 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-8~svn350071/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 160, __PRETTY_FUNCTION__))
;
161 if (isTriviallyDead(MI, MRI)) {
162 LLVM_DEBUG(dbgs() << MI << "Is dead; erasing.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << MI << "Is dead; erasing.\n"
; } } while (false)
;
163 MI.eraseFromParentAndMarkDBGValuesForRemoval();
164 continue;
165 }
166
167 // Do the legalization for this instruction.
168 auto Res = Helper.legalizeInstrStep(MI);
169 // Error out if we couldn't legalize this instruction. We may want to
170 // fall back to DAG ISel instead in the future.
171 if (Res == LegalizerHelper::UnableToLegalize) {
172 Helper.MIRBuilder.stopObservingChanges();
173 reportGISelFailure(MF, TPC, MORE, "gisel-legalize",
174 "unable to legalize instruction", MI);
175 return false;
176 }
177 Changed |= Res == LegalizerHelper::Legalized;
178 }
179 while (!ArtifactList.empty()) {
6
Assuming the condition is false
7
Loop condition is false. Execution continues on line 204
180 MachineInstr &MI = *ArtifactList.pop_back_val();
181 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-8~svn350071/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 181, __PRETTY_FUNCTION__))
;
182 if (isTriviallyDead(MI, MRI)) {
183 LLVM_DEBUG(dbgs() << MI << "Is dead\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << MI << "Is dead\n"; } }
while (false)
;
184 RemoveDeadInstFromLists(&MI);
185 MI.eraseFromParentAndMarkDBGValuesForRemoval();
186 continue;
187 }
188 SmallVector<MachineInstr *, 4> DeadInstructions;
189 if (ArtCombiner.tryCombineInstruction(MI, DeadInstructions)) {
190 for (auto *DeadMI : DeadInstructions) {
191 LLVM_DEBUG(dbgs() << *DeadMI << "Is dead\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << *DeadMI << "Is dead\n"
; } } while (false)
;
192 RemoveDeadInstFromLists(DeadMI);
193 DeadMI->eraseFromParentAndMarkDBGValuesForRemoval();
194 }
195 Changed = true;
196 continue;
197 }
198 // If this was not an artifact (that could be combined away), this might
199 // need special handling. Add it to InstList, so when it's processed
200 // there, it has to be legal or specially handled.
201 else
202 InstList.insert(&MI);
203 }
204 } while (!InstList.empty());
8
Assuming the condition is false
205
206 // For now don't support if new blocks are inserted - we would need to fix the
207 // outerloop for that.
208 if (MF.size() != NumBlocks) {
10
Assuming the condition is true
11
Taking true branch
209 MachineOptimizationRemarkMissed R("gisel-legalize", "GISelFailure",
13
Calling constructor for 'MachineOptimizationRemarkMissed'
210 MF.getFunction().getSubprogram(),
211 /*MBB=*/nullptr);
12
Passing null pointer value via 4th parameter 'MBB'
212 R << "inserting blocks is not supported yet";
213 reportGISelFailure(MF, TPC, MORE, R);
214 return false;
215 }
216
217 return Changed;
218}

/build/llvm-toolchain-snapshot-8~svn350071/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h

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