1 //===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Pass to verify generated machine code. The following is checked:
12 // Operand counts: All explicit operands must be present.
14 // Register classes: All physical and virtual register operands must be
15 // compatible with the register class required by the instruction descriptor.
17 // Register live intervals: Registers must be defined only once, and must be
18 // defined before use.
20 // The machine code verifier is enabled from LLVMTargetMachine.cpp with the
21 // command-line option -verify-machineinstrs, or by defining the environment
22 // variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
23 // the verifier errors.
24 //===----------------------------------------------------------------------===//
26 #include "llvm/CodeGen/Passes.h"
27 #include "llvm/ADT/DenseSet.h"
28 #include "llvm/ADT/DepthFirstIterator.h"
29 #include "llvm/ADT/SetOperations.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/Analysis/EHPersonalities.h"
32 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
33 #include "llvm/CodeGen/LiveStackAnalysis.h"
34 #include "llvm/CodeGen/LiveVariables.h"
35 #include "llvm/CodeGen/MachineFrameInfo.h"
36 #include "llvm/CodeGen/MachineFunctionPass.h"
37 #include "llvm/CodeGen/MachineMemOperand.h"
38 #include "llvm/CodeGen/MachineRegisterInfo.h"
39 #include "llvm/IR/BasicBlock.h"
40 #include "llvm/IR/InlineAsm.h"
41 #include "llvm/IR/Instructions.h"
42 #include "llvm/MC/MCAsmInfo.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/FileSystem.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Target/TargetInstrInfo.h"
48 #include "llvm/Target/TargetMachine.h"
49 #include "llvm/Target/TargetRegisterInfo.h"
50 #include "llvm/Target/TargetSubtargetInfo.h"
54 struct MachineVerifier {
56 MachineVerifier(Pass *pass, const char *b) :
61 unsigned verify(MachineFunction &MF);
65 const MachineFunction *MF;
66 const TargetMachine *TM;
67 const TargetInstrInfo *TII;
68 const TargetRegisterInfo *TRI;
69 const MachineRegisterInfo *MRI;
73 // Avoid querying the MachineFunctionProperties for each operand.
74 bool isFunctionRegBankSelected;
75 bool isFunctionSelected;
77 typedef SmallVector<unsigned, 16> RegVector;
78 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
79 typedef DenseSet<unsigned> RegSet;
80 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
81 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
83 const MachineInstr *FirstTerminator;
84 BlockSet FunctionBlocks;
86 BitVector regsReserved;
88 RegVector regsDefined, regsDead, regsKilled;
89 RegMaskVector regMasks;
90 RegSet regsLiveInButUnused;
94 // Add Reg and any sub-registers to RV
95 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
97 if (TargetRegisterInfo::isPhysicalRegister(Reg))
98 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
99 RV.push_back(*SubRegs);
103 // Is this MBB reachable from the MF entry point?
106 // Vregs that must be live in because they are used without being
107 // defined. Map value is the user.
110 // Regs killed in MBB. They may be defined again, and will then be in both
111 // regsKilled and regsLiveOut.
114 // Regs defined in MBB and live out. Note that vregs passing through may
115 // be live out without being mentioned here.
118 // Vregs that pass through MBB untouched. This set is disjoint from
119 // regsKilled and regsLiveOut.
122 // Vregs that must pass through MBB because they are needed by a successor
123 // block. This set is disjoint from regsLiveOut.
124 RegSet vregsRequired;
126 // Set versions of block's predecessor and successor lists.
127 BlockSet Preds, Succs;
129 BBInfo() : reachable(false) {}
131 // Add register to vregsPassed if it belongs there. Return true if
133 bool addPassed(unsigned Reg) {
134 if (!TargetRegisterInfo::isVirtualRegister(Reg))
136 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
138 return vregsPassed.insert(Reg).second;
141 // Same for a full set.
142 bool addPassed(const RegSet &RS) {
143 bool changed = false;
144 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
150 // Add register to vregsRequired if it belongs there. Return true if
152 bool addRequired(unsigned Reg) {
153 if (!TargetRegisterInfo::isVirtualRegister(Reg))
155 if (regsLiveOut.count(Reg))
157 return vregsRequired.insert(Reg).second;
160 // Same for a full set.
161 bool addRequired(const RegSet &RS) {
162 bool changed = false;
163 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
169 // Same for a full map.
170 bool addRequired(const RegMap &RM) {
171 bool changed = false;
172 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
173 if (addRequired(I->first))
178 // Live-out registers are either in regsLiveOut or vregsPassed.
179 bool isLiveOut(unsigned Reg) const {
180 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
184 // Extra register info per MBB.
185 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
187 bool isReserved(unsigned Reg) {
188 return Reg < regsReserved.size() && regsReserved.test(Reg);
191 bool isAllocatable(unsigned Reg) {
192 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
195 // Analysis information if available
196 LiveVariables *LiveVars;
197 LiveIntervals *LiveInts;
198 LiveStacks *LiveStks;
199 SlotIndexes *Indexes;
201 void visitMachineFunctionBefore();
202 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
203 void visitMachineBundleBefore(const MachineInstr *MI);
204 void visitMachineInstrBefore(const MachineInstr *MI);
205 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
206 void visitMachineInstrAfter(const MachineInstr *MI);
207 void visitMachineBundleAfter(const MachineInstr *MI);
208 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
209 void visitMachineFunctionAfter();
211 void report(const char *msg, const MachineFunction *MF);
212 void report(const char *msg, const MachineBasicBlock *MBB);
213 void report(const char *msg, const MachineInstr *MI);
214 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
216 void report_context(const LiveInterval &LI) const;
217 void report_context(const LiveRange &LR, unsigned VRegUnit,
218 LaneBitmask LaneMask) const;
219 void report_context(const LiveRange::Segment &S) const;
220 void report_context(const VNInfo &VNI) const;
221 void report_context(SlotIndex Pos) const;
222 void report_context_liverange(const LiveRange &LR) const;
223 void report_context_lanemask(LaneBitmask LaneMask) const;
224 void report_context_vreg(unsigned VReg) const;
225 void report_context_vreg_regunit(unsigned VRegOrRegUnit) const;
227 void verifyInlineAsm(const MachineInstr *MI);
229 void checkLiveness(const MachineOperand *MO, unsigned MONum);
230 void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
231 SlotIndex UseIdx, const LiveRange &LR, unsigned Reg,
232 LaneBitmask LaneMask = LaneBitmask::getNone());
233 void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
234 SlotIndex DefIdx, const LiveRange &LR, unsigned Reg,
235 LaneBitmask LaneMask = LaneBitmask::getNone());
237 void markReachable(const MachineBasicBlock *MBB);
238 void calcRegsPassed();
239 void checkPHIOps(const MachineBasicBlock *MBB);
241 void calcRegsRequired();
242 void verifyLiveVariables();
243 void verifyLiveIntervals();
244 void verifyLiveInterval(const LiveInterval&);
245 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
247 void verifyLiveRangeSegment(const LiveRange&,
248 const LiveRange::const_iterator I, unsigned,
250 void verifyLiveRange(const LiveRange&, unsigned,
251 LaneBitmask LaneMask = LaneBitmask::getNone());
253 void verifyStackFrame();
255 void verifySlotIndexes() const;
256 void verifyProperties(const MachineFunction &MF);
259 struct MachineVerifierPass : public MachineFunctionPass {
260 static char ID; // Pass ID, replacement for typeid
261 const std::string Banner;
263 MachineVerifierPass(const std::string &banner = nullptr)
264 : MachineFunctionPass(ID), Banner(banner) {
265 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
268 void getAnalysisUsage(AnalysisUsage &AU) const override {
269 AU.setPreservesAll();
270 MachineFunctionPass::getAnalysisUsage(AU);
273 bool runOnMachineFunction(MachineFunction &MF) override {
274 unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
276 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
283 char MachineVerifierPass::ID = 0;
284 INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
285 "Verify generated machine code", false, false)
287 FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
288 return new MachineVerifierPass(Banner);
291 bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
293 MachineFunction &MF = const_cast<MachineFunction&>(*this);
294 unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
295 if (AbortOnErrors && FoundErrors)
296 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
297 return FoundErrors == 0;
300 void MachineVerifier::verifySlotIndexes() const {
301 if (Indexes == nullptr)
304 // Ensure the IdxMBB list is sorted by slot indexes.
306 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
307 E = Indexes->MBBIndexEnd(); I != E; ++I) {
308 assert(!Last.isValid() || I->first > Last);
313 void MachineVerifier::verifyProperties(const MachineFunction &MF) {
314 // If a pass has introduced virtual registers without clearing the
315 // NoVRegs property (or set it without allocating the vregs)
316 // then report an error.
317 if (MF.getProperties().hasProperty(
318 MachineFunctionProperties::Property::NoVRegs) &&
319 MRI->getNumVirtRegs())
320 report("Function has NoVRegs property but there are VReg operands", &MF);
323 unsigned MachineVerifier::verify(MachineFunction &MF) {
327 TM = &MF.getTarget();
328 TII = MF.getSubtarget().getInstrInfo();
329 TRI = MF.getSubtarget().getRegisterInfo();
330 MRI = &MF.getRegInfo();
332 isFunctionRegBankSelected = MF.getProperties().hasProperty(
333 MachineFunctionProperties::Property::RegBankSelected);
334 isFunctionSelected = MF.getProperties().hasProperty(
335 MachineFunctionProperties::Property::Selected);
342 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
343 // We don't want to verify LiveVariables if LiveIntervals is available.
345 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
346 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
347 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
352 verifyProperties(MF);
354 visitMachineFunctionBefore();
355 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
357 visitMachineBasicBlockBefore(&*MFI);
358 // Keep track of the current bundle header.
359 const MachineInstr *CurBundle = nullptr;
360 // Do we expect the next instruction to be part of the same bundle?
361 bool InBundle = false;
363 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
364 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
365 if (MBBI->getParent() != &*MFI) {
366 report("Bad instruction parent pointer", &*MFI);
367 errs() << "Instruction: " << *MBBI;
371 // Check for consistent bundle flags.
372 if (InBundle && !MBBI->isBundledWithPred())
373 report("Missing BundledPred flag, "
374 "BundledSucc was set on predecessor",
376 if (!InBundle && MBBI->isBundledWithPred())
377 report("BundledPred flag is set, "
378 "but BundledSucc not set on predecessor",
381 // Is this a bundle header?
382 if (!MBBI->isInsideBundle()) {
384 visitMachineBundleAfter(CurBundle);
386 visitMachineBundleBefore(CurBundle);
387 } else if (!CurBundle)
388 report("No bundle header", &*MBBI);
389 visitMachineInstrBefore(&*MBBI);
390 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
391 const MachineInstr &MI = *MBBI;
392 const MachineOperand &Op = MI.getOperand(I);
393 if (Op.getParent() != &MI) {
394 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
395 // functions when replacing operands of a MachineInstr.
396 report("Instruction has operand with wrong parent set", &MI);
399 visitMachineOperand(&Op, I);
402 visitMachineInstrAfter(&*MBBI);
404 // Was this the last bundled instruction?
405 InBundle = MBBI->isBundledWithSucc();
408 visitMachineBundleAfter(CurBundle);
410 report("BundledSucc flag set on last instruction in block", &MFI->back());
411 visitMachineBasicBlockAfter(&*MFI);
413 visitMachineFunctionAfter();
421 regsLiveInButUnused.clear();
427 void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
430 if (!foundErrors++) {
432 errs() << "# " << Banner << '\n';
433 if (LiveInts != nullptr)
434 LiveInts->print(errs());
436 MF->print(errs(), Indexes);
438 errs() << "*** Bad machine code: " << msg << " ***\n"
439 << "- function: " << MF->getName() << "\n";
442 void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
444 report(msg, MBB->getParent());
445 errs() << "- basic block: BB#" << MBB->getNumber()
446 << ' ' << MBB->getName()
447 << " (" << (const void*)MBB << ')';
449 errs() << " [" << Indexes->getMBBStartIdx(MBB)
450 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
454 void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
456 report(msg, MI->getParent());
457 errs() << "- instruction: ";
458 if (Indexes && Indexes->hasIndex(*MI))
459 errs() << Indexes->getInstructionIndex(*MI) << '\t';
460 MI->print(errs(), /*SkipOpers=*/true);
464 void MachineVerifier::report(const char *msg,
465 const MachineOperand *MO, unsigned MONum) {
467 report(msg, MO->getParent());
468 errs() << "- operand " << MONum << ": ";
469 MO->print(errs(), TRI);
473 void MachineVerifier::report_context(SlotIndex Pos) const {
474 errs() << "- at: " << Pos << '\n';
477 void MachineVerifier::report_context(const LiveInterval &LI) const {
478 errs() << "- interval: " << LI << '\n';
481 void MachineVerifier::report_context(const LiveRange &LR, unsigned VRegUnit,
482 LaneBitmask LaneMask) const {
483 report_context_liverange(LR);
484 report_context_vreg_regunit(VRegUnit);
486 report_context_lanemask(LaneMask);
489 void MachineVerifier::report_context(const LiveRange::Segment &S) const {
490 errs() << "- segment: " << S << '\n';
493 void MachineVerifier::report_context(const VNInfo &VNI) const {
494 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
497 void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
498 errs() << "- liverange: " << LR << '\n';
501 void MachineVerifier::report_context_vreg(unsigned VReg) const {
502 errs() << "- v. register: " << PrintReg(VReg, TRI) << '\n';
505 void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
506 if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
507 report_context_vreg(VRegOrUnit);
509 errs() << "- regunit: " << PrintRegUnit(VRegOrUnit, TRI) << '\n';
513 void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
514 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
517 void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
518 BBInfo &MInfo = MBBInfoMap[MBB];
519 if (!MInfo.reachable) {
520 MInfo.reachable = true;
521 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
522 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
527 void MachineVerifier::visitMachineFunctionBefore() {
528 lastIndex = SlotIndex();
529 regsReserved = MRI->getReservedRegs();
531 markReachable(&MF->front());
533 // Build a set of the basic blocks in the function.
534 FunctionBlocks.clear();
535 for (const auto &MBB : *MF) {
536 FunctionBlocks.insert(&MBB);
537 BBInfo &MInfo = MBBInfoMap[&MBB];
539 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
540 if (MInfo.Preds.size() != MBB.pred_size())
541 report("MBB has duplicate entries in its predecessor list.", &MBB);
543 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
544 if (MInfo.Succs.size() != MBB.succ_size())
545 report("MBB has duplicate entries in its successor list.", &MBB);
548 // Check that the register use lists are sane.
549 MRI->verifyUseLists();
554 // Does iterator point to a and b as the first two elements?
555 static bool matchPair(MachineBasicBlock::const_succ_iterator i,
556 const MachineBasicBlock *a, const MachineBasicBlock *b) {
565 MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
566 FirstTerminator = nullptr;
568 if (!MF->getProperties().hasProperty(
569 MachineFunctionProperties::Property::NoPHIs) && MRI->tracksLiveness()) {
570 // If this block has allocatable physical registers live-in, check that
571 // it is an entry block or landing pad.
572 for (const auto &LI : MBB->liveins()) {
573 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
574 MBB->getIterator() != MBB->getParent()->begin()) {
575 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
580 // Count the number of landing pad successors.
581 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
582 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
583 E = MBB->succ_end(); I != E; ++I) {
585 LandingPadSuccs.insert(*I);
586 if (!FunctionBlocks.count(*I))
587 report("MBB has successor that isn't part of the function.", MBB);
588 if (!MBBInfoMap[*I].Preds.count(MBB)) {
589 report("Inconsistent CFG", MBB);
590 errs() << "MBB is not in the predecessor list of the successor BB#"
591 << (*I)->getNumber() << ".\n";
595 // Check the predecessor list.
596 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
597 E = MBB->pred_end(); I != E; ++I) {
598 if (!FunctionBlocks.count(*I))
599 report("MBB has predecessor that isn't part of the function.", MBB);
600 if (!MBBInfoMap[*I].Succs.count(MBB)) {
601 report("Inconsistent CFG", MBB);
602 errs() << "MBB is not in the successor list of the predecessor BB#"
603 << (*I)->getNumber() << ".\n";
607 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
608 const BasicBlock *BB = MBB->getBasicBlock();
609 const Function *Fn = MF->getFunction();
610 if (LandingPadSuccs.size() > 1 &&
612 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
613 BB && isa<SwitchInst>(BB->getTerminator())) &&
614 !isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
615 report("MBB has more than one landing pad successor", MBB);
617 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
618 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
619 SmallVector<MachineOperand, 4> Cond;
620 if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
622 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
623 // check whether its answers match up with reality.
625 // Block falls through to its successor.
626 MachineFunction::const_iterator MBBI = MBB->getIterator();
628 if (MBBI == MF->end()) {
629 // It's possible that the block legitimately ends with a noreturn
630 // call or an unreachable, in which case it won't actually fall
631 // out the bottom of the function.
632 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
633 // It's possible that the block legitimately ends with a noreturn
634 // call or an unreachable, in which case it won't actuall fall
636 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
637 report("MBB exits via unconditional fall-through but doesn't have "
638 "exactly one CFG successor!", MBB);
639 } else if (!MBB->isSuccessor(&*MBBI)) {
640 report("MBB exits via unconditional fall-through but its successor "
641 "differs from its CFG successor!", MBB);
643 if (!MBB->empty() && MBB->back().isBarrier() &&
644 !TII->isPredicated(MBB->back())) {
645 report("MBB exits via unconditional fall-through but ends with a "
646 "barrier instruction!", MBB);
649 report("MBB exits via unconditional fall-through but has a condition!",
652 } else if (TBB && !FBB && Cond.empty()) {
653 // Block unconditionally branches somewhere.
654 // If the block has exactly one successor, that happens to be a
655 // landingpad, accept it as valid control flow.
656 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
657 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
658 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
659 report("MBB exits via unconditional branch but doesn't have "
660 "exactly one CFG successor!", MBB);
661 } else if (!MBB->isSuccessor(TBB)) {
662 report("MBB exits via unconditional branch but the CFG "
663 "successor doesn't match the actual successor!", MBB);
666 report("MBB exits via unconditional branch but doesn't contain "
667 "any instructions!", MBB);
668 } else if (!MBB->back().isBarrier()) {
669 report("MBB exits via unconditional branch but doesn't end with a "
670 "barrier instruction!", MBB);
671 } else if (!MBB->back().isTerminator()) {
672 report("MBB exits via unconditional branch but the branch isn't a "
673 "terminator instruction!", MBB);
675 } else if (TBB && !FBB && !Cond.empty()) {
676 // Block conditionally branches somewhere, otherwise falls through.
677 MachineFunction::const_iterator MBBI = MBB->getIterator();
679 if (MBBI == MF->end()) {
680 report("MBB conditionally falls through out of function!", MBB);
681 } else if (MBB->succ_size() == 1) {
682 // A conditional branch with only one successor is weird, but allowed.
684 report("MBB exits via conditional branch/fall-through but only has "
685 "one CFG successor!", MBB);
686 else if (TBB != *MBB->succ_begin())
687 report("MBB exits via conditional branch/fall-through but the CFG "
688 "successor don't match the actual successor!", MBB);
689 } else if (MBB->succ_size() != 2) {
690 report("MBB exits via conditional branch/fall-through but doesn't have "
691 "exactly two CFG successors!", MBB);
692 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
693 report("MBB exits via conditional branch/fall-through but the CFG "
694 "successors don't match the actual successors!", MBB);
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);
706 } else if (TBB && FBB) {
707 // Block conditionally branches somewhere, otherwise branches
709 if (MBB->succ_size() == 1) {
710 // A conditional branch with only one successor is weird, but allowed.
712 report("MBB exits via conditional branch/branch through but only has "
713 "one CFG successor!", MBB);
714 else if (TBB != *MBB->succ_begin())
715 report("MBB exits via conditional branch/branch through but the CFG "
716 "successor don't match the actual successor!", MBB);
717 } else if (MBB->succ_size() != 2) {
718 report("MBB exits via conditional branch/branch but doesn't have "
719 "exactly two CFG successors!", MBB);
720 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
721 report("MBB exits via conditional branch/branch but the CFG "
722 "successors don't match the actual successors!", MBB);
725 report("MBB exits via conditional branch/branch but doesn't "
726 "contain any instructions!", MBB);
727 } else if (!MBB->back().isBarrier()) {
728 report("MBB exits via conditional branch/branch but doesn't end with a "
729 "barrier instruction!", MBB);
730 } else if (!MBB->back().isTerminator()) {
731 report("MBB exits via conditional branch/branch but the branch "
732 "isn't a terminator instruction!", MBB);
735 report("MBB exits via conditinal branch/branch but there's no "
739 report("AnalyzeBranch returned invalid data!", MBB);
744 if (MRI->tracksLiveness()) {
745 for (const auto &LI : MBB->liveins()) {
746 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
747 report("MBB live-in list contains non-physical register", MBB);
750 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
751 SubRegs.isValid(); ++SubRegs)
752 regsLive.insert(*SubRegs);
755 regsLiveInButUnused = regsLive;
757 const MachineFrameInfo &MFI = MF->getFrameInfo();
758 BitVector PR = MFI.getPristineRegs(*MF);
759 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
760 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
761 SubRegs.isValid(); ++SubRegs)
762 regsLive.insert(*SubRegs);
769 lastIndex = Indexes->getMBBStartIdx(MBB);
772 // This function gets called for all bundle headers, including normal
773 // stand-alone unbundled instructions.
774 void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
775 if (Indexes && Indexes->hasIndex(*MI)) {
776 SlotIndex idx = Indexes->getInstructionIndex(*MI);
777 if (!(idx > lastIndex)) {
778 report("Instruction index out of order", MI);
779 errs() << "Last instruction was at " << lastIndex << '\n';
784 // Ensure non-terminators don't follow terminators.
785 // Ignore predicated terminators formed by if conversion.
786 // FIXME: If conversion shouldn't need to violate this rule.
787 if (MI->isTerminator() && !TII->isPredicated(*MI)) {
788 if (!FirstTerminator)
789 FirstTerminator = MI;
790 } else if (FirstTerminator) {
791 report("Non-terminator instruction after the first terminator", MI);
792 errs() << "First terminator was:\t" << *FirstTerminator;
796 // The operands on an INLINEASM instruction must follow a template.
797 // Verify that the flag operands make sense.
798 void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
799 // The first two operands on INLINEASM are the asm string and global flags.
800 if (MI->getNumOperands() < 2) {
801 report("Too few operands on inline asm", MI);
804 if (!MI->getOperand(0).isSymbol())
805 report("Asm string must be an external symbol", MI);
806 if (!MI->getOperand(1).isImm())
807 report("Asm flags must be an immediate", MI);
808 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
809 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
810 // and Extra_IsConvergent = 32.
811 if (!isUInt<6>(MI->getOperand(1).getImm()))
812 report("Unknown asm flags", &MI->getOperand(1), 1);
814 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
816 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
818 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
819 const MachineOperand &MO = MI->getOperand(OpNo);
820 // There may be implicit ops after the fixed operands.
823 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
826 if (OpNo > MI->getNumOperands())
827 report("Missing operands in last group", MI);
829 // An optional MDNode follows the groups.
830 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
833 // All trailing operands must be implicit registers.
834 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
835 const MachineOperand &MO = MI->getOperand(OpNo);
836 if (!MO.isReg() || !MO.isImplicit())
837 report("Expected implicit register after groups", &MO, OpNo);
841 void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
842 const MCInstrDesc &MCID = MI->getDesc();
843 if (MI->getNumOperands() < MCID.getNumOperands()) {
844 report("Too few operands", MI);
845 errs() << MCID.getNumOperands() << " operands expected, but "
846 << MI->getNumOperands() << " given.\n";
849 if (MI->isPHI() && MF->getProperties().hasProperty(
850 MachineFunctionProperties::Property::NoPHIs))
851 report("Found PHI instruction with NoPHIs property set", MI);
853 // Check the tied operands.
854 if (MI->isInlineAsm())
857 // Check the MachineMemOperands for basic consistency.
858 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
859 E = MI->memoperands_end(); I != E; ++I) {
860 if ((*I)->isLoad() && !MI->mayLoad())
861 report("Missing mayLoad flag", MI);
862 if ((*I)->isStore() && !MI->mayStore())
863 report("Missing mayStore flag", MI);
866 // Debug values must not have a slot index.
867 // Other instructions must have one, unless they are inside a bundle.
869 bool mapped = !LiveInts->isNotInMIMap(*MI);
870 if (MI->isDebugValue()) {
872 report("Debug instruction has a slot index", MI);
873 } else if (MI->isInsideBundle()) {
875 report("Instruction inside bundle has a slot index", MI);
878 report("Missing slot index", MI);
883 if (isPreISelGenericOpcode(MCID.getOpcode())) {
884 if (isFunctionSelected)
885 report("Unexpected generic instruction in a Selected function", MI);
887 // Generic instructions specify equality constraints between some
888 // of their operands. Make sure these are consistent.
889 SmallVector<LLT, 4> Types;
890 for (unsigned i = 0; i < MCID.getNumOperands(); ++i) {
891 if (!MCID.OpInfo[i].isGenericType())
893 size_t TypeIdx = MCID.OpInfo[i].getGenericTypeIndex();
894 Types.resize(std::max(TypeIdx + 1, Types.size()));
896 LLT OpTy = MRI->getType(MI->getOperand(i).getReg());
897 if (Types[TypeIdx].isValid() && Types[TypeIdx] != OpTy)
898 report("type mismatch in generic instruction", MI);
899 Types[TypeIdx] = OpTy;
903 // Generic opcodes must not have physical register operands.
904 if (isPreISelGenericOpcode(MCID.getOpcode())) {
905 for (auto &Op : MI->operands()) {
906 if (Op.isReg() && TargetRegisterInfo::isPhysicalRegister(Op.getReg()))
907 report("Generic instruction cannot have physical register", MI);
912 if (!TII->verifyInstruction(*MI, ErrorInfo))
913 report(ErrorInfo.data(), MI);
917 MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
918 const MachineInstr *MI = MO->getParent();
919 const MCInstrDesc &MCID = MI->getDesc();
920 unsigned NumDefs = MCID.getNumDefs();
921 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
922 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
924 // The first MCID.NumDefs operands must be explicit register defines
925 if (MONum < NumDefs) {
926 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
928 report("Explicit definition must be a register", MO, MONum);
929 else if (!MO->isDef() && !MCOI.isOptionalDef())
930 report("Explicit definition marked as use", MO, MONum);
931 else if (MO->isImplicit())
932 report("Explicit definition marked as implicit", MO, MONum);
933 } else if (MONum < MCID.getNumOperands()) {
934 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
935 // Don't check if it's the last operand in a variadic instruction. See,
936 // e.g., LDM_RET in the arm back end.
938 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
939 if (MO->isDef() && !MCOI.isOptionalDef())
940 report("Explicit operand marked as def", MO, MONum);
941 if (MO->isImplicit())
942 report("Explicit operand marked as implicit", MO, MONum);
945 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
948 report("Tied use must be a register", MO, MONum);
949 else if (!MO->isTied())
950 report("Operand should be tied", MO, MONum);
951 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
952 report("Tied def doesn't match MCInstrDesc", MO, MONum);
953 } else if (MO->isReg() && MO->isTied())
954 report("Explicit operand should not be tied", MO, MONum);
956 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
957 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
958 report("Extra explicit operand on non-variadic instruction", MO, MONum);
961 switch (MO->getType()) {
962 case MachineOperand::MO_Register: {
963 const unsigned Reg = MO->getReg();
966 if (MRI->tracksLiveness() && !MI->isDebugValue())
967 checkLiveness(MO, MONum);
969 // Verify the consistency of tied operands.
971 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
972 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
973 if (!OtherMO.isReg())
974 report("Must be tied to a register", MO, MONum);
975 if (!OtherMO.isTied())
976 report("Missing tie flags on tied operand", MO, MONum);
977 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
978 report("Inconsistent tie links", MO, MONum);
979 if (MONum < MCID.getNumDefs()) {
980 if (OtherIdx < MCID.getNumOperands()) {
981 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
982 report("Explicit def tied to explicit use without tie constraint",
985 if (!OtherMO.isImplicit())
986 report("Explicit def should be tied to implicit use", MO, MONum);
991 // Verify two-address constraints after leaving SSA form.
993 if (!MRI->isSSA() && MO->isUse() &&
994 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
995 Reg != MI->getOperand(DefIdx).getReg())
996 report("Two-address instruction operands must be identical", MO, MONum);
998 // Check register classes.
999 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
1000 unsigned SubIdx = MO->getSubReg();
1002 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1004 report("Illegal subregister index for physical register", MO, MONum);
1007 if (const TargetRegisterClass *DRC =
1008 TII->getRegClass(MCID, MONum, TRI, *MF)) {
1009 if (!DRC->contains(Reg)) {
1010 report("Illegal physical register for instruction", MO, MONum);
1011 errs() << TRI->getName(Reg) << " is not a "
1012 << TRI->getRegClassName(DRC) << " register.\n";
1016 // Virtual register.
1017 const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
1019 // This is a generic virtual register.
1021 // If we're post-Select, we can't have gvregs anymore.
1022 if (isFunctionSelected) {
1023 report("Generic virtual register invalid in a Selected function",
1028 // The gvreg must have a type and it must not have a SubIdx.
1029 LLT Ty = MRI->getType(Reg);
1030 if (!Ty.isValid()) {
1031 report("Generic virtual register must have a valid type", MO,
1036 const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
1038 // If we're post-RegBankSelect, the gvreg must have a bank.
1039 if (!RegBank && isFunctionRegBankSelected) {
1040 report("Generic virtual register must have a bank in a "
1041 "RegBankSelected function",
1046 // Make sure the register fits into its register bank if any.
1047 if (RegBank && Ty.isValid() &&
1048 RegBank->getSize() < Ty.getSizeInBits()) {
1049 report("Register bank is too small for virtual register", MO,
1051 errs() << "Register bank " << RegBank->getName() << " too small("
1052 << RegBank->getSize() << ") to fit " << Ty.getSizeInBits()
1057 report("Generic virtual register does not subregister index", MO,
1062 // If this is a target specific instruction and this operand
1063 // has register class constraint, the virtual register must
1065 if (!isPreISelGenericOpcode(MCID.getOpcode()) &&
1066 TII->getRegClass(MCID, MONum, TRI, *MF)) {
1067 report("Virtual register does not match instruction constraint", MO,
1069 errs() << "Expect register class "
1070 << TRI->getRegClassName(
1071 TII->getRegClass(MCID, MONum, TRI, *MF))
1072 << " but got nothing\n";
1079 const TargetRegisterClass *SRC =
1080 TRI->getSubClassWithSubReg(RC, SubIdx);
1082 report("Invalid subregister index for virtual register", MO, MONum);
1083 errs() << "Register class " << TRI->getRegClassName(RC)
1084 << " does not support subreg index " << SubIdx << "\n";
1088 report("Invalid register class for subregister index", MO, MONum);
1089 errs() << "Register class " << TRI->getRegClassName(RC)
1090 << " does not fully support subreg index " << SubIdx << "\n";
1094 if (const TargetRegisterClass *DRC =
1095 TII->getRegClass(MCID, MONum, TRI, *MF)) {
1097 const TargetRegisterClass *SuperRC =
1098 TRI->getLargestLegalSuperClass(RC, *MF);
1100 report("No largest legal super class exists.", MO, MONum);
1103 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
1105 report("No matching super-reg register class.", MO, MONum);
1109 if (!RC->hasSuperClassEq(DRC)) {
1110 report("Illegal virtual register for instruction", MO, MONum);
1111 errs() << "Expected a " << TRI->getRegClassName(DRC)
1112 << " register, but got a " << TRI->getRegClassName(RC)
1121 case MachineOperand::MO_RegisterMask:
1122 regMasks.push_back(MO->getRegMask());
1125 case MachineOperand::MO_MachineBasicBlock:
1126 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
1127 report("PHI operand is not in the CFG", MO, MONum);
1130 case MachineOperand::MO_FrameIndex:
1131 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
1132 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1133 int FI = MO->getIndex();
1134 LiveInterval &LI = LiveStks->getInterval(FI);
1135 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
1137 bool stores = MI->mayStore();
1138 bool loads = MI->mayLoad();
1139 // For a memory-to-memory move, we need to check if the frame
1140 // index is used for storing or loading, by inspecting the
1142 if (stores && loads) {
1143 for (auto *MMO : MI->memoperands()) {
1144 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1145 if (PSV == nullptr) continue;
1146 const FixedStackPseudoSourceValue *Value =
1147 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1148 if (Value == nullptr) continue;
1149 if (Value->getFrameIndex() != FI) continue;
1157 if (loads == stores)
1158 report("Missing fixed stack memoperand.", MI);
1160 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
1161 report("Instruction loads from dead spill slot", MO, MONum);
1162 errs() << "Live stack: " << LI << '\n';
1164 if (stores && !LI.liveAt(Idx.getRegSlot())) {
1165 report("Instruction stores to dead spill slot", MO, MONum);
1166 errs() << "Live stack: " << LI << '\n';
1176 void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
1177 unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
1178 LaneBitmask LaneMask) {
1179 LiveQueryResult LRQ = LR.Query(UseIdx);
1180 // Check if we have a segment at the use, note however that we only need one
1181 // live subregister range, the others may be dead.
1182 if (!LRQ.valueIn() && LaneMask.none()) {
1183 report("No live segment at use", MO, MONum);
1184 report_context_liverange(LR);
1185 report_context_vreg_regunit(VRegOrUnit);
1186 report_context(UseIdx);
1188 if (MO->isKill() && !LRQ.isKill()) {
1189 report("Live range continues after kill flag", MO, MONum);
1190 report_context_liverange(LR);
1191 report_context_vreg_regunit(VRegOrUnit);
1193 report_context_lanemask(LaneMask);
1194 report_context(UseIdx);
1198 void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
1199 unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
1200 LaneBitmask LaneMask) {
1201 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
1202 assert(VNI && "NULL valno is not allowed");
1203 if (VNI->def != DefIdx) {
1204 report("Inconsistent valno->def", MO, MONum);
1205 report_context_liverange(LR);
1206 report_context_vreg_regunit(VRegOrUnit);
1208 report_context_lanemask(LaneMask);
1209 report_context(*VNI);
1210 report_context(DefIdx);
1213 report("No live segment at def", MO, MONum);
1214 report_context_liverange(LR);
1215 report_context_vreg_regunit(VRegOrUnit);
1217 report_context_lanemask(LaneMask);
1218 report_context(DefIdx);
1220 // Check that, if the dead def flag is present, LiveInts agree.
1222 LiveQueryResult LRQ = LR.Query(DefIdx);
1223 if (!LRQ.isDeadDef()) {
1224 // In case of physregs we can have a non-dead definition on another
1226 bool otherDef = false;
1227 if (!TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
1228 const MachineInstr &MI = *MO->getParent();
1229 for (const MachineOperand &MO : MI.operands()) {
1230 if (!MO.isReg() || !MO.isDef() || MO.isDead())
1232 unsigned Reg = MO.getReg();
1233 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1234 if (*Units == VRegOrUnit) {
1243 report("Live range continues after dead def flag", MO, MONum);
1244 report_context_liverange(LR);
1245 report_context_vreg_regunit(VRegOrUnit);
1247 report_context_lanemask(LaneMask);
1253 void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1254 const MachineInstr *MI = MO->getParent();
1255 const unsigned Reg = MO->getReg();
1257 // Both use and def operands can read a register.
1258 if (MO->readsReg()) {
1259 regsLiveInButUnused.erase(Reg);
1262 addRegWithSubRegs(regsKilled, Reg);
1264 // Check that LiveVars knows this kill.
1265 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1267 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1268 if (!is_contained(VI.Kills, MI))
1269 report("Kill missing from LiveVariables", MO, MONum);
1272 // Check LiveInts liveness and kill.
1273 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1274 SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
1275 // Check the cached regunit intervals.
1276 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1277 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1278 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
1279 checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
1283 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1284 if (LiveInts->hasInterval(Reg)) {
1285 // This is a virtual register interval.
1286 const LiveInterval &LI = LiveInts->getInterval(Reg);
1287 checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
1289 if (LI.hasSubRanges() && !MO->isDef()) {
1290 unsigned SubRegIdx = MO->getSubReg();
1291 LaneBitmask MOMask = SubRegIdx != 0
1292 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1293 : MRI->getMaxLaneMaskForVReg(Reg);
1294 LaneBitmask LiveInMask;
1295 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1296 if ((MOMask & SR.LaneMask).none())
1298 checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
1299 LiveQueryResult LRQ = SR.Query(UseIdx);
1301 LiveInMask |= SR.LaneMask;
1303 // At least parts of the register has to be live at the use.
1304 if ((LiveInMask & MOMask).none()) {
1305 report("No live subrange at use", MO, MONum);
1307 report_context(UseIdx);
1311 report("Virtual register has no live interval", MO, MONum);
1316 // Use of a dead register.
1317 if (!regsLive.count(Reg)) {
1318 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1319 // Reserved registers may be used even when 'dead'.
1320 bool Bad = !isReserved(Reg);
1321 // We are fine if just any subregister has a defined value.
1323 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1325 if (regsLive.count(*SubRegs)) {
1331 // If there is an additional implicit-use of a super register we stop
1332 // here. By definition we are fine if the super register is not
1333 // (completely) dead, if the complete super register is dead we will
1334 // get a report for its operand.
1336 for (const MachineOperand &MOP : MI->uses()) {
1339 if (!MOP.isImplicit())
1341 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1343 if (*SubRegs == Reg) {
1351 report("Using an undefined physical register", MO, MONum);
1352 } else if (MRI->def_empty(Reg)) {
1353 report("Reading virtual register without a def", MO, MONum);
1355 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1356 // We don't know which virtual registers are live in, so only complain
1357 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1358 // must be live in. PHI instructions are handled separately.
1359 if (MInfo.regsKilled.count(Reg))
1360 report("Using a killed virtual register", MO, MONum);
1361 else if (!MI->isPHI())
1362 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1368 // Register defined.
1369 // TODO: verify that earlyclobber ops are not used.
1371 addRegWithSubRegs(regsDead, Reg);
1373 addRegWithSubRegs(regsDefined, Reg);
1376 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
1377 std::next(MRI->def_begin(Reg)) != MRI->def_end())
1378 report("Multiple virtual register defs in SSA form", MO, MONum);
1380 // Check LiveInts for a live segment, but only for virtual registers.
1381 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1382 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
1383 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
1385 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1386 if (LiveInts->hasInterval(Reg)) {
1387 const LiveInterval &LI = LiveInts->getInterval(Reg);
1388 checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
1390 if (LI.hasSubRanges()) {
1391 unsigned SubRegIdx = MO->getSubReg();
1392 LaneBitmask MOMask = SubRegIdx != 0
1393 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1394 : MRI->getMaxLaneMaskForVReg(Reg);
1395 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1396 if ((SR.LaneMask & MOMask).none())
1398 checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, SR.LaneMask);
1402 report("Virtual register has no Live interval", MO, MONum);
1409 void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
1412 // This function gets called after visiting all instructions in a bundle. The
1413 // argument points to the bundle header.
1414 // Normal stand-alone instructions are also considered 'bundles', and this
1415 // function is called for all of them.
1416 void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
1417 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1418 set_union(MInfo.regsKilled, regsKilled);
1419 set_subtract(regsLive, regsKilled); regsKilled.clear();
1420 // Kill any masked registers.
1421 while (!regMasks.empty()) {
1422 const uint32_t *Mask = regMasks.pop_back_val();
1423 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1424 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1425 MachineOperand::clobbersPhysReg(Mask, *I))
1426 regsDead.push_back(*I);
1428 set_subtract(regsLive, regsDead); regsDead.clear();
1429 set_union(regsLive, regsDefined); regsDefined.clear();
1433 MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
1434 MBBInfoMap[MBB].regsLiveOut = regsLive;
1438 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1439 if (!(stop > lastIndex)) {
1440 report("Block ends before last instruction index", MBB);
1441 errs() << "Block ends at " << stop
1442 << " last instruction was at " << lastIndex << '\n';
1448 // Calculate the largest possible vregsPassed sets. These are the registers that
1449 // can pass through an MBB live, but may not be live every time. It is assumed
1450 // that all vregsPassed sets are empty before the call.
1451 void MachineVerifier::calcRegsPassed() {
1452 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1453 // have any vregsPassed.
1454 SmallPtrSet<const MachineBasicBlock*, 8> todo;
1455 for (const auto &MBB : *MF) {
1456 BBInfo &MInfo = MBBInfoMap[&MBB];
1457 if (!MInfo.reachable)
1459 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1460 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1461 BBInfo &SInfo = MBBInfoMap[*SuI];
1462 if (SInfo.addPassed(MInfo.regsLiveOut))
1467 // Iteratively push vregsPassed to successors. This will converge to the same
1468 // final state regardless of DenseSet iteration order.
1469 while (!todo.empty()) {
1470 const MachineBasicBlock *MBB = *todo.begin();
1472 BBInfo &MInfo = MBBInfoMap[MBB];
1473 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1474 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1477 BBInfo &SInfo = MBBInfoMap[*SuI];
1478 if (SInfo.addPassed(MInfo.vregsPassed))
1484 // Calculate the set of virtual registers that must be passed through each basic
1485 // block in order to satisfy the requirements of successor blocks. This is very
1486 // similar to calcRegsPassed, only backwards.
1487 void MachineVerifier::calcRegsRequired() {
1488 // First push live-in regs to predecessors' vregsRequired.
1489 SmallPtrSet<const MachineBasicBlock*, 8> todo;
1490 for (const auto &MBB : *MF) {
1491 BBInfo &MInfo = MBBInfoMap[&MBB];
1492 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1493 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1494 BBInfo &PInfo = MBBInfoMap[*PrI];
1495 if (PInfo.addRequired(MInfo.vregsLiveIn))
1500 // Iteratively push vregsRequired to predecessors. This will converge to the
1501 // same final state regardless of DenseSet iteration order.
1502 while (!todo.empty()) {
1503 const MachineBasicBlock *MBB = *todo.begin();
1505 BBInfo &MInfo = MBBInfoMap[MBB];
1506 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1507 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1510 BBInfo &SInfo = MBBInfoMap[*PrI];
1511 if (SInfo.addRequired(MInfo.vregsRequired))
1517 // Check PHI instructions at the beginning of MBB. It is assumed that
1518 // calcRegsPassed has been run so BBInfo::isLiveOut is valid.
1519 void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
1520 SmallPtrSet<const MachineBasicBlock*, 8> seen;
1521 for (const auto &BBI : *MBB) {
1526 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1527 unsigned Reg = BBI.getOperand(i).getReg();
1528 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
1529 if (!Pre->isSuccessor(MBB))
1532 BBInfo &PrInfo = MBBInfoMap[Pre];
1533 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1534 report("PHI operand is not live-out from predecessor",
1535 &BBI.getOperand(i), i);
1538 // Did we see all predecessors?
1539 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1540 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1541 if (!seen.count(*PrI)) {
1542 report("Missing PHI operand", &BBI);
1543 errs() << "BB#" << (*PrI)->getNumber()
1544 << " is a predecessor according to the CFG.\n";
1550 void MachineVerifier::visitMachineFunctionAfter() {
1553 for (const auto &MBB : *MF) {
1554 BBInfo &MInfo = MBBInfoMap[&MBB];
1556 // Skip unreachable MBBs.
1557 if (!MInfo.reachable)
1563 // Now check liveness info if available
1566 // Check for killed virtual registers that should be live out.
1567 for (const auto &MBB : *MF) {
1568 BBInfo &MInfo = MBBInfoMap[&MBB];
1569 for (RegSet::iterator
1570 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1572 if (MInfo.regsKilled.count(*I)) {
1573 report("Virtual register killed in block, but needed live out.", &MBB);
1574 errs() << "Virtual register " << PrintReg(*I)
1575 << " is used after the block.\n";
1580 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1581 for (RegSet::iterator
1582 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1584 report("Virtual register defs don't dominate all uses.", MF);
1585 report_context_vreg(*I);
1590 verifyLiveVariables();
1592 verifyLiveIntervals();
1595 void MachineVerifier::verifyLiveVariables() {
1596 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
1597 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1598 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
1599 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1600 for (const auto &MBB : *MF) {
1601 BBInfo &MInfo = MBBInfoMap[&MBB];
1603 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1604 if (MInfo.vregsRequired.count(Reg)) {
1605 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1606 report("LiveVariables: Block missing from AliveBlocks", &MBB);
1607 errs() << "Virtual register " << PrintReg(Reg)
1608 << " must be live through the block.\n";
1611 if (VI.AliveBlocks.test(MBB.getNumber())) {
1612 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
1613 errs() << "Virtual register " << PrintReg(Reg)
1614 << " is not needed live through the block.\n";
1621 void MachineVerifier::verifyLiveIntervals() {
1622 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
1623 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1624 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
1626 // Spilling and splitting may leave unused registers around. Skip them.
1627 if (MRI->reg_nodbg_empty(Reg))
1630 if (!LiveInts->hasInterval(Reg)) {
1631 report("Missing live interval for virtual register", MF);
1632 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
1636 const LiveInterval &LI = LiveInts->getInterval(Reg);
1637 assert(Reg == LI.reg && "Invalid reg to interval mapping");
1638 verifyLiveInterval(LI);
1641 // Verify all the cached regunit intervals.
1642 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
1643 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1644 verifyLiveRange(*LR, i);
1647 void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
1648 const VNInfo *VNI, unsigned Reg,
1649 LaneBitmask LaneMask) {
1650 if (VNI->isUnused())
1653 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
1656 report("Value not live at VNInfo def and not marked unused", MF);
1657 report_context(LR, Reg, LaneMask);
1658 report_context(*VNI);
1662 if (DefVNI != VNI) {
1663 report("Live segment at def has different VNInfo", MF);
1664 report_context(LR, Reg, LaneMask);
1665 report_context(*VNI);
1669 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1671 report("Invalid VNInfo definition index", MF);
1672 report_context(LR, Reg, LaneMask);
1673 report_context(*VNI);
1677 if (VNI->isPHIDef()) {
1678 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1679 report("PHIDef VNInfo is not defined at MBB start", MBB);
1680 report_context(LR, Reg, LaneMask);
1681 report_context(*VNI);
1687 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1689 report("No instruction at VNInfo def index", MBB);
1690 report_context(LR, Reg, LaneMask);
1691 report_context(*VNI);
1696 bool hasDef = false;
1697 bool isEarlyClobber = false;
1698 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
1699 if (!MOI->isReg() || !MOI->isDef())
1701 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1702 if (MOI->getReg() != Reg)
1705 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1706 !TRI->hasRegUnit(MOI->getReg(), Reg))
1709 if (LaneMask.any() &&
1710 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask).none())
1713 if (MOI->isEarlyClobber())
1714 isEarlyClobber = true;
1718 report("Defining instruction does not modify register", MI);
1719 report_context(LR, Reg, LaneMask);
1720 report_context(*VNI);
1723 // Early clobber defs begin at USE slots, but other defs must begin at
1725 if (isEarlyClobber) {
1726 if (!VNI->def.isEarlyClobber()) {
1727 report("Early clobber def must be at an early-clobber slot", MBB);
1728 report_context(LR, Reg, LaneMask);
1729 report_context(*VNI);
1731 } else if (!VNI->def.isRegister()) {
1732 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
1733 report_context(LR, Reg, LaneMask);
1734 report_context(*VNI);
1739 void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1740 const LiveRange::const_iterator I,
1741 unsigned Reg, LaneBitmask LaneMask)
1743 const LiveRange::Segment &S = *I;
1744 const VNInfo *VNI = S.valno;
1745 assert(VNI && "Live segment has no valno");
1747 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
1748 report("Foreign valno in live segment", MF);
1749 report_context(LR, Reg, LaneMask);
1751 report_context(*VNI);
1754 if (VNI->isUnused()) {
1755 report("Live segment valno is marked unused", MF);
1756 report_context(LR, Reg, LaneMask);
1760 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
1762 report("Bad start of live segment, no basic block", MF);
1763 report_context(LR, Reg, LaneMask);
1767 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1768 if (S.start != MBBStartIdx && S.start != VNI->def) {
1769 report("Live segment must begin at MBB entry or valno def", MBB);
1770 report_context(LR, Reg, LaneMask);
1774 const MachineBasicBlock *EndMBB =
1775 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
1777 report("Bad end of live segment, no basic block", MF);
1778 report_context(LR, Reg, LaneMask);
1783 // No more checks for live-out segments.
1784 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
1787 // RegUnit intervals are allowed dead phis.
1788 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1789 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
1792 // The live segment is ending inside EndMBB
1793 const MachineInstr *MI =
1794 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
1796 report("Live segment doesn't end at a valid instruction", EndMBB);
1797 report_context(LR, Reg, LaneMask);
1802 // The block slot must refer to a basic block boundary.
1803 if (S.end.isBlock()) {
1804 report("Live segment ends at B slot of an instruction", EndMBB);
1805 report_context(LR, Reg, LaneMask);
1809 if (S.end.isDead()) {
1810 // Segment ends on the dead slot.
1811 // That means there must be a dead def.
1812 if (!SlotIndex::isSameInstr(S.start, S.end)) {
1813 report("Live segment ending at dead slot spans instructions", EndMBB);
1814 report_context(LR, Reg, LaneMask);
1819 // A live segment can only end at an early-clobber slot if it is being
1820 // redefined by an early-clobber def.
1821 if (S.end.isEarlyClobber()) {
1822 if (I+1 == LR.end() || (I+1)->start != S.end) {
1823 report("Live segment ending at early clobber slot must be "
1824 "redefined by an EC def in the same instruction", EndMBB);
1825 report_context(LR, Reg, LaneMask);
1830 // The following checks only apply to virtual registers. Physreg liveness
1831 // is too weird to check.
1832 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1833 // A live segment can end with either a redefinition, a kill flag on a
1834 // use, or a dead flag on a def.
1835 bool hasRead = false;
1836 bool hasSubRegDef = false;
1837 bool hasDeadDef = false;
1838 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
1839 if (!MOI->isReg() || MOI->getReg() != Reg)
1841 unsigned Sub = MOI->getSubReg();
1842 LaneBitmask SLM = Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub)
1843 : LaneBitmask::getAll();
1846 hasSubRegDef = true;
1847 // An operand vreg0:sub0<def> reads vreg0:sub1..n. Invert the lane
1848 // mask for subregister defs. Read-undef defs will be handled by
1855 if (LaneMask.any() && (LaneMask & SLM).none())
1857 if (MOI->readsReg())
1860 if (S.end.isDead()) {
1861 // Make sure that the corresponding machine operand for a "dead" live
1862 // range has the dead flag. We cannot perform this check for subregister
1863 // liveranges as partially dead values are allowed.
1864 if (LaneMask.none() && !hasDeadDef) {
1865 report("Instruction ending live segment on dead slot has no dead flag",
1867 report_context(LR, Reg, LaneMask);
1872 // When tracking subregister liveness, the main range must start new
1873 // values on partial register writes, even if there is no read.
1874 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask.any() ||
1876 report("Instruction ending live segment doesn't read the register",
1878 report_context(LR, Reg, LaneMask);
1885 // Now check all the basic blocks in this live segment.
1886 MachineFunction::const_iterator MFI = MBB->getIterator();
1887 // Is this live segment the beginning of a non-PHIDef VN?
1888 if (S.start == VNI->def && !VNI->isPHIDef()) {
1889 // Not live-in to any blocks.
1896 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
1897 // We don't know how to track physregs into a landing pad.
1898 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
1900 if (&*MFI == EndMBB)
1906 // Is VNI a PHI-def in the current block?
1907 bool IsPHI = VNI->isPHIDef() &&
1908 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
1910 // Check that VNI is live-out of all predecessors.
1911 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1912 PE = MFI->pred_end(); PI != PE; ++PI) {
1913 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1914 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
1916 // All predecessors must have a live-out value if this is not a
1917 // subregister liverange.
1918 if (!PVNI && LaneMask.none()) {
1919 report("Register not marked live out of predecessor", *PI);
1920 report_context(LR, Reg, LaneMask);
1921 report_context(*VNI);
1922 errs() << " live into BB#" << MFI->getNumber()
1923 << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
1928 // Only PHI-defs can take different predecessor values.
1929 if (!IsPHI && PVNI != VNI) {
1930 report("Different value live out of predecessor", *PI);
1931 report_context(LR, Reg, LaneMask);
1932 errs() << "Valno #" << PVNI->id << " live out of BB#"
1933 << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
1934 << " live into BB#" << MFI->getNumber() << '@'
1935 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
1938 if (&*MFI == EndMBB)
1944 void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
1945 LaneBitmask LaneMask) {
1946 for (const VNInfo *VNI : LR.valnos)
1947 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
1949 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
1950 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
1953 void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1954 unsigned Reg = LI.reg;
1955 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1956 verifyLiveRange(LI, Reg);
1959 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
1960 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1961 if ((Mask & SR.LaneMask).any()) {
1962 report("Lane masks of sub ranges overlap in live interval", MF);
1965 if ((SR.LaneMask & ~MaxMask).any()) {
1966 report("Subrange lanemask is invalid", MF);
1970 report("Subrange must not be empty", MF);
1971 report_context(SR, LI.reg, SR.LaneMask);
1973 Mask |= SR.LaneMask;
1974 verifyLiveRange(SR, LI.reg, SR.LaneMask);
1975 if (!LI.covers(SR)) {
1976 report("A Subrange is not covered by the main range", MF);
1981 // Check the LI only has one connected component.
1982 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1983 unsigned NumComp = ConEQ.Classify(LI);
1985 report("Multiple connected components in live interval", MF);
1987 for (unsigned comp = 0; comp != NumComp; ++comp) {
1988 errs() << comp << ": valnos";
1989 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1990 E = LI.vni_end(); I!=E; ++I)
1991 if (comp == ConEQ.getEqClass(*I))
1992 errs() << ' ' << (*I)->id;
1999 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
2000 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
2002 // We use a bool plus an integer to capture the stack state.
2003 struct StackStateOfBB {
2004 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
2005 ExitIsSetup(false) { }
2006 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
2007 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
2008 ExitIsSetup(ExitSetup) { }
2009 // Can be negative, which means we are setting up a frame.
2017 /// Make sure on every path through the CFG, a FrameSetup <n> is always followed
2018 /// by a FrameDestroy <n>, stack adjustments are identical on all
2019 /// CFG edges to a merge point, and frame is destroyed at end of a return block.
2020 void MachineVerifier::verifyStackFrame() {
2021 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
2022 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
2024 SmallVector<StackStateOfBB, 8> SPState;
2025 SPState.resize(MF->getNumBlockIDs());
2026 df_iterator_default_set<const MachineBasicBlock*> Reachable;
2028 // Visit the MBBs in DFS order.
2029 for (df_ext_iterator<const MachineFunction*,
2030 df_iterator_default_set<const MachineBasicBlock*> >
2031 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
2032 DFI != DFE; ++DFI) {
2033 const MachineBasicBlock *MBB = *DFI;
2035 StackStateOfBB BBState;
2036 // Check the exit state of the DFS stack predecessor.
2037 if (DFI.getPathLength() >= 2) {
2038 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
2039 assert(Reachable.count(StackPred) &&
2040 "DFS stack predecessor is already visited.\n");
2041 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
2042 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
2043 BBState.ExitValue = BBState.EntryValue;
2044 BBState.ExitIsSetup = BBState.EntryIsSetup;
2047 // Update stack state by checking contents of MBB.
2048 for (const auto &I : *MBB) {
2049 if (I.getOpcode() == FrameSetupOpcode) {
2050 // The first operand of a FrameOpcode should be i32.
2051 int Size = I.getOperand(0).getImm();
2053 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
2055 if (BBState.ExitIsSetup)
2056 report("FrameSetup is after another FrameSetup", &I);
2057 BBState.ExitValue -= Size;
2058 BBState.ExitIsSetup = true;
2061 if (I.getOpcode() == FrameDestroyOpcode) {
2062 // The first operand of a FrameOpcode should be i32.
2063 int Size = I.getOperand(0).getImm();
2065 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
2067 if (!BBState.ExitIsSetup)
2068 report("FrameDestroy is not after a FrameSetup", &I);
2069 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
2071 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
2072 report("FrameDestroy <n> is after FrameSetup <m>", &I);
2073 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
2074 << AbsSPAdj << ">.\n";
2076 BBState.ExitValue += Size;
2077 BBState.ExitIsSetup = false;
2080 SPState[MBB->getNumber()] = BBState;
2082 // Make sure the exit state of any predecessor is consistent with the entry
2084 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
2085 E = MBB->pred_end(); I != E; ++I) {
2086 if (Reachable.count(*I) &&
2087 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
2088 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
2089 report("The exit stack state of a predecessor is inconsistent.", MBB);
2090 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
2091 << SPState[(*I)->getNumber()].ExitValue << ", "
2092 << SPState[(*I)->getNumber()].ExitIsSetup
2093 << "), while BB#" << MBB->getNumber() << " has entry state ("
2094 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
2098 // Make sure the entry state of any successor is consistent with the exit
2100 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
2101 E = MBB->succ_end(); I != E; ++I) {
2102 if (Reachable.count(*I) &&
2103 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
2104 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
2105 report("The entry stack state of a successor is inconsistent.", MBB);
2106 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
2107 << SPState[(*I)->getNumber()].EntryValue << ", "
2108 << SPState[(*I)->getNumber()].EntryIsSetup
2109 << "), while BB#" << MBB->getNumber() << " has exit state ("
2110 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
2114 // Make sure a basic block with return ends with zero stack adjustment.
2115 if (!MBB->empty() && MBB->back().isReturn()) {
2116 if (BBState.ExitIsSetup)
2117 report("A return block ends with a FrameSetup.", MBB);
2118 if (BBState.ExitValue)
2119 report("A return block ends with a nonzero stack adjustment.", MBB);