blob: 0e6120b98cdc874133260fd31c27dd5fda01ea57 [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 Olesen30e98a02012-02-29 00:33:41 +000031#include "llvm/CodeGen/MachineInstrBundle.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +000033#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman2dbc4c82009-10-07 17:36:00 +000034#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
36#include "llvm/CodeGen/Passes.h"
Bill Wendlingd29052b2011-05-04 22:54:05 +000037#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000038#include "llvm/Target/TargetMachine.h"
39#include "llvm/Target/TargetRegisterInfo.h"
40#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000041#include "llvm/ADT/DenseSet.h"
42#include "llvm/ADT/SetOperations.h"
43#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000044#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000045#include "llvm/Support/ErrorHandling.h"
46#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000047using namespace llvm;
48
49namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000050 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000051
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000052 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000053 PASS(pass),
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000054 Banner(b),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000055 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000056 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000057
58 bool runOnMachineFunction(MachineFunction &MF);
59
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000060 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000061 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000062 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000063 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000064 const MachineFunction *MF;
65 const TargetMachine *TM;
Evan Cheng15993f82011-06-27 21:26:13 +000066 const TargetInstrInfo *TII;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000067 const TargetRegisterInfo *TRI;
68 const MachineRegisterInfo *MRI;
69
70 unsigned foundErrors;
71
72 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000073 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000074 typedef DenseSet<unsigned> RegSet;
75 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
76
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000077 const MachineInstr *FirstTerminator;
78
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000079 BitVector regsReserved;
Lang Hames03698de2012-02-14 19:17:48 +000080 BitVector regsAllocatable;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000081 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000082 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000083 RegMaskVector regMasks;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000084 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000085
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000086 SlotIndex lastIndex;
87
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000088 // Add Reg and any sub-registers to RV
89 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
90 RV.push_back(Reg);
91 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topper9ebfbf82012-03-05 05:37:41 +000092 for (const uint16_t *R = TRI->getSubRegisters(Reg); *R; R++)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000093 RV.push_back(*R);
94 }
95
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000096 struct BBInfo {
97 // Is this MBB reachable from the MF entry point?
98 bool reachable;
99
100 // Vregs that must be live in because they are used without being
101 // defined. Map value is the user.
102 RegMap vregsLiveIn;
103
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000104 // Regs killed in MBB. They may be defined again, and will then be in both
105 // regsKilled and regsLiveOut.
106 RegSet regsKilled;
107
108 // Regs defined in MBB and live out. Note that vregs passing through may
109 // be live out without being mentioned here.
110 RegSet regsLiveOut;
111
112 // Vregs that pass through MBB untouched. This set is disjoint from
113 // regsKilled and regsLiveOut.
114 RegSet vregsPassed;
115
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000116 // Vregs that must pass through MBB because they are needed by a successor
117 // block. This set is disjoint from regsLiveOut.
118 RegSet vregsRequired;
119
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000120 BBInfo() : reachable(false) {}
121
122 // Add register to vregsPassed if it belongs there. Return true if
123 // anything changed.
124 bool addPassed(unsigned Reg) {
125 if (!TargetRegisterInfo::isVirtualRegister(Reg))
126 return false;
127 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
128 return false;
129 return vregsPassed.insert(Reg).second;
130 }
131
132 // Same for a full set.
133 bool addPassed(const RegSet &RS) {
134 bool changed = false;
135 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
136 if (addPassed(*I))
137 changed = true;
138 return changed;
139 }
140
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000141 // Add register to vregsRequired if it belongs there. Return true if
142 // anything changed.
143 bool addRequired(unsigned Reg) {
144 if (!TargetRegisterInfo::isVirtualRegister(Reg))
145 return false;
146 if (regsLiveOut.count(Reg))
147 return false;
148 return vregsRequired.insert(Reg).second;
149 }
150
151 // Same for a full set.
152 bool addRequired(const RegSet &RS) {
153 bool changed = false;
154 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
155 if (addRequired(*I))
156 changed = true;
157 return changed;
158 }
159
160 // Same for a full map.
161 bool addRequired(const RegMap &RM) {
162 bool changed = false;
163 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
164 if (addRequired(I->first))
165 changed = true;
166 return changed;
167 }
168
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000169 // Live-out registers are either in regsLiveOut or vregsPassed.
170 bool isLiveOut(unsigned Reg) const {
171 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
172 }
173 };
174
175 // Extra register info per MBB.
176 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
177
178 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000179 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000180 }
181
Lang Hames03698de2012-02-14 19:17:48 +0000182 bool isAllocatable(unsigned Reg) {
183 return Reg < regsAllocatable.size() && regsAllocatable.test(Reg);
184 }
185
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000186 // Analysis information if available
187 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000188 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000189 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000190 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000191
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000192 void visitMachineFunctionBefore();
193 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
194 void visitMachineInstrBefore(const MachineInstr *MI);
195 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
196 void visitMachineInstrAfter(const MachineInstr *MI);
197 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
198 void visitMachineFunctionAfter();
199
200 void report(const char *msg, const MachineFunction *MF);
201 void report(const char *msg, const MachineBasicBlock *MBB);
202 void report(const char *msg, const MachineInstr *MI);
203 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
204
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000205 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000206 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000207 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000208 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000209
210 void calcRegsRequired();
211 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000212 void verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000213 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000214
215 struct MachineVerifierPass : public MachineFunctionPass {
216 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000217 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000218
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000219 MachineVerifierPass(const char *b = 0)
220 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000221 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
222 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000223
224 void getAnalysisUsage(AnalysisUsage &AU) const {
225 AU.setPreservesAll();
226 MachineFunctionPass::getAnalysisUsage(AU);
227 }
228
229 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000230 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000231 return false;
232 }
233 };
234
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000235}
236
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000237char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000238INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000239 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000240
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000241FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
242 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000243}
244
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000245void MachineFunction::verify(Pass *p, const char *Banner) const {
246 MachineVerifier(p, Banner)
247 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000248}
249
Chris Lattner17e9edc2009-08-23 02:51:22 +0000250bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
251 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000252 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000253 std::string ErrorInfo;
254 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
255 raw_fd_ostream::F_Append);
256 if (!ErrorInfo.empty()) {
257 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
258 exit(1);
259 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000260
Chris Lattner17e9edc2009-08-23 02:51:22 +0000261 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000262 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000263 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000264 }
265
266 foundErrors = 0;
267
268 this->MF = &MF;
269 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000270 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000271 TRI = TM->getRegisterInfo();
272 MRI = &MF.getRegInfo();
273
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000274 LiveVars = NULL;
275 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000276 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000277 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000278 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000279 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000280 // We don't want to verify LiveVariables if LiveIntervals is available.
281 if (!LiveInts)
282 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000283 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000284 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000285 }
286
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000287 visitMachineFunctionBefore();
288 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
289 MFI!=MFE; ++MFI) {
290 visitMachineBasicBlockBefore(MFI);
Evan Chengddfd1372011-12-14 02:11:42 +0000291 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
292 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000293 if (MBBI->getParent() != MFI) {
294 report("Bad instruction parent pointer", MFI);
295 *OS << "Instruction: " << *MBBI;
296 continue;
297 }
Evan Chengddfd1372011-12-14 02:11:42 +0000298 // Skip BUNDLE instruction for now. FIXME: We should add code to verify
299 // the BUNDLE's specifically.
300 if (MBBI->isBundle())
301 continue;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000302 visitMachineInstrBefore(MBBI);
303 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
304 visitMachineOperand(&MBBI->getOperand(I), I);
305 visitMachineInstrAfter(MBBI);
306 }
307 visitMachineBasicBlockAfter(MFI);
308 }
309 visitMachineFunctionAfter();
310
Chris Lattner17e9edc2009-08-23 02:51:22 +0000311 if (OutFile)
312 delete OutFile;
313 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000314 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000315
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000316 // Clean up.
317 regsLive.clear();
318 regsDefined.clear();
319 regsDead.clear();
320 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000321 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000322 regsLiveInButUnused.clear();
323 MBBInfoMap.clear();
324
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000325 return false; // no changes
326}
327
Chris Lattner372fefe2009-08-23 01:03:30 +0000328void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000329 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000330 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000331 if (!foundErrors++) {
332 if (Banner)
333 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000334 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000335 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000336 *OS << "*** Bad machine code: " << msg << " ***\n"
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000337 << "- function: " << MF->getFunction()->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000338}
339
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000340void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000341 assert(MBB);
342 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000343 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000344 << " " << (void*)MBB
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000345 << " (BB#" << MBB->getNumber() << ")";
346 if (Indexes)
347 *OS << " [" << Indexes->getMBBStartIdx(MBB)
348 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
349 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000350}
351
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000352void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000353 assert(MI);
354 report(msg, MI->getParent());
355 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000356 if (Indexes && Indexes->hasIndex(MI))
357 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000358 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000359}
360
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000361void MachineVerifier::report(const char *msg,
362 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000363 assert(MO);
364 report(msg, MO->getParent());
365 *OS << "- operand " << MONum << ": ";
366 MO->print(*OS, TM);
367 *OS << "\n";
368}
369
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000370void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000371 BBInfo &MInfo = MBBInfoMap[MBB];
372 if (!MInfo.reachable) {
373 MInfo.reachable = true;
374 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
375 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
376 markReachable(*SuI);
377 }
378}
379
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000380void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000381 lastIndex = SlotIndex();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000382 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000383
384 // A sub-register of a reserved register is also reserved
385 for (int Reg = regsReserved.find_first(); Reg>=0;
386 Reg = regsReserved.find_next(Reg)) {
Craig Topper9ebfbf82012-03-05 05:37:41 +0000387 for (const uint16_t *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000388 // FIXME: This should probably be:
389 // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
390 regsReserved.set(*Sub);
391 }
392 }
Lang Hames03698de2012-02-14 19:17:48 +0000393
394 regsAllocatable = TRI->getAllocatableSet(*MF);
395
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000396 markReachable(&MF->front());
397}
398
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000399// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000400static bool matchPair(MachineBasicBlock::const_succ_iterator i,
401 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000402 if (*i == a)
403 return *++i == b;
404 if (*i == b)
405 return *++i == a;
406 return false;
407}
408
409void
410MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000411 FirstTerminator = 0;
412
Lang Hames03698de2012-02-14 19:17:48 +0000413 if (MRI->isSSA()) {
414 // If this block has allocatable physical registers live-in, check that
415 // it is an entry block or landing pad.
416 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
417 LE = MBB->livein_end();
418 LI != LE; ++LI) {
419 unsigned reg = *LI;
420 if (isAllocatable(reg) && !MBB->isLandingPad() &&
421 MBB != MBB->getParent()->begin()) {
422 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
423 }
424 }
425 }
426
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000427 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000428 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000429 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000430 E = MBB->succ_end(); I != E; ++I) {
431 if ((*I)->isLandingPad())
432 LandingPadSuccs.insert(*I);
433 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000434
435 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
436 const BasicBlock *BB = MBB->getBasicBlock();
437 if (LandingPadSuccs.size() > 1 &&
438 !(AsmInfo &&
439 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
440 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000441 report("MBB has more than one landing pad successor", MBB);
442
Dan Gohman27920592009-08-27 02:43:49 +0000443 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
444 MachineBasicBlock *TBB = 0, *FBB = 0;
445 SmallVector<MachineOperand, 4> Cond;
446 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
447 TBB, FBB, Cond)) {
448 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
449 // check whether its answers match up with reality.
450 if (!TBB && !FBB) {
451 // Block falls through to its successor.
452 MachineFunction::const_iterator MBBI = MBB;
453 ++MBBI;
454 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000455 // It's possible that the block legitimately ends with a noreturn
456 // call or an unreachable, in which case it won't actually fall
457 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000458 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000459 // It's possible that the block legitimately ends with a noreturn
460 // call or an unreachable, in which case it won't actuall fall
461 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000462 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000463 report("MBB exits via unconditional fall-through but doesn't have "
464 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000465 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000466 report("MBB exits via unconditional fall-through but its successor "
467 "differs from its CFG successor!", MBB);
468 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000469 if (!MBB->empty() && MBB->back().isBarrier() &&
Evan Cheng86050dc2010-06-18 23:09:54 +0000470 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000471 report("MBB exits via unconditional fall-through but ends with a "
472 "barrier instruction!", MBB);
473 }
474 if (!Cond.empty()) {
475 report("MBB exits via unconditional fall-through but has a condition!",
476 MBB);
477 }
478 } else if (TBB && !FBB && Cond.empty()) {
479 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000480 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000481 report("MBB exits via unconditional branch but doesn't have "
482 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000483 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000484 report("MBB exits via unconditional branch but the CFG "
485 "successor doesn't match the actual successor!", MBB);
486 }
487 if (MBB->empty()) {
488 report("MBB exits via unconditional branch but doesn't contain "
489 "any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000490 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000491 report("MBB exits via unconditional branch but doesn't end with a "
492 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000493 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000494 report("MBB exits via unconditional branch but the branch isn't a "
495 "terminator instruction!", MBB);
496 }
497 } else if (TBB && !FBB && !Cond.empty()) {
498 // Block conditionally branches somewhere, otherwise falls through.
499 MachineFunction::const_iterator MBBI = MBB;
500 ++MBBI;
501 if (MBBI == MF->end()) {
502 report("MBB conditionally falls through out of function!", MBB);
503 } if (MBB->succ_size() != 2) {
504 report("MBB exits via conditional branch/fall-through but doesn't have "
505 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000506 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000507 report("MBB exits via conditional branch/fall-through but the CFG "
508 "successors don't match the actual successors!", MBB);
509 }
510 if (MBB->empty()) {
511 report("MBB exits via conditional branch/fall-through but doesn't "
512 "contain any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000513 } else if (MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000514 report("MBB exits via conditional branch/fall-through but ends with a "
515 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000516 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000517 report("MBB exits via conditional branch/fall-through but the branch "
518 "isn't a terminator instruction!", MBB);
519 }
520 } else if (TBB && FBB) {
521 // Block conditionally branches somewhere, otherwise branches
522 // somewhere else.
523 if (MBB->succ_size() != 2) {
524 report("MBB exits via conditional branch/branch but doesn't have "
525 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000526 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000527 report("MBB exits via conditional branch/branch but the CFG "
528 "successors don't match the actual successors!", MBB);
529 }
530 if (MBB->empty()) {
531 report("MBB exits via conditional branch/branch but doesn't "
532 "contain any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000533 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000534 report("MBB exits via conditional branch/branch but doesn't end with a "
535 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000536 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000537 report("MBB exits via conditional branch/branch but the branch "
538 "isn't a terminator instruction!", MBB);
539 }
540 if (Cond.empty()) {
541 report("MBB exits via conditinal branch/branch but there's no "
542 "condition!", MBB);
543 }
544 } else {
545 report("AnalyzeBranch returned invalid data!", MBB);
546 }
547 }
548
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000549 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000550 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000551 E = MBB->livein_end(); I != E; ++I) {
552 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
553 report("MBB live-in list contains non-physical register", MBB);
554 continue;
555 }
556 regsLive.insert(*I);
Craig Topper9ebfbf82012-03-05 05:37:41 +0000557 for (const uint16_t *R = TRI->getSubRegisters(*I); *R; R++)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000558 regsLive.insert(*R);
559 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000560 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000561
562 const MachineFrameInfo *MFI = MF->getFrameInfo();
563 assert(MFI && "Function has no frame info");
564 BitVector PR = MFI->getPristineRegs(MBB);
565 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
566 regsLive.insert(I);
Craig Topper9ebfbf82012-03-05 05:37:41 +0000567 for (const uint16_t *R = TRI->getSubRegisters(I); *R; R++)
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000568 regsLive.insert(*R);
569 }
570
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000571 regsKilled.clear();
572 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000573
574 if (Indexes)
575 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000576}
577
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000578void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000579 const MCInstrDesc &MCID = MI->getDesc();
580 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000581 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000582 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000583 << MI->getNumExplicitOperands() << " given.\n";
584 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000585
586 // Check the MachineMemOperands for basic consistency.
587 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
588 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000589 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000590 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000591 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000592 report("Missing mayStore flag", MI);
593 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000594
595 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000596 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000597 if (LiveInts) {
598 bool mapped = !LiveInts->isNotInMIMap(MI);
599 if (MI->isDebugValue()) {
600 if (mapped)
601 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000602 } else if (MI->isInsideBundle()) {
603 if (mapped)
604 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000605 } else {
606 if (!mapped)
607 report("Missing slot index", MI);
608 }
609 }
610
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000611 // Ensure non-terminators don't follow terminators.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000612 if (MI->isTerminator()) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000613 if (!FirstTerminator)
614 FirstTerminator = MI;
615 } else if (FirstTerminator) {
616 report("Non-terminator instruction after the first terminator", MI);
617 *OS << "First terminator was:\t" << *FirstTerminator;
618 }
619
Andrew Trick3be654f2011-09-21 02:20:46 +0000620 StringRef ErrorInfo;
621 if (!TII->verifyInstruction(MI, ErrorInfo))
622 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000623}
624
625void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000626MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000627 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000628 const MCInstrDesc &MCID = MI->getDesc();
629 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000630
Evan Chenge837dea2011-06-28 19:10:37 +0000631 // The first MCID.NumDefs operands must be explicit register defines
632 if (MONum < MCID.getNumDefs()) {
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000633 if (!MO->isReg())
634 report("Explicit definition must be a register", MO, MONum);
635 else if (!MO->isDef())
636 report("Explicit definition marked as use", MO, MONum);
637 else if (MO->isImplicit())
638 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000639 } else if (MONum < MCID.getNumOperands()) {
Eric Christopher113a06c2010-11-17 00:55:36 +0000640 // Don't check if it's the last operand in a variadic instruction. See,
641 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000642 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000643 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000644 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000645 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000646 if (MO->isImplicit())
647 report("Explicit operand marked as implicit", MO, MONum);
648 }
649 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000650 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000651 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000652 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000653 }
654
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000655 switch (MO->getType()) {
656 case MachineOperand::MO_Register: {
657 const unsigned Reg = MO->getReg();
658 if (!Reg)
659 return;
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000660 if (MRI->tracksLiveness() && !MI->isDebugValue())
661 checkLiveness(MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000662
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000663
664 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000665 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000666 unsigned SubIdx = MO->getSubReg();
667
668 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000669 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000670 report("Illegal subregister index for physical register", MO, MONum);
671 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000672 }
Evan Chenge837dea2011-06-28 19:10:37 +0000673 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000674 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000675 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000676 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000677 << DRC->getName() << " register.\n";
678 }
679 }
680 } else {
681 // Virtual register.
682 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
683 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000684 const TargetRegisterClass *SRC =
685 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000686 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000687 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000688 *OS << "Register class " << RC->getName()
689 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000690 return;
691 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000692 if (RC != SRC) {
693 report("Invalid register class for subregister index", MO, MONum);
694 *OS << "Register class " << RC->getName()
695 << " does not fully support subreg index " << SubIdx << "\n";
696 return;
697 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000698 }
Evan Chenge837dea2011-06-28 19:10:37 +0000699 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000700 if (SubIdx) {
701 const TargetRegisterClass *SuperRC =
702 TRI->getLargestLegalSuperClass(RC);
703 if (!SuperRC) {
704 report("No largest legal super class exists.", MO, MONum);
705 return;
706 }
707 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
708 if (!DRC) {
709 report("No matching super-reg register class.", MO, MONum);
710 return;
711 }
712 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000713 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000714 report("Illegal virtual register for instruction", MO, MONum);
715 *OS << "Expected a " << DRC->getName() << " register, but got a "
716 << RC->getName() << " register\n";
717 }
718 }
719 }
720 }
721 break;
722 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000723
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000724 case MachineOperand::MO_RegisterMask:
725 regMasks.push_back(MO->getRegMask());
726 break;
727
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000728 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000729 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
730 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000731 break;
732
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000733 case MachineOperand::MO_FrameIndex:
734 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
735 LiveInts && !LiveInts->isNotInMIMap(MI)) {
736 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
737 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000738 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000739 report("Instruction loads from dead spill slot", MO, MONum);
740 *OS << "Live stack: " << LI << '\n';
741 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000742 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000743 report("Instruction stores to dead spill slot", MO, MONum);
744 *OS << "Live stack: " << LI << '\n';
745 }
746 }
747 break;
748
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000749 default:
750 break;
751 }
752}
753
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000754void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
755 const MachineInstr *MI = MO->getParent();
756 const unsigned Reg = MO->getReg();
757
758 // Both use and def operands can read a register.
759 if (MO->readsReg()) {
760 regsLiveInButUnused.erase(Reg);
761
762 bool isKill = false;
763 unsigned defIdx;
764 if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
765 // A two-addr use counts as a kill if use and def are the same.
766 unsigned DefReg = MI->getOperand(defIdx).getReg();
767 if (Reg == DefReg)
768 isKill = true;
769 else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
770 report("Two-address instruction operands must be identical", MO, MONum);
771 }
772 } else
773 isKill = MO->isKill();
774
775 if (isKill)
776 addRegWithSubRegs(regsKilled, Reg);
777
778 // Check that LiveVars knows this kill.
779 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
780 MO->isKill()) {
781 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
782 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
783 report("Kill missing from LiveVariables", MO, MONum);
784 }
785
786 // Check LiveInts liveness and kill.
787 if (TargetRegisterInfo::isVirtualRegister(Reg) &&
788 LiveInts && !LiveInts->isNotInMIMap(MI)) {
789 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getRegSlot(true);
790 if (LiveInts->hasInterval(Reg)) {
791 const LiveInterval &LI = LiveInts->getInterval(Reg);
792 if (!LI.liveAt(UseIdx)) {
793 report("No live range at use", MO, MONum);
794 *OS << UseIdx << " is not live in " << LI << '\n';
795 }
796 // Check for extra kill flags.
797 // Note that we allow missing kill flags for now.
798 if (MO->isKill() && !LI.killedAt(UseIdx.getRegSlot())) {
799 report("Live range continues after kill flag", MO, MONum);
800 *OS << "Live range: " << LI << '\n';
801 }
802 } else {
803 report("Virtual register has no Live interval", MO, MONum);
804 }
805 }
806
807 // Use of a dead register.
808 if (!regsLive.count(Reg)) {
809 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
810 // Reserved registers may be used even when 'dead'.
811 if (!isReserved(Reg))
812 report("Using an undefined physical register", MO, MONum);
813 } else {
814 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
815 // We don't know which virtual registers are live in, so only complain
816 // if vreg was killed in this MBB. Otherwise keep track of vregs that
817 // must be live in. PHI instructions are handled separately.
818 if (MInfo.regsKilled.count(Reg))
819 report("Using a killed virtual register", MO, MONum);
820 else if (!MI->isPHI())
821 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
822 }
823 }
824 }
825
826 if (MO->isDef()) {
827 // Register defined.
828 // TODO: verify that earlyclobber ops are not used.
829 if (MO->isDead())
830 addRegWithSubRegs(regsDead, Reg);
831 else
832 addRegWithSubRegs(regsDefined, Reg);
833
834 // Verify SSA form.
835 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
836 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
837 report("Multiple virtual register defs in SSA form", MO, MONum);
838
839 // Check LiveInts for a live range, but only for virtual registers.
840 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
841 !LiveInts->isNotInMIMap(MI)) {
842 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getRegSlot();
843 if (LiveInts->hasInterval(Reg)) {
844 const LiveInterval &LI = LiveInts->getInterval(Reg);
845 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
846 assert(VNI && "NULL valno is not allowed");
847 if (VNI->def != DefIdx && !MO->isEarlyClobber()) {
848 report("Inconsistent valno->def", MO, MONum);
849 *OS << "Valno " << VNI->id << " is not defined at "
850 << DefIdx << " in " << LI << '\n';
851 }
852 } else {
853 report("No live range at def", MO, MONum);
854 *OS << DefIdx << " is not live in " << LI << '\n';
855 }
856 } else {
857 report("Virtual register has no Live interval", MO, MONum);
858 }
859 }
860 }
861}
862
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000863void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000864 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
865 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000866 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000867 // Kill any masked registers.
868 while (!regMasks.empty()) {
869 const uint32_t *Mask = regMasks.pop_back_val();
870 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
871 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
872 MachineOperand::clobbersPhysReg(Mask, *I))
873 regsDead.push_back(*I);
874 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000875 set_subtract(regsLive, regsDead); regsDead.clear();
876 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000877
878 if (Indexes && Indexes->hasIndex(MI)) {
879 SlotIndex idx = Indexes->getInstructionIndex(MI);
880 if (!(idx > lastIndex)) {
881 report("Instruction index out of order", MI);
882 *OS << "Last instruction was at " << lastIndex << '\n';
883 }
884 lastIndex = idx;
885 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000886}
887
888void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000889MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000890 MBBInfoMap[MBB].regsLiveOut = regsLive;
891 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000892
893 if (Indexes) {
894 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
895 if (!(stop > lastIndex)) {
896 report("Block ends before last instruction index", MBB);
897 *OS << "Block ends at " << stop
898 << " last instruction was at " << lastIndex << '\n';
899 }
900 lastIndex = stop;
901 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000902}
903
904// Calculate the largest possible vregsPassed sets. These are the registers that
905// can pass through an MBB live, but may not be live every time. It is assumed
906// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000907void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000908 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
909 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +0000910 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000911 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
912 MFI != MFE; ++MFI) {
913 const MachineBasicBlock &MBB(*MFI);
914 BBInfo &MInfo = MBBInfoMap[&MBB];
915 if (!MInfo.reachable)
916 continue;
917 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
918 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
919 BBInfo &SInfo = MBBInfoMap[*SuI];
920 if (SInfo.addPassed(MInfo.regsLiveOut))
921 todo.insert(*SuI);
922 }
923 }
924
925 // Iteratively push vregsPassed to successors. This will converge to the same
926 // final state regardless of DenseSet iteration order.
927 while (!todo.empty()) {
928 const MachineBasicBlock *MBB = *todo.begin();
929 todo.erase(MBB);
930 BBInfo &MInfo = MBBInfoMap[MBB];
931 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
932 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
933 if (*SuI == MBB)
934 continue;
935 BBInfo &SInfo = MBBInfoMap[*SuI];
936 if (SInfo.addPassed(MInfo.vregsPassed))
937 todo.insert(*SuI);
938 }
939 }
940}
941
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000942// Calculate the set of virtual registers that must be passed through each basic
943// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000944// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000945void MachineVerifier::calcRegsRequired() {
946 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +0000947 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000948 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
949 MFI != MFE; ++MFI) {
950 const MachineBasicBlock &MBB(*MFI);
951 BBInfo &MInfo = MBBInfoMap[&MBB];
952 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
953 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
954 BBInfo &PInfo = MBBInfoMap[*PrI];
955 if (PInfo.addRequired(MInfo.vregsLiveIn))
956 todo.insert(*PrI);
957 }
958 }
959
960 // Iteratively push vregsRequired to predecessors. This will converge to the
961 // same final state regardless of DenseSet iteration order.
962 while (!todo.empty()) {
963 const MachineBasicBlock *MBB = *todo.begin();
964 todo.erase(MBB);
965 BBInfo &MInfo = MBBInfoMap[MBB];
966 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
967 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
968 if (*PrI == MBB)
969 continue;
970 BBInfo &SInfo = MBBInfoMap[*PrI];
971 if (SInfo.addRequired(MInfo.vregsRequired))
972 todo.insert(*PrI);
973 }
974 }
975}
976
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000977// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000978// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000979void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +0000980 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000981 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +0000982 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +0000983 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000984
985 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
986 unsigned Reg = BBI->getOperand(i).getReg();
987 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
988 if (!Pre->isSuccessor(MBB))
989 continue;
990 seen.insert(Pre);
991 BBInfo &PrInfo = MBBInfoMap[Pre];
992 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
993 report("PHI operand is not live-out from predecessor",
994 &BBI->getOperand(i), i);
995 }
996
997 // Did we see all predecessors?
998 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
999 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1000 if (!seen.count(*PrI)) {
1001 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +00001002 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001003 << " is a predecessor according to the CFG.\n";
1004 }
1005 }
1006 }
1007}
1008
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001009void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001010 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001011
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001012 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1013 MFI != MFE; ++MFI) {
1014 BBInfo &MInfo = MBBInfoMap[MFI];
1015
1016 // Skip unreachable MBBs.
1017 if (!MInfo.reachable)
1018 continue;
1019
1020 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001021 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001022
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001023 // Now check liveness info if available
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001024 calcRegsRequired();
1025
1026 if (MRI->isSSA() && !MF->empty()) {
1027 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1028 for (RegSet::iterator
1029 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesenff0275e2012-03-10 00:44:11 +00001030 ++I)
1031 report("Virtual register def doesn't dominate all uses.",
1032 MRI->getVRegDef(*I));
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001033 }
1034
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001035 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001036 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001037 if (LiveInts)
1038 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001039}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001040
1041void MachineVerifier::verifyLiveVariables() {
1042 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001043 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1044 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001045 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1046 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1047 MFI != MFE; ++MFI) {
1048 BBInfo &MInfo = MBBInfoMap[MFI];
1049
1050 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1051 if (MInfo.vregsRequired.count(Reg)) {
1052 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1053 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001054 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001055 << " must be live through the block.\n";
1056 }
1057 } else {
1058 if (VI.AliveBlocks.test(MFI->getNumber())) {
1059 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001060 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001061 << " is not needed live through the block.\n";
1062 }
1063 }
1064 }
1065 }
1066}
1067
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001068void MachineVerifier::verifyLiveIntervals() {
1069 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
1070 for (LiveIntervals::const_iterator LVI = LiveInts->begin(),
1071 LVE = LiveInts->end(); LVI != LVE; ++LVI) {
1072 const LiveInterval &LI = *LVI->second;
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001073
1074 // Spilling and splitting may leave unused registers around. Skip them.
1075 if (MRI->use_empty(LI.reg))
1076 continue;
1077
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001078 // Physical registers have much weirdness going on, mostly from coalescing.
1079 // We should probably fix it, but for now just ignore them.
1080 if (TargetRegisterInfo::isPhysicalRegister(LI.reg))
1081 continue;
1082
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001083 assert(LVI->first == LI.reg && "Invalid reg to interval mapping");
1084
1085 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1086 I!=E; ++I) {
1087 VNInfo *VNI = *I;
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001088 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001089
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001090 if (!DefVNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001091 if (!VNI->isUnused()) {
1092 report("Valno not live at def and not marked unused", MF);
1093 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1094 }
1095 continue;
1096 }
1097
1098 if (VNI->isUnused())
1099 continue;
1100
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001101 if (DefVNI != VNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001102 report("Live range at def has different valno", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001103 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001104 << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001105 continue;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001106 }
1107
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001108 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1109 if (!MBB) {
1110 report("Invalid definition index", MF);
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001111 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1112 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001113 continue;
1114 }
1115
1116 if (VNI->isPHIDef()) {
1117 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1118 report("PHIDef value is not defined at MBB start", MF);
1119 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001120 << ", not at the beginning of BB#" << MBB->getNumber()
1121 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001122 }
1123 } else {
1124 // Non-PHI def.
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +00001125 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001126 if (!MI) {
1127 report("No instruction at def index", MF);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001128 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1129 << " in " << LI << '\n';
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001130 continue;
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001131 }
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001132
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001133 bool hasDef = false;
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001134 bool isEarlyClobber = false;
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +00001135 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001136 if (!MOI->isReg() || !MOI->isDef())
1137 continue;
1138 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1139 if (MOI->getReg() != LI.reg)
1140 continue;
1141 } else {
1142 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1143 !TRI->regsOverlap(LI.reg, MOI->getReg()))
1144 continue;
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001145 }
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001146 hasDef = true;
1147 if (MOI->isEarlyClobber())
1148 isEarlyClobber = true;
1149 }
1150
1151 if (!hasDef) {
1152 report("Defining instruction does not modify register", MI);
1153 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001154 }
1155
1156 // Early clobber defs begin at USE slots, but other defs must begin at
1157 // DEF slots.
1158 if (isEarlyClobber) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001159 if (!VNI->def.isEarlyClobber()) {
1160 report("Early clobber def must be at an early-clobber slot", MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001161 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1162 << " in " << LI << '\n';
1163 }
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001164 } else if (!VNI->def.isRegister()) {
1165 report("Non-PHI, non-early clobber def must be at a register slot",
1166 MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001167 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1168 << " in " << LI << '\n';
1169 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001170 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001171 }
1172
1173 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001174 const VNInfo *VNI = I->valno;
1175 assert(VNI && "Live range has no valno");
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001176
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001177 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001178 report("Foreign valno in live range", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001179 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001180 *OS << " has a valno not in " << LI << '\n';
1181 }
1182
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001183 if (VNI->isUnused()) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001184 report("Live range valno is marked unused", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001185 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001186 *OS << " in " << LI << '\n';
1187 }
1188
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001189 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1190 if (!MBB) {
1191 report("Bad start of live segment, no basic block", MF);
1192 I->print(*OS);
1193 *OS << " in " << LI << '\n';
1194 continue;
1195 }
1196 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1197 if (I->start != MBBStartIdx && I->start != VNI->def) {
1198 report("Live segment must begin at MBB entry or valno def", MBB);
1199 I->print(*OS);
1200 *OS << " in " << LI << '\n' << "Basic block starts at "
1201 << MBBStartIdx << '\n';
1202 }
1203
1204 const MachineBasicBlock *EndMBB =
1205 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1206 if (!EndMBB) {
1207 report("Bad end of live segment, no basic block", MF);
1208 I->print(*OS);
1209 *OS << " in " << LI << '\n';
1210 continue;
1211 }
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001212
1213 // No more checks for live-out segments.
1214 if (I->end == LiveInts->getMBBEndIdx(EndMBB))
1215 continue;
1216
1217 // The live segment is ending inside EndMBB
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +00001218 const MachineInstr *MI =
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001219 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1220 if (!MI) {
1221 report("Live segment doesn't end at a valid instruction", EndMBB);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001222 I->print(*OS);
1223 *OS << " in " << LI << '\n' << "Basic block starts at "
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001224 << MBBStartIdx << '\n';
1225 continue;
1226 }
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001227
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001228 // The block slot must refer to a basic block boundary.
1229 if (I->end.isBlock()) {
1230 report("Live segment ends at B slot of an instruction", MI);
1231 I->print(*OS);
1232 *OS << " in " << LI << '\n';
1233 }
1234
1235 if (I->end.isDead()) {
1236 // Segment ends on the dead slot.
1237 // That means there must be a dead def.
1238 if (!SlotIndex::isSameInstr(I->start, I->end)) {
1239 report("Live segment ending at dead slot spans instructions", MI);
1240 I->print(*OS);
1241 *OS << " in " << LI << '\n';
1242 }
1243 }
1244
1245 // A live segment can only end at an early-clobber slot if it is being
1246 // redefined by an early-clobber def.
1247 if (I->end.isEarlyClobber()) {
1248 if (I+1 == E || (I+1)->start != I->end) {
1249 report("Live segment ending at early clobber slot must be "
1250 "redefined by an EC def in the same instruction", MI);
1251 I->print(*OS);
1252 *OS << " in " << LI << '\n';
1253 }
1254 }
1255
1256 // The following checks only apply to virtual registers. Physreg liveness
1257 // is too weird to check.
1258 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1259 // A live range can end with either a redefinition, a kill flag on a
1260 // use, or a dead flag on a def.
1261 bool hasRead = false;
1262 bool hasDeadDef = false;
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +00001263 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001264 if (!MOI->isReg() || MOI->getReg() != LI.reg)
1265 continue;
1266 if (MOI->readsReg())
1267 hasRead = true;
1268 if (MOI->isDef() && MOI->isDead())
1269 hasDeadDef = true;
1270 }
1271
1272 if (I->end.isDead()) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001273 if (!hasDeadDef) {
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001274 report("Instruction doesn't have a dead def operand", MI);
1275 I->print(*OS);
1276 *OS << " in " << LI << '\n';
1277 }
1278 } else {
1279 if (!hasRead) {
1280 report("Instruction ending live range doesn't read the register",
1281 MI);
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001282 I->print(*OS);
1283 *OS << " in " << LI << '\n';
1284 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001285 }
1286 }
1287
1288 // Now check all the basic blocks in this live segment.
1289 MachineFunction::const_iterator MFI = MBB;
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001290 // Is this live range the beginning of a non-PHIDef VN?
1291 if (I->start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001292 // Not live-in to any blocks.
1293 if (MBB == EndMBB)
1294 continue;
1295 // Skip this block.
1296 ++MFI;
1297 }
1298 for (;;) {
1299 assert(LiveInts->isLiveInToMBB(LI, MFI));
Jakob Stoklund Olesene459d552010-10-26 16:49:23 +00001300 // We don't know how to track physregs into a landing pad.
1301 if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1302 MFI->isLandingPad()) {
1303 if (&*MFI == EndMBB)
1304 break;
1305 ++MFI;
1306 continue;
1307 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001308 // Check that VNI is live-out of all predecessors.
1309 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1310 PE = MFI->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001311 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1312 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001313
Jakob Stoklund Olesendf8412c2011-09-15 05:16:30 +00001314 if (VNI->isPHIDef() && VNI->def == LiveInts->getMBBStartIdx(MFI))
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001315 continue;
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001316
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001317 if (!PVNI) {
1318 report("Register not marked live out of predecessor", *PI);
1319 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001320 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001321 << PEnd << " in " << LI << '\n';
1322 continue;
1323 }
1324
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001325 if (PVNI != VNI) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001326 report("Different value live out of predecessor", *PI);
1327 *OS << "Valno #" << PVNI->id << " live out of BB#"
1328 << (*PI)->getNumber() << '@' << PEnd
1329 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1330 << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n';
1331 }
1332 }
1333 if (&*MFI == EndMBB)
1334 break;
1335 ++MFI;
1336 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001337 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001338
1339 // Check the LI only has one connected component.
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001340 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1341 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1342 unsigned NumComp = ConEQ.Classify(&LI);
1343 if (NumComp > 1) {
1344 report("Multiple connected components in live interval", MF);
1345 *OS << NumComp << " components in " << LI << '\n';
Jakob Stoklund Olesencb367772010-10-29 00:40:57 +00001346 for (unsigned comp = 0; comp != NumComp; ++comp) {
1347 *OS << comp << ": valnos";
1348 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1349 E = LI.vni_end(); I!=E; ++I)
1350 if (comp == ConEQ.getEqClass(*I))
1351 *OS << ' ' << (*I)->id;
1352 *OS << '\n';
1353 }
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001354 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001355 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001356 }
1357}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001358