blob: 9deafe30322d8d32a7463a3c61db1d07eb102508 [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))
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +000092 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
93 RV.push_back(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000094 }
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);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000194 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000195 void visitMachineInstrBefore(const MachineInstr *MI);
196 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
197 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000198 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000199 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
200 void visitMachineFunctionAfter();
201
202 void report(const char *msg, const MachineFunction *MF);
203 void report(const char *msg, const MachineBasicBlock *MBB);
204 void report(const char *msg, const MachineInstr *MI);
205 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
206
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000207 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000208 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000209 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000210 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000211
212 void calcRegsRequired();
213 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000214 void verifyLiveIntervals();
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +0000215 void verifyLiveInterval(const LiveInterval&);
216 void verifyLiveIntervalValue(const LiveInterval&, VNInfo*);
217 void verifyLiveIntervalSegment(const LiveInterval&,
218 LiveInterval::const_iterator);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000219 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000220
221 struct MachineVerifierPass : public MachineFunctionPass {
222 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000223 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000224
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000225 MachineVerifierPass(const char *b = 0)
226 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000227 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
228 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000229
230 void getAnalysisUsage(AnalysisUsage &AU) const {
231 AU.setPreservesAll();
232 MachineFunctionPass::getAnalysisUsage(AU);
233 }
234
235 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000236 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000237 return false;
238 }
239 };
240
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000241}
242
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000243char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000244INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000245 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000246
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000247FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
248 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000249}
250
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000251void MachineFunction::verify(Pass *p, const char *Banner) const {
252 MachineVerifier(p, Banner)
253 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000254}
255
Chris Lattner17e9edc2009-08-23 02:51:22 +0000256bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
257 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000258 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000259 std::string ErrorInfo;
260 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
261 raw_fd_ostream::F_Append);
262 if (!ErrorInfo.empty()) {
263 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
264 exit(1);
265 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000266
Chris Lattner17e9edc2009-08-23 02:51:22 +0000267 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000268 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000269 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000270 }
271
272 foundErrors = 0;
273
274 this->MF = &MF;
275 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000276 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000277 TRI = TM->getRegisterInfo();
278 MRI = &MF.getRegInfo();
279
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000280 LiveVars = NULL;
281 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000282 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000283 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000284 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000285 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000286 // We don't want to verify LiveVariables if LiveIntervals is available.
287 if (!LiveInts)
288 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000289 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000290 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000291 }
292
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000293 visitMachineFunctionBefore();
294 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
295 MFI!=MFE; ++MFI) {
296 visitMachineBasicBlockBefore(MFI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000297 // Keep track of the current bundle header.
298 const MachineInstr *CurBundle = 0;
Evan Chengddfd1372011-12-14 02:11:42 +0000299 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
300 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000301 if (MBBI->getParent() != MFI) {
302 report("Bad instruction parent pointer", MFI);
303 *OS << "Instruction: " << *MBBI;
304 continue;
305 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000306 // Is this a bundle header?
307 if (!MBBI->isInsideBundle()) {
308 if (CurBundle)
309 visitMachineBundleAfter(CurBundle);
310 CurBundle = MBBI;
311 visitMachineBundleBefore(CurBundle);
312 } else if (!CurBundle)
313 report("No bundle header", MBBI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000314 visitMachineInstrBefore(MBBI);
315 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
316 visitMachineOperand(&MBBI->getOperand(I), I);
317 visitMachineInstrAfter(MBBI);
318 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000319 if (CurBundle)
320 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000321 visitMachineBasicBlockAfter(MFI);
322 }
323 visitMachineFunctionAfter();
324
Chris Lattner17e9edc2009-08-23 02:51:22 +0000325 if (OutFile)
326 delete OutFile;
327 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000328 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000329
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000330 // Clean up.
331 regsLive.clear();
332 regsDefined.clear();
333 regsDead.clear();
334 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000335 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000336 regsLiveInButUnused.clear();
337 MBBInfoMap.clear();
338
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000339 return false; // no changes
340}
341
Chris Lattner372fefe2009-08-23 01:03:30 +0000342void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000343 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000344 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000345 if (!foundErrors++) {
346 if (Banner)
347 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000348 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000349 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000350 *OS << "*** Bad machine code: " << msg << " ***\n"
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000351 << "- function: " << MF->getFunction()->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000352}
353
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000354void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000355 assert(MBB);
356 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000357 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000358 << " " << (void*)MBB
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000359 << " (BB#" << MBB->getNumber() << ")";
360 if (Indexes)
361 *OS << " [" << Indexes->getMBBStartIdx(MBB)
362 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
363 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000364}
365
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000366void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000367 assert(MI);
368 report(msg, MI->getParent());
369 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000370 if (Indexes && Indexes->hasIndex(MI))
371 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000372 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000373}
374
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000375void MachineVerifier::report(const char *msg,
376 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000377 assert(MO);
378 report(msg, MO->getParent());
379 *OS << "- operand " << MONum << ": ";
380 MO->print(*OS, TM);
381 *OS << "\n";
382}
383
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000384void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000385 BBInfo &MInfo = MBBInfoMap[MBB];
386 if (!MInfo.reachable) {
387 MInfo.reachable = true;
388 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
389 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
390 markReachable(*SuI);
391 }
392}
393
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000394void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000395 lastIndex = SlotIndex();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000396 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000397
398 // A sub-register of a reserved register is also reserved
399 for (int Reg = regsReserved.find_first(); Reg>=0;
400 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000401 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000402 // FIXME: This should probably be:
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000403 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
404 regsReserved.set(*SubRegs);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000405 }
406 }
Lang Hames03698de2012-02-14 19:17:48 +0000407
408 regsAllocatable = TRI->getAllocatableSet(*MF);
409
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000410 markReachable(&MF->front());
411}
412
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000413// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000414static bool matchPair(MachineBasicBlock::const_succ_iterator i,
415 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000416 if (*i == a)
417 return *++i == b;
418 if (*i == b)
419 return *++i == a;
420 return false;
421}
422
423void
424MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000425 FirstTerminator = 0;
426
Lang Hames03698de2012-02-14 19:17:48 +0000427 if (MRI->isSSA()) {
428 // If this block has allocatable physical registers live-in, check that
429 // it is an entry block or landing pad.
430 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
431 LE = MBB->livein_end();
432 LI != LE; ++LI) {
433 unsigned reg = *LI;
434 if (isAllocatable(reg) && !MBB->isLandingPad() &&
435 MBB != MBB->getParent()->begin()) {
436 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
437 }
438 }
439 }
440
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000441 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000442 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000443 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000444 E = MBB->succ_end(); I != E; ++I) {
445 if ((*I)->isLandingPad())
446 LandingPadSuccs.insert(*I);
447 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000448
449 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
450 const BasicBlock *BB = MBB->getBasicBlock();
451 if (LandingPadSuccs.size() > 1 &&
452 !(AsmInfo &&
453 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
454 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000455 report("MBB has more than one landing pad successor", MBB);
456
Dan Gohman27920592009-08-27 02:43:49 +0000457 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
458 MachineBasicBlock *TBB = 0, *FBB = 0;
459 SmallVector<MachineOperand, 4> Cond;
460 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
461 TBB, FBB, Cond)) {
462 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
463 // check whether its answers match up with reality.
464 if (!TBB && !FBB) {
465 // Block falls through to its successor.
466 MachineFunction::const_iterator MBBI = MBB;
467 ++MBBI;
468 if (MBBI == MF->end()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000469 // It's possible that the block legitimately ends with a noreturn
470 // call or an unreachable, in which case it won't actually fall
471 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000472 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000473 // It's possible that the block legitimately ends with a noreturn
474 // call or an unreachable, in which case it won't actuall fall
475 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000476 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000477 report("MBB exits via unconditional fall-through but doesn't have "
478 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000479 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000480 report("MBB exits via unconditional fall-through but its successor "
481 "differs from its CFG successor!", MBB);
482 }
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000483 if (!MBB->empty() && getBundleStart(&MBB->back())->isBarrier() &&
484 !TII->isPredicated(getBundleStart(&MBB->back()))) {
Dan Gohman27920592009-08-27 02:43:49 +0000485 report("MBB exits via unconditional fall-through but ends with a "
486 "barrier instruction!", MBB);
487 }
488 if (!Cond.empty()) {
489 report("MBB exits via unconditional fall-through but has a condition!",
490 MBB);
491 }
492 } else if (TBB && !FBB && Cond.empty()) {
493 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000494 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000495 report("MBB exits via unconditional branch but doesn't have "
496 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000497 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000498 report("MBB exits via unconditional branch but the CFG "
499 "successor doesn't match the actual successor!", MBB);
500 }
501 if (MBB->empty()) {
502 report("MBB exits via unconditional branch but doesn't contain "
503 "any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000504 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000505 report("MBB exits via unconditional branch but doesn't end with a "
506 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000507 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000508 report("MBB exits via unconditional branch but the branch isn't a "
509 "terminator instruction!", MBB);
510 }
511 } else if (TBB && !FBB && !Cond.empty()) {
512 // Block conditionally branches somewhere, otherwise falls through.
513 MachineFunction::const_iterator MBBI = MBB;
514 ++MBBI;
515 if (MBBI == MF->end()) {
516 report("MBB conditionally falls through out of function!", MBB);
517 } if (MBB->succ_size() != 2) {
518 report("MBB exits via conditional branch/fall-through but doesn't have "
519 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000520 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000521 report("MBB exits via conditional branch/fall-through but the CFG "
522 "successors don't match the actual successors!", MBB);
523 }
524 if (MBB->empty()) {
525 report("MBB exits via conditional branch/fall-through but doesn't "
526 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000527 } else if (getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000528 report("MBB exits via conditional branch/fall-through but ends with a "
529 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000530 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000531 report("MBB exits via conditional branch/fall-through but the branch "
532 "isn't a terminator instruction!", MBB);
533 }
534 } else if (TBB && FBB) {
535 // Block conditionally branches somewhere, otherwise branches
536 // somewhere else.
537 if (MBB->succ_size() != 2) {
538 report("MBB exits via conditional branch/branch but doesn't have "
539 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000540 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000541 report("MBB exits via conditional branch/branch but the CFG "
542 "successors don't match the actual successors!", MBB);
543 }
544 if (MBB->empty()) {
545 report("MBB exits via conditional branch/branch but doesn't "
546 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000547 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000548 report("MBB exits via conditional branch/branch but doesn't end with a "
549 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000550 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000551 report("MBB exits via conditional branch/branch but the branch "
552 "isn't a terminator instruction!", MBB);
553 }
554 if (Cond.empty()) {
555 report("MBB exits via conditinal branch/branch but there's no "
556 "condition!", MBB);
557 }
558 } else {
559 report("AnalyzeBranch returned invalid data!", MBB);
560 }
561 }
562
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000563 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000564 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000565 E = MBB->livein_end(); I != E; ++I) {
566 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
567 report("MBB live-in list contains non-physical register", MBB);
568 continue;
569 }
570 regsLive.insert(*I);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000571 for (MCSubRegIterator SubRegs(*I, TRI); SubRegs.isValid(); ++SubRegs)
572 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000573 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000574 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000575
576 const MachineFrameInfo *MFI = MF->getFrameInfo();
577 assert(MFI && "Function has no frame info");
578 BitVector PR = MFI->getPristineRegs(MBB);
579 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
580 regsLive.insert(I);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000581 for (MCSubRegIterator SubRegs(I, TRI); SubRegs.isValid(); ++SubRegs)
582 regsLive.insert(*SubRegs);
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000583 }
584
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000585 regsKilled.clear();
586 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000587
588 if (Indexes)
589 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000590}
591
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000592// This function gets called for all bundle headers, including normal
593// stand-alone unbundled instructions.
594void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
595 if (Indexes && Indexes->hasIndex(MI)) {
596 SlotIndex idx = Indexes->getInstructionIndex(MI);
597 if (!(idx > lastIndex)) {
598 report("Instruction index out of order", MI);
599 *OS << "Last instruction was at " << lastIndex << '\n';
600 }
601 lastIndex = idx;
602 }
Pete Cooper83569cb2012-06-07 17:41:39 +0000603
604 // Ensure non-terminators don't follow terminators.
605 // Ignore predicated terminators formed by if conversion.
606 // FIXME: If conversion shouldn't need to violate this rule.
607 if (MI->isTerminator() && !TII->isPredicated(MI)) {
608 if (!FirstTerminator)
609 FirstTerminator = MI;
610 } else if (FirstTerminator) {
611 report("Non-terminator instruction after the first terminator", MI);
612 *OS << "First terminator was:\t" << *FirstTerminator;
613 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000614}
615
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000616void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000617 const MCInstrDesc &MCID = MI->getDesc();
618 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000619 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000620 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000621 << MI->getNumExplicitOperands() << " given.\n";
622 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000623
624 // Check the MachineMemOperands for basic consistency.
625 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
626 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000627 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000628 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000629 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000630 report("Missing mayStore flag", MI);
631 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000632
633 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000634 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000635 if (LiveInts) {
636 bool mapped = !LiveInts->isNotInMIMap(MI);
637 if (MI->isDebugValue()) {
638 if (mapped)
639 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000640 } else if (MI->isInsideBundle()) {
641 if (mapped)
642 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000643 } else {
644 if (!mapped)
645 report("Missing slot index", MI);
646 }
647 }
648
Andrew Trick3be654f2011-09-21 02:20:46 +0000649 StringRef ErrorInfo;
650 if (!TII->verifyInstruction(MI, ErrorInfo))
651 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000652}
653
654void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000655MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000656 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000657 const MCInstrDesc &MCID = MI->getDesc();
658 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000659
Evan Chenge837dea2011-06-28 19:10:37 +0000660 // The first MCID.NumDefs operands must be explicit register defines
661 if (MONum < MCID.getNumDefs()) {
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000662 if (!MO->isReg())
663 report("Explicit definition must be a register", MO, MONum);
Evan Chengcac58aa2012-05-29 19:40:44 +0000664 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000665 report("Explicit definition marked as use", MO, MONum);
666 else if (MO->isImplicit())
667 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000668 } else if (MONum < MCID.getNumOperands()) {
Eric Christopher113a06c2010-11-17 00:55:36 +0000669 // Don't check if it's the last operand in a variadic instruction. See,
670 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000671 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000672 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000673 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000674 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000675 if (MO->isImplicit())
676 report("Explicit operand marked as implicit", MO, MONum);
677 }
678 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000679 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000680 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000681 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000682 }
683
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000684 switch (MO->getType()) {
685 case MachineOperand::MO_Register: {
686 const unsigned Reg = MO->getReg();
687 if (!Reg)
688 return;
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000689 if (MRI->tracksLiveness() && !MI->isDebugValue())
690 checkLiveness(MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000691
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000692 // Verify two-address constraints after leaving SSA form.
693 unsigned DefIdx;
694 if (!MRI->isSSA() && MO->isUse() &&
695 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
696 Reg != MI->getOperand(DefIdx).getReg())
697 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000698
699 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000700 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000701 unsigned SubIdx = MO->getSubReg();
702
703 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000704 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000705 report("Illegal subregister index for physical register", MO, MONum);
706 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000707 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000708 if (const TargetRegisterClass *DRC =
709 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000710 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000711 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000712 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000713 << DRC->getName() << " register.\n";
714 }
715 }
716 } else {
717 // Virtual register.
718 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
719 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000720 const TargetRegisterClass *SRC =
721 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000722 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000723 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000724 *OS << "Register class " << RC->getName()
725 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000726 return;
727 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000728 if (RC != SRC) {
729 report("Invalid register class for subregister index", MO, MONum);
730 *OS << "Register class " << RC->getName()
731 << " does not fully support subreg index " << SubIdx << "\n";
732 return;
733 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000734 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000735 if (const TargetRegisterClass *DRC =
736 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000737 if (SubIdx) {
738 const TargetRegisterClass *SuperRC =
739 TRI->getLargestLegalSuperClass(RC);
740 if (!SuperRC) {
741 report("No largest legal super class exists.", MO, MONum);
742 return;
743 }
744 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
745 if (!DRC) {
746 report("No matching super-reg register class.", MO, MONum);
747 return;
748 }
749 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000750 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000751 report("Illegal virtual register for instruction", MO, MONum);
752 *OS << "Expected a " << DRC->getName() << " register, but got a "
753 << RC->getName() << " register\n";
754 }
755 }
756 }
757 }
758 break;
759 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000760
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000761 case MachineOperand::MO_RegisterMask:
762 regMasks.push_back(MO->getRegMask());
763 break;
764
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000765 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000766 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
767 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000768 break;
769
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000770 case MachineOperand::MO_FrameIndex:
771 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
772 LiveInts && !LiveInts->isNotInMIMap(MI)) {
773 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
774 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000775 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000776 report("Instruction loads from dead spill slot", MO, MONum);
777 *OS << "Live stack: " << LI << '\n';
778 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000779 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000780 report("Instruction stores to dead spill slot", MO, MONum);
781 *OS << "Live stack: " << LI << '\n';
782 }
783 }
784 break;
785
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000786 default:
787 break;
788 }
789}
790
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000791void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
792 const MachineInstr *MI = MO->getParent();
793 const unsigned Reg = MO->getReg();
794
795 // Both use and def operands can read a register.
796 if (MO->readsReg()) {
797 regsLiveInButUnused.erase(Reg);
798
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000799 if (MO->isKill())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000800 addRegWithSubRegs(regsKilled, Reg);
801
802 // Check that LiveVars knows this kill.
803 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
804 MO->isKill()) {
805 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
806 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
807 report("Kill missing from LiveVariables", MO, MONum);
808 }
809
810 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +0000811 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
812 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
813 // Check the cached regunit intervals.
814 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
815 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
816 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(*Units)) {
817 LiveRangeQuery LRQ(*LI, UseIdx);
818 if (!LRQ.valueIn()) {
819 report("No live range at use", MO, MONum);
820 *OS << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
821 << ' ' << *LI << '\n';
822 }
823 if (MO->isKill() && !LRQ.isKill()) {
824 report("Live range continues after kill flag", MO, MONum);
825 *OS << PrintRegUnit(*Units, TRI) << ' ' << *LI << '\n';
826 }
827 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000828 }
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +0000829 }
830
831 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
832 if (LiveInts->hasInterval(Reg)) {
833 // This is a virtual register interval.
834 const LiveInterval &LI = LiveInts->getInterval(Reg);
835 LiveRangeQuery LRQ(LI, UseIdx);
836 if (!LRQ.valueIn()) {
837 report("No live range at use", MO, MONum);
838 *OS << UseIdx << " is not live in " << LI << '\n';
839 }
840 // Check for extra kill flags.
841 // Note that we allow missing kill flags for now.
842 if (MO->isKill() && !LRQ.isKill()) {
843 report("Live range continues after kill flag", MO, MONum);
844 *OS << "Live range: " << LI << '\n';
845 }
846 } else {
847 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000848 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000849 }
850 }
851
852 // Use of a dead register.
853 if (!regsLive.count(Reg)) {
854 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
855 // Reserved registers may be used even when 'dead'.
856 if (!isReserved(Reg))
857 report("Using an undefined physical register", MO, MONum);
Pete Cooperb97c57a2012-07-19 23:40:38 +0000858 } else if (MRI->def_empty(Reg)) {
859 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000860 } else {
861 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
862 // We don't know which virtual registers are live in, so only complain
863 // if vreg was killed in this MBB. Otherwise keep track of vregs that
864 // must be live in. PHI instructions are handled separately.
865 if (MInfo.regsKilled.count(Reg))
866 report("Using a killed virtual register", MO, MONum);
867 else if (!MI->isPHI())
868 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
869 }
870 }
871 }
872
873 if (MO->isDef()) {
874 // Register defined.
875 // TODO: verify that earlyclobber ops are not used.
876 if (MO->isDead())
877 addRegWithSubRegs(regsDead, Reg);
878 else
879 addRegWithSubRegs(regsDefined, Reg);
880
881 // Verify SSA form.
882 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
883 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
884 report("Multiple virtual register defs in SSA form", MO, MONum);
885
886 // Check LiveInts for a live range, but only for virtual registers.
887 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
888 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +0000889 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
890 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000891 if (LiveInts->hasInterval(Reg)) {
892 const LiveInterval &LI = LiveInts->getInterval(Reg);
893 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
894 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +0000895 if (VNI->def != DefIdx) {
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000896 report("Inconsistent valno->def", MO, MONum);
897 *OS << "Valno " << VNI->id << " is not defined at "
898 << DefIdx << " in " << LI << '\n';
899 }
900 } else {
901 report("No live range at def", MO, MONum);
902 *OS << DefIdx << " is not live in " << LI << '\n';
903 }
904 } else {
905 report("Virtual register has no Live interval", MO, MONum);
906 }
907 }
908 }
909}
910
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000911void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000912}
913
914// This function gets called after visiting all instructions in a bundle. The
915// argument points to the bundle header.
916// Normal stand-alone instructions are also considered 'bundles', and this
917// function is called for all of them.
918void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000919 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
920 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000921 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000922 // Kill any masked registers.
923 while (!regMasks.empty()) {
924 const uint32_t *Mask = regMasks.pop_back_val();
925 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
926 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
927 MachineOperand::clobbersPhysReg(Mask, *I))
928 regsDead.push_back(*I);
929 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000930 set_subtract(regsLive, regsDead); regsDead.clear();
931 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000932}
933
934void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000935MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000936 MBBInfoMap[MBB].regsLiveOut = regsLive;
937 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000938
939 if (Indexes) {
940 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
941 if (!(stop > lastIndex)) {
942 report("Block ends before last instruction index", MBB);
943 *OS << "Block ends at " << stop
944 << " last instruction was at " << lastIndex << '\n';
945 }
946 lastIndex = stop;
947 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000948}
949
950// Calculate the largest possible vregsPassed sets. These are the registers that
951// can pass through an MBB live, but may not be live every time. It is assumed
952// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000953void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000954 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
955 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +0000956 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000957 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
958 MFI != MFE; ++MFI) {
959 const MachineBasicBlock &MBB(*MFI);
960 BBInfo &MInfo = MBBInfoMap[&MBB];
961 if (!MInfo.reachable)
962 continue;
963 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
964 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
965 BBInfo &SInfo = MBBInfoMap[*SuI];
966 if (SInfo.addPassed(MInfo.regsLiveOut))
967 todo.insert(*SuI);
968 }
969 }
970
971 // Iteratively push vregsPassed to successors. This will converge to the same
972 // final state regardless of DenseSet iteration order.
973 while (!todo.empty()) {
974 const MachineBasicBlock *MBB = *todo.begin();
975 todo.erase(MBB);
976 BBInfo &MInfo = MBBInfoMap[MBB];
977 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
978 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
979 if (*SuI == MBB)
980 continue;
981 BBInfo &SInfo = MBBInfoMap[*SuI];
982 if (SInfo.addPassed(MInfo.vregsPassed))
983 todo.insert(*SuI);
984 }
985 }
986}
987
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000988// Calculate the set of virtual registers that must be passed through each basic
989// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000990// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000991void MachineVerifier::calcRegsRequired() {
992 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +0000993 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000994 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
995 MFI != MFE; ++MFI) {
996 const MachineBasicBlock &MBB(*MFI);
997 BBInfo &MInfo = MBBInfoMap[&MBB];
998 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
999 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1000 BBInfo &PInfo = MBBInfoMap[*PrI];
1001 if (PInfo.addRequired(MInfo.vregsLiveIn))
1002 todo.insert(*PrI);
1003 }
1004 }
1005
1006 // Iteratively push vregsRequired to predecessors. This will converge to the
1007 // same final state regardless of DenseSet iteration order.
1008 while (!todo.empty()) {
1009 const MachineBasicBlock *MBB = *todo.begin();
1010 todo.erase(MBB);
1011 BBInfo &MInfo = MBBInfoMap[MBB];
1012 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1013 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1014 if (*PrI == MBB)
1015 continue;
1016 BBInfo &SInfo = MBBInfoMap[*PrI];
1017 if (SInfo.addRequired(MInfo.vregsRequired))
1018 todo.insert(*PrI);
1019 }
1020 }
1021}
1022
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001023// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001024// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001025void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001026 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001027 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +00001028 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001029 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001030
1031 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
1032 unsigned Reg = BBI->getOperand(i).getReg();
1033 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
1034 if (!Pre->isSuccessor(MBB))
1035 continue;
1036 seen.insert(Pre);
1037 BBInfo &PrInfo = MBBInfoMap[Pre];
1038 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1039 report("PHI operand is not live-out from predecessor",
1040 &BBI->getOperand(i), i);
1041 }
1042
1043 // Did we see all predecessors?
1044 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1045 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1046 if (!seen.count(*PrI)) {
1047 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +00001048 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001049 << " is a predecessor according to the CFG.\n";
1050 }
1051 }
1052 }
1053}
1054
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001055void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001056 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001057
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001058 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1059 MFI != MFE; ++MFI) {
1060 BBInfo &MInfo = MBBInfoMap[MFI];
1061
1062 // Skip unreachable MBBs.
1063 if (!MInfo.reachable)
1064 continue;
1065
1066 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001067 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001068
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001069 // Now check liveness info if available
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001070 calcRegsRequired();
1071
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001072 // Check for killed virtual registers that should be live out.
1073 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1074 MFI != MFE; ++MFI) {
1075 BBInfo &MInfo = MBBInfoMap[MFI];
1076 for (RegSet::iterator
1077 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1078 ++I)
1079 if (MInfo.regsKilled.count(*I)) {
Bill Wendling96cb1122012-07-19 00:04:14 +00001080 report("Virtual register killed in block, but needed live out.", MFI);
1081 *OS << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001082 << " is used after the block.\n";
1083 }
1084 }
1085
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001086 if (!MF->empty()) {
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001087 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1088 for (RegSet::iterator
1089 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesenff0275e2012-03-10 00:44:11 +00001090 ++I)
1091 report("Virtual register def doesn't dominate all uses.",
1092 MRI->getVRegDef(*I));
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001093 }
1094
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001095 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001096 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001097 if (LiveInts)
1098 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001099}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001100
1101void MachineVerifier::verifyLiveVariables() {
1102 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001103 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1104 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001105 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1106 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1107 MFI != MFE; ++MFI) {
1108 BBInfo &MInfo = MBBInfoMap[MFI];
1109
1110 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1111 if (MInfo.vregsRequired.count(Reg)) {
1112 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1113 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001114 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001115 << " must be live through the block.\n";
1116 }
1117 } else {
1118 if (VI.AliveBlocks.test(MFI->getNumber())) {
1119 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001120 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001121 << " is not needed live through the block.\n";
1122 }
1123 }
1124 }
1125 }
1126}
1127
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001128void MachineVerifier::verifyLiveIntervals() {
1129 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001130 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1131 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001132
1133 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001134 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001135 continue;
1136
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001137 if (!LiveInts->hasInterval(Reg)) {
1138 report("Missing live interval for virtual register", MF);
1139 *OS << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001140 continue;
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001141 }
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001142
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001143 const LiveInterval &LI = LiveInts->getInterval(Reg);
1144 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001145 verifyLiveInterval(LI);
1146 }
1147}
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001148
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001149void MachineVerifier::verifyLiveIntervalValue(const LiveInterval &LI,
1150 VNInfo *VNI) {
1151 if (VNI->isUnused())
1152 return;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001153
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001154 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001155
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001156 if (!DefVNI) {
1157 report("Valno not live at def and not marked unused", MF);
1158 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1159 return;
1160 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001161
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001162 if (DefVNI != VNI) {
1163 report("Live range at def has different valno", MF);
1164 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1165 << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
1166 return;
1167 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001168
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001169 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1170 if (!MBB) {
1171 report("Invalid definition index", MF);
1172 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1173 << " in " << LI << '\n';
1174 return;
1175 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001176
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001177 if (VNI->isPHIDef()) {
1178 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1179 report("PHIDef value is not defined at MBB start", MF);
1180 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1181 << ", not at the beginning of BB#" << MBB->getNumber()
1182 << " in " << LI << '\n';
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001183 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001184 return;
1185 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001186
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001187 // Non-PHI def.
1188 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1189 if (!MI) {
1190 report("No instruction at def index", MF);
1191 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1192 << " in " << LI << '\n';
1193 return;
1194 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001195
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001196 bool hasDef = false;
1197 bool isEarlyClobber = false;
1198 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1199 if (!MOI->isReg() || !MOI->isDef())
1200 continue;
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001201 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001202 if (MOI->getReg() != LI.reg)
1203 continue;
1204 } else {
1205 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1206 !TRI->regsOverlap(LI.reg, MOI->getReg()))
1207 continue;
1208 }
1209 hasDef = true;
1210 if (MOI->isEarlyClobber())
1211 isEarlyClobber = true;
1212 }
1213
1214 if (!hasDef) {
1215 report("Defining instruction does not modify register", MI);
1216 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1217 }
1218
1219 // Early clobber defs begin at USE slots, but other defs must begin at
1220 // DEF slots.
1221 if (isEarlyClobber) {
1222 if (!VNI->def.isEarlyClobber()) {
1223 report("Early clobber def must be at an early-clobber slot", MF);
1224 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1225 << " in " << LI << '\n';
1226 }
1227 } else if (!VNI->def.isRegister()) {
1228 report("Non-PHI, non-early clobber def must be at a register slot",
1229 MF);
1230 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1231 << " in " << LI << '\n';
1232 }
1233}
1234
1235void
1236MachineVerifier::verifyLiveIntervalSegment(const LiveInterval &LI,
1237 LiveInterval::const_iterator I) {
1238 const VNInfo *VNI = I->valno;
1239 assert(VNI && "Live range has no valno");
1240
1241 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
1242 report("Foreign valno in live range", MF);
1243 I->print(*OS);
1244 *OS << " has a valno not in " << LI << '\n';
1245 }
1246
1247 if (VNI->isUnused()) {
1248 report("Live range valno is marked unused", MF);
1249 I->print(*OS);
1250 *OS << " in " << LI << '\n';
1251 }
1252
1253 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1254 if (!MBB) {
1255 report("Bad start of live segment, no basic block", MF);
1256 I->print(*OS);
1257 *OS << " in " << LI << '\n';
1258 return;
1259 }
1260 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1261 if (I->start != MBBStartIdx && I->start != VNI->def) {
1262 report("Live segment must begin at MBB entry or valno def", MBB);
1263 I->print(*OS);
1264 *OS << " in " << LI << '\n'
1265 << "Basic block starts at " << MBBStartIdx << '\n';
1266 }
1267
1268 const MachineBasicBlock *EndMBB =
1269 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1270 if (!EndMBB) {
1271 report("Bad end of live segment, no basic block", MF);
1272 I->print(*OS);
1273 *OS << " in " << LI << '\n';
1274 return;
1275 }
1276
1277 // No more checks for live-out segments.
1278 if (I->end == LiveInts->getMBBEndIdx(EndMBB))
1279 return;
1280
1281 // The live segment is ending inside EndMBB
1282 const MachineInstr *MI =
1283 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1284 if (!MI) {
1285 report("Live segment doesn't end at a valid instruction", EndMBB);
1286 I->print(*OS);
1287 *OS << " in " << LI << '\n'
1288 << "Basic block starts at " << MBBStartIdx << '\n';
1289 return;
1290 }
1291
1292 // The block slot must refer to a basic block boundary.
1293 if (I->end.isBlock()) {
1294 report("Live segment ends at B slot of an instruction", MI);
1295 I->print(*OS);
1296 *OS << " in " << LI << '\n';
1297 }
1298
1299 if (I->end.isDead()) {
1300 // Segment ends on the dead slot.
1301 // That means there must be a dead def.
1302 if (!SlotIndex::isSameInstr(I->start, I->end)) {
1303 report("Live segment ending at dead slot spans instructions", MI);
1304 I->print(*OS);
1305 *OS << " in " << LI << '\n';
1306 }
1307 }
1308
1309 // A live segment can only end at an early-clobber slot if it is being
1310 // redefined by an early-clobber def.
1311 if (I->end.isEarlyClobber()) {
1312 if (I+1 == LI.end() || (I+1)->start != I->end) {
1313 report("Live segment ending at early clobber slot must be "
1314 "redefined by an EC def in the same instruction", MI);
1315 I->print(*OS);
1316 *OS << " in " << LI << '\n';
1317 }
1318 }
1319
1320 // The following checks only apply to virtual registers. Physreg liveness
1321 // is too weird to check.
1322 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1323 // A live range can end with either a redefinition, a kill flag on a
1324 // use, or a dead flag on a def.
1325 bool hasRead = false;
1326 bool hasDeadDef = false;
1327 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1328 if (!MOI->isReg() || MOI->getReg() != LI.reg)
1329 continue;
1330 if (MOI->readsReg())
1331 hasRead = true;
1332 if (MOI->isDef() && MOI->isDead())
1333 hasDeadDef = true;
1334 }
1335
1336 if (I->end.isDead()) {
1337 if (!hasDeadDef) {
1338 report("Instruction doesn't have a dead def operand", MI);
1339 I->print(*OS);
1340 *OS << " in " << LI << '\n';
1341 }
1342 } else {
1343 if (!hasRead) {
1344 report("Instruction ending live range doesn't read the register",
1345 MI);
1346 I->print(*OS);
1347 *OS << " in " << LI << '\n';
1348 }
1349 }
1350 }
1351
1352 // Now check all the basic blocks in this live segment.
1353 MachineFunction::const_iterator MFI = MBB;
1354 // Is this live range the beginning of a non-PHIDef VN?
1355 if (I->start == VNI->def && !VNI->isPHIDef()) {
1356 // Not live-in to any blocks.
1357 if (MBB == EndMBB)
1358 return;
1359 // Skip this block.
1360 ++MFI;
1361 }
1362 for (;;) {
1363 assert(LiveInts->isLiveInToMBB(LI, MFI));
1364 // We don't know how to track physregs into a landing pad.
1365 if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1366 MFI->isLandingPad()) {
1367 if (&*MFI == EndMBB)
1368 break;
1369 ++MFI;
1370 continue;
1371 }
1372
1373 // Is VNI a PHI-def in the current block?
1374 bool IsPHI = VNI->isPHIDef() &&
1375 VNI->def == LiveInts->getMBBStartIdx(MFI);
1376
1377 // Check that VNI is live-out of all predecessors.
1378 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1379 PE = MFI->pred_end(); PI != PE; ++PI) {
1380 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1381 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
1382
1383 // All predecessors must have a live-out value.
1384 if (!PVNI) {
1385 report("Register not marked live out of predecessor", *PI);
1386 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1387 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
1388 << PEnd << " in " << LI << '\n';
1389 continue;
1390 }
1391
1392 // Only PHI-defs can take different predecessor values.
1393 if (!IsPHI && PVNI != VNI) {
1394 report("Different value live out of predecessor", *PI);
1395 *OS << "Valno #" << PVNI->id << " live out of BB#"
1396 << (*PI)->getNumber() << '@' << PEnd
1397 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1398 << '@' << LiveInts->getMBBStartIdx(MFI) << " in "
1399 << PrintReg(LI.reg) << ": " << LI << '\n';
1400 }
1401 }
1402 if (&*MFI == EndMBB)
1403 break;
1404 ++MFI;
1405 }
1406}
1407
1408void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1409 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1410 I!=E; ++I)
1411 verifyLiveIntervalValue(LI, *I);
1412
1413 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I)
1414 verifyLiveIntervalSegment(LI, I);
1415
1416 // Check the LI only has one connected component.
1417 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1418 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1419 unsigned NumComp = ConEQ.Classify(&LI);
1420 if (NumComp > 1) {
1421 report("Multiple connected components in live interval", MF);
1422 *OS << NumComp << " components in " << LI << '\n';
1423 for (unsigned comp = 0; comp != NumComp; ++comp) {
1424 *OS << comp << ": valnos";
1425 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1426 E = LI.vni_end(); I!=E; ++I)
1427 if (comp == ConEQ.getEqClass(*I))
1428 *OS << ' ' << (*I)->id;
1429 *OS << '\n';
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001430 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001431 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001432 }
1433}