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/Instructions.h"
27 #include "llvm/Function.h"
28 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
29 #include "llvm/CodeGen/LiveVariables.h"
30 #include "llvm/CodeGen/LiveStackAnalysis.h"
31 #include "llvm/CodeGen/MachineInstrBundle.h"
32 #include "llvm/CodeGen/MachineFunctionPass.h"
33 #include "llvm/CodeGen/MachineFrameInfo.h"
34 #include "llvm/CodeGen/MachineMemOperand.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/Passes.h"
37 #include "llvm/MC/MCAsmInfo.h"
38 #include "llvm/Target/TargetMachine.h"
39 #include "llvm/Target/TargetRegisterInfo.h"
40 #include "llvm/Target/TargetInstrInfo.h"
41 #include "llvm/ADT/DenseSet.h"
42 #include "llvm/ADT/SetOperations.h"
43 #include "llvm/ADT/SmallVector.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/ErrorHandling.h"
46 #include "llvm/Support/raw_ostream.h"
50 struct MachineVerifier {
52 MachineVerifier(Pass *pass, const char *b) :
55 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
58 bool runOnMachineFunction(MachineFunction &MF);
62 const char *const OutFileName;
64 const MachineFunction *MF;
65 const TargetMachine *TM;
66 const TargetInstrInfo *TII;
67 const TargetRegisterInfo *TRI;
68 const MachineRegisterInfo *MRI;
72 typedef SmallVector<unsigned, 16> RegVector;
73 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
74 typedef DenseSet<unsigned> RegSet;
75 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
76 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
78 const MachineInstr *FirstTerminator;
79 BlockSet FunctionBlocks;
81 BitVector regsReserved;
82 BitVector regsAllocatable;
84 RegVector regsDefined, regsDead, regsKilled;
85 RegMaskVector regMasks;
86 RegSet regsLiveInButUnused;
90 // Add Reg and any sub-registers to RV
91 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
93 if (TargetRegisterInfo::isPhysicalRegister(Reg))
94 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
95 RV.push_back(*SubRegs);
99 // Is this MBB reachable from the MF entry point?
102 // Vregs that must be live in because they are used without being
103 // defined. Map value is the user.
106 // Regs killed in MBB. They may be defined again, and will then be in both
107 // regsKilled and regsLiveOut.
110 // Regs defined in MBB and live out. Note that vregs passing through may
111 // be live out without being mentioned here.
114 // Vregs that pass through MBB untouched. This set is disjoint from
115 // regsKilled and regsLiveOut.
118 // Vregs that must pass through MBB because they are needed by a successor
119 // block. This set is disjoint from regsLiveOut.
120 RegSet vregsRequired;
122 // Set versions of block's predecessor and successor lists.
123 BlockSet Preds, Succs;
125 BBInfo() : reachable(false) {}
127 // Add register to vregsPassed if it belongs there. Return true if
129 bool addPassed(unsigned Reg) {
130 if (!TargetRegisterInfo::isVirtualRegister(Reg))
132 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
134 return vregsPassed.insert(Reg).second;
137 // Same for a full set.
138 bool addPassed(const RegSet &RS) {
139 bool changed = false;
140 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
146 // Add register to vregsRequired if it belongs there. Return true if
148 bool addRequired(unsigned Reg) {
149 if (!TargetRegisterInfo::isVirtualRegister(Reg))
151 if (regsLiveOut.count(Reg))
153 return vregsRequired.insert(Reg).second;
156 // Same for a full set.
157 bool addRequired(const RegSet &RS) {
158 bool changed = false;
159 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
165 // Same for a full map.
166 bool addRequired(const RegMap &RM) {
167 bool changed = false;
168 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
169 if (addRequired(I->first))
174 // Live-out registers are either in regsLiveOut or vregsPassed.
175 bool isLiveOut(unsigned Reg) const {
176 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
180 // Extra register info per MBB.
181 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
183 bool isReserved(unsigned Reg) {
184 return Reg < regsReserved.size() && regsReserved.test(Reg);
187 bool isAllocatable(unsigned Reg) {
188 return Reg < regsAllocatable.size() && regsAllocatable.test(Reg);
191 // Analysis information if available
192 LiveVariables *LiveVars;
193 LiveIntervals *LiveInts;
194 LiveStacks *LiveStks;
195 SlotIndexes *Indexes;
197 void visitMachineFunctionBefore();
198 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
199 void visitMachineBundleBefore(const MachineInstr *MI);
200 void visitMachineInstrBefore(const MachineInstr *MI);
201 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
202 void visitMachineInstrAfter(const MachineInstr *MI);
203 void visitMachineBundleAfter(const MachineInstr *MI);
204 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
205 void visitMachineFunctionAfter();
207 void report(const char *msg, const MachineFunction *MF);
208 void report(const char *msg, const MachineBasicBlock *MBB);
209 void report(const char *msg, const MachineInstr *MI);
210 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
211 void report(const char *msg, const MachineFunction *MF,
212 const LiveInterval &LI);
213 void report(const char *msg, const MachineBasicBlock *MBB,
214 const LiveInterval &LI);
216 void checkLiveness(const MachineOperand *MO, unsigned MONum);
217 void markReachable(const MachineBasicBlock *MBB);
218 void calcRegsPassed();
219 void checkPHIOps(const MachineBasicBlock *MBB);
221 void calcRegsRequired();
222 void verifyLiveVariables();
223 void verifyLiveIntervals();
224 void verifyLiveInterval(const LiveInterval&);
225 void verifyLiveIntervalValue(const LiveInterval&, VNInfo*);
226 void verifyLiveIntervalSegment(const LiveInterval&,
227 LiveInterval::const_iterator);
230 struct MachineVerifierPass : public MachineFunctionPass {
231 static char ID; // Pass ID, replacement for typeid
232 const char *const Banner;
234 MachineVerifierPass(const char *b = 0)
235 : MachineFunctionPass(ID), Banner(b) {
236 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
239 void getAnalysisUsage(AnalysisUsage &AU) const {
240 AU.setPreservesAll();
241 MachineFunctionPass::getAnalysisUsage(AU);
244 bool runOnMachineFunction(MachineFunction &MF) {
245 MF.verify(this, Banner);
252 char MachineVerifierPass::ID = 0;
253 INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
254 "Verify generated machine code", false, false)
256 FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
257 return new MachineVerifierPass(Banner);
260 void MachineFunction::verify(Pass *p, const char *Banner) const {
261 MachineVerifier(p, Banner)
262 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
265 bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
266 raw_ostream *OutFile = 0;
268 std::string ErrorInfo;
269 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
270 raw_fd_ostream::F_Append);
271 if (!ErrorInfo.empty()) {
272 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
284 TM = &MF.getTarget();
285 TII = TM->getInstrInfo();
286 TRI = TM->getRegisterInfo();
287 MRI = &MF.getRegInfo();
294 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
295 // We don't want to verify LiveVariables if LiveIntervals is available.
297 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
298 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
299 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
302 visitMachineFunctionBefore();
303 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
305 visitMachineBasicBlockBefore(MFI);
306 // Keep track of the current bundle header.
307 const MachineInstr *CurBundle = 0;
308 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
309 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
310 if (MBBI->getParent() != MFI) {
311 report("Bad instruction parent pointer", MFI);
312 *OS << "Instruction: " << *MBBI;
315 // Is this a bundle header?
316 if (!MBBI->isInsideBundle()) {
318 visitMachineBundleAfter(CurBundle);
320 visitMachineBundleBefore(CurBundle);
321 } else if (!CurBundle)
322 report("No bundle header", MBBI);
323 visitMachineInstrBefore(MBBI);
324 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
325 visitMachineOperand(&MBBI->getOperand(I), I);
326 visitMachineInstrAfter(MBBI);
329 visitMachineBundleAfter(CurBundle);
330 visitMachineBasicBlockAfter(MFI);
332 visitMachineFunctionAfter();
336 else if (foundErrors)
337 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
345 regsLiveInButUnused.clear();
348 return false; // no changes
351 void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
354 if (!foundErrors++) {
356 *OS << "# " << Banner << '\n';
357 MF->print(*OS, Indexes);
359 *OS << "*** Bad machine code: " << msg << " ***\n"
360 << "- function: " << MF->getFunction()->getName() << "\n";
363 void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
365 report(msg, MBB->getParent());
366 *OS << "- basic block: BB#" << MBB->getNumber()
367 << ' ' << MBB->getName()
368 << " (" << (void*)MBB << ')';
370 *OS << " [" << Indexes->getMBBStartIdx(MBB)
371 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
375 void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
377 report(msg, MI->getParent());
378 *OS << "- instruction: ";
379 if (Indexes && Indexes->hasIndex(MI))
380 *OS << Indexes->getInstructionIndex(MI) << '\t';
384 void MachineVerifier::report(const char *msg,
385 const MachineOperand *MO, unsigned MONum) {
387 report(msg, MO->getParent());
388 *OS << "- operand " << MONum << ": ";
393 void MachineVerifier::report(const char *msg, const MachineFunction *MF,
394 const LiveInterval &LI) {
396 *OS << "- interval: ";
397 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
398 *OS << PrintReg(LI.reg, TRI);
400 *OS << PrintRegUnit(LI.reg, TRI);
401 *OS << ' ' << LI << '\n';
404 void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
405 const LiveInterval &LI) {
407 *OS << "- interval: ";
408 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
409 *OS << PrintReg(LI.reg, TRI);
411 *OS << PrintRegUnit(LI.reg, TRI);
412 *OS << ' ' << LI << '\n';
415 void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
416 BBInfo &MInfo = MBBInfoMap[MBB];
417 if (!MInfo.reachable) {
418 MInfo.reachable = true;
419 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
420 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
425 void MachineVerifier::visitMachineFunctionBefore() {
426 lastIndex = SlotIndex();
427 regsReserved = TRI->getReservedRegs(*MF);
429 // A sub-register of a reserved register is also reserved
430 for (int Reg = regsReserved.find_first(); Reg>=0;
431 Reg = regsReserved.find_next(Reg)) {
432 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
433 // FIXME: This should probably be:
434 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
435 regsReserved.set(*SubRegs);
439 regsAllocatable = TRI->getAllocatableSet(*MF);
441 markReachable(&MF->front());
443 // Build a set of the basic blocks in the function.
444 FunctionBlocks.clear();
445 for (MachineFunction::const_iterator
446 I = MF->begin(), E = MF->end(); I != E; ++I) {
447 FunctionBlocks.insert(I);
448 BBInfo &MInfo = MBBInfoMap[I];
450 MInfo.Preds.insert(I->pred_begin(), I->pred_end());
451 if (MInfo.Preds.size() != I->pred_size())
452 report("MBB has duplicate entries in its predecessor list.", I);
454 MInfo.Succs.insert(I->succ_begin(), I->succ_end());
455 if (MInfo.Succs.size() != I->succ_size())
456 report("MBB has duplicate entries in its successor list.", I);
460 // Does iterator point to a and b as the first two elements?
461 static bool matchPair(MachineBasicBlock::const_succ_iterator i,
462 const MachineBasicBlock *a, const MachineBasicBlock *b) {
471 MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
475 // If this block has allocatable physical registers live-in, check that
476 // it is an entry block or landing pad.
477 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
478 LE = MBB->livein_end();
481 if (isAllocatable(reg) && !MBB->isLandingPad() &&
482 MBB != MBB->getParent()->begin()) {
483 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
488 // Count the number of landing pad successors.
489 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
490 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
491 E = MBB->succ_end(); I != E; ++I) {
492 if ((*I)->isLandingPad())
493 LandingPadSuccs.insert(*I);
494 if (!FunctionBlocks.count(*I))
495 report("MBB has successor that isn't part of the function.", MBB);
496 if (!MBBInfoMap[*I].Preds.count(MBB)) {
497 report("Inconsistent CFG", MBB);
498 *OS << "MBB is not in the predecessor list of the successor BB#"
499 << (*I)->getNumber() << ".\n";
503 // Check the predecessor list.
504 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
505 E = MBB->pred_end(); I != E; ++I) {
506 if (!FunctionBlocks.count(*I))
507 report("MBB has predecessor that isn't part of the function.", MBB);
508 if (!MBBInfoMap[*I].Succs.count(MBB)) {
509 report("Inconsistent CFG", MBB);
510 *OS << "MBB is not in the successor list of the predecessor BB#"
511 << (*I)->getNumber() << ".\n";
515 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
516 const BasicBlock *BB = MBB->getBasicBlock();
517 if (LandingPadSuccs.size() > 1 &&
519 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
520 BB && isa<SwitchInst>(BB->getTerminator())))
521 report("MBB has more than one landing pad successor", MBB);
523 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
524 MachineBasicBlock *TBB = 0, *FBB = 0;
525 SmallVector<MachineOperand, 4> Cond;
526 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
528 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
529 // check whether its answers match up with reality.
531 // Block falls through to its successor.
532 MachineFunction::const_iterator MBBI = MBB;
534 if (MBBI == MF->end()) {
535 // It's possible that the block legitimately ends with a noreturn
536 // call or an unreachable, in which case it won't actually fall
537 // out the bottom of the function.
538 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
539 // It's possible that the block legitimately ends with a noreturn
540 // call or an unreachable, in which case it won't actuall fall
542 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
543 report("MBB exits via unconditional fall-through but doesn't have "
544 "exactly one CFG successor!", MBB);
545 } else if (!MBB->isSuccessor(MBBI)) {
546 report("MBB exits via unconditional fall-through but its successor "
547 "differs from its CFG successor!", MBB);
549 if (!MBB->empty() && getBundleStart(&MBB->back())->isBarrier() &&
550 !TII->isPredicated(getBundleStart(&MBB->back()))) {
551 report("MBB exits via unconditional fall-through but ends with a "
552 "barrier instruction!", MBB);
555 report("MBB exits via unconditional fall-through but has a condition!",
558 } else if (TBB && !FBB && Cond.empty()) {
559 // Block unconditionally branches somewhere.
560 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
561 report("MBB exits via unconditional branch but doesn't have "
562 "exactly one CFG successor!", MBB);
563 } else if (!MBB->isSuccessor(TBB)) {
564 report("MBB exits via unconditional branch but the CFG "
565 "successor doesn't match the actual successor!", MBB);
568 report("MBB exits via unconditional branch but doesn't contain "
569 "any instructions!", MBB);
570 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
571 report("MBB exits via unconditional branch but doesn't end with a "
572 "barrier instruction!", MBB);
573 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
574 report("MBB exits via unconditional branch but the branch isn't a "
575 "terminator instruction!", MBB);
577 } else if (TBB && !FBB && !Cond.empty()) {
578 // Block conditionally branches somewhere, otherwise falls through.
579 MachineFunction::const_iterator MBBI = MBB;
581 if (MBBI == MF->end()) {
582 report("MBB conditionally falls through out of function!", MBB);
583 } if (MBB->succ_size() != 2) {
584 report("MBB exits via conditional branch/fall-through but doesn't have "
585 "exactly two CFG successors!", MBB);
586 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
587 report("MBB exits via conditional branch/fall-through but the CFG "
588 "successors don't match the actual successors!", MBB);
591 report("MBB exits via conditional branch/fall-through but doesn't "
592 "contain any instructions!", MBB);
593 } else if (getBundleStart(&MBB->back())->isBarrier()) {
594 report("MBB exits via conditional branch/fall-through but ends with a "
595 "barrier instruction!", MBB);
596 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
597 report("MBB exits via conditional branch/fall-through but the branch "
598 "isn't a terminator instruction!", MBB);
600 } else if (TBB && FBB) {
601 // Block conditionally branches somewhere, otherwise branches
603 if (MBB->succ_size() != 2) {
604 report("MBB exits via conditional branch/branch but doesn't have "
605 "exactly two CFG successors!", MBB);
606 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
607 report("MBB exits via conditional branch/branch but the CFG "
608 "successors don't match the actual successors!", MBB);
611 report("MBB exits via conditional branch/branch but doesn't "
612 "contain any instructions!", MBB);
613 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
614 report("MBB exits via conditional branch/branch but doesn't end with a "
615 "barrier instruction!", MBB);
616 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
617 report("MBB exits via conditional branch/branch but the branch "
618 "isn't a terminator instruction!", MBB);
621 report("MBB exits via conditinal branch/branch but there's no "
625 report("AnalyzeBranch returned invalid data!", MBB);
630 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
631 E = MBB->livein_end(); I != E; ++I) {
632 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
633 report("MBB live-in list contains non-physical register", MBB);
637 for (MCSubRegIterator SubRegs(*I, TRI); SubRegs.isValid(); ++SubRegs)
638 regsLive.insert(*SubRegs);
640 regsLiveInButUnused = regsLive;
642 const MachineFrameInfo *MFI = MF->getFrameInfo();
643 assert(MFI && "Function has no frame info");
644 BitVector PR = MFI->getPristineRegs(MBB);
645 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
647 for (MCSubRegIterator SubRegs(I, TRI); SubRegs.isValid(); ++SubRegs)
648 regsLive.insert(*SubRegs);
655 lastIndex = Indexes->getMBBStartIdx(MBB);
658 // This function gets called for all bundle headers, including normal
659 // stand-alone unbundled instructions.
660 void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
661 if (Indexes && Indexes->hasIndex(MI)) {
662 SlotIndex idx = Indexes->getInstructionIndex(MI);
663 if (!(idx > lastIndex)) {
664 report("Instruction index out of order", MI);
665 *OS << "Last instruction was at " << lastIndex << '\n';
670 // Ensure non-terminators don't follow terminators.
671 // Ignore predicated terminators formed by if conversion.
672 // FIXME: If conversion shouldn't need to violate this rule.
673 if (MI->isTerminator() && !TII->isPredicated(MI)) {
674 if (!FirstTerminator)
675 FirstTerminator = MI;
676 } else if (FirstTerminator) {
677 report("Non-terminator instruction after the first terminator", MI);
678 *OS << "First terminator was:\t" << *FirstTerminator;
682 void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
683 const MCInstrDesc &MCID = MI->getDesc();
684 if (MI->getNumOperands() < MCID.getNumOperands()) {
685 report("Too few operands", MI);
686 *OS << MCID.getNumOperands() << " operands expected, but "
687 << MI->getNumExplicitOperands() << " given.\n";
690 // Check the MachineMemOperands for basic consistency.
691 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
692 E = MI->memoperands_end(); I != E; ++I) {
693 if ((*I)->isLoad() && !MI->mayLoad())
694 report("Missing mayLoad flag", MI);
695 if ((*I)->isStore() && !MI->mayStore())
696 report("Missing mayStore flag", MI);
699 // Debug values must not have a slot index.
700 // Other instructions must have one, unless they are inside a bundle.
702 bool mapped = !LiveInts->isNotInMIMap(MI);
703 if (MI->isDebugValue()) {
705 report("Debug instruction has a slot index", MI);
706 } else if (MI->isInsideBundle()) {
708 report("Instruction inside bundle has a slot index", MI);
711 report("Missing slot index", MI);
716 if (!TII->verifyInstruction(MI, ErrorInfo))
717 report(ErrorInfo.data(), MI);
721 MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
722 const MachineInstr *MI = MO->getParent();
723 const MCInstrDesc &MCID = MI->getDesc();
725 // The first MCID.NumDefs operands must be explicit register defines
726 if (MONum < MCID.getNumDefs()) {
727 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
729 report("Explicit definition must be a register", MO, MONum);
730 else if (!MO->isDef() && !MCOI.isOptionalDef())
731 report("Explicit definition marked as use", MO, MONum);
732 else if (MO->isImplicit())
733 report("Explicit definition marked as implicit", MO, MONum);
734 } else if (MONum < MCID.getNumOperands()) {
735 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
736 // Don't check if it's the last operand in a variadic instruction. See,
737 // e.g., LDM_RET in the arm back end.
739 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
740 if (MO->isDef() && !MCOI.isOptionalDef())
741 report("Explicit operand marked as def", MO, MONum);
742 if (MO->isImplicit())
743 report("Explicit operand marked as implicit", MO, MONum);
746 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
747 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
748 report("Extra explicit operand on non-variadic instruction", MO, MONum);
751 switch (MO->getType()) {
752 case MachineOperand::MO_Register: {
753 const unsigned Reg = MO->getReg();
756 if (MRI->tracksLiveness() && !MI->isDebugValue())
757 checkLiveness(MO, MONum);
759 // Verify two-address constraints after leaving SSA form.
761 if (!MRI->isSSA() && MO->isUse() &&
762 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
763 Reg != MI->getOperand(DefIdx).getReg())
764 report("Two-address instruction operands must be identical", MO, MONum);
766 // Check register classes.
767 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
768 unsigned SubIdx = MO->getSubReg();
770 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
772 report("Illegal subregister index for physical register", MO, MONum);
775 if (const TargetRegisterClass *DRC =
776 TII->getRegClass(MCID, MONum, TRI, *MF)) {
777 if (!DRC->contains(Reg)) {
778 report("Illegal physical register for instruction", MO, MONum);
779 *OS << TRI->getName(Reg) << " is not a "
780 << DRC->getName() << " register.\n";
785 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
787 const TargetRegisterClass *SRC =
788 TRI->getSubClassWithSubReg(RC, SubIdx);
790 report("Invalid subregister index for virtual register", MO, MONum);
791 *OS << "Register class " << RC->getName()
792 << " does not support subreg index " << SubIdx << "\n";
796 report("Invalid register class for subregister index", MO, MONum);
797 *OS << "Register class " << RC->getName()
798 << " does not fully support subreg index " << SubIdx << "\n";
802 if (const TargetRegisterClass *DRC =
803 TII->getRegClass(MCID, MONum, TRI, *MF)) {
805 const TargetRegisterClass *SuperRC =
806 TRI->getLargestLegalSuperClass(RC);
808 report("No largest legal super class exists.", MO, MONum);
811 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
813 report("No matching super-reg register class.", MO, MONum);
817 if (!RC->hasSuperClassEq(DRC)) {
818 report("Illegal virtual register for instruction", MO, MONum);
819 *OS << "Expected a " << DRC->getName() << " register, but got a "
820 << RC->getName() << " register\n";
828 case MachineOperand::MO_RegisterMask:
829 regMasks.push_back(MO->getRegMask());
832 case MachineOperand::MO_MachineBasicBlock:
833 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
834 report("PHI operand is not in the CFG", MO, MONum);
837 case MachineOperand::MO_FrameIndex:
838 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
839 LiveInts && !LiveInts->isNotInMIMap(MI)) {
840 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
841 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
842 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
843 report("Instruction loads from dead spill slot", MO, MONum);
844 *OS << "Live stack: " << LI << '\n';
846 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
847 report("Instruction stores to dead spill slot", MO, MONum);
848 *OS << "Live stack: " << LI << '\n';
858 void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
859 const MachineInstr *MI = MO->getParent();
860 const unsigned Reg = MO->getReg();
862 // Both use and def operands can read a register.
863 if (MO->readsReg()) {
864 regsLiveInButUnused.erase(Reg);
867 addRegWithSubRegs(regsKilled, Reg);
869 // Check that LiveVars knows this kill.
870 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
872 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
873 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
874 report("Kill missing from LiveVariables", MO, MONum);
877 // Check LiveInts liveness and kill.
878 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
879 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
880 // Check the cached regunit intervals.
881 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
882 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
883 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(*Units)) {
884 LiveRangeQuery LRQ(*LI, UseIdx);
885 if (!LRQ.valueIn()) {
886 report("No live range at use", MO, MONum);
887 *OS << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
888 << ' ' << *LI << '\n';
890 if (MO->isKill() && !LRQ.isKill()) {
891 report("Live range continues after kill flag", MO, MONum);
892 *OS << PrintRegUnit(*Units, TRI) << ' ' << *LI << '\n';
898 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
899 if (LiveInts->hasInterval(Reg)) {
900 // This is a virtual register interval.
901 const LiveInterval &LI = LiveInts->getInterval(Reg);
902 LiveRangeQuery LRQ(LI, UseIdx);
903 if (!LRQ.valueIn()) {
904 report("No live range at use", MO, MONum);
905 *OS << UseIdx << " is not live in " << LI << '\n';
907 // Check for extra kill flags.
908 // Note that we allow missing kill flags for now.
909 if (MO->isKill() && !LRQ.isKill()) {
910 report("Live range continues after kill flag", MO, MONum);
911 *OS << "Live range: " << LI << '\n';
914 report("Virtual register has no live interval", MO, MONum);
919 // Use of a dead register.
920 if (!regsLive.count(Reg)) {
921 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
922 // Reserved registers may be used even when 'dead'.
923 if (!isReserved(Reg))
924 report("Using an undefined physical register", MO, MONum);
925 } else if (MRI->def_empty(Reg)) {
926 report("Reading virtual register without a def", MO, MONum);
928 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
929 // We don't know which virtual registers are live in, so only complain
930 // if vreg was killed in this MBB. Otherwise keep track of vregs that
931 // must be live in. PHI instructions are handled separately.
932 if (MInfo.regsKilled.count(Reg))
933 report("Using a killed virtual register", MO, MONum);
934 else if (!MI->isPHI())
935 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
942 // TODO: verify that earlyclobber ops are not used.
944 addRegWithSubRegs(regsDead, Reg);
946 addRegWithSubRegs(regsDefined, Reg);
949 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
950 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
951 report("Multiple virtual register defs in SSA form", MO, MONum);
953 // Check LiveInts for a live range, but only for virtual registers.
954 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
955 !LiveInts->isNotInMIMap(MI)) {
956 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
957 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
958 if (LiveInts->hasInterval(Reg)) {
959 const LiveInterval &LI = LiveInts->getInterval(Reg);
960 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
961 assert(VNI && "NULL valno is not allowed");
962 if (VNI->def != DefIdx) {
963 report("Inconsistent valno->def", MO, MONum);
964 *OS << "Valno " << VNI->id << " is not defined at "
965 << DefIdx << " in " << LI << '\n';
968 report("No live range at def", MO, MONum);
969 *OS << DefIdx << " is not live in " << LI << '\n';
972 report("Virtual register has no Live interval", MO, MONum);
978 void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
981 // This function gets called after visiting all instructions in a bundle. The
982 // argument points to the bundle header.
983 // Normal stand-alone instructions are also considered 'bundles', and this
984 // function is called for all of them.
985 void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
986 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
987 set_union(MInfo.regsKilled, regsKilled);
988 set_subtract(regsLive, regsKilled); regsKilled.clear();
989 // Kill any masked registers.
990 while (!regMasks.empty()) {
991 const uint32_t *Mask = regMasks.pop_back_val();
992 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
993 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
994 MachineOperand::clobbersPhysReg(Mask, *I))
995 regsDead.push_back(*I);
997 set_subtract(regsLive, regsDead); regsDead.clear();
998 set_union(regsLive, regsDefined); regsDefined.clear();
1002 MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
1003 MBBInfoMap[MBB].regsLiveOut = regsLive;
1007 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1008 if (!(stop > lastIndex)) {
1009 report("Block ends before last instruction index", MBB);
1010 *OS << "Block ends at " << stop
1011 << " last instruction was at " << lastIndex << '\n';
1017 // Calculate the largest possible vregsPassed sets. These are the registers that
1018 // can pass through an MBB live, but may not be live every time. It is assumed
1019 // that all vregsPassed sets are empty before the call.
1020 void MachineVerifier::calcRegsPassed() {
1021 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1022 // have any vregsPassed.
1023 SmallPtrSet<const MachineBasicBlock*, 8> todo;
1024 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1025 MFI != MFE; ++MFI) {
1026 const MachineBasicBlock &MBB(*MFI);
1027 BBInfo &MInfo = MBBInfoMap[&MBB];
1028 if (!MInfo.reachable)
1030 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1031 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1032 BBInfo &SInfo = MBBInfoMap[*SuI];
1033 if (SInfo.addPassed(MInfo.regsLiveOut))
1038 // Iteratively push vregsPassed to successors. This will converge to the same
1039 // final state regardless of DenseSet iteration order.
1040 while (!todo.empty()) {
1041 const MachineBasicBlock *MBB = *todo.begin();
1043 BBInfo &MInfo = MBBInfoMap[MBB];
1044 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1045 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1048 BBInfo &SInfo = MBBInfoMap[*SuI];
1049 if (SInfo.addPassed(MInfo.vregsPassed))
1055 // Calculate the set of virtual registers that must be passed through each basic
1056 // block in order to satisfy the requirements of successor blocks. This is very
1057 // similar to calcRegsPassed, only backwards.
1058 void MachineVerifier::calcRegsRequired() {
1059 // First push live-in regs to predecessors' vregsRequired.
1060 SmallPtrSet<const MachineBasicBlock*, 8> todo;
1061 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1062 MFI != MFE; ++MFI) {
1063 const MachineBasicBlock &MBB(*MFI);
1064 BBInfo &MInfo = MBBInfoMap[&MBB];
1065 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1066 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1067 BBInfo &PInfo = MBBInfoMap[*PrI];
1068 if (PInfo.addRequired(MInfo.vregsLiveIn))
1073 // Iteratively push vregsRequired to predecessors. This will converge to the
1074 // same final state regardless of DenseSet iteration order.
1075 while (!todo.empty()) {
1076 const MachineBasicBlock *MBB = *todo.begin();
1078 BBInfo &MInfo = MBBInfoMap[MBB];
1079 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1080 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1083 BBInfo &SInfo = MBBInfoMap[*PrI];
1084 if (SInfo.addRequired(MInfo.vregsRequired))
1090 // Check PHI instructions at the beginning of MBB. It is assumed that
1091 // calcRegsPassed has been run so BBInfo::isLiveOut is valid.
1092 void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
1093 SmallPtrSet<const MachineBasicBlock*, 8> seen;
1094 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
1095 BBI != BBE && BBI->isPHI(); ++BBI) {
1098 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
1099 unsigned Reg = BBI->getOperand(i).getReg();
1100 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
1101 if (!Pre->isSuccessor(MBB))
1104 BBInfo &PrInfo = MBBInfoMap[Pre];
1105 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1106 report("PHI operand is not live-out from predecessor",
1107 &BBI->getOperand(i), i);
1110 // Did we see all predecessors?
1111 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1112 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1113 if (!seen.count(*PrI)) {
1114 report("Missing PHI operand", BBI);
1115 *OS << "BB#" << (*PrI)->getNumber()
1116 << " is a predecessor according to the CFG.\n";
1122 void MachineVerifier::visitMachineFunctionAfter() {
1125 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1126 MFI != MFE; ++MFI) {
1127 BBInfo &MInfo = MBBInfoMap[MFI];
1129 // Skip unreachable MBBs.
1130 if (!MInfo.reachable)
1136 // Now check liveness info if available
1139 // Check for killed virtual registers that should be live out.
1140 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1141 MFI != MFE; ++MFI) {
1142 BBInfo &MInfo = MBBInfoMap[MFI];
1143 for (RegSet::iterator
1144 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1146 if (MInfo.regsKilled.count(*I)) {
1147 report("Virtual register killed in block, but needed live out.", MFI);
1148 *OS << "Virtual register " << PrintReg(*I)
1149 << " is used after the block.\n";
1154 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1155 for (RegSet::iterator
1156 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1158 report("Virtual register def doesn't dominate all uses.",
1159 MRI->getVRegDef(*I));
1163 verifyLiveVariables();
1165 verifyLiveIntervals();
1168 void MachineVerifier::verifyLiveVariables() {
1169 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
1170 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1171 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
1172 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1173 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1174 MFI != MFE; ++MFI) {
1175 BBInfo &MInfo = MBBInfoMap[MFI];
1177 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1178 if (MInfo.vregsRequired.count(Reg)) {
1179 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1180 report("LiveVariables: Block missing from AliveBlocks", MFI);
1181 *OS << "Virtual register " << PrintReg(Reg)
1182 << " must be live through the block.\n";
1185 if (VI.AliveBlocks.test(MFI->getNumber())) {
1186 report("LiveVariables: Block should not be in AliveBlocks", MFI);
1187 *OS << "Virtual register " << PrintReg(Reg)
1188 << " is not needed live through the block.\n";
1195 void MachineVerifier::verifyLiveIntervals() {
1196 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
1197 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1198 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
1200 // Spilling and splitting may leave unused registers around. Skip them.
1201 if (MRI->reg_nodbg_empty(Reg))
1204 if (!LiveInts->hasInterval(Reg)) {
1205 report("Missing live interval for virtual register", MF);
1206 *OS << PrintReg(Reg, TRI) << " still has defs or uses\n";
1210 const LiveInterval &LI = LiveInts->getInterval(Reg);
1211 assert(Reg == LI.reg && "Invalid reg to interval mapping");
1212 verifyLiveInterval(LI);
1215 // Verify all the cached regunit intervals.
1216 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
1217 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(i))
1218 verifyLiveInterval(*LI);
1221 void MachineVerifier::verifyLiveIntervalValue(const LiveInterval &LI,
1223 if (VNI->isUnused())
1226 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
1229 report("Valno not live at def and not marked unused", MF, LI);
1230 *OS << "Valno #" << VNI->id << '\n';
1234 if (DefVNI != VNI) {
1235 report("Live range at def has different valno", MF, LI);
1236 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1237 << " where valno #" << DefVNI->id << " is live\n";
1241 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1243 report("Invalid definition index", MF, LI);
1244 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1245 << " in " << LI << '\n';
1249 if (VNI->isPHIDef()) {
1250 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1251 report("PHIDef value is not defined at MBB start", MBB, LI);
1252 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1253 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
1259 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1261 report("No instruction at def index", MBB, LI);
1262 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
1266 bool hasDef = false;
1267 bool isEarlyClobber = false;
1268 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1269 if (!MOI->isReg() || !MOI->isDef())
1271 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1272 if (MOI->getReg() != LI.reg)
1275 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1276 !TRI->hasRegUnit(MOI->getReg(), LI.reg))
1280 if (MOI->isEarlyClobber())
1281 isEarlyClobber = true;
1285 report("Defining instruction does not modify register", MI);
1286 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1289 // Early clobber defs begin at USE slots, but other defs must begin at
1291 if (isEarlyClobber) {
1292 if (!VNI->def.isEarlyClobber()) {
1293 report("Early clobber def must be at an early-clobber slot", MBB, LI);
1294 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
1296 } else if (!VNI->def.isRegister()) {
1297 report("Non-PHI, non-early clobber def must be at a register slot",
1299 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
1304 MachineVerifier::verifyLiveIntervalSegment(const LiveInterval &LI,
1305 LiveInterval::const_iterator I) {
1306 const VNInfo *VNI = I->valno;
1307 assert(VNI && "Live range has no valno");
1309 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
1310 report("Foreign valno in live range", MF, LI);
1311 *OS << *I << " has a bad valno\n";
1314 if (VNI->isUnused()) {
1315 report("Live range valno is marked unused", MF, LI);
1319 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1321 report("Bad start of live segment, no basic block", MF, LI);
1325 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1326 if (I->start != MBBStartIdx && I->start != VNI->def) {
1327 report("Live segment must begin at MBB entry or valno def", MBB, LI);
1331 const MachineBasicBlock *EndMBB =
1332 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1334 report("Bad end of live segment, no basic block", MF, LI);
1339 // No more checks for live-out segments.
1340 if (I->end == LiveInts->getMBBEndIdx(EndMBB))
1343 // RegUnit intervals are allowed dead phis.
1344 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) && VNI->isPHIDef() &&
1345 I->start == VNI->def && I->end == VNI->def.getDeadSlot())
1348 // The live segment is ending inside EndMBB
1349 const MachineInstr *MI =
1350 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1352 report("Live segment doesn't end at a valid instruction", EndMBB, LI);
1357 // The block slot must refer to a basic block boundary.
1358 if (I->end.isBlock()) {
1359 report("Live segment ends at B slot of an instruction", EndMBB, LI);
1363 if (I->end.isDead()) {
1364 // Segment ends on the dead slot.
1365 // That means there must be a dead def.
1366 if (!SlotIndex::isSameInstr(I->start, I->end)) {
1367 report("Live segment ending at dead slot spans instructions", EndMBB, LI);
1372 // A live segment can only end at an early-clobber slot if it is being
1373 // redefined by an early-clobber def.
1374 if (I->end.isEarlyClobber()) {
1375 if (I+1 == LI.end() || (I+1)->start != I->end) {
1376 report("Live segment ending at early clobber slot must be "
1377 "redefined by an EC def in the same instruction", EndMBB, LI);
1382 // The following checks only apply to virtual registers. Physreg liveness
1383 // is too weird to check.
1384 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1385 // A live range can end with either a redefinition, a kill flag on a
1386 // use, or a dead flag on a def.
1387 bool hasRead = false;
1388 bool hasDeadDef = false;
1389 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1390 if (!MOI->isReg() || MOI->getReg() != LI.reg)
1392 if (MOI->readsReg())
1394 if (MOI->isDef() && MOI->isDead())
1398 if (I->end.isDead()) {
1400 report("Instruction doesn't have a dead def operand", MI);
1402 *OS << " in " << LI << '\n';
1406 report("Instruction ending live range doesn't read the register", MI);
1407 *OS << *I << " in " << LI << '\n';
1412 // Now check all the basic blocks in this live segment.
1413 MachineFunction::const_iterator MFI = MBB;
1414 // Is this live range the beginning of a non-PHIDef VN?
1415 if (I->start == VNI->def && !VNI->isPHIDef()) {
1416 // Not live-in to any blocks.
1423 assert(LiveInts->isLiveInToMBB(LI, MFI));
1424 // We don't know how to track physregs into a landing pad.
1425 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) &&
1426 MFI->isLandingPad()) {
1427 if (&*MFI == EndMBB)
1433 // Is VNI a PHI-def in the current block?
1434 bool IsPHI = VNI->isPHIDef() &&
1435 VNI->def == LiveInts->getMBBStartIdx(MFI);
1437 // Check that VNI is live-out of all predecessors.
1438 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1439 PE = MFI->pred_end(); PI != PE; ++PI) {
1440 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1441 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
1443 // All predecessors must have a live-out value.
1445 report("Register not marked live out of predecessor", *PI, LI);
1446 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1447 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
1452 // Only PHI-defs can take different predecessor values.
1453 if (!IsPHI && PVNI != VNI) {
1454 report("Different value live out of predecessor", *PI, LI);
1455 *OS << "Valno #" << PVNI->id << " live out of BB#"
1456 << (*PI)->getNumber() << '@' << PEnd
1457 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1458 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n';
1461 if (&*MFI == EndMBB)
1467 void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1468 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1470 verifyLiveIntervalValue(LI, *I);
1472 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I)
1473 verifyLiveIntervalSegment(LI, I);
1475 // Check the LI only has one connected component.
1476 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1477 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1478 unsigned NumComp = ConEQ.Classify(&LI);
1480 report("Multiple connected components in live interval", MF, LI);
1481 for (unsigned comp = 0; comp != NumComp; ++comp) {
1482 *OS << comp << ": valnos";
1483 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1484 E = LI.vni_end(); I!=E; ++I)
1485 if (comp == ConEQ.getEqClass(*I))
1486 *OS << ' ' << (*I)->id;