blob: 959269f85f2f03775081671e769231b9d33f0c31 [file] [log] [blame]
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001//===-- MachineVerifier.cpp - Machine Code Verifier -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Pass to verify generated machine code. The following is checked:
11//
12// Operand counts: All explicit operands must be present.
13//
14// Register classes: All physical and virtual register operands must be
15// compatible with the register class required by the instruction descriptor.
16//
17// Register live intervals: Registers must be defined only once, and must be
18// defined before use.
19//
20// The machine code verifier is enabled from LLVMTargetMachine.cpp with the
21// command-line option -verify-machineinstrs, or by defining the environment
22// variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
23// the verifier errors.
24//===----------------------------------------------------------------------===//
25
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000026#include "llvm/Function.h"
27#include "llvm/CodeGen/LiveVariables.h"
28#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman2dbc4c82009-10-07 17:36:00 +000030#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
32#include "llvm/CodeGen/Passes.h"
33#include "llvm/Target/TargetMachine.h"
34#include "llvm/Target/TargetRegisterInfo.h"
35#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000036#include "llvm/ADT/DenseSet.h"
37#include "llvm/ADT/SetOperations.h"
38#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000039#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000040#include "llvm/Support/ErrorHandling.h"
41#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000042using namespace llvm;
43
44namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000045 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000046
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000047 MachineVerifier(Pass *pass, bool allowDoubleDefs) :
48 PASS(pass),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000049 allowVirtDoubleDefs(allowDoubleDefs),
50 allowPhysDoubleDefs(allowDoubleDefs),
51 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000052 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000053
54 bool runOnMachineFunction(MachineFunction &MF);
55
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000056 Pass *const PASS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000057 const bool allowVirtDoubleDefs;
58 const bool allowPhysDoubleDefs;
59
60 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000061 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000062 const MachineFunction *MF;
63 const TargetMachine *TM;
64 const TargetRegisterInfo *TRI;
65 const MachineRegisterInfo *MRI;
66
67 unsigned foundErrors;
68
69 typedef SmallVector<unsigned, 16> RegVector;
70 typedef DenseSet<unsigned> RegSet;
71 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
72
73 BitVector regsReserved;
74 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000075 RegVector regsDefined, regsDead, regsKilled;
76 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000077
78 // Add Reg and any sub-registers to RV
79 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
80 RV.push_back(Reg);
81 if (TargetRegisterInfo::isPhysicalRegister(Reg))
82 for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
83 RV.push_back(*R);
84 }
85
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000086 struct BBInfo {
87 // Is this MBB reachable from the MF entry point?
88 bool reachable;
89
90 // Vregs that must be live in because they are used without being
91 // defined. Map value is the user.
92 RegMap vregsLiveIn;
93
94 // Vregs that must be dead in because they are defined without being
95 // killed first. Map value is the defining instruction.
96 RegMap vregsDeadIn;
97
98 // Regs killed in MBB. They may be defined again, and will then be in both
99 // regsKilled and regsLiveOut.
100 RegSet regsKilled;
101
102 // Regs defined in MBB and live out. Note that vregs passing through may
103 // be live out without being mentioned here.
104 RegSet regsLiveOut;
105
106 // Vregs that pass through MBB untouched. This set is disjoint from
107 // regsKilled and regsLiveOut.
108 RegSet vregsPassed;
109
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000110 // Vregs that must pass through MBB because they are needed by a successor
111 // block. This set is disjoint from regsLiveOut.
112 RegSet vregsRequired;
113
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000114 BBInfo() : reachable(false) {}
115
116 // Add register to vregsPassed if it belongs there. Return true if
117 // anything changed.
118 bool addPassed(unsigned Reg) {
119 if (!TargetRegisterInfo::isVirtualRegister(Reg))
120 return false;
121 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
122 return false;
123 return vregsPassed.insert(Reg).second;
124 }
125
126 // Same for a full set.
127 bool addPassed(const RegSet &RS) {
128 bool changed = false;
129 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
130 if (addPassed(*I))
131 changed = true;
132 return changed;
133 }
134
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000135 // Add register to vregsRequired if it belongs there. Return true if
136 // anything changed.
137 bool addRequired(unsigned Reg) {
138 if (!TargetRegisterInfo::isVirtualRegister(Reg))
139 return false;
140 if (regsLiveOut.count(Reg))
141 return false;
142 return vregsRequired.insert(Reg).second;
143 }
144
145 // Same for a full set.
146 bool addRequired(const RegSet &RS) {
147 bool changed = false;
148 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
149 if (addRequired(*I))
150 changed = true;
151 return changed;
152 }
153
154 // Same for a full map.
155 bool addRequired(const RegMap &RM) {
156 bool changed = false;
157 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
158 if (addRequired(I->first))
159 changed = true;
160 return changed;
161 }
162
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000163 // Live-out registers are either in regsLiveOut or vregsPassed.
164 bool isLiveOut(unsigned Reg) const {
165 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
166 }
167 };
168
169 // Extra register info per MBB.
170 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
171
172 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000173 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000174 }
175
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000176 // Analysis information if available
177 LiveVariables *LiveVars;
178
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000179 void visitMachineFunctionBefore();
180 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
181 void visitMachineInstrBefore(const MachineInstr *MI);
182 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
183 void visitMachineInstrAfter(const MachineInstr *MI);
184 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
185 void visitMachineFunctionAfter();
186
187 void report(const char *msg, const MachineFunction *MF);
188 void report(const char *msg, const MachineBasicBlock *MBB);
189 void report(const char *msg, const MachineInstr *MI);
190 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
191
192 void markReachable(const MachineBasicBlock *MBB);
193 void calcMaxRegsPassed();
194 void calcMinRegsPassed();
195 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000196
197 void calcRegsRequired();
198 void verifyLiveVariables();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000199 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000200
201 struct MachineVerifierPass : public MachineFunctionPass {
202 static char ID; // Pass ID, replacement for typeid
203 bool AllowDoubleDefs;
204
205 explicit MachineVerifierPass(bool allowDoubleDefs = false)
206 : MachineFunctionPass(&ID),
207 AllowDoubleDefs(allowDoubleDefs) {}
208
209 void getAnalysisUsage(AnalysisUsage &AU) const {
210 AU.setPreservesAll();
211 MachineFunctionPass::getAnalysisUsage(AU);
212 }
213
214 bool runOnMachineFunction(MachineFunction &MF) {
215 MF.verify(this, AllowDoubleDefs);
216 return false;
217 }
218 };
219
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000220}
221
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000222char MachineVerifierPass::ID = 0;
223static RegisterPass<MachineVerifierPass>
Jakob Stoklund Olesende67a512009-05-17 19:37:14 +0000224MachineVer("machineverifier", "Verify generated machine code");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000225static const PassInfo *const MachineVerifyID = &MachineVer;
226
Chris Lattner17e9edc2009-08-23 02:51:22 +0000227FunctionPass *llvm::createMachineVerifierPass(bool allowPhysDoubleDefs) {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000228 return new MachineVerifierPass(allowPhysDoubleDefs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000229}
230
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000231void MachineFunction::verify(Pass *p, bool allowDoubleDefs) const {
232 MachineVerifier(p, allowDoubleDefs)
233 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000234}
235
Chris Lattner17e9edc2009-08-23 02:51:22 +0000236bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
237 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000238 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000239 std::string ErrorInfo;
240 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
241 raw_fd_ostream::F_Append);
242 if (!ErrorInfo.empty()) {
243 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
244 exit(1);
245 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000246
Chris Lattner17e9edc2009-08-23 02:51:22 +0000247 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000248 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000249 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000250 }
251
252 foundErrors = 0;
253
254 this->MF = &MF;
255 TM = &MF.getTarget();
256 TRI = TM->getRegisterInfo();
257 MRI = &MF.getRegInfo();
258
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000259 if (PASS) {
260 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
261 } else {
262 LiveVars = NULL;
263 }
264
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000265 visitMachineFunctionBefore();
266 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
267 MFI!=MFE; ++MFI) {
268 visitMachineBasicBlockBefore(MFI);
269 for (MachineBasicBlock::const_iterator MBBI = MFI->begin(),
270 MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
271 visitMachineInstrBefore(MBBI);
272 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
273 visitMachineOperand(&MBBI->getOperand(I), I);
274 visitMachineInstrAfter(MBBI);
275 }
276 visitMachineBasicBlockAfter(MFI);
277 }
278 visitMachineFunctionAfter();
279
Chris Lattner17e9edc2009-08-23 02:51:22 +0000280 if (OutFile)
281 delete OutFile;
282 else if (foundErrors)
283 llvm_report_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000284
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000285 // Clean up.
286 regsLive.clear();
287 regsDefined.clear();
288 regsDead.clear();
289 regsKilled.clear();
290 regsLiveInButUnused.clear();
291 MBBInfoMap.clear();
292
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000293 return false; // no changes
294}
295
Chris Lattner372fefe2009-08-23 01:03:30 +0000296void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000297 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000298 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000299 if (!foundErrors++)
Chris Lattner372fefe2009-08-23 01:03:30 +0000300 MF->print(*OS);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000301 *OS << "*** Bad machine code: " << msg << " ***\n"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000302 << "- function: " << MF->getFunction()->getNameStr() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000303}
304
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000305void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000306 assert(MBB);
307 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000308 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000309 << " " << (void*)MBB
Dan Gohman0ba90f32009-10-31 20:19:03 +0000310 << " (BB#" << MBB->getNumber() << ")\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000311}
312
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000313void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000314 assert(MI);
315 report(msg, MI->getParent());
316 *OS << "- instruction: ";
Chris Lattner705e07f2009-08-23 03:41:05 +0000317 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000318}
319
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000320void MachineVerifier::report(const char *msg,
321 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000322 assert(MO);
323 report(msg, MO->getParent());
324 *OS << "- operand " << MONum << ": ";
325 MO->print(*OS, TM);
326 *OS << "\n";
327}
328
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000329void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000330 BBInfo &MInfo = MBBInfoMap[MBB];
331 if (!MInfo.reachable) {
332 MInfo.reachable = true;
333 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
334 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
335 markReachable(*SuI);
336 }
337}
338
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000339void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000340 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000341
342 // A sub-register of a reserved register is also reserved
343 for (int Reg = regsReserved.find_first(); Reg>=0;
344 Reg = regsReserved.find_next(Reg)) {
345 for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
346 // FIXME: This should probably be:
347 // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
348 regsReserved.set(*Sub);
349 }
350 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000351 markReachable(&MF->front());
352}
353
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000354// Does iterator point to a and b as the first two elements?
355bool matchPair(MachineBasicBlock::const_succ_iterator i,
356 const MachineBasicBlock *a, const MachineBasicBlock *b) {
357 if (*i == a)
358 return *++i == b;
359 if (*i == b)
360 return *++i == a;
361 return false;
362}
363
364void
365MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Dan Gohman27920592009-08-27 02:43:49 +0000366 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
367
368 // Start with minimal CFG sanity checks.
369 MachineFunction::const_iterator MBBI = MBB;
370 ++MBBI;
371 if (MBBI != MF->end()) {
372 // Block is not last in function.
373 if (!MBB->isSuccessor(MBBI)) {
374 // Block does not fall through.
375 if (MBB->empty()) {
376 report("MBB doesn't fall through but is empty!", MBB);
377 }
Dan Gohmana01a80f2009-08-27 18:14:26 +0000378 }
Dan Gohman27920592009-08-27 02:43:49 +0000379 } else {
380 // Block is last in function.
381 if (MBB->empty()) {
382 report("MBB is last in function but is empty!", MBB);
383 }
384 }
385
386 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
387 MachineBasicBlock *TBB = 0, *FBB = 0;
388 SmallVector<MachineOperand, 4> Cond;
389 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
390 TBB, FBB, Cond)) {
391 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
392 // check whether its answers match up with reality.
393 if (!TBB && !FBB) {
394 // Block falls through to its successor.
395 MachineFunction::const_iterator MBBI = MBB;
396 ++MBBI;
397 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000398 // It's possible that the block legitimately ends with a noreturn
399 // call or an unreachable, in which case it won't actually fall
400 // out the bottom of the function.
401 } else if (MBB->succ_empty()) {
402 // It's possible that the block legitimately ends with a noreturn
403 // call or an unreachable, in which case it won't actuall fall
404 // out of the block.
Dan Gohman27920592009-08-27 02:43:49 +0000405 } else if (MBB->succ_size() != 1) {
406 report("MBB exits via unconditional fall-through but doesn't have "
407 "exactly one CFG successor!", MBB);
408 } else if (MBB->succ_begin()[0] != MBBI) {
409 report("MBB exits via unconditional fall-through but its successor "
410 "differs from its CFG successor!", MBB);
411 }
412 if (!MBB->empty() && MBB->back().getDesc().isBarrier()) {
413 report("MBB exits via unconditional fall-through but ends with a "
414 "barrier instruction!", MBB);
415 }
416 if (!Cond.empty()) {
417 report("MBB exits via unconditional fall-through but has a condition!",
418 MBB);
419 }
420 } else if (TBB && !FBB && Cond.empty()) {
421 // Block unconditionally branches somewhere.
422 if (MBB->succ_size() != 1) {
423 report("MBB exits via unconditional branch but doesn't have "
424 "exactly one CFG successor!", MBB);
425 } else if (MBB->succ_begin()[0] != TBB) {
426 report("MBB exits via unconditional branch but the CFG "
427 "successor doesn't match the actual successor!", MBB);
428 }
429 if (MBB->empty()) {
430 report("MBB exits via unconditional branch but doesn't contain "
431 "any instructions!", MBB);
432 } else if (!MBB->back().getDesc().isBarrier()) {
433 report("MBB exits via unconditional branch but doesn't end with a "
434 "barrier instruction!", MBB);
435 } else if (!MBB->back().getDesc().isTerminator()) {
436 report("MBB exits via unconditional branch but the branch isn't a "
437 "terminator instruction!", MBB);
438 }
439 } else if (TBB && !FBB && !Cond.empty()) {
440 // Block conditionally branches somewhere, otherwise falls through.
441 MachineFunction::const_iterator MBBI = MBB;
442 ++MBBI;
443 if (MBBI == MF->end()) {
444 report("MBB conditionally falls through out of function!", MBB);
445 } if (MBB->succ_size() != 2) {
446 report("MBB exits via conditional branch/fall-through but doesn't have "
447 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000448 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000449 report("MBB exits via conditional branch/fall-through but the CFG "
450 "successors don't match the actual successors!", MBB);
451 }
452 if (MBB->empty()) {
453 report("MBB exits via conditional branch/fall-through but doesn't "
454 "contain any instructions!", MBB);
455 } else if (MBB->back().getDesc().isBarrier()) {
456 report("MBB exits via conditional branch/fall-through but ends with a "
457 "barrier instruction!", MBB);
458 } else if (!MBB->back().getDesc().isTerminator()) {
459 report("MBB exits via conditional branch/fall-through but the branch "
460 "isn't a terminator instruction!", MBB);
461 }
462 } else if (TBB && FBB) {
463 // Block conditionally branches somewhere, otherwise branches
464 // somewhere else.
465 if (MBB->succ_size() != 2) {
466 report("MBB exits via conditional branch/branch but doesn't have "
467 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000468 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000469 report("MBB exits via conditional branch/branch but the CFG "
470 "successors don't match the actual successors!", MBB);
471 }
472 if (MBB->empty()) {
473 report("MBB exits via conditional branch/branch but doesn't "
474 "contain any instructions!", MBB);
475 } else if (!MBB->back().getDesc().isBarrier()) {
476 report("MBB exits via conditional branch/branch but doesn't end with a "
477 "barrier instruction!", MBB);
478 } else if (!MBB->back().getDesc().isTerminator()) {
479 report("MBB exits via conditional branch/branch but the branch "
480 "isn't a terminator instruction!", MBB);
481 }
482 if (Cond.empty()) {
483 report("MBB exits via conditinal branch/branch but there's no "
484 "condition!", MBB);
485 }
486 } else {
487 report("AnalyzeBranch returned invalid data!", MBB);
488 }
489 }
490
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000491 regsLive.clear();
492 for (MachineBasicBlock::const_livein_iterator I = MBB->livein_begin(),
493 E = MBB->livein_end(); I != E; ++I) {
494 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
495 report("MBB live-in list contains non-physical register", MBB);
496 continue;
497 }
498 regsLive.insert(*I);
499 for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++)
500 regsLive.insert(*R);
501 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000502 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000503
504 const MachineFrameInfo *MFI = MF->getFrameInfo();
505 assert(MFI && "Function has no frame info");
506 BitVector PR = MFI->getPristineRegs(MBB);
507 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
508 regsLive.insert(I);
509 for (const unsigned *R = TRI->getSubRegisters(I); *R; R++)
510 regsLive.insert(*R);
511 }
512
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000513 regsKilled.clear();
514 regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000515}
516
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000517void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000518 const TargetInstrDesc &TI = MI->getDesc();
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000519 if (MI->getNumOperands() < TI.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000520 report("Too few operands", MI);
521 *OS << TI.getNumOperands() << " operands expected, but "
522 << MI->getNumExplicitOperands() << " given.\n";
523 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000524
525 // Check the MachineMemOperands for basic consistency.
526 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
527 E = MI->memoperands_end(); I != E; ++I) {
528 if ((*I)->isLoad() && !TI.mayLoad())
529 report("Missing mayLoad flag", MI);
530 if ((*I)->isStore() && !TI.mayStore())
531 report("Missing mayStore flag", MI);
532 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000533}
534
535void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000536MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000537 const MachineInstr *MI = MO->getParent();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000538 const TargetInstrDesc &TI = MI->getDesc();
539
540 // The first TI.NumDefs operands must be explicit register defines
541 if (MONum < TI.getNumDefs()) {
542 if (!MO->isReg())
543 report("Explicit definition must be a register", MO, MONum);
544 else if (!MO->isDef())
545 report("Explicit definition marked as use", MO, MONum);
546 else if (MO->isImplicit())
547 report("Explicit definition marked as implicit", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000548 } else if (MONum < TI.getNumOperands()) {
549 if (MO->isReg()) {
550 if (MO->isDef())
551 report("Explicit operand marked as def", MO, MONum);
552 if (MO->isImplicit())
553 report("Explicit operand marked as implicit", MO, MONum);
554 }
555 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000556 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
557 if (MO->isReg() && !MO->isImplicit() && !TI.isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000558 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000559 }
560
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000561 switch (MO->getType()) {
562 case MachineOperand::MO_Register: {
563 const unsigned Reg = MO->getReg();
564 if (!Reg)
565 return;
566
567 // Check Live Variables.
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000568 if (MO->isUndef()) {
569 // An <undef> doesn't refer to any register, so just skip it.
570 } else if (MO->isUse()) {
571 regsLiveInButUnused.erase(Reg);
572
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000573 bool isKill = false;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000574 if (MO->isKill()) {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000575 isKill = true;
Jakob Stoklund Olesenf7d3e692009-07-15 23:37:26 +0000576 // Tied operands on two-address instuctions MUST NOT have a <kill> flag.
577 if (MI->isRegTiedToDefOperand(MONum))
578 report("Illegal kill flag on two-address instruction operand",
579 MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000580 } else {
Jakob Stoklund Olesenf7d3e692009-07-15 23:37:26 +0000581 // TwoAddress instr modifying a reg is treated as kill+def.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000582 unsigned defIdx;
583 if (MI->isRegTiedToDefOperand(MONum, &defIdx) &&
584 MI->getOperand(defIdx).getReg() == Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000585 isKill = true;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000586 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000587 if (isKill) {
588 addRegWithSubRegs(regsKilled, Reg);
589
590 // Check that LiveVars knows this kill
591 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg)) {
592 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
593 if (std::find(VI.Kills.begin(),
594 VI.Kills.end(), MI) == VI.Kills.end())
595 report("Kill missing from LiveVariables", MO, MONum);
596 }
597 }
598
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000599 // Use of a dead register.
600 if (!regsLive.count(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000601 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
602 // Reserved registers may be used even when 'dead'.
603 if (!isReserved(Reg))
604 report("Using an undefined physical register", MO, MONum);
605 } else {
606 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
607 // We don't know which virtual registers are live in, so only complain
608 // if vreg was killed in this MBB. Otherwise keep track of vregs that
609 // must be live in. PHI instructions are handled separately.
610 if (MInfo.regsKilled.count(Reg))
611 report("Using a killed virtual register", MO, MONum);
612 else if (MI->getOpcode() != TargetInstrInfo::PHI)
613 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
614 }
Duncan Sandse5567202009-05-16 03:28:54 +0000615 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000616 } else {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000617 assert(MO->isDef());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000618 // Register defined.
619 // TODO: verify that earlyclobber ops are not used.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000620 if (MO->isDead())
621 addRegWithSubRegs(regsDead, Reg);
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000622 else
623 addRegWithSubRegs(regsDefined, Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000624 }
625
626 // Check register classes.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000627 if (MONum < TI.getNumOperands() && !MO->isImplicit()) {
628 const TargetOperandInfo &TOI = TI.OpInfo[MONum];
629 unsigned SubIdx = MO->getSubReg();
630
631 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
632 unsigned sr = Reg;
633 if (SubIdx) {
634 unsigned s = TRI->getSubReg(Reg, SubIdx);
635 if (!s) {
636 report("Invalid subregister index for physical register",
637 MO, MONum);
638 return;
639 }
640 sr = s;
641 }
Chris Lattnercb778a82009-07-29 21:10:12 +0000642 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000643 if (!DRC->contains(sr)) {
644 report("Illegal physical register for instruction", MO, MONum);
645 *OS << TRI->getName(sr) << " is not a "
646 << DRC->getName() << " register.\n";
647 }
648 }
649 } else {
650 // Virtual register.
651 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
652 if (SubIdx) {
653 if (RC->subregclasses_begin()+SubIdx >= RC->subregclasses_end()) {
654 report("Invalid subregister index for virtual register", MO, MONum);
655 return;
656 }
657 RC = *(RC->subregclasses_begin()+SubIdx);
658 }
Chris Lattnercb778a82009-07-29 21:10:12 +0000659 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000660 if (RC != DRC && !RC->hasSuperClass(DRC)) {
661 report("Illegal virtual register for instruction", MO, MONum);
662 *OS << "Expected a " << DRC->getName() << " register, but got a "
663 << RC->getName() << " register\n";
664 }
665 }
666 }
667 }
668 break;
669 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000670
671 case MachineOperand::MO_MachineBasicBlock:
672 if (MI->getOpcode() == TargetInstrInfo::PHI) {
673 if (!MO->getMBB()->isSuccessor(MI->getParent()))
674 report("PHI operand is not in the CFG", MO, MONum);
675 }
676 break;
677
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000678 default:
679 break;
680 }
681}
682
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000683void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000684 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
685 set_union(MInfo.regsKilled, regsKilled);
686 set_subtract(regsLive, regsKilled);
687 regsKilled.clear();
688
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000689 // Verify that both <def> and <def,dead> operands refer to dead registers.
690 RegVector defs(regsDefined);
691 defs.append(regsDead.begin(), regsDead.end());
692
693 for (RegVector::const_iterator I = defs.begin(), E = defs.end();
694 I != E; ++I) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000695 if (regsLive.count(*I)) {
696 if (TargetRegisterInfo::isPhysicalRegister(*I)) {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000697 if (!allowPhysDoubleDefs && !isReserved(*I) &&
698 !regsLiveInButUnused.count(*I)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000699 report("Redefining a live physical register", MI);
700 *OS << "Register " << TRI->getName(*I)
701 << " was defined but already live.\n";
702 }
703 } else {
704 if (!allowVirtDoubleDefs) {
705 report("Redefining a live virtual register", MI);
706 *OS << "Virtual register %reg" << *I
707 << " was defined but already live.\n";
708 }
709 }
710 } else if (TargetRegisterInfo::isVirtualRegister(*I) &&
711 !MInfo.regsKilled.count(*I)) {
712 // Virtual register defined without being killed first must be dead on
713 // entry.
714 MInfo.vregsDeadIn.insert(std::make_pair(*I, MI));
715 }
716 }
717
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000718 set_subtract(regsLive, regsDead); regsDead.clear();
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000719 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000720}
721
722void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000723MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000724 MBBInfoMap[MBB].regsLiveOut = regsLive;
725 regsLive.clear();
726}
727
728// Calculate the largest possible vregsPassed sets. These are the registers that
729// can pass through an MBB live, but may not be live every time. It is assumed
730// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000731void MachineVerifier::calcMaxRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000732 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
733 // have any vregsPassed.
734 DenseSet<const MachineBasicBlock*> todo;
735 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
736 MFI != MFE; ++MFI) {
737 const MachineBasicBlock &MBB(*MFI);
738 BBInfo &MInfo = MBBInfoMap[&MBB];
739 if (!MInfo.reachable)
740 continue;
741 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
742 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
743 BBInfo &SInfo = MBBInfoMap[*SuI];
744 if (SInfo.addPassed(MInfo.regsLiveOut))
745 todo.insert(*SuI);
746 }
747 }
748
749 // Iteratively push vregsPassed to successors. This will converge to the same
750 // final state regardless of DenseSet iteration order.
751 while (!todo.empty()) {
752 const MachineBasicBlock *MBB = *todo.begin();
753 todo.erase(MBB);
754 BBInfo &MInfo = MBBInfoMap[MBB];
755 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
756 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
757 if (*SuI == MBB)
758 continue;
759 BBInfo &SInfo = MBBInfoMap[*SuI];
760 if (SInfo.addPassed(MInfo.vregsPassed))
761 todo.insert(*SuI);
762 }
763 }
764}
765
766// Calculate the minimum vregsPassed set. These are the registers that always
767// pass live through an MBB. The calculation assumes that calcMaxRegsPassed has
768// been called earlier.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000769void MachineVerifier::calcMinRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000770 DenseSet<const MachineBasicBlock*> todo;
771 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
772 MFI != MFE; ++MFI)
773 todo.insert(MFI);
774
775 while (!todo.empty()) {
776 const MachineBasicBlock *MBB = *todo.begin();
777 todo.erase(MBB);
778 BBInfo &MInfo = MBBInfoMap[MBB];
779
780 // Remove entries from vRegsPassed that are not live out from all
781 // reachable predecessors.
782 RegSet dead;
783 for (RegSet::iterator I = MInfo.vregsPassed.begin(),
784 E = MInfo.vregsPassed.end(); I != E; ++I) {
785 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
786 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
787 BBInfo &PrInfo = MBBInfoMap[*PrI];
788 if (PrInfo.reachable && !PrInfo.isLiveOut(*I)) {
789 dead.insert(*I);
790 break;
791 }
792 }
793 }
794 // If any regs removed, we need to recheck successors.
795 if (!dead.empty()) {
796 set_subtract(MInfo.vregsPassed, dead);
797 todo.insert(MBB->succ_begin(), MBB->succ_end());
798 }
799 }
800}
801
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000802// Calculate the set of virtual registers that must be passed through each basic
803// block in order to satisfy the requirements of successor blocks. This is very
804// similar to calcMaxRegsPassed, only backwards.
805void MachineVerifier::calcRegsRequired() {
806 // First push live-in regs to predecessors' vregsRequired.
807 DenseSet<const MachineBasicBlock*> todo;
808 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
809 MFI != MFE; ++MFI) {
810 const MachineBasicBlock &MBB(*MFI);
811 BBInfo &MInfo = MBBInfoMap[&MBB];
812 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
813 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
814 BBInfo &PInfo = MBBInfoMap[*PrI];
815 if (PInfo.addRequired(MInfo.vregsLiveIn))
816 todo.insert(*PrI);
817 }
818 }
819
820 // Iteratively push vregsRequired to predecessors. This will converge to the
821 // same final state regardless of DenseSet iteration order.
822 while (!todo.empty()) {
823 const MachineBasicBlock *MBB = *todo.begin();
824 todo.erase(MBB);
825 BBInfo &MInfo = MBBInfoMap[MBB];
826 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
827 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
828 if (*PrI == MBB)
829 continue;
830 BBInfo &SInfo = MBBInfoMap[*PrI];
831 if (SInfo.addRequired(MInfo.vregsRequired))
832 todo.insert(*PrI);
833 }
834 }
835}
836
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000837// Check PHI instructions at the beginning of MBB. It is assumed that
838// calcMinRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000839void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000840 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
841 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI) {
842 DenseSet<const MachineBasicBlock*> seen;
843
844 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
845 unsigned Reg = BBI->getOperand(i).getReg();
846 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
847 if (!Pre->isSuccessor(MBB))
848 continue;
849 seen.insert(Pre);
850 BBInfo &PrInfo = MBBInfoMap[Pre];
851 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
852 report("PHI operand is not live-out from predecessor",
853 &BBI->getOperand(i), i);
854 }
855
856 // Did we see all predecessors?
857 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
858 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
859 if (!seen.count(*PrI)) {
860 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000861 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000862 << " is a predecessor according to the CFG.\n";
863 }
864 }
865 }
866}
867
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000868void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000869 calcMaxRegsPassed();
870
871 // With the maximal set of vregsPassed we can verify dead-in registers.
872 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
873 MFI != MFE; ++MFI) {
874 BBInfo &MInfo = MBBInfoMap[MFI];
875
876 // Skip unreachable MBBs.
877 if (!MInfo.reachable)
878 continue;
879
880 for (MachineBasicBlock::const_pred_iterator PrI = MFI->pred_begin(),
881 PrE = MFI->pred_end(); PrI != PrE; ++PrI) {
882 BBInfo &PrInfo = MBBInfoMap[*PrI];
883 if (!PrInfo.reachable)
884 continue;
885
886 // Verify physical live-ins. EH landing pads have magic live-ins so we
887 // ignore them.
888 if (!MFI->isLandingPad()) {
889 for (MachineBasicBlock::const_livein_iterator I = MFI->livein_begin(),
890 E = MFI->livein_end(); I != E; ++I) {
891 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
Jakob Stoklund Olesend6fb9772009-05-16 07:24:54 +0000892 !isReserved (*I) && !PrInfo.isLiveOut(*I)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000893 report("Live-in physical register is not live-out from predecessor",
894 MFI);
895 *OS << "Register " << TRI->getName(*I)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000896 << " is not live-out from BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000897 << ".\n";
898 }
899 }
900 }
901
902
903 // Verify dead-in virtual registers.
904 if (!allowVirtDoubleDefs) {
905 for (RegMap::iterator I = MInfo.vregsDeadIn.begin(),
906 E = MInfo.vregsDeadIn.end(); I != E; ++I) {
907 // DeadIn register must be in neither regsLiveOut or vregsPassed of
908 // any predecessor.
909 if (PrInfo.isLiveOut(I->first)) {
910 report("Live-in virtual register redefined", I->second);
911 *OS << "Register %reg" << I->first
912 << " was live-out from predecessor MBB #"
913 << (*PrI)->getNumber() << ".\n";
914 }
915 }
916 }
917 }
918 }
919
920 calcMinRegsPassed();
921
922 // With the minimal set of vregsPassed we can verify live-in virtual
923 // registers, including PHI instructions.
924 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
925 MFI != MFE; ++MFI) {
926 BBInfo &MInfo = MBBInfoMap[MFI];
927
928 // Skip unreachable MBBs.
929 if (!MInfo.reachable)
930 continue;
931
932 checkPHIOps(MFI);
933
934 for (MachineBasicBlock::const_pred_iterator PrI = MFI->pred_begin(),
935 PrE = MFI->pred_end(); PrI != PrE; ++PrI) {
936 BBInfo &PrInfo = MBBInfoMap[*PrI];
937 if (!PrInfo.reachable)
938 continue;
939
940 for (RegMap::iterator I = MInfo.vregsLiveIn.begin(),
941 E = MInfo.vregsLiveIn.end(); I != E; ++I) {
942 if (!PrInfo.isLiveOut(I->first)) {
943 report("Used virtual register is not live-in", I->second);
944 *OS << "Register %reg" << I->first
945 << " is not live-out from predecessor MBB #"
946 << (*PrI)->getNumber()
947 << ".\n";
948 }
949 }
950 }
951 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000952
953 // Now check LiveVariables info if available
954 if (LiveVars) {
955 calcRegsRequired();
956 verifyLiveVariables();
957 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000958}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000959
960void MachineVerifier::verifyLiveVariables() {
961 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
962 for (unsigned Reg = TargetRegisterInfo::FirstVirtualRegister,
963 RegE = MRI->getLastVirtReg()-1; Reg != RegE; ++Reg) {
964 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
965 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
966 MFI != MFE; ++MFI) {
967 BBInfo &MInfo = MBBInfoMap[MFI];
968
969 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
970 if (MInfo.vregsRequired.count(Reg)) {
971 if (!VI.AliveBlocks.test(MFI->getNumber())) {
972 report("LiveVariables: Block missing from AliveBlocks", MFI);
973 *OS << "Virtual register %reg" << Reg
974 << " must be live through the block.\n";
975 }
976 } else {
977 if (VI.AliveBlocks.test(MFI->getNumber())) {
978 report("LiveVariables: Block should not be in AliveBlocks", MFI);
979 *OS << "Virtual register %reg" << Reg
980 << " is not needed live through the block.\n";
981 }
982 }
983 }
984 }
985}
986
987