blob: c0df1b19a2650ee85873b92987c98bad54945bbd [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.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000591 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000592 if (LiveInts) {
593 bool mapped = !LiveInts->isNotInMIMap(MI);
594 if (MI->isDebugValue()) {
595 if (mapped)
596 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000597 } else if (MI->isInsideBundle()) {
598 if (mapped)
599 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000600 } else {
601 if (!mapped)
602 report("Missing slot index", MI);
603 }
604 }
605
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000606 // Ensure non-terminators don't follow terminators.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000607 if (MI->isTerminator()) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000608 if (!FirstTerminator)
609 FirstTerminator = MI;
610 } else if (FirstTerminator) {
611 report("Non-terminator instruction after the first terminator", MI);
612 *OS << "First terminator was:\t" << *FirstTerminator;
613 }
614
Andrew Trick3be654f2011-09-21 02:20:46 +0000615 StringRef ErrorInfo;
616 if (!TII->verifyInstruction(MI, ErrorInfo))
617 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000618}
619
620void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000621MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000622 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000623 const MCInstrDesc &MCID = MI->getDesc();
624 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000625
Evan Chenge837dea2011-06-28 19:10:37 +0000626 // The first MCID.NumDefs operands must be explicit register defines
627 if (MONum < MCID.getNumDefs()) {
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000628 if (!MO->isReg())
629 report("Explicit definition must be a register", MO, MONum);
630 else if (!MO->isDef())
631 report("Explicit definition marked as use", MO, MONum);
632 else if (MO->isImplicit())
633 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000634 } else if (MONum < MCID.getNumOperands()) {
Eric Christopher113a06c2010-11-17 00:55:36 +0000635 // Don't check if it's the last operand in a variadic instruction. See,
636 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000637 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000638 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000639 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000640 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000641 if (MO->isImplicit())
642 report("Explicit operand marked as implicit", MO, MONum);
643 }
644 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000645 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000646 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000647 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000648 }
649
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000650 switch (MO->getType()) {
651 case MachineOperand::MO_Register: {
652 const unsigned Reg = MO->getReg();
653 if (!Reg)
654 return;
655
656 // Check Live Variables.
Cameron Zwarich8ec88ba2010-12-20 00:08:10 +0000657 if (MI->isDebugValue()) {
658 // Liveness checks are not valid for debug values.
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000659 } else if (MO->isUse() && !MO->isUndef()) {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000660 regsLiveInButUnused.erase(Reg);
661
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000662 bool isKill = false;
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000663 unsigned defIdx;
664 if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
665 // A two-addr use counts as a kill if use and def are the same.
666 unsigned DefReg = MI->getOperand(defIdx).getReg();
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000667 if (Reg == DefReg)
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000668 isKill = true;
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000669 else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000670 report("Two-address instruction operands must be identical",
671 MO, MONum);
672 }
673 } else
674 isKill = MO->isKill();
675
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000676 if (isKill)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000677 addRegWithSubRegs(regsKilled, Reg);
678
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000679 // Check that LiveVars knows this kill.
680 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
681 MO->isKill()) {
682 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
683 if (std::find(VI.Kills.begin(),
684 VI.Kills.end(), MI) == VI.Kills.end())
685 report("Kill missing from LiveVariables", MO, MONum);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000686 }
687
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000688 // Check LiveInts liveness and kill.
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000689 if (TargetRegisterInfo::isVirtualRegister(Reg) &&
690 LiveInts && !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000691 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getRegSlot(true);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000692 if (LiveInts->hasInterval(Reg)) {
693 const LiveInterval &LI = LiveInts->getInterval(Reg);
694 if (!LI.liveAt(UseIdx)) {
695 report("No live range at use", MO, MONum);
696 *OS << UseIdx << " is not live in " << LI << '\n';
697 }
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000698 // Check for extra kill flags.
699 // Note that we allow missing kill flags for now.
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000700 if (MO->isKill() && !LI.killedAt(UseIdx.getRegSlot())) {
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000701 report("Live range continues after kill flag", MO, MONum);
702 *OS << "Live range: " << LI << '\n';
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000703 }
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000704 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000705 report("Virtual register has no Live interval", MO, MONum);
706 }
707 }
708
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000709 // Use of a dead register.
710 if (!regsLive.count(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000711 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen4af0f5f2011-07-30 00:57:25 +0000712 // Reserved registers may be used even when 'dead'.
713 if (!isReserved(Reg))
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000714 report("Using an undefined physical register", MO, MONum);
715 } else {
716 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
717 // We don't know which virtual registers are live in, so only complain
718 // if vreg was killed in this MBB. Otherwise keep track of vregs that
719 // must be live in. PHI instructions are handled separately.
720 if (MInfo.regsKilled.count(Reg))
721 report("Using a killed virtual register", MO, MONum);
Chris Lattner518bb532010-02-09 19:54:29 +0000722 else if (!MI->isPHI())
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000723 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
724 }
Duncan Sandse5567202009-05-16 03:28:54 +0000725 }
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000726 } else if (MO->isDef()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000727 // Register defined.
728 // TODO: verify that earlyclobber ops are not used.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000729 if (MO->isDead())
730 addRegWithSubRegs(regsDead, Reg);
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000731 else
732 addRegWithSubRegs(regsDefined, Reg);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000733
Jakob Stoklund Olesen93e6f022011-07-29 23:02:48 +0000734 // Verify SSA form.
735 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
736 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
737 report("Multiple virtual register defs in SSA form", MO, MONum);
738
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000739 // Check LiveInts for a live range, but only for virtual registers.
740 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
741 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000742 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getRegSlot();
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000743 if (LiveInts->hasInterval(Reg)) {
744 const LiveInterval &LI = LiveInts->getInterval(Reg);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000745 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
746 assert(VNI && "NULL valno is not allowed");
Cameron Zwarich1b031dd2010-12-19 23:50:53 +0000747 if (VNI->def != DefIdx && !MO->isEarlyClobber()) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000748 report("Inconsistent valno->def", MO, MONum);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000749 *OS << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000750 << DefIdx << " in " << LI << '\n';
751 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000752 } else {
753 report("No live range at def", MO, MONum);
754 *OS << DefIdx << " is not live in " << LI << '\n';
755 }
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000756 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000757 report("Virtual register has no Live interval", MO, MONum);
758 }
759 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000760 }
761
762 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000763 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000764 unsigned SubIdx = MO->getSubReg();
765
766 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000767 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000768 report("Illegal subregister index for physical register", MO, MONum);
769 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000770 }
Evan Chenge837dea2011-06-28 19:10:37 +0000771 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000772 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000773 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000774 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000775 << DRC->getName() << " register.\n";
776 }
777 }
778 } else {
779 // Virtual register.
780 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
781 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000782 const TargetRegisterClass *SRC =
783 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000784 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000785 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000786 *OS << "Register class " << RC->getName()
787 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000788 return;
789 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000790 if (RC != SRC) {
791 report("Invalid register class for subregister index", MO, MONum);
792 *OS << "Register class " << RC->getName()
793 << " does not fully support subreg index " << SubIdx << "\n";
794 return;
795 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000796 }
Evan Chenge837dea2011-06-28 19:10:37 +0000797 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000798 if (SubIdx) {
799 const TargetRegisterClass *SuperRC =
800 TRI->getLargestLegalSuperClass(RC);
801 if (!SuperRC) {
802 report("No largest legal super class exists.", MO, MONum);
803 return;
804 }
805 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
806 if (!DRC) {
807 report("No matching super-reg register class.", MO, MONum);
808 return;
809 }
810 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000811 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000812 report("Illegal virtual register for instruction", MO, MONum);
813 *OS << "Expected a " << DRC->getName() << " register, but got a "
814 << RC->getName() << " register\n";
815 }
816 }
817 }
818 }
819 break;
820 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000821
822 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000823 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
824 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000825 break;
826
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000827 case MachineOperand::MO_FrameIndex:
828 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
829 LiveInts && !LiveInts->isNotInMIMap(MI)) {
830 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
831 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000832 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000833 report("Instruction loads from dead spill slot", MO, MONum);
834 *OS << "Live stack: " << LI << '\n';
835 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000836 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000837 report("Instruction stores to dead spill slot", MO, MONum);
838 *OS << "Live stack: " << LI << '\n';
839 }
840 }
841 break;
842
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000843 default:
844 break;
845 }
846}
847
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000848void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000849 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
850 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000851 set_subtract(regsLive, regsKilled); regsKilled.clear();
852 set_subtract(regsLive, regsDead); regsDead.clear();
853 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000854
855 if (Indexes && Indexes->hasIndex(MI)) {
856 SlotIndex idx = Indexes->getInstructionIndex(MI);
857 if (!(idx > lastIndex)) {
858 report("Instruction index out of order", MI);
859 *OS << "Last instruction was at " << lastIndex << '\n';
860 }
861 lastIndex = idx;
862 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000863}
864
865void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000866MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000867 MBBInfoMap[MBB].regsLiveOut = regsLive;
868 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000869
870 if (Indexes) {
871 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
872 if (!(stop > lastIndex)) {
873 report("Block ends before last instruction index", MBB);
874 *OS << "Block ends at " << stop
875 << " last instruction was at " << lastIndex << '\n';
876 }
877 lastIndex = stop;
878 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000879}
880
881// Calculate the largest possible vregsPassed sets. These are the registers that
882// can pass through an MBB live, but may not be live every time. It is assumed
883// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000884void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000885 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
886 // have any vregsPassed.
887 DenseSet<const MachineBasicBlock*> todo;
888 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
889 MFI != MFE; ++MFI) {
890 const MachineBasicBlock &MBB(*MFI);
891 BBInfo &MInfo = MBBInfoMap[&MBB];
892 if (!MInfo.reachable)
893 continue;
894 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
895 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
896 BBInfo &SInfo = MBBInfoMap[*SuI];
897 if (SInfo.addPassed(MInfo.regsLiveOut))
898 todo.insert(*SuI);
899 }
900 }
901
902 // Iteratively push vregsPassed to successors. This will converge to the same
903 // final state regardless of DenseSet iteration order.
904 while (!todo.empty()) {
905 const MachineBasicBlock *MBB = *todo.begin();
906 todo.erase(MBB);
907 BBInfo &MInfo = MBBInfoMap[MBB];
908 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
909 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
910 if (*SuI == MBB)
911 continue;
912 BBInfo &SInfo = MBBInfoMap[*SuI];
913 if (SInfo.addPassed(MInfo.vregsPassed))
914 todo.insert(*SuI);
915 }
916 }
917}
918
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000919// Calculate the set of virtual registers that must be passed through each basic
920// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000921// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000922void MachineVerifier::calcRegsRequired() {
923 // First push live-in regs to predecessors' vregsRequired.
924 DenseSet<const MachineBasicBlock*> todo;
925 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
926 MFI != MFE; ++MFI) {
927 const MachineBasicBlock &MBB(*MFI);
928 BBInfo &MInfo = MBBInfoMap[&MBB];
929 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
930 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
931 BBInfo &PInfo = MBBInfoMap[*PrI];
932 if (PInfo.addRequired(MInfo.vregsLiveIn))
933 todo.insert(*PrI);
934 }
935 }
936
937 // Iteratively push vregsRequired to predecessors. This will converge to the
938 // same final state regardless of DenseSet iteration order.
939 while (!todo.empty()) {
940 const MachineBasicBlock *MBB = *todo.begin();
941 todo.erase(MBB);
942 BBInfo &MInfo = MBBInfoMap[MBB];
943 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
944 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
945 if (*PrI == MBB)
946 continue;
947 BBInfo &SInfo = MBBInfoMap[*PrI];
948 if (SInfo.addRequired(MInfo.vregsRequired))
949 todo.insert(*PrI);
950 }
951 }
952}
953
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000954// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000955// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000956void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000957 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +0000958 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000959 DenseSet<const MachineBasicBlock*> seen;
960
961 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
962 unsigned Reg = BBI->getOperand(i).getReg();
963 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
964 if (!Pre->isSuccessor(MBB))
965 continue;
966 seen.insert(Pre);
967 BBInfo &PrInfo = MBBInfoMap[Pre];
968 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
969 report("PHI operand is not live-out from predecessor",
970 &BBI->getOperand(i), i);
971 }
972
973 // Did we see all predecessors?
974 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
975 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
976 if (!seen.count(*PrI)) {
977 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000978 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000979 << " is a predecessor according to the CFG.\n";
980 }
981 }
982 }
983}
984
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000985void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000986 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000987
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000988 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
989 MFI != MFE; ++MFI) {
990 BBInfo &MInfo = MBBInfoMap[MFI];
991
992 // Skip unreachable MBBs.
993 if (!MInfo.reachable)
994 continue;
995
996 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000997 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000998
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000999 // Now check liveness info if available
1000 if (LiveVars || LiveInts)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001001 calcRegsRequired();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001002 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001003 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001004 if (LiveInts)
1005 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001006}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001007
1008void MachineVerifier::verifyLiveVariables() {
1009 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001010 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1011 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001012 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1013 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1014 MFI != MFE; ++MFI) {
1015 BBInfo &MInfo = MBBInfoMap[MFI];
1016
1017 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1018 if (MInfo.vregsRequired.count(Reg)) {
1019 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1020 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001021 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001022 << " must be live through the block.\n";
1023 }
1024 } else {
1025 if (VI.AliveBlocks.test(MFI->getNumber())) {
1026 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001027 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001028 << " is not needed live through the block.\n";
1029 }
1030 }
1031 }
1032 }
1033}
1034
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001035void MachineVerifier::verifyLiveIntervals() {
1036 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
1037 for (LiveIntervals::const_iterator LVI = LiveInts->begin(),
1038 LVE = LiveInts->end(); LVI != LVE; ++LVI) {
1039 const LiveInterval &LI = *LVI->second;
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001040
1041 // Spilling and splitting may leave unused registers around. Skip them.
1042 if (MRI->use_empty(LI.reg))
1043 continue;
1044
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001045 // Physical registers have much weirdness going on, mostly from coalescing.
1046 // We should probably fix it, but for now just ignore them.
1047 if (TargetRegisterInfo::isPhysicalRegister(LI.reg))
1048 continue;
1049
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001050 assert(LVI->first == LI.reg && "Invalid reg to interval mapping");
1051
1052 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1053 I!=E; ++I) {
1054 VNInfo *VNI = *I;
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001055 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001056
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001057 if (!DefVNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001058 if (!VNI->isUnused()) {
1059 report("Valno not live at def and not marked unused", MF);
1060 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1061 }
1062 continue;
1063 }
1064
1065 if (VNI->isUnused())
1066 continue;
1067
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001068 if (DefVNI != VNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001069 report("Live range at def has different valno", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001070 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001071 << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001072 continue;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001073 }
1074
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001075 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1076 if (!MBB) {
1077 report("Invalid definition index", MF);
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001078 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1079 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001080 continue;
1081 }
1082
1083 if (VNI->isPHIDef()) {
1084 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1085 report("PHIDef value is not defined at MBB start", MF);
1086 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001087 << ", not at the beginning of BB#" << MBB->getNumber()
1088 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001089 }
1090 } else {
1091 // Non-PHI def.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001092 MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001093 if (!MI) {
1094 report("No instruction at def index", MF);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001095 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1096 << " in " << LI << '\n';
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001097 continue;
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001098 }
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001099
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001100 bool hasDef = false;
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001101 bool isEarlyClobber = false;
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001102 for (MIOperands MOI(MI, true); MOI.isValid(); ++MOI) {
1103 if (!MOI->isReg() || !MOI->isDef())
1104 continue;
1105 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1106 if (MOI->getReg() != LI.reg)
1107 continue;
1108 } else {
1109 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1110 !TRI->regsOverlap(LI.reg, MOI->getReg()))
1111 continue;
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001112 }
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001113 hasDef = true;
1114 if (MOI->isEarlyClobber())
1115 isEarlyClobber = true;
1116 }
1117
1118 if (!hasDef) {
1119 report("Defining instruction does not modify register", MI);
1120 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001121 }
1122
1123 // Early clobber defs begin at USE slots, but other defs must begin at
1124 // DEF slots.
1125 if (isEarlyClobber) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001126 if (!VNI->def.isEarlyClobber()) {
1127 report("Early clobber def must be at an early-clobber slot", MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001128 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1129 << " in " << LI << '\n';
1130 }
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001131 } else if (!VNI->def.isRegister()) {
1132 report("Non-PHI, non-early clobber def must be at a register slot",
1133 MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001134 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1135 << " in " << LI << '\n';
1136 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001137 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001138 }
1139
1140 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001141 const VNInfo *VNI = I->valno;
1142 assert(VNI && "Live range has no valno");
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001143
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001144 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001145 report("Foreign valno in live range", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001146 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001147 *OS << " has a valno not in " << LI << '\n';
1148 }
1149
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001150 if (VNI->isUnused()) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001151 report("Live range valno is marked unused", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001152 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001153 *OS << " in " << LI << '\n';
1154 }
1155
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001156 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1157 if (!MBB) {
1158 report("Bad start of live segment, no basic block", MF);
1159 I->print(*OS);
1160 *OS << " in " << LI << '\n';
1161 continue;
1162 }
1163 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1164 if (I->start != MBBStartIdx && I->start != VNI->def) {
1165 report("Live segment must begin at MBB entry or valno def", MBB);
1166 I->print(*OS);
1167 *OS << " in " << LI << '\n' << "Basic block starts at "
1168 << MBBStartIdx << '\n';
1169 }
1170
1171 const MachineBasicBlock *EndMBB =
1172 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1173 if (!EndMBB) {
1174 report("Bad end of live segment, no basic block", MF);
1175 I->print(*OS);
1176 *OS << " in " << LI << '\n';
1177 continue;
1178 }
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001179
1180 // No more checks for live-out segments.
1181 if (I->end == LiveInts->getMBBEndIdx(EndMBB))
1182 continue;
1183
1184 // The live segment is ending inside EndMBB
1185 MachineInstr *MI =
1186 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1187 if (!MI) {
1188 report("Live segment doesn't end at a valid instruction", EndMBB);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001189 I->print(*OS);
1190 *OS << " in " << LI << '\n' << "Basic block starts at "
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001191 << MBBStartIdx << '\n';
1192 continue;
1193 }
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001194
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001195 // The block slot must refer to a basic block boundary.
1196 if (I->end.isBlock()) {
1197 report("Live segment ends at B slot of an instruction", MI);
1198 I->print(*OS);
1199 *OS << " in " << LI << '\n';
1200 }
1201
1202 if (I->end.isDead()) {
1203 // Segment ends on the dead slot.
1204 // That means there must be a dead def.
1205 if (!SlotIndex::isSameInstr(I->start, I->end)) {
1206 report("Live segment ending at dead slot spans instructions", MI);
1207 I->print(*OS);
1208 *OS << " in " << LI << '\n';
1209 }
1210 }
1211
1212 // A live segment can only end at an early-clobber slot if it is being
1213 // redefined by an early-clobber def.
1214 if (I->end.isEarlyClobber()) {
1215 if (I+1 == E || (I+1)->start != I->end) {
1216 report("Live segment ending at early clobber slot must be "
1217 "redefined by an EC def in the same instruction", MI);
1218 I->print(*OS);
1219 *OS << " in " << LI << '\n';
1220 }
1221 }
1222
1223 // The following checks only apply to virtual registers. Physreg liveness
1224 // is too weird to check.
1225 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1226 // A live range can end with either a redefinition, a kill flag on a
1227 // use, or a dead flag on a def.
1228 bool hasRead = false;
1229 bool hasDeadDef = false;
1230 for (MIOperands MOI(MI, true); MOI.isValid(); ++MOI) {
1231 if (!MOI->isReg() || MOI->getReg() != LI.reg)
1232 continue;
1233 if (MOI->readsReg())
1234 hasRead = true;
1235 if (MOI->isDef() && MOI->isDead())
1236 hasDeadDef = true;
1237 }
1238
1239 if (I->end.isDead()) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001240 if (!hasDeadDef) {
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001241 report("Instruction doesn't have a dead def operand", MI);
1242 I->print(*OS);
1243 *OS << " in " << LI << '\n';
1244 }
1245 } else {
1246 if (!hasRead) {
1247 report("Instruction ending live range doesn't read the register",
1248 MI);
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001249 I->print(*OS);
1250 *OS << " in " << LI << '\n';
1251 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001252 }
1253 }
1254
1255 // Now check all the basic blocks in this live segment.
1256 MachineFunction::const_iterator MFI = MBB;
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001257 // Is this live range the beginning of a non-PHIDef VN?
1258 if (I->start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001259 // Not live-in to any blocks.
1260 if (MBB == EndMBB)
1261 continue;
1262 // Skip this block.
1263 ++MFI;
1264 }
1265 for (;;) {
1266 assert(LiveInts->isLiveInToMBB(LI, MFI));
Jakob Stoklund Olesene459d552010-10-26 16:49:23 +00001267 // We don't know how to track physregs into a landing pad.
1268 if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1269 MFI->isLandingPad()) {
1270 if (&*MFI == EndMBB)
1271 break;
1272 ++MFI;
1273 continue;
1274 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001275 // Check that VNI is live-out of all predecessors.
1276 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1277 PE = MFI->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001278 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1279 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001280
Jakob Stoklund Olesendf8412c2011-09-15 05:16:30 +00001281 if (VNI->isPHIDef() && VNI->def == LiveInts->getMBBStartIdx(MFI))
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001282 continue;
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001283
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001284 if (!PVNI) {
1285 report("Register not marked live out of predecessor", *PI);
1286 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001287 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001288 << PEnd << " in " << LI << '\n';
1289 continue;
1290 }
1291
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001292 if (PVNI != VNI) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001293 report("Different value live out of predecessor", *PI);
1294 *OS << "Valno #" << PVNI->id << " live out of BB#"
1295 << (*PI)->getNumber() << '@' << PEnd
1296 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1297 << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n';
1298 }
1299 }
1300 if (&*MFI == EndMBB)
1301 break;
1302 ++MFI;
1303 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001304 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001305
1306 // Check the LI only has one connected component.
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001307 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1308 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1309 unsigned NumComp = ConEQ.Classify(&LI);
1310 if (NumComp > 1) {
1311 report("Multiple connected components in live interval", MF);
1312 *OS << NumComp << " components in " << LI << '\n';
Jakob Stoklund Olesencb367772010-10-29 00:40:57 +00001313 for (unsigned comp = 0; comp != NumComp; ++comp) {
1314 *OS << comp << ": valnos";
1315 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1316 E = LI.vni_end(); I!=E; ++I)
1317 if (comp == ConEQ.getEqClass(*I))
1318 *OS << ' ' << (*I)->id;
1319 *OS << '\n';
1320 }
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001321 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001322 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001323 }
1324}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001325