Bug Summary

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

Annotated Source Code

/build/llvm-toolchain-snapshot-6.0~svn320417/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/GISelWorkList.h"
20#include "llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h"
21#include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
22#include "llvm/CodeGen/GlobalISel/Utils.h"
23#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/CodeGen/TargetInstrInfo.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 MachineFunctionPass::getAnalysisUsage(AU);
52}
53
54void Legalizer::init(MachineFunction &MF) {
55}
56
57static bool isArtifact(const MachineInstr &MI) {
58 switch (MI.getOpcode()) {
59 default:
60 return false;
61 case TargetOpcode::G_TRUNC:
62 case TargetOpcode::G_ZEXT:
63 case TargetOpcode::G_ANYEXT:
64 case TargetOpcode::G_SEXT:
65 case TargetOpcode::G_MERGE_VALUES:
66 case TargetOpcode::G_UNMERGE_VALUES:
67 return true;
68 }
69}
70
71bool Legalizer::runOnMachineFunction(MachineFunction &MF) {
72 // If the ISel pipeline failed, do not bother running that pass.
73 if (MF.getProperties().hasProperty(
1
Taking false branch
74 MachineFunctionProperties::Property::FailedISel))
75 return false;
76 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)
;
77 init(MF);
78 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
79 MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
80 LegalizerHelper Helper(MF);
81
82 const size_t NumBlocks = MF.size();
83 MachineRegisterInfo &MRI = MF.getRegInfo();
84
85 // Populate Insts
86 GISelWorkList<256> InstList;
87 GISelWorkList<128> ArtifactList;
88 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
89 // Perform legalization bottom up so we can DCE as we legalize.
90 // Traverse BB in RPOT and within each basic block, add insts top down,
91 // so when we pop_back_val in the legalization process, we traverse bottom-up.
92 for (auto *MBB : RPOT) {
93 if (MBB->empty())
2
Assuming the condition is false
3
Taking false branch
10
Assuming the condition is true
11
Taking true branch
13
Assuming the condition is true
14
Taking true branch
94 continue;
12
Execution continues on line 92
15
Execution continues on line 92
95 for (MachineInstr &MI : *MBB) {
96 // Only legalize pre-isel generic instructions: others don't have types
97 // and are assumed to be legal.
98 if (!isPreISelGenericOpcode(MI.getOpcode()))
4
Taking true branch
6
Taking true branch
8
Taking true branch
99 continue;
5
Execution continues on line 95
7
Execution continues on line 95
9
Execution continues on line 95
100 if (isArtifact(MI))
101 ArtifactList.insert(&MI);
102 else
103 InstList.insert(&MI);
104 }
105 }
106 Helper.MIRBuilder.recordInsertions([&](MachineInstr *MI) {
107 // Only legalize pre-isel generic instructions.
108 // Legalization process could generate Target specific pseudo
109 // instructions with generic types. Don't record them
110 if (isPreISelGenericOpcode(MI->getOpcode())) {
111 if (isArtifact(*MI))
112 ArtifactList.insert(MI);
113 else
114 InstList.insert(MI);
115 }
116 DEBUG(dbgs() << ".. .. New MI: " << *MI;)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << ".. .. New MI: " << *MI
;; } } while (false)
;
117 });
118 const LegalizerInfo &LInfo(Helper.getLegalizerInfo());
119 LegalizationArtifactCombiner ArtCombiner(Helper.MIRBuilder, MF.getRegInfo(), LInfo);
120 auto RemoveDeadInstFromLists = [&InstList,
121 &ArtifactList](MachineInstr *DeadMI) {
122 InstList.remove(DeadMI);
123 ArtifactList.remove(DeadMI);
124 };
125 bool Changed = false;
126 do {
21
Loop condition is false. Exiting loop
127 while (!InstList.empty()) {
16
Assuming the condition is false
17
Loop condition is false. Execution continues on line 148
128 MachineInstr &MI = *InstList.pop_back_val();
129 assert(isPreISelGenericOpcode(MI.getOpcode()) && "Expecting generic opcode")(static_cast <bool> (isPreISelGenericOpcode(MI.getOpcode
()) && "Expecting generic opcode") ? void (0) : __assert_fail
("isPreISelGenericOpcode(MI.getOpcode()) && \"Expecting generic opcode\""
, "/build/llvm-toolchain-snapshot-6.0~svn320417/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 129, __extension__ __PRETTY_FUNCTION__))
;
130 if (isTriviallyDead(MI, MRI)) {
131 DEBUG(dbgs() << MI << "Is dead; erasing.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << MI << "Is dead; erasing.\n"
; } } while (false)
;
132 MI.eraseFromParentAndMarkDBGValuesForRemoval();
133 continue;
134 }
135
136 // Do the legalization for this instruction.
137 auto Res = Helper.legalizeInstrStep(MI);
138 // Error out if we couldn't legalize this instruction. We may want to
139 // fall back to DAG ISel instead in the future.
140 if (Res == LegalizerHelper::UnableToLegalize) {
141 Helper.MIRBuilder.stopRecordingInsertions();
142 reportGISelFailure(MF, TPC, MORE, "gisel-legalize",
143 "unable to legalize instruction", MI);
144 return false;
145 }
146 Changed |= Res == LegalizerHelper::Legalized;
147 }
148 while (!ArtifactList.empty()) {
18
Assuming the condition is false
19
Loop condition is false. Execution continues on line 173
149 MachineInstr &MI = *ArtifactList.pop_back_val();
150 assert(isPreISelGenericOpcode(MI.getOpcode()) && "Expecting generic opcode")(static_cast <bool> (isPreISelGenericOpcode(MI.getOpcode
()) && "Expecting generic opcode") ? void (0) : __assert_fail
("isPreISelGenericOpcode(MI.getOpcode()) && \"Expecting generic opcode\""
, "/build/llvm-toolchain-snapshot-6.0~svn320417/lib/CodeGen/GlobalISel/Legalizer.cpp"
, 150, __extension__ __PRETTY_FUNCTION__))
;
151 if (isTriviallyDead(MI, MRI)) {
152 DEBUG(dbgs() << MI << "Is dead; erasing.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << MI << "Is dead; erasing.\n"
; } } while (false)
;
153 RemoveDeadInstFromLists(&MI);
154 MI.eraseFromParentAndMarkDBGValuesForRemoval();
155 continue;
156 }
157 SmallVector<MachineInstr *, 4> DeadInstructions;
158 if (ArtCombiner.tryCombineInstruction(MI, DeadInstructions)) {
159 for (auto *DeadMI : DeadInstructions) {
160 DEBUG(dbgs() << ".. Erasing Dead Instruction " << *DeadMI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalizer")) { dbgs() << ".. Erasing Dead Instruction "
<< *DeadMI; } } while (false)
;
161 RemoveDeadInstFromLists(DeadMI);
162 DeadMI->eraseFromParentAndMarkDBGValuesForRemoval();
163 }
164 Changed = true;
165 continue;
166 }
167 // If this was not an artifact (that could be combined away), this might
168 // need special handling. Add it to InstList, so when it's processed
169 // there, it has to be legal or specially handled.
170 else
171 InstList.insert(&MI);
172 }
173 } while (!InstList.empty());
20
Assuming the condition is false
174
175 // For now don't support if new blocks are inserted - we would need to fix the
176 // outerloop for that.
177 if (MF.size() != NumBlocks) {
22
Taking true branch
178 MachineOptimizationRemarkMissed R("gisel-legalize", "GISelFailure",
24
Calling constructor for 'MachineOptimizationRemarkMissed'
179 MF.getFunction()->getSubprogram(),
180 /*MBB=*/nullptr);
23
Passing null pointer value via 4th parameter 'MBB'
181 R << "inserting blocks is not supported yet";
182 reportGISelFailure(MF, TPC, MORE, R);
183 return false;
184 }
185
186 return Changed;
187}

/build/llvm-toolchain-snapshot-6.0~svn320417/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),
27
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,
26
Calling constructor for 'DiagnosticInfoMIROptimization'
94 PassName, RemarkName, Loc, MBB) {}
25
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")(static_cast <bool> (ORE && "pass not run yet")
? void (0) : __assert_fail ("ORE && \"pass not run yet\""
, "/build/llvm-toolchain-snapshot-6.0~svn320417/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
, 216, __extension__ __PRETTY_FUNCTION__))
;
217 return *ORE;
218 }
219
220 static char ID;
221};
222}
223
224#endif