blob: 4a766b328c5d3f7906d348023b3ad240ff4c057d [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
Craig Topper96601ca2012-08-22 06:07:19 +000026#include "llvm/BasicBlock.h"
Bill Wendlingd29052b2011-05-04 22:54:05 +000027#include "llvm/Instructions.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 Olesen30e98a02012-02-29 00:33:41 +000031#include "llvm/CodeGen/MachineInstrBundle.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +000033#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman2dbc4c82009-10-07 17:36:00 +000034#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
36#include "llvm/CodeGen/Passes.h"
Bill Wendlingd29052b2011-05-04 22:54:05 +000037#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000038#include "llvm/Target/TargetMachine.h"
39#include "llvm/Target/TargetRegisterInfo.h"
40#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000041#include "llvm/ADT/DenseSet.h"
42#include "llvm/ADT/SetOperations.h"
43#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000044#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000045#include "llvm/Support/ErrorHandling.h"
46#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000047using namespace llvm;
48
49namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000050 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000051
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000052 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000053 PASS(pass),
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000054 Banner(b),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000055 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000056 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000057
58 bool runOnMachineFunction(MachineFunction &MF);
59
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000060 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000061 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000062 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000063 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000064 const MachineFunction *MF;
65 const TargetMachine *TM;
Evan Cheng15993f82011-06-27 21:26:13 +000066 const TargetInstrInfo *TII;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000067 const TargetRegisterInfo *TRI;
68 const MachineRegisterInfo *MRI;
69
70 unsigned foundErrors;
71
72 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000073 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000074 typedef DenseSet<unsigned> RegSet;
75 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +000076 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000077
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000078 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +000079 BlockSet FunctionBlocks;
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000080
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000081 BitVector regsReserved;
Lang Hames03698de2012-02-14 19:17:48 +000082 BitVector regsAllocatable;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000083 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000084 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000085 RegMaskVector regMasks;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000086 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000087
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000088 SlotIndex lastIndex;
89
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000090 // Add Reg and any sub-registers to RV
91 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
92 RV.push_back(Reg);
93 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +000094 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
95 RV.push_back(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000096 }
97
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000098 struct BBInfo {
99 // Is this MBB reachable from the MF entry point?
100 bool reachable;
101
102 // Vregs that must be live in because they are used without being
103 // defined. Map value is the user.
104 RegMap vregsLiveIn;
105
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000106 // Regs killed in MBB. They may be defined again, and will then be in both
107 // regsKilled and regsLiveOut.
108 RegSet regsKilled;
109
110 // Regs defined in MBB and live out. Note that vregs passing through may
111 // be live out without being mentioned here.
112 RegSet regsLiveOut;
113
114 // Vregs that pass through MBB untouched. This set is disjoint from
115 // regsKilled and regsLiveOut.
116 RegSet vregsPassed;
117
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000118 // Vregs that must pass through MBB because they are needed by a successor
119 // block. This set is disjoint from regsLiveOut.
120 RegSet vregsRequired;
121
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000122 // Set versions of block's predecessor and successor lists.
123 BlockSet Preds, Succs;
124
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000125 BBInfo() : reachable(false) {}
126
127 // Add register to vregsPassed if it belongs there. Return true if
128 // anything changed.
129 bool addPassed(unsigned Reg) {
130 if (!TargetRegisterInfo::isVirtualRegister(Reg))
131 return false;
132 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
133 return false;
134 return vregsPassed.insert(Reg).second;
135 }
136
137 // Same for a full set.
138 bool addPassed(const RegSet &RS) {
139 bool changed = false;
140 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
141 if (addPassed(*I))
142 changed = true;
143 return changed;
144 }
145
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000146 // Add register to vregsRequired if it belongs there. Return true if
147 // anything changed.
148 bool addRequired(unsigned Reg) {
149 if (!TargetRegisterInfo::isVirtualRegister(Reg))
150 return false;
151 if (regsLiveOut.count(Reg))
152 return false;
153 return vregsRequired.insert(Reg).second;
154 }
155
156 // Same for a full set.
157 bool addRequired(const RegSet &RS) {
158 bool changed = false;
159 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
160 if (addRequired(*I))
161 changed = true;
162 return changed;
163 }
164
165 // Same for a full map.
166 bool addRequired(const RegMap &RM) {
167 bool changed = false;
168 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
169 if (addRequired(I->first))
170 changed = true;
171 return changed;
172 }
173
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000174 // Live-out registers are either in regsLiveOut or vregsPassed.
175 bool isLiveOut(unsigned Reg) const {
176 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
177 }
178 };
179
180 // Extra register info per MBB.
181 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
182
183 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000184 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000185 }
186
Lang Hames03698de2012-02-14 19:17:48 +0000187 bool isAllocatable(unsigned Reg) {
188 return Reg < regsAllocatable.size() && regsAllocatable.test(Reg);
189 }
190
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000191 // Analysis information if available
192 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000193 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000194 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000195 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000196
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000197 void visitMachineFunctionBefore();
198 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000199 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000200 void visitMachineInstrBefore(const MachineInstr *MI);
201 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
202 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000203 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000204 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
205 void visitMachineFunctionAfter();
206
207 void report(const char *msg, const MachineFunction *MF);
208 void report(const char *msg, const MachineBasicBlock *MBB);
209 void report(const char *msg, const MachineInstr *MI);
210 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000211 void report(const char *msg, const MachineFunction *MF,
212 const LiveInterval &LI);
213 void report(const char *msg, const MachineBasicBlock *MBB,
214 const LiveInterval &LI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000215
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000216 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000217 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000218 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000219 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000220
221 void calcRegsRequired();
222 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000223 void verifyLiveIntervals();
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +0000224 void verifyLiveInterval(const LiveInterval&);
225 void verifyLiveIntervalValue(const LiveInterval&, VNInfo*);
226 void verifyLiveIntervalSegment(const LiveInterval&,
227 LiveInterval::const_iterator);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000228 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000229
230 struct MachineVerifierPass : public MachineFunctionPass {
231 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000232 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000233
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000234 MachineVerifierPass(const char *b = 0)
235 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000236 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
237 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000238
239 void getAnalysisUsage(AnalysisUsage &AU) const {
240 AU.setPreservesAll();
241 MachineFunctionPass::getAnalysisUsage(AU);
242 }
243
244 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000245 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000246 return false;
247 }
248 };
249
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000250}
251
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000252char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000253INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000254 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000255
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000256FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
257 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000258}
259
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000260void MachineFunction::verify(Pass *p, const char *Banner) const {
261 MachineVerifier(p, Banner)
262 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000263}
264
Chris Lattner17e9edc2009-08-23 02:51:22 +0000265bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
266 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000267 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000268 std::string ErrorInfo;
269 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
270 raw_fd_ostream::F_Append);
271 if (!ErrorInfo.empty()) {
272 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
273 exit(1);
274 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000275
Chris Lattner17e9edc2009-08-23 02:51:22 +0000276 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000277 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000278 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000279 }
280
281 foundErrors = 0;
282
283 this->MF = &MF;
284 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000285 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000286 TRI = TM->getRegisterInfo();
287 MRI = &MF.getRegInfo();
288
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000289 LiveVars = NULL;
290 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000291 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000292 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000293 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000294 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000295 // We don't want to verify LiveVariables if LiveIntervals is available.
296 if (!LiveInts)
297 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000298 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000299 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000300 }
301
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000302 visitMachineFunctionBefore();
303 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
304 MFI!=MFE; ++MFI) {
305 visitMachineBasicBlockBefore(MFI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000306 // Keep track of the current bundle header.
307 const MachineInstr *CurBundle = 0;
Evan Chengddfd1372011-12-14 02:11:42 +0000308 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
309 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000310 if (MBBI->getParent() != MFI) {
311 report("Bad instruction parent pointer", MFI);
312 *OS << "Instruction: " << *MBBI;
313 continue;
314 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000315 // Is this a bundle header?
316 if (!MBBI->isInsideBundle()) {
317 if (CurBundle)
318 visitMachineBundleAfter(CurBundle);
319 CurBundle = MBBI;
320 visitMachineBundleBefore(CurBundle);
321 } else if (!CurBundle)
322 report("No bundle header", MBBI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000323 visitMachineInstrBefore(MBBI);
324 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
325 visitMachineOperand(&MBBI->getOperand(I), I);
326 visitMachineInstrAfter(MBBI);
327 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000328 if (CurBundle)
329 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000330 visitMachineBasicBlockAfter(MFI);
331 }
332 visitMachineFunctionAfter();
333
Chris Lattner17e9edc2009-08-23 02:51:22 +0000334 if (OutFile)
335 delete OutFile;
336 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000337 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000338
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000339 // Clean up.
340 regsLive.clear();
341 regsDefined.clear();
342 regsDead.clear();
343 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000344 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000345 regsLiveInButUnused.clear();
346 MBBInfoMap.clear();
347
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000348 return false; // no changes
349}
350
Chris Lattner372fefe2009-08-23 01:03:30 +0000351void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000352 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000353 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000354 if (!foundErrors++) {
355 if (Banner)
356 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000357 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000358 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000359 *OS << "*** Bad machine code: " << msg << " ***\n"
Craig Topper96601ca2012-08-22 06:07:19 +0000360 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000361}
362
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000363void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000364 assert(MBB);
365 report(msg, MBB->getParent());
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000366 *OS << "- basic block: BB#" << MBB->getNumber()
367 << ' ' << MBB->getName()
368 << " (" << (void*)MBB << ')';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000369 if (Indexes)
370 *OS << " [" << Indexes->getMBBStartIdx(MBB)
371 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
372 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000373}
374
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000375void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000376 assert(MI);
377 report(msg, MI->getParent());
378 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000379 if (Indexes && Indexes->hasIndex(MI))
380 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000381 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000382}
383
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000384void MachineVerifier::report(const char *msg,
385 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000386 assert(MO);
387 report(msg, MO->getParent());
388 *OS << "- operand " << MONum << ": ";
389 MO->print(*OS, TM);
390 *OS << "\n";
391}
392
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000393void MachineVerifier::report(const char *msg, const MachineFunction *MF,
394 const LiveInterval &LI) {
395 report(msg, MF);
396 *OS << "- interval: ";
397 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
398 *OS << PrintReg(LI.reg, TRI);
399 else
400 *OS << PrintRegUnit(LI.reg, TRI);
401 *OS << ' ' << LI << '\n';
402}
403
404void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
405 const LiveInterval &LI) {
406 report(msg, MBB);
407 *OS << "- interval: ";
408 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
409 *OS << PrintReg(LI.reg, TRI);
410 else
411 *OS << PrintRegUnit(LI.reg, TRI);
412 *OS << ' ' << LI << '\n';
413}
414
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000415void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000416 BBInfo &MInfo = MBBInfoMap[MBB];
417 if (!MInfo.reachable) {
418 MInfo.reachable = true;
419 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
420 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
421 markReachable(*SuI);
422 }
423}
424
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000425void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000426 lastIndex = SlotIndex();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000427 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000428
429 // A sub-register of a reserved register is also reserved
430 for (int Reg = regsReserved.find_first(); Reg>=0;
431 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000432 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000433 // FIXME: This should probably be:
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000434 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
435 regsReserved.set(*SubRegs);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000436 }
437 }
Lang Hames03698de2012-02-14 19:17:48 +0000438
439 regsAllocatable = TRI->getAllocatableSet(*MF);
440
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000441 markReachable(&MF->front());
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000442
443 // Build a set of the basic blocks in the function.
444 FunctionBlocks.clear();
445 for (MachineFunction::const_iterator
446 I = MF->begin(), E = MF->end(); I != E; ++I) {
447 FunctionBlocks.insert(I);
448 BBInfo &MInfo = MBBInfoMap[I];
449
450 MInfo.Preds.insert(I->pred_begin(), I->pred_end());
451 if (MInfo.Preds.size() != I->pred_size())
452 report("MBB has duplicate entries in its predecessor list.", I);
453
454 MInfo.Succs.insert(I->succ_begin(), I->succ_end());
455 if (MInfo.Succs.size() != I->succ_size())
456 report("MBB has duplicate entries in its successor list.", I);
457 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000458}
459
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000460// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000461static bool matchPair(MachineBasicBlock::const_succ_iterator i,
462 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000463 if (*i == a)
464 return *++i == b;
465 if (*i == b)
466 return *++i == a;
467 return false;
468}
469
470void
471MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000472 FirstTerminator = 0;
473
Lang Hames03698de2012-02-14 19:17:48 +0000474 if (MRI->isSSA()) {
475 // If this block has allocatable physical registers live-in, check that
476 // it is an entry block or landing pad.
477 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
478 LE = MBB->livein_end();
479 LI != LE; ++LI) {
480 unsigned reg = *LI;
481 if (isAllocatable(reg) && !MBB->isLandingPad() &&
482 MBB != MBB->getParent()->begin()) {
483 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
484 }
485 }
486 }
487
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000488 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000489 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000490 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000491 E = MBB->succ_end(); I != E; ++I) {
492 if ((*I)->isLandingPad())
493 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000494 if (!FunctionBlocks.count(*I))
495 report("MBB has successor that isn't part of the function.", MBB);
496 if (!MBBInfoMap[*I].Preds.count(MBB)) {
497 report("Inconsistent CFG", MBB);
498 *OS << "MBB is not in the predecessor list of the successor BB#"
499 << (*I)->getNumber() << ".\n";
500 }
501 }
502
503 // Check the predecessor list.
504 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
505 E = MBB->pred_end(); I != E; ++I) {
506 if (!FunctionBlocks.count(*I))
507 report("MBB has predecessor that isn't part of the function.", MBB);
508 if (!MBBInfoMap[*I].Succs.count(MBB)) {
509 report("Inconsistent CFG", MBB);
510 *OS << "MBB is not in the successor list of the predecessor BB#"
511 << (*I)->getNumber() << ".\n";
512 }
Cameron Zwarich2100d212010-12-20 04:19:48 +0000513 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000514
515 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
516 const BasicBlock *BB = MBB->getBasicBlock();
517 if (LandingPadSuccs.size() > 1 &&
518 !(AsmInfo &&
519 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
520 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000521 report("MBB has more than one landing pad successor", MBB);
522
Dan Gohman27920592009-08-27 02:43:49 +0000523 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
524 MachineBasicBlock *TBB = 0, *FBB = 0;
525 SmallVector<MachineOperand, 4> Cond;
526 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
527 TBB, FBB, Cond)) {
528 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
529 // check whether its answers match up with reality.
530 if (!TBB && !FBB) {
531 // Block falls through to its successor.
532 MachineFunction::const_iterator MBBI = MBB;
533 ++MBBI;
534 if (MBBI == MF->end()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000535 // It's possible that the block legitimately ends with a noreturn
536 // call or an unreachable, in which case it won't actually fall
537 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000538 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000539 // It's possible that the block legitimately ends with a noreturn
540 // call or an unreachable, in which case it won't actuall fall
541 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000542 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000543 report("MBB exits via unconditional fall-through but doesn't have "
544 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000545 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000546 report("MBB exits via unconditional fall-through but its successor "
547 "differs from its CFG successor!", MBB);
548 }
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000549 if (!MBB->empty() && getBundleStart(&MBB->back())->isBarrier() &&
550 !TII->isPredicated(getBundleStart(&MBB->back()))) {
Dan Gohman27920592009-08-27 02:43:49 +0000551 report("MBB exits via unconditional fall-through but ends with a "
552 "barrier instruction!", MBB);
553 }
554 if (!Cond.empty()) {
555 report("MBB exits via unconditional fall-through but has a condition!",
556 MBB);
557 }
558 } else if (TBB && !FBB && Cond.empty()) {
559 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000560 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000561 report("MBB exits via unconditional branch but doesn't have "
562 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000563 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000564 report("MBB exits via unconditional branch but the CFG "
565 "successor doesn't match the actual successor!", MBB);
566 }
567 if (MBB->empty()) {
568 report("MBB exits via unconditional branch but doesn't contain "
569 "any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000570 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000571 report("MBB exits via unconditional branch but doesn't end with a "
572 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000573 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000574 report("MBB exits via unconditional branch but the branch isn't a "
575 "terminator instruction!", MBB);
576 }
577 } else if (TBB && !FBB && !Cond.empty()) {
578 // Block conditionally branches somewhere, otherwise falls through.
579 MachineFunction::const_iterator MBBI = MBB;
580 ++MBBI;
581 if (MBBI == MF->end()) {
582 report("MBB conditionally falls through out of function!", MBB);
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000583 } if (MBB->succ_size() == 1) {
584 // A conditional branch with only one successor is weird, but allowed.
585 if (&*MBBI != TBB)
586 report("MBB exits via conditional branch/fall-through but only has "
587 "one CFG successor!", MBB);
588 else if (TBB != *MBB->succ_begin())
589 report("MBB exits via conditional branch/fall-through but the CFG "
590 "successor don't match the actual successor!", MBB);
591 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000592 report("MBB exits via conditional branch/fall-through but doesn't have "
593 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000594 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000595 report("MBB exits via conditional branch/fall-through but the CFG "
596 "successors don't match the actual successors!", MBB);
597 }
598 if (MBB->empty()) {
599 report("MBB exits via conditional branch/fall-through but doesn't "
600 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000601 } else if (getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000602 report("MBB exits via conditional branch/fall-through but ends with a "
603 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000604 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000605 report("MBB exits via conditional branch/fall-through but the branch "
606 "isn't a terminator instruction!", MBB);
607 }
608 } else if (TBB && FBB) {
609 // Block conditionally branches somewhere, otherwise branches
610 // somewhere else.
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000611 if (MBB->succ_size() == 1) {
612 // A conditional branch with only one successor is weird, but allowed.
613 if (FBB != TBB)
614 report("MBB exits via conditional branch/branch through but only has "
615 "one CFG successor!", MBB);
616 else if (TBB != *MBB->succ_begin())
617 report("MBB exits via conditional branch/branch through but the CFG "
618 "successor don't match the actual successor!", MBB);
619 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000620 report("MBB exits via conditional branch/branch but doesn't have "
621 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000622 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000623 report("MBB exits via conditional branch/branch but the CFG "
624 "successors don't match the actual successors!", MBB);
625 }
626 if (MBB->empty()) {
627 report("MBB exits via conditional branch/branch but doesn't "
628 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000629 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000630 report("MBB exits via conditional branch/branch but doesn't end with a "
631 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000632 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000633 report("MBB exits via conditional branch/branch but the branch "
634 "isn't a terminator instruction!", MBB);
635 }
636 if (Cond.empty()) {
637 report("MBB exits via conditinal branch/branch but there's no "
638 "condition!", MBB);
639 }
640 } else {
641 report("AnalyzeBranch returned invalid data!", MBB);
642 }
643 }
644
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000645 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000646 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000647 E = MBB->livein_end(); I != E; ++I) {
648 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
649 report("MBB live-in list contains non-physical register", MBB);
650 continue;
651 }
652 regsLive.insert(*I);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000653 for (MCSubRegIterator SubRegs(*I, TRI); SubRegs.isValid(); ++SubRegs)
654 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000655 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000656 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000657
658 const MachineFrameInfo *MFI = MF->getFrameInfo();
659 assert(MFI && "Function has no frame info");
660 BitVector PR = MFI->getPristineRegs(MBB);
661 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
662 regsLive.insert(I);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000663 for (MCSubRegIterator SubRegs(I, TRI); SubRegs.isValid(); ++SubRegs)
664 regsLive.insert(*SubRegs);
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000665 }
666
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000667 regsKilled.clear();
668 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000669
670 if (Indexes)
671 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000672}
673
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000674// This function gets called for all bundle headers, including normal
675// stand-alone unbundled instructions.
676void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
677 if (Indexes && Indexes->hasIndex(MI)) {
678 SlotIndex idx = Indexes->getInstructionIndex(MI);
679 if (!(idx > lastIndex)) {
680 report("Instruction index out of order", MI);
681 *OS << "Last instruction was at " << lastIndex << '\n';
682 }
683 lastIndex = idx;
684 }
Pete Cooper83569cb2012-06-07 17:41:39 +0000685
686 // Ensure non-terminators don't follow terminators.
687 // Ignore predicated terminators formed by if conversion.
688 // FIXME: If conversion shouldn't need to violate this rule.
689 if (MI->isTerminator() && !TII->isPredicated(MI)) {
690 if (!FirstTerminator)
691 FirstTerminator = MI;
692 } else if (FirstTerminator) {
693 report("Non-terminator instruction after the first terminator", MI);
694 *OS << "First terminator was:\t" << *FirstTerminator;
695 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000696}
697
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000698void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000699 const MCInstrDesc &MCID = MI->getDesc();
700 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000701 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000702 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000703 << MI->getNumExplicitOperands() << " given.\n";
704 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000705
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000706 // Check the tied operands.
707 SmallVector<unsigned, 4> TiedDefs;
708 SmallVector<unsigned, 4> TiedUses;
709 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
710 const MachineOperand &MO = MI->getOperand(i);
711 if (!MO.isReg() || !MO.isTied())
712 continue;
713 if (MO.isDef()) {
714 TiedDefs.push_back(i);
715 continue;
716 }
717 TiedUses.push_back(i);
718 if (TiedDefs.size() < TiedUses.size()) {
719 report("No tied def for tied use", &MO, i);
720 break;
721 }
722 if (i >= MCID.getNumOperands())
723 continue;
724 int DefIdx = MCID.getOperandConstraint(i, MCOI::TIED_TO);
725 if (unsigned(DefIdx) != TiedDefs[TiedUses.size() - 1]) {
726 report("Tied def doesn't match MCInstrDesc", &MO, i);
727 *OS << "Descriptor says tied def should be operand " << DefIdx << ".\n";
728 }
729 }
730 if (TiedDefs.size() > TiedUses.size()) {
731 unsigned i = TiedDefs[TiedUses.size() - 1];
732 report("No tied use for tied def", &MI->getOperand(i), i);
733 }
734
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000735 // Check the MachineMemOperands for basic consistency.
736 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
737 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000738 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000739 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000740 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000741 report("Missing mayStore flag", MI);
742 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000743
744 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000745 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000746 if (LiveInts) {
747 bool mapped = !LiveInts->isNotInMIMap(MI);
748 if (MI->isDebugValue()) {
749 if (mapped)
750 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000751 } else if (MI->isInsideBundle()) {
752 if (mapped)
753 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000754 } else {
755 if (!mapped)
756 report("Missing slot index", MI);
757 }
758 }
759
Andrew Trick3be654f2011-09-21 02:20:46 +0000760 StringRef ErrorInfo;
761 if (!TII->verifyInstruction(MI, ErrorInfo))
762 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000763}
764
765void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000766MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000767 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000768 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000769
Evan Chenge837dea2011-06-28 19:10:37 +0000770 // The first MCID.NumDefs operands must be explicit register defines
771 if (MONum < MCID.getNumDefs()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000772 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000773 if (!MO->isReg())
774 report("Explicit definition must be a register", MO, MONum);
Evan Chengcac58aa2012-05-29 19:40:44 +0000775 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000776 report("Explicit definition marked as use", MO, MONum);
777 else if (MO->isImplicit())
778 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000779 } else if (MONum < MCID.getNumOperands()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000780 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopher113a06c2010-11-17 00:55:36 +0000781 // Don't check if it's the last operand in a variadic instruction. See,
782 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000783 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000784 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000785 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000786 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000787 if (MO->isImplicit())
788 report("Explicit operand marked as implicit", MO, MONum);
789 }
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000790
791 if (MCID.getOperandConstraint(MONum, MCOI::TIED_TO) != -1) {
792 if (!MO->isReg())
793 report("Tied use must be a register", MO, MONum);
794 else if (!MO->isTied())
795 report("Operand should be tied", MO, MONum);
796 } else if (MO->isReg() && MO->isTied())
797 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000798 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000799 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000800 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000801 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000802 }
803
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000804 switch (MO->getType()) {
805 case MachineOperand::MO_Register: {
806 const unsigned Reg = MO->getReg();
807 if (!Reg)
808 return;
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000809 if (MRI->tracksLiveness() && !MI->isDebugValue())
810 checkLiveness(MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000811
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000812 // Verify two-address constraints after leaving SSA form.
813 unsigned DefIdx;
814 if (!MRI->isSSA() && MO->isUse() &&
815 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
816 Reg != MI->getOperand(DefIdx).getReg())
817 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000818
819 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000820 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000821 unsigned SubIdx = MO->getSubReg();
822
823 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000824 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000825 report("Illegal subregister index for physical register", MO, MONum);
826 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000827 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000828 if (const TargetRegisterClass *DRC =
829 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000830 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000831 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000832 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000833 << DRC->getName() << " register.\n";
834 }
835 }
836 } else {
837 // Virtual register.
838 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
839 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000840 const TargetRegisterClass *SRC =
841 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000842 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000843 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000844 *OS << "Register class " << RC->getName()
845 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000846 return;
847 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000848 if (RC != SRC) {
849 report("Invalid register class for subregister index", MO, MONum);
850 *OS << "Register class " << RC->getName()
851 << " does not fully support subreg index " << SubIdx << "\n";
852 return;
853 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000854 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000855 if (const TargetRegisterClass *DRC =
856 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000857 if (SubIdx) {
858 const TargetRegisterClass *SuperRC =
859 TRI->getLargestLegalSuperClass(RC);
860 if (!SuperRC) {
861 report("No largest legal super class exists.", MO, MONum);
862 return;
863 }
864 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
865 if (!DRC) {
866 report("No matching super-reg register class.", MO, MONum);
867 return;
868 }
869 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000870 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000871 report("Illegal virtual register for instruction", MO, MONum);
872 *OS << "Expected a " << DRC->getName() << " register, but got a "
873 << RC->getName() << " register\n";
874 }
875 }
876 }
877 }
878 break;
879 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000880
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000881 case MachineOperand::MO_RegisterMask:
882 regMasks.push_back(MO->getRegMask());
883 break;
884
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000885 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000886 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
887 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000888 break;
889
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000890 case MachineOperand::MO_FrameIndex:
891 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
892 LiveInts && !LiveInts->isNotInMIMap(MI)) {
893 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
894 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000895 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000896 report("Instruction loads from dead spill slot", MO, MONum);
897 *OS << "Live stack: " << LI << '\n';
898 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000899 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000900 report("Instruction stores to dead spill slot", MO, MONum);
901 *OS << "Live stack: " << LI << '\n';
902 }
903 }
904 break;
905
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000906 default:
907 break;
908 }
909}
910
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000911void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
912 const MachineInstr *MI = MO->getParent();
913 const unsigned Reg = MO->getReg();
914
915 // Both use and def operands can read a register.
916 if (MO->readsReg()) {
917 regsLiveInButUnused.erase(Reg);
918
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000919 if (MO->isKill())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000920 addRegWithSubRegs(regsKilled, Reg);
921
922 // Check that LiveVars knows this kill.
923 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
924 MO->isKill()) {
925 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
926 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
927 report("Kill missing from LiveVariables", MO, MONum);
928 }
929
930 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +0000931 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
932 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
933 // Check the cached regunit intervals.
934 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
935 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
936 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(*Units)) {
937 LiveRangeQuery LRQ(*LI, UseIdx);
938 if (!LRQ.valueIn()) {
939 report("No live range at use", MO, MONum);
940 *OS << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
941 << ' ' << *LI << '\n';
942 }
943 if (MO->isKill() && !LRQ.isKill()) {
944 report("Live range continues after kill flag", MO, MONum);
945 *OS << PrintRegUnit(*Units, TRI) << ' ' << *LI << '\n';
946 }
947 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000948 }
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +0000949 }
950
951 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
952 if (LiveInts->hasInterval(Reg)) {
953 // This is a virtual register interval.
954 const LiveInterval &LI = LiveInts->getInterval(Reg);
955 LiveRangeQuery LRQ(LI, UseIdx);
956 if (!LRQ.valueIn()) {
957 report("No live range at use", MO, MONum);
958 *OS << UseIdx << " is not live in " << LI << '\n';
959 }
960 // Check for extra kill flags.
961 // Note that we allow missing kill flags for now.
962 if (MO->isKill() && !LRQ.isKill()) {
963 report("Live range continues after kill flag", MO, MONum);
964 *OS << "Live range: " << LI << '\n';
965 }
966 } else {
967 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000968 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000969 }
970 }
971
972 // Use of a dead register.
973 if (!regsLive.count(Reg)) {
974 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
975 // Reserved registers may be used even when 'dead'.
976 if (!isReserved(Reg))
977 report("Using an undefined physical register", MO, MONum);
Pete Cooperb97c57a2012-07-19 23:40:38 +0000978 } else if (MRI->def_empty(Reg)) {
979 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000980 } else {
981 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
982 // We don't know which virtual registers are live in, so only complain
983 // if vreg was killed in this MBB. Otherwise keep track of vregs that
984 // must be live in. PHI instructions are handled separately.
985 if (MInfo.regsKilled.count(Reg))
986 report("Using a killed virtual register", MO, MONum);
987 else if (!MI->isPHI())
988 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
989 }
990 }
991 }
992
993 if (MO->isDef()) {
994 // Register defined.
995 // TODO: verify that earlyclobber ops are not used.
996 if (MO->isDead())
997 addRegWithSubRegs(regsDead, Reg);
998 else
999 addRegWithSubRegs(regsDefined, Reg);
1000
1001 // Verify SSA form.
1002 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
1003 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
1004 report("Multiple virtual register defs in SSA form", MO, MONum);
1005
1006 // Check LiveInts for a live range, but only for virtual registers.
1007 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1008 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001009 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1010 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001011 if (LiveInts->hasInterval(Reg)) {
1012 const LiveInterval &LI = LiveInts->getInterval(Reg);
1013 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1014 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001015 if (VNI->def != DefIdx) {
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001016 report("Inconsistent valno->def", MO, MONum);
1017 *OS << "Valno " << VNI->id << " is not defined at "
1018 << DefIdx << " in " << LI << '\n';
1019 }
1020 } else {
1021 report("No live range at def", MO, MONum);
1022 *OS << DefIdx << " is not live in " << LI << '\n';
1023 }
1024 } else {
1025 report("Virtual register has no Live interval", MO, MONum);
1026 }
1027 }
1028 }
1029}
1030
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001031void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +00001032}
1033
1034// This function gets called after visiting all instructions in a bundle. The
1035// argument points to the bundle header.
1036// Normal stand-alone instructions are also considered 'bundles', and this
1037// function is called for all of them.
1038void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001039 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1040 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001041 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +00001042 // Kill any masked registers.
1043 while (!regMasks.empty()) {
1044 const uint32_t *Mask = regMasks.pop_back_val();
1045 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1046 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1047 MachineOperand::clobbersPhysReg(Mask, *I))
1048 regsDead.push_back(*I);
1049 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001050 set_subtract(regsLive, regsDead); regsDead.clear();
1051 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001052}
1053
1054void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001055MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001056 MBBInfoMap[MBB].regsLiveOut = regsLive;
1057 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +00001058
1059 if (Indexes) {
1060 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1061 if (!(stop > lastIndex)) {
1062 report("Block ends before last instruction index", MBB);
1063 *OS << "Block ends at " << stop
1064 << " last instruction was at " << lastIndex << '\n';
1065 }
1066 lastIndex = stop;
1067 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001068}
1069
1070// Calculate the largest possible vregsPassed sets. These are the registers that
1071// can pass through an MBB live, but may not be live every time. It is assumed
1072// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001073void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001074 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1075 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001076 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001077 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1078 MFI != MFE; ++MFI) {
1079 const MachineBasicBlock &MBB(*MFI);
1080 BBInfo &MInfo = MBBInfoMap[&MBB];
1081 if (!MInfo.reachable)
1082 continue;
1083 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1084 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1085 BBInfo &SInfo = MBBInfoMap[*SuI];
1086 if (SInfo.addPassed(MInfo.regsLiveOut))
1087 todo.insert(*SuI);
1088 }
1089 }
1090
1091 // Iteratively push vregsPassed to successors. This will converge to the same
1092 // final state regardless of DenseSet iteration order.
1093 while (!todo.empty()) {
1094 const MachineBasicBlock *MBB = *todo.begin();
1095 todo.erase(MBB);
1096 BBInfo &MInfo = MBBInfoMap[MBB];
1097 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1098 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1099 if (*SuI == MBB)
1100 continue;
1101 BBInfo &SInfo = MBBInfoMap[*SuI];
1102 if (SInfo.addPassed(MInfo.vregsPassed))
1103 todo.insert(*SuI);
1104 }
1105 }
1106}
1107
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001108// Calculate the set of virtual registers that must be passed through each basic
1109// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001110// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001111void MachineVerifier::calcRegsRequired() {
1112 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001113 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001114 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1115 MFI != MFE; ++MFI) {
1116 const MachineBasicBlock &MBB(*MFI);
1117 BBInfo &MInfo = MBBInfoMap[&MBB];
1118 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1119 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1120 BBInfo &PInfo = MBBInfoMap[*PrI];
1121 if (PInfo.addRequired(MInfo.vregsLiveIn))
1122 todo.insert(*PrI);
1123 }
1124 }
1125
1126 // Iteratively push vregsRequired to predecessors. This will converge to the
1127 // same final state regardless of DenseSet iteration order.
1128 while (!todo.empty()) {
1129 const MachineBasicBlock *MBB = *todo.begin();
1130 todo.erase(MBB);
1131 BBInfo &MInfo = MBBInfoMap[MBB];
1132 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1133 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1134 if (*PrI == MBB)
1135 continue;
1136 BBInfo &SInfo = MBBInfoMap[*PrI];
1137 if (SInfo.addRequired(MInfo.vregsRequired))
1138 todo.insert(*PrI);
1139 }
1140 }
1141}
1142
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001143// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001144// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001145void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001146 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001147 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +00001148 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001149 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001150
1151 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
1152 unsigned Reg = BBI->getOperand(i).getReg();
1153 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
1154 if (!Pre->isSuccessor(MBB))
1155 continue;
1156 seen.insert(Pre);
1157 BBInfo &PrInfo = MBBInfoMap[Pre];
1158 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1159 report("PHI operand is not live-out from predecessor",
1160 &BBI->getOperand(i), i);
1161 }
1162
1163 // Did we see all predecessors?
1164 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1165 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1166 if (!seen.count(*PrI)) {
1167 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +00001168 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001169 << " is a predecessor according to the CFG.\n";
1170 }
1171 }
1172 }
1173}
1174
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001175void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001176 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001177
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001178 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1179 MFI != MFE; ++MFI) {
1180 BBInfo &MInfo = MBBInfoMap[MFI];
1181
1182 // Skip unreachable MBBs.
1183 if (!MInfo.reachable)
1184 continue;
1185
1186 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001187 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001188
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001189 // Now check liveness info if available
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001190 calcRegsRequired();
1191
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001192 // Check for killed virtual registers that should be live out.
1193 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1194 MFI != MFE; ++MFI) {
1195 BBInfo &MInfo = MBBInfoMap[MFI];
1196 for (RegSet::iterator
1197 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1198 ++I)
1199 if (MInfo.regsKilled.count(*I)) {
Bill Wendling96cb1122012-07-19 00:04:14 +00001200 report("Virtual register killed in block, but needed live out.", MFI);
1201 *OS << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001202 << " is used after the block.\n";
1203 }
1204 }
1205
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001206 if (!MF->empty()) {
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001207 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1208 for (RegSet::iterator
1209 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesenff0275e2012-03-10 00:44:11 +00001210 ++I)
1211 report("Virtual register def doesn't dominate all uses.",
1212 MRI->getVRegDef(*I));
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001213 }
1214
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001215 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001216 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001217 if (LiveInts)
1218 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001219}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001220
1221void MachineVerifier::verifyLiveVariables() {
1222 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001223 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1224 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001225 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1226 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1227 MFI != MFE; ++MFI) {
1228 BBInfo &MInfo = MBBInfoMap[MFI];
1229
1230 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1231 if (MInfo.vregsRequired.count(Reg)) {
1232 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1233 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001234 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001235 << " must be live through the block.\n";
1236 }
1237 } else {
1238 if (VI.AliveBlocks.test(MFI->getNumber())) {
1239 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001240 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001241 << " is not needed live through the block.\n";
1242 }
1243 }
1244 }
1245 }
1246}
1247
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001248void MachineVerifier::verifyLiveIntervals() {
1249 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001250 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1251 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001252
1253 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001254 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001255 continue;
1256
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001257 if (!LiveInts->hasInterval(Reg)) {
1258 report("Missing live interval for virtual register", MF);
1259 *OS << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001260 continue;
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001261 }
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001262
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001263 const LiveInterval &LI = LiveInts->getInterval(Reg);
1264 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001265 verifyLiveInterval(LI);
1266 }
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001267
1268 // Verify all the cached regunit intervals.
1269 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
1270 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(i))
1271 verifyLiveInterval(*LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001272}
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001273
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001274void MachineVerifier::verifyLiveIntervalValue(const LiveInterval &LI,
1275 VNInfo *VNI) {
1276 if (VNI->isUnused())
1277 return;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001278
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001279 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001280
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001281 if (!DefVNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001282 report("Valno not live at def and not marked unused", MF, LI);
1283 *OS << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001284 return;
1285 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001286
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001287 if (DefVNI != VNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001288 report("Live range at def has different valno", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001289 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001290 << " where valno #" << DefVNI->id << " is live\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001291 return;
1292 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001293
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001294 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1295 if (!MBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001296 report("Invalid definition index", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001297 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1298 << " in " << LI << '\n';
1299 return;
1300 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001301
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001302 if (VNI->isPHIDef()) {
1303 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001304 report("PHIDef value is not defined at MBB start", MBB, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001305 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001306 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001307 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001308 return;
1309 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001310
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001311 // Non-PHI def.
1312 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1313 if (!MI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001314 report("No instruction at def index", MBB, LI);
1315 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001316 return;
1317 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001318
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001319 bool hasDef = false;
1320 bool isEarlyClobber = false;
1321 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1322 if (!MOI->isReg() || !MOI->isDef())
1323 continue;
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001324 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001325 if (MOI->getReg() != LI.reg)
1326 continue;
1327 } else {
1328 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001329 !TRI->hasRegUnit(MOI->getReg(), LI.reg))
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001330 continue;
1331 }
1332 hasDef = true;
1333 if (MOI->isEarlyClobber())
1334 isEarlyClobber = true;
1335 }
1336
1337 if (!hasDef) {
1338 report("Defining instruction does not modify register", MI);
1339 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1340 }
1341
1342 // Early clobber defs begin at USE slots, but other defs must begin at
1343 // DEF slots.
1344 if (isEarlyClobber) {
1345 if (!VNI->def.isEarlyClobber()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001346 report("Early clobber def must be at an early-clobber slot", MBB, LI);
1347 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001348 }
1349 } else if (!VNI->def.isRegister()) {
1350 report("Non-PHI, non-early clobber def must be at a register slot",
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001351 MBB, LI);
1352 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001353 }
1354}
1355
1356void
1357MachineVerifier::verifyLiveIntervalSegment(const LiveInterval &LI,
1358 LiveInterval::const_iterator I) {
1359 const VNInfo *VNI = I->valno;
1360 assert(VNI && "Live range has no valno");
1361
1362 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001363 report("Foreign valno in live range", MF, LI);
1364 *OS << *I << " has a bad valno\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001365 }
1366
1367 if (VNI->isUnused()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001368 report("Live range valno is marked unused", MF, LI);
1369 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001370 }
1371
1372 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1373 if (!MBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001374 report("Bad start of live segment, no basic block", MF, LI);
1375 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001376 return;
1377 }
1378 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1379 if (I->start != MBBStartIdx && I->start != VNI->def) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001380 report("Live segment must begin at MBB entry or valno def", MBB, LI);
1381 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001382 }
1383
1384 const MachineBasicBlock *EndMBB =
1385 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1386 if (!EndMBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001387 report("Bad end of live segment, no basic block", MF, LI);
1388 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001389 return;
1390 }
1391
1392 // No more checks for live-out segments.
1393 if (I->end == LiveInts->getMBBEndIdx(EndMBB))
1394 return;
1395
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001396 // RegUnit intervals are allowed dead phis.
1397 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) && VNI->isPHIDef() &&
1398 I->start == VNI->def && I->end == VNI->def.getDeadSlot())
1399 return;
1400
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001401 // The live segment is ending inside EndMBB
1402 const MachineInstr *MI =
1403 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1404 if (!MI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001405 report("Live segment doesn't end at a valid instruction", EndMBB, LI);
1406 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001407 return;
1408 }
1409
1410 // The block slot must refer to a basic block boundary.
1411 if (I->end.isBlock()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001412 report("Live segment ends at B slot of an instruction", EndMBB, LI);
1413 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001414 }
1415
1416 if (I->end.isDead()) {
1417 // Segment ends on the dead slot.
1418 // That means there must be a dead def.
1419 if (!SlotIndex::isSameInstr(I->start, I->end)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001420 report("Live segment ending at dead slot spans instructions", EndMBB, LI);
1421 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001422 }
1423 }
1424
1425 // A live segment can only end at an early-clobber slot if it is being
1426 // redefined by an early-clobber def.
1427 if (I->end.isEarlyClobber()) {
1428 if (I+1 == LI.end() || (I+1)->start != I->end) {
1429 report("Live segment ending at early clobber slot must be "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001430 "redefined by an EC def in the same instruction", EndMBB, LI);
1431 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001432 }
1433 }
1434
1435 // The following checks only apply to virtual registers. Physreg liveness
1436 // is too weird to check.
1437 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1438 // A live range can end with either a redefinition, a kill flag on a
1439 // use, or a dead flag on a def.
1440 bool hasRead = false;
1441 bool hasDeadDef = false;
1442 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1443 if (!MOI->isReg() || MOI->getReg() != LI.reg)
1444 continue;
1445 if (MOI->readsReg())
1446 hasRead = true;
1447 if (MOI->isDef() && MOI->isDead())
1448 hasDeadDef = true;
1449 }
1450
1451 if (I->end.isDead()) {
1452 if (!hasDeadDef) {
1453 report("Instruction doesn't have a dead def operand", MI);
1454 I->print(*OS);
1455 *OS << " in " << LI << '\n';
1456 }
1457 } else {
1458 if (!hasRead) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001459 report("Instruction ending live range doesn't read the register", MI);
1460 *OS << *I << " in " << LI << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001461 }
1462 }
1463 }
1464
1465 // Now check all the basic blocks in this live segment.
1466 MachineFunction::const_iterator MFI = MBB;
1467 // Is this live range the beginning of a non-PHIDef VN?
1468 if (I->start == VNI->def && !VNI->isPHIDef()) {
1469 // Not live-in to any blocks.
1470 if (MBB == EndMBB)
1471 return;
1472 // Skip this block.
1473 ++MFI;
1474 }
1475 for (;;) {
1476 assert(LiveInts->isLiveInToMBB(LI, MFI));
1477 // We don't know how to track physregs into a landing pad.
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001478 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) &&
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001479 MFI->isLandingPad()) {
1480 if (&*MFI == EndMBB)
1481 break;
1482 ++MFI;
1483 continue;
1484 }
1485
1486 // Is VNI a PHI-def in the current block?
1487 bool IsPHI = VNI->isPHIDef() &&
1488 VNI->def == LiveInts->getMBBStartIdx(MFI);
1489
1490 // Check that VNI is live-out of all predecessors.
1491 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1492 PE = MFI->pred_end(); PI != PE; ++PI) {
1493 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1494 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
1495
1496 // All predecessors must have a live-out value.
1497 if (!PVNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001498 report("Register not marked live out of predecessor", *PI, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001499 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1500 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001501 << PEnd << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001502 continue;
1503 }
1504
1505 // Only PHI-defs can take different predecessor values.
1506 if (!IsPHI && PVNI != VNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001507 report("Different value live out of predecessor", *PI, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001508 *OS << "Valno #" << PVNI->id << " live out of BB#"
1509 << (*PI)->getNumber() << '@' << PEnd
1510 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001511 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001512 }
1513 }
1514 if (&*MFI == EndMBB)
1515 break;
1516 ++MFI;
1517 }
1518}
1519
1520void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1521 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1522 I!=E; ++I)
1523 verifyLiveIntervalValue(LI, *I);
1524
1525 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I)
1526 verifyLiveIntervalSegment(LI, I);
1527
1528 // Check the LI only has one connected component.
1529 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1530 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1531 unsigned NumComp = ConEQ.Classify(&LI);
1532 if (NumComp > 1) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001533 report("Multiple connected components in live interval", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001534 for (unsigned comp = 0; comp != NumComp; ++comp) {
1535 *OS << comp << ": valnos";
1536 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1537 E = LI.vni_end(); I!=E; ++I)
1538 if (comp == ConEQ.getEqClass(*I))
1539 *OS << ' ' << (*I)->id;
1540 *OS << '\n';
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001541 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001542 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001543 }
1544}