blob: ace8252385ae89cc0ac2bcb104dda1530239f4a7 [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;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000072 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000073 typedef DenseSet<unsigned> RegSet;
74 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
75
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000076 const MachineInstr *FirstTerminator;
77
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000078 BitVector regsReserved;
Lang Hames03698de2012-02-14 19:17:48 +000079 BitVector regsAllocatable;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000080 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000081 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000082 RegMaskVector regMasks;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000083 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000084
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000085 SlotIndex lastIndex;
86
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000087 // Add Reg and any sub-registers to RV
88 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
89 RV.push_back(Reg);
90 if (TargetRegisterInfo::isPhysicalRegister(Reg))
91 for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
92 RV.push_back(*R);
93 }
94
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000095 struct BBInfo {
96 // Is this MBB reachable from the MF entry point?
97 bool reachable;
98
99 // Vregs that must be live in because they are used without being
100 // defined. Map value is the user.
101 RegMap vregsLiveIn;
102
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000103 // Regs killed in MBB. They may be defined again, and will then be in both
104 // regsKilled and regsLiveOut.
105 RegSet regsKilled;
106
107 // Regs defined in MBB and live out. Note that vregs passing through may
108 // be live out without being mentioned here.
109 RegSet regsLiveOut;
110
111 // Vregs that pass through MBB untouched. This set is disjoint from
112 // regsKilled and regsLiveOut.
113 RegSet vregsPassed;
114
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000115 // Vregs that must pass through MBB because they are needed by a successor
116 // block. This set is disjoint from regsLiveOut.
117 RegSet vregsRequired;
118
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000119 BBInfo() : reachable(false) {}
120
121 // Add register to vregsPassed if it belongs there. Return true if
122 // anything changed.
123 bool addPassed(unsigned Reg) {
124 if (!TargetRegisterInfo::isVirtualRegister(Reg))
125 return false;
126 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
127 return false;
128 return vregsPassed.insert(Reg).second;
129 }
130
131 // Same for a full set.
132 bool addPassed(const RegSet &RS) {
133 bool changed = false;
134 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
135 if (addPassed(*I))
136 changed = true;
137 return changed;
138 }
139
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000140 // Add register to vregsRequired if it belongs there. Return true if
141 // anything changed.
142 bool addRequired(unsigned Reg) {
143 if (!TargetRegisterInfo::isVirtualRegister(Reg))
144 return false;
145 if (regsLiveOut.count(Reg))
146 return false;
147 return vregsRequired.insert(Reg).second;
148 }
149
150 // Same for a full set.
151 bool addRequired(const RegSet &RS) {
152 bool changed = false;
153 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
154 if (addRequired(*I))
155 changed = true;
156 return changed;
157 }
158
159 // Same for a full map.
160 bool addRequired(const RegMap &RM) {
161 bool changed = false;
162 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
163 if (addRequired(I->first))
164 changed = true;
165 return changed;
166 }
167
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000168 // Live-out registers are either in regsLiveOut or vregsPassed.
169 bool isLiveOut(unsigned Reg) const {
170 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
171 }
172 };
173
174 // Extra register info per MBB.
175 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
176
177 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000178 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000179 }
180
Lang Hames03698de2012-02-14 19:17:48 +0000181 bool isAllocatable(unsigned Reg) {
182 return Reg < regsAllocatable.size() && regsAllocatable.test(Reg);
183 }
184
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000185 // Analysis information if available
186 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000187 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000188 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000189 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000190
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000191 void visitMachineFunctionBefore();
192 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
193 void visitMachineInstrBefore(const MachineInstr *MI);
194 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
195 void visitMachineInstrAfter(const MachineInstr *MI);
196 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
197 void visitMachineFunctionAfter();
198
199 void report(const char *msg, const MachineFunction *MF);
200 void report(const char *msg, const MachineBasicBlock *MBB);
201 void report(const char *msg, const MachineInstr *MI);
202 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
203
204 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000205 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000206 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000207
208 void calcRegsRequired();
209 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000210 void verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000211 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000212
213 struct MachineVerifierPass : public MachineFunctionPass {
214 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000215 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000216
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000217 MachineVerifierPass(const char *b = 0)
218 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000219 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
220 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000221
222 void getAnalysisUsage(AnalysisUsage &AU) const {
223 AU.setPreservesAll();
224 MachineFunctionPass::getAnalysisUsage(AU);
225 }
226
227 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000228 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000229 return false;
230 }
231 };
232
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000233}
234
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000235char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000236INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000237 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000238
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000239FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
240 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000241}
242
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000243void MachineFunction::verify(Pass *p, const char *Banner) const {
244 MachineVerifier(p, Banner)
245 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000246}
247
Chris Lattner17e9edc2009-08-23 02:51:22 +0000248bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
249 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000250 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000251 std::string ErrorInfo;
252 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
253 raw_fd_ostream::F_Append);
254 if (!ErrorInfo.empty()) {
255 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
256 exit(1);
257 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000258
Chris Lattner17e9edc2009-08-23 02:51:22 +0000259 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000260 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000261 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000262 }
263
264 foundErrors = 0;
265
266 this->MF = &MF;
267 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000268 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000269 TRI = TM->getRegisterInfo();
270 MRI = &MF.getRegInfo();
271
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000272 LiveVars = NULL;
273 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000274 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000275 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000276 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000277 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000278 // We don't want to verify LiveVariables if LiveIntervals is available.
279 if (!LiveInts)
280 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000281 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000282 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000283 }
284
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000285 visitMachineFunctionBefore();
286 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
287 MFI!=MFE; ++MFI) {
288 visitMachineBasicBlockBefore(MFI);
Evan Chengddfd1372011-12-14 02:11:42 +0000289 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
290 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000291 if (MBBI->getParent() != MFI) {
292 report("Bad instruction parent pointer", MFI);
293 *OS << "Instruction: " << *MBBI;
294 continue;
295 }
Evan Chengddfd1372011-12-14 02:11:42 +0000296 // Skip BUNDLE instruction for now. FIXME: We should add code to verify
297 // the BUNDLE's specifically.
298 if (MBBI->isBundle())
299 continue;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000300 visitMachineInstrBefore(MBBI);
301 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
302 visitMachineOperand(&MBBI->getOperand(I), I);
303 visitMachineInstrAfter(MBBI);
304 }
305 visitMachineBasicBlockAfter(MFI);
306 }
307 visitMachineFunctionAfter();
308
Chris Lattner17e9edc2009-08-23 02:51:22 +0000309 if (OutFile)
310 delete OutFile;
311 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000312 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000313
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000314 // Clean up.
315 regsLive.clear();
316 regsDefined.clear();
317 regsDead.clear();
318 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000319 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000320 regsLiveInButUnused.clear();
321 MBBInfoMap.clear();
322
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000323 return false; // no changes
324}
325
Chris Lattner372fefe2009-08-23 01:03:30 +0000326void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000327 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000328 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000329 if (!foundErrors++) {
330 if (Banner)
331 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000332 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000333 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000334 *OS << "*** Bad machine code: " << msg << " ***\n"
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000335 << "- function: " << MF->getFunction()->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000336}
337
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000338void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000339 assert(MBB);
340 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000341 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000342 << " " << (void*)MBB
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000343 << " (BB#" << MBB->getNumber() << ")";
344 if (Indexes)
345 *OS << " [" << Indexes->getMBBStartIdx(MBB)
346 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
347 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000348}
349
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000350void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000351 assert(MI);
352 report(msg, MI->getParent());
353 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000354 if (Indexes && Indexes->hasIndex(MI))
355 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000356 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000357}
358
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000359void MachineVerifier::report(const char *msg,
360 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000361 assert(MO);
362 report(msg, MO->getParent());
363 *OS << "- operand " << MONum << ": ";
364 MO->print(*OS, TM);
365 *OS << "\n";
366}
367
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000368void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000369 BBInfo &MInfo = MBBInfoMap[MBB];
370 if (!MInfo.reachable) {
371 MInfo.reachable = true;
372 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
373 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
374 markReachable(*SuI);
375 }
376}
377
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000378void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000379 lastIndex = SlotIndex();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000380 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000381
382 // A sub-register of a reserved register is also reserved
383 for (int Reg = regsReserved.find_first(); Reg>=0;
384 Reg = regsReserved.find_next(Reg)) {
385 for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
386 // FIXME: This should probably be:
387 // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
388 regsReserved.set(*Sub);
389 }
390 }
Lang Hames03698de2012-02-14 19:17:48 +0000391
392 regsAllocatable = TRI->getAllocatableSet(*MF);
393
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000394 markReachable(&MF->front());
395}
396
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000397// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000398static bool matchPair(MachineBasicBlock::const_succ_iterator i,
399 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000400 if (*i == a)
401 return *++i == b;
402 if (*i == b)
403 return *++i == a;
404 return false;
405}
406
407void
408MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000409 FirstTerminator = 0;
410
Lang Hames03698de2012-02-14 19:17:48 +0000411 if (MRI->isSSA()) {
412 // If this block has allocatable physical registers live-in, check that
413 // it is an entry block or landing pad.
414 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
415 LE = MBB->livein_end();
416 LI != LE; ++LI) {
417 unsigned reg = *LI;
418 if (isAllocatable(reg) && !MBB->isLandingPad() &&
419 MBB != MBB->getParent()->begin()) {
420 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
421 }
422 }
423 }
424
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000425 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000426 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000427 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000428 E = MBB->succ_end(); I != E; ++I) {
429 if ((*I)->isLandingPad())
430 LandingPadSuccs.insert(*I);
431 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000432
433 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
434 const BasicBlock *BB = MBB->getBasicBlock();
435 if (LandingPadSuccs.size() > 1 &&
436 !(AsmInfo &&
437 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
438 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000439 report("MBB has more than one landing pad successor", MBB);
440
Dan Gohman27920592009-08-27 02:43:49 +0000441 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
442 MachineBasicBlock *TBB = 0, *FBB = 0;
443 SmallVector<MachineOperand, 4> Cond;
444 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
445 TBB, FBB, Cond)) {
446 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
447 // check whether its answers match up with reality.
448 if (!TBB && !FBB) {
449 // Block falls through to its successor.
450 MachineFunction::const_iterator MBBI = MBB;
451 ++MBBI;
452 if (MBBI == MF->end()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000453 // It's possible that the block legitimately ends with a noreturn
454 // call or an unreachable, in which case it won't actually fall
455 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000456 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000457 // It's possible that the block legitimately ends with a noreturn
458 // call or an unreachable, in which case it won't actuall fall
459 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000460 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000461 report("MBB exits via unconditional fall-through but doesn't have "
462 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000463 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000464 report("MBB exits via unconditional fall-through but its successor "
465 "differs from its CFG successor!", MBB);
466 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000467 if (!MBB->empty() && MBB->back().isBarrier() &&
Evan Cheng86050dc2010-06-18 23:09:54 +0000468 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000469 report("MBB exits via unconditional fall-through but ends with a "
470 "barrier instruction!", MBB);
471 }
472 if (!Cond.empty()) {
473 report("MBB exits via unconditional fall-through but has a condition!",
474 MBB);
475 }
476 } else if (TBB && !FBB && Cond.empty()) {
477 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000478 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000479 report("MBB exits via unconditional branch but doesn't have "
480 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000481 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000482 report("MBB exits via unconditional branch but the CFG "
483 "successor doesn't match the actual successor!", MBB);
484 }
485 if (MBB->empty()) {
486 report("MBB exits via unconditional branch but doesn't contain "
487 "any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000488 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000489 report("MBB exits via unconditional branch but doesn't end with a "
490 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000491 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000492 report("MBB exits via unconditional branch but the branch isn't a "
493 "terminator instruction!", MBB);
494 }
495 } else if (TBB && !FBB && !Cond.empty()) {
496 // Block conditionally branches somewhere, otherwise falls through.
497 MachineFunction::const_iterator MBBI = MBB;
498 ++MBBI;
499 if (MBBI == MF->end()) {
500 report("MBB conditionally falls through out of function!", MBB);
501 } if (MBB->succ_size() != 2) {
502 report("MBB exits via conditional branch/fall-through but doesn't have "
503 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000504 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000505 report("MBB exits via conditional branch/fall-through but the CFG "
506 "successors don't match the actual successors!", MBB);
507 }
508 if (MBB->empty()) {
509 report("MBB exits via conditional branch/fall-through but doesn't "
510 "contain any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000511 } else if (MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000512 report("MBB exits via conditional branch/fall-through but ends with a "
513 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000514 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000515 report("MBB exits via conditional branch/fall-through but the branch "
516 "isn't a terminator instruction!", MBB);
517 }
518 } else if (TBB && FBB) {
519 // Block conditionally branches somewhere, otherwise branches
520 // somewhere else.
521 if (MBB->succ_size() != 2) {
522 report("MBB exits via conditional branch/branch but doesn't have "
523 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000524 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000525 report("MBB exits via conditional branch/branch but the CFG "
526 "successors don't match the actual successors!", MBB);
527 }
528 if (MBB->empty()) {
529 report("MBB exits via conditional branch/branch but doesn't "
530 "contain any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000531 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000532 report("MBB exits via conditional branch/branch but doesn't end with a "
533 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000534 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000535 report("MBB exits via conditional branch/branch but the branch "
536 "isn't a terminator instruction!", MBB);
537 }
538 if (Cond.empty()) {
539 report("MBB exits via conditinal branch/branch but there's no "
540 "condition!", MBB);
541 }
542 } else {
543 report("AnalyzeBranch returned invalid data!", MBB);
544 }
545 }
546
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000547 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000548 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000549 E = MBB->livein_end(); I != E; ++I) {
550 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
551 report("MBB live-in list contains non-physical register", MBB);
552 continue;
553 }
554 regsLive.insert(*I);
555 for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++)
556 regsLive.insert(*R);
557 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000558 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000559
560 const MachineFrameInfo *MFI = MF->getFrameInfo();
561 assert(MFI && "Function has no frame info");
562 BitVector PR = MFI->getPristineRegs(MBB);
563 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
564 regsLive.insert(I);
565 for (const unsigned *R = TRI->getSubRegisters(I); *R; R++)
566 regsLive.insert(*R);
567 }
568
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000569 regsKilled.clear();
570 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000571
572 if (Indexes)
573 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000574}
575
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000576void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000577 const MCInstrDesc &MCID = MI->getDesc();
578 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000579 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000580 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000581 << MI->getNumExplicitOperands() << " given.\n";
582 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000583
584 // Check the MachineMemOperands for basic consistency.
585 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
586 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000587 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000588 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000589 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000590 report("Missing mayStore flag", MI);
591 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000592
593 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000594 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000595 if (LiveInts) {
596 bool mapped = !LiveInts->isNotInMIMap(MI);
597 if (MI->isDebugValue()) {
598 if (mapped)
599 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000600 } else if (MI->isInsideBundle()) {
601 if (mapped)
602 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000603 } else {
604 if (!mapped)
605 report("Missing slot index", MI);
606 }
607 }
608
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000609 // Ensure non-terminators don't follow terminators.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000610 if (MI->isTerminator()) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000611 if (!FirstTerminator)
612 FirstTerminator = MI;
613 } else if (FirstTerminator) {
614 report("Non-terminator instruction after the first terminator", MI);
615 *OS << "First terminator was:\t" << *FirstTerminator;
616 }
617
Andrew Trick3be654f2011-09-21 02:20:46 +0000618 StringRef ErrorInfo;
619 if (!TII->verifyInstruction(MI, ErrorInfo))
620 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000621}
622
623void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000624MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000625 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000626 const MCInstrDesc &MCID = MI->getDesc();
627 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000628
Evan Chenge837dea2011-06-28 19:10:37 +0000629 // The first MCID.NumDefs operands must be explicit register defines
630 if (MONum < MCID.getNumDefs()) {
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000631 if (!MO->isReg())
632 report("Explicit definition must be a register", MO, MONum);
633 else if (!MO->isDef())
634 report("Explicit definition marked as use", MO, MONum);
635 else if (MO->isImplicit())
636 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000637 } else if (MONum < MCID.getNumOperands()) {
Eric Christopher113a06c2010-11-17 00:55:36 +0000638 // Don't check if it's the last operand in a variadic instruction. See,
639 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000640 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000641 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000642 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000643 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000644 if (MO->isImplicit())
645 report("Explicit operand marked as implicit", MO, MONum);
646 }
647 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000648 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000649 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000650 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000651 }
652
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000653 switch (MO->getType()) {
654 case MachineOperand::MO_Register: {
655 const unsigned Reg = MO->getReg();
656 if (!Reg)
657 return;
658
659 // Check Live Variables.
Cameron Zwarich8ec88ba2010-12-20 00:08:10 +0000660 if (MI->isDebugValue()) {
661 // Liveness checks are not valid for debug values.
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000662 } else if (MO->isUse() && !MO->isUndef()) {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000663 regsLiveInButUnused.erase(Reg);
664
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000665 bool isKill = false;
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000666 unsigned defIdx;
667 if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
668 // A two-addr use counts as a kill if use and def are the same.
669 unsigned DefReg = MI->getOperand(defIdx).getReg();
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000670 if (Reg == DefReg)
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000671 isKill = true;
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000672 else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000673 report("Two-address instruction operands must be identical",
674 MO, MONum);
675 }
676 } else
677 isKill = MO->isKill();
678
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000679 if (isKill)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000680 addRegWithSubRegs(regsKilled, Reg);
681
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000682 // Check that LiveVars knows this kill.
683 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
684 MO->isKill()) {
685 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
686 if (std::find(VI.Kills.begin(),
687 VI.Kills.end(), MI) == VI.Kills.end())
688 report("Kill missing from LiveVariables", MO, MONum);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000689 }
690
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000691 // Check LiveInts liveness and kill.
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000692 if (TargetRegisterInfo::isVirtualRegister(Reg) &&
693 LiveInts && !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000694 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getRegSlot(true);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000695 if (LiveInts->hasInterval(Reg)) {
696 const LiveInterval &LI = LiveInts->getInterval(Reg);
697 if (!LI.liveAt(UseIdx)) {
698 report("No live range at use", MO, MONum);
699 *OS << UseIdx << " is not live in " << LI << '\n';
700 }
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000701 // Check for extra kill flags.
702 // Note that we allow missing kill flags for now.
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000703 if (MO->isKill() && !LI.killedAt(UseIdx.getRegSlot())) {
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000704 report("Live range continues after kill flag", MO, MONum);
705 *OS << "Live range: " << LI << '\n';
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000706 }
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000707 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000708 report("Virtual register has no Live interval", MO, MONum);
709 }
710 }
711
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000712 // Use of a dead register.
713 if (!regsLive.count(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000714 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen4af0f5f2011-07-30 00:57:25 +0000715 // Reserved registers may be used even when 'dead'.
716 if (!isReserved(Reg))
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000717 report("Using an undefined physical register", MO, MONum);
718 } else {
719 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
720 // We don't know which virtual registers are live in, so only complain
721 // if vreg was killed in this MBB. Otherwise keep track of vregs that
722 // must be live in. PHI instructions are handled separately.
723 if (MInfo.regsKilled.count(Reg))
724 report("Using a killed virtual register", MO, MONum);
Chris Lattner518bb532010-02-09 19:54:29 +0000725 else if (!MI->isPHI())
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000726 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
727 }
Duncan Sandse5567202009-05-16 03:28:54 +0000728 }
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000729 } else if (MO->isDef()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000730 // Register defined.
731 // TODO: verify that earlyclobber ops are not used.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000732 if (MO->isDead())
733 addRegWithSubRegs(regsDead, Reg);
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000734 else
735 addRegWithSubRegs(regsDefined, Reg);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000736
Jakob Stoklund Olesen93e6f022011-07-29 23:02:48 +0000737 // Verify SSA form.
738 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
739 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
740 report("Multiple virtual register defs in SSA form", MO, MONum);
741
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000742 // Check LiveInts for a live range, but only for virtual registers.
743 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
744 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000745 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getRegSlot();
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000746 if (LiveInts->hasInterval(Reg)) {
747 const LiveInterval &LI = LiveInts->getInterval(Reg);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000748 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
749 assert(VNI && "NULL valno is not allowed");
Cameron Zwarich1b031dd2010-12-19 23:50:53 +0000750 if (VNI->def != DefIdx && !MO->isEarlyClobber()) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000751 report("Inconsistent valno->def", MO, MONum);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000752 *OS << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000753 << DefIdx << " in " << LI << '\n';
754 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000755 } else {
756 report("No live range at def", MO, MONum);
757 *OS << DefIdx << " is not live in " << LI << '\n';
758 }
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000759 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000760 report("Virtual register has no Live interval", MO, MONum);
761 }
762 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000763 }
764
765 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000766 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000767 unsigned SubIdx = MO->getSubReg();
768
769 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000770 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000771 report("Illegal subregister index for physical register", MO, MONum);
772 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000773 }
Evan Chenge837dea2011-06-28 19:10:37 +0000774 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000775 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000776 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000777 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000778 << DRC->getName() << " register.\n";
779 }
780 }
781 } else {
782 // Virtual register.
783 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
784 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000785 const TargetRegisterClass *SRC =
786 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000787 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000788 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000789 *OS << "Register class " << RC->getName()
790 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000791 return;
792 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000793 if (RC != SRC) {
794 report("Invalid register class for subregister index", MO, MONum);
795 *OS << "Register class " << RC->getName()
796 << " does not fully support subreg index " << SubIdx << "\n";
797 return;
798 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000799 }
Evan Chenge837dea2011-06-28 19:10:37 +0000800 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000801 if (SubIdx) {
802 const TargetRegisterClass *SuperRC =
803 TRI->getLargestLegalSuperClass(RC);
804 if (!SuperRC) {
805 report("No largest legal super class exists.", MO, MONum);
806 return;
807 }
808 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
809 if (!DRC) {
810 report("No matching super-reg register class.", MO, MONum);
811 return;
812 }
813 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000814 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000815 report("Illegal virtual register for instruction", MO, MONum);
816 *OS << "Expected a " << DRC->getName() << " register, but got a "
817 << RC->getName() << " register\n";
818 }
819 }
820 }
821 }
822 break;
823 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000824
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000825 case MachineOperand::MO_RegisterMask:
826 regMasks.push_back(MO->getRegMask());
827 break;
828
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000829 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000830 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
831 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000832 break;
833
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000834 case MachineOperand::MO_FrameIndex:
835 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
836 LiveInts && !LiveInts->isNotInMIMap(MI)) {
837 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
838 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000839 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000840 report("Instruction loads from dead spill slot", MO, MONum);
841 *OS << "Live stack: " << LI << '\n';
842 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000843 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000844 report("Instruction stores to dead spill slot", MO, MONum);
845 *OS << "Live stack: " << LI << '\n';
846 }
847 }
848 break;
849
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000850 default:
851 break;
852 }
853}
854
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000855void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000856 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
857 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000858 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000859 // Kill any masked registers.
860 while (!regMasks.empty()) {
861 const uint32_t *Mask = regMasks.pop_back_val();
862 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
863 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
864 MachineOperand::clobbersPhysReg(Mask, *I))
865 regsDead.push_back(*I);
866 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000867 set_subtract(regsLive, regsDead); regsDead.clear();
868 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000869
870 if (Indexes && Indexes->hasIndex(MI)) {
871 SlotIndex idx = Indexes->getInstructionIndex(MI);
872 if (!(idx > lastIndex)) {
873 report("Instruction index out of order", MI);
874 *OS << "Last instruction was at " << lastIndex << '\n';
875 }
876 lastIndex = idx;
877 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000878}
879
880void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000881MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000882 MBBInfoMap[MBB].regsLiveOut = regsLive;
883 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000884
885 if (Indexes) {
886 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
887 if (!(stop > lastIndex)) {
888 report("Block ends before last instruction index", MBB);
889 *OS << "Block ends at " << stop
890 << " last instruction was at " << lastIndex << '\n';
891 }
892 lastIndex = stop;
893 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000894}
895
896// Calculate the largest possible vregsPassed sets. These are the registers that
897// can pass through an MBB live, but may not be live every time. It is assumed
898// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000899void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000900 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
901 // have any vregsPassed.
902 DenseSet<const MachineBasicBlock*> todo;
903 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
904 MFI != MFE; ++MFI) {
905 const MachineBasicBlock &MBB(*MFI);
906 BBInfo &MInfo = MBBInfoMap[&MBB];
907 if (!MInfo.reachable)
908 continue;
909 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
910 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
911 BBInfo &SInfo = MBBInfoMap[*SuI];
912 if (SInfo.addPassed(MInfo.regsLiveOut))
913 todo.insert(*SuI);
914 }
915 }
916
917 // Iteratively push vregsPassed to successors. This will converge to the same
918 // final state regardless of DenseSet iteration order.
919 while (!todo.empty()) {
920 const MachineBasicBlock *MBB = *todo.begin();
921 todo.erase(MBB);
922 BBInfo &MInfo = MBBInfoMap[MBB];
923 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
924 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
925 if (*SuI == MBB)
926 continue;
927 BBInfo &SInfo = MBBInfoMap[*SuI];
928 if (SInfo.addPassed(MInfo.vregsPassed))
929 todo.insert(*SuI);
930 }
931 }
932}
933
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000934// Calculate the set of virtual registers that must be passed through each basic
935// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000936// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000937void MachineVerifier::calcRegsRequired() {
938 // First push live-in regs to predecessors' vregsRequired.
939 DenseSet<const MachineBasicBlock*> todo;
940 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
941 MFI != MFE; ++MFI) {
942 const MachineBasicBlock &MBB(*MFI);
943 BBInfo &MInfo = MBBInfoMap[&MBB];
944 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
945 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
946 BBInfo &PInfo = MBBInfoMap[*PrI];
947 if (PInfo.addRequired(MInfo.vregsLiveIn))
948 todo.insert(*PrI);
949 }
950 }
951
952 // Iteratively push vregsRequired to predecessors. This will converge to the
953 // same final state regardless of DenseSet iteration order.
954 while (!todo.empty()) {
955 const MachineBasicBlock *MBB = *todo.begin();
956 todo.erase(MBB);
957 BBInfo &MInfo = MBBInfoMap[MBB];
958 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
959 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
960 if (*PrI == MBB)
961 continue;
962 BBInfo &SInfo = MBBInfoMap[*PrI];
963 if (SInfo.addRequired(MInfo.vregsRequired))
964 todo.insert(*PrI);
965 }
966 }
967}
968
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000969// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000970// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000971void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000972 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +0000973 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000974 DenseSet<const MachineBasicBlock*> seen;
975
976 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
977 unsigned Reg = BBI->getOperand(i).getReg();
978 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
979 if (!Pre->isSuccessor(MBB))
980 continue;
981 seen.insert(Pre);
982 BBInfo &PrInfo = MBBInfoMap[Pre];
983 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
984 report("PHI operand is not live-out from predecessor",
985 &BBI->getOperand(i), i);
986 }
987
988 // Did we see all predecessors?
989 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
990 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
991 if (!seen.count(*PrI)) {
992 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000993 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000994 << " is a predecessor according to the CFG.\n";
995 }
996 }
997 }
998}
999
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001000void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001001 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001002
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001003 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1004 MFI != MFE; ++MFI) {
1005 BBInfo &MInfo = MBBInfoMap[MFI];
1006
1007 // Skip unreachable MBBs.
1008 if (!MInfo.reachable)
1009 continue;
1010
1011 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001012 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001013
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001014 // Now check liveness info if available
1015 if (LiveVars || LiveInts)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001016 calcRegsRequired();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001017 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001018 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001019 if (LiveInts)
1020 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001021}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001022
1023void MachineVerifier::verifyLiveVariables() {
1024 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001025 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1026 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001027 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1028 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1029 MFI != MFE; ++MFI) {
1030 BBInfo &MInfo = MBBInfoMap[MFI];
1031
1032 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1033 if (MInfo.vregsRequired.count(Reg)) {
1034 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1035 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001036 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001037 << " must be live through the block.\n";
1038 }
1039 } else {
1040 if (VI.AliveBlocks.test(MFI->getNumber())) {
1041 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001042 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001043 << " is not needed live through the block.\n";
1044 }
1045 }
1046 }
1047 }
1048}
1049
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001050void MachineVerifier::verifyLiveIntervals() {
1051 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
1052 for (LiveIntervals::const_iterator LVI = LiveInts->begin(),
1053 LVE = LiveInts->end(); LVI != LVE; ++LVI) {
1054 const LiveInterval &LI = *LVI->second;
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001055
1056 // Spilling and splitting may leave unused registers around. Skip them.
1057 if (MRI->use_empty(LI.reg))
1058 continue;
1059
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001060 // Physical registers have much weirdness going on, mostly from coalescing.
1061 // We should probably fix it, but for now just ignore them.
1062 if (TargetRegisterInfo::isPhysicalRegister(LI.reg))
1063 continue;
1064
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001065 assert(LVI->first == LI.reg && "Invalid reg to interval mapping");
1066
1067 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1068 I!=E; ++I) {
1069 VNInfo *VNI = *I;
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001070 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001071
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001072 if (!DefVNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001073 if (!VNI->isUnused()) {
1074 report("Valno not live at def and not marked unused", MF);
1075 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1076 }
1077 continue;
1078 }
1079
1080 if (VNI->isUnused())
1081 continue;
1082
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001083 if (DefVNI != VNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001084 report("Live range at def has different valno", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001085 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001086 << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001087 continue;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001088 }
1089
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001090 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1091 if (!MBB) {
1092 report("Invalid definition index", MF);
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001093 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1094 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001095 continue;
1096 }
1097
1098 if (VNI->isPHIDef()) {
1099 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1100 report("PHIDef value is not defined at MBB start", MF);
1101 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001102 << ", not at the beginning of BB#" << MBB->getNumber()
1103 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001104 }
1105 } else {
1106 // Non-PHI def.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001107 MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001108 if (!MI) {
1109 report("No instruction at def index", MF);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001110 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1111 << " in " << LI << '\n';
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001112 continue;
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001113 }
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001114
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001115 bool hasDef = false;
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001116 bool isEarlyClobber = false;
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001117 for (MIOperands MOI(MI, true); MOI.isValid(); ++MOI) {
1118 if (!MOI->isReg() || !MOI->isDef())
1119 continue;
1120 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1121 if (MOI->getReg() != LI.reg)
1122 continue;
1123 } else {
1124 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1125 !TRI->regsOverlap(LI.reg, MOI->getReg()))
1126 continue;
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001127 }
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001128 hasDef = true;
1129 if (MOI->isEarlyClobber())
1130 isEarlyClobber = true;
1131 }
1132
1133 if (!hasDef) {
1134 report("Defining instruction does not modify register", MI);
1135 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001136 }
1137
1138 // Early clobber defs begin at USE slots, but other defs must begin at
1139 // DEF slots.
1140 if (isEarlyClobber) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001141 if (!VNI->def.isEarlyClobber()) {
1142 report("Early clobber def must be at an early-clobber slot", MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001143 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1144 << " in " << LI << '\n';
1145 }
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001146 } else if (!VNI->def.isRegister()) {
1147 report("Non-PHI, non-early clobber def must be at a register slot",
1148 MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001149 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1150 << " in " << LI << '\n';
1151 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001152 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001153 }
1154
1155 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001156 const VNInfo *VNI = I->valno;
1157 assert(VNI && "Live range has no valno");
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001158
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001159 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001160 report("Foreign valno in live range", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001161 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001162 *OS << " has a valno not in " << LI << '\n';
1163 }
1164
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001165 if (VNI->isUnused()) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001166 report("Live range valno is marked unused", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001167 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001168 *OS << " in " << LI << '\n';
1169 }
1170
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001171 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1172 if (!MBB) {
1173 report("Bad start of live segment, no basic block", MF);
1174 I->print(*OS);
1175 *OS << " in " << LI << '\n';
1176 continue;
1177 }
1178 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1179 if (I->start != MBBStartIdx && I->start != VNI->def) {
1180 report("Live segment must begin at MBB entry or valno def", MBB);
1181 I->print(*OS);
1182 *OS << " in " << LI << '\n' << "Basic block starts at "
1183 << MBBStartIdx << '\n';
1184 }
1185
1186 const MachineBasicBlock *EndMBB =
1187 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1188 if (!EndMBB) {
1189 report("Bad end of live segment, no basic block", MF);
1190 I->print(*OS);
1191 *OS << " in " << LI << '\n';
1192 continue;
1193 }
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001194
1195 // No more checks for live-out segments.
1196 if (I->end == LiveInts->getMBBEndIdx(EndMBB))
1197 continue;
1198
1199 // The live segment is ending inside EndMBB
1200 MachineInstr *MI =
1201 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1202 if (!MI) {
1203 report("Live segment doesn't end at a valid instruction", EndMBB);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001204 I->print(*OS);
1205 *OS << " in " << LI << '\n' << "Basic block starts at "
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001206 << MBBStartIdx << '\n';
1207 continue;
1208 }
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001209
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001210 // The block slot must refer to a basic block boundary.
1211 if (I->end.isBlock()) {
1212 report("Live segment ends at B slot of an instruction", MI);
1213 I->print(*OS);
1214 *OS << " in " << LI << '\n';
1215 }
1216
1217 if (I->end.isDead()) {
1218 // Segment ends on the dead slot.
1219 // That means there must be a dead def.
1220 if (!SlotIndex::isSameInstr(I->start, I->end)) {
1221 report("Live segment ending at dead slot spans instructions", MI);
1222 I->print(*OS);
1223 *OS << " in " << LI << '\n';
1224 }
1225 }
1226
1227 // A live segment can only end at an early-clobber slot if it is being
1228 // redefined by an early-clobber def.
1229 if (I->end.isEarlyClobber()) {
1230 if (I+1 == E || (I+1)->start != I->end) {
1231 report("Live segment ending at early clobber slot must be "
1232 "redefined by an EC def in the same instruction", MI);
1233 I->print(*OS);
1234 *OS << " in " << LI << '\n';
1235 }
1236 }
1237
1238 // The following checks only apply to virtual registers. Physreg liveness
1239 // is too weird to check.
1240 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1241 // A live range can end with either a redefinition, a kill flag on a
1242 // use, or a dead flag on a def.
1243 bool hasRead = false;
1244 bool hasDeadDef = false;
1245 for (MIOperands MOI(MI, true); MOI.isValid(); ++MOI) {
1246 if (!MOI->isReg() || MOI->getReg() != LI.reg)
1247 continue;
1248 if (MOI->readsReg())
1249 hasRead = true;
1250 if (MOI->isDef() && MOI->isDead())
1251 hasDeadDef = true;
1252 }
1253
1254 if (I->end.isDead()) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001255 if (!hasDeadDef) {
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001256 report("Instruction doesn't have a dead def operand", MI);
1257 I->print(*OS);
1258 *OS << " in " << LI << '\n';
1259 }
1260 } else {
1261 if (!hasRead) {
1262 report("Instruction ending live range doesn't read the register",
1263 MI);
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001264 I->print(*OS);
1265 *OS << " in " << LI << '\n';
1266 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001267 }
1268 }
1269
1270 // Now check all the basic blocks in this live segment.
1271 MachineFunction::const_iterator MFI = MBB;
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001272 // Is this live range the beginning of a non-PHIDef VN?
1273 if (I->start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001274 // Not live-in to any blocks.
1275 if (MBB == EndMBB)
1276 continue;
1277 // Skip this block.
1278 ++MFI;
1279 }
1280 for (;;) {
1281 assert(LiveInts->isLiveInToMBB(LI, MFI));
Jakob Stoklund Olesene459d552010-10-26 16:49:23 +00001282 // We don't know how to track physregs into a landing pad.
1283 if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1284 MFI->isLandingPad()) {
1285 if (&*MFI == EndMBB)
1286 break;
1287 ++MFI;
1288 continue;
1289 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001290 // Check that VNI is live-out of all predecessors.
1291 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1292 PE = MFI->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001293 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1294 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001295
Jakob Stoklund Olesendf8412c2011-09-15 05:16:30 +00001296 if (VNI->isPHIDef() && VNI->def == LiveInts->getMBBStartIdx(MFI))
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001297 continue;
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001298
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001299 if (!PVNI) {
1300 report("Register not marked live out of predecessor", *PI);
1301 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001302 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001303 << PEnd << " in " << LI << '\n';
1304 continue;
1305 }
1306
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001307 if (PVNI != VNI) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001308 report("Different value live out of predecessor", *PI);
1309 *OS << "Valno #" << PVNI->id << " live out of BB#"
1310 << (*PI)->getNumber() << '@' << PEnd
1311 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1312 << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n';
1313 }
1314 }
1315 if (&*MFI == EndMBB)
1316 break;
1317 ++MFI;
1318 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001319 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001320
1321 // Check the LI only has one connected component.
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001322 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1323 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1324 unsigned NumComp = ConEQ.Classify(&LI);
1325 if (NumComp > 1) {
1326 report("Multiple connected components in live interval", MF);
1327 *OS << NumComp << " components in " << LI << '\n';
Jakob Stoklund Olesencb367772010-10-29 00:40:57 +00001328 for (unsigned comp = 0; comp != NumComp; ++comp) {
1329 *OS << comp << ": valnos";
1330 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1331 E = LI.vni_end(); I!=E; ++I)
1332 if (comp == ConEQ.getEqClass(*I))
1333 *OS << ' ' << (*I)->id;
1334 *OS << '\n';
1335 }
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001336 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001337 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001338 }
1339}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001340