blob: e798ba11ea92e9583986a99c4060402e8ea9b346 [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
Bill Wendlingd29052b2011-05-04 22:54:05 +000026#include "llvm/Instructions.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000027#include "llvm/Function.h"
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +000028#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000029#include "llvm/CodeGen/LiveVariables.h"
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +000030#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman2dbc4c82009-10-07 17:36:00 +000033#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
35#include "llvm/CodeGen/Passes.h"
Bill Wendlingd29052b2011-05-04 22:54:05 +000036#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000037#include "llvm/Target/TargetMachine.h"
38#include "llvm/Target/TargetRegisterInfo.h"
39#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000040#include "llvm/ADT/DenseSet.h"
41#include "llvm/ADT/SetOperations.h"
42#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000043#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000044#include "llvm/Support/ErrorHandling.h"
45#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000046using namespace llvm;
47
48namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000049 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000050
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000051 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000052 PASS(pass),
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000053 Banner(b),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000054 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000055 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000056
57 bool runOnMachineFunction(MachineFunction &MF);
58
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000059 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000060 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000061 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000062 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000063 const MachineFunction *MF;
64 const TargetMachine *TM;
Evan Cheng15993f82011-06-27 21:26:13 +000065 const TargetInstrInfo *TII;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000066 const TargetRegisterInfo *TRI;
67 const MachineRegisterInfo *MRI;
68
69 unsigned foundErrors;
70
71 typedef SmallVector<unsigned, 16> RegVector;
72 typedef DenseSet<unsigned> RegSet;
73 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
74
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000075 const MachineInstr *FirstTerminator;
76
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000077 BitVector regsReserved;
78 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000079 RegVector regsDefined, regsDead, regsKilled;
80 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000081
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000082 SlotIndex lastIndex;
83
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000084 // Add Reg and any sub-registers to RV
85 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
86 RV.push_back(Reg);
87 if (TargetRegisterInfo::isPhysicalRegister(Reg))
88 for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
89 RV.push_back(*R);
90 }
91
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000092 struct BBInfo {
93 // Is this MBB reachable from the MF entry point?
94 bool reachable;
95
96 // Vregs that must be live in because they are used without being
97 // defined. Map value is the user.
98 RegMap vregsLiveIn;
99
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000100 // Regs killed in MBB. They may be defined again, and will then be in both
101 // regsKilled and regsLiveOut.
102 RegSet regsKilled;
103
104 // Regs defined in MBB and live out. Note that vregs passing through may
105 // be live out without being mentioned here.
106 RegSet regsLiveOut;
107
108 // Vregs that pass through MBB untouched. This set is disjoint from
109 // regsKilled and regsLiveOut.
110 RegSet vregsPassed;
111
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000112 // Vregs that must pass through MBB because they are needed by a successor
113 // block. This set is disjoint from regsLiveOut.
114 RegSet vregsRequired;
115
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000116 BBInfo() : reachable(false) {}
117
118 // Add register to vregsPassed if it belongs there. Return true if
119 // anything changed.
120 bool addPassed(unsigned Reg) {
121 if (!TargetRegisterInfo::isVirtualRegister(Reg))
122 return false;
123 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
124 return false;
125 return vregsPassed.insert(Reg).second;
126 }
127
128 // Same for a full set.
129 bool addPassed(const RegSet &RS) {
130 bool changed = false;
131 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
132 if (addPassed(*I))
133 changed = true;
134 return changed;
135 }
136
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000137 // Add register to vregsRequired if it belongs there. Return true if
138 // anything changed.
139 bool addRequired(unsigned Reg) {
140 if (!TargetRegisterInfo::isVirtualRegister(Reg))
141 return false;
142 if (regsLiveOut.count(Reg))
143 return false;
144 return vregsRequired.insert(Reg).second;
145 }
146
147 // Same for a full set.
148 bool addRequired(const RegSet &RS) {
149 bool changed = false;
150 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
151 if (addRequired(*I))
152 changed = true;
153 return changed;
154 }
155
156 // Same for a full map.
157 bool addRequired(const RegMap &RM) {
158 bool changed = false;
159 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
160 if (addRequired(I->first))
161 changed = true;
162 return changed;
163 }
164
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000165 // Live-out registers are either in regsLiveOut or vregsPassed.
166 bool isLiveOut(unsigned Reg) const {
167 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
168 }
169 };
170
171 // Extra register info per MBB.
172 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
173
174 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000175 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000176 }
177
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000178 // Analysis information if available
179 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000180 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000181 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000182 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000183
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000184 void visitMachineFunctionBefore();
185 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
186 void visitMachineInstrBefore(const MachineInstr *MI);
187 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
188 void visitMachineInstrAfter(const MachineInstr *MI);
189 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
190 void visitMachineFunctionAfter();
191
192 void report(const char *msg, const MachineFunction *MF);
193 void report(const char *msg, const MachineBasicBlock *MBB);
194 void report(const char *msg, const MachineInstr *MI);
195 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
196
197 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000198 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000199 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000200
201 void calcRegsRequired();
202 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000203 void verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000204 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000205
206 struct MachineVerifierPass : public MachineFunctionPass {
207 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000208 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000209
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000210 MachineVerifierPass(const char *b = 0)
211 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000212 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
213 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000214
215 void getAnalysisUsage(AnalysisUsage &AU) const {
216 AU.setPreservesAll();
217 MachineFunctionPass::getAnalysisUsage(AU);
218 }
219
220 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000221 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000222 return false;
223 }
224 };
225
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000226}
227
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000228char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000229INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000230 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000231
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000232FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
233 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000234}
235
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000236void MachineFunction::verify(Pass *p, const char *Banner) const {
237 MachineVerifier(p, Banner)
238 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000239}
240
Chris Lattner17e9edc2009-08-23 02:51:22 +0000241bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
242 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000243 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000244 std::string ErrorInfo;
245 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
246 raw_fd_ostream::F_Append);
247 if (!ErrorInfo.empty()) {
248 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
249 exit(1);
250 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000251
Chris Lattner17e9edc2009-08-23 02:51:22 +0000252 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000253 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000254 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000255 }
256
257 foundErrors = 0;
258
259 this->MF = &MF;
260 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000261 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000262 TRI = TM->getRegisterInfo();
263 MRI = &MF.getRegInfo();
264
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000265 LiveVars = NULL;
266 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000267 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000268 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000269 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000270 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000271 // We don't want to verify LiveVariables if LiveIntervals is available.
272 if (!LiveInts)
273 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000274 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000275 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000276 }
277
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000278 visitMachineFunctionBefore();
279 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
280 MFI!=MFE; ++MFI) {
281 visitMachineBasicBlockBefore(MFI);
282 for (MachineBasicBlock::const_iterator MBBI = MFI->begin(),
283 MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000284 if (MBBI->getParent() != MFI) {
285 report("Bad instruction parent pointer", MFI);
286 *OS << "Instruction: " << *MBBI;
287 continue;
288 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000289 visitMachineInstrBefore(MBBI);
290 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
291 visitMachineOperand(&MBBI->getOperand(I), I);
292 visitMachineInstrAfter(MBBI);
293 }
294 visitMachineBasicBlockAfter(MFI);
295 }
296 visitMachineFunctionAfter();
297
Chris Lattner17e9edc2009-08-23 02:51:22 +0000298 if (OutFile)
299 delete OutFile;
300 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000301 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000302
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000303 // Clean up.
304 regsLive.clear();
305 regsDefined.clear();
306 regsDead.clear();
307 regsKilled.clear();
308 regsLiveInButUnused.clear();
309 MBBInfoMap.clear();
310
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000311 return false; // no changes
312}
313
Chris Lattner372fefe2009-08-23 01:03:30 +0000314void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000315 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000316 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000317 if (!foundErrors++) {
318 if (Banner)
319 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000320 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000321 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000322 *OS << "*** Bad machine code: " << msg << " ***\n"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000323 << "- function: " << MF->getFunction()->getNameStr() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000324}
325
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000326void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000327 assert(MBB);
328 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000329 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000330 << " " << (void*)MBB
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000331 << " (BB#" << MBB->getNumber() << ")";
332 if (Indexes)
333 *OS << " [" << Indexes->getMBBStartIdx(MBB)
334 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
335 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000336}
337
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000338void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000339 assert(MI);
340 report(msg, MI->getParent());
341 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000342 if (Indexes && Indexes->hasIndex(MI))
343 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000344 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000345}
346
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000347void MachineVerifier::report(const char *msg,
348 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000349 assert(MO);
350 report(msg, MO->getParent());
351 *OS << "- operand " << MONum << ": ";
352 MO->print(*OS, TM);
353 *OS << "\n";
354}
355
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000356void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000357 BBInfo &MInfo = MBBInfoMap[MBB];
358 if (!MInfo.reachable) {
359 MInfo.reachable = true;
360 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
361 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
362 markReachable(*SuI);
363 }
364}
365
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000366void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000367 lastIndex = SlotIndex();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000368 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000369
370 // A sub-register of a reserved register is also reserved
371 for (int Reg = regsReserved.find_first(); Reg>=0;
372 Reg = regsReserved.find_next(Reg)) {
373 for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
374 // FIXME: This should probably be:
375 // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
376 regsReserved.set(*Sub);
377 }
378 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000379 markReachable(&MF->front());
380}
381
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000382// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000383static bool matchPair(MachineBasicBlock::const_succ_iterator i,
384 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000385 if (*i == a)
386 return *++i == b;
387 if (*i == b)
388 return *++i == a;
389 return false;
390}
391
392void
393MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000394 FirstTerminator = 0;
395
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000396 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000397 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000398 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000399 E = MBB->succ_end(); I != E; ++I) {
400 if ((*I)->isLandingPad())
401 LandingPadSuccs.insert(*I);
402 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000403
404 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
405 const BasicBlock *BB = MBB->getBasicBlock();
406 if (LandingPadSuccs.size() > 1 &&
407 !(AsmInfo &&
408 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
409 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000410 report("MBB has more than one landing pad successor", MBB);
411
Dan Gohman27920592009-08-27 02:43:49 +0000412 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
413 MachineBasicBlock *TBB = 0, *FBB = 0;
414 SmallVector<MachineOperand, 4> Cond;
415 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
416 TBB, FBB, Cond)) {
417 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
418 // check whether its answers match up with reality.
419 if (!TBB && !FBB) {
420 // Block falls through to its successor.
421 MachineFunction::const_iterator MBBI = MBB;
422 ++MBBI;
423 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000424 // It's possible that the block legitimately ends with a noreturn
425 // call or an unreachable, in which case it won't actually fall
426 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000427 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000428 // It's possible that the block legitimately ends with a noreturn
429 // call or an unreachable, in which case it won't actuall fall
430 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000431 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000432 report("MBB exits via unconditional fall-through but doesn't have "
433 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000434 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000435 report("MBB exits via unconditional fall-through but its successor "
436 "differs from its CFG successor!", MBB);
437 }
Evan Cheng86050dc2010-06-18 23:09:54 +0000438 if (!MBB->empty() && MBB->back().getDesc().isBarrier() &&
439 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000440 report("MBB exits via unconditional fall-through but ends with a "
441 "barrier instruction!", MBB);
442 }
443 if (!Cond.empty()) {
444 report("MBB exits via unconditional fall-through but has a condition!",
445 MBB);
446 }
447 } else if (TBB && !FBB && Cond.empty()) {
448 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000449 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000450 report("MBB exits via unconditional branch but doesn't have "
451 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000452 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000453 report("MBB exits via unconditional branch but the CFG "
454 "successor doesn't match the actual successor!", MBB);
455 }
456 if (MBB->empty()) {
457 report("MBB exits via unconditional branch but doesn't contain "
458 "any instructions!", MBB);
459 } else if (!MBB->back().getDesc().isBarrier()) {
460 report("MBB exits via unconditional branch but doesn't end with a "
461 "barrier instruction!", MBB);
462 } else if (!MBB->back().getDesc().isTerminator()) {
463 report("MBB exits via unconditional branch but the branch isn't a "
464 "terminator instruction!", MBB);
465 }
466 } else if (TBB && !FBB && !Cond.empty()) {
467 // Block conditionally branches somewhere, otherwise falls through.
468 MachineFunction::const_iterator MBBI = MBB;
469 ++MBBI;
470 if (MBBI == MF->end()) {
471 report("MBB conditionally falls through out of function!", MBB);
472 } if (MBB->succ_size() != 2) {
473 report("MBB exits via conditional branch/fall-through but doesn't have "
474 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000475 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000476 report("MBB exits via conditional branch/fall-through but the CFG "
477 "successors don't match the actual successors!", MBB);
478 }
479 if (MBB->empty()) {
480 report("MBB exits via conditional branch/fall-through but doesn't "
481 "contain any instructions!", MBB);
482 } else if (MBB->back().getDesc().isBarrier()) {
483 report("MBB exits via conditional branch/fall-through but ends with a "
484 "barrier instruction!", MBB);
485 } else if (!MBB->back().getDesc().isTerminator()) {
486 report("MBB exits via conditional branch/fall-through but the branch "
487 "isn't a terminator instruction!", MBB);
488 }
489 } else if (TBB && FBB) {
490 // Block conditionally branches somewhere, otherwise branches
491 // somewhere else.
492 if (MBB->succ_size() != 2) {
493 report("MBB exits via conditional branch/branch but doesn't have "
494 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000495 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000496 report("MBB exits via conditional branch/branch but the CFG "
497 "successors don't match the actual successors!", MBB);
498 }
499 if (MBB->empty()) {
500 report("MBB exits via conditional branch/branch but doesn't "
501 "contain any instructions!", MBB);
502 } else if (!MBB->back().getDesc().isBarrier()) {
503 report("MBB exits via conditional branch/branch but doesn't end with a "
504 "barrier instruction!", MBB);
505 } else if (!MBB->back().getDesc().isTerminator()) {
506 report("MBB exits via conditional branch/branch but the branch "
507 "isn't a terminator instruction!", MBB);
508 }
509 if (Cond.empty()) {
510 report("MBB exits via conditinal branch/branch but there's no "
511 "condition!", MBB);
512 }
513 } else {
514 report("AnalyzeBranch returned invalid data!", MBB);
515 }
516 }
517
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000518 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000519 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000520 E = MBB->livein_end(); I != E; ++I) {
521 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
522 report("MBB live-in list contains non-physical register", MBB);
523 continue;
524 }
525 regsLive.insert(*I);
526 for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++)
527 regsLive.insert(*R);
528 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000529 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000530
531 const MachineFrameInfo *MFI = MF->getFrameInfo();
532 assert(MFI && "Function has no frame info");
533 BitVector PR = MFI->getPristineRegs(MBB);
534 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
535 regsLive.insert(I);
536 for (const unsigned *R = TRI->getSubRegisters(I); *R; R++)
537 regsLive.insert(*R);
538 }
539
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000540 regsKilled.clear();
541 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000542
543 if (Indexes)
544 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000545}
546
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000547void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000548 const MCInstrDesc &MCID = MI->getDesc();
549 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000550 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000551 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000552 << MI->getNumExplicitOperands() << " given.\n";
553 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000554
555 // Check the MachineMemOperands for basic consistency.
556 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
557 E = MI->memoperands_end(); I != E; ++I) {
Evan Chenge837dea2011-06-28 19:10:37 +0000558 if ((*I)->isLoad() && !MCID.mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000559 report("Missing mayLoad flag", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000560 if ((*I)->isStore() && !MCID.mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000561 report("Missing mayStore flag", MI);
562 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000563
564 // Debug values must not have a slot index.
565 // Other instructions must have one.
566 if (LiveInts) {
567 bool mapped = !LiveInts->isNotInMIMap(MI);
568 if (MI->isDebugValue()) {
569 if (mapped)
570 report("Debug instruction has a slot index", MI);
571 } else {
572 if (!mapped)
573 report("Missing slot index", MI);
574 }
575 }
576
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000577 // Ensure non-terminators don't follow terminators.
578 if (MCID.isTerminator()) {
579 if (!FirstTerminator)
580 FirstTerminator = MI;
581 } else if (FirstTerminator) {
582 report("Non-terminator instruction after the first terminator", MI);
583 *OS << "First terminator was:\t" << *FirstTerminator;
584 }
585
Andrew Trick3be654f2011-09-21 02:20:46 +0000586 StringRef ErrorInfo;
587 if (!TII->verifyInstruction(MI, ErrorInfo))
588 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000589}
590
591void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000592MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000593 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000594 const MCInstrDesc &MCID = MI->getDesc();
595 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000596
Evan Chenge837dea2011-06-28 19:10:37 +0000597 // The first MCID.NumDefs operands must be explicit register defines
598 if (MONum < MCID.getNumDefs()) {
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000599 if (!MO->isReg())
600 report("Explicit definition must be a register", MO, MONum);
601 else if (!MO->isDef())
602 report("Explicit definition marked as use", MO, MONum);
603 else if (MO->isImplicit())
604 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000605 } else if (MONum < MCID.getNumOperands()) {
Eric Christopher113a06c2010-11-17 00:55:36 +0000606 // Don't check if it's the last operand in a variadic instruction. See,
607 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000608 if (MO->isReg() &&
609 !(MCID.isVariadic() && MONum == MCID.getNumOperands()-1)) {
610 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000611 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000612 if (MO->isImplicit())
613 report("Explicit operand marked as implicit", MO, MONum);
614 }
615 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000616 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Chenge837dea2011-06-28 19:10:37 +0000617 if (MO->isReg() && !MO->isImplicit() && !MCID.isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000618 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000619 }
620
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000621 switch (MO->getType()) {
622 case MachineOperand::MO_Register: {
623 const unsigned Reg = MO->getReg();
624 if (!Reg)
625 return;
626
627 // Check Live Variables.
Cameron Zwarich8ec88ba2010-12-20 00:08:10 +0000628 if (MI->isDebugValue()) {
629 // Liveness checks are not valid for debug values.
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000630 } else if (MO->isUse() && !MO->isUndef()) {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000631 regsLiveInButUnused.erase(Reg);
632
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000633 bool isKill = false;
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000634 unsigned defIdx;
635 if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
636 // A two-addr use counts as a kill if use and def are the same.
637 unsigned DefReg = MI->getOperand(defIdx).getReg();
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000638 if (Reg == DefReg)
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000639 isKill = true;
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000640 else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000641 report("Two-address instruction operands must be identical",
642 MO, MONum);
643 }
644 } else
645 isKill = MO->isKill();
646
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000647 if (isKill)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000648 addRegWithSubRegs(regsKilled, Reg);
649
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000650 // Check that LiveVars knows this kill.
651 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
652 MO->isKill()) {
653 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
654 if (std::find(VI.Kills.begin(),
655 VI.Kills.end(), MI) == VI.Kills.end())
656 report("Kill missing from LiveVariables", MO, MONum);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000657 }
658
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000659 // Check LiveInts liveness and kill.
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000660 if (TargetRegisterInfo::isVirtualRegister(Reg) &&
661 LiveInts && !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000662 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getUseIndex();
663 if (LiveInts->hasInterval(Reg)) {
664 const LiveInterval &LI = LiveInts->getInterval(Reg);
665 if (!LI.liveAt(UseIdx)) {
666 report("No live range at use", MO, MONum);
667 *OS << UseIdx << " is not live in " << LI << '\n';
668 }
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000669 // Check for extra kill flags.
670 // Note that we allow missing kill flags for now.
671 if (MO->isKill() && !LI.killedAt(UseIdx.getDefIndex())) {
672 report("Live range continues after kill flag", MO, MONum);
673 *OS << "Live range: " << LI << '\n';
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000674 }
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000675 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000676 report("Virtual register has no Live interval", MO, MONum);
677 }
678 }
679
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000680 // Use of a dead register.
681 if (!regsLive.count(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000682 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen4af0f5f2011-07-30 00:57:25 +0000683 // Reserved registers may be used even when 'dead'.
684 if (!isReserved(Reg))
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000685 report("Using an undefined physical register", MO, MONum);
686 } else {
687 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
688 // We don't know which virtual registers are live in, so only complain
689 // if vreg was killed in this MBB. Otherwise keep track of vregs that
690 // must be live in. PHI instructions are handled separately.
691 if (MInfo.regsKilled.count(Reg))
692 report("Using a killed virtual register", MO, MONum);
Chris Lattner518bb532010-02-09 19:54:29 +0000693 else if (!MI->isPHI())
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000694 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
695 }
Duncan Sandse5567202009-05-16 03:28:54 +0000696 }
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000697 } else if (MO->isDef()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000698 // Register defined.
699 // TODO: verify that earlyclobber ops are not used.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000700 if (MO->isDead())
701 addRegWithSubRegs(regsDead, Reg);
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000702 else
703 addRegWithSubRegs(regsDefined, Reg);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000704
Jakob Stoklund Olesen93e6f022011-07-29 23:02:48 +0000705 // Verify SSA form.
706 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
707 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
708 report("Multiple virtual register defs in SSA form", MO, MONum);
709
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000710 // Check LiveInts for a live range, but only for virtual registers.
711 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
712 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000713 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getDefIndex();
714 if (LiveInts->hasInterval(Reg)) {
715 const LiveInterval &LI = LiveInts->getInterval(Reg);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000716 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
717 assert(VNI && "NULL valno is not allowed");
Cameron Zwarich1b031dd2010-12-19 23:50:53 +0000718 if (VNI->def != DefIdx && !MO->isEarlyClobber()) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000719 report("Inconsistent valno->def", MO, MONum);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000720 *OS << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000721 << DefIdx << " in " << LI << '\n';
722 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000723 } else {
724 report("No live range at def", MO, MONum);
725 *OS << DefIdx << " is not live in " << LI << '\n';
726 }
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000727 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000728 report("Virtual register has no Live interval", MO, MONum);
729 }
730 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000731 }
732
733 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000734 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000735 unsigned SubIdx = MO->getSubReg();
736
737 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
738 unsigned sr = Reg;
739 if (SubIdx) {
740 unsigned s = TRI->getSubReg(Reg, SubIdx);
741 if (!s) {
742 report("Invalid subregister index for physical register",
743 MO, MONum);
744 return;
745 }
746 sr = s;
747 }
Evan Chenge837dea2011-06-28 19:10:37 +0000748 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000749 if (!DRC->contains(sr)) {
750 report("Illegal physical register for instruction", MO, MONum);
751 *OS << TRI->getName(sr) << " is not a "
752 << DRC->getName() << " register.\n";
753 }
754 }
755 } else {
756 // Virtual register.
757 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
758 if (SubIdx) {
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000759 const TargetRegisterClass *SRC = RC->getSubRegisterRegClass(SubIdx);
760 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000761 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000762 *OS << "Register class " << RC->getName()
763 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000764 return;
765 }
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000766 RC = SRC;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000767 }
Evan Chenge837dea2011-06-28 19:10:37 +0000768 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000769 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000770 report("Illegal virtual register for instruction", MO, MONum);
771 *OS << "Expected a " << DRC->getName() << " register, but got a "
772 << RC->getName() << " register\n";
773 }
774 }
775 }
776 }
777 break;
778 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000779
780 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000781 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
782 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000783 break;
784
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000785 case MachineOperand::MO_FrameIndex:
786 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
787 LiveInts && !LiveInts->isNotInMIMap(MI)) {
788 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
789 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000790 if (MCID.mayLoad() && !LI.liveAt(Idx.getUseIndex())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000791 report("Instruction loads from dead spill slot", MO, MONum);
792 *OS << "Live stack: " << LI << '\n';
793 }
Evan Chenge837dea2011-06-28 19:10:37 +0000794 if (MCID.mayStore() && !LI.liveAt(Idx.getDefIndex())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000795 report("Instruction stores to dead spill slot", MO, MONum);
796 *OS << "Live stack: " << LI << '\n';
797 }
798 }
799 break;
800
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000801 default:
802 break;
803 }
804}
805
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000806void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000807 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
808 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000809 set_subtract(regsLive, regsKilled); regsKilled.clear();
810 set_subtract(regsLive, regsDead); regsDead.clear();
811 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000812
813 if (Indexes && Indexes->hasIndex(MI)) {
814 SlotIndex idx = Indexes->getInstructionIndex(MI);
815 if (!(idx > lastIndex)) {
816 report("Instruction index out of order", MI);
817 *OS << "Last instruction was at " << lastIndex << '\n';
818 }
819 lastIndex = idx;
820 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000821}
822
823void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000824MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000825 MBBInfoMap[MBB].regsLiveOut = regsLive;
826 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000827
828 if (Indexes) {
829 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
830 if (!(stop > lastIndex)) {
831 report("Block ends before last instruction index", MBB);
832 *OS << "Block ends at " << stop
833 << " last instruction was at " << lastIndex << '\n';
834 }
835 lastIndex = stop;
836 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000837}
838
839// Calculate the largest possible vregsPassed sets. These are the registers that
840// can pass through an MBB live, but may not be live every time. It is assumed
841// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000842void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000843 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
844 // have any vregsPassed.
845 DenseSet<const MachineBasicBlock*> todo;
846 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
847 MFI != MFE; ++MFI) {
848 const MachineBasicBlock &MBB(*MFI);
849 BBInfo &MInfo = MBBInfoMap[&MBB];
850 if (!MInfo.reachable)
851 continue;
852 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
853 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
854 BBInfo &SInfo = MBBInfoMap[*SuI];
855 if (SInfo.addPassed(MInfo.regsLiveOut))
856 todo.insert(*SuI);
857 }
858 }
859
860 // Iteratively push vregsPassed to successors. This will converge to the same
861 // final state regardless of DenseSet iteration order.
862 while (!todo.empty()) {
863 const MachineBasicBlock *MBB = *todo.begin();
864 todo.erase(MBB);
865 BBInfo &MInfo = MBBInfoMap[MBB];
866 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
867 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
868 if (*SuI == MBB)
869 continue;
870 BBInfo &SInfo = MBBInfoMap[*SuI];
871 if (SInfo.addPassed(MInfo.vregsPassed))
872 todo.insert(*SuI);
873 }
874 }
875}
876
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000877// Calculate the set of virtual registers that must be passed through each basic
878// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000879// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000880void MachineVerifier::calcRegsRequired() {
881 // First push live-in regs to predecessors' vregsRequired.
882 DenseSet<const MachineBasicBlock*> todo;
883 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
884 MFI != MFE; ++MFI) {
885 const MachineBasicBlock &MBB(*MFI);
886 BBInfo &MInfo = MBBInfoMap[&MBB];
887 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
888 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
889 BBInfo &PInfo = MBBInfoMap[*PrI];
890 if (PInfo.addRequired(MInfo.vregsLiveIn))
891 todo.insert(*PrI);
892 }
893 }
894
895 // Iteratively push vregsRequired to predecessors. This will converge to the
896 // same final state regardless of DenseSet iteration order.
897 while (!todo.empty()) {
898 const MachineBasicBlock *MBB = *todo.begin();
899 todo.erase(MBB);
900 BBInfo &MInfo = MBBInfoMap[MBB];
901 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
902 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
903 if (*PrI == MBB)
904 continue;
905 BBInfo &SInfo = MBBInfoMap[*PrI];
906 if (SInfo.addRequired(MInfo.vregsRequired))
907 todo.insert(*PrI);
908 }
909 }
910}
911
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000912// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000913// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000914void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000915 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +0000916 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000917 DenseSet<const MachineBasicBlock*> seen;
918
919 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
920 unsigned Reg = BBI->getOperand(i).getReg();
921 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
922 if (!Pre->isSuccessor(MBB))
923 continue;
924 seen.insert(Pre);
925 BBInfo &PrInfo = MBBInfoMap[Pre];
926 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
927 report("PHI operand is not live-out from predecessor",
928 &BBI->getOperand(i), i);
929 }
930
931 // Did we see all predecessors?
932 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
933 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
934 if (!seen.count(*PrI)) {
935 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000936 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000937 << " is a predecessor according to the CFG.\n";
938 }
939 }
940 }
941}
942
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000943void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000944 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000945
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000946 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
947 MFI != MFE; ++MFI) {
948 BBInfo &MInfo = MBBInfoMap[MFI];
949
950 // Skip unreachable MBBs.
951 if (!MInfo.reachable)
952 continue;
953
954 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000955 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000956
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000957 // Now check liveness info if available
958 if (LiveVars || LiveInts)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000959 calcRegsRequired();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000960 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000961 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000962 if (LiveInts)
963 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000964}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000965
966void MachineVerifier::verifyLiveVariables() {
967 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +0000968 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
969 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000970 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
971 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
972 MFI != MFE; ++MFI) {
973 BBInfo &MInfo = MBBInfoMap[MFI];
974
975 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
976 if (MInfo.vregsRequired.count(Reg)) {
977 if (!VI.AliveBlocks.test(MFI->getNumber())) {
978 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000979 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000980 << " must be live through the block.\n";
981 }
982 } else {
983 if (VI.AliveBlocks.test(MFI->getNumber())) {
984 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000985 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000986 << " is not needed live through the block.\n";
987 }
988 }
989 }
990 }
991}
992
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000993void MachineVerifier::verifyLiveIntervals() {
994 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
995 for (LiveIntervals::const_iterator LVI = LiveInts->begin(),
996 LVE = LiveInts->end(); LVI != LVE; ++LVI) {
997 const LiveInterval &LI = *LVI->second;
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +0000998
999 // Spilling and splitting may leave unused registers around. Skip them.
1000 if (MRI->use_empty(LI.reg))
1001 continue;
1002
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001003 // Physical registers have much weirdness going on, mostly from coalescing.
1004 // We should probably fix it, but for now just ignore them.
1005 if (TargetRegisterInfo::isPhysicalRegister(LI.reg))
1006 continue;
1007
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001008 assert(LVI->first == LI.reg && "Invalid reg to interval mapping");
1009
1010 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1011 I!=E; ++I) {
1012 VNInfo *VNI = *I;
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001013 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001014
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001015 if (!DefVNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001016 if (!VNI->isUnused()) {
1017 report("Valno not live at def and not marked unused", MF);
1018 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1019 }
1020 continue;
1021 }
1022
1023 if (VNI->isUnused())
1024 continue;
1025
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001026 if (DefVNI != VNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001027 report("Live range at def has different valno", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001028 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001029 << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001030 continue;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001031 }
1032
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001033 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1034 if (!MBB) {
1035 report("Invalid definition index", MF);
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001036 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1037 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001038 continue;
1039 }
1040
1041 if (VNI->isPHIDef()) {
1042 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1043 report("PHIDef value is not defined at MBB start", MF);
1044 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001045 << ", not at the beginning of BB#" << MBB->getNumber()
1046 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001047 }
1048 } else {
1049 // Non-PHI def.
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001050 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1051 if (!MI) {
1052 report("No instruction at def index", MF);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001053 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1054 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001055 } else if (!MI->modifiesRegister(LI.reg, TRI)) {
1056 report("Defining instruction does not modify register", MI);
1057 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1058 }
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001059
1060 bool isEarlyClobber = false;
1061 if (MI) {
1062 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1063 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1064 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() &&
1065 MOI->isEarlyClobber()) {
1066 isEarlyClobber = true;
1067 break;
1068 }
1069 }
1070 }
1071
1072 // Early clobber defs begin at USE slots, but other defs must begin at
1073 // DEF slots.
1074 if (isEarlyClobber) {
1075 if (!VNI->def.isUse()) {
1076 report("Early clobber def must be at a USE slot", MF);
1077 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1078 << " in " << LI << '\n';
1079 }
1080 } else if (!VNI->def.isDef()) {
1081 report("Non-PHI, non-early clobber def must be at a DEF slot", MF);
1082 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1083 << " in " << LI << '\n';
1084 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001085 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001086 }
1087
1088 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001089 const VNInfo *VNI = I->valno;
1090 assert(VNI && "Live range has no valno");
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001091
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001092 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001093 report("Foreign valno in live range", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001094 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001095 *OS << " has a valno not in " << LI << '\n';
1096 }
1097
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001098 if (VNI->isUnused()) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001099 report("Live range valno is marked unused", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001100 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001101 *OS << " in " << LI << '\n';
1102 }
1103
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001104 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1105 if (!MBB) {
1106 report("Bad start of live segment, no basic block", MF);
1107 I->print(*OS);
1108 *OS << " in " << LI << '\n';
1109 continue;
1110 }
1111 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1112 if (I->start != MBBStartIdx && I->start != VNI->def) {
1113 report("Live segment must begin at MBB entry or valno def", MBB);
1114 I->print(*OS);
1115 *OS << " in " << LI << '\n' << "Basic block starts at "
1116 << MBBStartIdx << '\n';
1117 }
1118
1119 const MachineBasicBlock *EndMBB =
1120 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1121 if (!EndMBB) {
1122 report("Bad end of live segment, no basic block", MF);
1123 I->print(*OS);
1124 *OS << " in " << LI << '\n';
1125 continue;
1126 }
1127 if (I->end != LiveInts->getMBBEndIdx(EndMBB)) {
1128 // The live segment is ending inside EndMBB
1129 const MachineInstr *MI =
1130 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1131 if (!MI) {
1132 report("Live segment doesn't end at a valid instruction", EndMBB);
1133 I->print(*OS);
1134 *OS << " in " << LI << '\n' << "Basic block starts at "
1135 << MBBStartIdx << '\n';
1136 } else if (TargetRegisterInfo::isVirtualRegister(LI.reg) &&
1137 !MI->readsVirtualRegister(LI.reg)) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001138 // A live range can end with either a redefinition, a kill flag on a
1139 // use, or a dead flag on a def.
1140 // FIXME: Should we check for each of these?
1141 bool hasDeadDef = false;
1142 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1143 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
Cameron Zwarich5e61f992010-12-20 02:59:51 +00001144 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() && MOI->isDead()) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001145 hasDeadDef = true;
1146 break;
1147 }
1148 }
1149
1150 if (!hasDeadDef) {
1151 report("Instruction killing live segment neither defines nor reads "
1152 "register", MI);
1153 I->print(*OS);
1154 *OS << " in " << LI << '\n';
1155 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001156 }
1157 }
1158
1159 // Now check all the basic blocks in this live segment.
1160 MachineFunction::const_iterator MFI = MBB;
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001161 // Is this live range the beginning of a non-PHIDef VN?
1162 if (I->start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001163 // Not live-in to any blocks.
1164 if (MBB == EndMBB)
1165 continue;
1166 // Skip this block.
1167 ++MFI;
1168 }
1169 for (;;) {
1170 assert(LiveInts->isLiveInToMBB(LI, MFI));
Jakob Stoklund Olesene459d552010-10-26 16:49:23 +00001171 // We don't know how to track physregs into a landing pad.
1172 if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1173 MFI->isLandingPad()) {
1174 if (&*MFI == EndMBB)
1175 break;
1176 ++MFI;
1177 continue;
1178 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001179 // Check that VNI is live-out of all predecessors.
1180 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1181 PE = MFI->pred_end(); PI != PE; ++PI) {
1182 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI).getPrevSlot();
1183 const VNInfo *PVNI = LI.getVNInfoAt(PEnd);
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001184
Jakob Stoklund Olesendf8412c2011-09-15 05:16:30 +00001185 if (VNI->isPHIDef() && VNI->def == LiveInts->getMBBStartIdx(MFI))
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001186 continue;
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001187
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001188 if (!PVNI) {
1189 report("Register not marked live out of predecessor", *PI);
1190 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1191 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live at "
1192 << PEnd << " in " << LI << '\n';
1193 continue;
1194 }
1195
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001196 if (PVNI != VNI) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001197 report("Different value live out of predecessor", *PI);
1198 *OS << "Valno #" << PVNI->id << " live out of BB#"
1199 << (*PI)->getNumber() << '@' << PEnd
1200 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1201 << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n';
1202 }
1203 }
1204 if (&*MFI == EndMBB)
1205 break;
1206 ++MFI;
1207 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001208 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001209
1210 // Check the LI only has one connected component.
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001211 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1212 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1213 unsigned NumComp = ConEQ.Classify(&LI);
1214 if (NumComp > 1) {
1215 report("Multiple connected components in live interval", MF);
1216 *OS << NumComp << " components in " << LI << '\n';
Jakob Stoklund Olesencb367772010-10-29 00:40:57 +00001217 for (unsigned comp = 0; comp != NumComp; ++comp) {
1218 *OS << comp << ": valnos";
1219 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1220 E = LI.vni_end(); I!=E; ++I)
1221 if (comp == ConEQ.getEqClass(*I))
1222 *OS << ' ' << (*I)->id;
1223 *OS << '\n';
1224 }
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001225 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001226 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001227 }
1228}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001229