blob: b4d6588bf43a9c09a4c86c0ea21832e4f107189c [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"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/CodeGen/Passes.h"
32#include "llvm/Target/TargetMachine.h"
33#include "llvm/Target/TargetRegisterInfo.h"
34#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000035#include "llvm/ADT/DenseSet.h"
36#include "llvm/ADT/SetOperations.h"
37#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000038#include "llvm/Support/Compiler.h"
39#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 {
45 struct VISIBILITY_HIDDEN MachineVerifier : public MachineFunctionPass {
46 static char ID; // Pass ID, replacement for typeid
47
48 MachineVerifier(bool allowDoubleDefs = false) :
49 MachineFunctionPass(&ID),
50 allowVirtDoubleDefs(allowDoubleDefs),
51 allowPhysDoubleDefs(allowDoubleDefs),
52 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
53 {}
54
55 void getAnalysisUsage(AnalysisUsage &AU) const {
56 AU.setPreservesAll();
Dan Gohmanad2afc22009-07-31 18:16:33 +000057 MachineFunctionPass::getAnalysisUsage(AU);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000058 }
59
60 bool runOnMachineFunction(MachineFunction &MF);
61
62 const bool allowVirtDoubleDefs;
63 const bool allowPhysDoubleDefs;
64
65 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000066 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000067 const MachineFunction *MF;
68 const TargetMachine *TM;
69 const TargetRegisterInfo *TRI;
70 const MachineRegisterInfo *MRI;
71
72 unsigned foundErrors;
73
74 typedef SmallVector<unsigned, 16> RegVector;
75 typedef DenseSet<unsigned> RegSet;
76 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
77
78 BitVector regsReserved;
79 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000080 RegVector regsDefined, regsDead, regsKilled;
81 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000082
83 // Add Reg and any sub-registers to RV
84 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
85 RV.push_back(Reg);
86 if (TargetRegisterInfo::isPhysicalRegister(Reg))
87 for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
88 RV.push_back(*R);
89 }
90
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000091 struct BBInfo {
92 // Is this MBB reachable from the MF entry point?
93 bool reachable;
94
95 // Vregs that must be live in because they are used without being
96 // defined. Map value is the user.
97 RegMap vregsLiveIn;
98
99 // Vregs that must be dead in because they are defined without being
100 // killed first. Map value is the defining instruction.
101 RegMap vregsDeadIn;
102
103 // Regs killed in MBB. They may be defined again, and will then be in both
104 // regsKilled and regsLiveOut.
105 RegSet regsKilled;
106
107 // Regs defined in MBB and live out. Note that vregs passing through may
108 // be live out without being mentioned here.
109 RegSet regsLiveOut;
110
111 // Vregs that pass through MBB untouched. This set is disjoint from
112 // regsKilled and regsLiveOut.
113 RegSet vregsPassed;
114
115 BBInfo() : reachable(false) {}
116
117 // Add register to vregsPassed if it belongs there. Return true if
118 // anything changed.
119 bool addPassed(unsigned Reg) {
120 if (!TargetRegisterInfo::isVirtualRegister(Reg))
121 return false;
122 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
123 return false;
124 return vregsPassed.insert(Reg).second;
125 }
126
127 // Same for a full set.
128 bool addPassed(const RegSet &RS) {
129 bool changed = false;
130 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
131 if (addPassed(*I))
132 changed = true;
133 return changed;
134 }
135
136 // Live-out registers are either in regsLiveOut or vregsPassed.
137 bool isLiveOut(unsigned Reg) const {
138 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
139 }
140 };
141
142 // Extra register info per MBB.
143 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
144
145 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000146 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000147 }
148
149 void visitMachineFunctionBefore();
150 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
151 void visitMachineInstrBefore(const MachineInstr *MI);
152 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
153 void visitMachineInstrAfter(const MachineInstr *MI);
154 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
155 void visitMachineFunctionAfter();
156
157 void report(const char *msg, const MachineFunction *MF);
158 void report(const char *msg, const MachineBasicBlock *MBB);
159 void report(const char *msg, const MachineInstr *MI);
160 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
161
162 void markReachable(const MachineBasicBlock *MBB);
163 void calcMaxRegsPassed();
164 void calcMinRegsPassed();
165 void checkPHIOps(const MachineBasicBlock *MBB);
166 };
167}
168
169char MachineVerifier::ID = 0;
170static RegisterPass<MachineVerifier>
Jakob Stoklund Olesende67a512009-05-17 19:37:14 +0000171MachineVer("machineverifier", "Verify generated machine code");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000172static const PassInfo *const MachineVerifyID = &MachineVer;
173
Chris Lattner17e9edc2009-08-23 02:51:22 +0000174FunctionPass *llvm::createMachineVerifierPass(bool allowPhysDoubleDefs) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000175 return new MachineVerifier(allowPhysDoubleDefs);
176}
177
Chris Lattner17e9edc2009-08-23 02:51:22 +0000178bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
179 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000180 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000181 std::string ErrorInfo;
182 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
183 raw_fd_ostream::F_Append);
184 if (!ErrorInfo.empty()) {
185 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
186 exit(1);
187 }
188
189 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000190 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000191 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000192 }
193
194 foundErrors = 0;
195
196 this->MF = &MF;
197 TM = &MF.getTarget();
198 TRI = TM->getRegisterInfo();
199 MRI = &MF.getRegInfo();
200
201 visitMachineFunctionBefore();
202 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
203 MFI!=MFE; ++MFI) {
204 visitMachineBasicBlockBefore(MFI);
205 for (MachineBasicBlock::const_iterator MBBI = MFI->begin(),
206 MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
207 visitMachineInstrBefore(MBBI);
208 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
209 visitMachineOperand(&MBBI->getOperand(I), I);
210 visitMachineInstrAfter(MBBI);
211 }
212 visitMachineBasicBlockAfter(MFI);
213 }
214 visitMachineFunctionAfter();
215
Chris Lattner17e9edc2009-08-23 02:51:22 +0000216 if (OutFile)
217 delete OutFile;
218 else if (foundErrors)
219 llvm_report_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000220
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000221 // Clean up.
222 regsLive.clear();
223 regsDefined.clear();
224 regsDead.clear();
225 regsKilled.clear();
226 regsLiveInButUnused.clear();
227 MBBInfoMap.clear();
228
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000229 return false; // no changes
230}
231
Chris Lattner372fefe2009-08-23 01:03:30 +0000232void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000233 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000234 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000235 if (!foundErrors++)
Chris Lattner372fefe2009-08-23 01:03:30 +0000236 MF->print(*OS);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000237 *OS << "*** Bad machine code: " << msg << " ***\n"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000238 << "- function: " << MF->getFunction()->getNameStr() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000239}
240
241void
242MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB)
243{
244 assert(MBB);
245 report(msg, MBB->getParent());
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000246 *OS << "- basic block: " << MBB->getBasicBlock()->getNameStr()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000247 << " " << (void*)MBB
248 << " (#" << MBB->getNumber() << ")\n";
249}
250
251void
252MachineVerifier::report(const char *msg, const MachineInstr *MI)
253{
254 assert(MI);
255 report(msg, MI->getParent());
256 *OS << "- instruction: ";
Chris Lattner705e07f2009-08-23 03:41:05 +0000257 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000258}
259
260void
261MachineVerifier::report(const char *msg,
262 const MachineOperand *MO, unsigned MONum)
263{
264 assert(MO);
265 report(msg, MO->getParent());
266 *OS << "- operand " << MONum << ": ";
267 MO->print(*OS, TM);
268 *OS << "\n";
269}
270
271void
272MachineVerifier::markReachable(const MachineBasicBlock *MBB)
273{
274 BBInfo &MInfo = MBBInfoMap[MBB];
275 if (!MInfo.reachable) {
276 MInfo.reachable = true;
277 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
278 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
279 markReachable(*SuI);
280 }
281}
282
283void
284MachineVerifier::visitMachineFunctionBefore()
285{
286 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000287
288 // A sub-register of a reserved register is also reserved
289 for (int Reg = regsReserved.find_first(); Reg>=0;
290 Reg = regsReserved.find_next(Reg)) {
291 for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
292 // FIXME: This should probably be:
293 // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
294 regsReserved.set(*Sub);
295 }
296 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000297 markReachable(&MF->front());
298}
299
300void
301MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB)
302{
Dan Gohman27920592009-08-27 02:43:49 +0000303 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
304
305 // Start with minimal CFG sanity checks.
306 MachineFunction::const_iterator MBBI = MBB;
307 ++MBBI;
308 if (MBBI != MF->end()) {
309 // Block is not last in function.
310 if (!MBB->isSuccessor(MBBI)) {
311 // Block does not fall through.
312 if (MBB->empty()) {
313 report("MBB doesn't fall through but is empty!", MBB);
314 }
Dan Gohmana01a80f2009-08-27 18:14:26 +0000315 }
316 if (TII->BlockHasNoFallThrough(*MBB)) {
317 if (MBB->empty()) {
Dan Gohman27920592009-08-27 02:43:49 +0000318 report("TargetInstrInfo says the block has no fall through, but the "
Dan Gohmana01a80f2009-08-27 18:14:26 +0000319 "block is empty!", MBB);
320 } else if (!MBB->back().getDesc().isBarrier()) {
321 report("TargetInstrInfo says the block has no fall through, but the "
322 "block does not end in a barrier!", MBB);
Dan Gohman27920592009-08-27 02:43:49 +0000323 }
324 }
325 } else {
326 // Block is last in function.
327 if (MBB->empty()) {
328 report("MBB is last in function but is empty!", MBB);
329 }
330 }
331
332 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
333 MachineBasicBlock *TBB = 0, *FBB = 0;
334 SmallVector<MachineOperand, 4> Cond;
335 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
336 TBB, FBB, Cond)) {
337 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
338 // check whether its answers match up with reality.
339 if (!TBB && !FBB) {
340 // Block falls through to its successor.
341 MachineFunction::const_iterator MBBI = MBB;
342 ++MBBI;
343 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000344 // It's possible that the block legitimately ends with a noreturn
345 // call or an unreachable, in which case it won't actually fall
346 // out the bottom of the function.
347 } else if (MBB->succ_empty()) {
348 // It's possible that the block legitimately ends with a noreturn
349 // call or an unreachable, in which case it won't actuall fall
350 // out of the block.
Dan Gohman27920592009-08-27 02:43:49 +0000351 } else if (MBB->succ_size() != 1) {
352 report("MBB exits via unconditional fall-through but doesn't have "
353 "exactly one CFG successor!", MBB);
354 } else if (MBB->succ_begin()[0] != MBBI) {
355 report("MBB exits via unconditional fall-through but its successor "
356 "differs from its CFG successor!", MBB);
357 }
358 if (!MBB->empty() && MBB->back().getDesc().isBarrier()) {
359 report("MBB exits via unconditional fall-through but ends with a "
360 "barrier instruction!", MBB);
361 }
362 if (!Cond.empty()) {
363 report("MBB exits via unconditional fall-through but has a condition!",
364 MBB);
365 }
366 } else if (TBB && !FBB && Cond.empty()) {
367 // Block unconditionally branches somewhere.
368 if (MBB->succ_size() != 1) {
369 report("MBB exits via unconditional branch but doesn't have "
370 "exactly one CFG successor!", MBB);
371 } else if (MBB->succ_begin()[0] != TBB) {
372 report("MBB exits via unconditional branch but the CFG "
373 "successor doesn't match the actual successor!", MBB);
374 }
375 if (MBB->empty()) {
376 report("MBB exits via unconditional branch but doesn't contain "
377 "any instructions!", MBB);
378 } else if (!MBB->back().getDesc().isBarrier()) {
379 report("MBB exits via unconditional branch but doesn't end with a "
380 "barrier instruction!", MBB);
381 } else if (!MBB->back().getDesc().isTerminator()) {
382 report("MBB exits via unconditional branch but the branch isn't a "
383 "terminator instruction!", MBB);
384 }
385 } else if (TBB && !FBB && !Cond.empty()) {
386 // Block conditionally branches somewhere, otherwise falls through.
387 MachineFunction::const_iterator MBBI = MBB;
388 ++MBBI;
389 if (MBBI == MF->end()) {
390 report("MBB conditionally falls through out of function!", MBB);
391 } if (MBB->succ_size() != 2) {
392 report("MBB exits via conditional branch/fall-through but doesn't have "
393 "exactly two CFG successors!", MBB);
394 } else if ((MBB->succ_begin()[0] == TBB && MBB->succ_end()[1] == MBBI) ||
395 (MBB->succ_begin()[1] == TBB && MBB->succ_end()[0] == MBBI)) {
396 report("MBB exits via conditional branch/fall-through but the CFG "
397 "successors don't match the actual successors!", MBB);
398 }
399 if (MBB->empty()) {
400 report("MBB exits via conditional branch/fall-through but doesn't "
401 "contain any instructions!", MBB);
402 } else if (MBB->back().getDesc().isBarrier()) {
403 report("MBB exits via conditional branch/fall-through but ends with a "
404 "barrier instruction!", MBB);
405 } else if (!MBB->back().getDesc().isTerminator()) {
406 report("MBB exits via conditional branch/fall-through but the branch "
407 "isn't a terminator instruction!", MBB);
408 }
409 } else if (TBB && FBB) {
410 // Block conditionally branches somewhere, otherwise branches
411 // somewhere else.
412 if (MBB->succ_size() != 2) {
413 report("MBB exits via conditional branch/branch but doesn't have "
414 "exactly two CFG successors!", MBB);
415 } else if ((MBB->succ_begin()[0] == TBB && MBB->succ_end()[1] == FBB) ||
416 (MBB->succ_begin()[1] == TBB && MBB->succ_end()[0] == FBB)) {
417 report("MBB exits via conditional branch/branch but the CFG "
418 "successors don't match the actual successors!", MBB);
419 }
420 if (MBB->empty()) {
421 report("MBB exits via conditional branch/branch but doesn't "
422 "contain any instructions!", MBB);
423 } else if (!MBB->back().getDesc().isBarrier()) {
424 report("MBB exits via conditional branch/branch but doesn't end with a "
425 "barrier instruction!", MBB);
426 } else if (!MBB->back().getDesc().isTerminator()) {
427 report("MBB exits via conditional branch/branch but the branch "
428 "isn't a terminator instruction!", MBB);
429 }
430 if (Cond.empty()) {
431 report("MBB exits via conditinal branch/branch but there's no "
432 "condition!", MBB);
433 }
434 } else {
435 report("AnalyzeBranch returned invalid data!", MBB);
436 }
437 }
438
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000439 regsLive.clear();
440 for (MachineBasicBlock::const_livein_iterator I = MBB->livein_begin(),
441 E = MBB->livein_end(); I != E; ++I) {
442 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
443 report("MBB live-in list contains non-physical register", MBB);
444 continue;
445 }
446 regsLive.insert(*I);
447 for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++)
448 regsLive.insert(*R);
449 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000450 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000451
452 const MachineFrameInfo *MFI = MF->getFrameInfo();
453 assert(MFI && "Function has no frame info");
454 BitVector PR = MFI->getPristineRegs(MBB);
455 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
456 regsLive.insert(I);
457 for (const unsigned *R = TRI->getSubRegisters(I); *R; R++)
458 regsLive.insert(*R);
459 }
460
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000461 regsKilled.clear();
462 regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000463}
464
465void
466MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI)
467{
468 const TargetInstrDesc &TI = MI->getDesc();
469 if (MI->getNumExplicitOperands() < TI.getNumOperands()) {
470 report("Too few operands", MI);
471 *OS << TI.getNumOperands() << " operands expected, but "
472 << MI->getNumExplicitOperands() << " given.\n";
473 }
474 if (!TI.isVariadic()) {
475 if (MI->getNumExplicitOperands() > TI.getNumOperands()) {
476 report("Too many operands", MI);
477 *OS << TI.getNumOperands() << " operands expected, but "
478 << MI->getNumExplicitOperands() << " given.\n";
479 }
480 }
481}
482
483void
484MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum)
485{
486 const MachineInstr *MI = MO->getParent();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000487 const TargetInstrDesc &TI = MI->getDesc();
488
489 // The first TI.NumDefs operands must be explicit register defines
490 if (MONum < TI.getNumDefs()) {
491 if (!MO->isReg())
492 report("Explicit definition must be a register", MO, MONum);
493 else if (!MO->isDef())
494 report("Explicit definition marked as use", MO, MONum);
495 else if (MO->isImplicit())
496 report("Explicit definition marked as implicit", MO, MONum);
497 }
498
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000499 switch (MO->getType()) {
500 case MachineOperand::MO_Register: {
501 const unsigned Reg = MO->getReg();
502 if (!Reg)
503 return;
504
505 // Check Live Variables.
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000506 if (MO->isUndef()) {
507 // An <undef> doesn't refer to any register, so just skip it.
508 } else if (MO->isUse()) {
509 regsLiveInButUnused.erase(Reg);
510
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000511 if (MO->isKill()) {
512 addRegWithSubRegs(regsKilled, Reg);
Jakob Stoklund Olesenf7d3e692009-07-15 23:37:26 +0000513 // Tied operands on two-address instuctions MUST NOT have a <kill> flag.
514 if (MI->isRegTiedToDefOperand(MONum))
515 report("Illegal kill flag on two-address instruction operand",
516 MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000517 } else {
Jakob Stoklund Olesenf7d3e692009-07-15 23:37:26 +0000518 // TwoAddress instr modifying a reg is treated as kill+def.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000519 unsigned defIdx;
520 if (MI->isRegTiedToDefOperand(MONum, &defIdx) &&
521 MI->getOperand(defIdx).getReg() == Reg)
522 addRegWithSubRegs(regsKilled, Reg);
523 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000524 // Use of a dead register.
525 if (!regsLive.count(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000526 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
527 // Reserved registers may be used even when 'dead'.
528 if (!isReserved(Reg))
529 report("Using an undefined physical register", MO, MONum);
530 } else {
531 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
532 // We don't know which virtual registers are live in, so only complain
533 // if vreg was killed in this MBB. Otherwise keep track of vregs that
534 // must be live in. PHI instructions are handled separately.
535 if (MInfo.regsKilled.count(Reg))
536 report("Using a killed virtual register", MO, MONum);
537 else if (MI->getOpcode() != TargetInstrInfo::PHI)
538 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
539 }
Duncan Sandse5567202009-05-16 03:28:54 +0000540 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000541 } else {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000542 assert(MO->isDef());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000543 // Register defined.
544 // TODO: verify that earlyclobber ops are not used.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000545 if (MO->isDead())
546 addRegWithSubRegs(regsDead, Reg);
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000547 else
548 addRegWithSubRegs(regsDefined, Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000549 }
550
551 // Check register classes.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000552 if (MONum < TI.getNumOperands() && !MO->isImplicit()) {
553 const TargetOperandInfo &TOI = TI.OpInfo[MONum];
554 unsigned SubIdx = MO->getSubReg();
555
556 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
557 unsigned sr = Reg;
558 if (SubIdx) {
559 unsigned s = TRI->getSubReg(Reg, SubIdx);
560 if (!s) {
561 report("Invalid subregister index for physical register",
562 MO, MONum);
563 return;
564 }
565 sr = s;
566 }
Chris Lattnercb778a82009-07-29 21:10:12 +0000567 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000568 if (!DRC->contains(sr)) {
569 report("Illegal physical register for instruction", MO, MONum);
570 *OS << TRI->getName(sr) << " is not a "
571 << DRC->getName() << " register.\n";
572 }
573 }
574 } else {
575 // Virtual register.
576 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
577 if (SubIdx) {
578 if (RC->subregclasses_begin()+SubIdx >= RC->subregclasses_end()) {
579 report("Invalid subregister index for virtual register", MO, MONum);
580 return;
581 }
582 RC = *(RC->subregclasses_begin()+SubIdx);
583 }
Chris Lattnercb778a82009-07-29 21:10:12 +0000584 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000585 if (RC != DRC && !RC->hasSuperClass(DRC)) {
586 report("Illegal virtual register for instruction", MO, MONum);
587 *OS << "Expected a " << DRC->getName() << " register, but got a "
588 << RC->getName() << " register\n";
589 }
590 }
591 }
592 }
593 break;
594 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000595
596 case MachineOperand::MO_MachineBasicBlock:
597 if (MI->getOpcode() == TargetInstrInfo::PHI) {
598 if (!MO->getMBB()->isSuccessor(MI->getParent()))
599 report("PHI operand is not in the CFG", MO, MONum);
600 }
601 break;
602
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000603 default:
604 break;
605 }
606}
607
608void
609MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI)
610{
611 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
612 set_union(MInfo.regsKilled, regsKilled);
613 set_subtract(regsLive, regsKilled);
614 regsKilled.clear();
615
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000616 // Verify that both <def> and <def,dead> operands refer to dead registers.
617 RegVector defs(regsDefined);
618 defs.append(regsDead.begin(), regsDead.end());
619
620 for (RegVector::const_iterator I = defs.begin(), E = defs.end();
621 I != E; ++I) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000622 if (regsLive.count(*I)) {
623 if (TargetRegisterInfo::isPhysicalRegister(*I)) {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000624 if (!allowPhysDoubleDefs && !isReserved(*I) &&
625 !regsLiveInButUnused.count(*I)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000626 report("Redefining a live physical register", MI);
627 *OS << "Register " << TRI->getName(*I)
628 << " was defined but already live.\n";
629 }
630 } else {
631 if (!allowVirtDoubleDefs) {
632 report("Redefining a live virtual register", MI);
633 *OS << "Virtual register %reg" << *I
634 << " was defined but already live.\n";
635 }
636 }
637 } else if (TargetRegisterInfo::isVirtualRegister(*I) &&
638 !MInfo.regsKilled.count(*I)) {
639 // Virtual register defined without being killed first must be dead on
640 // entry.
641 MInfo.vregsDeadIn.insert(std::make_pair(*I, MI));
642 }
643 }
644
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000645 set_subtract(regsLive, regsDead); regsDead.clear();
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000646 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000647}
648
649void
650MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB)
651{
652 MBBInfoMap[MBB].regsLiveOut = regsLive;
653 regsLive.clear();
654}
655
656// Calculate the largest possible vregsPassed sets. These are the registers that
657// can pass through an MBB live, but may not be live every time. It is assumed
658// that all vregsPassed sets are empty before the call.
659void
660MachineVerifier::calcMaxRegsPassed()
661{
662 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
663 // have any vregsPassed.
664 DenseSet<const MachineBasicBlock*> todo;
665 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
666 MFI != MFE; ++MFI) {
667 const MachineBasicBlock &MBB(*MFI);
668 BBInfo &MInfo = MBBInfoMap[&MBB];
669 if (!MInfo.reachable)
670 continue;
671 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
672 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
673 BBInfo &SInfo = MBBInfoMap[*SuI];
674 if (SInfo.addPassed(MInfo.regsLiveOut))
675 todo.insert(*SuI);
676 }
677 }
678
679 // Iteratively push vregsPassed to successors. This will converge to the same
680 // final state regardless of DenseSet iteration order.
681 while (!todo.empty()) {
682 const MachineBasicBlock *MBB = *todo.begin();
683 todo.erase(MBB);
684 BBInfo &MInfo = MBBInfoMap[MBB];
685 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
686 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
687 if (*SuI == MBB)
688 continue;
689 BBInfo &SInfo = MBBInfoMap[*SuI];
690 if (SInfo.addPassed(MInfo.vregsPassed))
691 todo.insert(*SuI);
692 }
693 }
694}
695
696// Calculate the minimum vregsPassed set. These are the registers that always
697// pass live through an MBB. The calculation assumes that calcMaxRegsPassed has
698// been called earlier.
699void
700MachineVerifier::calcMinRegsPassed()
701{
702 DenseSet<const MachineBasicBlock*> todo;
703 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
704 MFI != MFE; ++MFI)
705 todo.insert(MFI);
706
707 while (!todo.empty()) {
708 const MachineBasicBlock *MBB = *todo.begin();
709 todo.erase(MBB);
710 BBInfo &MInfo = MBBInfoMap[MBB];
711
712 // Remove entries from vRegsPassed that are not live out from all
713 // reachable predecessors.
714 RegSet dead;
715 for (RegSet::iterator I = MInfo.vregsPassed.begin(),
716 E = MInfo.vregsPassed.end(); I != E; ++I) {
717 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
718 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
719 BBInfo &PrInfo = MBBInfoMap[*PrI];
720 if (PrInfo.reachable && !PrInfo.isLiveOut(*I)) {
721 dead.insert(*I);
722 break;
723 }
724 }
725 }
726 // If any regs removed, we need to recheck successors.
727 if (!dead.empty()) {
728 set_subtract(MInfo.vregsPassed, dead);
729 todo.insert(MBB->succ_begin(), MBB->succ_end());
730 }
731 }
732}
733
734// Check PHI instructions at the beginning of MBB. It is assumed that
735// calcMinRegsPassed has been run so BBInfo::isLiveOut is valid.
736void
737MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB)
738{
739 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
740 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI) {
741 DenseSet<const MachineBasicBlock*> seen;
742
743 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
744 unsigned Reg = BBI->getOperand(i).getReg();
745 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
746 if (!Pre->isSuccessor(MBB))
747 continue;
748 seen.insert(Pre);
749 BBInfo &PrInfo = MBBInfoMap[Pre];
750 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
751 report("PHI operand is not live-out from predecessor",
752 &BBI->getOperand(i), i);
753 }
754
755 // Did we see all predecessors?
756 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
757 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
758 if (!seen.count(*PrI)) {
759 report("Missing PHI operand", BBI);
760 *OS << "MBB #" << (*PrI)->getNumber()
761 << " is a predecessor according to the CFG.\n";
762 }
763 }
764 }
765}
766
767void
768MachineVerifier::visitMachineFunctionAfter()
769{
770 calcMaxRegsPassed();
771
772 // With the maximal set of vregsPassed we can verify dead-in registers.
773 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
774 MFI != MFE; ++MFI) {
775 BBInfo &MInfo = MBBInfoMap[MFI];
776
777 // Skip unreachable MBBs.
778 if (!MInfo.reachable)
779 continue;
780
781 for (MachineBasicBlock::const_pred_iterator PrI = MFI->pred_begin(),
782 PrE = MFI->pred_end(); PrI != PrE; ++PrI) {
783 BBInfo &PrInfo = MBBInfoMap[*PrI];
784 if (!PrInfo.reachable)
785 continue;
786
787 // Verify physical live-ins. EH landing pads have magic live-ins so we
788 // ignore them.
789 if (!MFI->isLandingPad()) {
790 for (MachineBasicBlock::const_livein_iterator I = MFI->livein_begin(),
791 E = MFI->livein_end(); I != E; ++I) {
792 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
Jakob Stoklund Olesend6fb9772009-05-16 07:24:54 +0000793 !isReserved (*I) && !PrInfo.isLiveOut(*I)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000794 report("Live-in physical register is not live-out from predecessor",
795 MFI);
796 *OS << "Register " << TRI->getName(*I)
797 << " is not live-out from MBB #" << (*PrI)->getNumber()
798 << ".\n";
799 }
800 }
801 }
802
803
804 // Verify dead-in virtual registers.
805 if (!allowVirtDoubleDefs) {
806 for (RegMap::iterator I = MInfo.vregsDeadIn.begin(),
807 E = MInfo.vregsDeadIn.end(); I != E; ++I) {
808 // DeadIn register must be in neither regsLiveOut or vregsPassed of
809 // any predecessor.
810 if (PrInfo.isLiveOut(I->first)) {
811 report("Live-in virtual register redefined", I->second);
812 *OS << "Register %reg" << I->first
813 << " was live-out from predecessor MBB #"
814 << (*PrI)->getNumber() << ".\n";
815 }
816 }
817 }
818 }
819 }
820
821 calcMinRegsPassed();
822
823 // With the minimal set of vregsPassed we can verify live-in virtual
824 // registers, including PHI instructions.
825 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
826 MFI != MFE; ++MFI) {
827 BBInfo &MInfo = MBBInfoMap[MFI];
828
829 // Skip unreachable MBBs.
830 if (!MInfo.reachable)
831 continue;
832
833 checkPHIOps(MFI);
834
835 for (MachineBasicBlock::const_pred_iterator PrI = MFI->pred_begin(),
836 PrE = MFI->pred_end(); PrI != PrE; ++PrI) {
837 BBInfo &PrInfo = MBBInfoMap[*PrI];
838 if (!PrInfo.reachable)
839 continue;
840
841 for (RegMap::iterator I = MInfo.vregsLiveIn.begin(),
842 E = MInfo.vregsLiveIn.end(); I != E; ++I) {
843 if (!PrInfo.isLiveOut(I->first)) {
844 report("Used virtual register is not live-in", I->second);
845 *OS << "Register %reg" << I->first
846 << " is not live-out from predecessor MBB #"
847 << (*PrI)->getNumber()
848 << ".\n";
849 }
850 }
851 }
852 }
853}