Bug Summary

File:build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/lib/CodeGen/MachineVerifier.cpp
Warning:line 2384, column 9
Forming reference to null pointer

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name MachineVerifier.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 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm -resource-dir /usr/lib/llvm-15/lib/clang/15.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/CodeGen -I /build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/lib/CodeGen -I include -I /build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-15/lib/clang/15.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-04-20-140412-16051-1 -x c++ /build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/lib/CodeGen/MachineVerifier.cpp
1//===- MachineVerifier.cpp - Machine Code Verifier ------------------------===//
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// Pass to verify generated machine code. The following is checked:
10//
11// Operand counts: All explicit operands must be present.
12//
13// Register classes: All physical and virtual register operands must be
14// compatible with the register class required by the instruction descriptor.
15//
16// Register live intervals: Registers must be defined only once, and must be
17// defined before use.
18//
19// The machine code verifier is enabled with the command-line option
20// -verify-machineinstrs.
21//===----------------------------------------------------------------------===//
22
23#include "llvm/ADT/BitVector.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/DenseSet.h"
26#include "llvm/ADT/DepthFirstIterator.h"
27#include "llvm/ADT/PostOrderIterator.h"
28#include "llvm/ADT/STLExtras.h"
29#include "llvm/ADT/SetOperations.h"
30#include "llvm/ADT/SmallPtrSet.h"
31#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/StringRef.h"
33#include "llvm/ADT/Twine.h"
34#include "llvm/Analysis/EHPersonalities.h"
35#include "llvm/CodeGen/LiveInterval.h"
36#include "llvm/CodeGen/LiveIntervals.h"
37#include "llvm/CodeGen/LiveRangeCalc.h"
38#include "llvm/CodeGen/LiveStacks.h"
39#include "llvm/CodeGen/LiveVariables.h"
40#include "llvm/CodeGen/MachineBasicBlock.h"
41#include "llvm/CodeGen/MachineFrameInfo.h"
42#include "llvm/CodeGen/MachineFunction.h"
43#include "llvm/CodeGen/MachineFunctionPass.h"
44#include "llvm/CodeGen/MachineInstr.h"
45#include "llvm/CodeGen/MachineInstrBundle.h"
46#include "llvm/CodeGen/MachineMemOperand.h"
47#include "llvm/CodeGen/MachineOperand.h"
48#include "llvm/CodeGen/MachineRegisterInfo.h"
49#include "llvm/CodeGen/PseudoSourceValue.h"
50#include "llvm/CodeGen/RegisterBank.h"
51#include "llvm/CodeGen/SlotIndexes.h"
52#include "llvm/CodeGen/StackMaps.h"
53#include "llvm/CodeGen/TargetInstrInfo.h"
54#include "llvm/CodeGen/TargetOpcodes.h"
55#include "llvm/CodeGen/TargetRegisterInfo.h"
56#include "llvm/CodeGen/TargetSubtargetInfo.h"
57#include "llvm/IR/BasicBlock.h"
58#include "llvm/IR/Constants.h"
59#include "llvm/IR/Function.h"
60#include "llvm/IR/InlineAsm.h"
61#include "llvm/IR/Instructions.h"
62#include "llvm/InitializePasses.h"
63#include "llvm/MC/LaneBitmask.h"
64#include "llvm/MC/MCAsmInfo.h"
65#include "llvm/MC/MCInstrDesc.h"
66#include "llvm/MC/MCRegisterInfo.h"
67#include "llvm/MC/MCTargetOptions.h"
68#include "llvm/Pass.h"
69#include "llvm/Support/Casting.h"
70#include "llvm/Support/ErrorHandling.h"
71#include "llvm/Support/LowLevelTypeImpl.h"
72#include "llvm/Support/MathExtras.h"
73#include "llvm/Support/raw_ostream.h"
74#include "llvm/Target/TargetMachine.h"
75#include <algorithm>
76#include <cassert>
77#include <cstddef>
78#include <cstdint>
79#include <iterator>
80#include <string>
81#include <utility>
82
83using namespace llvm;
84
85namespace {
86
87 struct MachineVerifier {
88 MachineVerifier(Pass *pass, const char *b) : PASS(pass), Banner(b) {}
89
90 unsigned verify(const MachineFunction &MF);
91
92 Pass *const PASS;
93 const char *Banner;
94 const MachineFunction *MF;
95 const TargetMachine *TM;
96 const TargetInstrInfo *TII;
97 const TargetRegisterInfo *TRI;
98 const MachineRegisterInfo *MRI;
99
100 unsigned foundErrors;
101
102 // Avoid querying the MachineFunctionProperties for each operand.
103 bool isFunctionRegBankSelected;
104 bool isFunctionSelected;
105 bool isFunctionTracksDebugUserValues;
106
107 using RegVector = SmallVector<Register, 16>;
108 using RegMaskVector = SmallVector<const uint32_t *, 4>;
109 using RegSet = DenseSet<Register>;
110 using RegMap = DenseMap<Register, const MachineInstr *>;
111 using BlockSet = SmallPtrSet<const MachineBasicBlock *, 8>;
112
113 const MachineInstr *FirstNonPHI;
114 const MachineInstr *FirstTerminator;
115 BlockSet FunctionBlocks;
116
117 BitVector regsReserved;
118 RegSet regsLive;
119 RegVector regsDefined, regsDead, regsKilled;
120 RegMaskVector regMasks;
121
122 SlotIndex lastIndex;
123
124 // Add Reg and any sub-registers to RV
125 void addRegWithSubRegs(RegVector &RV, Register Reg) {
126 RV.push_back(Reg);
21
Value assigned to field 'LiveInts', which participates in a condition later
127 if (Reg.isPhysical())
22
Taking false branch
23
Taking false branch
128 append_range(RV, TRI->subregs(Reg.asMCReg()));
129 }
130
131 struct BBInfo {
132 // Is this MBB reachable from the MF entry point?
133 bool reachable = false;
134
135 // Vregs that must be live in because they are used without being
136 // defined. Map value is the user. vregsLiveIn doesn't include regs
137 // that only are used by PHI nodes.
138 RegMap vregsLiveIn;
139
140 // Regs killed in MBB. They may be defined again, and will then be in both
141 // regsKilled and regsLiveOut.
142 RegSet regsKilled;
143
144 // Regs defined in MBB and live out. Note that vregs passing through may
145 // be live out without being mentioned here.
146 RegSet regsLiveOut;
147
148 // Vregs that pass through MBB untouched. This set is disjoint from
149 // regsKilled and regsLiveOut.
150 RegSet vregsPassed;
151
152 // Vregs that must pass through MBB because they are needed by a successor
153 // block. This set is disjoint from regsLiveOut.
154 RegSet vregsRequired;
155
156 // Set versions of block's predecessor and successor lists.
157 BlockSet Preds, Succs;
158
159 BBInfo() = default;
160
161 // Add register to vregsRequired if it belongs there. Return true if
162 // anything changed.
163 bool addRequired(Register Reg) {
164 if (!Reg.isVirtual())
165 return false;
166 if (regsLiveOut.count(Reg))
167 return false;
168 return vregsRequired.insert(Reg).second;
169 }
170
171 // Same for a full set.
172 bool addRequired(const RegSet &RS) {
173 bool Changed = false;
174 for (Register Reg : RS)
175 Changed |= addRequired(Reg);
176 return Changed;
177 }
178
179 // Same for a full map.
180 bool addRequired(const RegMap &RM) {
181 bool Changed = false;
182 for (const auto &I : RM)
183 Changed |= addRequired(I.first);
184 return Changed;
185 }
186
187 // Live-out registers are either in regsLiveOut or vregsPassed.
188 bool isLiveOut(Register Reg) const {
189 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
190 }
191 };
192
193 // Extra register info per MBB.
194 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
195
196 bool isReserved(Register Reg) {
197 return Reg.id() < regsReserved.size() && regsReserved.test(Reg.id());
198 }
199
200 bool isAllocatable(Register Reg) const {
201 return Reg.id() < TRI->getNumRegs() && TRI->isInAllocatableClass(Reg) &&
202 !regsReserved.test(Reg.id());
203 }
204
205 // Analysis information if available
206 LiveVariables *LiveVars;
207 LiveIntervals *LiveInts;
208 LiveStacks *LiveStks;
209 SlotIndexes *Indexes;
210
211 void visitMachineFunctionBefore();
212 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
213 void visitMachineBundleBefore(const MachineInstr *MI);
214
215 /// Verify that all of \p MI's virtual register operands are scalars.
216 /// \returns True if all virtual register operands are scalar. False
217 /// otherwise.
218 bool verifyAllRegOpsScalar(const MachineInstr &MI,
219 const MachineRegisterInfo &MRI);
220 bool verifyVectorElementMatch(LLT Ty0, LLT Ty1, const MachineInstr *MI);
221 void verifyPreISelGenericInstruction(const MachineInstr *MI);
222 void visitMachineInstrBefore(const MachineInstr *MI);
223 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
224 void visitMachineBundleAfter(const MachineInstr *MI);
225 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
226 void visitMachineFunctionAfter();
227
228 void report(const char *msg, const MachineFunction *MF);
229 void report(const char *msg, const MachineBasicBlock *MBB);
230 void report(const char *msg, const MachineInstr *MI);
231 void report(const char *msg, const MachineOperand *MO, unsigned MONum,
232 LLT MOVRegType = LLT{});
233 void report(const Twine &Msg, const MachineInstr *MI);
234
235 void report_context(const LiveInterval &LI) const;
236 void report_context(const LiveRange &LR, Register VRegUnit,
237 LaneBitmask LaneMask) const;
238 void report_context(const LiveRange::Segment &S) const;
239 void report_context(const VNInfo &VNI) const;
240 void report_context(SlotIndex Pos) const;
241 void report_context(MCPhysReg PhysReg) const;
242 void report_context_liverange(const LiveRange &LR) const;
243 void report_context_lanemask(LaneBitmask LaneMask) const;
244 void report_context_vreg(Register VReg) const;
245 void report_context_vreg_regunit(Register VRegOrUnit) const;
246
247 void verifyInlineAsm(const MachineInstr *MI);
248
249 void checkLiveness(const MachineOperand *MO, unsigned MONum);
250 void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
251 SlotIndex UseIdx, const LiveRange &LR,
252 Register VRegOrUnit,
253 LaneBitmask LaneMask = LaneBitmask::getNone());
254 void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
255 SlotIndex DefIdx, const LiveRange &LR,
256 Register VRegOrUnit, bool SubRangeCheck = false,
257 LaneBitmask LaneMask = LaneBitmask::getNone());
258
259 void markReachable(const MachineBasicBlock *MBB);
260 void calcRegsPassed();
261 void checkPHIOps(const MachineBasicBlock &MBB);
262
263 void calcRegsRequired();
264 void verifyLiveVariables();
265 void verifyLiveIntervals();
266 void verifyLiveInterval(const LiveInterval&);
267 void verifyLiveRangeValue(const LiveRange &, const VNInfo *, Register,
268 LaneBitmask);
269 void verifyLiveRangeSegment(const LiveRange &,
270 const LiveRange::const_iterator I, Register,
271 LaneBitmask);
272 void verifyLiveRange(const LiveRange &, Register,
273 LaneBitmask LaneMask = LaneBitmask::getNone());
274
275 void verifyStackFrame();
276
277 void verifySlotIndexes() const;
278 void verifyProperties(const MachineFunction &MF);
279 };
280
281 struct MachineVerifierPass : public MachineFunctionPass {
282 static char ID; // Pass ID, replacement for typeid
283
284 const std::string Banner;
285
286 MachineVerifierPass(std::string banner = std::string())
287 : MachineFunctionPass(ID), Banner(std::move(banner)) {
288 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
289 }
290
291 void getAnalysisUsage(AnalysisUsage &AU) const override {
292 AU.setPreservesAll();
293 MachineFunctionPass::getAnalysisUsage(AU);
294 }
295
296 bool runOnMachineFunction(MachineFunction &MF) override {
297 // Skip functions that have known verification problems.
298 // FIXME: Remove this mechanism when all problematic passes have been
299 // fixed.
300 if (MF.getProperties().hasProperty(
301 MachineFunctionProperties::Property::FailsVerification))
302 return false;
303
304 unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
305 if (FoundErrors)
306 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
307 return false;
308 }
309 };
310
311} // end anonymous namespace
312
313char MachineVerifierPass::ID = 0;
314
315INITIALIZE_PASS(MachineVerifierPass, "machineverifier",static void *initializeMachineVerifierPassPassOnce(PassRegistry
&Registry) { PassInfo *PI = new PassInfo( "Verify generated machine code"
, "machineverifier", &MachineVerifierPass::ID, PassInfo::
NormalCtor_t(callDefaultCtor<MachineVerifierPass>), false
, false); Registry.registerPass(*PI, true); return PI; } static
llvm::once_flag InitializeMachineVerifierPassPassFlag; void llvm
::initializeMachineVerifierPassPass(PassRegistry &Registry
) { llvm::call_once(InitializeMachineVerifierPassPassFlag, initializeMachineVerifierPassPassOnce
, std::ref(Registry)); }
316 "Verify generated machine code", false, false)static void *initializeMachineVerifierPassPassOnce(PassRegistry
&Registry) { PassInfo *PI = new PassInfo( "Verify generated machine code"
, "machineverifier", &MachineVerifierPass::ID, PassInfo::
NormalCtor_t(callDefaultCtor<MachineVerifierPass>), false
, false); Registry.registerPass(*PI, true); return PI; } static
llvm::once_flag InitializeMachineVerifierPassPassFlag; void llvm
::initializeMachineVerifierPassPass(PassRegistry &Registry
) { llvm::call_once(InitializeMachineVerifierPassPassFlag, initializeMachineVerifierPassPassOnce
, std::ref(Registry)); }
317
318FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
319 return new MachineVerifierPass(Banner);
320}
321
322void llvm::verifyMachineFunction(MachineFunctionAnalysisManager *,
323 const std::string &Banner,
324 const MachineFunction &MF) {
325 // TODO: Use MFAM after porting below analyses.
326 // LiveVariables *LiveVars;
327 // LiveIntervals *LiveInts;
328 // LiveStacks *LiveStks;
329 // SlotIndexes *Indexes;
330 unsigned FoundErrors = MachineVerifier(nullptr, Banner.c_str()).verify(MF);
331 if (FoundErrors)
332 report_fatal_error("Found " + Twine(FoundErrors) + " machine code errors.");
333}
334
335bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
336 const {
337 MachineFunction &MF = const_cast<MachineFunction&>(*this);
338 unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
339 if (AbortOnErrors && FoundErrors)
340 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
341 return FoundErrors == 0;
342}
343
344void MachineVerifier::verifySlotIndexes() const {
345 if (Indexes == nullptr)
346 return;
347
348 // Ensure the IdxMBB list is sorted by slot indexes.
349 SlotIndex Last;
350 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
351 E = Indexes->MBBIndexEnd(); I != E; ++I) {
352 assert(!Last.isValid() || I->first > Last)(static_cast <bool> (!Last.isValid() || I->first >
Last) ? void (0) : __assert_fail ("!Last.isValid() || I->first > Last"
, "llvm/lib/CodeGen/MachineVerifier.cpp", 352, __extension__ __PRETTY_FUNCTION__
))
;
353 Last = I->first;
354 }
355}
356
357void MachineVerifier::verifyProperties(const MachineFunction &MF) {
358 // If a pass has introduced virtual registers without clearing the
359 // NoVRegs property (or set it without allocating the vregs)
360 // then report an error.
361 if (MF.getProperties().hasProperty(
362 MachineFunctionProperties::Property::NoVRegs) &&
363 MRI->getNumVirtRegs())
364 report("Function has NoVRegs property but there are VReg operands", &MF);
365}
366
367unsigned MachineVerifier::verify(const MachineFunction &MF) {
368 foundErrors = 0;
369
370 this->MF = &MF;
371 TM = &MF.getTarget();
372 TII = MF.getSubtarget().getInstrInfo();
373 TRI = MF.getSubtarget().getRegisterInfo();
374 MRI = &MF.getRegInfo();
375
376 const bool isFunctionFailedISel = MF.getProperties().hasProperty(
377 MachineFunctionProperties::Property::FailedISel);
378
379 // If we're mid-GlobalISel and we already triggered the fallback path then
380 // it's expected that the MIR is somewhat broken but that's ok since we'll
381 // reset it and clear the FailedISel attribute in ResetMachineFunctions.
382 if (isFunctionFailedISel)
383 return foundErrors;
384
385 isFunctionRegBankSelected = MF.getProperties().hasProperty(
386 MachineFunctionProperties::Property::RegBankSelected);
387 isFunctionSelected = MF.getProperties().hasProperty(
388 MachineFunctionProperties::Property::Selected);
389 isFunctionTracksDebugUserValues = MF.getProperties().hasProperty(
390 MachineFunctionProperties::Property::TracksDebugUserValues);
391
392 LiveVars = nullptr;
393 LiveInts = nullptr;
394 LiveStks = nullptr;
395 Indexes = nullptr;
396 if (PASS) {
397 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
398 // We don't want to verify LiveVariables if LiveIntervals is available.
399 if (!LiveInts)
400 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
401 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
402 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
403 }
404
405 verifySlotIndexes();
406
407 verifyProperties(MF);
408
409 visitMachineFunctionBefore();
410 for (const MachineBasicBlock &MBB : MF) {
411 visitMachineBasicBlockBefore(&MBB);
412 // Keep track of the current bundle header.
413 const MachineInstr *CurBundle = nullptr;
414 // Do we expect the next instruction to be part of the same bundle?
415 bool InBundle = false;
416
417 for (const MachineInstr &MI : MBB.instrs()) {
418 if (MI.getParent() != &MBB) {
419 report("Bad instruction parent pointer", &MBB);
420 errs() << "Instruction: " << MI;
421 continue;
422 }
423
424 // Check for consistent bundle flags.
425 if (InBundle && !MI.isBundledWithPred())
426 report("Missing BundledPred flag, "
427 "BundledSucc was set on predecessor",
428 &MI);
429 if (!InBundle && MI.isBundledWithPred())
430 report("BundledPred flag is set, "
431 "but BundledSucc not set on predecessor",
432 &MI);
433
434 // Is this a bundle header?
435 if (!MI.isInsideBundle()) {
436 if (CurBundle)
437 visitMachineBundleAfter(CurBundle);
438 CurBundle = &MI;
439 visitMachineBundleBefore(CurBundle);
440 } else if (!CurBundle)
441 report("No bundle header", &MI);
442 visitMachineInstrBefore(&MI);
443 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
444 const MachineOperand &Op = MI.getOperand(I);
445 if (Op.getParent() != &MI) {
446 // Make sure to use correct addOperand / removeOperand / ChangeTo
447 // functions when replacing operands of a MachineInstr.
448 report("Instruction has operand with wrong parent set", &MI);
449 }
450
451 visitMachineOperand(&Op, I);
452 }
453
454 // Was this the last bundled instruction?
455 InBundle = MI.isBundledWithSucc();
456 }
457 if (CurBundle)
458 visitMachineBundleAfter(CurBundle);
459 if (InBundle)
460 report("BundledSucc flag set on last instruction in block", &MBB.back());
461 visitMachineBasicBlockAfter(&MBB);
462 }
463 visitMachineFunctionAfter();
464
465 // Clean up.
466 regsLive.clear();
467 regsDefined.clear();
468 regsDead.clear();
469 regsKilled.clear();
470 regMasks.clear();
471 MBBInfoMap.clear();
472
473 return foundErrors;
474}
475
476void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
477 assert(MF)(static_cast <bool> (MF) ? void (0) : __assert_fail ("MF"
, "llvm/lib/CodeGen/MachineVerifier.cpp", 477, __extension__ __PRETTY_FUNCTION__
))
;
478 errs() << '\n';
479 if (!foundErrors++) {
480 if (Banner)
481 errs() << "# " << Banner << '\n';
482 if (LiveInts != nullptr)
483 LiveInts->print(errs());
484 else
485 MF->print(errs(), Indexes);
486 }
487 errs() << "*** Bad machine code: " << msg << " ***\n"
488 << "- function: " << MF->getName() << "\n";
489}
490
491void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
492 assert(MBB)(static_cast <bool> (MBB) ? void (0) : __assert_fail ("MBB"
, "llvm/lib/CodeGen/MachineVerifier.cpp", 492, __extension__ __PRETTY_FUNCTION__
))
;
493 report(msg, MBB->getParent());
494 errs() << "- basic block: " << printMBBReference(*MBB) << ' '
495 << MBB->getName() << " (" << (const void *)MBB << ')';
496 if (Indexes)
497 errs() << " [" << Indexes->getMBBStartIdx(MBB)
498 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
499 errs() << '\n';
500}
501
502void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
503 assert(MI)(static_cast <bool> (MI) ? void (0) : __assert_fail ("MI"
, "llvm/lib/CodeGen/MachineVerifier.cpp", 503, __extension__ __PRETTY_FUNCTION__
))
;
504 report(msg, MI->getParent());
505 errs() << "- instruction: ";
506 if (Indexes && Indexes->hasIndex(*MI))
507 errs() << Indexes->getInstructionIndex(*MI) << '\t';
508 MI->print(errs(), /*IsStandalone=*/true);
509}
510
511void MachineVerifier::report(const char *msg, const MachineOperand *MO,
512 unsigned MONum, LLT MOVRegType) {
513 assert(MO)(static_cast <bool> (MO) ? void (0) : __assert_fail ("MO"
, "llvm/lib/CodeGen/MachineVerifier.cpp", 513, __extension__ __PRETTY_FUNCTION__
))
;
514 report(msg, MO->getParent());
515 errs() << "- operand " << MONum << ": ";
516 MO->print(errs(), MOVRegType, TRI);
517 errs() << "\n";
518}
519
520void MachineVerifier::report(const Twine &Msg, const MachineInstr *MI) {
521 report(Msg.str().c_str(), MI);
522}
523
524void MachineVerifier::report_context(SlotIndex Pos) const {
525 errs() << "- at: " << Pos << '\n';
526}
527
528void MachineVerifier::report_context(const LiveInterval &LI) const {
529 errs() << "- interval: " << LI << '\n';
530}
531
532void MachineVerifier::report_context(const LiveRange &LR, Register VRegUnit,
533 LaneBitmask LaneMask) const {
534 report_context_liverange(LR);
535 report_context_vreg_regunit(VRegUnit);
536 if (LaneMask.any())
537 report_context_lanemask(LaneMask);
538}
539
540void MachineVerifier::report_context(const LiveRange::Segment &S) const {
541 errs() << "- segment: " << S << '\n';
542}
543
544void MachineVerifier::report_context(const VNInfo &VNI) const {
545 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
546}
547
548void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
549 errs() << "- liverange: " << LR << '\n';
550}
551
552void MachineVerifier::report_context(MCPhysReg PReg) const {
553 errs() << "- p. register: " << printReg(PReg, TRI) << '\n';
554}
555
556void MachineVerifier::report_context_vreg(Register VReg) const {
557 errs() << "- v. register: " << printReg(VReg, TRI) << '\n';
558}
559
560void MachineVerifier::report_context_vreg_regunit(Register VRegOrUnit) const {
561 if (Register::isVirtualRegister(VRegOrUnit)) {
562 report_context_vreg(VRegOrUnit);
563 } else {
564 errs() << "- regunit: " << printRegUnit(VRegOrUnit, TRI) << '\n';
565 }
566}
567
568void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
569 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
570}
571
572void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
573 BBInfo &MInfo = MBBInfoMap[MBB];
574 if (!MInfo.reachable) {
575 MInfo.reachable = true;
576 for (const MachineBasicBlock *Succ : MBB->successors())
577 markReachable(Succ);
578 }
579}
580
581void MachineVerifier::visitMachineFunctionBefore() {
582 lastIndex = SlotIndex();
583 regsReserved = MRI->reservedRegsFrozen() ? MRI->getReservedRegs()
584 : TRI->getReservedRegs(*MF);
585
586 if (!MF->empty())
587 markReachable(&MF->front());
588
589 // Build a set of the basic blocks in the function.
590 FunctionBlocks.clear();
591 for (const auto &MBB : *MF) {
592 FunctionBlocks.insert(&MBB);
593 BBInfo &MInfo = MBBInfoMap[&MBB];
594
595 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
596 if (MInfo.Preds.size() != MBB.pred_size())
597 report("MBB has duplicate entries in its predecessor list.", &MBB);
598
599 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
600 if (MInfo.Succs.size() != MBB.succ_size())
601 report("MBB has duplicate entries in its successor list.", &MBB);
602 }
603
604 // Check that the register use lists are sane.
605 MRI->verifyUseLists();
606
607 if (!MF->empty())
608 verifyStackFrame();
609}
610
611void
612MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
613 FirstTerminator = nullptr;
614 FirstNonPHI = nullptr;
615
616 if (!MF->getProperties().hasProperty(
617 MachineFunctionProperties::Property::NoPHIs) && MRI->tracksLiveness()) {
618 // If this block has allocatable physical registers live-in, check that
619 // it is an entry block or landing pad.
620 for (const auto &LI : MBB->liveins()) {
621 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
622 MBB->getIterator() != MBB->getParent()->begin()) {
623 report("MBB has allocatable live-in, but isn't entry or landing-pad.", MBB);
624 report_context(LI.PhysReg);
625 }
626 }
627 }
628
629 // Count the number of landing pad successors.
630 SmallPtrSet<const MachineBasicBlock*, 4> LandingPadSuccs;
631 for (const auto *succ : MBB->successors()) {
632 if (succ->isEHPad())
633 LandingPadSuccs.insert(succ);
634 if (!FunctionBlocks.count(succ))
635 report("MBB has successor that isn't part of the function.", MBB);
636 if (!MBBInfoMap[succ].Preds.count(MBB)) {
637 report("Inconsistent CFG", MBB);
638 errs() << "MBB is not in the predecessor list of the successor "
639 << printMBBReference(*succ) << ".\n";
640 }
641 }
642
643 // Check the predecessor list.
644 for (const MachineBasicBlock *Pred : MBB->predecessors()) {
645 if (!FunctionBlocks.count(Pred))
646 report("MBB has predecessor that isn't part of the function.", MBB);
647 if (!MBBInfoMap[Pred].Succs.count(MBB)) {
648 report("Inconsistent CFG", MBB);
649 errs() << "MBB is not in the successor list of the predecessor "
650 << printMBBReference(*Pred) << ".\n";
651 }
652 }
653
654 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
655 const BasicBlock *BB = MBB->getBasicBlock();
656 const Function &F = MF->getFunction();
657 if (LandingPadSuccs.size() > 1 &&
658 !(AsmInfo &&
659 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
660 BB && isa<SwitchInst>(BB->getTerminator())) &&
661 !isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
662 report("MBB has more than one landing pad successor", MBB);
663
664 // Call analyzeBranch. If it succeeds, there several more conditions to check.
665 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
666 SmallVector<MachineOperand, 4> Cond;
667 if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
668 Cond)) {
669 // Ok, analyzeBranch thinks it knows what's going on with this block. Let's
670 // check whether its answers match up with reality.
671 if (!TBB && !FBB) {
672 // Block falls through to its successor.
673 if (!MBB->empty() && MBB->back().isBarrier() &&
674 !TII->isPredicated(MBB->back())) {
675 report("MBB exits via unconditional fall-through but ends with a "
676 "barrier instruction!", MBB);
677 }
678 if (!Cond.empty()) {
679 report("MBB exits via unconditional fall-through but has a condition!",
680 MBB);
681 }
682 } else if (TBB && !FBB && Cond.empty()) {
683 // Block unconditionally branches somewhere.
684 if (MBB->empty()) {
685 report("MBB exits via unconditional branch but doesn't contain "
686 "any instructions!", MBB);
687 } else if (!MBB->back().isBarrier()) {
688 report("MBB exits via unconditional branch but doesn't end with a "
689 "barrier instruction!", MBB);
690 } else if (!MBB->back().isTerminator()) {
691 report("MBB exits via unconditional branch but the branch isn't a "
692 "terminator instruction!", MBB);
693 }
694 } else if (TBB && !FBB && !Cond.empty()) {
695 // Block conditionally branches somewhere, otherwise falls through.
696 if (MBB->empty()) {
697 report("MBB exits via conditional branch/fall-through but doesn't "
698 "contain any instructions!", MBB);
699 } else if (MBB->back().isBarrier()) {
700 report("MBB exits via conditional branch/fall-through but ends with a "
701 "barrier instruction!", MBB);
702 } else if (!MBB->back().isTerminator()) {
703 report("MBB exits via conditional branch/fall-through but the branch "
704 "isn't a terminator instruction!", MBB);
705 }
706 } else if (TBB && FBB) {
707 // Block conditionally branches somewhere, otherwise branches
708 // somewhere else.
709 if (MBB->empty()) {
710 report("MBB exits via conditional branch/branch but doesn't "
711 "contain any instructions!", MBB);
712 } else if (!MBB->back().isBarrier()) {
713 report("MBB exits via conditional branch/branch but doesn't end with a "
714 "barrier instruction!", MBB);
715 } else if (!MBB->back().isTerminator()) {
716 report("MBB exits via conditional branch/branch but the branch "
717 "isn't a terminator instruction!", MBB);
718 }
719 if (Cond.empty()) {
720 report("MBB exits via conditional branch/branch but there's no "
721 "condition!", MBB);
722 }
723 } else {
724 report("analyzeBranch returned invalid data!", MBB);
725 }
726
727 // Now check that the successors match up with the answers reported by
728 // analyzeBranch.
729 if (TBB && !MBB->isSuccessor(TBB))
730 report("MBB exits via jump or conditional branch, but its target isn't a "
731 "CFG successor!",
732 MBB);
733 if (FBB && !MBB->isSuccessor(FBB))
734 report("MBB exits via conditional branch, but its target isn't a CFG "
735 "successor!",
736 MBB);
737
738 // There might be a fallthrough to the next block if there's either no
739 // unconditional true branch, or if there's a condition, and one of the
740 // branches is missing.
741 bool Fallthrough = !TBB || (!Cond.empty() && !FBB);
742
743 // A conditional fallthrough must be an actual CFG successor, not
744 // unreachable. (Conversely, an unconditional fallthrough might not really
745 // be a successor, because the block might end in unreachable.)
746 if (!Cond.empty() && !FBB) {
747 MachineFunction::const_iterator MBBI = std::next(MBB->getIterator());
748 if (MBBI == MF->end()) {
749 report("MBB conditionally falls through out of function!", MBB);
750 } else if (!MBB->isSuccessor(&*MBBI))
751 report("MBB exits via conditional branch/fall-through but the CFG "
752 "successors don't match the actual successors!",
753 MBB);
754 }
755
756 // Verify that there aren't any extra un-accounted-for successors.
757 for (const MachineBasicBlock *SuccMBB : MBB->successors()) {
758 // If this successor is one of the branch targets, it's okay.
759 if (SuccMBB == TBB || SuccMBB == FBB)
760 continue;
761 // If we might have a fallthrough, and the successor is the fallthrough
762 // block, that's also ok.
763 if (Fallthrough && SuccMBB == MBB->getNextNode())
764 continue;
765 // Also accept successors which are for exception-handling or might be
766 // inlineasm_br targets.
767 if (SuccMBB->isEHPad() || SuccMBB->isInlineAsmBrIndirectTarget())
768 continue;
769 report("MBB has unexpected successors which are not branch targets, "
770 "fallthrough, EHPads, or inlineasm_br targets.",
771 MBB);
772 }
773 }
774
775 regsLive.clear();
776 if (MRI->tracksLiveness()) {
777 for (const auto &LI : MBB->liveins()) {
778 if (!Register::isPhysicalRegister(LI.PhysReg)) {
779 report("MBB live-in list contains non-physical register", MBB);
780 continue;
781 }
782 for (const MCPhysReg &SubReg : TRI->subregs_inclusive(LI.PhysReg))
783 regsLive.insert(SubReg);
784 }
785 }
786
787 const MachineFrameInfo &MFI = MF->getFrameInfo();
788 BitVector PR = MFI.getPristineRegs(*MF);
789 for (unsigned I : PR.set_bits()) {
790 for (const MCPhysReg &SubReg : TRI->subregs_inclusive(I))
791 regsLive.insert(SubReg);
792 }
793
794 regsKilled.clear();
795 regsDefined.clear();
796
797 if (Indexes)
798 lastIndex = Indexes->getMBBStartIdx(MBB);
799}
800
801// This function gets called for all bundle headers, including normal
802// stand-alone unbundled instructions.
803void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
804 if (Indexes && Indexes->hasIndex(*MI)) {
805 SlotIndex idx = Indexes->getInstructionIndex(*MI);
806 if (!(idx > lastIndex)) {
807 report("Instruction index out of order", MI);
808 errs() << "Last instruction was at " << lastIndex << '\n';
809 }
810 lastIndex = idx;
811 }
812
813 // Ensure non-terminators don't follow terminators.
814 if (MI->isTerminator()) {
815 if (!FirstTerminator)
816 FirstTerminator = MI;
817 } else if (FirstTerminator) {
818 report("Non-terminator instruction after the first terminator", MI);
819 errs() << "First terminator was:\t" << *FirstTerminator;
820 }
821}
822
823// The operands on an INLINEASM instruction must follow a template.
824// Verify that the flag operands make sense.
825void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
826 // The first two operands on INLINEASM are the asm string and global flags.
827 if (MI->getNumOperands() < 2) {
828 report("Too few operands on inline asm", MI);
829 return;
830 }
831 if (!MI->getOperand(0).isSymbol())
832 report("Asm string must be an external symbol", MI);
833 if (!MI->getOperand(1).isImm())
834 report("Asm flags must be an immediate", MI);
835 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
836 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
837 // and Extra_IsConvergent = 32.
838 if (!isUInt<6>(MI->getOperand(1).getImm()))
839 report("Unknown asm flags", &MI->getOperand(1), 1);
840
841 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
842
843 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
844 unsigned NumOps;
845 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
846 const MachineOperand &MO = MI->getOperand(OpNo);
847 // There may be implicit ops after the fixed operands.
848 if (!MO.isImm())
849 break;
850 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
851 }
852
853 if (OpNo > MI->getNumOperands())
854 report("Missing operands in last group", MI);
855
856 // An optional MDNode follows the groups.
857 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
858 ++OpNo;
859
860 // All trailing operands must be implicit registers.
861 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
862 const MachineOperand &MO = MI->getOperand(OpNo);
863 if (!MO.isReg() || !MO.isImplicit())
864 report("Expected implicit register after groups", &MO, OpNo);
865 }
866}
867
868bool MachineVerifier::verifyAllRegOpsScalar(const MachineInstr &MI,
869 const MachineRegisterInfo &MRI) {
870 if (none_of(MI.explicit_operands(), [&MRI](const MachineOperand &Op) {
871 if (!Op.isReg())
872 return false;
873 const auto Reg = Op.getReg();
874 if (Reg.isPhysical())
875 return false;
876 return !MRI.getType(Reg).isScalar();
877 }))
878 return true;
879 report("All register operands must have scalar types", &MI);
880 return false;
881}
882
883/// Check that types are consistent when two operands need to have the same
884/// number of vector elements.
885/// \return true if the types are valid.
886bool MachineVerifier::verifyVectorElementMatch(LLT Ty0, LLT Ty1,
887 const MachineInstr *MI) {
888 if (Ty0.isVector() != Ty1.isVector()) {
889 report("operand types must be all-vector or all-scalar", MI);
890 // Generally we try to report as many issues as possible at once, but in
891 // this case it's not clear what should we be comparing the size of the
892 // scalar with: the size of the whole vector or its lane. Instead of
893 // making an arbitrary choice and emitting not so helpful message, let's
894 // avoid the extra noise and stop here.
895 return false;
896 }
897
898 if (Ty0.isVector() && Ty0.getNumElements() != Ty1.getNumElements()) {
899 report("operand types must preserve number of vector elements", MI);
900 return false;
901 }
902
903 return true;
904}
905
906void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) {
907 if (isFunctionSelected)
908 report("Unexpected generic instruction in a Selected function", MI);
909
910 const MCInstrDesc &MCID = MI->getDesc();
911 unsigned NumOps = MI->getNumOperands();
912
913 // Branches must reference a basic block if they are not indirect
914 if (MI->isBranch() && !MI->isIndirectBranch()) {
915 bool HasMBB = false;
916 for (const MachineOperand &Op : MI->operands()) {
917 if (Op.isMBB()) {
918 HasMBB = true;
919 break;
920 }
921 }
922
923 if (!HasMBB) {
924 report("Branch instruction is missing a basic block operand or "
925 "isIndirectBranch property",
926 MI);
927 }
928 }
929
930 // Check types.
931 SmallVector<LLT, 4> Types;
932 for (unsigned I = 0, E = std::min(MCID.getNumOperands(), NumOps);
933 I != E; ++I) {
934 if (!MCID.OpInfo[I].isGenericType())
935 continue;
936 // Generic instructions specify type equality constraints between some of
937 // their operands. Make sure these are consistent.
938 size_t TypeIdx = MCID.OpInfo[I].getGenericTypeIndex();
939 Types.resize(std::max(TypeIdx + 1, Types.size()));
940
941 const MachineOperand *MO = &MI->getOperand(I);
942 if (!MO->isReg()) {
943 report("generic instruction must use register operands", MI);
944 continue;
945 }
946
947 LLT OpTy = MRI->getType(MO->getReg());
948 // Don't report a type mismatch if there is no actual mismatch, only a
949 // type missing, to reduce noise:
950 if (OpTy.isValid()) {
951 // Only the first valid type for a type index will be printed: don't
952 // overwrite it later so it's always clear which type was expected:
953 if (!Types[TypeIdx].isValid())
954 Types[TypeIdx] = OpTy;
955 else if (Types[TypeIdx] != OpTy)
956 report("Type mismatch in generic instruction", MO, I, OpTy);
957 } else {
958 // Generic instructions must have types attached to their operands.
959 report("Generic instruction is missing a virtual register type", MO, I);
960 }
961 }
962
963 // Generic opcodes must not have physical register operands.
964 for (unsigned I = 0; I < MI->getNumOperands(); ++I) {
965 const MachineOperand *MO = &MI->getOperand(I);
966 if (MO->isReg() && Register::isPhysicalRegister(MO->getReg()))
967 report("Generic instruction cannot have physical register", MO, I);
968 }
969
970 // Avoid out of bounds in checks below. This was already reported earlier.
971 if (MI->getNumOperands() < MCID.getNumOperands())
972 return;
973
974 StringRef ErrorInfo;
975 if (!TII->verifyInstruction(*MI, ErrorInfo))
976 report(ErrorInfo.data(), MI);
977
978 // Verify properties of various specific instruction types
979 unsigned Opc = MI->getOpcode();
980 switch (Opc) {
981 case TargetOpcode::G_ASSERT_SEXT:
982 case TargetOpcode::G_ASSERT_ZEXT: {
983 std::string OpcName =
984 Opc == TargetOpcode::G_ASSERT_ZEXT ? "G_ASSERT_ZEXT" : "G_ASSERT_SEXT";
985 if (!MI->getOperand(2).isImm()) {
986 report(Twine(OpcName, " expects an immediate operand #2"), MI);
987 break;
988 }
989
990 Register Dst = MI->getOperand(0).getReg();
991 Register Src = MI->getOperand(1).getReg();
992 LLT SrcTy = MRI->getType(Src);
993 int64_t Imm = MI->getOperand(2).getImm();
994 if (Imm <= 0) {
995 report(Twine(OpcName, " size must be >= 1"), MI);
996 break;
997 }
998
999 if (Imm >= SrcTy.getScalarSizeInBits()) {
1000 report(Twine(OpcName, " size must be less than source bit width"), MI);
1001 break;
1002 }
1003
1004 if (MRI->getRegBankOrNull(Src) != MRI->getRegBankOrNull(Dst)) {
1005 report(
1006 Twine(OpcName, " source and destination register banks must match"),
1007 MI);
1008 break;
1009 }
1010
1011 if (MRI->getRegClassOrNull(Src) != MRI->getRegClassOrNull(Dst))
1012 report(
1013 Twine(OpcName, " source and destination register classes must match"),
1014 MI);
1015
1016 break;
1017 }
1018
1019 case TargetOpcode::G_CONSTANT:
1020 case TargetOpcode::G_FCONSTANT: {
1021 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1022 if (DstTy.isVector())
1023 report("Instruction cannot use a vector result type", MI);
1024
1025 if (MI->getOpcode() == TargetOpcode::G_CONSTANT) {
1026 if (!MI->getOperand(1).isCImm()) {
1027 report("G_CONSTANT operand must be cimm", MI);
1028 break;
1029 }
1030
1031 const ConstantInt *CI = MI->getOperand(1).getCImm();
1032 if (CI->getBitWidth() != DstTy.getSizeInBits())
1033 report("inconsistent constant size", MI);
1034 } else {
1035 if (!MI->getOperand(1).isFPImm()) {
1036 report("G_FCONSTANT operand must be fpimm", MI);
1037 break;
1038 }
1039 const ConstantFP *CF = MI->getOperand(1).getFPImm();
1040
1041 if (APFloat::getSizeInBits(CF->getValueAPF().getSemantics()) !=
1042 DstTy.getSizeInBits()) {
1043 report("inconsistent constant size", MI);
1044 }
1045 }
1046
1047 break;
1048 }
1049 case TargetOpcode::G_LOAD:
1050 case TargetOpcode::G_STORE:
1051 case TargetOpcode::G_ZEXTLOAD:
1052 case TargetOpcode::G_SEXTLOAD: {
1053 LLT ValTy = MRI->getType(MI->getOperand(0).getReg());
1054 LLT PtrTy = MRI->getType(MI->getOperand(1).getReg());
1055 if (!PtrTy.isPointer())
1056 report("Generic memory instruction must access a pointer", MI);
1057
1058 // Generic loads and stores must have a single MachineMemOperand
1059 // describing that access.
1060 if (!MI->hasOneMemOperand()) {
1061 report("Generic instruction accessing memory must have one mem operand",
1062 MI);
1063 } else {
1064 const MachineMemOperand &MMO = **MI->memoperands_begin();
1065 if (MI->getOpcode() == TargetOpcode::G_ZEXTLOAD ||
1066 MI->getOpcode() == TargetOpcode::G_SEXTLOAD) {
1067 if (MMO.getSizeInBits() >= ValTy.getSizeInBits())
1068 report("Generic extload must have a narrower memory type", MI);
1069 } else if (MI->getOpcode() == TargetOpcode::G_LOAD) {
1070 if (MMO.getSize() > ValTy.getSizeInBytes())
1071 report("load memory size cannot exceed result size", MI);
1072 } else if (MI->getOpcode() == TargetOpcode::G_STORE) {
1073 if (ValTy.getSizeInBytes() < MMO.getSize())
1074 report("store memory size cannot exceed value size", MI);
1075 }
1076
1077 const AtomicOrdering Order = MMO.getSuccessOrdering();
1078 if (Opc == TargetOpcode::G_STORE) {
1079 if (Order == AtomicOrdering::Acquire ||
1080 Order == AtomicOrdering::AcquireRelease)
1081 report("atomic store cannot use acquire ordering", MI);
1082
1083 } else {
1084 if (Order == AtomicOrdering::Release ||
1085 Order == AtomicOrdering::AcquireRelease)
1086 report("atomic load cannot use release ordering", MI);
1087 }
1088 }
1089
1090 break;
1091 }
1092 case TargetOpcode::G_PHI: {
1093 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1094 if (!DstTy.isValid() || !all_of(drop_begin(MI->operands()),
1095 [this, &DstTy](const MachineOperand &MO) {
1096 if (!MO.isReg())
1097 return true;
1098 LLT Ty = MRI->getType(MO.getReg());
1099 if (!Ty.isValid() || (Ty != DstTy))
1100 return false;
1101 return true;
1102 }))
1103 report("Generic Instruction G_PHI has operands with incompatible/missing "
1104 "types",
1105 MI);
1106 break;
1107 }
1108 case TargetOpcode::G_BITCAST: {
1109 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1110 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1111 if (!DstTy.isValid() || !SrcTy.isValid())
1112 break;
1113
1114 if (SrcTy.isPointer() != DstTy.isPointer())
1115 report("bitcast cannot convert between pointers and other types", MI);
1116
1117 if (SrcTy.getSizeInBits() != DstTy.getSizeInBits())
1118 report("bitcast sizes must match", MI);
1119
1120 if (SrcTy == DstTy)
1121 report("bitcast must change the type", MI);
1122
1123 break;
1124 }
1125 case TargetOpcode::G_INTTOPTR:
1126 case TargetOpcode::G_PTRTOINT:
1127 case TargetOpcode::G_ADDRSPACE_CAST: {
1128 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1129 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1130 if (!DstTy.isValid() || !SrcTy.isValid())
1131 break;
1132
1133 verifyVectorElementMatch(DstTy, SrcTy, MI);
1134
1135 DstTy = DstTy.getScalarType();
1136 SrcTy = SrcTy.getScalarType();
1137
1138 if (MI->getOpcode() == TargetOpcode::G_INTTOPTR) {
1139 if (!DstTy.isPointer())
1140 report("inttoptr result type must be a pointer", MI);
1141 if (SrcTy.isPointer())
1142 report("inttoptr source type must not be a pointer", MI);
1143 } else if (MI->getOpcode() == TargetOpcode::G_PTRTOINT) {
1144 if (!SrcTy.isPointer())
1145 report("ptrtoint source type must be a pointer", MI);
1146 if (DstTy.isPointer())
1147 report("ptrtoint result type must not be a pointer", MI);
1148 } else {
1149 assert(MI->getOpcode() == TargetOpcode::G_ADDRSPACE_CAST)(static_cast <bool> (MI->getOpcode() == TargetOpcode
::G_ADDRSPACE_CAST) ? void (0) : __assert_fail ("MI->getOpcode() == TargetOpcode::G_ADDRSPACE_CAST"
, "llvm/lib/CodeGen/MachineVerifier.cpp", 1149, __extension__
__PRETTY_FUNCTION__))
;
1150 if (!SrcTy.isPointer() || !DstTy.isPointer())
1151 report("addrspacecast types must be pointers", MI);
1152 else {
1153 if (SrcTy.getAddressSpace() == DstTy.getAddressSpace())
1154 report("addrspacecast must convert different address spaces", MI);
1155 }
1156 }
1157
1158 break;
1159 }
1160 case TargetOpcode::G_PTR_ADD: {
1161 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1162 LLT PtrTy = MRI->getType(MI->getOperand(1).getReg());
1163 LLT OffsetTy = MRI->getType(MI->getOperand(2).getReg());
1164 if (!DstTy.isValid() || !PtrTy.isValid() || !OffsetTy.isValid())
1165 break;
1166
1167 if (!PtrTy.getScalarType().isPointer())
1168 report("gep first operand must be a pointer", MI);
1169
1170 if (OffsetTy.getScalarType().isPointer())
1171 report("gep offset operand must not be a pointer", MI);
1172
1173 // TODO: Is the offset allowed to be a scalar with a vector?
1174 break;
1175 }
1176 case TargetOpcode::G_PTRMASK: {
1177 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1178 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1179 LLT MaskTy = MRI->getType(MI->getOperand(2).getReg());
1180 if (!DstTy.isValid() || !SrcTy.isValid() || !MaskTy.isValid())
1181 break;
1182
1183 if (!DstTy.getScalarType().isPointer())
1184 report("ptrmask result type must be a pointer", MI);
1185
1186 if (!MaskTy.getScalarType().isScalar())
1187 report("ptrmask mask type must be an integer", MI);
1188
1189 verifyVectorElementMatch(DstTy, MaskTy, MI);
1190 break;
1191 }
1192 case TargetOpcode::G_SEXT:
1193 case TargetOpcode::G_ZEXT:
1194 case TargetOpcode::G_ANYEXT:
1195 case TargetOpcode::G_TRUNC:
1196 case TargetOpcode::G_FPEXT:
1197 case TargetOpcode::G_FPTRUNC: {
1198 // Number of operands and presense of types is already checked (and
1199 // reported in case of any issues), so no need to report them again. As
1200 // we're trying to report as many issues as possible at once, however, the
1201 // instructions aren't guaranteed to have the right number of operands or
1202 // types attached to them at this point
1203 assert(MCID.getNumOperands() == 2 && "Expected 2 operands G_*{EXT,TRUNC}")(static_cast <bool> (MCID.getNumOperands() == 2 &&
"Expected 2 operands G_*{EXT,TRUNC}") ? void (0) : __assert_fail
("MCID.getNumOperands() == 2 && \"Expected 2 operands G_*{EXT,TRUNC}\""
, "llvm/lib/CodeGen/MachineVerifier.cpp", 1203, __extension__
__PRETTY_FUNCTION__))
;
1204 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1205 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1206 if (!DstTy.isValid() || !SrcTy.isValid())
1207 break;
1208
1209 LLT DstElTy = DstTy.getScalarType();
1210 LLT SrcElTy = SrcTy.getScalarType();
1211 if (DstElTy.isPointer() || SrcElTy.isPointer())
1212 report("Generic extend/truncate can not operate on pointers", MI);
1213
1214 verifyVectorElementMatch(DstTy, SrcTy, MI);
1215
1216 unsigned DstSize = DstElTy.getSizeInBits();
1217 unsigned SrcSize = SrcElTy.getSizeInBits();
1218 switch (MI->getOpcode()) {
1219 default:
1220 if (DstSize <= SrcSize)
1221 report("Generic extend has destination type no larger than source", MI);
1222 break;
1223 case TargetOpcode::G_TRUNC:
1224 case TargetOpcode::G_FPTRUNC:
1225 if (DstSize >= SrcSize)
1226 report("Generic truncate has destination type no smaller than source",
1227 MI);
1228 break;
1229 }
1230 break;
1231 }
1232 case TargetOpcode::G_SELECT: {
1233 LLT SelTy = MRI->getType(MI->getOperand(0).getReg());
1234 LLT CondTy = MRI->getType(MI->getOperand(1).getReg());
1235 if (!SelTy.isValid() || !CondTy.isValid())
1236 break;
1237
1238 // Scalar condition select on a vector is valid.
1239 if (CondTy.isVector())
1240 verifyVectorElementMatch(SelTy, CondTy, MI);
1241 break;
1242 }
1243 case TargetOpcode::G_MERGE_VALUES: {
1244 // G_MERGE_VALUES should only be used to merge scalars into a larger scalar,
1245 // e.g. s2N = MERGE sN, sN
1246 // Merging multiple scalars into a vector is not allowed, should use
1247 // G_BUILD_VECTOR for that.
1248 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1249 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1250 if (DstTy.isVector() || SrcTy.isVector())
1251 report("G_MERGE_VALUES cannot operate on vectors", MI);
1252
1253 const unsigned NumOps = MI->getNumOperands();
1254 if (DstTy.getSizeInBits() != SrcTy.getSizeInBits() * (NumOps - 1))
1255 report("G_MERGE_VALUES result size is inconsistent", MI);
1256
1257 for (unsigned I = 2; I != NumOps; ++I) {
1258 if (MRI->getType(MI->getOperand(I).getReg()) != SrcTy)
1259 report("G_MERGE_VALUES source types do not match", MI);
1260 }
1261
1262 break;
1263 }
1264 case TargetOpcode::G_UNMERGE_VALUES: {
1265 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1266 LLT SrcTy = MRI->getType(MI->getOperand(MI->getNumOperands()-1).getReg());
1267 // For now G_UNMERGE can split vectors.
1268 for (unsigned i = 0; i < MI->getNumOperands()-1; ++i) {
1269 if (MRI->getType(MI->getOperand(i).getReg()) != DstTy)
1270 report("G_UNMERGE_VALUES destination types do not match", MI);
1271 }
1272 if (SrcTy.getSizeInBits() !=
1273 (DstTy.getSizeInBits() * (MI->getNumOperands() - 1))) {
1274 report("G_UNMERGE_VALUES source operand does not cover dest operands",
1275 MI);
1276 }
1277 break;
1278 }
1279 case TargetOpcode::G_BUILD_VECTOR: {
1280 // Source types must be scalars, dest type a vector. Total size of scalars
1281 // must match the dest vector size.
1282 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1283 LLT SrcEltTy = MRI->getType(MI->getOperand(1).getReg());
1284 if (!DstTy.isVector() || SrcEltTy.isVector()) {
1285 report("G_BUILD_VECTOR must produce a vector from scalar operands", MI);
1286 break;
1287 }
1288
1289 if (DstTy.getElementType() != SrcEltTy)
1290 report("G_BUILD_VECTOR result element type must match source type", MI);
1291
1292 if (DstTy.getNumElements() != MI->getNumOperands() - 1)
1293 report("G_BUILD_VECTOR must have an operand for each elemement", MI);
1294
1295 for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 2))
1296 if (MRI->getType(MI->getOperand(1).getReg()) != MRI->getType(MO.getReg()))
1297 report("G_BUILD_VECTOR source operand types are not homogeneous", MI);
1298
1299 break;
1300 }
1301 case TargetOpcode::G_BUILD_VECTOR_TRUNC: {
1302 // Source types must be scalars, dest type a vector. Scalar types must be
1303 // larger than the dest vector elt type, as this is a truncating operation.
1304 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1305 LLT SrcEltTy = MRI->getType(MI->getOperand(1).getReg());
1306 if (!DstTy.isVector() || SrcEltTy.isVector())
1307 report("G_BUILD_VECTOR_TRUNC must produce a vector from scalar operands",
1308 MI);
1309 for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 2))
1310 if (MRI->getType(MI->getOperand(1).getReg()) != MRI->getType(MO.getReg()))
1311 report("G_BUILD_VECTOR_TRUNC source operand types are not homogeneous",
1312 MI);
1313 if (SrcEltTy.getSizeInBits() <= DstTy.getElementType().getSizeInBits())
1314 report("G_BUILD_VECTOR_TRUNC source operand types are not larger than "
1315 "dest elt type",
1316 MI);
1317 break;
1318 }
1319 case TargetOpcode::G_CONCAT_VECTORS: {
1320 // Source types should be vectors, and total size should match the dest
1321 // vector size.
1322 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1323 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1324 if (!DstTy.isVector() || !SrcTy.isVector())
1325 report("G_CONCAT_VECTOR requires vector source and destination operands",
1326 MI);
1327
1328 if (MI->getNumOperands() < 3)
1329 report("G_CONCAT_VECTOR requires at least 2 source operands", MI);
1330
1331 for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 2))
1332 if (MRI->getType(MI->getOperand(1).getReg()) != MRI->getType(MO.getReg()))
1333 report("G_CONCAT_VECTOR source operand types are not homogeneous", MI);
1334 if (DstTy.getNumElements() !=
1335 SrcTy.getNumElements() * (MI->getNumOperands() - 1))
1336 report("G_CONCAT_VECTOR num dest and source elements should match", MI);
1337 break;
1338 }
1339 case TargetOpcode::G_ICMP:
1340 case TargetOpcode::G_FCMP: {
1341 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1342 LLT SrcTy = MRI->getType(MI->getOperand(2).getReg());
1343
1344 if ((DstTy.isVector() != SrcTy.isVector()) ||
1345 (DstTy.isVector() && DstTy.getNumElements() != SrcTy.getNumElements()))
1346 report("Generic vector icmp/fcmp must preserve number of lanes", MI);
1347
1348 break;
1349 }
1350 case TargetOpcode::G_EXTRACT: {
1351 const MachineOperand &SrcOp = MI->getOperand(1);
1352 if (!SrcOp.isReg()) {
1353 report("extract source must be a register", MI);
1354 break;
1355 }
1356
1357 const MachineOperand &OffsetOp = MI->getOperand(2);
1358 if (!OffsetOp.isImm()) {
1359 report("extract offset must be a constant", MI);
1360 break;
1361 }
1362
1363 unsigned DstSize = MRI->getType(MI->getOperand(0).getReg()).getSizeInBits();
1364 unsigned SrcSize = MRI->getType(SrcOp.getReg()).getSizeInBits();
1365 if (SrcSize == DstSize)
1366 report("extract source must be larger than result", MI);
1367
1368 if (DstSize + OffsetOp.getImm() > SrcSize)
1369 report("extract reads past end of register", MI);
1370 break;
1371 }
1372 case TargetOpcode::G_INSERT: {
1373 const MachineOperand &SrcOp = MI->getOperand(2);
1374 if (!SrcOp.isReg()) {
1375 report("insert source must be a register", MI);
1376 break;
1377 }
1378
1379 const MachineOperand &OffsetOp = MI->getOperand(3);
1380 if (!OffsetOp.isImm()) {
1381 report("insert offset must be a constant", MI);
1382 break;
1383 }
1384
1385 unsigned DstSize = MRI->getType(MI->getOperand(0).getReg()).getSizeInBits();
1386 unsigned SrcSize = MRI->getType(SrcOp.getReg()).getSizeInBits();
1387
1388 if (DstSize <= SrcSize)
1389 report("inserted size must be smaller than total register", MI);
1390
1391 if (SrcSize + OffsetOp.getImm() > DstSize)
1392 report("insert writes past end of register", MI);
1393
1394 break;
1395 }
1396 case TargetOpcode::G_JUMP_TABLE: {
1397 if (!MI->getOperand(1).isJTI())
1398 report("G_JUMP_TABLE source operand must be a jump table index", MI);
1399 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1400 if (!DstTy.isPointer())
1401 report("G_JUMP_TABLE dest operand must have a pointer type", MI);
1402 break;
1403 }
1404 case TargetOpcode::G_BRJT: {
1405 if (!MRI->getType(MI->getOperand(0).getReg()).isPointer())
1406 report("G_BRJT src operand 0 must be a pointer type", MI);
1407
1408 if (!MI->getOperand(1).isJTI())
1409 report("G_BRJT src operand 1 must be a jump table index", MI);
1410
1411 const auto &IdxOp = MI->getOperand(2);
1412 if (!IdxOp.isReg() || MRI->getType(IdxOp.getReg()).isPointer())
1413 report("G_BRJT src operand 2 must be a scalar reg type", MI);
1414 break;
1415 }
1416 case TargetOpcode::G_INTRINSIC:
1417 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS: {
1418 // TODO: Should verify number of def and use operands, but the current
1419 // interface requires passing in IR types for mangling.
1420 const MachineOperand &IntrIDOp = MI->getOperand(MI->getNumExplicitDefs());
1421 if (!IntrIDOp.isIntrinsicID()) {
1422 report("G_INTRINSIC first src operand must be an intrinsic ID", MI);
1423 break;
1424 }
1425
1426 bool NoSideEffects = MI->getOpcode() == TargetOpcode::G_INTRINSIC;
1427 unsigned IntrID = IntrIDOp.getIntrinsicID();
1428 if (IntrID != 0 && IntrID < Intrinsic::num_intrinsics) {
1429 AttributeList Attrs
1430 = Intrinsic::getAttributes(MF->getFunction().getContext(),
1431 static_cast<Intrinsic::ID>(IntrID));
1432 bool DeclHasSideEffects = !Attrs.hasFnAttr(Attribute::ReadNone);
1433 if (NoSideEffects && DeclHasSideEffects) {
1434 report("G_INTRINSIC used with intrinsic that accesses memory", MI);
1435 break;
1436 }
1437 if (!NoSideEffects && !DeclHasSideEffects) {
1438 report("G_INTRINSIC_W_SIDE_EFFECTS used with readnone intrinsic", MI);
1439 break;
1440 }
1441 }
1442
1443 break;
1444 }
1445 case TargetOpcode::G_SEXT_INREG: {
1446 if (!MI->getOperand(2).isImm()) {
1447 report("G_SEXT_INREG expects an immediate operand #2", MI);
1448 break;
1449 }
1450
1451 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1452 int64_t Imm = MI->getOperand(2).getImm();
1453 if (Imm <= 0)
1454 report("G_SEXT_INREG size must be >= 1", MI);
1455 if (Imm >= SrcTy.getScalarSizeInBits())
1456 report("G_SEXT_INREG size must be less than source bit width", MI);
1457 break;
1458 }
1459 case TargetOpcode::G_SHUFFLE_VECTOR: {
1460 const MachineOperand &MaskOp = MI->getOperand(3);
1461 if (!MaskOp.isShuffleMask()) {
1462 report("Incorrect mask operand type for G_SHUFFLE_VECTOR", MI);
1463 break;
1464 }
1465
1466 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1467 LLT Src0Ty = MRI->getType(MI->getOperand(1).getReg());
1468 LLT Src1Ty = MRI->getType(MI->getOperand(2).getReg());
1469
1470 if (Src0Ty != Src1Ty)
1471 report("Source operands must be the same type", MI);
1472
1473 if (Src0Ty.getScalarType() != DstTy.getScalarType())
1474 report("G_SHUFFLE_VECTOR cannot change element type", MI);
1475
1476 // Don't check that all operands are vector because scalars are used in
1477 // place of 1 element vectors.
1478 int SrcNumElts = Src0Ty.isVector() ? Src0Ty.getNumElements() : 1;
1479 int DstNumElts = DstTy.isVector() ? DstTy.getNumElements() : 1;
1480
1481 ArrayRef<int> MaskIdxes = MaskOp.getShuffleMask();
1482
1483 if (static_cast<int>(MaskIdxes.size()) != DstNumElts)
1484 report("Wrong result type for shufflemask", MI);
1485
1486 for (int Idx : MaskIdxes) {
1487 if (Idx < 0)
1488 continue;
1489
1490 if (Idx >= 2 * SrcNumElts)
1491 report("Out of bounds shuffle index", MI);
1492 }
1493
1494 break;
1495 }
1496 case TargetOpcode::G_DYN_STACKALLOC: {
1497 const MachineOperand &DstOp = MI->getOperand(0);
1498 const MachineOperand &AllocOp = MI->getOperand(1);
1499 const MachineOperand &AlignOp = MI->getOperand(2);
1500
1501 if (!DstOp.isReg() || !MRI->getType(DstOp.getReg()).isPointer()) {
1502 report("dst operand 0 must be a pointer type", MI);
1503 break;
1504 }
1505
1506 if (!AllocOp.isReg() || !MRI->getType(AllocOp.getReg()).isScalar()) {
1507 report("src operand 1 must be a scalar reg type", MI);
1508 break;
1509 }
1510
1511 if (!AlignOp.isImm()) {
1512 report("src operand 2 must be an immediate type", MI);
1513 break;
1514 }
1515 break;
1516 }
1517 case TargetOpcode::G_MEMCPY_INLINE:
1518 case TargetOpcode::G_MEMCPY:
1519 case TargetOpcode::G_MEMMOVE: {
1520 ArrayRef<MachineMemOperand *> MMOs = MI->memoperands();
1521 if (MMOs.size() != 2) {
1522 report("memcpy/memmove must have 2 memory operands", MI);
1523 break;
1524 }
1525
1526 if ((!MMOs[0]->isStore() || MMOs[0]->isLoad()) ||
1527 (MMOs[1]->isStore() || !MMOs[1]->isLoad())) {
1528 report("wrong memory operand types", MI);
1529 break;
1530 }
1531
1532 if (MMOs[0]->getSize() != MMOs[1]->getSize())
1533 report("inconsistent memory operand sizes", MI);
1534
1535 LLT DstPtrTy = MRI->getType(MI->getOperand(0).getReg());
1536 LLT SrcPtrTy = MRI->getType(MI->getOperand(1).getReg());
1537
1538 if (!DstPtrTy.isPointer() || !SrcPtrTy.isPointer()) {
1539 report("memory instruction operand must be a pointer", MI);
1540 break;
1541 }
1542
1543 if (DstPtrTy.getAddressSpace() != MMOs[0]->getAddrSpace())
1544 report("inconsistent store address space", MI);
1545 if (SrcPtrTy.getAddressSpace() != MMOs[1]->getAddrSpace())
1546 report("inconsistent load address space", MI);
1547
1548 if (Opc != TargetOpcode::G_MEMCPY_INLINE)
1549 if (!MI->getOperand(3).isImm() || (MI->getOperand(3).getImm() & ~1LL))
1550 report("'tail' flag (operand 3) must be an immediate 0 or 1", MI);
1551
1552 break;
1553 }
1554 case TargetOpcode::G_BZERO:
1555 case TargetOpcode::G_MEMSET: {
1556 ArrayRef<MachineMemOperand *> MMOs = MI->memoperands();
1557 std::string Name = Opc == TargetOpcode::G_MEMSET ? "memset" : "bzero";
1558 if (MMOs.size() != 1) {
1559 report(Twine(Name, " must have 1 memory operand"), MI);
1560 break;
1561 }
1562
1563 if ((!MMOs[0]->isStore() || MMOs[0]->isLoad())) {
1564 report(Twine(Name, " memory operand must be a store"), MI);
1565 break;
1566 }
1567
1568 LLT DstPtrTy = MRI->getType(MI->getOperand(0).getReg());
1569 if (!DstPtrTy.isPointer()) {
1570 report(Twine(Name, " operand must be a pointer"), MI);
1571 break;
1572 }
1573
1574 if (DstPtrTy.getAddressSpace() != MMOs[0]->getAddrSpace())
1575 report("inconsistent " + Twine(Name, " address space"), MI);
1576
1577 if (!MI->getOperand(MI->getNumOperands() - 1).isImm() ||
1578 (MI->getOperand(MI->getNumOperands() - 1).getImm() & ~1LL))
1579 report("'tail' flag (last operand) must be an immediate 0 or 1", MI);
1580
1581 break;
1582 }
1583 case TargetOpcode::G_VECREDUCE_SEQ_FADD:
1584 case TargetOpcode::G_VECREDUCE_SEQ_FMUL: {
1585 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1586 LLT Src1Ty = MRI->getType(MI->getOperand(1).getReg());
1587 LLT Src2Ty = MRI->getType(MI->getOperand(2).getReg());
1588 if (!DstTy.isScalar())
1589 report("Vector reduction requires a scalar destination type", MI);
1590 if (!Src1Ty.isScalar())
1591 report("Sequential FADD/FMUL vector reduction requires a scalar 1st operand", MI);
1592 if (!Src2Ty.isVector())
1593 report("Sequential FADD/FMUL vector reduction must have a vector 2nd operand", MI);
1594 break;
1595 }
1596 case TargetOpcode::G_VECREDUCE_FADD:
1597 case TargetOpcode::G_VECREDUCE_FMUL:
1598 case TargetOpcode::G_VECREDUCE_FMAX:
1599 case TargetOpcode::G_VECREDUCE_FMIN:
1600 case TargetOpcode::G_VECREDUCE_ADD:
1601 case TargetOpcode::G_VECREDUCE_MUL:
1602 case TargetOpcode::G_VECREDUCE_AND:
1603 case TargetOpcode::G_VECREDUCE_OR:
1604 case TargetOpcode::G_VECREDUCE_XOR:
1605 case TargetOpcode::G_VECREDUCE_SMAX:
1606 case TargetOpcode::G_VECREDUCE_SMIN:
1607 case TargetOpcode::G_VECREDUCE_UMAX:
1608 case TargetOpcode::G_VECREDUCE_UMIN: {
1609 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1610 if (!DstTy.isScalar())
1611 report("Vector reduction requires a scalar destination type", MI);
1612 break;
1613 }
1614
1615 case TargetOpcode::G_SBFX:
1616 case TargetOpcode::G_UBFX: {
1617 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1618 if (DstTy.isVector()) {
1619 report("Bitfield extraction is not supported on vectors", MI);
1620 break;
1621 }
1622 break;
1623 }
1624 case TargetOpcode::G_SHL:
1625 case TargetOpcode::G_LSHR:
1626 case TargetOpcode::G_ASHR:
1627 case TargetOpcode::G_ROTR:
1628 case TargetOpcode::G_ROTL: {
1629 LLT Src1Ty = MRI->getType(MI->getOperand(1).getReg());
1630 LLT Src2Ty = MRI->getType(MI->getOperand(2).getReg());
1631 if (Src1Ty.isVector() != Src2Ty.isVector()) {
1632 report("Shifts and rotates require operands to be either all scalars or "
1633 "all vectors",
1634 MI);
1635 break;
1636 }
1637 break;
1638 }
1639 case TargetOpcode::G_LLROUND:
1640 case TargetOpcode::G_LROUND: {
1641 verifyAllRegOpsScalar(*MI, *MRI);
1642 break;
1643 }
1644 default:
1645 break;
1646 }
1647}
1648
1649void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
1650 const MCInstrDesc &MCID = MI->getDesc();
1651 if (MI->getNumOperands() < MCID.getNumOperands()) {
1652 report("Too few operands", MI);
1653 errs() << MCID.getNumOperands() << " operands expected, but "
1654 << MI->getNumOperands() << " given.\n";
1655 }
1656
1657 if (MI->isPHI()) {
1658 if (MF->getProperties().hasProperty(
1659 MachineFunctionProperties::Property::NoPHIs))
1660 report("Found PHI instruction with NoPHIs property set", MI);
1661
1662 if (FirstNonPHI)
1663 report("Found PHI instruction after non-PHI", MI);
1664 } else if (FirstNonPHI == nullptr)
1665 FirstNonPHI = MI;
1666
1667 // Check the tied operands.
1668 if (MI->isInlineAsm())
1669 verifyInlineAsm(MI);
1670
1671 // Check that unspillable terminators define a reg and have at most one use.
1672 if (TII->isUnspillableTerminator(MI)) {
1673 if (!MI->getOperand(0).isReg() || !MI->getOperand(0).isDef())
1674 report("Unspillable Terminator does not define a reg", MI);
1675 Register Def = MI->getOperand(0).getReg();
1676 if (Def.isVirtual() &&
1677 !MF->getProperties().hasProperty(
1678 MachineFunctionProperties::Property::NoPHIs) &&
1679 std::distance(MRI->use_nodbg_begin(Def), MRI->use_nodbg_end()) > 1)
1680 report("Unspillable Terminator expected to have at most one use!", MI);
1681 }
1682
1683 // A fully-formed DBG_VALUE must have a location. Ignore partially formed
1684 // DBG_VALUEs: these are convenient to use in tests, but should never get
1685 // generated.
1686 if (MI->isDebugValue() && MI->getNumOperands() == 4)
1687 if (!MI->getDebugLoc())
1688 report("Missing DebugLoc for debug instruction", MI);
1689
1690 // Meta instructions should never be the subject of debug value tracking,
1691 // they don't create a value in the output program at all.
1692 if (MI->isMetaInstruction() && MI->peekDebugInstrNum())
1693 report("Metadata instruction should not have a value tracking number", MI);
1694
1695 // Check the MachineMemOperands for basic consistency.
1696 for (MachineMemOperand *Op : MI->memoperands()) {
1697 if (Op->isLoad() && !MI->mayLoad())
1698 report("Missing mayLoad flag", MI);
1699 if (Op->isStore() && !MI->mayStore())
1700 report("Missing mayStore flag", MI);
1701 }
1702
1703 // Debug values must not have a slot index.
1704 // Other instructions must have one, unless they are inside a bundle.
1705 if (LiveInts) {
1706 bool mapped = !LiveInts->isNotInMIMap(*MI);
1707 if (MI->isDebugOrPseudoInstr()) {
1708 if (mapped)
1709 report("Debug instruction has a slot index", MI);
1710 } else if (MI->isInsideBundle()) {
1711 if (mapped)
1712 report("Instruction inside bundle has a slot index", MI);
1713 } else {
1714 if (!mapped)
1715 report("Missing slot index", MI);
1716 }
1717 }
1718
1719 unsigned Opc = MCID.getOpcode();
1720 if (isPreISelGenericOpcode(Opc) || isPreISelGenericOptimizationHint(Opc)) {
1721 verifyPreISelGenericInstruction(MI);
1722 return;
1723 }
1724
1725 StringRef ErrorInfo;
1726 if (!TII->verifyInstruction(*MI, ErrorInfo))
1727 report(ErrorInfo.data(), MI);
1728
1729 // Verify properties of various specific instruction types
1730 switch (MI->getOpcode()) {
1731 case TargetOpcode::COPY: {
1732 const MachineOperand &DstOp = MI->getOperand(0);
1733 const MachineOperand &SrcOp = MI->getOperand(1);
1734 const Register SrcReg = SrcOp.getReg();
1735 const Register DstReg = DstOp.getReg();
1736
1737 LLT DstTy = MRI->getType(DstReg);
1738 LLT SrcTy = MRI->getType(SrcReg);
1739 if (SrcTy.isValid() && DstTy.isValid()) {
1740 // If both types are valid, check that the types are the same.
1741 if (SrcTy != DstTy) {
1742 report("Copy Instruction is illegal with mismatching types", MI);
1743 errs() << "Def = " << DstTy << ", Src = " << SrcTy << "\n";
1744 }
1745
1746 break;
1747 }
1748
1749 if (!SrcTy.isValid() && !DstTy.isValid())
1750 break;
1751
1752 // If we have only one valid type, this is likely a copy between a virtual
1753 // and physical register.
1754 unsigned SrcSize = 0;
1755 unsigned DstSize = 0;
1756 if (SrcReg.isPhysical() && DstTy.isValid()) {
1757 const TargetRegisterClass *SrcRC =
1758 TRI->getMinimalPhysRegClassLLT(SrcReg, DstTy);
1759 if (SrcRC)
1760 SrcSize = TRI->getRegSizeInBits(*SrcRC);
1761 }
1762
1763 if (SrcSize == 0)
1764 SrcSize = TRI->getRegSizeInBits(SrcReg, *MRI);
1765
1766 if (DstReg.isPhysical() && SrcTy.isValid()) {
1767 const TargetRegisterClass *DstRC =
1768 TRI->getMinimalPhysRegClassLLT(DstReg, SrcTy);
1769 if (DstRC)
1770 DstSize = TRI->getRegSizeInBits(*DstRC);
1771 }
1772
1773 if (DstSize == 0)
1774 DstSize = TRI->getRegSizeInBits(DstReg, *MRI);
1775
1776 if (SrcSize != 0 && DstSize != 0 && SrcSize != DstSize) {
1777 if (!DstOp.getSubReg() && !SrcOp.getSubReg()) {
1778 report("Copy Instruction is illegal with mismatching sizes", MI);
1779 errs() << "Def Size = " << DstSize << ", Src Size = " << SrcSize
1780 << "\n";
1781 }
1782 }
1783 break;
1784 }
1785 case TargetOpcode::STATEPOINT: {
1786 StatepointOpers SO(MI);
1787 if (!MI->getOperand(SO.getIDPos()).isImm() ||
1788 !MI->getOperand(SO.getNBytesPos()).isImm() ||
1789 !MI->getOperand(SO.getNCallArgsPos()).isImm()) {
1790 report("meta operands to STATEPOINT not constant!", MI);
1791 break;
1792 }
1793
1794 auto VerifyStackMapConstant = [&](unsigned Offset) {
1795 if (Offset >= MI->getNumOperands()) {
1796 report("stack map constant to STATEPOINT is out of range!", MI);
1797 return;
1798 }
1799 if (!MI->getOperand(Offset - 1).isImm() ||
1800 MI->getOperand(Offset - 1).getImm() != StackMaps::ConstantOp ||
1801 !MI->getOperand(Offset).isImm())
1802 report("stack map constant to STATEPOINT not well formed!", MI);
1803 };
1804 VerifyStackMapConstant(SO.getCCIdx());
1805 VerifyStackMapConstant(SO.getFlagsIdx());
1806 VerifyStackMapConstant(SO.getNumDeoptArgsIdx());
1807 VerifyStackMapConstant(SO.getNumGCPtrIdx());
1808 VerifyStackMapConstant(SO.getNumAllocaIdx());
1809 VerifyStackMapConstant(SO.getNumGcMapEntriesIdx());
1810
1811 // Verify that all explicit statepoint defs are tied to gc operands as
1812 // they are expected to be a relocation of gc operands.
1813 unsigned FirstGCPtrIdx = SO.getFirstGCPtrIdx();
1814 unsigned LastGCPtrIdx = SO.getNumAllocaIdx() - 2;
1815 for (unsigned Idx = 0; Idx < MI->getNumDefs(); Idx++) {
1816 unsigned UseOpIdx;
1817 if (!MI->isRegTiedToUseOperand(Idx, &UseOpIdx)) {
1818 report("STATEPOINT defs expected to be tied", MI);
1819 break;
1820 }
1821 if (UseOpIdx < FirstGCPtrIdx || UseOpIdx > LastGCPtrIdx) {
1822 report("STATEPOINT def tied to non-gc operand", MI);
1823 break;
1824 }
1825 }
1826
1827 // TODO: verify we have properly encoded deopt arguments
1828 } break;
1829 case TargetOpcode::INSERT_SUBREG: {
1830 unsigned InsertedSize;
1831 if (unsigned SubIdx = MI->getOperand(2).getSubReg())
1832 InsertedSize = TRI->getSubRegIdxSize(SubIdx);
1833 else
1834 InsertedSize = TRI->getRegSizeInBits(MI->getOperand(2).getReg(), *MRI);
1835 unsigned SubRegSize = TRI->getSubRegIdxSize(MI->getOperand(3).getImm());
1836 if (SubRegSize < InsertedSize) {
1837 report("INSERT_SUBREG expected inserted value to have equal or lesser "
1838 "size than the subreg it was inserted into", MI);
1839 break;
1840 }
1841 } break;
1842 }
1843}
1844
1845void
1846MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
1847 const MachineInstr *MI = MO->getParent();
1848 const MCInstrDesc &MCID = MI->getDesc();
1849 unsigned NumDefs = MCID.getNumDefs();
1850 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
1
Assuming the condition is false
2
Taking false branch
1851 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
1852
1853 // The first MCID.NumDefs operands must be explicit register defines
1854 if (MONum < NumDefs) {
3
Assuming 'MONum' is < 'NumDefs'
4
Taking true branch
1855 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
1856 if (!MO->isReg())
1857 report("Explicit definition must be a register", MO, MONum);
1858 else if (!MO->isDef() && !MCOI.isOptionalDef())
5
Assuming the condition is false
1859 report("Explicit definition marked as use", MO, MONum);
1860 else if (MO->isImplicit())
6
Assuming the condition is false
7
Taking false branch
1861 report("Explicit definition marked as implicit", MO, MONum);
1862 } else if (MONum < MCID.getNumOperands()) {
1863 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
1864 // Don't check if it's the last operand in a variadic instruction. See,
1865 // e.g., LDM_RET in the arm back end. Check non-variadic operands only.
1866 bool IsOptional = MI->isVariadic() && MONum == MCID.getNumOperands() - 1;
1867 if (!IsOptional) {
1868 if (MO->isReg()) {
1869 if (MO->isDef() && !MCOI.isOptionalDef() && !MCID.variadicOpsAreDefs())
1870 report("Explicit operand marked as def", MO, MONum);
1871 if (MO->isImplicit())
1872 report("Explicit operand marked as implicit", MO, MONum);
1873 }
1874
1875 // Check that an instruction has register operands only as expected.
1876 if (MCOI.OperandType == MCOI::OPERAND_REGISTER &&
1877 !MO->isReg() && !MO->isFI())
1878 report("Expected a register operand.", MO, MONum);
1879 if (MO->isReg()) {
1880 if (MCOI.OperandType == MCOI::OPERAND_IMMEDIATE ||
1881 (MCOI.OperandType == MCOI::OPERAND_PCREL &&
1882 !TII->isPCRelRegisterOperandLegal(*MO)))
1883 report("Expected a non-register operand.", MO, MONum);
1884 }
1885 }
1886
1887 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
1888 if (TiedTo != -1) {
1889 if (!MO->isReg())
1890 report("Tied use must be a register", MO, MONum);
1891 else if (!MO->isTied())
1892 report("Operand should be tied", MO, MONum);
1893 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
1894 report("Tied def doesn't match MCInstrDesc", MO, MONum);
1895 else if (Register::isPhysicalRegister(MO->getReg())) {
1896 const MachineOperand &MOTied = MI->getOperand(TiedTo);
1897 if (!MOTied.isReg())
1898 report("Tied counterpart must be a register", &MOTied, TiedTo);
1899 else if (Register::isPhysicalRegister(MOTied.getReg()) &&
1900 MO->getReg() != MOTied.getReg())
1901 report("Tied physical registers must match.", &MOTied, TiedTo);
1902 }
1903 } else if (MO->isReg() && MO->isTied())
1904 report("Explicit operand should not be tied", MO, MONum);
1905 } else {
1906 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
1907 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
1908 report("Extra explicit operand on non-variadic instruction", MO, MONum);
1909 }
1910
1911 switch (MO->getType()) {
8
Control jumps to 'case MO_Register:' at line 1912
1912 case MachineOperand::MO_Register: {
1913 // Verify debug flag on debug instructions. Check this first because reg0
1914 // indicates an undefined debug value.
1915 if (MI->isDebugInstr() && MO->isUse()) {
1916 if (!MO->isDebug())
1917 report("Register operand must be marked debug", MO, MONum);
1918 } else if (MO->isDebug()) {
9
Assuming the condition is false
10
Taking false branch
1919 report("Register operand must not be marked debug", MO, MONum);
1920 }
1921
1922 const Register Reg = MO->getReg();
1923 if (!Reg)
11
Assuming the condition is false
1924 return;
1925 if (MRI->tracksLiveness() && !MI->isDebugInstr())
12
Taking true branch
1926 checkLiveness(MO, MONum);
13
Calling 'MachineVerifier::checkLiveness'
1927
1928 if (MO->isDef() && MO->isUndef() && !MO->getSubReg() &&
1929 MO->getReg().isVirtual()) // TODO: Apply to physregs too
1930 report("Undef virtual register def operands require a subregister", MO, MONum);
1931
1932 // Verify the consistency of tied operands.
1933 if (MO->isTied()) {
1934 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
1935 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
1936 if (!OtherMO.isReg())
1937 report("Must be tied to a register", MO, MONum);
1938 if (!OtherMO.isTied())
1939 report("Missing tie flags on tied operand", MO, MONum);
1940 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
1941 report("Inconsistent tie links", MO, MONum);
1942 if (MONum < MCID.getNumDefs()) {
1943 if (OtherIdx < MCID.getNumOperands()) {
1944 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
1945 report("Explicit def tied to explicit use without tie constraint",
1946 MO, MONum);
1947 } else {
1948 if (!OtherMO.isImplicit())
1949 report("Explicit def should be tied to implicit use", MO, MONum);
1950 }
1951 }
1952 }
1953
1954 // Verify two-address constraints after the twoaddressinstruction pass.
1955 // Both twoaddressinstruction pass and phi-node-elimination pass call
1956 // MRI->leaveSSA() to set MF as NoSSA, we should do the verification after
1957 // twoaddressinstruction pass not after phi-node-elimination pass. So we
1958 // shouldn't use the NoSSA as the condition, we should based on
1959 // TiedOpsRewritten property to verify two-address constraints, this
1960 // property will be set in twoaddressinstruction pass.
1961 unsigned DefIdx;
1962 if (MF->getProperties().hasProperty(
1963 MachineFunctionProperties::Property::TiedOpsRewritten) &&
1964 MO->isUse() && MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
1965 Reg != MI->getOperand(DefIdx).getReg())
1966 report("Two-address instruction operands must be identical", MO, MONum);
1967
1968 // Check register classes.
1969 unsigned SubIdx = MO->getSubReg();
1970
1971 if (Register::isPhysicalRegister(Reg)) {
1972 if (SubIdx) {
1973 report("Illegal subregister index for physical register", MO, MONum);
1974 return;
1975 }
1976 if (MONum < MCID.getNumOperands()) {
1977 if (const TargetRegisterClass *DRC =
1978 TII->getRegClass(MCID, MONum, TRI, *MF)) {
1979 if (!DRC->contains(Reg)) {
1980 report("Illegal physical register for instruction", MO, MONum);
1981 errs() << printReg(Reg, TRI) << " is not a "
1982 << TRI->getRegClassName(DRC) << " register.\n";
1983 }
1984 }
1985 }
1986 if (MO->isRenamable()) {
1987 if (MRI->isReserved(Reg)) {
1988 report("isRenamable set on reserved register", MO, MONum);
1989 return;
1990 }
1991 }
1992 } else {
1993 // Virtual register.
1994 const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
1995 if (!RC) {
1996 // This is a generic virtual register.
1997
1998 // Do not allow undef uses for generic virtual registers. This ensures
1999 // getVRegDef can never fail and return null on a generic register.
2000 //
2001 // FIXME: This restriction should probably be broadened to all SSA
2002 // MIR. However, DetectDeadLanes/ProcessImplicitDefs technically still
2003 // run on the SSA function just before phi elimination.
2004 if (MO->isUndef())
2005 report("Generic virtual register use cannot be undef", MO, MONum);
2006
2007 // Debug value instruction is permitted to use undefined vregs.
2008 // This is a performance measure to skip the overhead of immediately
2009 // pruning unused debug operands. The final undef substitution occurs
2010 // when debug values are allocated in LDVImpl::handleDebugValue, so
2011 // these verifications always apply after this pass.
2012 if (isFunctionTracksDebugUserValues || !MO->isUse() ||
2013 !MI->isDebugValue() || !MRI->def_empty(Reg)) {
2014 // If we're post-Select, we can't have gvregs anymore.
2015 if (isFunctionSelected) {
2016 report("Generic virtual register invalid in a Selected function",
2017 MO, MONum);
2018 return;
2019 }
2020
2021 // The gvreg must have a type and it must not have a SubIdx.
2022 LLT Ty = MRI->getType(Reg);
2023 if (!Ty.isValid()) {
2024 report("Generic virtual register must have a valid type", MO,
2025 MONum);
2026 return;
2027 }
2028
2029 const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
2030
2031 // If we're post-RegBankSelect, the gvreg must have a bank.
2032 if (!RegBank && isFunctionRegBankSelected) {
2033 report("Generic virtual register must have a bank in a "
2034 "RegBankSelected function",
2035 MO, MONum);
2036 return;
2037 }
2038
2039 // Make sure the register fits into its register bank if any.
2040 if (RegBank && Ty.isValid() &&
2041 RegBank->getSize() < Ty.getSizeInBits()) {
2042 report("Register bank is too small for virtual register", MO,
2043 MONum);
2044 errs() << "Register bank " << RegBank->getName() << " too small("
2045 << RegBank->getSize() << ") to fit " << Ty.getSizeInBits()
2046 << "-bits\n";
2047 return;
2048 }
2049 }
2050
2051 if (SubIdx) {
2052 report("Generic virtual register does not allow subregister index", MO,
2053 MONum);
2054 return;
2055 }
2056
2057 // If this is a target specific instruction and this operand
2058 // has register class constraint, the virtual register must
2059 // comply to it.
2060 if (!isPreISelGenericOpcode(MCID.getOpcode()) &&
2061 MONum < MCID.getNumOperands() &&
2062 TII->getRegClass(MCID, MONum, TRI, *MF)) {
2063 report("Virtual register does not match instruction constraint", MO,
2064 MONum);
2065 errs() << "Expect register class "
2066 << TRI->getRegClassName(
2067 TII->getRegClass(MCID, MONum, TRI, *MF))
2068 << " but got nothing\n";
2069 return;
2070 }
2071
2072 break;
2073 }
2074 if (SubIdx) {
2075 const TargetRegisterClass *SRC =
2076 TRI->getSubClassWithSubReg(RC, SubIdx);
2077 if (!SRC) {
2078 report("Invalid subregister index for virtual register", MO, MONum);
2079 errs() << "Register class " << TRI->getRegClassName(RC)
2080 << " does not support subreg index " << SubIdx << "\n";
2081 return;
2082 }
2083 if (RC != SRC) {
2084 report("Invalid register class for subregister index", MO, MONum);
2085 errs() << "Register class " << TRI->getRegClassName(RC)
2086 << " does not fully support subreg index " << SubIdx << "\n";
2087 return;
2088 }
2089 }
2090 if (MONum < MCID.getNumOperands()) {
2091 if (const TargetRegisterClass *DRC =
2092 TII->getRegClass(MCID, MONum, TRI, *MF)) {
2093 if (SubIdx) {
2094 const TargetRegisterClass *SuperRC =
2095 TRI->getLargestLegalSuperClass(RC, *MF);
2096 if (!SuperRC) {
2097 report("No largest legal super class exists.", MO, MONum);
2098 return;
2099 }
2100 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
2101 if (!DRC) {
2102 report("No matching super-reg register class.", MO, MONum);
2103 return;
2104 }
2105 }
2106 if (!RC->hasSuperClassEq(DRC)) {
2107 report("Illegal virtual register for instruction", MO, MONum);
2108 errs() << "Expected a " << TRI->getRegClassName(DRC)
2109 << " register, but got a " << TRI->getRegClassName(RC)
2110 << " register\n";
2111 }
2112 }
2113 }
2114 }
2115 break;
2116 }
2117
2118 case MachineOperand::MO_RegisterMask:
2119 regMasks.push_back(MO->getRegMask());
2120 break;
2121
2122 case MachineOperand::MO_MachineBasicBlock:
2123 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
2124 report("PHI operand is not in the CFG", MO, MONum);
2125 break;
2126
2127 case MachineOperand::MO_FrameIndex:
2128 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
2129 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
2130 int FI = MO->getIndex();
2131 LiveInterval &LI = LiveStks->getInterval(FI);
2132 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
2133
2134 bool stores = MI->mayStore();
2135 bool loads = MI->mayLoad();
2136 // For a memory-to-memory move, we need to check if the frame
2137 // index is used for storing or loading, by inspecting the
2138 // memory operands.
2139 if (stores && loads) {
2140 for (auto *MMO : MI->memoperands()) {
2141 const PseudoSourceValue *PSV = MMO->getPseudoValue();
2142 if (PSV == nullptr) continue;
2143 const FixedStackPseudoSourceValue *Value =
2144 dyn_cast<FixedStackPseudoSourceValue>(PSV);
2145 if (Value == nullptr) continue;
2146 if (Value->getFrameIndex() != FI) continue;
2147
2148 if (MMO->isStore())
2149 loads = false;
2150 else
2151 stores = false;
2152 break;
2153 }
2154 if (loads == stores)
2155 report("Missing fixed stack memoperand.", MI);
2156 }
2157 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
2158 report("Instruction loads from dead spill slot", MO, MONum);
2159 errs() << "Live stack: " << LI << '\n';
2160 }
2161 if (stores && !LI.liveAt(Idx.getRegSlot())) {
2162 report("Instruction stores to dead spill slot", MO, MONum);
2163 errs() << "Live stack: " << LI << '\n';
2164 }
2165 }
2166 break;
2167
2168 default:
2169 break;
2170 }
2171}
2172
2173void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
2174 unsigned MONum, SlotIndex UseIdx,
2175 const LiveRange &LR,
2176 Register VRegOrUnit,
2177 LaneBitmask LaneMask) {
2178 LiveQueryResult LRQ = LR.Query(UseIdx);
2179 // Check if we have a segment at the use, note however that we only need one
2180 // live subregister range, the others may be dead.
2181 if (!LRQ.valueIn() && LaneMask.none()) {
2182 report("No live segment at use", MO, MONum);
2183 report_context_liverange(LR);
2184 report_context_vreg_regunit(VRegOrUnit);
2185 report_context(UseIdx);
2186 }
2187 if (MO->isKill() && !LRQ.isKill()) {
2188 report("Live range continues after kill flag", MO, MONum);
2189 report_context_liverange(LR);
2190 report_context_vreg_regunit(VRegOrUnit);
2191 if (LaneMask.any())
2192 report_context_lanemask(LaneMask);
2193 report_context(UseIdx);
2194 }
2195}
2196
2197void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
2198 unsigned MONum, SlotIndex DefIdx,
2199 const LiveRange &LR,
2200 Register VRegOrUnit,
2201 bool SubRangeCheck,
2202 LaneBitmask LaneMask) {
2203 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
2204 assert(VNI && "NULL valno is not allowed")(static_cast <bool> (VNI && "NULL valno is not allowed"
) ? void (0) : __assert_fail ("VNI && \"NULL valno is not allowed\""
, "llvm/lib/CodeGen/MachineVerifier.cpp", 2204, __extension__
__PRETTY_FUNCTION__))
;
2205 if (VNI->def != DefIdx) {
2206 report("Inconsistent valno->def", MO, MONum);
2207 report_context_liverange(LR);
2208 report_context_vreg_regunit(VRegOrUnit);
2209 if (LaneMask.any())
2210 report_context_lanemask(LaneMask);
2211 report_context(*VNI);
2212 report_context(DefIdx);
2213 }
2214 } else {
2215 report("No live segment at def", MO, MONum);
2216 report_context_liverange(LR);
2217 report_context_vreg_regunit(VRegOrUnit);
2218 if (LaneMask.any())
2219 report_context_lanemask(LaneMask);
2220 report_context(DefIdx);
2221 }
2222 // Check that, if the dead def flag is present, LiveInts agree.
2223 if (MO->isDead()) {
2224 LiveQueryResult LRQ = LR.Query(DefIdx);
2225 if (!LRQ.isDeadDef()) {
2226 assert(Register::isVirtualRegister(VRegOrUnit) &&(static_cast <bool> (Register::isVirtualRegister(VRegOrUnit
) && "Expecting a virtual register.") ? void (0) : __assert_fail
("Register::isVirtualRegister(VRegOrUnit) && \"Expecting a virtual register.\""
, "llvm/lib/CodeGen/MachineVerifier.cpp", 2227, __extension__
__PRETTY_FUNCTION__))
2227 "Expecting a virtual register.")(static_cast <bool> (Register::isVirtualRegister(VRegOrUnit
) && "Expecting a virtual register.") ? void (0) : __assert_fail
("Register::isVirtualRegister(VRegOrUnit) && \"Expecting a virtual register.\""
, "llvm/lib/CodeGen/MachineVerifier.cpp", 2227, __extension__
__PRETTY_FUNCTION__))
;
2228 // A dead subreg def only tells us that the specific subreg is dead. There
2229 // could be other non-dead defs of other subregs, or we could have other
2230 // parts of the register being live through the instruction. So unless we
2231 // are checking liveness for a subrange it is ok for the live range to
2232 // continue, given that we have a dead def of a subregister.
2233 if (SubRangeCheck || MO->getSubReg() == 0) {
2234 report("Live range continues after dead def flag", MO, MONum);
2235 report_context_liverange(LR);
2236 report_context_vreg_regunit(VRegOrUnit);
2237 if (LaneMask.any())
2238 report_context_lanemask(LaneMask);
2239 }
2240 }
2241 }
2242}
2243
2244void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
2245 const MachineInstr *MI = MO->getParent();
2246 const Register Reg = MO->getReg();
2247 const unsigned SubRegIdx = MO->getSubReg();
2248
2249 const LiveInterval *LI = nullptr;
14
'LI' initialized to a null pointer value
2250 if (LiveInts && Reg.isVirtual()) {
15
Assuming field 'LiveInts' is null
2251 if (LiveInts->hasInterval(Reg)) {
2252 LI = &LiveInts->getInterval(Reg);
2253 if (SubRegIdx != 0 && (MO->isDef() || !MO->isUndef()) && !LI->empty() &&
2254 !LI->hasSubRanges() && MRI->shouldTrackSubRegLiveness(Reg))
2255 report("Live interval for subreg operand has no subranges", MO, MONum);
2256 } else {
2257 report("Virtual register has no live interval", MO, MONum);
2258 }
2259 }
2260
2261 // Both use and def operands can read a register.
2262 if (MO->readsReg()) {
16
Taking false branch
2263 if (MO->isKill())
2264 addRegWithSubRegs(regsKilled, Reg);
2265
2266 // Check that LiveVars knows this kill (unless we are inside a bundle, in
2267 // which case we have already checked that LiveVars knows any kills on the
2268 // bundle header instead).
2269 if (LiveVars && Reg.isVirtual() && MO->isKill() &&
2270 !MI->isBundledWithPred()) {
2271 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
2272 if (!is_contained(VI.Kills, MI))
2273 report("Kill missing from LiveVariables", MO, MONum);
2274 }
2275
2276 // Check LiveInts liveness and kill.
2277 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
2278 SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
2279 // Check the cached regunit intervals.
2280 if (Reg.isPhysical() && !isReserved(Reg)) {
2281 for (MCRegUnitIterator Units(Reg.asMCReg(), TRI); Units.isValid();
2282 ++Units) {
2283 if (MRI->isReservedRegUnit(*Units))
2284 continue;
2285 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
2286 checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
2287 }
2288 }
2289
2290 if (Reg.isVirtual()) {
2291 // This is a virtual register interval.
2292 checkLivenessAtUse(MO, MONum, UseIdx, *LI, Reg);
2293
2294 if (LI->hasSubRanges() && !MO->isDef()) {
2295 LaneBitmask MOMask = SubRegIdx != 0
2296 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
2297 : MRI->getMaxLaneMaskForVReg(Reg);
2298 LaneBitmask LiveInMask;
2299 for (const LiveInterval::SubRange &SR : LI->subranges()) {
2300 if ((MOMask & SR.LaneMask).none())
2301 continue;
2302 checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
2303 LiveQueryResult LRQ = SR.Query(UseIdx);
2304 if (LRQ.valueIn())
2305 LiveInMask |= SR.LaneMask;
2306 }
2307 // At least parts of the register has to be live at the use.
2308 if ((LiveInMask & MOMask).none()) {
2309 report("No live subrange at use", MO, MONum);
2310 report_context(*LI);
2311 report_context(UseIdx);
2312 }
2313 }
2314 }
2315 }
2316
2317 // Use of a dead register.
2318 if (!regsLive.count(Reg)) {
2319 if (Reg.isPhysical()) {
2320 // Reserved registers may be used even when 'dead'.
2321 bool Bad = !isReserved(Reg);
2322 // We are fine if just any subregister has a defined value.
2323 if (Bad) {
2324
2325 for (const MCPhysReg &SubReg : TRI->subregs(Reg)) {
2326 if (regsLive.count(SubReg)) {
2327 Bad = false;
2328 break;
2329 }
2330 }
2331 }
2332 // If there is an additional implicit-use of a super register we stop
2333 // here. By definition we are fine if the super register is not
2334 // (completely) dead, if the complete super register is dead we will
2335 // get a report for its operand.
2336 if (Bad) {
2337 for (const MachineOperand &MOP : MI->uses()) {
2338 if (!MOP.isReg() || !MOP.isImplicit())
2339 continue;
2340
2341 if (!MOP.getReg().isPhysical())
2342 continue;
2343
2344 if (llvm::is_contained(TRI->subregs(MOP.getReg()), Reg))
2345 Bad = false;
2346 }
2347 }
2348 if (Bad)
2349 report("Using an undefined physical register", MO, MONum);
2350 } else if (MRI->def_empty(Reg)) {
2351 report("Reading virtual register without a def", MO, MONum);
2352 } else {
2353 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
2354 // We don't know which virtual registers are live in, so only complain
2355 // if vreg was killed in this MBB. Otherwise keep track of vregs that
2356 // must be live in. PHI instructions are handled separately.
2357 if (MInfo.regsKilled.count(Reg))
2358 report("Using a killed virtual register", MO, MONum);
2359 else if (!MI->isPHI())
2360 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
2361 }
2362 }
2363 }
2364
2365 if (MO->isDef()) {
17
Taking true branch
2366 // Register defined.
2367 // TODO: verify that earlyclobber ops are not used.
2368 if (MO->isDead())
18
Assuming the condition is false
19
Taking false branch
2369 addRegWithSubRegs(regsDead, Reg);
2370 else
2371 addRegWithSubRegs(regsDefined, Reg);
20
Calling 'MachineVerifier::addRegWithSubRegs'
24
Returning from 'MachineVerifier::addRegWithSubRegs'
2372
2373 // Verify SSA form.
2374 if (MRI->isSSA() && Reg.isVirtual() &&
2375 std::next(MRI->def_begin(Reg)) != MRI->def_end())
2376 report("Multiple virtual register defs in SSA form", MO, MONum);
2377
2378 // Check LiveInts for a live segment, but only for virtual registers.
2379 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
25
Assuming field 'LiveInts' is non-null
26
Taking true branch
2380 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
2381 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
2382
2383 if (Reg.isVirtual()) {
27
Taking true branch
2384 checkLivenessAtDef(MO, MONum, DefIdx, *LI, Reg);
28
Forming reference to null pointer
2385
2386 if (LI->hasSubRanges()) {
2387 LaneBitmask MOMask = SubRegIdx != 0
2388 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
2389 : MRI->getMaxLaneMaskForVReg(Reg);
2390 for (const LiveInterval::SubRange &SR : LI->subranges()) {
2391 if ((SR.LaneMask & MOMask).none())
2392 continue;
2393 checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, true, SR.LaneMask);
2394 }
2395 }
2396 }
2397 }
2398 }
2399}
2400
2401// This function gets called after visiting all instructions in a bundle. The
2402// argument points to the bundle header.
2403// Normal stand-alone instructions are also considered 'bundles', and this
2404// function is called for all of them.
2405void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
2406 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
2407 set_union(MInfo.regsKilled, regsKilled);
2408 set_subtract(regsLive, regsKilled); regsKilled.clear();
2409 // Kill any masked registers.
2410 while (!regMasks.empty()) {
2411 const uint32_t *Mask = regMasks.pop_back_val();
2412 for (Register Reg : regsLive)
2413 if (Reg.isPhysical() &&
2414 MachineOperand::clobbersPhysReg(Mask, Reg.asMCReg()))
2415 regsDead.push_back(Reg);
2416 }
2417 set_subtract(regsLive, regsDead); regsDead.clear();
2418 set_union(regsLive, regsDefined); regsDefined.clear();
2419}
2420
2421void
2422MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
2423 MBBInfoMap[MBB].regsLiveOut = regsLive;
2424 regsLive.clear();
2425
2426 if (Indexes) {
2427 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
2428 if (!(stop > lastIndex)) {
2429 report("Block ends before last instruction index", MBB);
2430 errs() << "Block ends at " << stop
2431 << " last instruction was at " << lastIndex << '\n';
2432 }
2433 lastIndex = stop;
2434 }
2435}
2436
2437namespace {
2438// This implements a set of registers that serves as a filter: can filter other
2439// sets by passing through elements not in the filter and blocking those that
2440// are. Any filter implicitly includes the full set of physical registers upon
2441// creation, thus filtering them all out. The filter itself as a set only grows,
2442// and needs to be as efficient as possible.
2443struct VRegFilter {
2444 // Add elements to the filter itself. \pre Input set \p FromRegSet must have
2445 // no duplicates. Both virtual and physical registers are fine.
2446 template <typename RegSetT> void add(const RegSetT &FromRegSet) {
2447 SmallVector<Register, 0> VRegsBuffer;
2448 filterAndAdd(FromRegSet, VRegsBuffer);
2449 }
2450 // Filter \p FromRegSet through the filter and append passed elements into \p
2451 // ToVRegs. All elements appended are then added to the filter itself.
2452 // \returns true if anything changed.
2453 template <typename RegSetT>
2454 bool filterAndAdd(const RegSetT &FromRegSet,
2455 SmallVectorImpl<Register> &ToVRegs) {
2456 unsigned SparseUniverse = Sparse.size();
2457 unsigned NewSparseUniverse = SparseUniverse;
2458 unsigned NewDenseSize = Dense.size();
2459 size_t Begin = ToVRegs.size();
2460 for (Register Reg : FromRegSet) {
2461 if (!Reg.isVirtual())
2462 continue;
2463 unsigned Index = Register::virtReg2Index(Reg);
2464 if (Index < SparseUniverseMax) {
2465 if (Index < SparseUniverse && Sparse.test(Index))
2466 continue;
2467 NewSparseUniverse = std::max(NewSparseUniverse, Index + 1);
2468 } else {
2469 if (Dense.count(Reg))
2470 continue;
2471 ++NewDenseSize;
2472 }
2473 ToVRegs.push_back(Reg);
2474 }
2475 size_t End = ToVRegs.size();
2476 if (Begin == End)
2477 return false;
2478 // Reserving space in sets once performs better than doing so continuously
2479 // and pays easily for double look-ups (even in Dense with SparseUniverseMax
2480 // tuned all the way down) and double iteration (the second one is over a
2481 // SmallVector, which is a lot cheaper compared to DenseSet or BitVector).
2482 Sparse.resize(NewSparseUniverse);
2483 Dense.reserve(NewDenseSize);
2484 for (unsigned I = Begin; I < End; ++I) {
2485 Register Reg = ToVRegs[I];
2486 unsigned Index = Register::virtReg2Index(Reg);
2487 if (Index < SparseUniverseMax)
2488 Sparse.set(Index);
2489 else
2490 Dense.insert(Reg);
2491 }
2492 return true;
2493 }
2494
2495private:
2496 static constexpr unsigned SparseUniverseMax = 10 * 1024 * 8;
2497 // VRegs indexed within SparseUniverseMax are tracked by Sparse, those beyound
2498 // are tracked by Dense. The only purpose of the threashold and the Dense set
2499 // is to have a reasonably growing memory usage in pathological cases (large
2500 // number of very sparse VRegFilter instances live at the same time). In
2501 // practice even in the worst-by-execution time cases having all elements
2502 // tracked by Sparse (very large SparseUniverseMax scenario) tends to be more
2503 // space efficient than if tracked by Dense. The threashold is set to keep the
2504 // worst-case memory usage within 2x of figures determined empirically for
2505 // "all Dense" scenario in such worst-by-execution-time cases.
2506 BitVector Sparse;
2507 DenseSet<unsigned> Dense;
2508};
2509
2510// Implements both a transfer function and a (binary, in-place) join operator
2511// for a dataflow over register sets with set union join and filtering transfer
2512// (out_b = in_b \ filter_b). filter_b is expected to be set-up ahead of time.
2513// Maintains out_b as its state, allowing for O(n) iteration over it at any
2514// time, where n is the size of the set (as opposed to O(U) where U is the
2515// universe). filter_b implicitly contains all physical registers at all times.
2516class FilteringVRegSet {
2517 VRegFilter Filter;
2518 SmallVector<Register, 0> VRegs;
2519
2520public:
2521 // Set-up the filter_b. \pre Input register set \p RS must have no duplicates.
2522 // Both virtual and physical registers are fine.
2523 template <typename RegSetT> void addToFilter(const RegSetT &RS) {
2524 Filter.add(RS);
2525 }
2526 // Passes \p RS through the filter_b (transfer function) and adds what's left
2527 // to itself (out_b).
2528 template <typename RegSetT> bool add(const RegSetT &RS) {
2529 // Double-duty the Filter: to maintain VRegs a set (and the join operation
2530 // a set union) just add everything being added here to the Filter as well.
2531 return Filter.filterAndAdd(RS, VRegs);
2532 }
2533 using const_iterator = decltype(VRegs)::const_iterator;
2534 const_iterator begin() const { return VRegs.begin(); }
2535 const_iterator end() const { return VRegs.end(); }
2536 size_t size() const { return VRegs.size(); }
2537};
2538} // namespace
2539
2540// Calculate the largest possible vregsPassed sets. These are the registers that
2541// can pass through an MBB live, but may not be live every time. It is assumed
2542// that all vregsPassed sets are empty before the call.
2543void MachineVerifier::calcRegsPassed() {
2544 if (MF->empty())
2545 // ReversePostOrderTraversal doesn't handle empty functions.
2546 return;
2547
2548 for (const MachineBasicBlock *MB :
2549 ReversePostOrderTraversal<const MachineFunction *>(MF)) {
2550 FilteringVRegSet VRegs;
2551 BBInfo &Info = MBBInfoMap[MB];
2552 assert(Info.reachable)(static_cast <bool> (Info.reachable) ? void (0) : __assert_fail
("Info.reachable", "llvm/lib/CodeGen/MachineVerifier.cpp", 2552
, __extension__ __PRETTY_FUNCTION__))
;
2553
2554 VRegs.addToFilter(Info.regsKilled);
2555 VRegs.addToFilter(Info.regsLiveOut);
2556 for (const MachineBasicBlock *Pred : MB->predecessors()) {
2557 const BBInfo &PredInfo = MBBInfoMap[Pred];
2558 if (!PredInfo.reachable)
2559 continue;
2560
2561 VRegs.add(PredInfo.regsLiveOut);
2562 VRegs.add(PredInfo.vregsPassed);
2563 }
2564 Info.vregsPassed.reserve(VRegs.size());
2565 Info.vregsPassed.insert(VRegs.begin(), VRegs.end());
2566 }
2567}
2568
2569// Calculate the set of virtual registers that must be passed through each basic
2570// block in order to satisfy the requirements of successor blocks. This is very
2571// similar to calcRegsPassed, only backwards.
2572void MachineVerifier::calcRegsRequired() {
2573 // First push live-in regs to predecessors' vregsRequired.
2574 SmallPtrSet<const MachineBasicBlock*, 8> todo;
2575 for (const auto &MBB : *MF) {
2576 BBInfo &MInfo = MBBInfoMap[&MBB];
2577 for (const MachineBasicBlock *Pred : MBB.predecessors()) {
2578 BBInfo &PInfo = MBBInfoMap[Pred];
2579 if (PInfo.addRequired(MInfo.vregsLiveIn))
2580 todo.insert(Pred);
2581 }
2582
2583 // Handle the PHI node.
2584 for (const MachineInstr &MI : MBB.phis()) {
2585 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
2586 // Skip those Operands which are undef regs or not regs.
2587 if (!MI.getOperand(i).isReg() || !MI.getOperand(i).readsReg())
2588 continue;
2589
2590 // Get register and predecessor for one PHI edge.
2591 Register Reg = MI.getOperand(i).getReg();
2592 const MachineBasicBlock *Pred = MI.getOperand(i + 1).getMBB();
2593
2594 BBInfo &PInfo = MBBInfoMap[Pred];
2595 if (PInfo.addRequired(Reg))
2596 todo.insert(Pred);
2597 }
2598 }
2599 }
2600
2601 // Iteratively push vregsRequired to predecessors. This will converge to the
2602 // same final state regardless of DenseSet iteration order.
2603 while (!todo.empty()) {
2604 const MachineBasicBlock *MBB = *todo.begin();
2605 todo.erase(MBB);
2606 BBInfo &MInfo = MBBInfoMap[MBB];
2607 for (const MachineBasicBlock *Pred : MBB->predecessors()) {
2608 if (Pred == MBB)
2609 continue;
2610 BBInfo &SInfo = MBBInfoMap[Pred];
2611 if (SInfo.addRequired(MInfo.vregsRequired))
2612 todo.insert(Pred);
2613 }
2614 }
2615}
2616
2617// Check PHI instructions at the beginning of MBB. It is assumed that
2618// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
2619void MachineVerifier::checkPHIOps(const MachineBasicBlock &MBB) {
2620 BBInfo &MInfo = MBBInfoMap[&MBB];
2621
2622 SmallPtrSet<const MachineBasicBlock*, 8> seen;
2623 for (const MachineInstr &Phi : MBB) {
2624 if (!Phi.isPHI())
2625 break;
2626 seen.clear();
2627
2628 const MachineOperand &MODef = Phi.getOperand(0);
2629 if (!MODef.isReg() || !MODef.isDef()) {
2630 report("Expected first PHI operand to be a register def", &MODef, 0);
2631 continue;
2632 }
2633 if (MODef.isTied() || MODef.isImplicit() || MODef.isInternalRead() ||
2634 MODef.isEarlyClobber() || MODef.isDebug())
2635 report("Unexpected flag on PHI operand", &MODef, 0);
2636 Register DefReg = MODef.getReg();
2637 if (!Register::isVirtualRegister(DefReg))
2638 report("Expected first PHI operand to be a virtual register", &MODef, 0);
2639
2640 for (unsigned I = 1, E = Phi.getNumOperands(); I != E; I += 2) {
2641 const MachineOperand &MO0 = Phi.getOperand(I);
2642 if (!MO0.isReg()) {
2643 report("Expected PHI operand to be a register", &MO0, I);
2644 continue;
2645 }
2646 if (MO0.isImplicit() || MO0.isInternalRead() || MO0.isEarlyClobber() ||
2647 MO0.isDebug() || MO0.isTied())
2648 report("Unexpected flag on PHI operand", &MO0, I);
2649
2650 const MachineOperand &MO1 = Phi.getOperand(I + 1);
2651 if (!MO1.isMBB()) {
2652 report("Expected PHI operand to be a basic block", &MO1, I + 1);
2653 continue;
2654 }
2655
2656 const MachineBasicBlock &Pre = *MO1.getMBB();
2657 if (!Pre.isSuccessor(&MBB)) {
2658 report("PHI input is not a predecessor block", &MO1, I + 1);
2659 continue;
2660 }
2661
2662 if (MInfo.reachable) {
2663 seen.insert(&Pre);
2664 BBInfo &PrInfo = MBBInfoMap[&Pre];
2665 if (!MO0.isUndef() && PrInfo.reachable &&
2666 !PrInfo.isLiveOut(MO0.getReg()))
2667 report("PHI operand is not live-out from predecessor", &MO0, I);
2668 }
2669 }
2670
2671 // Did we see all predecessors?
2672 if (MInfo.reachable) {
2673 for (MachineBasicBlock *Pred : MBB.predecessors()) {
2674 if (!seen.count(Pred)) {
2675 report("Missing PHI operand", &Phi);
2676 errs() << printMBBReference(*Pred)
2677 << " is a predecessor according to the CFG.\n";
2678 }
2679 }
2680 }
2681 }
2682}
2683
2684void MachineVerifier::visitMachineFunctionAfter() {
2685 calcRegsPassed();
2686
2687 for (const MachineBasicBlock &MBB : *MF)
2688 checkPHIOps(MBB);
2689
2690 // Now check liveness info if available
2691 calcRegsRequired();
2692
2693 // Check for killed virtual registers that should be live out.
2694 for (const auto &MBB : *MF) {
2695 BBInfo &MInfo = MBBInfoMap[&MBB];
2696 for (Register VReg : MInfo.vregsRequired)
2697 if (MInfo.regsKilled.count(VReg)) {
2698 report("Virtual register killed in block, but needed live out.", &MBB);
2699 errs() << "Virtual register " << printReg(VReg)
2700 << " is used after the block.\n";
2701 }
2702 }
2703
2704 if (!MF->empty()) {
2705 BBInfo &MInfo = MBBInfoMap[&MF->front()];
2706 for (Register VReg : MInfo.vregsRequired) {
2707 report("Virtual register defs don't dominate all uses.", MF);
2708 report_context_vreg(VReg);
2709 }
2710 }
2711
2712 if (LiveVars)
2713 verifyLiveVariables();
2714 if (LiveInts)
2715 verifyLiveIntervals();
2716
2717 // Check live-in list of each MBB. If a register is live into MBB, check
2718 // that the register is in regsLiveOut of each predecessor block. Since
2719 // this must come from a definition in the predecesssor or its live-in
2720 // list, this will catch a live-through case where the predecessor does not
2721 // have the register in its live-in list. This currently only checks
2722 // registers that have no aliases, are not allocatable and are not
2723 // reserved, which could mean a condition code register for instance.
2724 if (MRI->tracksLiveness())
2725 for (const auto &MBB : *MF)
2726 for (MachineBasicBlock::RegisterMaskPair P : MBB.liveins()) {
2727 MCPhysReg LiveInReg = P.PhysReg;
2728 bool hasAliases = MCRegAliasIterator(LiveInReg, TRI, false).isValid();
2729 if (hasAliases || isAllocatable(LiveInReg) || isReserved(LiveInReg))
2730 continue;
2731 for (const MachineBasicBlock *Pred : MBB.predecessors()) {
2732 BBInfo &PInfo = MBBInfoMap[Pred];
2733 if (!PInfo.regsLiveOut.count(LiveInReg)) {
2734 report("Live in register not found to be live out from predecessor.",
2735 &MBB);
2736 errs() << TRI->getName(LiveInReg)
2737 << " not found to be live out from "
2738 << printMBBReference(*Pred) << "\n";
2739 }
2740 }
2741 }
2742
2743 for (auto CSInfo : MF->getCallSitesInfo())
2744 if (!CSInfo.first->isCall())
2745 report("Call site info referencing instruction that is not call", MF);
2746
2747 // If there's debug-info, check that we don't have any duplicate value
2748 // tracking numbers.
2749 if (MF->getFunction().getSubprogram()) {
2750 DenseSet<unsigned> SeenNumbers;
2751 for (auto &MBB : *MF) {
2752 for (auto &MI : MBB) {
2753 if (auto Num = MI.peekDebugInstrNum()) {
2754 auto Result = SeenNumbers.insert((unsigned)Num);
2755 if (!Result.second)
2756 report("Instruction has a duplicated value tracking number", &MI);
2757 }
2758 }
2759 }
2760 }
2761}
2762
2763void MachineVerifier::verifyLiveVariables() {
2764 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars")(static_cast <bool> (LiveVars && "Don't call verifyLiveVariables without LiveVars"
) ? void (0) : __assert_fail ("LiveVars && \"Don't call verifyLiveVariables without LiveVars\""
, "llvm/lib/CodeGen/MachineVerifier.cpp", 2764, __extension__
__PRETTY_FUNCTION__))
;
2765 for (unsigned I = 0, E = MRI->getNumVirtRegs(); I != E; ++I) {
2766 Register Reg = Register::index2VirtReg(I);
2767 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
2768 for (const auto &MBB : *MF) {
2769 BBInfo &MInfo = MBBInfoMap[&MBB];
2770
2771 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
2772 if (MInfo.vregsRequired.count(Reg)) {
2773 if (!VI.AliveBlocks.test(MBB.getNumber())) {
2774 report("LiveVariables: Block missing from AliveBlocks", &MBB);
2775 errs() << "Virtual register " << printReg(Reg)
2776 << " must be live through the block.\n";
2777 }
2778 } else {
2779 if (VI.AliveBlocks.test(MBB.getNumber())) {
2780 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
2781 errs() << "Virtual register " << printReg(Reg)
2782 << " is not needed live through the block.\n";
2783 }
2784 }
2785 }
2786 }
2787}
2788
2789void MachineVerifier::verifyLiveIntervals() {
2790 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts")(static_cast <bool> (LiveInts && "Don't call verifyLiveIntervals without LiveInts"
) ? void (0) : __assert_fail ("LiveInts && \"Don't call verifyLiveIntervals without LiveInts\""
, "llvm/lib/CodeGen/MachineVerifier.cpp", 2790, __extension__
__PRETTY_FUNCTION__))
;
2791 for (unsigned I = 0, E = MRI->getNumVirtRegs(); I != E; ++I) {
2792 Register Reg = Register::index2VirtReg(I);
2793
2794 // Spilling and splitting may leave unused registers around. Skip them.
2795 if (MRI->reg_nodbg_empty(Reg))
2796 continue;
2797
2798 if (!LiveInts->hasInterval(Reg)) {
2799 report("Missing live interval for virtual register", MF);
2800 errs() << printReg(Reg, TRI) << " still has defs or uses\n";
2801 continue;
2802 }
2803
2804 const LiveInterval &LI = LiveInts->getInterval(Reg);
2805 assert(Reg == LI.reg() && "Invalid reg to interval mapping")(static_cast <bool> (Reg == LI.reg() && "Invalid reg to interval mapping"
) ? void (0) : __assert_fail ("Reg == LI.reg() && \"Invalid reg to interval mapping\""
, "llvm/lib/CodeGen/MachineVerifier.cpp", 2805, __extension__
__PRETTY_FUNCTION__))
;
2806 verifyLiveInterval(LI);
2807 }
2808
2809 // Verify all the cached regunit intervals.
2810 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
2811 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
2812 verifyLiveRange(*LR, i);
2813}
2814
2815void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
2816 const VNInfo *VNI, Register Reg,
2817 LaneBitmask LaneMask) {
2818 if (VNI->isUnused())
2819 return;
2820
2821 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
2822
2823 if (!DefVNI) {
2824 report("Value not live at VNInfo def and not marked unused", MF);
2825 report_context(LR, Reg, LaneMask);
2826 report_context(*VNI);
2827 return;
2828 }
2829
2830 if (DefVNI != VNI) {
2831 report("Live segment at def has different VNInfo", MF);
2832 report_context(LR, Reg, LaneMask);
2833 report_context(*VNI);
2834 return;
2835 }
2836
2837 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
2838 if (!MBB) {
2839 report("Invalid VNInfo definition index", MF);
2840 report_context(LR, Reg, LaneMask);
2841 report_context(*VNI);
2842 return;
2843 }
2844
2845 if (VNI->isPHIDef()) {
2846 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
2847 report("PHIDef VNInfo is not defined at MBB start", MBB);
2848 report_context(LR, Reg, LaneMask);
2849 report_context(*VNI);
2850 }
2851 return;
2852 }
2853
2854 // Non-PHI def.
2855 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
2856 if (!MI) {
2857 report("No instruction at VNInfo def index", MBB);
2858 report_context(LR, Reg, LaneMask);
2859 report_context(*VNI);
2860 return;
2861 }
2862
2863 if (Reg != 0) {
2864 bool hasDef = false;
2865 bool isEarlyClobber = false;
2866 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
2867 if (!MOI->isReg() || !MOI->isDef())
2868 continue;
2869 if (Register::isVirtualRegister(Reg)) {
2870 if (MOI->getReg() != Reg)
2871 continue;
2872 } else {
2873 if (!Register::isPhysicalRegister(MOI->getReg()) ||
2874 !TRI->hasRegUnit(MOI->getReg(), Reg))
2875 continue;
2876 }
2877 if (LaneMask.any() &&
2878 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask).none())
2879 continue;
2880 hasDef = true;
2881 if (MOI->isEarlyClobber())
2882 isEarlyClobber = true;
2883 }
2884
2885 if (!hasDef) {
2886 report("Defining instruction does not modify register", MI);
2887 report_context(LR, Reg, LaneMask);
2888 report_context(*VNI);
2889 }
2890
2891 // Early clobber defs begin at USE slots, but other defs must begin at
2892 // DEF slots.
2893 if (isEarlyClobber) {
2894 if (!VNI->def.isEarlyClobber()) {
2895 report("Early clobber def must be at an early-clobber slot", MBB);
2896 report_context(LR, Reg, LaneMask);
2897 report_context(*VNI);
2898 }
2899 } else if (!VNI->def.isRegister()) {
2900 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
2901 report_context(LR, Reg, LaneMask);
2902 report_context(*VNI);
2903 }
2904 }
2905}
2906
2907void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
2908 const LiveRange::const_iterator I,
2909 Register Reg,
2910 LaneBitmask LaneMask) {
2911 const LiveRange::Segment &S = *I;
2912 const VNInfo *VNI = S.valno;
2913 assert(VNI && "Live segment has no valno")(static_cast <bool> (VNI && "Live segment has no valno"
) ? void (0) : __assert_fail ("VNI && \"Live segment has no valno\""
, "llvm/lib/CodeGen/MachineVerifier.cpp", 2913, __extension__
__PRETTY_FUNCTION__))
;
2914
2915 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
2916 report("Foreign valno in live segment", MF);
2917 report_context(LR, Reg, LaneMask);
2918 report_context(S);
2919 report_context(*VNI);
2920 }
2921
2922 if (VNI->isUnused()) {
2923 report("Live segment valno is marked unused", MF);
2924 report_context(LR, Reg, LaneMask);
2925 report_context(S);
2926 }
2927
2928 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
2929 if (!MBB) {
2930 report("Bad start of live segment, no basic block", MF);
2931 report_context(LR, Reg, LaneMask);
2932 report_context(S);
2933 return;
2934 }
2935 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
2936 if (S.start != MBBStartIdx && S.start != VNI->def) {
2937 report("Live segment must begin at MBB entry or valno def", MBB);
2938 report_context(LR, Reg, LaneMask);
2939 report_context(S);
2940 }
2941
2942 const MachineBasicBlock *EndMBB =
2943 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
2944 if (!EndMBB) {
2945 report("Bad end of live segment, no basic block", MF);
2946 report_context(LR, Reg, LaneMask);
2947 report_context(S);
2948 return;
2949 }
2950
2951 // No more checks for live-out segments.
2952 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
2953 return;
2954
2955 // RegUnit intervals are allowed dead phis.
2956 if (!Register::isVirtualRegister(Reg) && VNI->isPHIDef() &&
2957 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
2958 return;
2959
2960 // The live segment is ending inside EndMBB
2961 const MachineInstr *MI =
2962 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
2963 if (!MI) {
2964 report("Live segment doesn't end at a valid instruction", EndMBB);
2965 report_context(LR, Reg, LaneMask);
2966 report_context(S);
2967 return;
2968 }
2969
2970 // The block slot must refer to a basic block boundary.
2971 if (S.end.isBlock()) {
2972 report("Live segment ends at B slot of an instruction", EndMBB);
2973 report_context(LR, Reg, LaneMask);
2974 report_context(S);
2975 }
2976
2977 if (S.end.isDead()) {
2978 // Segment ends on the dead slot.
2979 // That means there must be a dead def.
2980 if (!SlotIndex::isSameInstr(S.start, S.end)) {
2981 report("Live segment ending at dead slot spans instructions", EndMBB);
2982 report_context(LR, Reg, LaneMask);
2983 report_context(S);
2984 }
2985 }
2986
2987 // After tied operands are rewritten, a live segment can only end at an
2988 // early-clobber slot if it is being redefined by an early-clobber def.
2989 // TODO: Before tied operands are rewritten, a live segment can only end at an
2990 // early-clobber slot if the last use is tied to an early-clobber def.
2991 if (MF->getProperties().hasProperty(
2992 MachineFunctionProperties::Property::TiedOpsRewritten) &&
2993 S.end.isEarlyClobber()) {
2994 if (I+1 == LR.end() || (I+1)->start != S.end) {
2995 report("Live segment ending at early clobber slot must be "
2996 "redefined by an EC def in the same instruction", EndMBB);
2997 report_context(LR, Reg, LaneMask);
2998 report_context(S);
2999 }
3000 }
3001
3002 // The following checks only apply to virtual registers. Physreg liveness
3003 // is too weird to check.
3004 if (Register::isVirtualRegister(Reg)) {
3005 // A live segment can end with either a redefinition, a kill flag on a
3006 // use, or a dead flag on a def.
3007 bool hasRead = false;
3008 bool hasSubRegDef = false;
3009 bool hasDeadDef = false;
3010 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
3011 if (!MOI->isReg() || MOI->getReg() != Reg)
3012 continue;
3013 unsigned Sub = MOI->getSubReg();
3014 LaneBitmask SLM = Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub)
3015 : LaneBitmask::getAll();
3016 if (MOI->isDef()) {
3017 if (Sub != 0) {
3018 hasSubRegDef = true;
3019 // An operand %0:sub0 reads %0:sub1..n. Invert the lane
3020 // mask for subregister defs. Read-undef defs will be handled by
3021 // readsReg below.
3022 SLM = ~SLM;
3023 }
3024 if (MOI->isDead())
3025 hasDeadDef = true;
3026 }
3027 if (LaneMask.any() && (LaneMask & SLM).none())
3028 continue;
3029 if (MOI->readsReg())
3030 hasRead = true;
3031 }
3032 if (S.end.isDead()) {
3033 // Make sure that the corresponding machine operand for a "dead" live
3034 // range has the dead flag. We cannot perform this check for subregister
3035 // liveranges as partially dead values are allowed.
3036 if (LaneMask.none() && !hasDeadDef) {
3037 report("Instruction ending live segment on dead slot has no dead flag",
3038 MI);
3039 report_context(LR, Reg, LaneMask);
3040 report_context(S);
3041 }
3042 } else {
3043 if (!hasRead) {
3044 // When tracking subregister liveness, the main range must start new
3045 // values on partial register writes, even if there is no read.
3046 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask.any() ||
3047 !hasSubRegDef) {
3048 report("Instruction ending live segment doesn't read the register",
3049 MI);
3050 report_context(LR, Reg, LaneMask);
3051 report_context(S);
3052 }
3053 }
3054 }
3055 }
3056
3057 // Now check all the basic blocks in this live segment.
3058 MachineFunction::const_iterator MFI = MBB->getIterator();
3059 // Is this live segment the beginning of a non-PHIDef VN?
3060 if (S.start == VNI->def && !VNI->isPHIDef()) {
3061 // Not live-in to any blocks.
3062 if (MBB == EndMBB)
3063 return;
3064 // Skip this block.
3065 ++MFI;
3066 }
3067
3068 SmallVector<SlotIndex, 4> Undefs;
3069 if (LaneMask.any()) {
3070 LiveInterval &OwnerLI = LiveInts->getInterval(Reg);
3071 OwnerLI.computeSubRangeUndefs(Undefs, LaneMask, *MRI, *Indexes);
3072 }
3073
3074 while (true) {
3075 assert(LiveInts->isLiveInToMBB(LR, &*MFI))(static_cast <bool> (LiveInts->isLiveInToMBB(LR, &
*MFI)) ? void (0) : __assert_fail ("LiveInts->isLiveInToMBB(LR, &*MFI)"
, "llvm/lib/CodeGen/MachineVerifier.cpp", 3075, __extension__
__PRETTY_FUNCTION__))
;
3076 // We don't know how to track physregs into a landing pad.
3077 if (!Register::isVirtualRegister(Reg) && MFI->isEHPad()) {
3078 if (&*MFI == EndMBB)
3079 break;
3080 ++MFI;
3081 continue;
3082 }
3083
3084 // Is VNI a PHI-def in the current block?
3085 bool IsPHI = VNI->isPHIDef() &&
3086 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
3087
3088 // Check that VNI is live-out of all predecessors.
3089 for (const MachineBasicBlock *Pred : MFI->predecessors()) {
3090 SlotIndex PEnd = LiveInts->getMBBEndIdx(Pred);
3091 // Predecessor of landing pad live-out on last call.
3092 if (MFI->isEHPad()) {
3093 for (const MachineInstr &MI : llvm::reverse(*Pred)) {
3094 if (MI.isCall()) {
3095 PEnd = Indexes->getInstructionIndex(MI).getBoundaryIndex();
3096 break;
3097 }
3098 }
3099 }
3100 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
3101
3102 // All predecessors must have a live-out value. However for a phi
3103 // instruction with subregister intervals
3104 // only one of the subregisters (not necessarily the current one) needs to
3105 // be defined.
3106 if (!PVNI && (LaneMask.none() || !IsPHI)) {
3107 if (LiveRangeCalc::isJointlyDominated(Pred, Undefs, *Indexes))
3108 continue;
3109 report("Register not marked live out of predecessor", Pred);
3110 report_context(LR, Reg, LaneMask);
3111 report_context(*VNI);
3112 errs() << " live into " << printMBBReference(*MFI) << '@'
3113 << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
3114 << PEnd << '\n';
3115 continue;
3116 }
3117
3118 // Only PHI-defs can take different predecessor values.
3119 if (!IsPHI && PVNI != VNI) {
3120 report("Different value live out of predecessor", Pred);
3121 report_context(LR, Reg, LaneMask);
3122 errs() << "Valno #" << PVNI->id << " live out of "
3123 << printMBBReference(*Pred) << '@' << PEnd << "\nValno #"
3124 << VNI->id << " live into " << printMBBReference(*MFI) << '@'
3125 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
3126 }
3127 }
3128 if (&*MFI == EndMBB)
3129 break;
3130 ++MFI;
3131 }
3132}
3133
3134void MachineVerifier::verifyLiveRange(const LiveRange &LR, Register Reg,
3135 LaneBitmask LaneMask) {
3136 for (const VNInfo *VNI : LR.valnos)
3137 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
3138
3139 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
3140 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
3141}
3142
3143void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
3144 Register Reg = LI.reg();
3145 assert(Register::isVirtualRegister(Reg))(static_cast <bool> (Register::isVirtualRegister(Reg)) ?
void (0) : __assert_fail ("Register::isVirtualRegister(Reg)"
, "llvm/lib/CodeGen/MachineVerifier.cpp", 3145, __extension__
__PRETTY_FUNCTION__))
;
3146 verifyLiveRange(LI, Reg);
3147
3148 LaneBitmask Mask;
3149 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
3150 for (const LiveInterval::SubRange &SR : LI.subranges()) {
3151 if ((Mask & SR.LaneMask).any()) {
3152 report("Lane masks of sub ranges overlap in live interval", MF);
3153 report_context(LI);
3154 }
3155 if ((SR.LaneMask & ~MaxMask).any()) {
3156 report("Subrange lanemask is invalid", MF);
3157 report_context(LI);
3158 }
3159 if (SR.empty()) {
3160 report("Subrange must not be empty", MF);
3161 report_context(SR, LI.reg(), SR.LaneMask);
3162 }
3163 Mask |= SR.LaneMask;
3164 verifyLiveRange(SR, LI.reg(), SR.LaneMask);
3165 if (!LI.covers(SR)) {
3166 report("A Subrange is not covered by the main range", MF);
3167 report_context(LI);
3168 }
3169 }
3170
3171 // Check the LI only has one connected component.
3172 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
3173 unsigned NumComp = ConEQ.Classify(LI);
3174 if (NumComp > 1) {
3175 report("Multiple connected components in live interval", MF);
3176 report_context(LI);
3177 for (unsigned comp = 0; comp != NumComp; ++comp) {
3178 errs() << comp << ": valnos";
3179 for (const VNInfo *I : LI.valnos)
3180 if (comp == ConEQ.getEqClass(I))
3181 errs() << ' ' << I->id;
3182 errs() << '\n';
3183 }
3184 }
3185}
3186
3187namespace {
3188
3189 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
3190 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
3191 // value is zero.
3192 // We use a bool plus an integer to capture the stack state.
3193 struct StackStateOfBB {
3194 StackStateOfBB() = default;
3195 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
3196 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
3197 ExitIsSetup(ExitSetup) {}
3198
3199 // Can be negative, which means we are setting up a frame.
3200 int EntryValue = 0;
3201 int ExitValue = 0;
3202 bool EntryIsSetup = false;
3203 bool ExitIsSetup = false;
3204 };
3205
3206} // end anonymous namespace
3207
3208/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
3209/// by a FrameDestroy <n>, stack adjustments are identical on all
3210/// CFG edges to a merge point, and frame is destroyed at end of a return block.
3211void MachineVerifier::verifyStackFrame() {
3212 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
3213 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
3214 if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
3215 return;
3216
3217 SmallVector<StackStateOfBB, 8> SPState;
3218 SPState.resize(MF->getNumBlockIDs());
3219 df_iterator_default_set<const MachineBasicBlock*> Reachable;
3220
3221 // Visit the MBBs in DFS order.
3222 for (df_ext_iterator<const MachineFunction *,
3223 df_iterator_default_set<const MachineBasicBlock *>>
3224 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
3225 DFI != DFE; ++DFI) {
3226 const MachineBasicBlock *MBB = *DFI;
3227
3228 StackStateOfBB BBState;
3229 // Check the exit state of the DFS stack predecessor.
3230 if (DFI.getPathLength() >= 2) {
3231 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
3232 assert(Reachable.count(StackPred) &&(static_cast <bool> (Reachable.count(StackPred) &&
"DFS stack predecessor is already visited.\n") ? void (0) : __assert_fail
("Reachable.count(StackPred) && \"DFS stack predecessor is already visited.\\n\""
, "llvm/lib/CodeGen/MachineVerifier.cpp", 3233, __extension__
__PRETTY_FUNCTION__))
3233 "DFS stack predecessor is already visited.\n")(static_cast <bool> (Reachable.count(StackPred) &&
"DFS stack predecessor is already visited.\n") ? void (0) : __assert_fail
("Reachable.count(StackPred) && \"DFS stack predecessor is already visited.\\n\""
, "llvm/lib/CodeGen/MachineVerifier.cpp", 3233, __extension__
__PRETTY_FUNCTION__))
;
3234 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
3235 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
3236 BBState.ExitValue = BBState.EntryValue;
3237 BBState.ExitIsSetup = BBState.EntryIsSetup;
3238 }
3239
3240 // Update stack state by checking contents of MBB.
3241 for (const auto &I : *MBB) {
3242 if (I.getOpcode() == FrameSetupOpcode) {
3243 if (BBState.ExitIsSetup)
3244 report("FrameSetup is after another FrameSetup", &I);
3245 BBState.ExitValue -= TII->getFrameTotalSize(I);
3246 BBState.ExitIsSetup = true;
3247 }
3248
3249 if (I.getOpcode() == FrameDestroyOpcode) {
3250 int Size = TII->getFrameTotalSize(I);
3251 if (!BBState.ExitIsSetup)
3252 report("FrameDestroy is not after a FrameSetup", &I);
3253 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
3254 BBState.ExitValue;
3255 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
3256 report("FrameDestroy <n> is after FrameSetup <m>", &I);
3257 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
3258 << AbsSPAdj << ">.\n";
3259 }
3260 BBState.ExitValue += Size;
3261 BBState.ExitIsSetup = false;
3262 }
3263 }
3264 SPState[MBB->getNumber()] = BBState;
3265
3266 // Make sure the exit state of any predecessor is consistent with the entry
3267 // state.
3268 for (const MachineBasicBlock *Pred : MBB->predecessors()) {
3269 if (Reachable.count(Pred) &&
3270 (SPState[Pred->getNumber()].ExitValue != BBState.EntryValue ||
3271 SPState[Pred->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
3272 report("The exit stack state of a predecessor is inconsistent.", MBB);
3273 errs() << "Predecessor " << printMBBReference(*Pred)
3274 << " has exit state (" << SPState[Pred->getNumber()].ExitValue
3275 << ", " << SPState[Pred->getNumber()].ExitIsSetup << "), while "
3276 << printMBBReference(*MBB) << " has entry state ("
3277 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
3278 }
3279 }
3280
3281 // Make sure the entry state of any successor is consistent with the exit
3282 // state.
3283 for (const MachineBasicBlock *Succ : MBB->successors()) {
3284 if (Reachable.count(Succ) &&
3285 (SPState[Succ->getNumber()].EntryValue != BBState.ExitValue ||
3286 SPState[Succ->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
3287 report("The entry stack state of a successor is inconsistent.", MBB);
3288 errs() << "Successor " << printMBBReference(*Succ)
3289 << " has entry state (" << SPState[Succ->getNumber()].EntryValue
3290 << ", " << SPState[Succ->getNumber()].EntryIsSetup << "), while "
3291 << printMBBReference(*MBB) << " has exit state ("
3292 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
3293 }
3294 }
3295
3296 // Make sure a basic block with return ends with zero stack adjustment.
3297 if (!MBB->empty() && MBB->back().isReturn()) {
3298 if (BBState.ExitIsSetup)
3299 report("A return block ends with a FrameSetup.", MBB);
3300 if (BBState.ExitValue)
3301 report("A return block ends with a nonzero stack adjustment.", MBB);
3302 }
3303 }
3304}