a47709edb10417119b3c5f79f97e0f18ea7d2d8c
[oota-llvm.git] / lib / CodeGen / MachineVerifier.cpp
1 //===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Pass to verify generated machine code. The following is checked:
11 //
12 // Operand counts: All explicit operands must be present.
13 //
14 // Register classes: All physical and virtual register operands must be
15 // compatible with the register class required by the instruction descriptor.
16 //
17 // Register live intervals: Registers must be defined only once, and must be
18 // defined before use.
19 //
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 //===----------------------------------------------------------------------===//
25
26 #include "llvm/Function.h"
27 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
28 #include "llvm/CodeGen/LiveVariables.h"
29 #include "llvm/CodeGen/LiveStackAnalysis.h"
30 #include "llvm/CodeGen/MachineFunctionPass.h"
31 #include "llvm/CodeGen/MachineFrameInfo.h"
32 #include "llvm/CodeGen/MachineMemOperand.h"
33 #include "llvm/CodeGen/MachineRegisterInfo.h"
34 #include "llvm/CodeGen/Passes.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Target/TargetRegisterInfo.h"
37 #include "llvm/Target/TargetInstrInfo.h"
38 #include "llvm/ADT/DenseSet.h"
39 #include "llvm/ADT/SetOperations.h"
40 #include "llvm/ADT/SmallVector.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/raw_ostream.h"
44 using namespace llvm;
45
46 namespace {
47   struct MachineVerifier {
48
49     MachineVerifier(Pass *pass, const char *b) :
50       PASS(pass),
51       Banner(b),
52       OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
53       {}
54
55     bool runOnMachineFunction(MachineFunction &MF);
56
57     Pass *const PASS;
58     const char *Banner;
59     const char *const OutFileName;
60     raw_ostream *OS;
61     const MachineFunction *MF;
62     const TargetMachine *TM;
63     const TargetRegisterInfo *TRI;
64     const MachineRegisterInfo *MRI;
65
66     unsigned foundErrors;
67
68     typedef SmallVector<unsigned, 16> RegVector;
69     typedef DenseSet<unsigned> RegSet;
70     typedef DenseMap<unsigned, const MachineInstr*> RegMap;
71
72     BitVector regsReserved;
73     RegSet regsLive;
74     RegVector regsDefined, regsDead, regsKilled;
75     RegSet regsLiveInButUnused;
76
77     SlotIndex lastIndex;
78
79     // Add Reg and any sub-registers to RV
80     void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
81       RV.push_back(Reg);
82       if (TargetRegisterInfo::isPhysicalRegister(Reg))
83         for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
84           RV.push_back(*R);
85     }
86
87     struct BBInfo {
88       // Is this MBB reachable from the MF entry point?
89       bool reachable;
90
91       // Vregs that must be live in because they are used without being
92       // defined. Map value is the user.
93       RegMap vregsLiveIn;
94
95       // Regs killed in MBB. They may be defined again, and will then be in both
96       // regsKilled and regsLiveOut.
97       RegSet regsKilled;
98
99       // Regs defined in MBB and live out. Note that vregs passing through may
100       // be live out without being mentioned here.
101       RegSet regsLiveOut;
102
103       // Vregs that pass through MBB untouched. This set is disjoint from
104       // regsKilled and regsLiveOut.
105       RegSet vregsPassed;
106
107       // Vregs that must pass through MBB because they are needed by a successor
108       // block. This set is disjoint from regsLiveOut.
109       RegSet vregsRequired;
110
111       BBInfo() : reachable(false) {}
112
113       // Add register to vregsPassed if it belongs there. Return true if
114       // anything changed.
115       bool addPassed(unsigned Reg) {
116         if (!TargetRegisterInfo::isVirtualRegister(Reg))
117           return false;
118         if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
119           return false;
120         return vregsPassed.insert(Reg).second;
121       }
122
123       // Same for a full set.
124       bool addPassed(const RegSet &RS) {
125         bool changed = false;
126         for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
127           if (addPassed(*I))
128             changed = true;
129         return changed;
130       }
131
132       // Add register to vregsRequired if it belongs there. Return true if
133       // anything changed.
134       bool addRequired(unsigned Reg) {
135         if (!TargetRegisterInfo::isVirtualRegister(Reg))
136           return false;
137         if (regsLiveOut.count(Reg))
138           return false;
139         return vregsRequired.insert(Reg).second;
140       }
141
142       // Same for a full set.
143       bool addRequired(const RegSet &RS) {
144         bool changed = false;
145         for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
146           if (addRequired(*I))
147             changed = true;
148         return changed;
149       }
150
151       // Same for a full map.
152       bool addRequired(const RegMap &RM) {
153         bool changed = false;
154         for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
155           if (addRequired(I->first))
156             changed = true;
157         return changed;
158       }
159
160       // Live-out registers are either in regsLiveOut or vregsPassed.
161       bool isLiveOut(unsigned Reg) const {
162         return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
163       }
164     };
165
166     // Extra register info per MBB.
167     DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
168
169     bool isReserved(unsigned Reg) {
170       return Reg < regsReserved.size() && regsReserved.test(Reg);
171     }
172
173     // Analysis information if available
174     LiveVariables *LiveVars;
175     LiveIntervals *LiveInts;
176     LiveStacks *LiveStks;
177     SlotIndexes *Indexes;
178
179     void visitMachineFunctionBefore();
180     void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
181     void visitMachineInstrBefore(const MachineInstr *MI);
182     void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
183     void visitMachineInstrAfter(const MachineInstr *MI);
184     void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
185     void visitMachineFunctionAfter();
186
187     void report(const char *msg, const MachineFunction *MF);
188     void report(const char *msg, const MachineBasicBlock *MBB);
189     void report(const char *msg, const MachineInstr *MI);
190     void report(const char *msg, const MachineOperand *MO, unsigned MONum);
191
192     void markReachable(const MachineBasicBlock *MBB);
193     void calcRegsPassed();
194     void checkPHIOps(const MachineBasicBlock *MBB);
195
196     void calcRegsRequired();
197     void verifyLiveVariables();
198     void verifyLiveIntervals();
199   };
200
201   struct MachineVerifierPass : public MachineFunctionPass {
202     static char ID; // Pass ID, replacement for typeid
203     const char *const Banner;
204
205     MachineVerifierPass(const char *b = 0)
206       : MachineFunctionPass(ID), Banner(b) {
207         initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
208       }
209
210     void getAnalysisUsage(AnalysisUsage &AU) const {
211       AU.setPreservesAll();
212       MachineFunctionPass::getAnalysisUsage(AU);
213     }
214
215     bool runOnMachineFunction(MachineFunction &MF) {
216       MF.verify(this, Banner);
217       return false;
218     }
219   };
220
221 }
222
223 char MachineVerifierPass::ID = 0;
224 INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
225                 "Verify generated machine code", false, false)
226
227 FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
228   return new MachineVerifierPass(Banner);
229 }
230
231 void MachineFunction::verify(Pass *p, const char *Banner) const {
232   MachineVerifier(p, Banner)
233     .runOnMachineFunction(const_cast<MachineFunction&>(*this));
234 }
235
236 bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
237   raw_ostream *OutFile = 0;
238   if (OutFileName) {
239     std::string ErrorInfo;
240     OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
241                                  raw_fd_ostream::F_Append);
242     if (!ErrorInfo.empty()) {
243       errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
244       exit(1);
245     }
246
247     OS = OutFile;
248   } else {
249     OS = &errs();
250   }
251
252   foundErrors = 0;
253
254   this->MF = &MF;
255   TM = &MF.getTarget();
256   TRI = TM->getRegisterInfo();
257   MRI = &MF.getRegInfo();
258
259   LiveVars = NULL;
260   LiveInts = NULL;
261   LiveStks = NULL;
262   Indexes = NULL;
263   if (PASS) {
264     LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
265     // We don't want to verify LiveVariables if LiveIntervals is available.
266     if (!LiveInts)
267       LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
268     LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
269     Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
270   }
271
272   visitMachineFunctionBefore();
273   for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
274        MFI!=MFE; ++MFI) {
275     visitMachineBasicBlockBefore(MFI);
276     for (MachineBasicBlock::const_iterator MBBI = MFI->begin(),
277            MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
278       if (MBBI->getParent() != MFI) {
279         report("Bad instruction parent pointer", MFI);
280         *OS << "Instruction: " << *MBBI;
281         continue;
282       }
283       visitMachineInstrBefore(MBBI);
284       for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
285         visitMachineOperand(&MBBI->getOperand(I), I);
286       visitMachineInstrAfter(MBBI);
287     }
288     visitMachineBasicBlockAfter(MFI);
289   }
290   visitMachineFunctionAfter();
291
292   if (OutFile)
293     delete OutFile;
294   else if (foundErrors)
295     report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
296
297   // Clean up.
298   regsLive.clear();
299   regsDefined.clear();
300   regsDead.clear();
301   regsKilled.clear();
302   regsLiveInButUnused.clear();
303   MBBInfoMap.clear();
304
305   return false;                 // no changes
306 }
307
308 void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
309   assert(MF);
310   *OS << '\n';
311   if (!foundErrors++) {
312     if (Banner)
313       *OS << "# " << Banner << '\n';
314     MF->print(*OS, Indexes);
315   }
316   *OS << "*** Bad machine code: " << msg << " ***\n"
317       << "- function:    " << MF->getFunction()->getNameStr() << "\n";
318 }
319
320 void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
321   assert(MBB);
322   report(msg, MBB->getParent());
323   *OS << "- basic block: " << MBB->getName()
324       << " " << (void*)MBB
325       << " (BB#" << MBB->getNumber() << ")";
326   if (Indexes)
327     *OS << " [" << Indexes->getMBBStartIdx(MBB)
328         << ';' <<  Indexes->getMBBEndIdx(MBB) << ')';
329   *OS << '\n';
330 }
331
332 void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
333   assert(MI);
334   report(msg, MI->getParent());
335   *OS << "- instruction: ";
336   if (Indexes && Indexes->hasIndex(MI))
337     *OS << Indexes->getInstructionIndex(MI) << '\t';
338   MI->print(*OS, TM);
339 }
340
341 void MachineVerifier::report(const char *msg,
342                              const MachineOperand *MO, unsigned MONum) {
343   assert(MO);
344   report(msg, MO->getParent());
345   *OS << "- operand " << MONum << ":   ";
346   MO->print(*OS, TM);
347   *OS << "\n";
348 }
349
350 void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
351   BBInfo &MInfo = MBBInfoMap[MBB];
352   if (!MInfo.reachable) {
353     MInfo.reachable = true;
354     for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
355            SuE = MBB->succ_end(); SuI != SuE; ++SuI)
356       markReachable(*SuI);
357   }
358 }
359
360 void MachineVerifier::visitMachineFunctionBefore() {
361   lastIndex = SlotIndex();
362   regsReserved = TRI->getReservedRegs(*MF);
363
364   // A sub-register of a reserved register is also reserved
365   for (int Reg = regsReserved.find_first(); Reg>=0;
366        Reg = regsReserved.find_next(Reg)) {
367     for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
368       // FIXME: This should probably be:
369       // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
370       regsReserved.set(*Sub);
371     }
372   }
373   markReachable(&MF->front());
374 }
375
376 // Does iterator point to a and b as the first two elements?
377 static bool matchPair(MachineBasicBlock::const_succ_iterator i,
378                       const MachineBasicBlock *a, const MachineBasicBlock *b) {
379   if (*i == a)
380     return *++i == b;
381   if (*i == b)
382     return *++i == a;
383   return false;
384 }
385
386 void
387 MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
388   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
389
390   // Count the number of landing pad successors.
391   SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
392   for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
393        E = MBB->succ_end(); I != E; ++I) {
394     if ((*I)->isLandingPad())
395       LandingPadSuccs.insert(*I);
396   }
397   if (LandingPadSuccs.size() > 1)
398     report("MBB has more than one landing pad successor", MBB);
399
400   // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
401   MachineBasicBlock *TBB = 0, *FBB = 0;
402   SmallVector<MachineOperand, 4> Cond;
403   if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
404                           TBB, FBB, Cond)) {
405     // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
406     // check whether its answers match up with reality.
407     if (!TBB && !FBB) {
408       // Block falls through to its successor.
409       MachineFunction::const_iterator MBBI = MBB;
410       ++MBBI;
411       if (MBBI == MF->end()) {
412         // It's possible that the block legitimately ends with a noreturn
413         // call or an unreachable, in which case it won't actually fall
414         // out the bottom of the function.
415       } else if (MBB->succ_size() == LandingPadSuccs.size()) {
416         // It's possible that the block legitimately ends with a noreturn
417         // call or an unreachable, in which case it won't actuall fall
418         // out of the block.
419       } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
420         report("MBB exits via unconditional fall-through but doesn't have "
421                "exactly one CFG successor!", MBB);
422       } else if (!MBB->isSuccessor(MBBI)) {
423         report("MBB exits via unconditional fall-through but its successor "
424                "differs from its CFG successor!", MBB);
425       }
426       if (!MBB->empty() && MBB->back().getDesc().isBarrier() &&
427           !TII->isPredicated(&MBB->back())) {
428         report("MBB exits via unconditional fall-through but ends with a "
429                "barrier instruction!", MBB);
430       }
431       if (!Cond.empty()) {
432         report("MBB exits via unconditional fall-through but has a condition!",
433                MBB);
434       }
435     } else if (TBB && !FBB && Cond.empty()) {
436       // Block unconditionally branches somewhere.
437       if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
438         report("MBB exits via unconditional branch but doesn't have "
439                "exactly one CFG successor!", MBB);
440       } else if (!MBB->isSuccessor(TBB)) {
441         report("MBB exits via unconditional branch but the CFG "
442                "successor doesn't match the actual successor!", MBB);
443       }
444       if (MBB->empty()) {
445         report("MBB exits via unconditional branch but doesn't contain "
446                "any instructions!", MBB);
447       } else if (!MBB->back().getDesc().isBarrier()) {
448         report("MBB exits via unconditional branch but doesn't end with a "
449                "barrier instruction!", MBB);
450       } else if (!MBB->back().getDesc().isTerminator()) {
451         report("MBB exits via unconditional branch but the branch isn't a "
452                "terminator instruction!", MBB);
453       }
454     } else if (TBB && !FBB && !Cond.empty()) {
455       // Block conditionally branches somewhere, otherwise falls through.
456       MachineFunction::const_iterator MBBI = MBB;
457       ++MBBI;
458       if (MBBI == MF->end()) {
459         report("MBB conditionally falls through out of function!", MBB);
460       } if (MBB->succ_size() != 2) {
461         report("MBB exits via conditional branch/fall-through but doesn't have "
462                "exactly two CFG successors!", MBB);
463       } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
464         report("MBB exits via conditional branch/fall-through but the CFG "
465                "successors don't match the actual successors!", MBB);
466       }
467       if (MBB->empty()) {
468         report("MBB exits via conditional branch/fall-through but doesn't "
469                "contain any instructions!", MBB);
470       } else if (MBB->back().getDesc().isBarrier()) {
471         report("MBB exits via conditional branch/fall-through but ends with a "
472                "barrier instruction!", MBB);
473       } else if (!MBB->back().getDesc().isTerminator()) {
474         report("MBB exits via conditional branch/fall-through but the branch "
475                "isn't a terminator instruction!", MBB);
476       }
477     } else if (TBB && FBB) {
478       // Block conditionally branches somewhere, otherwise branches
479       // somewhere else.
480       if (MBB->succ_size() != 2) {
481         report("MBB exits via conditional branch/branch but doesn't have "
482                "exactly two CFG successors!", MBB);
483       } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
484         report("MBB exits via conditional branch/branch but the CFG "
485                "successors don't match the actual successors!", MBB);
486       }
487       if (MBB->empty()) {
488         report("MBB exits via conditional branch/branch but doesn't "
489                "contain any instructions!", MBB);
490       } else if (!MBB->back().getDesc().isBarrier()) {
491         report("MBB exits via conditional branch/branch but doesn't end with a "
492                "barrier instruction!", MBB);
493       } else if (!MBB->back().getDesc().isTerminator()) {
494         report("MBB exits via conditional branch/branch but the branch "
495                "isn't a terminator instruction!", MBB);
496       }
497       if (Cond.empty()) {
498         report("MBB exits via conditinal branch/branch but there's no "
499                "condition!", MBB);
500       }
501     } else {
502       report("AnalyzeBranch returned invalid data!", MBB);
503     }
504   }
505
506   regsLive.clear();
507   for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
508          E = MBB->livein_end(); I != E; ++I) {
509     if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
510       report("MBB live-in list contains non-physical register", MBB);
511       continue;
512     }
513     regsLive.insert(*I);
514     for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++)
515       regsLive.insert(*R);
516   }
517   regsLiveInButUnused = regsLive;
518
519   const MachineFrameInfo *MFI = MF->getFrameInfo();
520   assert(MFI && "Function has no frame info");
521   BitVector PR = MFI->getPristineRegs(MBB);
522   for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
523     regsLive.insert(I);
524     for (const unsigned *R = TRI->getSubRegisters(I); *R; R++)
525       regsLive.insert(*R);
526   }
527
528   regsKilled.clear();
529   regsDefined.clear();
530
531   if (Indexes)
532     lastIndex = Indexes->getMBBStartIdx(MBB);
533 }
534
535 void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
536   const TargetInstrDesc &TI = MI->getDesc();
537   if (MI->getNumOperands() < TI.getNumOperands()) {
538     report("Too few operands", MI);
539     *OS << TI.getNumOperands() << " operands expected, but "
540         << MI->getNumExplicitOperands() << " given.\n";
541   }
542
543   // Check the MachineMemOperands for basic consistency.
544   for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
545        E = MI->memoperands_end(); I != E; ++I) {
546     if ((*I)->isLoad() && !TI.mayLoad())
547       report("Missing mayLoad flag", MI);
548     if ((*I)->isStore() && !TI.mayStore())
549       report("Missing mayStore flag", MI);
550   }
551
552   // Debug values must not have a slot index.
553   // Other instructions must have one.
554   if (LiveInts) {
555     bool mapped = !LiveInts->isNotInMIMap(MI);
556     if (MI->isDebugValue()) {
557       if (mapped)
558         report("Debug instruction has a slot index", MI);
559     } else {
560       if (!mapped)
561         report("Missing slot index", MI);
562     }
563   }
564
565 }
566
567 void
568 MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
569   const MachineInstr *MI = MO->getParent();
570   const TargetInstrDesc &TI = MI->getDesc();
571   const TargetOperandInfo &TOI = TI.OpInfo[MONum];
572
573   // The first TI.NumDefs operands must be explicit register defines
574   if (MONum < TI.getNumDefs()) {
575     if (!MO->isReg())
576       report("Explicit definition must be a register", MO, MONum);
577     else if (!MO->isDef())
578       report("Explicit definition marked as use", MO, MONum);
579     else if (MO->isImplicit())
580       report("Explicit definition marked as implicit", MO, MONum);
581   } else if (MONum < TI.getNumOperands()) {
582     // Don't check if it's the last operand in a variadic instruction. See,
583     // e.g., LDM_RET in the arm back end.
584     if (MO->isReg() && !(TI.isVariadic() && MONum == TI.getNumOperands()-1)) {
585       if (MO->isDef() && !TOI.isOptionalDef())
586           report("Explicit operand marked as def", MO, MONum);
587       if (MO->isImplicit())
588         report("Explicit operand marked as implicit", MO, MONum);
589     }
590   } else {
591     // ARM adds %reg0 operands to indicate predicates. We'll allow that.
592     if (MO->isReg() && !MO->isImplicit() && !TI.isVariadic() && MO->getReg())
593       report("Extra explicit operand on non-variadic instruction", MO, MONum);
594   }
595
596   switch (MO->getType()) {
597   case MachineOperand::MO_Register: {
598     const unsigned Reg = MO->getReg();
599     if (!Reg)
600       return;
601
602     // Check Live Variables.
603     if (MI->isDebugValue()) {
604       // Liveness checks are not valid for debug values.
605     } else if (MO->isUse() && !MO->isUndef()) {
606       regsLiveInButUnused.erase(Reg);
607
608       bool isKill = false;
609       unsigned defIdx;
610       if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
611         // A two-addr use counts as a kill if use and def are the same.
612         unsigned DefReg = MI->getOperand(defIdx).getReg();
613         if (Reg == DefReg)
614           isKill = true;
615         else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
616           report("Two-address instruction operands must be identical",
617                  MO, MONum);
618         }
619       } else
620         isKill = MO->isKill();
621
622       if (isKill)
623         addRegWithSubRegs(regsKilled, Reg);
624
625       // Check that LiveVars knows this kill.
626       if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
627           MO->isKill()) {
628         LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
629         if (std::find(VI.Kills.begin(),
630                       VI.Kills.end(), MI) == VI.Kills.end())
631           report("Kill missing from LiveVariables", MO, MONum);
632       }
633
634       // Check LiveInts liveness and kill.
635       if (TargetRegisterInfo::isVirtualRegister(Reg) &&
636           LiveInts && !LiveInts->isNotInMIMap(MI)) {
637         SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getUseIndex();
638         if (LiveInts->hasInterval(Reg)) {
639           const LiveInterval &LI = LiveInts->getInterval(Reg);
640           if (!LI.liveAt(UseIdx)) {
641             report("No live range at use", MO, MONum);
642             *OS << UseIdx << " is not live in " << LI << '\n';
643           }
644           // Check for extra kill flags.
645           // Note that we allow missing kill flags for now.
646           if (MO->isKill() && !LI.killedAt(UseIdx.getDefIndex())) {
647             report("Live range continues after kill flag", MO, MONum);
648             *OS << "Live range: " << LI << '\n';
649           }
650         } else {
651           report("Virtual register has no Live interval", MO, MONum);
652         }
653       }
654
655       // Use of a dead register.
656       if (!regsLive.count(Reg)) {
657         if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
658           // Reserved registers may be used even when 'dead'.
659           if (!isReserved(Reg))
660             report("Using an undefined physical register", MO, MONum);
661         } else {
662           BBInfo &MInfo = MBBInfoMap[MI->getParent()];
663           // We don't know which virtual registers are live in, so only complain
664           // if vreg was killed in this MBB. Otherwise keep track of vregs that
665           // must be live in. PHI instructions are handled separately.
666           if (MInfo.regsKilled.count(Reg))
667             report("Using a killed virtual register", MO, MONum);
668           else if (!MI->isPHI())
669             MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
670         }
671       }
672     } else if (MO->isDef()) {
673       // Register defined.
674       // TODO: verify that earlyclobber ops are not used.
675       if (MO->isDead())
676         addRegWithSubRegs(regsDead, Reg);
677       else
678         addRegWithSubRegs(regsDefined, Reg);
679
680       // Check LiveInts for a live range, but only for virtual registers.
681       if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
682           !LiveInts->isNotInMIMap(MI)) {
683         SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getDefIndex();
684         if (LiveInts->hasInterval(Reg)) {
685           const LiveInterval &LI = LiveInts->getInterval(Reg);
686           if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
687             assert(VNI && "NULL valno is not allowed");
688             if (VNI->def != DefIdx && !MO->isEarlyClobber()) {
689               report("Inconsistent valno->def", MO, MONum);
690               *OS << "Valno " << VNI->id << " is not defined at "
691                   << DefIdx << " in " << LI << '\n';
692             }
693           } else {
694             report("No live range at def", MO, MONum);
695             *OS << DefIdx << " is not live in " << LI << '\n';
696           }
697         } else {
698           report("Virtual register has no Live interval", MO, MONum);
699         }
700       }
701     }
702
703     // Check register classes.
704     if (MONum < TI.getNumOperands() && !MO->isImplicit()) {
705       unsigned SubIdx = MO->getSubReg();
706
707       if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
708         unsigned sr = Reg;
709         if (SubIdx) {
710           unsigned s = TRI->getSubReg(Reg, SubIdx);
711           if (!s) {
712             report("Invalid subregister index for physical register",
713                    MO, MONum);
714             return;
715           }
716           sr = s;
717         }
718         if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
719           if (!DRC->contains(sr)) {
720             report("Illegal physical register for instruction", MO, MONum);
721             *OS << TRI->getName(sr) << " is not a "
722                 << DRC->getName() << " register.\n";
723           }
724         }
725       } else {
726         // Virtual register.
727         const TargetRegisterClass *RC = MRI->getRegClass(Reg);
728         if (SubIdx) {
729           const TargetRegisterClass *SRC = RC->getSubRegisterRegClass(SubIdx);
730           if (!SRC) {
731             report("Invalid subregister index for virtual register", MO, MONum);
732             *OS << "Register class " << RC->getName()
733                 << " does not support subreg index " << SubIdx << "\n";
734             return;
735           }
736           RC = SRC;
737         }
738         if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
739           if (RC != DRC && !RC->hasSuperClass(DRC)) {
740             report("Illegal virtual register for instruction", MO, MONum);
741             *OS << "Expected a " << DRC->getName() << " register, but got a "
742                 << RC->getName() << " register\n";
743           }
744         }
745       }
746     }
747     break;
748   }
749
750   case MachineOperand::MO_MachineBasicBlock:
751     if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
752       report("PHI operand is not in the CFG", MO, MONum);
753     break;
754
755   case MachineOperand::MO_FrameIndex:
756     if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
757         LiveInts && !LiveInts->isNotInMIMap(MI)) {
758       LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
759       SlotIndex Idx = LiveInts->getInstructionIndex(MI);
760       if (TI.mayLoad() && !LI.liveAt(Idx.getUseIndex())) {
761         report("Instruction loads from dead spill slot", MO, MONum);
762         *OS << "Live stack: " << LI << '\n';
763       }
764       if (TI.mayStore() && !LI.liveAt(Idx.getDefIndex())) {
765         report("Instruction stores to dead spill slot", MO, MONum);
766         *OS << "Live stack: " << LI << '\n';
767       }
768     }
769     break;
770
771   default:
772     break;
773   }
774 }
775
776 void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
777   BBInfo &MInfo = MBBInfoMap[MI->getParent()];
778   set_union(MInfo.regsKilled, regsKilled);
779   set_subtract(regsLive, regsKilled); regsKilled.clear();
780   set_subtract(regsLive, regsDead);   regsDead.clear();
781   set_union(regsLive, regsDefined);   regsDefined.clear();
782
783   if (Indexes && Indexes->hasIndex(MI)) {
784     SlotIndex idx = Indexes->getInstructionIndex(MI);
785     if (!(idx > lastIndex)) {
786       report("Instruction index out of order", MI);
787       *OS << "Last instruction was at " << lastIndex << '\n';
788     }
789     lastIndex = idx;
790   }
791 }
792
793 void
794 MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
795   MBBInfoMap[MBB].regsLiveOut = regsLive;
796   regsLive.clear();
797
798   if (Indexes) {
799     SlotIndex stop = Indexes->getMBBEndIdx(MBB);
800     if (!(stop > lastIndex)) {
801       report("Block ends before last instruction index", MBB);
802       *OS << "Block ends at " << stop
803           << " last instruction was at " << lastIndex << '\n';
804     }
805     lastIndex = stop;
806   }
807 }
808
809 // Calculate the largest possible vregsPassed sets. These are the registers that
810 // can pass through an MBB live, but may not be live every time. It is assumed
811 // that all vregsPassed sets are empty before the call.
812 void MachineVerifier::calcRegsPassed() {
813   // First push live-out regs to successors' vregsPassed. Remember the MBBs that
814   // have any vregsPassed.
815   DenseSet<const MachineBasicBlock*> todo;
816   for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
817        MFI != MFE; ++MFI) {
818     const MachineBasicBlock &MBB(*MFI);
819     BBInfo &MInfo = MBBInfoMap[&MBB];
820     if (!MInfo.reachable)
821       continue;
822     for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
823            SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
824       BBInfo &SInfo = MBBInfoMap[*SuI];
825       if (SInfo.addPassed(MInfo.regsLiveOut))
826         todo.insert(*SuI);
827     }
828   }
829
830   // Iteratively push vregsPassed to successors. This will converge to the same
831   // final state regardless of DenseSet iteration order.
832   while (!todo.empty()) {
833     const MachineBasicBlock *MBB = *todo.begin();
834     todo.erase(MBB);
835     BBInfo &MInfo = MBBInfoMap[MBB];
836     for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
837            SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
838       if (*SuI == MBB)
839         continue;
840       BBInfo &SInfo = MBBInfoMap[*SuI];
841       if (SInfo.addPassed(MInfo.vregsPassed))
842         todo.insert(*SuI);
843     }
844   }
845 }
846
847 // Calculate the set of virtual registers that must be passed through each basic
848 // block in order to satisfy the requirements of successor blocks. This is very
849 // similar to calcRegsPassed, only backwards.
850 void MachineVerifier::calcRegsRequired() {
851   // First push live-in regs to predecessors' vregsRequired.
852   DenseSet<const MachineBasicBlock*> todo;
853   for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
854        MFI != MFE; ++MFI) {
855     const MachineBasicBlock &MBB(*MFI);
856     BBInfo &MInfo = MBBInfoMap[&MBB];
857     for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
858            PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
859       BBInfo &PInfo = MBBInfoMap[*PrI];
860       if (PInfo.addRequired(MInfo.vregsLiveIn))
861         todo.insert(*PrI);
862     }
863   }
864
865   // Iteratively push vregsRequired to predecessors. This will converge to the
866   // same final state regardless of DenseSet iteration order.
867   while (!todo.empty()) {
868     const MachineBasicBlock *MBB = *todo.begin();
869     todo.erase(MBB);
870     BBInfo &MInfo = MBBInfoMap[MBB];
871     for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
872            PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
873       if (*PrI == MBB)
874         continue;
875       BBInfo &SInfo = MBBInfoMap[*PrI];
876       if (SInfo.addRequired(MInfo.vregsRequired))
877         todo.insert(*PrI);
878     }
879   }
880 }
881
882 // Check PHI instructions at the beginning of MBB. It is assumed that
883 // calcRegsPassed has been run so BBInfo::isLiveOut is valid.
884 void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
885   for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
886        BBI != BBE && BBI->isPHI(); ++BBI) {
887     DenseSet<const MachineBasicBlock*> seen;
888
889     for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
890       unsigned Reg = BBI->getOperand(i).getReg();
891       const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
892       if (!Pre->isSuccessor(MBB))
893         continue;
894       seen.insert(Pre);
895       BBInfo &PrInfo = MBBInfoMap[Pre];
896       if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
897         report("PHI operand is not live-out from predecessor",
898                &BBI->getOperand(i), i);
899     }
900
901     // Did we see all predecessors?
902     for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
903            PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
904       if (!seen.count(*PrI)) {
905         report("Missing PHI operand", BBI);
906         *OS << "BB#" << (*PrI)->getNumber()
907             << " is a predecessor according to the CFG.\n";
908       }
909     }
910   }
911 }
912
913 void MachineVerifier::visitMachineFunctionAfter() {
914   calcRegsPassed();
915
916   for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
917        MFI != MFE; ++MFI) {
918     BBInfo &MInfo = MBBInfoMap[MFI];
919
920     // Skip unreachable MBBs.
921     if (!MInfo.reachable)
922       continue;
923
924     checkPHIOps(MFI);
925   }
926
927   // Now check liveness info if available
928   if (LiveVars || LiveInts)
929     calcRegsRequired();
930   if (LiveVars)
931     verifyLiveVariables();
932   if (LiveInts)
933     verifyLiveIntervals();
934 }
935
936 void MachineVerifier::verifyLiveVariables() {
937   assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
938   for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
939     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
940     LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
941     for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
942          MFI != MFE; ++MFI) {
943       BBInfo &MInfo = MBBInfoMap[MFI];
944
945       // Our vregsRequired should be identical to LiveVariables' AliveBlocks
946       if (MInfo.vregsRequired.count(Reg)) {
947         if (!VI.AliveBlocks.test(MFI->getNumber())) {
948           report("LiveVariables: Block missing from AliveBlocks", MFI);
949           *OS << "Virtual register " << PrintReg(Reg)
950               << " must be live through the block.\n";
951         }
952       } else {
953         if (VI.AliveBlocks.test(MFI->getNumber())) {
954           report("LiveVariables: Block should not be in AliveBlocks", MFI);
955           *OS << "Virtual register " << PrintReg(Reg)
956               << " is not needed live through the block.\n";
957         }
958       }
959     }
960   }
961 }
962
963 void MachineVerifier::verifyLiveIntervals() {
964   assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
965   for (LiveIntervals::const_iterator LVI = LiveInts->begin(),
966        LVE = LiveInts->end(); LVI != LVE; ++LVI) {
967     const LiveInterval &LI = *LVI->second;
968
969     // Spilling and splitting may leave unused registers around. Skip them.
970     if (MRI->use_empty(LI.reg))
971       continue;
972
973     // Physical registers have much weirdness going on, mostly from coalescing.
974     // We should probably fix it, but for now just ignore them.
975     if (TargetRegisterInfo::isPhysicalRegister(LI.reg))
976       continue;
977
978     assert(LVI->first == LI.reg && "Invalid reg to interval mapping");
979
980     for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
981          I!=E; ++I) {
982       VNInfo *VNI = *I;
983       const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
984
985       if (!DefVNI) {
986         if (!VNI->isUnused()) {
987           report("Valno not live at def and not marked unused", MF);
988           *OS << "Valno #" << VNI->id << " in " << LI << '\n';
989         }
990         continue;
991       }
992
993       if (VNI->isUnused())
994         continue;
995
996       if (DefVNI != VNI) {
997         report("Live range at def has different valno", MF);
998         *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
999             << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
1000         continue;
1001       }
1002
1003       const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1004       if (!MBB) {
1005         report("Invalid definition index", MF);
1006         *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1007             << " in " << LI << '\n';
1008         continue;
1009       }
1010
1011       if (VNI->isPHIDef()) {
1012         if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1013           report("PHIDef value is not defined at MBB start", MF);
1014           *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1015               << ", not at the beginning of BB#" << MBB->getNumber()
1016               << " in " << LI << '\n';
1017         }
1018       } else {
1019         // Non-PHI def.
1020         const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1021         if (!MI) {
1022           report("No instruction at def index", MF);
1023           *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1024               << " in " << LI << '\n';
1025         } else if (!MI->modifiesRegister(LI.reg, TRI)) {
1026           report("Defining instruction does not modify register", MI);
1027           *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1028         }
1029
1030         bool isEarlyClobber = false;
1031         if (MI) {
1032           for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1033                MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1034             if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() &&
1035                 MOI->isEarlyClobber()) {
1036               isEarlyClobber = true;
1037               break;
1038             }
1039           }
1040         }
1041
1042         // Early clobber defs begin at USE slots, but other defs must begin at
1043         // DEF slots.
1044         if (isEarlyClobber) {
1045           if (!VNI->def.isUse()) {
1046             report("Early clobber def must be at a USE slot", MF);
1047             *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1048                 << " in " << LI << '\n';
1049           }
1050         } else if (!VNI->def.isDef()) {
1051           report("Non-PHI, non-early clobber def must be at a DEF slot", MF);
1052           *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1053               << " in " << LI << '\n';
1054         }
1055       }
1056     }
1057
1058     for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
1059       const VNInfo *VNI = I->valno;
1060       assert(VNI && "Live range has no valno");
1061
1062       if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
1063         report("Foreign valno in live range", MF);
1064         I->print(*OS);
1065         *OS << " has a valno not in " << LI << '\n';
1066       }
1067
1068       if (VNI->isUnused()) {
1069         report("Live range valno is marked unused", MF);
1070         I->print(*OS);
1071         *OS << " in " << LI << '\n';
1072       }
1073
1074       const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1075       if (!MBB) {
1076         report("Bad start of live segment, no basic block", MF);
1077         I->print(*OS);
1078         *OS << " in " << LI << '\n';
1079         continue;
1080       }
1081       SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1082       if (I->start != MBBStartIdx && I->start != VNI->def) {
1083         report("Live segment must begin at MBB entry or valno def", MBB);
1084         I->print(*OS);
1085         *OS << " in " << LI << '\n' << "Basic block starts at "
1086             << MBBStartIdx << '\n';
1087       }
1088
1089       const MachineBasicBlock *EndMBB =
1090                                 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1091       if (!EndMBB) {
1092         report("Bad end of live segment, no basic block", MF);
1093         I->print(*OS);
1094         *OS << " in " << LI << '\n';
1095         continue;
1096       }
1097       if (I->end != LiveInts->getMBBEndIdx(EndMBB)) {
1098         // The live segment is ending inside EndMBB
1099         const MachineInstr *MI =
1100                         LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1101         if (!MI) {
1102           report("Live segment doesn't end at a valid instruction", EndMBB);
1103         I->print(*OS);
1104         *OS << " in " << LI << '\n' << "Basic block starts at "
1105             << MBBStartIdx << '\n';
1106         } else if (TargetRegisterInfo::isVirtualRegister(LI.reg) &&
1107                    !MI->readsVirtualRegister(LI.reg)) {
1108           // A live range can end with either a redefinition, a kill flag on a
1109           // use, or a dead flag on a def.
1110           // FIXME: Should we check for each of these?
1111           bool hasDeadDef = false;
1112           for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1113                MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1114             if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() && MOI->isDead()) {
1115               hasDeadDef = true;
1116               break;
1117             }
1118           }
1119
1120           if (!hasDeadDef) {
1121             report("Instruction killing live segment neither defines nor reads "
1122                    "register", MI);
1123             I->print(*OS);
1124             *OS << " in " << LI << '\n';
1125           }
1126         }
1127       }
1128
1129       // Now check all the basic blocks in this live segment.
1130       MachineFunction::const_iterator MFI = MBB;
1131       // Is this live range the beginning of a non-PHIDef VN?
1132       if (I->start == VNI->def && !VNI->isPHIDef()) {
1133         // Not live-in to any blocks.
1134         if (MBB == EndMBB)
1135           continue;
1136         // Skip this block.
1137         ++MFI;
1138       }
1139       for (;;) {
1140         assert(LiveInts->isLiveInToMBB(LI, MFI));
1141         // We don't know how to track physregs into a landing pad.
1142         if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1143             MFI->isLandingPad()) {
1144           if (&*MFI == EndMBB)
1145             break;
1146           ++MFI;
1147           continue;
1148         }
1149         // Check that VNI is live-out of all predecessors.
1150         for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1151              PE = MFI->pred_end(); PI != PE; ++PI) {
1152           SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI).getPrevSlot();
1153           const VNInfo *PVNI = LI.getVNInfoAt(PEnd);
1154
1155           if (VNI->isPHIDef() && VNI->def == LiveInts->getMBBStartIdx(MFI)) {
1156             if (PVNI && !PVNI->hasPHIKill()) {
1157               report("Value live out of predecessor doesn't have PHIKill", MF);
1158               *OS << "Valno #" << PVNI->id << " live out of BB#"
1159                   << (*PI)->getNumber() << '@' << PEnd
1160                   << " doesn't have PHIKill, but Valno #" << VNI->id
1161                   << " is PHIDef and defined at the beginning of BB#"
1162                   << MFI->getNumber() << '@' << LiveInts->getMBBStartIdx(MFI)
1163                   << " in " << LI << '\n';
1164             }
1165             continue;
1166           }
1167
1168           if (!PVNI) {
1169             report("Register not marked live out of predecessor", *PI);
1170             *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1171                 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live at "
1172                 << PEnd << " in " << LI << '\n';
1173             continue;
1174           }
1175
1176           if (PVNI != VNI) {
1177             report("Different value live out of predecessor", *PI);
1178             *OS << "Valno #" << PVNI->id << " live out of BB#"
1179                 << (*PI)->getNumber() << '@' << PEnd
1180                 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1181                 << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n';
1182           }
1183         }
1184         if (&*MFI == EndMBB)
1185           break;
1186         ++MFI;
1187       }
1188     }
1189
1190     // Check the LI only has one connected component.
1191     if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1192       ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1193       unsigned NumComp = ConEQ.Classify(&LI);
1194       if (NumComp > 1) {
1195         report("Multiple connected components in live interval", MF);
1196         *OS << NumComp << " components in " << LI << '\n';
1197         for (unsigned comp = 0; comp != NumComp; ++comp) {
1198           *OS << comp << ": valnos";
1199           for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1200                E = LI.vni_end(); I!=E; ++I)
1201             if (comp == ConEQ.getEqClass(*I))
1202               *OS << ' ' << (*I)->id;
1203           *OS << '\n';
1204         }
1205       }
1206     }
1207   }
1208 }
1209