blob: fa9092b5ce886905859c166c48d49997f39125cf [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
Bill Wendlingd29052b2011-05-04 22:54:05 +000026#include "llvm/Instructions.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000027#include "llvm/Function.h"
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +000028#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000029#include "llvm/CodeGen/LiveVariables.h"
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +000030#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman2dbc4c82009-10-07 17:36:00 +000033#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
35#include "llvm/CodeGen/Passes.h"
Bill Wendlingd29052b2011-05-04 22:54:05 +000036#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000037#include "llvm/Target/TargetMachine.h"
38#include "llvm/Target/TargetRegisterInfo.h"
39#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000040#include "llvm/ADT/DenseSet.h"
41#include "llvm/ADT/SetOperations.h"
42#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000043#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000044#include "llvm/Support/ErrorHandling.h"
45#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000046using namespace llvm;
47
48namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000049 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000050
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000051 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000052 PASS(pass),
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000053 Banner(b),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000054 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000055 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000056
57 bool runOnMachineFunction(MachineFunction &MF);
58
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000059 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000060 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000061 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000062 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000063 const MachineFunction *MF;
64 const TargetMachine *TM;
65 const TargetRegisterInfo *TRI;
66 const MachineRegisterInfo *MRI;
67
68 unsigned foundErrors;
69
70 typedef SmallVector<unsigned, 16> RegVector;
71 typedef DenseSet<unsigned> RegSet;
72 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
73
74 BitVector regsReserved;
75 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000076 RegVector regsDefined, regsDead, regsKilled;
77 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000078
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000079 SlotIndex lastIndex;
80
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000081 // Add Reg and any sub-registers to RV
82 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
83 RV.push_back(Reg);
84 if (TargetRegisterInfo::isPhysicalRegister(Reg))
85 for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
86 RV.push_back(*R);
87 }
88
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000089 struct BBInfo {
90 // Is this MBB reachable from the MF entry point?
91 bool reachable;
92
93 // Vregs that must be live in because they are used without being
94 // defined. Map value is the user.
95 RegMap vregsLiveIn;
96
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000097 // Regs killed in MBB. They may be defined again, and will then be in both
98 // regsKilled and regsLiveOut.
99 RegSet regsKilled;
100
101 // Regs defined in MBB and live out. Note that vregs passing through may
102 // be live out without being mentioned here.
103 RegSet regsLiveOut;
104
105 // Vregs that pass through MBB untouched. This set is disjoint from
106 // regsKilled and regsLiveOut.
107 RegSet vregsPassed;
108
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000109 // Vregs that must pass through MBB because they are needed by a successor
110 // block. This set is disjoint from regsLiveOut.
111 RegSet vregsRequired;
112
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000113 BBInfo() : reachable(false) {}
114
115 // Add register to vregsPassed if it belongs there. Return true if
116 // anything changed.
117 bool addPassed(unsigned Reg) {
118 if (!TargetRegisterInfo::isVirtualRegister(Reg))
119 return false;
120 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
121 return false;
122 return vregsPassed.insert(Reg).second;
123 }
124
125 // Same for a full set.
126 bool addPassed(const RegSet &RS) {
127 bool changed = false;
128 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
129 if (addPassed(*I))
130 changed = true;
131 return changed;
132 }
133
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000134 // Add register to vregsRequired if it belongs there. Return true if
135 // anything changed.
136 bool addRequired(unsigned Reg) {
137 if (!TargetRegisterInfo::isVirtualRegister(Reg))
138 return false;
139 if (regsLiveOut.count(Reg))
140 return false;
141 return vregsRequired.insert(Reg).second;
142 }
143
144 // Same for a full set.
145 bool addRequired(const RegSet &RS) {
146 bool changed = false;
147 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
148 if (addRequired(*I))
149 changed = true;
150 return changed;
151 }
152
153 // Same for a full map.
154 bool addRequired(const RegMap &RM) {
155 bool changed = false;
156 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
157 if (addRequired(I->first))
158 changed = true;
159 return changed;
160 }
161
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000162 // Live-out registers are either in regsLiveOut or vregsPassed.
163 bool isLiveOut(unsigned Reg) const {
164 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
165 }
166 };
167
168 // Extra register info per MBB.
169 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
170
171 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000172 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000173 }
174
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000175 // Analysis information if available
176 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000177 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000178 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000179 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000180
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000181 void visitMachineFunctionBefore();
182 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
183 void visitMachineInstrBefore(const MachineInstr *MI);
184 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
185 void visitMachineInstrAfter(const MachineInstr *MI);
186 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
187 void visitMachineFunctionAfter();
188
189 void report(const char *msg, const MachineFunction *MF);
190 void report(const char *msg, const MachineBasicBlock *MBB);
191 void report(const char *msg, const MachineInstr *MI);
192 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
193
194 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000195 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000196 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000197
198 void calcRegsRequired();
199 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000200 void verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000201 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000202
203 struct MachineVerifierPass : public MachineFunctionPass {
204 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000205 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000206
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000207 MachineVerifierPass(const char *b = 0)
208 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000209 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
210 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000211
212 void getAnalysisUsage(AnalysisUsage &AU) const {
213 AU.setPreservesAll();
214 MachineFunctionPass::getAnalysisUsage(AU);
215 }
216
217 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000218 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000219 return false;
220 }
221 };
222
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000223}
224
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000225char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000226INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000227 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000228
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000229FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
230 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000231}
232
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000233void MachineFunction::verify(Pass *p, const char *Banner) const {
234 MachineVerifier(p, Banner)
235 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000236}
237
Chris Lattner17e9edc2009-08-23 02:51:22 +0000238bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
239 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000240 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000241 std::string ErrorInfo;
242 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
243 raw_fd_ostream::F_Append);
244 if (!ErrorInfo.empty()) {
245 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
246 exit(1);
247 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000248
Chris Lattner17e9edc2009-08-23 02:51:22 +0000249 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000250 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000251 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000252 }
253
254 foundErrors = 0;
255
256 this->MF = &MF;
257 TM = &MF.getTarget();
258 TRI = TM->getRegisterInfo();
259 MRI = &MF.getRegInfo();
260
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000261 LiveVars = NULL;
262 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000263 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000264 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000265 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000266 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000267 // We don't want to verify LiveVariables if LiveIntervals is available.
268 if (!LiveInts)
269 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000270 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000271 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000272 }
273
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000274 visitMachineFunctionBefore();
275 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
276 MFI!=MFE; ++MFI) {
277 visitMachineBasicBlockBefore(MFI);
278 for (MachineBasicBlock::const_iterator MBBI = MFI->begin(),
279 MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000280 if (MBBI->getParent() != MFI) {
281 report("Bad instruction parent pointer", MFI);
282 *OS << "Instruction: " << *MBBI;
283 continue;
284 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000285 visitMachineInstrBefore(MBBI);
286 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
287 visitMachineOperand(&MBBI->getOperand(I), I);
288 visitMachineInstrAfter(MBBI);
289 }
290 visitMachineBasicBlockAfter(MFI);
291 }
292 visitMachineFunctionAfter();
293
Chris Lattner17e9edc2009-08-23 02:51:22 +0000294 if (OutFile)
295 delete OutFile;
296 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000297 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000298
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000299 // Clean up.
300 regsLive.clear();
301 regsDefined.clear();
302 regsDead.clear();
303 regsKilled.clear();
304 regsLiveInButUnused.clear();
305 MBBInfoMap.clear();
306
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000307 return false; // no changes
308}
309
Chris Lattner372fefe2009-08-23 01:03:30 +0000310void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000311 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000312 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000313 if (!foundErrors++) {
314 if (Banner)
315 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000316 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000317 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000318 *OS << "*** Bad machine code: " << msg << " ***\n"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000319 << "- function: " << MF->getFunction()->getNameStr() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000320}
321
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000322void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000323 assert(MBB);
324 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000325 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000326 << " " << (void*)MBB
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000327 << " (BB#" << MBB->getNumber() << ")";
328 if (Indexes)
329 *OS << " [" << Indexes->getMBBStartIdx(MBB)
330 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
331 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000332}
333
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000334void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000335 assert(MI);
336 report(msg, MI->getParent());
337 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000338 if (Indexes && Indexes->hasIndex(MI))
339 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000340 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000341}
342
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000343void MachineVerifier::report(const char *msg,
344 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000345 assert(MO);
346 report(msg, MO->getParent());
347 *OS << "- operand " << MONum << ": ";
348 MO->print(*OS, TM);
349 *OS << "\n";
350}
351
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000352void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000353 BBInfo &MInfo = MBBInfoMap[MBB];
354 if (!MInfo.reachable) {
355 MInfo.reachable = true;
356 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
357 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
358 markReachable(*SuI);
359 }
360}
361
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000362void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000363 lastIndex = SlotIndex();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000364 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000365
366 // A sub-register of a reserved register is also reserved
367 for (int Reg = regsReserved.find_first(); Reg>=0;
368 Reg = regsReserved.find_next(Reg)) {
369 for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
370 // FIXME: This should probably be:
371 // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
372 regsReserved.set(*Sub);
373 }
374 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000375 markReachable(&MF->front());
376}
377
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000378// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000379static bool matchPair(MachineBasicBlock::const_succ_iterator i,
380 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000381 if (*i == a)
382 return *++i == b;
383 if (*i == b)
384 return *++i == a;
385 return false;
386}
387
388void
389MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Dan Gohman27920592009-08-27 02:43:49 +0000390 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
391
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000392 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000393 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000394 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000395 E = MBB->succ_end(); I != E; ++I) {
396 if ((*I)->isLandingPad())
397 LandingPadSuccs.insert(*I);
398 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000399
400 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
401 const BasicBlock *BB = MBB->getBasicBlock();
402 if (LandingPadSuccs.size() > 1 &&
403 !(AsmInfo &&
404 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
405 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000406 report("MBB has more than one landing pad successor", MBB);
407
Dan Gohman27920592009-08-27 02:43:49 +0000408 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
409 MachineBasicBlock *TBB = 0, *FBB = 0;
410 SmallVector<MachineOperand, 4> Cond;
411 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
412 TBB, FBB, Cond)) {
Jakob Stoklund Olesen3e228972011-04-05 23:43:11 +0000413 // If the block branches directly to a landing pad successor, pretend that
414 // the landing pad is a normal block.
415 LandingPadSuccs.erase(TBB);
416 LandingPadSuccs.erase(FBB);
417
Dan Gohman27920592009-08-27 02:43:49 +0000418 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
419 // check whether its answers match up with reality.
420 if (!TBB && !FBB) {
421 // Block falls through to its successor.
422 MachineFunction::const_iterator MBBI = MBB;
423 ++MBBI;
424 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000425 // It's possible that the block legitimately ends with a noreturn
426 // call or an unreachable, in which case it won't actually fall
427 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000428 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000429 // It's possible that the block legitimately ends with a noreturn
430 // call or an unreachable, in which case it won't actuall fall
431 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000432 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000433 report("MBB exits via unconditional fall-through but doesn't have "
434 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000435 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000436 report("MBB exits via unconditional fall-through but its successor "
437 "differs from its CFG successor!", MBB);
438 }
Evan Cheng86050dc2010-06-18 23:09:54 +0000439 if (!MBB->empty() && MBB->back().getDesc().isBarrier() &&
440 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000441 report("MBB exits via unconditional fall-through but ends with a "
442 "barrier instruction!", MBB);
443 }
444 if (!Cond.empty()) {
445 report("MBB exits via unconditional fall-through but has a condition!",
446 MBB);
447 }
448 } else if (TBB && !FBB && Cond.empty()) {
449 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000450 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000451 report("MBB exits via unconditional branch but doesn't have "
452 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000453 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000454 report("MBB exits via unconditional branch but the CFG "
455 "successor doesn't match the actual successor!", MBB);
456 }
457 if (MBB->empty()) {
458 report("MBB exits via unconditional branch but doesn't contain "
459 "any instructions!", MBB);
460 } else if (!MBB->back().getDesc().isBarrier()) {
461 report("MBB exits via unconditional branch but doesn't end with a "
462 "barrier instruction!", MBB);
463 } else if (!MBB->back().getDesc().isTerminator()) {
464 report("MBB exits via unconditional branch but the branch isn't a "
465 "terminator instruction!", MBB);
466 }
467 } else if (TBB && !FBB && !Cond.empty()) {
468 // Block conditionally branches somewhere, otherwise falls through.
469 MachineFunction::const_iterator MBBI = MBB;
470 ++MBBI;
471 if (MBBI == MF->end()) {
472 report("MBB conditionally falls through out of function!", MBB);
473 } if (MBB->succ_size() != 2) {
474 report("MBB exits via conditional branch/fall-through but doesn't have "
475 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000476 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000477 report("MBB exits via conditional branch/fall-through but the CFG "
478 "successors don't match the actual successors!", MBB);
479 }
480 if (MBB->empty()) {
481 report("MBB exits via conditional branch/fall-through but doesn't "
482 "contain any instructions!", MBB);
483 } else if (MBB->back().getDesc().isBarrier()) {
484 report("MBB exits via conditional branch/fall-through but ends with a "
485 "barrier instruction!", MBB);
486 } else if (!MBB->back().getDesc().isTerminator()) {
487 report("MBB exits via conditional branch/fall-through but the branch "
488 "isn't a terminator instruction!", MBB);
489 }
490 } else if (TBB && FBB) {
491 // Block conditionally branches somewhere, otherwise branches
492 // somewhere else.
493 if (MBB->succ_size() != 2) {
494 report("MBB exits via conditional branch/branch but doesn't have "
495 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000496 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000497 report("MBB exits via conditional branch/branch but the CFG "
498 "successors don't match the actual successors!", MBB);
499 }
500 if (MBB->empty()) {
501 report("MBB exits via conditional branch/branch but doesn't "
502 "contain any instructions!", MBB);
503 } else if (!MBB->back().getDesc().isBarrier()) {
504 report("MBB exits via conditional branch/branch but doesn't end with a "
505 "barrier instruction!", MBB);
506 } else if (!MBB->back().getDesc().isTerminator()) {
507 report("MBB exits via conditional branch/branch but the branch "
508 "isn't a terminator instruction!", MBB);
509 }
510 if (Cond.empty()) {
511 report("MBB exits via conditinal branch/branch but there's no "
512 "condition!", MBB);
513 }
514 } else {
515 report("AnalyzeBranch returned invalid data!", MBB);
516 }
517 }
518
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000519 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000520 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000521 E = MBB->livein_end(); I != E; ++I) {
522 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
523 report("MBB live-in list contains non-physical register", MBB);
524 continue;
525 }
526 regsLive.insert(*I);
527 for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++)
528 regsLive.insert(*R);
529 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000530 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000531
532 const MachineFrameInfo *MFI = MF->getFrameInfo();
533 assert(MFI && "Function has no frame info");
534 BitVector PR = MFI->getPristineRegs(MBB);
535 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
536 regsLive.insert(I);
537 for (const unsigned *R = TRI->getSubRegisters(I); *R; R++)
538 regsLive.insert(*R);
539 }
540
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000541 regsKilled.clear();
542 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000543
544 if (Indexes)
545 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000546}
547
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000548void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000549 const TargetInstrDesc &TI = MI->getDesc();
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000550 if (MI->getNumOperands() < TI.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000551 report("Too few operands", MI);
552 *OS << TI.getNumOperands() << " operands expected, but "
553 << MI->getNumExplicitOperands() << " given.\n";
554 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000555
556 // Check the MachineMemOperands for basic consistency.
557 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
558 E = MI->memoperands_end(); I != E; ++I) {
559 if ((*I)->isLoad() && !TI.mayLoad())
560 report("Missing mayLoad flag", MI);
561 if ((*I)->isStore() && !TI.mayStore())
562 report("Missing mayStore flag", MI);
563 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000564
565 // Debug values must not have a slot index.
566 // Other instructions must have one.
567 if (LiveInts) {
568 bool mapped = !LiveInts->isNotInMIMap(MI);
569 if (MI->isDebugValue()) {
570 if (mapped)
571 report("Debug instruction has a slot index", MI);
572 } else {
573 if (!mapped)
574 report("Missing slot index", MI);
575 }
576 }
577
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000578}
579
580void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000581MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000582 const MachineInstr *MI = MO->getParent();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000583 const TargetInstrDesc &TI = MI->getDesc();
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000584 const TargetOperandInfo &TOI = TI.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000585
586 // The first TI.NumDefs operands must be explicit register defines
587 if (MONum < TI.getNumDefs()) {
588 if (!MO->isReg())
589 report("Explicit definition must be a register", MO, MONum);
590 else if (!MO->isDef())
591 report("Explicit definition marked as use", MO, MONum);
592 else if (MO->isImplicit())
593 report("Explicit definition marked as implicit", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000594 } else if (MONum < TI.getNumOperands()) {
Eric Christopher113a06c2010-11-17 00:55:36 +0000595 // Don't check if it's the last operand in a variadic instruction. See,
596 // e.g., LDM_RET in the arm back end.
597 if (MO->isReg() && !(TI.isVariadic() && MONum == TI.getNumOperands()-1)) {
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000598 if (MO->isDef() && !TOI.isOptionalDef())
599 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000600 if (MO->isImplicit())
601 report("Explicit operand marked as implicit", MO, MONum);
602 }
603 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000604 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
605 if (MO->isReg() && !MO->isImplicit() && !TI.isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000606 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000607 }
608
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000609 switch (MO->getType()) {
610 case MachineOperand::MO_Register: {
611 const unsigned Reg = MO->getReg();
612 if (!Reg)
613 return;
614
615 // Check Live Variables.
Cameron Zwarich8ec88ba2010-12-20 00:08:10 +0000616 if (MI->isDebugValue()) {
617 // Liveness checks are not valid for debug values.
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000618 } else if (MO->isUse() && !MO->isUndef()) {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000619 regsLiveInButUnused.erase(Reg);
620
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000621 bool isKill = false;
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000622 unsigned defIdx;
623 if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
624 // A two-addr use counts as a kill if use and def are the same.
625 unsigned DefReg = MI->getOperand(defIdx).getReg();
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000626 if (Reg == DefReg)
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000627 isKill = true;
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000628 else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000629 report("Two-address instruction operands must be identical",
630 MO, MONum);
631 }
632 } else
633 isKill = MO->isKill();
634
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000635 if (isKill)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000636 addRegWithSubRegs(regsKilled, Reg);
637
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000638 // Check that LiveVars knows this kill.
639 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
640 MO->isKill()) {
641 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
642 if (std::find(VI.Kills.begin(),
643 VI.Kills.end(), MI) == VI.Kills.end())
644 report("Kill missing from LiveVariables", MO, MONum);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000645 }
646
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000647 // Check LiveInts liveness and kill.
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000648 if (TargetRegisterInfo::isVirtualRegister(Reg) &&
649 LiveInts && !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000650 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getUseIndex();
651 if (LiveInts->hasInterval(Reg)) {
652 const LiveInterval &LI = LiveInts->getInterval(Reg);
653 if (!LI.liveAt(UseIdx)) {
654 report("No live range at use", MO, MONum);
655 *OS << UseIdx << " is not live in " << LI << '\n';
656 }
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000657 // Check for extra kill flags.
658 // Note that we allow missing kill flags for now.
659 if (MO->isKill() && !LI.killedAt(UseIdx.getDefIndex())) {
660 report("Live range continues after kill flag", MO, MONum);
661 *OS << "Live range: " << LI << '\n';
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000662 }
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000663 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000664 report("Virtual register has no Live interval", MO, MONum);
665 }
666 }
667
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000668 // Use of a dead register.
669 if (!regsLive.count(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000670 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
671 // Reserved registers may be used even when 'dead'.
672 if (!isReserved(Reg))
673 report("Using an undefined physical register", MO, MONum);
674 } else {
675 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
676 // We don't know which virtual registers are live in, so only complain
677 // if vreg was killed in this MBB. Otherwise keep track of vregs that
678 // must be live in. PHI instructions are handled separately.
679 if (MInfo.regsKilled.count(Reg))
680 report("Using a killed virtual register", MO, MONum);
Chris Lattner518bb532010-02-09 19:54:29 +0000681 else if (!MI->isPHI())
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000682 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
683 }
Duncan Sandse5567202009-05-16 03:28:54 +0000684 }
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000685 } else if (MO->isDef()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000686 // Register defined.
687 // TODO: verify that earlyclobber ops are not used.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000688 if (MO->isDead())
689 addRegWithSubRegs(regsDead, Reg);
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000690 else
691 addRegWithSubRegs(regsDefined, Reg);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000692
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000693 // Check LiveInts for a live range, but only for virtual registers.
694 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
695 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000696 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getDefIndex();
697 if (LiveInts->hasInterval(Reg)) {
698 const LiveInterval &LI = LiveInts->getInterval(Reg);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000699 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
700 assert(VNI && "NULL valno is not allowed");
Cameron Zwarich1b031dd2010-12-19 23:50:53 +0000701 if (VNI->def != DefIdx && !MO->isEarlyClobber()) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000702 report("Inconsistent valno->def", MO, MONum);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000703 *OS << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000704 << DefIdx << " in " << LI << '\n';
705 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000706 } else {
707 report("No live range at def", MO, MONum);
708 *OS << DefIdx << " is not live in " << LI << '\n';
709 }
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000710 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000711 report("Virtual register has no Live interval", MO, MONum);
712 }
713 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000714 }
715
716 // Check register classes.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000717 if (MONum < TI.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000718 unsigned SubIdx = MO->getSubReg();
719
720 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
721 unsigned sr = Reg;
722 if (SubIdx) {
723 unsigned s = TRI->getSubReg(Reg, SubIdx);
724 if (!s) {
725 report("Invalid subregister index for physical register",
726 MO, MONum);
727 return;
728 }
729 sr = s;
730 }
Chris Lattnercb778a82009-07-29 21:10:12 +0000731 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000732 if (!DRC->contains(sr)) {
733 report("Illegal physical register for instruction", MO, MONum);
734 *OS << TRI->getName(sr) << " is not a "
735 << DRC->getName() << " register.\n";
736 }
737 }
738 } else {
739 // Virtual register.
740 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
741 if (SubIdx) {
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000742 const TargetRegisterClass *SRC = RC->getSubRegisterRegClass(SubIdx);
743 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000744 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000745 *OS << "Register class " << RC->getName()
746 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000747 return;
748 }
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000749 RC = SRC;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000750 }
Chris Lattnercb778a82009-07-29 21:10:12 +0000751 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000752 if (RC != DRC && !RC->hasSuperClass(DRC)) {
753 report("Illegal virtual register for instruction", MO, MONum);
754 *OS << "Expected a " << DRC->getName() << " register, but got a "
755 << RC->getName() << " register\n";
756 }
757 }
758 }
759 }
760 break;
761 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000762
763 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000764 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
765 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000766 break;
767
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000768 case MachineOperand::MO_FrameIndex:
769 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
770 LiveInts && !LiveInts->isNotInMIMap(MI)) {
771 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
772 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
773 if (TI.mayLoad() && !LI.liveAt(Idx.getUseIndex())) {
774 report("Instruction loads from dead spill slot", MO, MONum);
775 *OS << "Live stack: " << LI << '\n';
776 }
777 if (TI.mayStore() && !LI.liveAt(Idx.getDefIndex())) {
778 report("Instruction stores to dead spill slot", MO, MONum);
779 *OS << "Live stack: " << LI << '\n';
780 }
781 }
782 break;
783
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000784 default:
785 break;
786 }
787}
788
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000789void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000790 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
791 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000792 set_subtract(regsLive, regsKilled); regsKilled.clear();
793 set_subtract(regsLive, regsDead); regsDead.clear();
794 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000795
796 if (Indexes && Indexes->hasIndex(MI)) {
797 SlotIndex idx = Indexes->getInstructionIndex(MI);
798 if (!(idx > lastIndex)) {
799 report("Instruction index out of order", MI);
800 *OS << "Last instruction was at " << lastIndex << '\n';
801 }
802 lastIndex = idx;
803 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000804}
805
806void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000807MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000808 MBBInfoMap[MBB].regsLiveOut = regsLive;
809 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000810
811 if (Indexes) {
812 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
813 if (!(stop > lastIndex)) {
814 report("Block ends before last instruction index", MBB);
815 *OS << "Block ends at " << stop
816 << " last instruction was at " << lastIndex << '\n';
817 }
818 lastIndex = stop;
819 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000820}
821
822// Calculate the largest possible vregsPassed sets. These are the registers that
823// can pass through an MBB live, but may not be live every time. It is assumed
824// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000825void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000826 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
827 // have any vregsPassed.
828 DenseSet<const MachineBasicBlock*> todo;
829 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
830 MFI != MFE; ++MFI) {
831 const MachineBasicBlock &MBB(*MFI);
832 BBInfo &MInfo = MBBInfoMap[&MBB];
833 if (!MInfo.reachable)
834 continue;
835 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
836 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
837 BBInfo &SInfo = MBBInfoMap[*SuI];
838 if (SInfo.addPassed(MInfo.regsLiveOut))
839 todo.insert(*SuI);
840 }
841 }
842
843 // Iteratively push vregsPassed to successors. This will converge to the same
844 // final state regardless of DenseSet iteration order.
845 while (!todo.empty()) {
846 const MachineBasicBlock *MBB = *todo.begin();
847 todo.erase(MBB);
848 BBInfo &MInfo = MBBInfoMap[MBB];
849 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
850 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
851 if (*SuI == MBB)
852 continue;
853 BBInfo &SInfo = MBBInfoMap[*SuI];
854 if (SInfo.addPassed(MInfo.vregsPassed))
855 todo.insert(*SuI);
856 }
857 }
858}
859
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000860// Calculate the set of virtual registers that must be passed through each basic
861// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000862// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000863void MachineVerifier::calcRegsRequired() {
864 // First push live-in regs to predecessors' vregsRequired.
865 DenseSet<const MachineBasicBlock*> todo;
866 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
867 MFI != MFE; ++MFI) {
868 const MachineBasicBlock &MBB(*MFI);
869 BBInfo &MInfo = MBBInfoMap[&MBB];
870 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
871 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
872 BBInfo &PInfo = MBBInfoMap[*PrI];
873 if (PInfo.addRequired(MInfo.vregsLiveIn))
874 todo.insert(*PrI);
875 }
876 }
877
878 // Iteratively push vregsRequired to predecessors. This will converge to the
879 // same final state regardless of DenseSet iteration order.
880 while (!todo.empty()) {
881 const MachineBasicBlock *MBB = *todo.begin();
882 todo.erase(MBB);
883 BBInfo &MInfo = MBBInfoMap[MBB];
884 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
885 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
886 if (*PrI == MBB)
887 continue;
888 BBInfo &SInfo = MBBInfoMap[*PrI];
889 if (SInfo.addRequired(MInfo.vregsRequired))
890 todo.insert(*PrI);
891 }
892 }
893}
894
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000895// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000896// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000897void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000898 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +0000899 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000900 DenseSet<const MachineBasicBlock*> seen;
901
902 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
903 unsigned Reg = BBI->getOperand(i).getReg();
904 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
905 if (!Pre->isSuccessor(MBB))
906 continue;
907 seen.insert(Pre);
908 BBInfo &PrInfo = MBBInfoMap[Pre];
909 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
910 report("PHI operand is not live-out from predecessor",
911 &BBI->getOperand(i), i);
912 }
913
914 // Did we see all predecessors?
915 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
916 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
917 if (!seen.count(*PrI)) {
918 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000919 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000920 << " is a predecessor according to the CFG.\n";
921 }
922 }
923 }
924}
925
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000926void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000927 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000928
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000929 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
930 MFI != MFE; ++MFI) {
931 BBInfo &MInfo = MBBInfoMap[MFI];
932
933 // Skip unreachable MBBs.
934 if (!MInfo.reachable)
935 continue;
936
937 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000938 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000939
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000940 // Now check liveness info if available
941 if (LiveVars || LiveInts)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000942 calcRegsRequired();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000943 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000944 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000945 if (LiveInts)
946 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000947}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000948
949void MachineVerifier::verifyLiveVariables() {
950 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +0000951 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
952 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000953 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
954 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
955 MFI != MFE; ++MFI) {
956 BBInfo &MInfo = MBBInfoMap[MFI];
957
958 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
959 if (MInfo.vregsRequired.count(Reg)) {
960 if (!VI.AliveBlocks.test(MFI->getNumber())) {
961 report("LiveVariables: Block missing from 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 << " must be live through the block.\n";
964 }
965 } else {
966 if (VI.AliveBlocks.test(MFI->getNumber())) {
967 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000968 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000969 << " is not needed live through the block.\n";
970 }
971 }
972 }
973 }
974}
975
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000976void MachineVerifier::verifyLiveIntervals() {
977 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
978 for (LiveIntervals::const_iterator LVI = LiveInts->begin(),
979 LVE = LiveInts->end(); LVI != LVE; ++LVI) {
980 const LiveInterval &LI = *LVI->second;
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +0000981
982 // Spilling and splitting may leave unused registers around. Skip them.
983 if (MRI->use_empty(LI.reg))
984 continue;
985
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +0000986 // Physical registers have much weirdness going on, mostly from coalescing.
987 // We should probably fix it, but for now just ignore them.
988 if (TargetRegisterInfo::isPhysicalRegister(LI.reg))
989 continue;
990
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000991 assert(LVI->first == LI.reg && "Invalid reg to interval mapping");
992
993 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
994 I!=E; ++I) {
995 VNInfo *VNI = *I;
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000996 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000997
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000998 if (!DefVNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000999 if (!VNI->isUnused()) {
1000 report("Valno not live at def and not marked unused", MF);
1001 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1002 }
1003 continue;
1004 }
1005
1006 if (VNI->isUnused())
1007 continue;
1008
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001009 if (DefVNI != VNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001010 report("Live range at def has different valno", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001011 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001012 << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001013 continue;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001014 }
1015
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001016 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1017 if (!MBB) {
1018 report("Invalid definition index", MF);
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001019 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1020 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001021 continue;
1022 }
1023
1024 if (VNI->isPHIDef()) {
1025 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1026 report("PHIDef value is not defined at MBB start", MF);
1027 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001028 << ", not at the beginning of BB#" << MBB->getNumber()
1029 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001030 }
1031 } else {
1032 // Non-PHI def.
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001033 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1034 if (!MI) {
1035 report("No instruction at def index", MF);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001036 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1037 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001038 } else if (!MI->modifiesRegister(LI.reg, TRI)) {
1039 report("Defining instruction does not modify register", MI);
1040 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1041 }
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001042
1043 bool isEarlyClobber = false;
1044 if (MI) {
1045 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1046 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1047 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() &&
1048 MOI->isEarlyClobber()) {
1049 isEarlyClobber = true;
1050 break;
1051 }
1052 }
1053 }
1054
1055 // Early clobber defs begin at USE slots, but other defs must begin at
1056 // DEF slots.
1057 if (isEarlyClobber) {
1058 if (!VNI->def.isUse()) {
1059 report("Early clobber def must be at a USE slot", MF);
1060 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1061 << " in " << LI << '\n';
1062 }
1063 } else if (!VNI->def.isDef()) {
1064 report("Non-PHI, non-early clobber def must be at a DEF slot", MF);
1065 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1066 << " in " << LI << '\n';
1067 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001068 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001069 }
1070
1071 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001072 const VNInfo *VNI = I->valno;
1073 assert(VNI && "Live range has no valno");
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001074
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001075 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001076 report("Foreign valno in live range", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001077 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001078 *OS << " has a valno not in " << LI << '\n';
1079 }
1080
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001081 if (VNI->isUnused()) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001082 report("Live range valno is marked unused", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001083 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001084 *OS << " in " << LI << '\n';
1085 }
1086
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001087 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1088 if (!MBB) {
1089 report("Bad start of live segment, no basic block", MF);
1090 I->print(*OS);
1091 *OS << " in " << LI << '\n';
1092 continue;
1093 }
1094 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1095 if (I->start != MBBStartIdx && I->start != VNI->def) {
1096 report("Live segment must begin at MBB entry or valno def", MBB);
1097 I->print(*OS);
1098 *OS << " in " << LI << '\n' << "Basic block starts at "
1099 << MBBStartIdx << '\n';
1100 }
1101
1102 const MachineBasicBlock *EndMBB =
1103 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1104 if (!EndMBB) {
1105 report("Bad end of live segment, no basic block", MF);
1106 I->print(*OS);
1107 *OS << " in " << LI << '\n';
1108 continue;
1109 }
1110 if (I->end != LiveInts->getMBBEndIdx(EndMBB)) {
1111 // The live segment is ending inside EndMBB
1112 const MachineInstr *MI =
1113 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1114 if (!MI) {
1115 report("Live segment doesn't end at a valid instruction", EndMBB);
1116 I->print(*OS);
1117 *OS << " in " << LI << '\n' << "Basic block starts at "
1118 << MBBStartIdx << '\n';
1119 } else if (TargetRegisterInfo::isVirtualRegister(LI.reg) &&
1120 !MI->readsVirtualRegister(LI.reg)) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001121 // A live range can end with either a redefinition, a kill flag on a
1122 // use, or a dead flag on a def.
1123 // FIXME: Should we check for each of these?
1124 bool hasDeadDef = false;
1125 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1126 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
Cameron Zwarich5e61f992010-12-20 02:59:51 +00001127 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() && MOI->isDead()) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001128 hasDeadDef = true;
1129 break;
1130 }
1131 }
1132
1133 if (!hasDeadDef) {
1134 report("Instruction killing live segment neither defines nor reads "
1135 "register", MI);
1136 I->print(*OS);
1137 *OS << " in " << LI << '\n';
1138 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001139 }
1140 }
1141
1142 // Now check all the basic blocks in this live segment.
1143 MachineFunction::const_iterator MFI = MBB;
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001144 // Is this live range the beginning of a non-PHIDef VN?
1145 if (I->start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001146 // Not live-in to any blocks.
1147 if (MBB == EndMBB)
1148 continue;
1149 // Skip this block.
1150 ++MFI;
1151 }
1152 for (;;) {
1153 assert(LiveInts->isLiveInToMBB(LI, MFI));
Jakob Stoklund Olesene459d552010-10-26 16:49:23 +00001154 // We don't know how to track physregs into a landing pad.
1155 if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1156 MFI->isLandingPad()) {
1157 if (&*MFI == EndMBB)
1158 break;
1159 ++MFI;
1160 continue;
1161 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001162 // Check that VNI is live-out of all predecessors.
1163 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1164 PE = MFI->pred_end(); PI != PE; ++PI) {
1165 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI).getPrevSlot();
1166 const VNInfo *PVNI = LI.getVNInfoAt(PEnd);
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001167
1168 if (VNI->isPHIDef() && VNI->def == LiveInts->getMBBStartIdx(MFI)) {
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001169 if (PVNI && !PVNI->hasPHIKill()) {
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001170 report("Value live out of predecessor doesn't have PHIKill", MF);
1171 *OS << "Valno #" << PVNI->id << " live out of BB#"
1172 << (*PI)->getNumber() << '@' << PEnd
1173 << " doesn't have PHIKill, but Valno #" << VNI->id
1174 << " is PHIDef and defined at the beginning of BB#"
1175 << MFI->getNumber() << '@' << LiveInts->getMBBStartIdx(MFI)
1176 << " in " << LI << '\n';
1177 }
1178 continue;
1179 }
1180
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001181 if (!PVNI) {
1182 report("Register not marked live out of predecessor", *PI);
1183 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1184 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live at "
1185 << PEnd << " in " << LI << '\n';
1186 continue;
1187 }
1188
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001189 if (PVNI != VNI) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001190 report("Different value live out of predecessor", *PI);
1191 *OS << "Valno #" << PVNI->id << " live out of BB#"
1192 << (*PI)->getNumber() << '@' << PEnd
1193 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1194 << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n';
1195 }
1196 }
1197 if (&*MFI == EndMBB)
1198 break;
1199 ++MFI;
1200 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001201 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001202
1203 // Check the LI only has one connected component.
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001204 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1205 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1206 unsigned NumComp = ConEQ.Classify(&LI);
1207 if (NumComp > 1) {
1208 report("Multiple connected components in live interval", MF);
1209 *OS << NumComp << " components in " << LI << '\n';
Jakob Stoklund Olesencb367772010-10-29 00:40:57 +00001210 for (unsigned comp = 0; comp != NumComp; ++comp) {
1211 *OS << comp << ": valnos";
1212 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1213 E = LI.vni_end(); I!=E; ++I)
1214 if (comp == ConEQ.getEqClass(*I))
1215 *OS << ' ' << (*I)->id;
1216 *OS << '\n';
1217 }
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001218 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001219 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001220 }
1221}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001222