Bug Summary

File:lib/CodeGen/GlobalISel/InstructionSelect.cpp
Warning:line 36, column 41
Called C++ object pointer is null

Annotated Source Code

/build/llvm-toolchain-snapshot-6.0~svn318211/lib/CodeGen/GlobalISel/InstructionSelect.cpp

1//===- llvm/CodeGen/GlobalISel/InstructionSelect.cpp - InstructionSelect ---==//
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/// This file implements the InstructionSelect class.
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
14#include "llvm/ADT/PostOrderIterator.h"
15#include "llvm/ADT/ScopeExit.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
18#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
19#include "llvm/CodeGen/GlobalISel/Utils.h"
20#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/TargetPassConfig.h"
23#include "llvm/IR/Constants.h"
24#include "llvm/IR/Function.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Target/TargetSubtargetInfo.h"
29
30#define DEBUG_TYPE"instruction-select" "instruction-select"
31
32using namespace llvm;
33
34char InstructionSelect::ID = 0;
35INITIALIZE_PASS_BEGIN(InstructionSelect, DEBUG_TYPE,static void *initializeInstructionSelectPassOnce(PassRegistry
&Registry) {
36 "Select target instructions out of generic instructions",static void *initializeInstructionSelectPassOnce(PassRegistry
&Registry) {
37 false, false)static void *initializeInstructionSelectPassOnce(PassRegistry
&Registry) {
38INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)initializeTargetPassConfigPass(Registry);
39INITIALIZE_PASS_END(InstructionSelect, DEBUG_TYPE,PassInfo *PI = new PassInfo( "Select target instructions out of generic instructions"
, "instruction-select", &InstructionSelect::ID, PassInfo::
NormalCtor_t(callDefaultCtor<InstructionSelect>), false
, false); Registry.registerPass(*PI, true); return PI; } static
llvm::once_flag InitializeInstructionSelectPassFlag; void llvm
::initializeInstructionSelectPass(PassRegistry &Registry)
{ llvm::call_once(InitializeInstructionSelectPassFlag, initializeInstructionSelectPassOnce
, std::ref(Registry)); }
40 "Select target instructions out of generic instructions",PassInfo *PI = new PassInfo( "Select target instructions out of generic instructions"
, "instruction-select", &InstructionSelect::ID, PassInfo::
NormalCtor_t(callDefaultCtor<InstructionSelect>), false
, false); Registry.registerPass(*PI, true); return PI; } static
llvm::once_flag InitializeInstructionSelectPassFlag; void llvm
::initializeInstructionSelectPass(PassRegistry &Registry)
{ llvm::call_once(InitializeInstructionSelectPassFlag, initializeInstructionSelectPassOnce
, std::ref(Registry)); }
41 false, false)PassInfo *PI = new PassInfo( "Select target instructions out of generic instructions"
, "instruction-select", &InstructionSelect::ID, PassInfo::
NormalCtor_t(callDefaultCtor<InstructionSelect>), false
, false); Registry.registerPass(*PI, true); return PI; } static
llvm::once_flag InitializeInstructionSelectPassFlag; void llvm
::initializeInstructionSelectPass(PassRegistry &Registry)
{ llvm::call_once(InitializeInstructionSelectPassFlag, initializeInstructionSelectPassOnce
, std::ref(Registry)); }
42
43InstructionSelect::InstructionSelect() : MachineFunctionPass(ID) {
44 initializeInstructionSelectPass(*PassRegistry::getPassRegistry());
45}
46
47void InstructionSelect::getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.addRequired<TargetPassConfig>();
49 MachineFunctionPass::getAnalysisUsage(AU);
50}
51
52bool InstructionSelect::runOnMachineFunction(MachineFunction &MF) {
53 const MachineRegisterInfo &MRI = MF.getRegInfo();
54
55 // No matter what happens, whether we successfully select the function or not,
56 // nothing is going to use the vreg types after us. Make sure they disappear.
57 auto ClearVRegTypesOnReturn =
58 make_scope_exit([&]() { MRI.getVRegToType().clear(); });
59
60 // If the ISel pipeline failed, do not bother running that pass.
61 if (MF.getProperties().hasProperty(
1
Taking false branch
62 MachineFunctionProperties::Property::FailedISel))
63 return false;
64
65 DEBUG(dbgs() << "Selecting function: " << MF.getName() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("instruction-select")) { dbgs() << "Selecting function: "
<< MF.getName() << '\n'; } } while (false)
;
66
67 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
68 const InstructionSelector *ISel = MF.getSubtarget().getInstructionSelector();
69 assert(ISel && "Cannot work without InstructionSelector")((ISel && "Cannot work without InstructionSelector") ?
static_cast<void> (0) : __assert_fail ("ISel && \"Cannot work without InstructionSelector\""
, "/build/llvm-toolchain-snapshot-6.0~svn318211/lib/CodeGen/GlobalISel/InstructionSelect.cpp"
, 69, __PRETTY_FUNCTION__))
;
70
71 // An optimization remark emitter. Used to report failures.
72 MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
73
74 // FIXME: There are many other MF/MFI fields we need to initialize.
75
76#ifndef NDEBUG
77 // Check that our input is fully legal: we require the function to have the
78 // Legalized property, so it should be.
79 // FIXME: This should be in the MachineVerifier, but it can't use the
80 // LegalizerInfo as it's currently in the separate GlobalISel library.
81 // The RegBankSelected property is already checked in the verifier. Note
82 // that it has the same layering problem, but we only use inline methods so
83 // end up not needing to link against the GlobalISel library.
84 if (const LegalizerInfo *MLI = MF.getSubtarget().getLegalizerInfo())
2
Assuming 'MLI' is null
3
Taking false branch
85 for (MachineBasicBlock &MBB : MF)
86 for (MachineInstr &MI : MBB)
87 if (isPreISelGenericOpcode(MI.getOpcode()) && !MLI->isLegal(MI, MRI)) {
88 reportGISelFailure(MF, TPC, MORE, "gisel-select",
89 "instruction is not legal", MI);
90 return false;
91 }
92
93#endif
94 // FIXME: We could introduce new blocks and will need to fix the outer loop.
95 // Until then, keep track of the number of blocks to assert that we don't.
96 const size_t NumBlocks = MF.size();
97
98 for (MachineBasicBlock *MBB : post_order(&MF)) {
99 if (MBB->empty())
100 continue;
101
102 // Select instructions in reverse block order. We permit erasing so have
103 // to resort to manually iterating and recognizing the begin (rend) case.
104 bool ReachedBegin = false;
105 for (auto MII = std::prev(MBB->end()), Begin = MBB->begin();
106 !ReachedBegin;) {
107#ifndef NDEBUG
108 // Keep track of the insertion range for debug printing.
109 const auto AfterIt = std::next(MII);
110#endif
111 // Select this instruction.
112 MachineInstr &MI = *MII;
113
114 // And have our iterator point to the next instruction, if there is one.
115 if (MII == Begin)
116 ReachedBegin = true;
117 else
118 --MII;
119
120 DEBUG(dbgs() << "Selecting: \n " << MI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("instruction-select")) { dbgs() << "Selecting: \n " <<
MI; } } while (false)
;
121
122 // We could have folded this instruction away already, making it dead.
123 // If so, erase it.
124 if (isTriviallyDead(MI, MRI)) {
125 DEBUG(dbgs() << "Is dead; erasing.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("instruction-select")) { dbgs() << "Is dead; erasing.\n"
; } } while (false)
;
126 MI.eraseFromParentAndMarkDBGValuesForRemoval();
127 continue;
128 }
129
130 if (!ISel->select(MI)) {
131 // FIXME: It would be nice to dump all inserted instructions. It's
132 // not obvious how, esp. considering select() can insert after MI.
133 reportGISelFailure(MF, TPC, MORE, "gisel-select", "cannot select", MI);
134 return false;
135 }
136
137 // Dump the range of instructions that MI expanded into.
138 DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("instruction-select")) { { auto InsertedBegin = ReachedBegin
? MBB->begin() : std::next(MII); dbgs() << "Into:\n"
; for (auto &InsertedMI : make_range(InsertedBegin, AfterIt
)) dbgs() << " " << InsertedMI; dbgs() << '\n'
; }; } } while (false)
139 auto InsertedBegin = ReachedBegin ? MBB->begin() : std::next(MII);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("instruction-select")) { { auto InsertedBegin = ReachedBegin
? MBB->begin() : std::next(MII); dbgs() << "Into:\n"
; for (auto &InsertedMI : make_range(InsertedBegin, AfterIt
)) dbgs() << " " << InsertedMI; dbgs() << '\n'
; }; } } while (false)
140 dbgs() << "Into:\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("instruction-select")) { { auto InsertedBegin = ReachedBegin
? MBB->begin() : std::next(MII); dbgs() << "Into:\n"
; for (auto &InsertedMI : make_range(InsertedBegin, AfterIt
)) dbgs() << " " << InsertedMI; dbgs() << '\n'
; }; } } while (false)
141 for (auto &InsertedMI : make_range(InsertedBegin, AfterIt))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("instruction-select")) { { auto InsertedBegin = ReachedBegin
? MBB->begin() : std::next(MII); dbgs() << "Into:\n"
; for (auto &InsertedMI : make_range(InsertedBegin, AfterIt
)) dbgs() << " " << InsertedMI; dbgs() << '\n'
; }; } } while (false)
142 dbgs() << " " << InsertedMI;do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("instruction-select")) { { auto InsertedBegin = ReachedBegin
? MBB->begin() : std::next(MII); dbgs() << "Into:\n"
; for (auto &InsertedMI : make_range(InsertedBegin, AfterIt
)) dbgs() << " " << InsertedMI; dbgs() << '\n'
; }; } } while (false)
143 dbgs() << '\n';do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("instruction-select")) { { auto InsertedBegin = ReachedBegin
? MBB->begin() : std::next(MII); dbgs() << "Into:\n"
; for (auto &InsertedMI : make_range(InsertedBegin, AfterIt
)) dbgs() << " " << InsertedMI; dbgs() << '\n'
; }; } } while (false)
144 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("instruction-select")) { { auto InsertedBegin = ReachedBegin
? MBB->begin() : std::next(MII); dbgs() << "Into:\n"
; for (auto &InsertedMI : make_range(InsertedBegin, AfterIt
)) dbgs() << " " << InsertedMI; dbgs() << '\n'
; }; } } while (false)
;
145 }
146 }
147
148 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
149
150 // Now that selection is complete, there are no more generic vregs. Verify
151 // that the size of the now-constrained vreg is unchanged and that it has a
152 // register class.
153 for (auto &VRegToType : MRI.getVRegToType()) {
154 unsigned VReg = VRegToType.first;
155 auto *RC = MRI.getRegClassOrNull(VReg);
156 MachineInstr *MI = nullptr;
157 if (!MRI.def_empty(VReg))
158 MI = &*MRI.def_instr_begin(VReg);
159 else if (!MRI.use_empty(VReg))
160 MI = &*MRI.use_instr_begin(VReg);
161
162 if (MI && !RC) {
163 reportGISelFailure(MF, TPC, MORE, "gisel-select",
164 "VReg has no regclass after selection", *MI);
165 return false;
166 } else if (!RC)
167 continue;
168
169 if (VRegToType.second.isValid() &&
170 VRegToType.second.getSizeInBits() > TRI.getRegSizeInBits(*RC)) {
171 reportGISelFailure(MF, TPC, MORE, "gisel-select",
172 "VReg has explicit size different from class size",
173 *MI);
174 return false;
175 }
176 }
177
178 if (MF.size() != NumBlocks) {
4
Taking true branch
179 MachineOptimizationRemarkMissed R("gisel-select", "GISelFailure",
6
Calling constructor for 'MachineOptimizationRemarkMissed'
180 MF.getFunction()->getSubprogram(),
181 /*MBB=*/nullptr);
5
Passing null pointer value via 4th parameter 'MBB'
182 R << "inserting blocks is not supported yet";
183 reportGISelFailure(MF, TPC, MORE, R);
184 return false;
185 }
186
187 auto &TLI = *MF.getSubtarget().getTargetLowering();
188 TLI.finalizeLowering(MF);
189
190 // FIXME: Should we accurately track changes?
191 return true;
192}

/build/llvm-toolchain-snapshot-6.0~svn318211/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/// \brief 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),
9
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,
8
Calling constructor for 'DiagnosticInfoMIROptimization'
94 PassName, RemarkName, Loc, MBB) {}
7
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 /// \brief 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 /// \brief 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 /// \brief 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-6.0~svn318211/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
, 216, __PRETTY_FUNCTION__))
;
217 return *ORE;
218 }
219
220 static char ID;
221};
222}
223
224#endif