blob: 779fa9e33d95cb443d49c8d45591f39bb45be08d [file] [log] [blame]
Bill Wendling5567bb02010-08-19 18:52:17 +00001//===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Pass to verify generated machine code. The following is checked:
11//
12// Operand counts: All explicit operands must be present.
13//
14// Register classes: All physical and virtual register operands must be
15// compatible with the register class required by the instruction descriptor.
16//
17// Register live intervals: Registers must be defined only once, and must be
18// defined before use.
19//
20// The machine code verifier is enabled from LLVMTargetMachine.cpp with the
21// command-line option -verify-machineinstrs, or by defining the environment
22// variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
23// the verifier errors.
24//===----------------------------------------------------------------------===//
25
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000026#include "llvm/Function.h"
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +000027#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000028#include "llvm/CodeGen/LiveVariables.h"
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +000029#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000030#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman2dbc4c82009-10-07 17:36:00 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
34#include "llvm/CodeGen/Passes.h"
35#include "llvm/Target/TargetMachine.h"
36#include "llvm/Target/TargetRegisterInfo.h"
37#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000038#include "llvm/ADT/DenseSet.h"
39#include "llvm/ADT/SetOperations.h"
40#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000041#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000042#include "llvm/Support/ErrorHandling.h"
43#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000044using namespace llvm;
45
46namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000047 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000048
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000049 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000050 PASS(pass),
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000051 Banner(b),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000052 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000053 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000054
55 bool runOnMachineFunction(MachineFunction &MF);
56
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000057 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000058 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000059 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000060 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000061 const MachineFunction *MF;
62 const TargetMachine *TM;
63 const TargetRegisterInfo *TRI;
64 const MachineRegisterInfo *MRI;
65
66 unsigned foundErrors;
67
68 typedef SmallVector<unsigned, 16> RegVector;
69 typedef DenseSet<unsigned> RegSet;
70 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
71
72 BitVector regsReserved;
73 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000074 RegVector regsDefined, regsDead, regsKilled;
75 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000076
77 // Add Reg and any sub-registers to RV
78 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
79 RV.push_back(Reg);
80 if (TargetRegisterInfo::isPhysicalRegister(Reg))
81 for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
82 RV.push_back(*R);
83 }
84
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000085 struct BBInfo {
86 // Is this MBB reachable from the MF entry point?
87 bool reachable;
88
89 // Vregs that must be live in because they are used without being
90 // defined. Map value is the user.
91 RegMap vregsLiveIn;
92
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000093 // Regs killed in MBB. They may be defined again, and will then be in both
94 // regsKilled and regsLiveOut.
95 RegSet regsKilled;
96
97 // Regs defined in MBB and live out. Note that vregs passing through may
98 // be live out without being mentioned here.
99 RegSet regsLiveOut;
100
101 // Vregs that pass through MBB untouched. This set is disjoint from
102 // regsKilled and regsLiveOut.
103 RegSet vregsPassed;
104
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000105 // Vregs that must pass through MBB because they are needed by a successor
106 // block. This set is disjoint from regsLiveOut.
107 RegSet vregsRequired;
108
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000109 BBInfo() : reachable(false) {}
110
111 // Add register to vregsPassed if it belongs there. Return true if
112 // anything changed.
113 bool addPassed(unsigned Reg) {
114 if (!TargetRegisterInfo::isVirtualRegister(Reg))
115 return false;
116 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
117 return false;
118 return vregsPassed.insert(Reg).second;
119 }
120
121 // Same for a full set.
122 bool addPassed(const RegSet &RS) {
123 bool changed = false;
124 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
125 if (addPassed(*I))
126 changed = true;
127 return changed;
128 }
129
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000130 // Add register to vregsRequired if it belongs there. Return true if
131 // anything changed.
132 bool addRequired(unsigned Reg) {
133 if (!TargetRegisterInfo::isVirtualRegister(Reg))
134 return false;
135 if (regsLiveOut.count(Reg))
136 return false;
137 return vregsRequired.insert(Reg).second;
138 }
139
140 // Same for a full set.
141 bool addRequired(const RegSet &RS) {
142 bool changed = false;
143 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
144 if (addRequired(*I))
145 changed = true;
146 return changed;
147 }
148
149 // Same for a full map.
150 bool addRequired(const RegMap &RM) {
151 bool changed = false;
152 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
153 if (addRequired(I->first))
154 changed = true;
155 return changed;
156 }
157
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000158 // Live-out registers are either in regsLiveOut or vregsPassed.
159 bool isLiveOut(unsigned Reg) const {
160 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
161 }
162 };
163
164 // Extra register info per MBB.
165 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
166
167 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000168 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000169 }
170
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000171 // Analysis information if available
172 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000173 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000174 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000175 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000176
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000177 void visitMachineFunctionBefore();
178 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
179 void visitMachineInstrBefore(const MachineInstr *MI);
180 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
181 void visitMachineInstrAfter(const MachineInstr *MI);
182 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
183 void visitMachineFunctionAfter();
184
185 void report(const char *msg, const MachineFunction *MF);
186 void report(const char *msg, const MachineBasicBlock *MBB);
187 void report(const char *msg, const MachineInstr *MI);
188 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
189
190 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000191 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000192 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000193
194 void calcRegsRequired();
195 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000196 void verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000197 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000198
199 struct MachineVerifierPass : public MachineFunctionPass {
200 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000201 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000202
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000203 MachineVerifierPass(const char *b = 0)
204 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000205 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
206 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000207
208 void getAnalysisUsage(AnalysisUsage &AU) const {
209 AU.setPreservesAll();
210 MachineFunctionPass::getAnalysisUsage(AU);
211 }
212
213 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000214 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000215 return false;
216 }
217 };
218
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000219}
220
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000221char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000222INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000223 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000224
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000225FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
226 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000227}
228
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000229void MachineFunction::verify(Pass *p, const char *Banner) const {
230 MachineVerifier(p, Banner)
231 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000232}
233
Chris Lattner17e9edc2009-08-23 02:51:22 +0000234bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
235 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000236 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000237 std::string ErrorInfo;
238 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
239 raw_fd_ostream::F_Append);
240 if (!ErrorInfo.empty()) {
241 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
242 exit(1);
243 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000244
Chris Lattner17e9edc2009-08-23 02:51:22 +0000245 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000246 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000247 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000248 }
249
250 foundErrors = 0;
251
252 this->MF = &MF;
253 TM = &MF.getTarget();
254 TRI = TM->getRegisterInfo();
255 MRI = &MF.getRegInfo();
256
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000257 LiveVars = NULL;
258 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000259 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000260 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000261 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000262 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000263 // We don't want to verify LiveVariables if LiveIntervals is available.
264 if (!LiveInts)
265 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000266 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000267 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000268 }
269
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000270 visitMachineFunctionBefore();
271 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
272 MFI!=MFE; ++MFI) {
273 visitMachineBasicBlockBefore(MFI);
274 for (MachineBasicBlock::const_iterator MBBI = MFI->begin(),
275 MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
276 visitMachineInstrBefore(MBBI);
277 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
278 visitMachineOperand(&MBBI->getOperand(I), I);
279 visitMachineInstrAfter(MBBI);
280 }
281 visitMachineBasicBlockAfter(MFI);
282 }
283 visitMachineFunctionAfter();
284
Chris Lattner17e9edc2009-08-23 02:51:22 +0000285 if (OutFile)
286 delete OutFile;
287 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000288 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000289
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000290 // Clean up.
291 regsLive.clear();
292 regsDefined.clear();
293 regsDead.clear();
294 regsKilled.clear();
295 regsLiveInButUnused.clear();
296 MBBInfoMap.clear();
297
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000298 return false; // no changes
299}
300
Chris Lattner372fefe2009-08-23 01:03:30 +0000301void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000302 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000303 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000304 if (!foundErrors++) {
305 if (Banner)
306 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000307 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000308 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000309 *OS << "*** Bad machine code: " << msg << " ***\n"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000310 << "- function: " << MF->getFunction()->getNameStr() << "\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 MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000314 assert(MBB);
315 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000316 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000317 << " " << (void*)MBB
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000318 << " (BB#" << MBB->getNumber() << ")";
319 if (Indexes)
320 *OS << " [" << Indexes->getMBBStartIdx(MBB)
321 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
322 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000323}
324
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000325void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000326 assert(MI);
327 report(msg, MI->getParent());
328 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000329 if (Indexes && Indexes->hasIndex(MI))
330 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000331 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000332}
333
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000334void MachineVerifier::report(const char *msg,
335 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000336 assert(MO);
337 report(msg, MO->getParent());
338 *OS << "- operand " << MONum << ": ";
339 MO->print(*OS, TM);
340 *OS << "\n";
341}
342
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000343void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000344 BBInfo &MInfo = MBBInfoMap[MBB];
345 if (!MInfo.reachable) {
346 MInfo.reachable = true;
347 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
348 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
349 markReachable(*SuI);
350 }
351}
352
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000353void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000354 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000355
356 // A sub-register of a reserved register is also reserved
357 for (int Reg = regsReserved.find_first(); Reg>=0;
358 Reg = regsReserved.find_next(Reg)) {
359 for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
360 // FIXME: This should probably be:
361 // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
362 regsReserved.set(*Sub);
363 }
364 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000365 markReachable(&MF->front());
366}
367
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000368// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000369static bool matchPair(MachineBasicBlock::const_succ_iterator i,
370 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000371 if (*i == a)
372 return *++i == b;
373 if (*i == b)
374 return *++i == a;
375 return false;
376}
377
378void
379MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Dan Gohman27920592009-08-27 02:43:49 +0000380 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
381
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000382 // Count the number of landing pad successors.
383 unsigned LandingPadSuccs = 0;
384 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
385 E = MBB->succ_end(); I != E; ++I)
386 LandingPadSuccs += (*I)->isLandingPad();
387 if (LandingPadSuccs > 1)
388 report("MBB has more than one landing pad successor", MBB);
389
Dan Gohman27920592009-08-27 02:43:49 +0000390 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
391 MachineBasicBlock *TBB = 0, *FBB = 0;
392 SmallVector<MachineOperand, 4> Cond;
393 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
394 TBB, FBB, Cond)) {
395 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
396 // check whether its answers match up with reality.
397 if (!TBB && !FBB) {
398 // Block falls through to its successor.
399 MachineFunction::const_iterator MBBI = MBB;
400 ++MBBI;
401 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000402 // It's possible that the block legitimately ends with a noreturn
403 // call or an unreachable, in which case it won't actually fall
404 // out the bottom of the function.
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000405 } else if (MBB->succ_size() == LandingPadSuccs) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000406 // It's possible that the block legitimately ends with a noreturn
407 // call or an unreachable, in which case it won't actuall fall
408 // out of the block.
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000409 } else if (MBB->succ_size() != 1+LandingPadSuccs) {
Dan Gohman27920592009-08-27 02:43:49 +0000410 report("MBB exits via unconditional fall-through but doesn't have "
411 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000412 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000413 report("MBB exits via unconditional fall-through but its successor "
414 "differs from its CFG successor!", MBB);
415 }
Evan Cheng86050dc2010-06-18 23:09:54 +0000416 if (!MBB->empty() && MBB->back().getDesc().isBarrier() &&
417 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000418 report("MBB exits via unconditional fall-through but ends with a "
419 "barrier instruction!", MBB);
420 }
421 if (!Cond.empty()) {
422 report("MBB exits via unconditional fall-through but has a condition!",
423 MBB);
424 }
425 } else if (TBB && !FBB && Cond.empty()) {
426 // Block unconditionally branches somewhere.
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000427 if (MBB->succ_size() != 1+LandingPadSuccs) {
Dan Gohman27920592009-08-27 02:43:49 +0000428 report("MBB exits via unconditional branch but doesn't have "
429 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000430 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000431 report("MBB exits via unconditional branch but the CFG "
432 "successor doesn't match the actual successor!", MBB);
433 }
434 if (MBB->empty()) {
435 report("MBB exits via unconditional branch but doesn't contain "
436 "any instructions!", MBB);
437 } else if (!MBB->back().getDesc().isBarrier()) {
438 report("MBB exits via unconditional branch but doesn't end with a "
439 "barrier instruction!", MBB);
440 } else if (!MBB->back().getDesc().isTerminator()) {
441 report("MBB exits via unconditional branch but the branch isn't a "
442 "terminator instruction!", MBB);
443 }
444 } else if (TBB && !FBB && !Cond.empty()) {
445 // Block conditionally branches somewhere, otherwise falls through.
446 MachineFunction::const_iterator MBBI = MBB;
447 ++MBBI;
448 if (MBBI == MF->end()) {
449 report("MBB conditionally falls through out of function!", MBB);
450 } if (MBB->succ_size() != 2) {
451 report("MBB exits via conditional branch/fall-through but doesn't have "
452 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000453 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000454 report("MBB exits via conditional branch/fall-through but the CFG "
455 "successors don't match the actual successors!", MBB);
456 }
457 if (MBB->empty()) {
458 report("MBB exits via conditional branch/fall-through but doesn't "
459 "contain any instructions!", MBB);
460 } else if (MBB->back().getDesc().isBarrier()) {
461 report("MBB exits via conditional branch/fall-through but ends with a "
462 "barrier instruction!", MBB);
463 } else if (!MBB->back().getDesc().isTerminator()) {
464 report("MBB exits via conditional branch/fall-through but the branch "
465 "isn't a terminator instruction!", MBB);
466 }
467 } else if (TBB && FBB) {
468 // Block conditionally branches somewhere, otherwise branches
469 // somewhere else.
470 if (MBB->succ_size() != 2) {
471 report("MBB exits via conditional branch/branch but doesn't have "
472 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000473 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000474 report("MBB exits via conditional branch/branch but the CFG "
475 "successors don't match the actual successors!", MBB);
476 }
477 if (MBB->empty()) {
478 report("MBB exits via conditional branch/branch but doesn't "
479 "contain any instructions!", MBB);
480 } else if (!MBB->back().getDesc().isBarrier()) {
481 report("MBB exits via conditional branch/branch but doesn't end with a "
482 "barrier instruction!", MBB);
483 } else if (!MBB->back().getDesc().isTerminator()) {
484 report("MBB exits via conditional branch/branch but the branch "
485 "isn't a terminator instruction!", MBB);
486 }
487 if (Cond.empty()) {
488 report("MBB exits via conditinal branch/branch but there's no "
489 "condition!", MBB);
490 }
491 } else {
492 report("AnalyzeBranch returned invalid data!", MBB);
493 }
494 }
495
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000496 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000497 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000498 E = MBB->livein_end(); I != E; ++I) {
499 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
500 report("MBB live-in list contains non-physical register", MBB);
501 continue;
502 }
503 regsLive.insert(*I);
504 for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++)
505 regsLive.insert(*R);
506 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000507 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000508
509 const MachineFrameInfo *MFI = MF->getFrameInfo();
510 assert(MFI && "Function has no frame info");
511 BitVector PR = MFI->getPristineRegs(MBB);
512 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
513 regsLive.insert(I);
514 for (const unsigned *R = TRI->getSubRegisters(I); *R; R++)
515 regsLive.insert(*R);
516 }
517
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000518 regsKilled.clear();
519 regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000520}
521
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000522void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000523 const TargetInstrDesc &TI = MI->getDesc();
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000524 if (MI->getNumOperands() < TI.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000525 report("Too few operands", MI);
526 *OS << TI.getNumOperands() << " operands expected, but "
527 << MI->getNumExplicitOperands() << " given.\n";
528 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000529
530 // Check the MachineMemOperands for basic consistency.
531 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
532 E = MI->memoperands_end(); I != E; ++I) {
533 if ((*I)->isLoad() && !TI.mayLoad())
534 report("Missing mayLoad flag", MI);
535 if ((*I)->isStore() && !TI.mayStore())
536 report("Missing mayStore flag", MI);
537 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000538
539 // Debug values must not have a slot index.
540 // Other instructions must have one.
541 if (LiveInts) {
542 bool mapped = !LiveInts->isNotInMIMap(MI);
543 if (MI->isDebugValue()) {
544 if (mapped)
545 report("Debug instruction has a slot index", MI);
546 } else {
547 if (!mapped)
548 report("Missing slot index", MI);
549 }
550 }
551
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000552}
553
554void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000555MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000556 const MachineInstr *MI = MO->getParent();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000557 const TargetInstrDesc &TI = MI->getDesc();
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000558 const TargetOperandInfo &TOI = TI.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000559
560 // The first TI.NumDefs operands must be explicit register defines
561 if (MONum < TI.getNumDefs()) {
562 if (!MO->isReg())
563 report("Explicit definition must be a register", MO, MONum);
564 else if (!MO->isDef())
565 report("Explicit definition marked as use", MO, MONum);
566 else if (MO->isImplicit())
567 report("Explicit definition marked as implicit", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000568 } else if (MONum < TI.getNumOperands()) {
Eric Christopher113a06c2010-11-17 00:55:36 +0000569 // Don't check if it's the last operand in a variadic instruction. See,
570 // e.g., LDM_RET in the arm back end.
571 if (MO->isReg() && !(TI.isVariadic() && MONum == TI.getNumOperands()-1)) {
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000572 if (MO->isDef() && !TOI.isOptionalDef())
573 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000574 if (MO->isImplicit())
575 report("Explicit operand marked as implicit", MO, MONum);
576 }
577 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000578 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
579 if (MO->isReg() && !MO->isImplicit() && !TI.isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000580 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000581 }
582
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000583 switch (MO->getType()) {
584 case MachineOperand::MO_Register: {
585 const unsigned Reg = MO->getReg();
586 if (!Reg)
587 return;
588
589 // Check Live Variables.
Cameron Zwarich8ec88ba2010-12-20 00:08:10 +0000590 if (MI->isDebugValue()) {
591 // Liveness checks are not valid for debug values.
592 } else if (MO->isUndef()) {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000593 // An <undef> doesn't refer to any register, so just skip it.
594 } else if (MO->isUse()) {
595 regsLiveInButUnused.erase(Reg);
596
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000597 bool isKill = false;
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000598 unsigned defIdx;
599 if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
600 // A two-addr use counts as a kill if use and def are the same.
601 unsigned DefReg = MI->getOperand(defIdx).getReg();
602 if (Reg == DefReg) {
603 isKill = true;
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000604 // And in that case an explicit kill flag is not allowed.
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000605 if (MO->isKill())
Jakob Stoklund Olesenf7d3e692009-07-15 23:37:26 +0000606 report("Illegal kill flag on two-address instruction operand",
607 MO, MONum);
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000608 } else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
609 report("Two-address instruction operands must be identical",
610 MO, MONum);
611 }
612 } else
613 isKill = MO->isKill();
614
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000615 if (isKill)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000616 addRegWithSubRegs(regsKilled, Reg);
617
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000618 // Check that LiveVars knows this kill.
619 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
620 MO->isKill()) {
621 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
622 if (std::find(VI.Kills.begin(),
623 VI.Kills.end(), MI) == VI.Kills.end())
624 report("Kill missing from LiveVariables", MO, MONum);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000625 }
626
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000627 // Check LiveInts liveness and kill.
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000628 if (TargetRegisterInfo::isVirtualRegister(Reg) &&
629 LiveInts && !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000630 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getUseIndex();
631 if (LiveInts->hasInterval(Reg)) {
632 const LiveInterval &LI = LiveInts->getInterval(Reg);
633 if (!LI.liveAt(UseIdx)) {
634 report("No live range at use", MO, MONum);
635 *OS << UseIdx << " is not live in " << LI << '\n';
636 }
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000637 // Verify isKill == LI.killedAt.
Jakob Stoklund Olesen2f3a4aa2010-12-17 19:18:41 +0000638 // Two-address instrs don't have kill flags on the tied operands, and
639 // we even allow
640 // %r1 = add %r1, %r1
641 // without a kill flag on the untied operand.
642 // MI->findRegisterUseOperandIdx finds the first operand using reg.
643 if (!MI->isRegTiedToDefOperand(MI->findRegisterUseOperandIdx(Reg))) {
Jakob Stoklund Olesen962c7102010-11-01 23:59:53 +0000644 // MI could kill register without a kill flag on MO.
645 bool miKill = MI->killsRegister(Reg);
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000646 bool liKill = LI.killedAt(UseIdx.getDefIndex());
Jakob Stoklund Olesen962c7102010-11-01 23:59:53 +0000647 if (miKill && !liKill) {
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000648 report("Live range continues after kill flag", MO, MONum);
649 *OS << "Live range: " << LI << '\n';
650 }
Jakob Stoklund Olesen962c7102010-11-01 23:59:53 +0000651 if (!miKill && liKill) {
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000652 report("Live range ends without kill flag", MO, MONum);
653 *OS << "Live range: " << LI << '\n';
654 }
655 }
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000656 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000657 report("Virtual register has no Live interval", MO, MONum);
658 }
659 }
660
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000661 // Use of a dead register.
662 if (!regsLive.count(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000663 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
664 // Reserved registers may be used even when 'dead'.
665 if (!isReserved(Reg))
666 report("Using an undefined physical register", MO, MONum);
667 } else {
668 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
669 // We don't know which virtual registers are live in, so only complain
670 // if vreg was killed in this MBB. Otherwise keep track of vregs that
671 // must be live in. PHI instructions are handled separately.
672 if (MInfo.regsKilled.count(Reg))
673 report("Using a killed virtual register", MO, MONum);
Chris Lattner518bb532010-02-09 19:54:29 +0000674 else if (!MI->isPHI())
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000675 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
676 }
Duncan Sandse5567202009-05-16 03:28:54 +0000677 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000678 } else {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000679 assert(MO->isDef());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000680 // Register defined.
681 // TODO: verify that earlyclobber ops are not used.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000682 if (MO->isDead())
683 addRegWithSubRegs(regsDead, Reg);
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000684 else
685 addRegWithSubRegs(regsDefined, Reg);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000686
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000687 // Check LiveInts for a live range, but only for virtual registers.
688 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
689 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000690 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getDefIndex();
691 if (LiveInts->hasInterval(Reg)) {
692 const LiveInterval &LI = LiveInts->getInterval(Reg);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000693 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
694 assert(VNI && "NULL valno is not allowed");
Cameron Zwarich1b031dd2010-12-19 23:50:53 +0000695 if (VNI->def != DefIdx && !MO->isEarlyClobber()) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000696 report("Inconsistent valno->def", MO, MONum);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000697 *OS << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000698 << DefIdx << " in " << LI << '\n';
699 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000700 } else {
701 report("No live range at def", MO, MONum);
702 *OS << DefIdx << " is not live in " << LI << '\n';
703 }
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000704 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000705 report("Virtual register has no Live interval", MO, MONum);
706 }
707 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000708 }
709
710 // Check register classes.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000711 if (MONum < TI.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000712 unsigned SubIdx = MO->getSubReg();
713
714 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
715 unsigned sr = Reg;
716 if (SubIdx) {
717 unsigned s = TRI->getSubReg(Reg, SubIdx);
718 if (!s) {
719 report("Invalid subregister index for physical register",
720 MO, MONum);
721 return;
722 }
723 sr = s;
724 }
Chris Lattnercb778a82009-07-29 21:10:12 +0000725 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000726 if (!DRC->contains(sr)) {
727 report("Illegal physical register for instruction", MO, MONum);
728 *OS << TRI->getName(sr) << " is not a "
729 << DRC->getName() << " register.\n";
730 }
731 }
732 } else {
733 // Virtual register.
734 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
735 if (SubIdx) {
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000736 const TargetRegisterClass *SRC = RC->getSubRegisterRegClass(SubIdx);
737 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000738 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000739 *OS << "Register class " << RC->getName()
740 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000741 return;
742 }
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000743 RC = SRC;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000744 }
Chris Lattnercb778a82009-07-29 21:10:12 +0000745 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000746 if (RC != DRC && !RC->hasSuperClass(DRC)) {
747 report("Illegal virtual register for instruction", MO, MONum);
748 *OS << "Expected a " << DRC->getName() << " register, but got a "
749 << RC->getName() << " register\n";
750 }
751 }
752 }
753 }
754 break;
755 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000756
757 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000758 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
759 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000760 break;
761
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000762 case MachineOperand::MO_FrameIndex:
763 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
764 LiveInts && !LiveInts->isNotInMIMap(MI)) {
765 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
766 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
767 if (TI.mayLoad() && !LI.liveAt(Idx.getUseIndex())) {
768 report("Instruction loads from dead spill slot", MO, MONum);
769 *OS << "Live stack: " << LI << '\n';
770 }
771 if (TI.mayStore() && !LI.liveAt(Idx.getDefIndex())) {
772 report("Instruction stores to dead spill slot", MO, MONum);
773 *OS << "Live stack: " << LI << '\n';
774 }
775 }
776 break;
777
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000778 default:
779 break;
780 }
781}
782
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000783void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000784 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
785 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000786 set_subtract(regsLive, regsKilled); regsKilled.clear();
787 set_subtract(regsLive, regsDead); regsDead.clear();
788 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000789}
790
791void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000792MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000793 MBBInfoMap[MBB].regsLiveOut = regsLive;
794 regsLive.clear();
795}
796
797// Calculate the largest possible vregsPassed sets. These are the registers that
798// can pass through an MBB live, but may not be live every time. It is assumed
799// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000800void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000801 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
802 // have any vregsPassed.
803 DenseSet<const MachineBasicBlock*> todo;
804 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
805 MFI != MFE; ++MFI) {
806 const MachineBasicBlock &MBB(*MFI);
807 BBInfo &MInfo = MBBInfoMap[&MBB];
808 if (!MInfo.reachable)
809 continue;
810 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
811 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
812 BBInfo &SInfo = MBBInfoMap[*SuI];
813 if (SInfo.addPassed(MInfo.regsLiveOut))
814 todo.insert(*SuI);
815 }
816 }
817
818 // Iteratively push vregsPassed to successors. This will converge to the same
819 // final state regardless of DenseSet iteration order.
820 while (!todo.empty()) {
821 const MachineBasicBlock *MBB = *todo.begin();
822 todo.erase(MBB);
823 BBInfo &MInfo = MBBInfoMap[MBB];
824 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
825 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
826 if (*SuI == MBB)
827 continue;
828 BBInfo &SInfo = MBBInfoMap[*SuI];
829 if (SInfo.addPassed(MInfo.vregsPassed))
830 todo.insert(*SuI);
831 }
832 }
833}
834
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000835// Calculate the set of virtual registers that must be passed through each basic
836// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000837// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000838void MachineVerifier::calcRegsRequired() {
839 // First push live-in regs to predecessors' vregsRequired.
840 DenseSet<const MachineBasicBlock*> todo;
841 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
842 MFI != MFE; ++MFI) {
843 const MachineBasicBlock &MBB(*MFI);
844 BBInfo &MInfo = MBBInfoMap[&MBB];
845 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
846 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
847 BBInfo &PInfo = MBBInfoMap[*PrI];
848 if (PInfo.addRequired(MInfo.vregsLiveIn))
849 todo.insert(*PrI);
850 }
851 }
852
853 // Iteratively push vregsRequired to predecessors. This will converge to the
854 // same final state regardless of DenseSet iteration order.
855 while (!todo.empty()) {
856 const MachineBasicBlock *MBB = *todo.begin();
857 todo.erase(MBB);
858 BBInfo &MInfo = MBBInfoMap[MBB];
859 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
860 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
861 if (*PrI == MBB)
862 continue;
863 BBInfo &SInfo = MBBInfoMap[*PrI];
864 if (SInfo.addRequired(MInfo.vregsRequired))
865 todo.insert(*PrI);
866 }
867 }
868}
869
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000870// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000871// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000872void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000873 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +0000874 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000875 DenseSet<const MachineBasicBlock*> seen;
876
877 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
878 unsigned Reg = BBI->getOperand(i).getReg();
879 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
880 if (!Pre->isSuccessor(MBB))
881 continue;
882 seen.insert(Pre);
883 BBInfo &PrInfo = MBBInfoMap[Pre];
884 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
885 report("PHI operand is not live-out from predecessor",
886 &BBI->getOperand(i), i);
887 }
888
889 // Did we see all predecessors?
890 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
891 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
892 if (!seen.count(*PrI)) {
893 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000894 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000895 << " is a predecessor according to the CFG.\n";
896 }
897 }
898 }
899}
900
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000901void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000902 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000903
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000904 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
905 MFI != MFE; ++MFI) {
906 BBInfo &MInfo = MBBInfoMap[MFI];
907
908 // Skip unreachable MBBs.
909 if (!MInfo.reachable)
910 continue;
911
912 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000913 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000914
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000915 // Now check liveness info if available
916 if (LiveVars || LiveInts)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000917 calcRegsRequired();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000918 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000919 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000920 if (LiveInts)
921 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000922}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000923
924void MachineVerifier::verifyLiveVariables() {
925 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
926 for (unsigned Reg = TargetRegisterInfo::FirstVirtualRegister,
927 RegE = MRI->getLastVirtReg()-1; Reg != RegE; ++Reg) {
928 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
929 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
930 MFI != MFE; ++MFI) {
931 BBInfo &MInfo = MBBInfoMap[MFI];
932
933 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
934 if (MInfo.vregsRequired.count(Reg)) {
935 if (!VI.AliveBlocks.test(MFI->getNumber())) {
936 report("LiveVariables: Block missing from AliveBlocks", MFI);
937 *OS << "Virtual register %reg" << Reg
938 << " must be live through the block.\n";
939 }
940 } else {
941 if (VI.AliveBlocks.test(MFI->getNumber())) {
942 report("LiveVariables: Block should not be in AliveBlocks", MFI);
943 *OS << "Virtual register %reg" << Reg
944 << " is not needed live through the block.\n";
945 }
946 }
947 }
948 }
949}
950
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000951void MachineVerifier::verifyLiveIntervals() {
952 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
953 for (LiveIntervals::const_iterator LVI = LiveInts->begin(),
954 LVE = LiveInts->end(); LVI != LVE; ++LVI) {
955 const LiveInterval &LI = *LVI->second;
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +0000956
957 // Spilling and splitting may leave unused registers around. Skip them.
958 if (MRI->use_empty(LI.reg))
959 continue;
960
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +0000961 // Physical registers have much weirdness going on, mostly from coalescing.
962 // We should probably fix it, but for now just ignore them.
963 if (TargetRegisterInfo::isPhysicalRegister(LI.reg))
964 continue;
965
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000966 assert(LVI->first == LI.reg && "Invalid reg to interval mapping");
967
968 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
969 I!=E; ++I) {
970 VNInfo *VNI = *I;
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000971 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000972
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000973 if (!DefVNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000974 if (!VNI->isUnused()) {
975 report("Valno not live at def and not marked unused", MF);
976 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
977 }
978 continue;
979 }
980
981 if (VNI->isUnused())
982 continue;
983
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000984 if (DefVNI != VNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000985 report("Live range at def has different valno", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000986 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +0000987 << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +0000988 continue;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000989 }
990
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +0000991 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
992 if (!MBB) {
993 report("Invalid definition index", MF);
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +0000994 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
995 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +0000996 continue;
997 }
998
999 if (VNI->isPHIDef()) {
1000 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1001 report("PHIDef value is not defined at MBB start", MF);
1002 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001003 << ", not at the beginning of BB#" << MBB->getNumber()
1004 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001005 }
1006 } else {
1007 // Non-PHI def.
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001008 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1009 if (!MI) {
1010 report("No instruction at def index", MF);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001011 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1012 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001013 } else if (!MI->modifiesRegister(LI.reg, TRI)) {
1014 report("Defining instruction does not modify register", MI);
1015 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1016 }
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001017
1018 bool isEarlyClobber = false;
1019 if (MI) {
1020 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1021 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1022 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() &&
1023 MOI->isEarlyClobber()) {
1024 isEarlyClobber = true;
1025 break;
1026 }
1027 }
1028 }
1029
1030 // Early clobber defs begin at USE slots, but other defs must begin at
1031 // DEF slots.
1032 if (isEarlyClobber) {
1033 if (!VNI->def.isUse()) {
1034 report("Early clobber def must be at a USE slot", MF);
1035 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1036 << " in " << LI << '\n';
1037 }
1038 } else if (!VNI->def.isDef()) {
1039 report("Non-PHI, non-early clobber def must be at a DEF slot", MF);
1040 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1041 << " in " << LI << '\n';
1042 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001043 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001044 }
1045
1046 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001047 const VNInfo *VNI = I->valno;
1048 assert(VNI && "Live range has no valno");
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001049
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001050 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001051 report("Foreign valno in live range", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001052 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001053 *OS << " has a valno not in " << LI << '\n';
1054 }
1055
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001056 if (VNI->isUnused()) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001057 report("Live range valno is marked unused", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001058 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001059 *OS << " in " << LI << '\n';
1060 }
1061
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001062 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1063 if (!MBB) {
1064 report("Bad start of live segment, no basic block", MF);
1065 I->print(*OS);
1066 *OS << " in " << LI << '\n';
1067 continue;
1068 }
1069 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1070 if (I->start != MBBStartIdx && I->start != VNI->def) {
1071 report("Live segment must begin at MBB entry or valno def", MBB);
1072 I->print(*OS);
1073 *OS << " in " << LI << '\n' << "Basic block starts at "
1074 << MBBStartIdx << '\n';
1075 }
1076
1077 const MachineBasicBlock *EndMBB =
1078 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1079 if (!EndMBB) {
1080 report("Bad end of live segment, no basic block", MF);
1081 I->print(*OS);
1082 *OS << " in " << LI << '\n';
1083 continue;
1084 }
1085 if (I->end != LiveInts->getMBBEndIdx(EndMBB)) {
1086 // The live segment is ending inside EndMBB
1087 const MachineInstr *MI =
1088 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1089 if (!MI) {
1090 report("Live segment doesn't end at a valid instruction", EndMBB);
1091 I->print(*OS);
1092 *OS << " in " << LI << '\n' << "Basic block starts at "
1093 << MBBStartIdx << '\n';
1094 } else if (TargetRegisterInfo::isVirtualRegister(LI.reg) &&
1095 !MI->readsVirtualRegister(LI.reg)) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001096 // A live range can end with either a redefinition, a kill flag on a
1097 // use, or a dead flag on a def.
1098 // FIXME: Should we check for each of these?
1099 bool hasDeadDef = false;
1100 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1101 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
Cameron Zwarich5e61f992010-12-20 02:59:51 +00001102 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() && MOI->isDead()) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001103 hasDeadDef = true;
1104 break;
1105 }
1106 }
1107
1108 if (!hasDeadDef) {
1109 report("Instruction killing live segment neither defines nor reads "
1110 "register", MI);
1111 I->print(*OS);
1112 *OS << " in " << LI << '\n';
1113 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001114 }
1115 }
1116
1117 // Now check all the basic blocks in this live segment.
1118 MachineFunction::const_iterator MFI = MBB;
1119 // Is LI live-in to MBB and not a PHIDef?
1120 if (I->start == VNI->def) {
1121 // Not live-in to any blocks.
1122 if (MBB == EndMBB)
1123 continue;
1124 // Skip this block.
1125 ++MFI;
1126 }
1127 for (;;) {
1128 assert(LiveInts->isLiveInToMBB(LI, MFI));
Jakob Stoklund Olesene459d552010-10-26 16:49:23 +00001129 // We don't know how to track physregs into a landing pad.
1130 if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1131 MFI->isLandingPad()) {
1132 if (&*MFI == EndMBB)
1133 break;
1134 ++MFI;
1135 continue;
1136 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001137 // Check that VNI is live-out of all predecessors.
1138 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1139 PE = MFI->pred_end(); PI != PE; ++PI) {
1140 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI).getPrevSlot();
1141 const VNInfo *PVNI = LI.getVNInfoAt(PEnd);
1142 if (!PVNI) {
1143 report("Register not marked live out of predecessor", *PI);
1144 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1145 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live at "
1146 << PEnd << " in " << LI << '\n';
1147 } else if (PVNI != VNI) {
1148 report("Different value live out of predecessor", *PI);
1149 *OS << "Valno #" << PVNI->id << " live out of BB#"
1150 << (*PI)->getNumber() << '@' << PEnd
1151 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1152 << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n';
1153 }
1154 }
1155 if (&*MFI == EndMBB)
1156 break;
1157 ++MFI;
1158 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001159 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001160
1161 // Check the LI only has one connected component.
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001162 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1163 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1164 unsigned NumComp = ConEQ.Classify(&LI);
1165 if (NumComp > 1) {
1166 report("Multiple connected components in live interval", MF);
1167 *OS << NumComp << " components in " << LI << '\n';
Jakob Stoklund Olesencb367772010-10-29 00:40:57 +00001168 for (unsigned comp = 0; comp != NumComp; ++comp) {
1169 *OS << comp << ": valnos";
1170 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1171 E = LI.vni_end(); I!=E; ++I)
1172 if (comp == ConEQ.getEqClass(*I))
1173 *OS << ' ' << (*I)->id;
1174 *OS << '\n';
1175 }
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001176 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001177 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001178 }
1179}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001180