blob: 065f2527ca2299916b75c110dfe21b0448a4bcf6 [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;
Lang Hames03698de2012-02-14 19:17:48 +000078 BitVector regsAllocatable;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000079 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000080 RegVector regsDefined, regsDead, regsKilled;
81 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000082
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000083 SlotIndex lastIndex;
84
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000085 // Add Reg and any sub-registers to RV
86 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
87 RV.push_back(Reg);
88 if (TargetRegisterInfo::isPhysicalRegister(Reg))
89 for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
90 RV.push_back(*R);
91 }
92
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000093 struct BBInfo {
94 // Is this MBB reachable from the MF entry point?
95 bool reachable;
96
97 // Vregs that must be live in because they are used without being
98 // defined. Map value is the user.
99 RegMap vregsLiveIn;
100
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000101 // Regs killed in MBB. They may be defined again, and will then be in both
102 // regsKilled and regsLiveOut.
103 RegSet regsKilled;
104
105 // Regs defined in MBB and live out. Note that vregs passing through may
106 // be live out without being mentioned here.
107 RegSet regsLiveOut;
108
109 // Vregs that pass through MBB untouched. This set is disjoint from
110 // regsKilled and regsLiveOut.
111 RegSet vregsPassed;
112
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000113 // Vregs that must pass through MBB because they are needed by a successor
114 // block. This set is disjoint from regsLiveOut.
115 RegSet vregsRequired;
116
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000117 BBInfo() : reachable(false) {}
118
119 // Add register to vregsPassed if it belongs there. Return true if
120 // anything changed.
121 bool addPassed(unsigned Reg) {
122 if (!TargetRegisterInfo::isVirtualRegister(Reg))
123 return false;
124 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
125 return false;
126 return vregsPassed.insert(Reg).second;
127 }
128
129 // Same for a full set.
130 bool addPassed(const RegSet &RS) {
131 bool changed = false;
132 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
133 if (addPassed(*I))
134 changed = true;
135 return changed;
136 }
137
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000138 // Add register to vregsRequired if it belongs there. Return true if
139 // anything changed.
140 bool addRequired(unsigned Reg) {
141 if (!TargetRegisterInfo::isVirtualRegister(Reg))
142 return false;
143 if (regsLiveOut.count(Reg))
144 return false;
145 return vregsRequired.insert(Reg).second;
146 }
147
148 // Same for a full set.
149 bool addRequired(const RegSet &RS) {
150 bool changed = false;
151 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
152 if (addRequired(*I))
153 changed = true;
154 return changed;
155 }
156
157 // Same for a full map.
158 bool addRequired(const RegMap &RM) {
159 bool changed = false;
160 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
161 if (addRequired(I->first))
162 changed = true;
163 return changed;
164 }
165
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000166 // Live-out registers are either in regsLiveOut or vregsPassed.
167 bool isLiveOut(unsigned Reg) const {
168 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
169 }
170 };
171
172 // Extra register info per MBB.
173 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
174
175 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000176 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000177 }
178
Lang Hames03698de2012-02-14 19:17:48 +0000179 bool isAllocatable(unsigned Reg) {
180 return Reg < regsAllocatable.size() && regsAllocatable.test(Reg);
181 }
182
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000183 // Analysis information if available
184 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000185 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000186 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000187 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000188
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000189 void visitMachineFunctionBefore();
190 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
191 void visitMachineInstrBefore(const MachineInstr *MI);
192 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
193 void visitMachineInstrAfter(const MachineInstr *MI);
194 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
195 void visitMachineFunctionAfter();
196
197 void report(const char *msg, const MachineFunction *MF);
198 void report(const char *msg, const MachineBasicBlock *MBB);
199 void report(const char *msg, const MachineInstr *MI);
200 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
201
202 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000203 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000204 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000205
206 void calcRegsRequired();
207 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000208 void verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000209 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000210
211 struct MachineVerifierPass : public MachineFunctionPass {
212 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000213 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000214
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000215 MachineVerifierPass(const char *b = 0)
216 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000217 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
218 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000219
220 void getAnalysisUsage(AnalysisUsage &AU) const {
221 AU.setPreservesAll();
222 MachineFunctionPass::getAnalysisUsage(AU);
223 }
224
225 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000226 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000227 return false;
228 }
229 };
230
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000231}
232
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000233char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000234INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000235 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000236
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000237FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
238 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000239}
240
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000241void MachineFunction::verify(Pass *p, const char *Banner) const {
242 MachineVerifier(p, Banner)
243 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000244}
245
Chris Lattner17e9edc2009-08-23 02:51:22 +0000246bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
247 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000248 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000249 std::string ErrorInfo;
250 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
251 raw_fd_ostream::F_Append);
252 if (!ErrorInfo.empty()) {
253 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
254 exit(1);
255 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000256
Chris Lattner17e9edc2009-08-23 02:51:22 +0000257 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000258 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000259 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000260 }
261
262 foundErrors = 0;
263
264 this->MF = &MF;
265 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000266 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000267 TRI = TM->getRegisterInfo();
268 MRI = &MF.getRegInfo();
269
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000270 LiveVars = NULL;
271 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000272 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000273 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000274 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000275 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000276 // We don't want to verify LiveVariables if LiveIntervals is available.
277 if (!LiveInts)
278 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000279 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000280 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000281 }
282
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000283 visitMachineFunctionBefore();
284 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
285 MFI!=MFE; ++MFI) {
286 visitMachineBasicBlockBefore(MFI);
Evan Chengddfd1372011-12-14 02:11:42 +0000287 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
288 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000289 if (MBBI->getParent() != MFI) {
290 report("Bad instruction parent pointer", MFI);
291 *OS << "Instruction: " << *MBBI;
292 continue;
293 }
Evan Chengddfd1372011-12-14 02:11:42 +0000294 // Skip BUNDLE instruction for now. FIXME: We should add code to verify
295 // the BUNDLE's specifically.
296 if (MBBI->isBundle())
297 continue;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000298 visitMachineInstrBefore(MBBI);
299 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
300 visitMachineOperand(&MBBI->getOperand(I), I);
301 visitMachineInstrAfter(MBBI);
302 }
303 visitMachineBasicBlockAfter(MFI);
304 }
305 visitMachineFunctionAfter();
306
Chris Lattner17e9edc2009-08-23 02:51:22 +0000307 if (OutFile)
308 delete OutFile;
309 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000310 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000311
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000312 // Clean up.
313 regsLive.clear();
314 regsDefined.clear();
315 regsDead.clear();
316 regsKilled.clear();
317 regsLiveInButUnused.clear();
318 MBBInfoMap.clear();
319
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000320 return false; // no changes
321}
322
Chris Lattner372fefe2009-08-23 01:03:30 +0000323void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000324 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000325 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000326 if (!foundErrors++) {
327 if (Banner)
328 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000329 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000330 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000331 *OS << "*** Bad machine code: " << msg << " ***\n"
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000332 << "- function: " << MF->getFunction()->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000333}
334
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000335void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000336 assert(MBB);
337 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000338 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000339 << " " << (void*)MBB
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000340 << " (BB#" << MBB->getNumber() << ")";
341 if (Indexes)
342 *OS << " [" << Indexes->getMBBStartIdx(MBB)
343 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
344 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000345}
346
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000347void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000348 assert(MI);
349 report(msg, MI->getParent());
350 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000351 if (Indexes && Indexes->hasIndex(MI))
352 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000353 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000354}
355
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000356void MachineVerifier::report(const char *msg,
357 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000358 assert(MO);
359 report(msg, MO->getParent());
360 *OS << "- operand " << MONum << ": ";
361 MO->print(*OS, TM);
362 *OS << "\n";
363}
364
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000365void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000366 BBInfo &MInfo = MBBInfoMap[MBB];
367 if (!MInfo.reachable) {
368 MInfo.reachable = true;
369 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
370 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
371 markReachable(*SuI);
372 }
373}
374
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000375void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000376 lastIndex = SlotIndex();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000377 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000378
379 // A sub-register of a reserved register is also reserved
380 for (int Reg = regsReserved.find_first(); Reg>=0;
381 Reg = regsReserved.find_next(Reg)) {
382 for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
383 // FIXME: This should probably be:
384 // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
385 regsReserved.set(*Sub);
386 }
387 }
Lang Hames03698de2012-02-14 19:17:48 +0000388
389 regsAllocatable = TRI->getAllocatableSet(*MF);
390
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000391 markReachable(&MF->front());
392}
393
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000394// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000395static bool matchPair(MachineBasicBlock::const_succ_iterator i,
396 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000397 if (*i == a)
398 return *++i == b;
399 if (*i == b)
400 return *++i == a;
401 return false;
402}
403
404void
405MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000406 FirstTerminator = 0;
407
Lang Hames03698de2012-02-14 19:17:48 +0000408 if (MRI->isSSA()) {
409 // If this block has allocatable physical registers live-in, check that
410 // it is an entry block or landing pad.
411 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
412 LE = MBB->livein_end();
413 LI != LE; ++LI) {
414 unsigned reg = *LI;
415 if (isAllocatable(reg) && !MBB->isLandingPad() &&
416 MBB != MBB->getParent()->begin()) {
417 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
418 }
419 }
420 }
421
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000422 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000423 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000424 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000425 E = MBB->succ_end(); I != E; ++I) {
426 if ((*I)->isLandingPad())
427 LandingPadSuccs.insert(*I);
428 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000429
430 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
431 const BasicBlock *BB = MBB->getBasicBlock();
432 if (LandingPadSuccs.size() > 1 &&
433 !(AsmInfo &&
434 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
435 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000436 report("MBB has more than one landing pad successor", MBB);
437
Dan Gohman27920592009-08-27 02:43:49 +0000438 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
439 MachineBasicBlock *TBB = 0, *FBB = 0;
440 SmallVector<MachineOperand, 4> Cond;
441 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
442 TBB, FBB, Cond)) {
443 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
444 // check whether its answers match up with reality.
445 if (!TBB && !FBB) {
446 // Block falls through to its successor.
447 MachineFunction::const_iterator MBBI = MBB;
448 ++MBBI;
449 if (MBBI == MF->end()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000450 // It's possible that the block legitimately ends with a noreturn
451 // call or an unreachable, in which case it won't actually fall
452 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000453 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000454 // It's possible that the block legitimately ends with a noreturn
455 // call or an unreachable, in which case it won't actuall fall
456 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000457 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000458 report("MBB exits via unconditional fall-through but doesn't have "
459 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000460 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000461 report("MBB exits via unconditional fall-through but its successor "
462 "differs from its CFG successor!", MBB);
463 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000464 if (!MBB->empty() && MBB->back().isBarrier() &&
Evan Cheng86050dc2010-06-18 23:09:54 +0000465 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000466 report("MBB exits via unconditional fall-through but ends with a "
467 "barrier instruction!", MBB);
468 }
469 if (!Cond.empty()) {
470 report("MBB exits via unconditional fall-through but has a condition!",
471 MBB);
472 }
473 } else if (TBB && !FBB && Cond.empty()) {
474 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000475 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000476 report("MBB exits via unconditional branch but doesn't have "
477 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000478 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000479 report("MBB exits via unconditional branch but the CFG "
480 "successor doesn't match the actual successor!", MBB);
481 }
482 if (MBB->empty()) {
483 report("MBB exits via unconditional branch but doesn't contain "
484 "any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000485 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000486 report("MBB exits via unconditional branch but doesn't end with a "
487 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000488 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000489 report("MBB exits via unconditional branch but the branch isn't a "
490 "terminator instruction!", MBB);
491 }
492 } else if (TBB && !FBB && !Cond.empty()) {
493 // Block conditionally branches somewhere, otherwise falls through.
494 MachineFunction::const_iterator MBBI = MBB;
495 ++MBBI;
496 if (MBBI == MF->end()) {
497 report("MBB conditionally falls through out of function!", MBB);
498 } if (MBB->succ_size() != 2) {
499 report("MBB exits via conditional branch/fall-through but doesn't have "
500 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000501 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000502 report("MBB exits via conditional branch/fall-through but the CFG "
503 "successors don't match the actual successors!", MBB);
504 }
505 if (MBB->empty()) {
506 report("MBB exits via conditional branch/fall-through but doesn't "
507 "contain any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000508 } else if (MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000509 report("MBB exits via conditional branch/fall-through but ends with a "
510 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000511 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000512 report("MBB exits via conditional branch/fall-through but the branch "
513 "isn't a terminator instruction!", MBB);
514 }
515 } else if (TBB && FBB) {
516 // Block conditionally branches somewhere, otherwise branches
517 // somewhere else.
518 if (MBB->succ_size() != 2) {
519 report("MBB exits via conditional branch/branch but doesn't have "
520 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000521 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000522 report("MBB exits via conditional branch/branch but the CFG "
523 "successors don't match the actual successors!", MBB);
524 }
525 if (MBB->empty()) {
526 report("MBB exits via conditional branch/branch but doesn't "
527 "contain any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000528 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000529 report("MBB exits via conditional branch/branch but doesn't end with a "
530 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000531 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000532 report("MBB exits via conditional branch/branch but the branch "
533 "isn't a terminator instruction!", MBB);
534 }
535 if (Cond.empty()) {
536 report("MBB exits via conditinal branch/branch but there's no "
537 "condition!", MBB);
538 }
539 } else {
540 report("AnalyzeBranch returned invalid data!", MBB);
541 }
542 }
543
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000544 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000545 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000546 E = MBB->livein_end(); I != E; ++I) {
547 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
548 report("MBB live-in list contains non-physical register", MBB);
549 continue;
550 }
551 regsLive.insert(*I);
552 for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++)
553 regsLive.insert(*R);
554 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000555 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000556
557 const MachineFrameInfo *MFI = MF->getFrameInfo();
558 assert(MFI && "Function has no frame info");
559 BitVector PR = MFI->getPristineRegs(MBB);
560 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
561 regsLive.insert(I);
562 for (const unsigned *R = TRI->getSubRegisters(I); *R; R++)
563 regsLive.insert(*R);
564 }
565
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000566 regsKilled.clear();
567 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000568
569 if (Indexes)
570 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000571}
572
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000573void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000574 const MCInstrDesc &MCID = MI->getDesc();
575 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000576 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000577 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000578 << MI->getNumExplicitOperands() << " given.\n";
579 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000580
581 // Check the MachineMemOperands for basic consistency.
582 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
583 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000584 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000585 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000586 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000587 report("Missing mayStore flag", MI);
588 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000589
590 // Debug values must not have a slot index.
591 // Other instructions must have one.
592 if (LiveInts) {
593 bool mapped = !LiveInts->isNotInMIMap(MI);
594 if (MI->isDebugValue()) {
595 if (mapped)
596 report("Debug instruction has a slot index", MI);
597 } else {
598 if (!mapped)
599 report("Missing slot index", MI);
600 }
601 }
602
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000603 // Ensure non-terminators don't follow terminators.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000604 if (MI->isTerminator()) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000605 if (!FirstTerminator)
606 FirstTerminator = MI;
607 } else if (FirstTerminator) {
608 report("Non-terminator instruction after the first terminator", MI);
609 *OS << "First terminator was:\t" << *FirstTerminator;
610 }
611
Andrew Trick3be654f2011-09-21 02:20:46 +0000612 StringRef ErrorInfo;
613 if (!TII->verifyInstruction(MI, ErrorInfo))
614 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000615}
616
617void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000618MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000619 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000620 const MCInstrDesc &MCID = MI->getDesc();
621 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000622
Evan Chenge837dea2011-06-28 19:10:37 +0000623 // The first MCID.NumDefs operands must be explicit register defines
624 if (MONum < MCID.getNumDefs()) {
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000625 if (!MO->isReg())
626 report("Explicit definition must be a register", MO, MONum);
627 else if (!MO->isDef())
628 report("Explicit definition marked as use", MO, MONum);
629 else if (MO->isImplicit())
630 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000631 } else if (MONum < MCID.getNumOperands()) {
Eric Christopher113a06c2010-11-17 00:55:36 +0000632 // Don't check if it's the last operand in a variadic instruction. See,
633 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000634 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000635 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000636 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000637 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000638 if (MO->isImplicit())
639 report("Explicit operand marked as implicit", MO, MONum);
640 }
641 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000642 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000643 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000644 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000645 }
646
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000647 switch (MO->getType()) {
648 case MachineOperand::MO_Register: {
649 const unsigned Reg = MO->getReg();
650 if (!Reg)
651 return;
652
653 // Check Live Variables.
Cameron Zwarich8ec88ba2010-12-20 00:08:10 +0000654 if (MI->isDebugValue()) {
655 // Liveness checks are not valid for debug values.
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000656 } else if (MO->isUse() && !MO->isUndef()) {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000657 regsLiveInButUnused.erase(Reg);
658
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000659 bool isKill = false;
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000660 unsigned defIdx;
661 if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
662 // A two-addr use counts as a kill if use and def are the same.
663 unsigned DefReg = MI->getOperand(defIdx).getReg();
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000664 if (Reg == DefReg)
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000665 isKill = true;
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000666 else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000667 report("Two-address instruction operands must be identical",
668 MO, MONum);
669 }
670 } else
671 isKill = MO->isKill();
672
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000673 if (isKill)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000674 addRegWithSubRegs(regsKilled, Reg);
675
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000676 // Check that LiveVars knows this kill.
677 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
678 MO->isKill()) {
679 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
680 if (std::find(VI.Kills.begin(),
681 VI.Kills.end(), MI) == VI.Kills.end())
682 report("Kill missing from LiveVariables", MO, MONum);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000683 }
684
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000685 // Check LiveInts liveness and kill.
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000686 if (TargetRegisterInfo::isVirtualRegister(Reg) &&
687 LiveInts && !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000688 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getRegSlot(true);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000689 if (LiveInts->hasInterval(Reg)) {
690 const LiveInterval &LI = LiveInts->getInterval(Reg);
691 if (!LI.liveAt(UseIdx)) {
692 report("No live range at use", MO, MONum);
693 *OS << UseIdx << " is not live in " << LI << '\n';
694 }
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000695 // Check for extra kill flags.
696 // Note that we allow missing kill flags for now.
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000697 if (MO->isKill() && !LI.killedAt(UseIdx.getRegSlot())) {
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000698 report("Live range continues after kill flag", MO, MONum);
699 *OS << "Live range: " << LI << '\n';
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000700 }
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000701 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000702 report("Virtual register has no Live interval", MO, MONum);
703 }
704 }
705
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000706 // Use of a dead register.
707 if (!regsLive.count(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000708 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen4af0f5f2011-07-30 00:57:25 +0000709 // Reserved registers may be used even when 'dead'.
710 if (!isReserved(Reg))
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000711 report("Using an undefined physical register", MO, MONum);
712 } else {
713 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
714 // We don't know which virtual registers are live in, so only complain
715 // if vreg was killed in this MBB. Otherwise keep track of vregs that
716 // must be live in. PHI instructions are handled separately.
717 if (MInfo.regsKilled.count(Reg))
718 report("Using a killed virtual register", MO, MONum);
Chris Lattner518bb532010-02-09 19:54:29 +0000719 else if (!MI->isPHI())
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000720 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
721 }
Duncan Sandse5567202009-05-16 03:28:54 +0000722 }
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000723 } else if (MO->isDef()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000724 // Register defined.
725 // TODO: verify that earlyclobber ops are not used.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000726 if (MO->isDead())
727 addRegWithSubRegs(regsDead, Reg);
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000728 else
729 addRegWithSubRegs(regsDefined, Reg);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000730
Jakob Stoklund Olesen93e6f022011-07-29 23:02:48 +0000731 // Verify SSA form.
732 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
733 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
734 report("Multiple virtual register defs in SSA form", MO, MONum);
735
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000736 // Check LiveInts for a live range, but only for virtual registers.
737 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
738 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000739 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getRegSlot();
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000740 if (LiveInts->hasInterval(Reg)) {
741 const LiveInterval &LI = LiveInts->getInterval(Reg);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000742 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
743 assert(VNI && "NULL valno is not allowed");
Cameron Zwarich1b031dd2010-12-19 23:50:53 +0000744 if (VNI->def != DefIdx && !MO->isEarlyClobber()) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000745 report("Inconsistent valno->def", MO, MONum);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000746 *OS << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000747 << DefIdx << " in " << LI << '\n';
748 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000749 } else {
750 report("No live range at def", MO, MONum);
751 *OS << DefIdx << " is not live in " << LI << '\n';
752 }
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000753 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000754 report("Virtual register has no Live interval", MO, MONum);
755 }
756 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000757 }
758
759 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000760 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000761 unsigned SubIdx = MO->getSubReg();
762
763 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000764 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000765 report("Illegal subregister index for physical register", MO, MONum);
766 return;
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 Olesenb4a02212011-10-05 22:12:57 +0000769 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000770 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000771 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000772 << DRC->getName() << " register.\n";
773 }
774 }
775 } else {
776 // Virtual register.
777 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
778 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000779 const TargetRegisterClass *SRC =
780 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000781 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000782 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000783 *OS << "Register class " << RC->getName()
784 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000785 return;
786 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000787 if (RC != SRC) {
788 report("Invalid register class for subregister index", MO, MONum);
789 *OS << "Register class " << RC->getName()
790 << " does not fully support subreg index " << SubIdx << "\n";
791 return;
792 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000793 }
Evan Chenge837dea2011-06-28 19:10:37 +0000794 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000795 if (SubIdx) {
796 const TargetRegisterClass *SuperRC =
797 TRI->getLargestLegalSuperClass(RC);
798 if (!SuperRC) {
799 report("No largest legal super class exists.", MO, MONum);
800 return;
801 }
802 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
803 if (!DRC) {
804 report("No matching super-reg register class.", MO, MONum);
805 return;
806 }
807 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000808 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000809 report("Illegal virtual register for instruction", MO, MONum);
810 *OS << "Expected a " << DRC->getName() << " register, but got a "
811 << RC->getName() << " register\n";
812 }
813 }
814 }
815 }
816 break;
817 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000818
819 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000820 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
821 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000822 break;
823
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000824 case MachineOperand::MO_FrameIndex:
825 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
826 LiveInts && !LiveInts->isNotInMIMap(MI)) {
827 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
828 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000829 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000830 report("Instruction loads from dead spill slot", MO, MONum);
831 *OS << "Live stack: " << LI << '\n';
832 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000833 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000834 report("Instruction stores to dead spill slot", MO, MONum);
835 *OS << "Live stack: " << LI << '\n';
836 }
837 }
838 break;
839
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000840 default:
841 break;
842 }
843}
844
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000845void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000846 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
847 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000848 set_subtract(regsLive, regsKilled); regsKilled.clear();
849 set_subtract(regsLive, regsDead); regsDead.clear();
850 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000851
852 if (Indexes && Indexes->hasIndex(MI)) {
853 SlotIndex idx = Indexes->getInstructionIndex(MI);
854 if (!(idx > lastIndex)) {
855 report("Instruction index out of order", MI);
856 *OS << "Last instruction was at " << lastIndex << '\n';
857 }
858 lastIndex = idx;
859 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000860}
861
862void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000863MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000864 MBBInfoMap[MBB].regsLiveOut = regsLive;
865 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000866
867 if (Indexes) {
868 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
869 if (!(stop > lastIndex)) {
870 report("Block ends before last instruction index", MBB);
871 *OS << "Block ends at " << stop
872 << " last instruction was at " << lastIndex << '\n';
873 }
874 lastIndex = stop;
875 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000876}
877
878// Calculate the largest possible vregsPassed sets. These are the registers that
879// can pass through an MBB live, but may not be live every time. It is assumed
880// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000881void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000882 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
883 // have any vregsPassed.
884 DenseSet<const MachineBasicBlock*> todo;
885 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
886 MFI != MFE; ++MFI) {
887 const MachineBasicBlock &MBB(*MFI);
888 BBInfo &MInfo = MBBInfoMap[&MBB];
889 if (!MInfo.reachable)
890 continue;
891 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
892 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
893 BBInfo &SInfo = MBBInfoMap[*SuI];
894 if (SInfo.addPassed(MInfo.regsLiveOut))
895 todo.insert(*SuI);
896 }
897 }
898
899 // Iteratively push vregsPassed to successors. This will converge to the same
900 // final state regardless of DenseSet iteration order.
901 while (!todo.empty()) {
902 const MachineBasicBlock *MBB = *todo.begin();
903 todo.erase(MBB);
904 BBInfo &MInfo = MBBInfoMap[MBB];
905 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
906 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
907 if (*SuI == MBB)
908 continue;
909 BBInfo &SInfo = MBBInfoMap[*SuI];
910 if (SInfo.addPassed(MInfo.vregsPassed))
911 todo.insert(*SuI);
912 }
913 }
914}
915
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000916// Calculate the set of virtual registers that must be passed through each basic
917// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000918// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000919void MachineVerifier::calcRegsRequired() {
920 // First push live-in regs to predecessors' vregsRequired.
921 DenseSet<const MachineBasicBlock*> todo;
922 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
923 MFI != MFE; ++MFI) {
924 const MachineBasicBlock &MBB(*MFI);
925 BBInfo &MInfo = MBBInfoMap[&MBB];
926 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
927 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
928 BBInfo &PInfo = MBBInfoMap[*PrI];
929 if (PInfo.addRequired(MInfo.vregsLiveIn))
930 todo.insert(*PrI);
931 }
932 }
933
934 // Iteratively push vregsRequired to predecessors. This will converge to the
935 // same final state regardless of DenseSet iteration order.
936 while (!todo.empty()) {
937 const MachineBasicBlock *MBB = *todo.begin();
938 todo.erase(MBB);
939 BBInfo &MInfo = MBBInfoMap[MBB];
940 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
941 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
942 if (*PrI == MBB)
943 continue;
944 BBInfo &SInfo = MBBInfoMap[*PrI];
945 if (SInfo.addRequired(MInfo.vregsRequired))
946 todo.insert(*PrI);
947 }
948 }
949}
950
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000951// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000952// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000953void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000954 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +0000955 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000956 DenseSet<const MachineBasicBlock*> seen;
957
958 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
959 unsigned Reg = BBI->getOperand(i).getReg();
960 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
961 if (!Pre->isSuccessor(MBB))
962 continue;
963 seen.insert(Pre);
964 BBInfo &PrInfo = MBBInfoMap[Pre];
965 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
966 report("PHI operand is not live-out from predecessor",
967 &BBI->getOperand(i), i);
968 }
969
970 // Did we see all predecessors?
971 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
972 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
973 if (!seen.count(*PrI)) {
974 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000975 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000976 << " is a predecessor according to the CFG.\n";
977 }
978 }
979 }
980}
981
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000982void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000983 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000984
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000985 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
986 MFI != MFE; ++MFI) {
987 BBInfo &MInfo = MBBInfoMap[MFI];
988
989 // Skip unreachable MBBs.
990 if (!MInfo.reachable)
991 continue;
992
993 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000994 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000995
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000996 // Now check liveness info if available
997 if (LiveVars || LiveInts)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000998 calcRegsRequired();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000999 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001000 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001001 if (LiveInts)
1002 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001003}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001004
1005void MachineVerifier::verifyLiveVariables() {
1006 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001007 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1008 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001009 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1010 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1011 MFI != MFE; ++MFI) {
1012 BBInfo &MInfo = MBBInfoMap[MFI];
1013
1014 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1015 if (MInfo.vregsRequired.count(Reg)) {
1016 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1017 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001018 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001019 << " must be live through the block.\n";
1020 }
1021 } else {
1022 if (VI.AliveBlocks.test(MFI->getNumber())) {
1023 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001024 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001025 << " is not needed live through the block.\n";
1026 }
1027 }
1028 }
1029 }
1030}
1031
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001032void MachineVerifier::verifyLiveIntervals() {
1033 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
1034 for (LiveIntervals::const_iterator LVI = LiveInts->begin(),
1035 LVE = LiveInts->end(); LVI != LVE; ++LVI) {
1036 const LiveInterval &LI = *LVI->second;
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001037
1038 // Spilling and splitting may leave unused registers around. Skip them.
1039 if (MRI->use_empty(LI.reg))
1040 continue;
1041
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001042 // Physical registers have much weirdness going on, mostly from coalescing.
1043 // We should probably fix it, but for now just ignore them.
1044 if (TargetRegisterInfo::isPhysicalRegister(LI.reg))
1045 continue;
1046
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001047 assert(LVI->first == LI.reg && "Invalid reg to interval mapping");
1048
1049 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1050 I!=E; ++I) {
1051 VNInfo *VNI = *I;
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001052 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001053
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001054 if (!DefVNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001055 if (!VNI->isUnused()) {
1056 report("Valno not live at def and not marked unused", MF);
1057 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1058 }
1059 continue;
1060 }
1061
1062 if (VNI->isUnused())
1063 continue;
1064
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001065 if (DefVNI != VNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001066 report("Live range at def has different valno", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001067 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001068 << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001069 continue;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001070 }
1071
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001072 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1073 if (!MBB) {
1074 report("Invalid definition index", MF);
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001075 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1076 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001077 continue;
1078 }
1079
1080 if (VNI->isPHIDef()) {
1081 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1082 report("PHIDef value is not defined at MBB start", MF);
1083 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001084 << ", not at the beginning of BB#" << MBB->getNumber()
1085 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001086 }
1087 } else {
1088 // Non-PHI def.
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001089 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1090 if (!MI) {
1091 report("No instruction at def index", MF);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001092 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1093 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001094 } else if (!MI->modifiesRegister(LI.reg, TRI)) {
1095 report("Defining instruction does not modify register", MI);
1096 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1097 }
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001098
1099 bool isEarlyClobber = false;
1100 if (MI) {
1101 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1102 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1103 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() &&
1104 MOI->isEarlyClobber()) {
1105 isEarlyClobber = true;
1106 break;
1107 }
1108 }
1109 }
1110
1111 // Early clobber defs begin at USE slots, but other defs must begin at
1112 // DEF slots.
1113 if (isEarlyClobber) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001114 if (!VNI->def.isEarlyClobber()) {
1115 report("Early clobber def must be at an early-clobber slot", MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001116 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1117 << " in " << LI << '\n';
1118 }
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001119 } else if (!VNI->def.isRegister()) {
1120 report("Non-PHI, non-early clobber def must be at a register slot",
1121 MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001122 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1123 << " in " << LI << '\n';
1124 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001125 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001126 }
1127
1128 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001129 const VNInfo *VNI = I->valno;
1130 assert(VNI && "Live range has no valno");
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001131
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001132 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001133 report("Foreign valno in live range", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001134 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001135 *OS << " has a valno not in " << LI << '\n';
1136 }
1137
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001138 if (VNI->isUnused()) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001139 report("Live range valno is marked unused", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001140 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001141 *OS << " in " << LI << '\n';
1142 }
1143
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001144 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1145 if (!MBB) {
1146 report("Bad start of live segment, no basic block", MF);
1147 I->print(*OS);
1148 *OS << " in " << LI << '\n';
1149 continue;
1150 }
1151 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1152 if (I->start != MBBStartIdx && I->start != VNI->def) {
1153 report("Live segment must begin at MBB entry or valno def", MBB);
1154 I->print(*OS);
1155 *OS << " in " << LI << '\n' << "Basic block starts at "
1156 << MBBStartIdx << '\n';
1157 }
1158
1159 const MachineBasicBlock *EndMBB =
1160 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1161 if (!EndMBB) {
1162 report("Bad end of live segment, no basic block", MF);
1163 I->print(*OS);
1164 *OS << " in " << LI << '\n';
1165 continue;
1166 }
1167 if (I->end != LiveInts->getMBBEndIdx(EndMBB)) {
1168 // The live segment is ending inside EndMBB
1169 const MachineInstr *MI =
1170 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1171 if (!MI) {
1172 report("Live segment doesn't end at a valid instruction", EndMBB);
1173 I->print(*OS);
1174 *OS << " in " << LI << '\n' << "Basic block starts at "
1175 << MBBStartIdx << '\n';
1176 } else if (TargetRegisterInfo::isVirtualRegister(LI.reg) &&
1177 !MI->readsVirtualRegister(LI.reg)) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001178 // A live range can end with either a redefinition, a kill flag on a
1179 // use, or a dead flag on a def.
1180 // FIXME: Should we check for each of these?
1181 bool hasDeadDef = false;
1182 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1183 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
Cameron Zwarich5e61f992010-12-20 02:59:51 +00001184 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() && MOI->isDead()) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001185 hasDeadDef = true;
1186 break;
1187 }
1188 }
1189
1190 if (!hasDeadDef) {
1191 report("Instruction killing live segment neither defines nor reads "
1192 "register", MI);
1193 I->print(*OS);
1194 *OS << " in " << LI << '\n';
1195 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001196 }
1197 }
1198
1199 // Now check all the basic blocks in this live segment.
1200 MachineFunction::const_iterator MFI = MBB;
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001201 // Is this live range the beginning of a non-PHIDef VN?
1202 if (I->start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001203 // Not live-in to any blocks.
1204 if (MBB == EndMBB)
1205 continue;
1206 // Skip this block.
1207 ++MFI;
1208 }
1209 for (;;) {
1210 assert(LiveInts->isLiveInToMBB(LI, MFI));
Jakob Stoklund Olesene459d552010-10-26 16:49:23 +00001211 // We don't know how to track physregs into a landing pad.
1212 if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1213 MFI->isLandingPad()) {
1214 if (&*MFI == EndMBB)
1215 break;
1216 ++MFI;
1217 continue;
1218 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001219 // Check that VNI is live-out of all predecessors.
1220 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1221 PE = MFI->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001222 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1223 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001224
Jakob Stoklund Olesendf8412c2011-09-15 05:16:30 +00001225 if (VNI->isPHIDef() && VNI->def == LiveInts->getMBBStartIdx(MFI))
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001226 continue;
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001227
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001228 if (!PVNI) {
1229 report("Register not marked live out of predecessor", *PI);
1230 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001231 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001232 << PEnd << " in " << LI << '\n';
1233 continue;
1234 }
1235
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001236 if (PVNI != VNI) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001237 report("Different value live out of predecessor", *PI);
1238 *OS << "Valno #" << PVNI->id << " live out of BB#"
1239 << (*PI)->getNumber() << '@' << PEnd
1240 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1241 << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n';
1242 }
1243 }
1244 if (&*MFI == EndMBB)
1245 break;
1246 ++MFI;
1247 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001248 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001249
1250 // Check the LI only has one connected component.
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001251 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1252 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1253 unsigned NumComp = ConEQ.Classify(&LI);
1254 if (NumComp > 1) {
1255 report("Multiple connected components in live interval", MF);
1256 *OS << NumComp << " components in " << LI << '\n';
Jakob Stoklund Olesencb367772010-10-29 00:40:57 +00001257 for (unsigned comp = 0; comp != NumComp; ++comp) {
1258 *OS << comp << ": valnos";
1259 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1260 E = LI.vni_end(); I!=E; ++I)
1261 if (comp == ConEQ.getEqClass(*I))
1262 *OS << ' ' << (*I)->id;
1263 *OS << '\n';
1264 }
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001265 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001266 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001267 }
1268}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001269