blob: 7351119f4728e53a141dd423bb671d7ee4ba3268 [file] [log] [blame]
Bill Wendling5567bb02010-08-19 18:52:17 +00001//===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00002//
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
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000026#include "llvm/Function.h"
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +000027#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000028#include "llvm/CodeGen/LiveVariables.h"
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +000029#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000030#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman2dbc4c82009-10-07 17:36:00 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000033#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"
Chris Lattnercf143a42009-08-23 03:13:20 +000038#include "llvm/ADT/DenseSet.h"
39#include "llvm/ADT/SetOperations.h"
40#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000041#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000042#include "llvm/Support/ErrorHandling.h"
43#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000044using namespace llvm;
45
46namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000047 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000048
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000049 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000050 PASS(pass),
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000051 Banner(b),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000052 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000053 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000054
55 bool runOnMachineFunction(MachineFunction &MF);
56
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000057 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000058 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000059 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000060 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000061 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;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000074 RegVector regsDefined, regsDead, regsKilled;
75 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000076
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000077 SlotIndex lastIndex;
78
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000079 // 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
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000087 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
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000095 // 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
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000107 // 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
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000111 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
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000132 // 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
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000160 // 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) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000170 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000171 }
172
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000173 // Analysis information if available
174 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000175 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000176 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000177 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000178
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000179 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);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000193 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000194 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000195
196 void calcRegsRequired();
197 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000198 void verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000199 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000200
201 struct MachineVerifierPass : public MachineFunctionPass {
202 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000203 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000204
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000205 MachineVerifierPass(const char *b = 0)
206 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000207 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
208 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000209
210 void getAnalysisUsage(AnalysisUsage &AU) const {
211 AU.setPreservesAll();
212 MachineFunctionPass::getAnalysisUsage(AU);
213 }
214
215 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000216 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000217 return false;
218 }
219 };
220
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000221}
222
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000223char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000224INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000225 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000226
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000227FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
228 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000229}
230
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000231void MachineFunction::verify(Pass *p, const char *Banner) const {
232 MachineVerifier(p, Banner)
233 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000234}
235
Chris Lattner17e9edc2009-08-23 02:51:22 +0000236bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
237 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000238 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000239 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 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000246
Chris Lattner17e9edc2009-08-23 02:51:22 +0000247 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000248 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000249 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000250 }
251
252 foundErrors = 0;
253
254 this->MF = &MF;
255 TM = &MF.getTarget();
256 TRI = TM->getRegisterInfo();
257 MRI = &MF.getRegInfo();
258
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000259 LiveVars = NULL;
260 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000261 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000262 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000263 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000264 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000265 // We don't want to verify LiveVariables if LiveIntervals is available.
266 if (!LiveInts)
267 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000268 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000269 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000270 }
271
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000272 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) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000278 if (MBBI->getParent() != MFI) {
279 report("Bad instruction parent pointer", MFI);
280 *OS << "Instruction: " << *MBBI;
281 continue;
282 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000283 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
Chris Lattner17e9edc2009-08-23 02:51:22 +0000292 if (OutFile)
293 delete OutFile;
294 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000295 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000296
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000297 // Clean up.
298 regsLive.clear();
299 regsDefined.clear();
300 regsDead.clear();
301 regsKilled.clear();
302 regsLiveInButUnused.clear();
303 MBBInfoMap.clear();
304
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000305 return false; // no changes
306}
307
Chris Lattner372fefe2009-08-23 01:03:30 +0000308void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000309 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000310 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000311 if (!foundErrors++) {
312 if (Banner)
313 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000314 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000315 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000316 *OS << "*** Bad machine code: " << msg << " ***\n"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000317 << "- function: " << MF->getFunction()->getNameStr() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000318}
319
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000320void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000321 assert(MBB);
322 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000323 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000324 << " " << (void*)MBB
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000325 << " (BB#" << MBB->getNumber() << ")";
326 if (Indexes)
327 *OS << " [" << Indexes->getMBBStartIdx(MBB)
328 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
329 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000330}
331
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000332void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000333 assert(MI);
334 report(msg, MI->getParent());
335 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000336 if (Indexes && Indexes->hasIndex(MI))
337 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000338 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000339}
340
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000341void MachineVerifier::report(const char *msg,
342 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000343 assert(MO);
344 report(msg, MO->getParent());
345 *OS << "- operand " << MONum << ": ";
346 MO->print(*OS, TM);
347 *OS << "\n";
348}
349
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000350void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000351 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
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000360void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000361 lastIndex = SlotIndex();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000362 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000363
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 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000373 markReachable(&MF->front());
374}
375
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000376// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000377static bool matchPair(MachineBasicBlock::const_succ_iterator i,
378 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000379 if (*i == a)
380 return *++i == b;
381 if (*i == b)
382 return *++i == a;
383 return false;
384}
385
386void
387MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Dan Gohman27920592009-08-27 02:43:49 +0000388 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
389
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000390 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000391 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000392 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000393 E = MBB->succ_end(); I != E; ++I) {
394 if ((*I)->isLandingPad())
395 LandingPadSuccs.insert(*I);
396 }
397 if (LandingPadSuccs.size() > 1)
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000398 report("MBB has more than one landing pad successor", MBB);
399
Dan Gohman27920592009-08-27 02:43:49 +0000400 // 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()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000412 // 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.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000415 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000416 // 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.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000419 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000420 report("MBB exits via unconditional fall-through but doesn't have "
421 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000422 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000423 report("MBB exits via unconditional fall-through but its successor "
424 "differs from its CFG successor!", MBB);
425 }
Evan Cheng86050dc2010-06-18 23:09:54 +0000426 if (!MBB->empty() && MBB->back().getDesc().isBarrier() &&
427 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000428 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.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000437 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000438 report("MBB exits via unconditional branch but doesn't have "
439 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000440 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000441 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);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000463 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000464 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);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000483 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000484 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
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000506 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000507 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000508 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 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000517 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000518
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
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000528 regsKilled.clear();
529 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000530
531 if (Indexes)
532 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000533}
534
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000535void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000536 const TargetInstrDesc &TI = MI->getDesc();
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000537 if (MI->getNumOperands() < TI.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000538 report("Too few operands", MI);
539 *OS << TI.getNumOperands() << " operands expected, but "
540 << MI->getNumExplicitOperands() << " given.\n";
541 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000542
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 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000551
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
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000565}
566
567void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000568MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000569 const MachineInstr *MI = MO->getParent();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000570 const TargetInstrDesc &TI = MI->getDesc();
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000571 const TargetOperandInfo &TOI = TI.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000572
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);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000581 } else if (MONum < TI.getNumOperands()) {
Eric Christopher113a06c2010-11-17 00:55:36 +0000582 // 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)) {
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000585 if (MO->isDef() && !TOI.isOptionalDef())
586 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000587 if (MO->isImplicit())
588 report("Explicit operand marked as implicit", MO, MONum);
589 }
590 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000591 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
592 if (MO->isReg() && !MO->isImplicit() && !TI.isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000593 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000594 }
595
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000596 switch (MO->getType()) {
597 case MachineOperand::MO_Register: {
598 const unsigned Reg = MO->getReg();
599 if (!Reg)
600 return;
601
602 // Check Live Variables.
Cameron Zwarich8ec88ba2010-12-20 00:08:10 +0000603 if (MI->isDebugValue()) {
604 // Liveness checks are not valid for debug values.
605 } else if (MO->isUndef()) {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000606 // An <undef> doesn't refer to any register, so just skip it.
607 } else if (MO->isUse()) {
608 regsLiveInButUnused.erase(Reg);
609
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000610 bool isKill = false;
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000611 unsigned defIdx;
612 if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
613 // A two-addr use counts as a kill if use and def are the same.
614 unsigned DefReg = MI->getOperand(defIdx).getReg();
615 if (Reg == DefReg) {
616 isKill = true;
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000617 // And in that case an explicit kill flag is not allowed.
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000618 if (MO->isKill())
Jakob Stoklund Olesenf7d3e692009-07-15 23:37:26 +0000619 report("Illegal kill flag on two-address instruction operand",
620 MO, MONum);
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000621 } else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
622 report("Two-address instruction operands must be identical",
623 MO, MONum);
624 }
625 } else
626 isKill = MO->isKill();
627
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000628 if (isKill)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000629 addRegWithSubRegs(regsKilled, Reg);
630
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000631 // Check that LiveVars knows this kill.
632 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
633 MO->isKill()) {
634 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
635 if (std::find(VI.Kills.begin(),
636 VI.Kills.end(), MI) == VI.Kills.end())
637 report("Kill missing from LiveVariables", MO, MONum);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000638 }
639
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000640 // Check LiveInts liveness and kill.
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000641 if (TargetRegisterInfo::isVirtualRegister(Reg) &&
642 LiveInts && !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000643 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getUseIndex();
644 if (LiveInts->hasInterval(Reg)) {
645 const LiveInterval &LI = LiveInts->getInterval(Reg);
646 if (!LI.liveAt(UseIdx)) {
647 report("No live range at use", MO, MONum);
648 *OS << UseIdx << " is not live in " << LI << '\n';
649 }
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000650 // Check for extra kill flags.
651 // Note that we allow missing kill flags for now.
652 if (MO->isKill() && !LI.killedAt(UseIdx.getDefIndex())) {
653 report("Live range continues after kill flag", MO, MONum);
654 *OS << "Live range: " << LI << '\n';
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000655 }
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000656 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000657 report("Virtual register has no Live interval", MO, MONum);
658 }
659 }
660
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000661 // Use of a dead register.
662 if (!regsLive.count(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000663 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
664 // Reserved registers may be used even when 'dead'.
665 if (!isReserved(Reg))
666 report("Using an undefined physical register", MO, MONum);
667 } else {
668 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
669 // We don't know which virtual registers are live in, so only complain
670 // if vreg was killed in this MBB. Otherwise keep track of vregs that
671 // must be live in. PHI instructions are handled separately.
672 if (MInfo.regsKilled.count(Reg))
673 report("Using a killed virtual register", MO, MONum);
Chris Lattner518bb532010-02-09 19:54:29 +0000674 else if (!MI->isPHI())
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000675 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
676 }
Duncan Sandse5567202009-05-16 03:28:54 +0000677 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000678 } else {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000679 assert(MO->isDef());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000680 // Register defined.
681 // TODO: verify that earlyclobber ops are not used.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000682 if (MO->isDead())
683 addRegWithSubRegs(regsDead, Reg);
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000684 else
685 addRegWithSubRegs(regsDefined, Reg);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000686
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000687 // Check LiveInts for a live range, but only for virtual registers.
688 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
689 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000690 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getDefIndex();
691 if (LiveInts->hasInterval(Reg)) {
692 const LiveInterval &LI = LiveInts->getInterval(Reg);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000693 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
694 assert(VNI && "NULL valno is not allowed");
Cameron Zwarich1b031dd2010-12-19 23:50:53 +0000695 if (VNI->def != DefIdx && !MO->isEarlyClobber()) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000696 report("Inconsistent valno->def", MO, MONum);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000697 *OS << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000698 << DefIdx << " in " << LI << '\n';
699 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000700 } else {
701 report("No live range at def", MO, MONum);
702 *OS << DefIdx << " is not live in " << LI << '\n';
703 }
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000704 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000705 report("Virtual register has no Live interval", MO, MONum);
706 }
707 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000708 }
709
710 // Check register classes.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000711 if (MONum < TI.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000712 unsigned SubIdx = MO->getSubReg();
713
714 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
715 unsigned sr = Reg;
716 if (SubIdx) {
717 unsigned s = TRI->getSubReg(Reg, SubIdx);
718 if (!s) {
719 report("Invalid subregister index for physical register",
720 MO, MONum);
721 return;
722 }
723 sr = s;
724 }
Chris Lattnercb778a82009-07-29 21:10:12 +0000725 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000726 if (!DRC->contains(sr)) {
727 report("Illegal physical register for instruction", MO, MONum);
728 *OS << TRI->getName(sr) << " is not a "
729 << DRC->getName() << " register.\n";
730 }
731 }
732 } else {
733 // Virtual register.
734 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
735 if (SubIdx) {
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000736 const TargetRegisterClass *SRC = RC->getSubRegisterRegClass(SubIdx);
737 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000738 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000739 *OS << "Register class " << RC->getName()
740 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000741 return;
742 }
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000743 RC = SRC;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000744 }
Chris Lattnercb778a82009-07-29 21:10:12 +0000745 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000746 if (RC != DRC && !RC->hasSuperClass(DRC)) {
747 report("Illegal virtual register for instruction", MO, MONum);
748 *OS << "Expected a " << DRC->getName() << " register, but got a "
749 << RC->getName() << " register\n";
750 }
751 }
752 }
753 }
754 break;
755 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000756
757 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000758 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
759 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000760 break;
761
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000762 case MachineOperand::MO_FrameIndex:
763 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
764 LiveInts && !LiveInts->isNotInMIMap(MI)) {
765 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
766 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
767 if (TI.mayLoad() && !LI.liveAt(Idx.getUseIndex())) {
768 report("Instruction loads from dead spill slot", MO, MONum);
769 *OS << "Live stack: " << LI << '\n';
770 }
771 if (TI.mayStore() && !LI.liveAt(Idx.getDefIndex())) {
772 report("Instruction stores to dead spill slot", MO, MONum);
773 *OS << "Live stack: " << LI << '\n';
774 }
775 }
776 break;
777
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000778 default:
779 break;
780 }
781}
782
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000783void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000784 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
785 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000786 set_subtract(regsLive, regsKilled); regsKilled.clear();
787 set_subtract(regsLive, regsDead); regsDead.clear();
788 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000789
790 if (Indexes && Indexes->hasIndex(MI)) {
791 SlotIndex idx = Indexes->getInstructionIndex(MI);
792 if (!(idx > lastIndex)) {
793 report("Instruction index out of order", MI);
794 *OS << "Last instruction was at " << lastIndex << '\n';
795 }
796 lastIndex = idx;
797 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000798}
799
800void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000801MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000802 MBBInfoMap[MBB].regsLiveOut = regsLive;
803 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000804
805 if (Indexes) {
806 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
807 if (!(stop > lastIndex)) {
808 report("Block ends before last instruction index", MBB);
809 *OS << "Block ends at " << stop
810 << " last instruction was at " << lastIndex << '\n';
811 }
812 lastIndex = stop;
813 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000814}
815
816// Calculate the largest possible vregsPassed sets. These are the registers that
817// can pass through an MBB live, but may not be live every time. It is assumed
818// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000819void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000820 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
821 // have any vregsPassed.
822 DenseSet<const MachineBasicBlock*> todo;
823 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
824 MFI != MFE; ++MFI) {
825 const MachineBasicBlock &MBB(*MFI);
826 BBInfo &MInfo = MBBInfoMap[&MBB];
827 if (!MInfo.reachable)
828 continue;
829 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
830 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
831 BBInfo &SInfo = MBBInfoMap[*SuI];
832 if (SInfo.addPassed(MInfo.regsLiveOut))
833 todo.insert(*SuI);
834 }
835 }
836
837 // Iteratively push vregsPassed to successors. This will converge to the same
838 // final state regardless of DenseSet iteration order.
839 while (!todo.empty()) {
840 const MachineBasicBlock *MBB = *todo.begin();
841 todo.erase(MBB);
842 BBInfo &MInfo = MBBInfoMap[MBB];
843 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
844 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
845 if (*SuI == MBB)
846 continue;
847 BBInfo &SInfo = MBBInfoMap[*SuI];
848 if (SInfo.addPassed(MInfo.vregsPassed))
849 todo.insert(*SuI);
850 }
851 }
852}
853
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000854// Calculate the set of virtual registers that must be passed through each basic
855// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000856// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000857void MachineVerifier::calcRegsRequired() {
858 // First push live-in regs to predecessors' vregsRequired.
859 DenseSet<const MachineBasicBlock*> todo;
860 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
861 MFI != MFE; ++MFI) {
862 const MachineBasicBlock &MBB(*MFI);
863 BBInfo &MInfo = MBBInfoMap[&MBB];
864 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
865 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
866 BBInfo &PInfo = MBBInfoMap[*PrI];
867 if (PInfo.addRequired(MInfo.vregsLiveIn))
868 todo.insert(*PrI);
869 }
870 }
871
872 // Iteratively push vregsRequired to predecessors. This will converge to the
873 // same final state regardless of DenseSet iteration order.
874 while (!todo.empty()) {
875 const MachineBasicBlock *MBB = *todo.begin();
876 todo.erase(MBB);
877 BBInfo &MInfo = MBBInfoMap[MBB];
878 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
879 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
880 if (*PrI == MBB)
881 continue;
882 BBInfo &SInfo = MBBInfoMap[*PrI];
883 if (SInfo.addRequired(MInfo.vregsRequired))
884 todo.insert(*PrI);
885 }
886 }
887}
888
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000889// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000890// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000891void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000892 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +0000893 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000894 DenseSet<const MachineBasicBlock*> seen;
895
896 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
897 unsigned Reg = BBI->getOperand(i).getReg();
898 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
899 if (!Pre->isSuccessor(MBB))
900 continue;
901 seen.insert(Pre);
902 BBInfo &PrInfo = MBBInfoMap[Pre];
903 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
904 report("PHI operand is not live-out from predecessor",
905 &BBI->getOperand(i), i);
906 }
907
908 // Did we see all predecessors?
909 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
910 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
911 if (!seen.count(*PrI)) {
912 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000913 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000914 << " is a predecessor according to the CFG.\n";
915 }
916 }
917 }
918}
919
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000920void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000921 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000922
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000923 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
924 MFI != MFE; ++MFI) {
925 BBInfo &MInfo = MBBInfoMap[MFI];
926
927 // Skip unreachable MBBs.
928 if (!MInfo.reachable)
929 continue;
930
931 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000932 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000933
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000934 // Now check liveness info if available
935 if (LiveVars || LiveInts)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000936 calcRegsRequired();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000937 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000938 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000939 if (LiveInts)
940 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000941}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000942
943void MachineVerifier::verifyLiveVariables() {
944 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +0000945 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
946 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000947 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
948 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
949 MFI != MFE; ++MFI) {
950 BBInfo &MInfo = MBBInfoMap[MFI];
951
952 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
953 if (MInfo.vregsRequired.count(Reg)) {
954 if (!VI.AliveBlocks.test(MFI->getNumber())) {
955 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000956 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000957 << " must be live through the block.\n";
958 }
959 } else {
960 if (VI.AliveBlocks.test(MFI->getNumber())) {
961 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000962 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000963 << " is not needed live through the block.\n";
964 }
965 }
966 }
967 }
968}
969
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000970void MachineVerifier::verifyLiveIntervals() {
971 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
972 for (LiveIntervals::const_iterator LVI = LiveInts->begin(),
973 LVE = LiveInts->end(); LVI != LVE; ++LVI) {
974 const LiveInterval &LI = *LVI->second;
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +0000975
976 // Spilling and splitting may leave unused registers around. Skip them.
977 if (MRI->use_empty(LI.reg))
978 continue;
979
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +0000980 // Physical registers have much weirdness going on, mostly from coalescing.
981 // We should probably fix it, but for now just ignore them.
982 if (TargetRegisterInfo::isPhysicalRegister(LI.reg))
983 continue;
984
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000985 assert(LVI->first == LI.reg && "Invalid reg to interval mapping");
986
987 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
988 I!=E; ++I) {
989 VNInfo *VNI = *I;
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000990 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000991
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000992 if (!DefVNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000993 if (!VNI->isUnused()) {
994 report("Valno not live at def and not marked unused", MF);
995 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
996 }
997 continue;
998 }
999
1000 if (VNI->isUnused())
1001 continue;
1002
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001003 if (DefVNI != VNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001004 report("Live range at def has different valno", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001005 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001006 << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001007 continue;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001008 }
1009
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001010 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1011 if (!MBB) {
1012 report("Invalid definition index", MF);
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001013 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1014 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001015 continue;
1016 }
1017
1018 if (VNI->isPHIDef()) {
1019 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1020 report("PHIDef value is not defined at MBB start", MF);
1021 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001022 << ", not at the beginning of BB#" << MBB->getNumber()
1023 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001024 }
1025 } else {
1026 // Non-PHI def.
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001027 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1028 if (!MI) {
1029 report("No instruction at def index", MF);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001030 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1031 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001032 } else if (!MI->modifiesRegister(LI.reg, TRI)) {
1033 report("Defining instruction does not modify register", MI);
1034 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1035 }
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001036
1037 bool isEarlyClobber = false;
1038 if (MI) {
1039 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1040 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1041 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() &&
1042 MOI->isEarlyClobber()) {
1043 isEarlyClobber = true;
1044 break;
1045 }
1046 }
1047 }
1048
1049 // Early clobber defs begin at USE slots, but other defs must begin at
1050 // DEF slots.
1051 if (isEarlyClobber) {
1052 if (!VNI->def.isUse()) {
1053 report("Early clobber def must be at a USE slot", MF);
1054 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1055 << " in " << LI << '\n';
1056 }
1057 } else if (!VNI->def.isDef()) {
1058 report("Non-PHI, non-early clobber def must be at a DEF slot", MF);
1059 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1060 << " in " << LI << '\n';
1061 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001062 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001063 }
1064
1065 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001066 const VNInfo *VNI = I->valno;
1067 assert(VNI && "Live range has no valno");
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001068
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001069 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001070 report("Foreign valno in live range", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001071 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001072 *OS << " has a valno not in " << LI << '\n';
1073 }
1074
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001075 if (VNI->isUnused()) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001076 report("Live range valno is marked unused", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001077 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001078 *OS << " in " << LI << '\n';
1079 }
1080
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001081 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1082 if (!MBB) {
1083 report("Bad start of live segment, no basic block", MF);
1084 I->print(*OS);
1085 *OS << " in " << LI << '\n';
1086 continue;
1087 }
1088 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1089 if (I->start != MBBStartIdx && I->start != VNI->def) {
1090 report("Live segment must begin at MBB entry or valno def", MBB);
1091 I->print(*OS);
1092 *OS << " in " << LI << '\n' << "Basic block starts at "
1093 << MBBStartIdx << '\n';
1094 }
1095
1096 const MachineBasicBlock *EndMBB =
1097 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1098 if (!EndMBB) {
1099 report("Bad end of live segment, no basic block", MF);
1100 I->print(*OS);
1101 *OS << " in " << LI << '\n';
1102 continue;
1103 }
1104 if (I->end != LiveInts->getMBBEndIdx(EndMBB)) {
1105 // The live segment is ending inside EndMBB
1106 const MachineInstr *MI =
1107 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1108 if (!MI) {
1109 report("Live segment doesn't end at a valid instruction", EndMBB);
1110 I->print(*OS);
1111 *OS << " in " << LI << '\n' << "Basic block starts at "
1112 << MBBStartIdx << '\n';
1113 } else if (TargetRegisterInfo::isVirtualRegister(LI.reg) &&
1114 !MI->readsVirtualRegister(LI.reg)) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001115 // A live range can end with either a redefinition, a kill flag on a
1116 // use, or a dead flag on a def.
1117 // FIXME: Should we check for each of these?
1118 bool hasDeadDef = false;
1119 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1120 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
Cameron Zwarich5e61f992010-12-20 02:59:51 +00001121 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() && MOI->isDead()) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001122 hasDeadDef = true;
1123 break;
1124 }
1125 }
1126
1127 if (!hasDeadDef) {
1128 report("Instruction killing live segment neither defines nor reads "
1129 "register", MI);
1130 I->print(*OS);
1131 *OS << " in " << LI << '\n';
1132 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001133 }
1134 }
1135
1136 // Now check all the basic blocks in this live segment.
1137 MachineFunction::const_iterator MFI = MBB;
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001138 // Is this live range the beginning of a non-PHIDef VN?
1139 if (I->start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001140 // Not live-in to any blocks.
1141 if (MBB == EndMBB)
1142 continue;
1143 // Skip this block.
1144 ++MFI;
1145 }
1146 for (;;) {
1147 assert(LiveInts->isLiveInToMBB(LI, MFI));
Jakob Stoklund Olesene459d552010-10-26 16:49:23 +00001148 // We don't know how to track physregs into a landing pad.
1149 if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1150 MFI->isLandingPad()) {
1151 if (&*MFI == EndMBB)
1152 break;
1153 ++MFI;
1154 continue;
1155 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001156 // Check that VNI is live-out of all predecessors.
1157 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1158 PE = MFI->pred_end(); PI != PE; ++PI) {
1159 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI).getPrevSlot();
1160 const VNInfo *PVNI = LI.getVNInfoAt(PEnd);
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001161
1162 if (VNI->isPHIDef() && VNI->def == LiveInts->getMBBStartIdx(MFI)) {
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001163 if (PVNI && !PVNI->hasPHIKill()) {
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001164 report("Value live out of predecessor doesn't have PHIKill", MF);
1165 *OS << "Valno #" << PVNI->id << " live out of BB#"
1166 << (*PI)->getNumber() << '@' << PEnd
1167 << " doesn't have PHIKill, but Valno #" << VNI->id
1168 << " is PHIDef and defined at the beginning of BB#"
1169 << MFI->getNumber() << '@' << LiveInts->getMBBStartIdx(MFI)
1170 << " in " << LI << '\n';
1171 }
1172 continue;
1173 }
1174
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001175 if (!PVNI) {
1176 report("Register not marked live out of predecessor", *PI);
1177 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1178 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live at "
1179 << PEnd << " in " << LI << '\n';
1180 continue;
1181 }
1182
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001183 if (PVNI != VNI) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001184 report("Different value live out of predecessor", *PI);
1185 *OS << "Valno #" << PVNI->id << " live out of BB#"
1186 << (*PI)->getNumber() << '@' << PEnd
1187 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1188 << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n';
1189 }
1190 }
1191 if (&*MFI == EndMBB)
1192 break;
1193 ++MFI;
1194 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001195 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001196
1197 // Check the LI only has one connected component.
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001198 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1199 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1200 unsigned NumComp = ConEQ.Classify(&LI);
1201 if (NumComp > 1) {
1202 report("Multiple connected components in live interval", MF);
1203 *OS << NumComp << " components in " << LI << '\n';
Jakob Stoklund Olesencb367772010-10-29 00:40:57 +00001204 for (unsigned comp = 0; comp != NumComp; ++comp) {
1205 *OS << comp << ": valnos";
1206 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1207 E = LI.vni_end(); I!=E; ++I)
1208 if (comp == ConEQ.getEqClass(*I))
1209 *OS << ' ' << (*I)->id;
1210 *OS << '\n';
1211 }
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001212 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001213 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001214 }
1215}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001216