blob: d9a0adf6bfe71f169fdd2fd893eddd3949bb88f9 [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 Olesen73cf7092010-08-05 18:59:59 +000047 MachineVerifier(Pass *pass) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000048 PASS(pass),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000049 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000050 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000051
52 bool runOnMachineFunction(MachineFunction &MF);
53
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000054 Pass *const PASS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000055 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000056 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000057 const MachineFunction *MF;
58 const TargetMachine *TM;
59 const TargetRegisterInfo *TRI;
60 const MachineRegisterInfo *MRI;
61
62 unsigned foundErrors;
63
64 typedef SmallVector<unsigned, 16> RegVector;
65 typedef DenseSet<unsigned> RegSet;
66 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
67
68 BitVector regsReserved;
69 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000070 RegVector regsDefined, regsDead, regsKilled;
71 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000072
73 // Add Reg and any sub-registers to RV
74 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
75 RV.push_back(Reg);
76 if (TargetRegisterInfo::isPhysicalRegister(Reg))
77 for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
78 RV.push_back(*R);
79 }
80
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000081 struct BBInfo {
82 // Is this MBB reachable from the MF entry point?
83 bool reachable;
84
85 // Vregs that must be live in because they are used without being
86 // defined. Map value is the user.
87 RegMap vregsLiveIn;
88
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000089 // Regs killed in MBB. They may be defined again, and will then be in both
90 // regsKilled and regsLiveOut.
91 RegSet regsKilled;
92
93 // Regs defined in MBB and live out. Note that vregs passing through may
94 // be live out without being mentioned here.
95 RegSet regsLiveOut;
96
97 // Vregs that pass through MBB untouched. This set is disjoint from
98 // regsKilled and regsLiveOut.
99 RegSet vregsPassed;
100
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000101 // Vregs that must pass through MBB because they are needed by a successor
102 // block. This set is disjoint from regsLiveOut.
103 RegSet vregsRequired;
104
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000105 BBInfo() : reachable(false) {}
106
107 // Add register to vregsPassed if it belongs there. Return true if
108 // anything changed.
109 bool addPassed(unsigned Reg) {
110 if (!TargetRegisterInfo::isVirtualRegister(Reg))
111 return false;
112 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
113 return false;
114 return vregsPassed.insert(Reg).second;
115 }
116
117 // Same for a full set.
118 bool addPassed(const RegSet &RS) {
119 bool changed = false;
120 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
121 if (addPassed(*I))
122 changed = true;
123 return changed;
124 }
125
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000126 // Add register to vregsRequired if it belongs there. Return true if
127 // anything changed.
128 bool addRequired(unsigned Reg) {
129 if (!TargetRegisterInfo::isVirtualRegister(Reg))
130 return false;
131 if (regsLiveOut.count(Reg))
132 return false;
133 return vregsRequired.insert(Reg).second;
134 }
135
136 // Same for a full set.
137 bool addRequired(const RegSet &RS) {
138 bool changed = false;
139 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
140 if (addRequired(*I))
141 changed = true;
142 return changed;
143 }
144
145 // Same for a full map.
146 bool addRequired(const RegMap &RM) {
147 bool changed = false;
148 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
149 if (addRequired(I->first))
150 changed = true;
151 return changed;
152 }
153
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000154 // Live-out registers are either in regsLiveOut or vregsPassed.
155 bool isLiveOut(unsigned Reg) const {
156 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
157 }
158 };
159
160 // Extra register info per MBB.
161 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
162
163 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000164 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000165 }
166
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000167 // Analysis information if available
168 LiveVariables *LiveVars;
169
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000170 void visitMachineFunctionBefore();
171 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
172 void visitMachineInstrBefore(const MachineInstr *MI);
173 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
174 void visitMachineInstrAfter(const MachineInstr *MI);
175 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
176 void visitMachineFunctionAfter();
177
178 void report(const char *msg, const MachineFunction *MF);
179 void report(const char *msg, const MachineBasicBlock *MBB);
180 void report(const char *msg, const MachineInstr *MI);
181 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
182
183 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000184 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000185 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000186
187 void calcRegsRequired();
188 void verifyLiveVariables();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000189 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000190
191 struct MachineVerifierPass : public MachineFunctionPass {
192 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000193
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000194 MachineVerifierPass()
195 : MachineFunctionPass(&ID) {}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000196
197 void getAnalysisUsage(AnalysisUsage &AU) const {
198 AU.setPreservesAll();
199 MachineFunctionPass::getAnalysisUsage(AU);
200 }
201
202 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000203 MF.verify(this);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000204 return false;
205 }
206 };
207
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000208}
209
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000210char MachineVerifierPass::ID = 0;
211static RegisterPass<MachineVerifierPass>
Jakob Stoklund Olesende67a512009-05-17 19:37:14 +0000212MachineVer("machineverifier", "Verify generated machine code");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000213static const PassInfo *const MachineVerifyID = &MachineVer;
214
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000215FunctionPass *llvm::createMachineVerifierPass() {
216 return new MachineVerifierPass();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000217}
218
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000219void MachineFunction::verify(Pass *p) const {
220 MachineVerifier(p).runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000221}
222
Chris Lattner17e9edc2009-08-23 02:51:22 +0000223bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
224 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000225 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000226 std::string ErrorInfo;
227 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
228 raw_fd_ostream::F_Append);
229 if (!ErrorInfo.empty()) {
230 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
231 exit(1);
232 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000233
Chris Lattner17e9edc2009-08-23 02:51:22 +0000234 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000235 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000236 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000237 }
238
239 foundErrors = 0;
240
241 this->MF = &MF;
242 TM = &MF.getTarget();
243 TRI = TM->getRegisterInfo();
244 MRI = &MF.getRegInfo();
245
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000246 if (PASS) {
247 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
248 } else {
249 LiveVars = NULL;
250 }
251
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000252 visitMachineFunctionBefore();
253 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
254 MFI!=MFE; ++MFI) {
255 visitMachineBasicBlockBefore(MFI);
256 for (MachineBasicBlock::const_iterator MBBI = MFI->begin(),
257 MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
258 visitMachineInstrBefore(MBBI);
259 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
260 visitMachineOperand(&MBBI->getOperand(I), I);
261 visitMachineInstrAfter(MBBI);
262 }
263 visitMachineBasicBlockAfter(MFI);
264 }
265 visitMachineFunctionAfter();
266
Chris Lattner17e9edc2009-08-23 02:51:22 +0000267 if (OutFile)
268 delete OutFile;
269 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000270 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000271
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000272 // Clean up.
273 regsLive.clear();
274 regsDefined.clear();
275 regsDead.clear();
276 regsKilled.clear();
277 regsLiveInButUnused.clear();
278 MBBInfoMap.clear();
279
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000280 return false; // no changes
281}
282
Chris Lattner372fefe2009-08-23 01:03:30 +0000283void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000284 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000285 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000286 if (!foundErrors++)
Chris Lattner372fefe2009-08-23 01:03:30 +0000287 MF->print(*OS);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000288 *OS << "*** Bad machine code: " << msg << " ***\n"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000289 << "- function: " << MF->getFunction()->getNameStr() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000290}
291
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000292void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000293 assert(MBB);
294 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000295 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000296 << " " << (void*)MBB
Dan Gohman0ba90f32009-10-31 20:19:03 +0000297 << " (BB#" << MBB->getNumber() << ")\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000298}
299
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000300void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000301 assert(MI);
302 report(msg, MI->getParent());
303 *OS << "- instruction: ";
Chris Lattner705e07f2009-08-23 03:41:05 +0000304 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000305}
306
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000307void MachineVerifier::report(const char *msg,
308 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000309 assert(MO);
310 report(msg, MO->getParent());
311 *OS << "- operand " << MONum << ": ";
312 MO->print(*OS, TM);
313 *OS << "\n";
314}
315
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000316void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000317 BBInfo &MInfo = MBBInfoMap[MBB];
318 if (!MInfo.reachable) {
319 MInfo.reachable = true;
320 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
321 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
322 markReachable(*SuI);
323 }
324}
325
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000326void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000327 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000328
329 // A sub-register of a reserved register is also reserved
330 for (int Reg = regsReserved.find_first(); Reg>=0;
331 Reg = regsReserved.find_next(Reg)) {
332 for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
333 // FIXME: This should probably be:
334 // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
335 regsReserved.set(*Sub);
336 }
337 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000338 markReachable(&MF->front());
339}
340
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000341// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000342static bool matchPair(MachineBasicBlock::const_succ_iterator i,
343 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000344 if (*i == a)
345 return *++i == b;
346 if (*i == b)
347 return *++i == a;
348 return false;
349}
350
351void
352MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Dan Gohman27920592009-08-27 02:43:49 +0000353 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
354
Dan Gohman27920592009-08-27 02:43:49 +0000355 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
356 MachineBasicBlock *TBB = 0, *FBB = 0;
357 SmallVector<MachineOperand, 4> Cond;
358 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
359 TBB, FBB, Cond)) {
360 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
361 // check whether its answers match up with reality.
362 if (!TBB && !FBB) {
363 // Block falls through to its successor.
364 MachineFunction::const_iterator MBBI = MBB;
365 ++MBBI;
366 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000367 // It's possible that the block legitimately ends with a noreturn
368 // call or an unreachable, in which case it won't actually fall
369 // out the bottom of the function.
370 } else if (MBB->succ_empty()) {
371 // It's possible that the block legitimately ends with a noreturn
372 // call or an unreachable, in which case it won't actuall fall
373 // out of the block.
Dan Gohman27920592009-08-27 02:43:49 +0000374 } else if (MBB->succ_size() != 1) {
375 report("MBB exits via unconditional fall-through but doesn't have "
376 "exactly one CFG successor!", MBB);
377 } else if (MBB->succ_begin()[0] != MBBI) {
378 report("MBB exits via unconditional fall-through but its successor "
379 "differs from its CFG successor!", MBB);
380 }
Evan Cheng86050dc2010-06-18 23:09:54 +0000381 if (!MBB->empty() && MBB->back().getDesc().isBarrier() &&
382 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000383 report("MBB exits via unconditional fall-through but ends with a "
384 "barrier instruction!", MBB);
385 }
386 if (!Cond.empty()) {
387 report("MBB exits via unconditional fall-through but has a condition!",
388 MBB);
389 }
390 } else if (TBB && !FBB && Cond.empty()) {
391 // Block unconditionally branches somewhere.
392 if (MBB->succ_size() != 1) {
393 report("MBB exits via unconditional branch but doesn't have "
394 "exactly one CFG successor!", MBB);
395 } else if (MBB->succ_begin()[0] != TBB) {
396 report("MBB exits via unconditional branch but the CFG "
397 "successor doesn't match the actual successor!", MBB);
398 }
399 if (MBB->empty()) {
400 report("MBB exits via unconditional branch but doesn't contain "
401 "any instructions!", MBB);
402 } else if (!MBB->back().getDesc().isBarrier()) {
403 report("MBB exits via unconditional branch but doesn't end with a "
404 "barrier instruction!", MBB);
405 } else if (!MBB->back().getDesc().isTerminator()) {
406 report("MBB exits via unconditional branch but the branch isn't a "
407 "terminator instruction!", MBB);
408 }
409 } else if (TBB && !FBB && !Cond.empty()) {
410 // Block conditionally branches somewhere, otherwise falls through.
411 MachineFunction::const_iterator MBBI = MBB;
412 ++MBBI;
413 if (MBBI == MF->end()) {
414 report("MBB conditionally falls through out of function!", MBB);
415 } if (MBB->succ_size() != 2) {
416 report("MBB exits via conditional branch/fall-through but doesn't have "
417 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000418 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000419 report("MBB exits via conditional branch/fall-through but the CFG "
420 "successors don't match the actual successors!", MBB);
421 }
422 if (MBB->empty()) {
423 report("MBB exits via conditional branch/fall-through but doesn't "
424 "contain any instructions!", MBB);
425 } else if (MBB->back().getDesc().isBarrier()) {
426 report("MBB exits via conditional branch/fall-through but ends with a "
427 "barrier instruction!", MBB);
428 } else if (!MBB->back().getDesc().isTerminator()) {
429 report("MBB exits via conditional branch/fall-through but the branch "
430 "isn't a terminator instruction!", MBB);
431 }
432 } else if (TBB && FBB) {
433 // Block conditionally branches somewhere, otherwise branches
434 // somewhere else.
435 if (MBB->succ_size() != 2) {
436 report("MBB exits via conditional branch/branch but doesn't have "
437 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000438 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000439 report("MBB exits via conditional branch/branch but the CFG "
440 "successors don't match the actual successors!", MBB);
441 }
442 if (MBB->empty()) {
443 report("MBB exits via conditional branch/branch but doesn't "
444 "contain any instructions!", MBB);
445 } else if (!MBB->back().getDesc().isBarrier()) {
446 report("MBB exits via conditional branch/branch but doesn't end with a "
447 "barrier instruction!", MBB);
448 } else if (!MBB->back().getDesc().isTerminator()) {
449 report("MBB exits via conditional branch/branch but the branch "
450 "isn't a terminator instruction!", MBB);
451 }
452 if (Cond.empty()) {
453 report("MBB exits via conditinal branch/branch but there's no "
454 "condition!", MBB);
455 }
456 } else {
457 report("AnalyzeBranch returned invalid data!", MBB);
458 }
459 }
460
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000461 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000462 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000463 E = MBB->livein_end(); I != E; ++I) {
464 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
465 report("MBB live-in list contains non-physical register", MBB);
466 continue;
467 }
468 regsLive.insert(*I);
469 for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++)
470 regsLive.insert(*R);
471 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000472 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000473
474 const MachineFrameInfo *MFI = MF->getFrameInfo();
475 assert(MFI && "Function has no frame info");
476 BitVector PR = MFI->getPristineRegs(MBB);
477 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
478 regsLive.insert(I);
479 for (const unsigned *R = TRI->getSubRegisters(I); *R; R++)
480 regsLive.insert(*R);
481 }
482
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000483 regsKilled.clear();
484 regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000485}
486
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000487void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000488 const TargetInstrDesc &TI = MI->getDesc();
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000489 if (MI->getNumOperands() < TI.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000490 report("Too few operands", MI);
491 *OS << TI.getNumOperands() << " operands expected, but "
492 << MI->getNumExplicitOperands() << " given.\n";
493 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000494
495 // Check the MachineMemOperands for basic consistency.
496 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
497 E = MI->memoperands_end(); I != E; ++I) {
498 if ((*I)->isLoad() && !TI.mayLoad())
499 report("Missing mayLoad flag", MI);
500 if ((*I)->isStore() && !TI.mayStore())
501 report("Missing mayStore flag", MI);
502 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000503}
504
505void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000506MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000507 const MachineInstr *MI = MO->getParent();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000508 const TargetInstrDesc &TI = MI->getDesc();
509
510 // The first TI.NumDefs operands must be explicit register defines
511 if (MONum < TI.getNumDefs()) {
512 if (!MO->isReg())
513 report("Explicit definition must be a register", MO, MONum);
514 else if (!MO->isDef())
515 report("Explicit definition marked as use", MO, MONum);
516 else if (MO->isImplicit())
517 report("Explicit definition marked as implicit", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000518 } else if (MONum < TI.getNumOperands()) {
519 if (MO->isReg()) {
520 if (MO->isDef())
521 report("Explicit operand marked as def", MO, MONum);
522 if (MO->isImplicit())
523 report("Explicit operand marked as implicit", MO, MONum);
524 }
525 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000526 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
527 if (MO->isReg() && !MO->isImplicit() && !TI.isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000528 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000529 }
530
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000531 switch (MO->getType()) {
532 case MachineOperand::MO_Register: {
533 const unsigned Reg = MO->getReg();
534 if (!Reg)
535 return;
536
537 // Check Live Variables.
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000538 if (MO->isUndef()) {
539 // An <undef> doesn't refer to any register, so just skip it.
540 } else if (MO->isUse()) {
541 regsLiveInButUnused.erase(Reg);
542
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000543 bool isKill = false;
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000544 unsigned defIdx;
545 if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
546 // A two-addr use counts as a kill if use and def are the same.
547 unsigned DefReg = MI->getOperand(defIdx).getReg();
548 if (Reg == DefReg) {
549 isKill = true;
550 // ANd in that case an explicit kill flag is not allowed.
551 if (MO->isKill())
Jakob Stoklund Olesenf7d3e692009-07-15 23:37:26 +0000552 report("Illegal kill flag on two-address instruction operand",
553 MO, MONum);
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000554 } else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
555 report("Two-address instruction operands must be identical",
556 MO, MONum);
557 }
558 } else
559 isKill = MO->isKill();
560
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000561 if (isKill) {
562 addRegWithSubRegs(regsKilled, Reg);
563
564 // Check that LiveVars knows this kill
565 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg)) {
566 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
567 if (std::find(VI.Kills.begin(),
568 VI.Kills.end(), MI) == VI.Kills.end())
569 report("Kill missing from LiveVariables", MO, MONum);
570 }
571 }
572
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000573 // Use of a dead register.
574 if (!regsLive.count(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000575 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
576 // Reserved registers may be used even when 'dead'.
577 if (!isReserved(Reg))
578 report("Using an undefined physical register", MO, MONum);
579 } else {
580 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
581 // We don't know which virtual registers are live in, so only complain
582 // if vreg was killed in this MBB. Otherwise keep track of vregs that
583 // must be live in. PHI instructions are handled separately.
584 if (MInfo.regsKilled.count(Reg))
585 report("Using a killed virtual register", MO, MONum);
Chris Lattner518bb532010-02-09 19:54:29 +0000586 else if (!MI->isPHI())
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000587 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
588 }
Duncan Sandse5567202009-05-16 03:28:54 +0000589 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000590 } else {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000591 assert(MO->isDef());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000592 // Register defined.
593 // TODO: verify that earlyclobber ops are not used.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000594 if (MO->isDead())
595 addRegWithSubRegs(regsDead, Reg);
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000596 else
597 addRegWithSubRegs(regsDefined, Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000598 }
599
600 // Check register classes.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000601 if (MONum < TI.getNumOperands() && !MO->isImplicit()) {
602 const TargetOperandInfo &TOI = TI.OpInfo[MONum];
603 unsigned SubIdx = MO->getSubReg();
604
605 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
606 unsigned sr = Reg;
607 if (SubIdx) {
608 unsigned s = TRI->getSubReg(Reg, SubIdx);
609 if (!s) {
610 report("Invalid subregister index for physical register",
611 MO, MONum);
612 return;
613 }
614 sr = s;
615 }
Chris Lattnercb778a82009-07-29 21:10:12 +0000616 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000617 if (!DRC->contains(sr)) {
618 report("Illegal physical register for instruction", MO, MONum);
619 *OS << TRI->getName(sr) << " is not a "
620 << DRC->getName() << " register.\n";
621 }
622 }
623 } else {
624 // Virtual register.
625 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
626 if (SubIdx) {
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000627 const TargetRegisterClass *SRC = RC->getSubRegisterRegClass(SubIdx);
628 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000629 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000630 *OS << "Register class " << RC->getName()
631 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000632 return;
633 }
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000634 RC = SRC;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000635 }
Chris Lattnercb778a82009-07-29 21:10:12 +0000636 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000637 if (RC != DRC && !RC->hasSuperClass(DRC)) {
638 report("Illegal virtual register for instruction", MO, MONum);
639 *OS << "Expected a " << DRC->getName() << " register, but got a "
640 << RC->getName() << " register\n";
641 }
642 }
643 }
644 }
645 break;
646 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000647
648 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000649 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
650 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000651 break;
652
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000653 default:
654 break;
655 }
656}
657
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000658void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000659 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
660 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000661 set_subtract(regsLive, regsKilled); regsKilled.clear();
662 set_subtract(regsLive, regsDead); regsDead.clear();
663 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000664}
665
666void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000667MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000668 MBBInfoMap[MBB].regsLiveOut = regsLive;
669 regsLive.clear();
670}
671
672// Calculate the largest possible vregsPassed sets. These are the registers that
673// can pass through an MBB live, but may not be live every time. It is assumed
674// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000675void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000676 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
677 // have any vregsPassed.
678 DenseSet<const MachineBasicBlock*> todo;
679 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
680 MFI != MFE; ++MFI) {
681 const MachineBasicBlock &MBB(*MFI);
682 BBInfo &MInfo = MBBInfoMap[&MBB];
683 if (!MInfo.reachable)
684 continue;
685 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
686 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
687 BBInfo &SInfo = MBBInfoMap[*SuI];
688 if (SInfo.addPassed(MInfo.regsLiveOut))
689 todo.insert(*SuI);
690 }
691 }
692
693 // Iteratively push vregsPassed to successors. This will converge to the same
694 // final state regardless of DenseSet iteration order.
695 while (!todo.empty()) {
696 const MachineBasicBlock *MBB = *todo.begin();
697 todo.erase(MBB);
698 BBInfo &MInfo = MBBInfoMap[MBB];
699 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
700 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
701 if (*SuI == MBB)
702 continue;
703 BBInfo &SInfo = MBBInfoMap[*SuI];
704 if (SInfo.addPassed(MInfo.vregsPassed))
705 todo.insert(*SuI);
706 }
707 }
708}
709
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000710// Calculate the set of virtual registers that must be passed through each basic
711// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000712// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000713void MachineVerifier::calcRegsRequired() {
714 // First push live-in regs to predecessors' vregsRequired.
715 DenseSet<const MachineBasicBlock*> todo;
716 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
717 MFI != MFE; ++MFI) {
718 const MachineBasicBlock &MBB(*MFI);
719 BBInfo &MInfo = MBBInfoMap[&MBB];
720 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
721 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
722 BBInfo &PInfo = MBBInfoMap[*PrI];
723 if (PInfo.addRequired(MInfo.vregsLiveIn))
724 todo.insert(*PrI);
725 }
726 }
727
728 // Iteratively push vregsRequired to predecessors. This will converge to the
729 // same final state regardless of DenseSet iteration order.
730 while (!todo.empty()) {
731 const MachineBasicBlock *MBB = *todo.begin();
732 todo.erase(MBB);
733 BBInfo &MInfo = MBBInfoMap[MBB];
734 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
735 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
736 if (*PrI == MBB)
737 continue;
738 BBInfo &SInfo = MBBInfoMap[*PrI];
739 if (SInfo.addRequired(MInfo.vregsRequired))
740 todo.insert(*PrI);
741 }
742 }
743}
744
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000745// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000746// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000747void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000748 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +0000749 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000750 DenseSet<const MachineBasicBlock*> seen;
751
752 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
753 unsigned Reg = BBI->getOperand(i).getReg();
754 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
755 if (!Pre->isSuccessor(MBB))
756 continue;
757 seen.insert(Pre);
758 BBInfo &PrInfo = MBBInfoMap[Pre];
759 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
760 report("PHI operand is not live-out from predecessor",
761 &BBI->getOperand(i), i);
762 }
763
764 // Did we see all predecessors?
765 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
766 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
767 if (!seen.count(*PrI)) {
768 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000769 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000770 << " is a predecessor according to the CFG.\n";
771 }
772 }
773 }
774}
775
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000776void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000777 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000778
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000779 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
780 MFI != MFE; ++MFI) {
781 BBInfo &MInfo = MBBInfoMap[MFI];
782
783 // Skip unreachable MBBs.
784 if (!MInfo.reachable)
785 continue;
786
787 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000788 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000789
790 // Now check LiveVariables info if available
791 if (LiveVars) {
792 calcRegsRequired();
793 verifyLiveVariables();
794 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000795}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000796
797void MachineVerifier::verifyLiveVariables() {
798 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
799 for (unsigned Reg = TargetRegisterInfo::FirstVirtualRegister,
800 RegE = MRI->getLastVirtReg()-1; Reg != RegE; ++Reg) {
801 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
802 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
803 MFI != MFE; ++MFI) {
804 BBInfo &MInfo = MBBInfoMap[MFI];
805
806 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
807 if (MInfo.vregsRequired.count(Reg)) {
808 if (!VI.AliveBlocks.test(MFI->getNumber())) {
809 report("LiveVariables: Block missing from AliveBlocks", MFI);
810 *OS << "Virtual register %reg" << Reg
811 << " must be live through the block.\n";
812 }
813 } else {
814 if (VI.AliveBlocks.test(MFI->getNumber())) {
815 report("LiveVariables: Block should not be in AliveBlocks", MFI);
816 *OS << "Virtual register %reg" << Reg
817 << " is not needed live through the block.\n";
818 }
819 }
820 }
821 }
822}
823
824