blob: ee04bdb90192d7b5d31ce6907ebc02218d96fb51 [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 Olesen48872e02009-05-16 00:33:53 +0000215 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000216
217 struct MachineVerifierPass : public MachineFunctionPass {
218 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000219 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000220
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000221 MachineVerifierPass(const char *b = 0)
222 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000223 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
224 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000225
226 void getAnalysisUsage(AnalysisUsage &AU) const {
227 AU.setPreservesAll();
228 MachineFunctionPass::getAnalysisUsage(AU);
229 }
230
231 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000232 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000233 return false;
234 }
235 };
236
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000237}
238
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000239char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000240INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000241 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000242
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000243FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
244 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000245}
246
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000247void MachineFunction::verify(Pass *p, const char *Banner) const {
248 MachineVerifier(p, Banner)
249 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000250}
251
Chris Lattner17e9edc2009-08-23 02:51:22 +0000252bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
253 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000254 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000255 std::string ErrorInfo;
256 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
257 raw_fd_ostream::F_Append);
258 if (!ErrorInfo.empty()) {
259 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
260 exit(1);
261 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000262
Chris Lattner17e9edc2009-08-23 02:51:22 +0000263 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000264 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000265 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000266 }
267
268 foundErrors = 0;
269
270 this->MF = &MF;
271 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000272 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000273 TRI = TM->getRegisterInfo();
274 MRI = &MF.getRegInfo();
275
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000276 LiveVars = NULL;
277 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000278 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000279 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000280 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000281 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000282 // We don't want to verify LiveVariables if LiveIntervals is available.
283 if (!LiveInts)
284 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000285 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000286 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000287 }
288
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000289 visitMachineFunctionBefore();
290 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
291 MFI!=MFE; ++MFI) {
292 visitMachineBasicBlockBefore(MFI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000293 // Keep track of the current bundle header.
294 const MachineInstr *CurBundle = 0;
Evan Chengddfd1372011-12-14 02:11:42 +0000295 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
296 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000297 if (MBBI->getParent() != MFI) {
298 report("Bad instruction parent pointer", MFI);
299 *OS << "Instruction: " << *MBBI;
300 continue;
301 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000302 // Is this a bundle header?
303 if (!MBBI->isInsideBundle()) {
304 if (CurBundle)
305 visitMachineBundleAfter(CurBundle);
306 CurBundle = MBBI;
307 visitMachineBundleBefore(CurBundle);
308 } else if (!CurBundle)
309 report("No bundle header", MBBI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000310 visitMachineInstrBefore(MBBI);
311 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
312 visitMachineOperand(&MBBI->getOperand(I), I);
313 visitMachineInstrAfter(MBBI);
314 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000315 if (CurBundle)
316 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000317 visitMachineBasicBlockAfter(MFI);
318 }
319 visitMachineFunctionAfter();
320
Chris Lattner17e9edc2009-08-23 02:51:22 +0000321 if (OutFile)
322 delete OutFile;
323 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000324 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000325
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000326 // Clean up.
327 regsLive.clear();
328 regsDefined.clear();
329 regsDead.clear();
330 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000331 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000332 regsLiveInButUnused.clear();
333 MBBInfoMap.clear();
334
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000335 return false; // no changes
336}
337
Chris Lattner372fefe2009-08-23 01:03:30 +0000338void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000339 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000340 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000341 if (!foundErrors++) {
342 if (Banner)
343 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000344 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000345 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000346 *OS << "*** Bad machine code: " << msg << " ***\n"
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000347 << "- function: " << MF->getFunction()->getName() << "\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 MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000351 assert(MBB);
352 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000353 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000354 << " " << (void*)MBB
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000355 << " (BB#" << MBB->getNumber() << ")";
356 if (Indexes)
357 *OS << " [" << Indexes->getMBBStartIdx(MBB)
358 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
359 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000360}
361
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000362void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000363 assert(MI);
364 report(msg, MI->getParent());
365 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000366 if (Indexes && Indexes->hasIndex(MI))
367 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000368 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000369}
370
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000371void MachineVerifier::report(const char *msg,
372 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000373 assert(MO);
374 report(msg, MO->getParent());
375 *OS << "- operand " << MONum << ": ";
376 MO->print(*OS, TM);
377 *OS << "\n";
378}
379
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000380void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000381 BBInfo &MInfo = MBBInfoMap[MBB];
382 if (!MInfo.reachable) {
383 MInfo.reachable = true;
384 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
385 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
386 markReachable(*SuI);
387 }
388}
389
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000390void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000391 lastIndex = SlotIndex();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000392 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000393
394 // A sub-register of a reserved register is also reserved
395 for (int Reg = regsReserved.find_first(); Reg>=0;
396 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000397 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000398 // FIXME: This should probably be:
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000399 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
400 regsReserved.set(*SubRegs);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000401 }
402 }
Lang Hames03698de2012-02-14 19:17:48 +0000403
404 regsAllocatable = TRI->getAllocatableSet(*MF);
405
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000406 markReachable(&MF->front());
407}
408
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000409// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000410static bool matchPair(MachineBasicBlock::const_succ_iterator i,
411 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000412 if (*i == a)
413 return *++i == b;
414 if (*i == b)
415 return *++i == a;
416 return false;
417}
418
419void
420MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000421 FirstTerminator = 0;
422
Lang Hames03698de2012-02-14 19:17:48 +0000423 if (MRI->isSSA()) {
424 // If this block has allocatable physical registers live-in, check that
425 // it is an entry block or landing pad.
426 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
427 LE = MBB->livein_end();
428 LI != LE; ++LI) {
429 unsigned reg = *LI;
430 if (isAllocatable(reg) && !MBB->isLandingPad() &&
431 MBB != MBB->getParent()->begin()) {
432 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
433 }
434 }
435 }
436
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000437 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000438 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000439 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000440 E = MBB->succ_end(); I != E; ++I) {
441 if ((*I)->isLandingPad())
442 LandingPadSuccs.insert(*I);
443 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000444
445 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
446 const BasicBlock *BB = MBB->getBasicBlock();
447 if (LandingPadSuccs.size() > 1 &&
448 !(AsmInfo &&
449 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
450 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000451 report("MBB has more than one landing pad successor", MBB);
452
Dan Gohman27920592009-08-27 02:43:49 +0000453 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
454 MachineBasicBlock *TBB = 0, *FBB = 0;
455 SmallVector<MachineOperand, 4> Cond;
456 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
457 TBB, FBB, Cond)) {
458 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
459 // check whether its answers match up with reality.
460 if (!TBB && !FBB) {
461 // Block falls through to its successor.
462 MachineFunction::const_iterator MBBI = MBB;
463 ++MBBI;
464 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000465 // It's possible that the block legitimately ends with a noreturn
466 // call or an unreachable, in which case it won't actually fall
467 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000468 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80f2009-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 actuall fall
471 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000472 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000473 report("MBB exits via unconditional fall-through but doesn't have "
474 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000475 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000476 report("MBB exits via unconditional fall-through but its successor "
477 "differs from its CFG successor!", MBB);
478 }
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000479 if (!MBB->empty() && getBundleStart(&MBB->back())->isBarrier() &&
480 !TII->isPredicated(getBundleStart(&MBB->back()))) {
Dan Gohman27920592009-08-27 02:43:49 +0000481 report("MBB exits via unconditional fall-through but ends with a "
482 "barrier instruction!", MBB);
483 }
484 if (!Cond.empty()) {
485 report("MBB exits via unconditional fall-through but has a condition!",
486 MBB);
487 }
488 } else if (TBB && !FBB && Cond.empty()) {
489 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000490 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000491 report("MBB exits via unconditional branch but doesn't have "
492 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000493 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000494 report("MBB exits via unconditional branch but the CFG "
495 "successor doesn't match the actual successor!", MBB);
496 }
497 if (MBB->empty()) {
498 report("MBB exits via unconditional branch but doesn't contain "
499 "any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000500 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000501 report("MBB exits via unconditional branch but doesn't end with a "
502 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000503 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000504 report("MBB exits via unconditional branch but the branch isn't a "
505 "terminator instruction!", MBB);
506 }
507 } else if (TBB && !FBB && !Cond.empty()) {
508 // Block conditionally branches somewhere, otherwise falls through.
509 MachineFunction::const_iterator MBBI = MBB;
510 ++MBBI;
511 if (MBBI == MF->end()) {
512 report("MBB conditionally falls through out of function!", MBB);
513 } if (MBB->succ_size() != 2) {
514 report("MBB exits via conditional branch/fall-through but doesn't have "
515 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000516 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000517 report("MBB exits via conditional branch/fall-through but the CFG "
518 "successors don't match the actual successors!", MBB);
519 }
520 if (MBB->empty()) {
521 report("MBB exits via conditional branch/fall-through but doesn't "
522 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000523 } else if (getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000524 report("MBB exits via conditional branch/fall-through but ends with a "
525 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000526 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000527 report("MBB exits via conditional branch/fall-through but the branch "
528 "isn't a terminator instruction!", MBB);
529 }
530 } else if (TBB && FBB) {
531 // Block conditionally branches somewhere, otherwise branches
532 // somewhere else.
533 if (MBB->succ_size() != 2) {
534 report("MBB exits via conditional branch/branch but doesn't have "
535 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000536 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000537 report("MBB exits via conditional branch/branch but the CFG "
538 "successors don't match the actual successors!", MBB);
539 }
540 if (MBB->empty()) {
541 report("MBB exits via conditional branch/branch but doesn't "
542 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000543 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000544 report("MBB exits via conditional branch/branch but doesn't end with a "
545 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000546 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000547 report("MBB exits via conditional branch/branch but the branch "
548 "isn't a terminator instruction!", MBB);
549 }
550 if (Cond.empty()) {
551 report("MBB exits via conditinal branch/branch but there's no "
552 "condition!", MBB);
553 }
554 } else {
555 report("AnalyzeBranch returned invalid data!", MBB);
556 }
557 }
558
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000559 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000560 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000561 E = MBB->livein_end(); I != E; ++I) {
562 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
563 report("MBB live-in list contains non-physical register", MBB);
564 continue;
565 }
566 regsLive.insert(*I);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000567 for (MCSubRegIterator SubRegs(*I, TRI); SubRegs.isValid(); ++SubRegs)
568 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000569 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000570 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000571
572 const MachineFrameInfo *MFI = MF->getFrameInfo();
573 assert(MFI && "Function has no frame info");
574 BitVector PR = MFI->getPristineRegs(MBB);
575 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
576 regsLive.insert(I);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000577 for (MCSubRegIterator SubRegs(I, TRI); SubRegs.isValid(); ++SubRegs)
578 regsLive.insert(*SubRegs);
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000579 }
580
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000581 regsKilled.clear();
582 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000583
584 if (Indexes)
585 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000586}
587
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000588// This function gets called for all bundle headers, including normal
589// stand-alone unbundled instructions.
590void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
591 if (Indexes && Indexes->hasIndex(MI)) {
592 SlotIndex idx = Indexes->getInstructionIndex(MI);
593 if (!(idx > lastIndex)) {
594 report("Instruction index out of order", MI);
595 *OS << "Last instruction was at " << lastIndex << '\n';
596 }
597 lastIndex = idx;
598 }
Pete Cooper83569cb2012-06-07 17:41:39 +0000599
600 // Ensure non-terminators don't follow terminators.
601 // Ignore predicated terminators formed by if conversion.
602 // FIXME: If conversion shouldn't need to violate this rule.
603 if (MI->isTerminator() && !TII->isPredicated(MI)) {
604 if (!FirstTerminator)
605 FirstTerminator = MI;
606 } else if (FirstTerminator) {
607 report("Non-terminator instruction after the first terminator", MI);
608 *OS << "First terminator was:\t" << *FirstTerminator;
609 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000610}
611
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000612void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000613 const MCInstrDesc &MCID = MI->getDesc();
614 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000615 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000616 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000617 << MI->getNumExplicitOperands() << " given.\n";
618 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000619
620 // Check the MachineMemOperands for basic consistency.
621 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
622 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000623 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000624 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000625 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000626 report("Missing mayStore flag", MI);
627 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000628
629 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000630 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000631 if (LiveInts) {
632 bool mapped = !LiveInts->isNotInMIMap(MI);
633 if (MI->isDebugValue()) {
634 if (mapped)
635 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000636 } else if (MI->isInsideBundle()) {
637 if (mapped)
638 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000639 } else {
640 if (!mapped)
641 report("Missing slot index", MI);
642 }
643 }
644
Andrew Trick3be654f2011-09-21 02:20:46 +0000645 StringRef ErrorInfo;
646 if (!TII->verifyInstruction(MI, ErrorInfo))
647 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000648}
649
650void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000651MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000652 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000653 const MCInstrDesc &MCID = MI->getDesc();
654 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000655
Evan Chenge837dea2011-06-28 19:10:37 +0000656 // The first MCID.NumDefs operands must be explicit register defines
657 if (MONum < MCID.getNumDefs()) {
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000658 if (!MO->isReg())
659 report("Explicit definition must be a register", MO, MONum);
Evan Chengcac58aa2012-05-29 19:40:44 +0000660 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000661 report("Explicit definition marked as use", MO, MONum);
662 else if (MO->isImplicit())
663 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000664 } else if (MONum < MCID.getNumOperands()) {
Eric Christopher113a06c2010-11-17 00:55:36 +0000665 // Don't check if it's the last operand in a variadic instruction. See,
666 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000667 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000668 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000669 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000670 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000671 if (MO->isImplicit())
672 report("Explicit operand marked as implicit", MO, MONum);
673 }
674 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000675 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000676 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000677 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000678 }
679
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000680 switch (MO->getType()) {
681 case MachineOperand::MO_Register: {
682 const unsigned Reg = MO->getReg();
683 if (!Reg)
684 return;
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000685 if (MRI->tracksLiveness() && !MI->isDebugValue())
686 checkLiveness(MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000687
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000688 // Verify two-address constraints after leaving SSA form.
689 unsigned DefIdx;
690 if (!MRI->isSSA() && MO->isUse() &&
691 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
692 Reg != MI->getOperand(DefIdx).getReg())
693 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000694
695 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000696 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000697 unsigned SubIdx = MO->getSubReg();
698
699 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000700 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000701 report("Illegal subregister index for physical register", MO, MONum);
702 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000703 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000704 if (const TargetRegisterClass *DRC =
705 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000706 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000707 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000708 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000709 << DRC->getName() << " register.\n";
710 }
711 }
712 } else {
713 // Virtual register.
714 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
715 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000716 const TargetRegisterClass *SRC =
717 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000718 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000719 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000720 *OS << "Register class " << RC->getName()
721 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000722 return;
723 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000724 if (RC != SRC) {
725 report("Invalid register class for subregister index", MO, MONum);
726 *OS << "Register class " << RC->getName()
727 << " does not fully support subreg index " << SubIdx << "\n";
728 return;
729 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000730 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000731 if (const TargetRegisterClass *DRC =
732 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000733 if (SubIdx) {
734 const TargetRegisterClass *SuperRC =
735 TRI->getLargestLegalSuperClass(RC);
736 if (!SuperRC) {
737 report("No largest legal super class exists.", MO, MONum);
738 return;
739 }
740 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
741 if (!DRC) {
742 report("No matching super-reg register class.", MO, MONum);
743 return;
744 }
745 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000746 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000747 report("Illegal virtual register for instruction", MO, MONum);
748 *OS << "Expected a " << DRC->getName() << " register, but got a "
749 << RC->getName() << " register\n";
750 }
751 }
752 }
753 }
754 break;
755 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000756
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000757 case MachineOperand::MO_RegisterMask:
758 regMasks.push_back(MO->getRegMask());
759 break;
760
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000761 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000762 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
763 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000764 break;
765
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000766 case MachineOperand::MO_FrameIndex:
767 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
768 LiveInts && !LiveInts->isNotInMIMap(MI)) {
769 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
770 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000771 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000772 report("Instruction loads from dead spill slot", MO, MONum);
773 *OS << "Live stack: " << LI << '\n';
774 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000775 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000776 report("Instruction stores to dead spill slot", MO, MONum);
777 *OS << "Live stack: " << LI << '\n';
778 }
779 }
780 break;
781
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000782 default:
783 break;
784 }
785}
786
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000787void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
788 const MachineInstr *MI = MO->getParent();
789 const unsigned Reg = MO->getReg();
790
791 // Both use and def operands can read a register.
792 if (MO->readsReg()) {
793 regsLiveInButUnused.erase(Reg);
794
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000795 if (MO->isKill())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000796 addRegWithSubRegs(regsKilled, Reg);
797
798 // Check that LiveVars knows this kill.
799 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
800 MO->isKill()) {
801 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
802 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
803 report("Kill missing from LiveVariables", MO, MONum);
804 }
805
806 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +0000807 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
808 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
809 // Check the cached regunit intervals.
810 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
811 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
812 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(*Units)) {
813 LiveRangeQuery LRQ(*LI, UseIdx);
814 if (!LRQ.valueIn()) {
815 report("No live range at use", MO, MONum);
816 *OS << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
817 << ' ' << *LI << '\n';
818 }
819 if (MO->isKill() && !LRQ.isKill()) {
820 report("Live range continues after kill flag", MO, MONum);
821 *OS << PrintRegUnit(*Units, TRI) << ' ' << *LI << '\n';
822 }
823 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000824 }
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +0000825 }
826
827 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
828 if (LiveInts->hasInterval(Reg)) {
829 // This is a virtual register interval.
830 const LiveInterval &LI = LiveInts->getInterval(Reg);
831 LiveRangeQuery LRQ(LI, UseIdx);
832 if (!LRQ.valueIn()) {
833 report("No live range at use", MO, MONum);
834 *OS << UseIdx << " is not live in " << LI << '\n';
835 }
836 // Check for extra kill flags.
837 // Note that we allow missing kill flags for now.
838 if (MO->isKill() && !LRQ.isKill()) {
839 report("Live range continues after kill flag", MO, MONum);
840 *OS << "Live range: " << LI << '\n';
841 }
842 } else {
843 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000844 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000845 }
846 }
847
848 // Use of a dead register.
849 if (!regsLive.count(Reg)) {
850 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
851 // Reserved registers may be used even when 'dead'.
852 if (!isReserved(Reg))
853 report("Using an undefined physical register", MO, MONum);
Pete Cooperb97c57a2012-07-19 23:40:38 +0000854 } else if (MRI->def_empty(Reg)) {
855 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000856 } else {
857 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
858 // We don't know which virtual registers are live in, so only complain
859 // if vreg was killed in this MBB. Otherwise keep track of vregs that
860 // must be live in. PHI instructions are handled separately.
861 if (MInfo.regsKilled.count(Reg))
862 report("Using a killed virtual register", MO, MONum);
863 else if (!MI->isPHI())
864 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
865 }
866 }
867 }
868
869 if (MO->isDef()) {
870 // Register defined.
871 // TODO: verify that earlyclobber ops are not used.
872 if (MO->isDead())
873 addRegWithSubRegs(regsDead, Reg);
874 else
875 addRegWithSubRegs(regsDefined, Reg);
876
877 // Verify SSA form.
878 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
879 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
880 report("Multiple virtual register defs in SSA form", MO, MONum);
881
882 // Check LiveInts for a live range, but only for virtual registers.
883 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
884 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +0000885 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
886 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000887 if (LiveInts->hasInterval(Reg)) {
888 const LiveInterval &LI = LiveInts->getInterval(Reg);
889 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
890 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +0000891 if (VNI->def != DefIdx) {
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000892 report("Inconsistent valno->def", MO, MONum);
893 *OS << "Valno " << VNI->id << " is not defined at "
894 << DefIdx << " in " << LI << '\n';
895 }
896 } else {
897 report("No live range at def", MO, MONum);
898 *OS << DefIdx << " is not live in " << LI << '\n';
899 }
900 } else {
901 report("Virtual register has no Live interval", MO, MONum);
902 }
903 }
904 }
905}
906
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000907void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000908}
909
910// This function gets called after visiting all instructions in a bundle. The
911// argument points to the bundle header.
912// Normal stand-alone instructions are also considered 'bundles', and this
913// function is called for all of them.
914void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000915 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
916 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000917 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000918 // Kill any masked registers.
919 while (!regMasks.empty()) {
920 const uint32_t *Mask = regMasks.pop_back_val();
921 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
922 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
923 MachineOperand::clobbersPhysReg(Mask, *I))
924 regsDead.push_back(*I);
925 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000926 set_subtract(regsLive, regsDead); regsDead.clear();
927 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000928}
929
930void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000931MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000932 MBBInfoMap[MBB].regsLiveOut = regsLive;
933 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000934
935 if (Indexes) {
936 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
937 if (!(stop > lastIndex)) {
938 report("Block ends before last instruction index", MBB);
939 *OS << "Block ends at " << stop
940 << " last instruction was at " << lastIndex << '\n';
941 }
942 lastIndex = stop;
943 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000944}
945
946// Calculate the largest possible vregsPassed sets. These are the registers that
947// can pass through an MBB live, but may not be live every time. It is assumed
948// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000949void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000950 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
951 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +0000952 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000953 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
954 MFI != MFE; ++MFI) {
955 const MachineBasicBlock &MBB(*MFI);
956 BBInfo &MInfo = MBBInfoMap[&MBB];
957 if (!MInfo.reachable)
958 continue;
959 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
960 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
961 BBInfo &SInfo = MBBInfoMap[*SuI];
962 if (SInfo.addPassed(MInfo.regsLiveOut))
963 todo.insert(*SuI);
964 }
965 }
966
967 // Iteratively push vregsPassed to successors. This will converge to the same
968 // final state regardless of DenseSet iteration order.
969 while (!todo.empty()) {
970 const MachineBasicBlock *MBB = *todo.begin();
971 todo.erase(MBB);
972 BBInfo &MInfo = MBBInfoMap[MBB];
973 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
974 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
975 if (*SuI == MBB)
976 continue;
977 BBInfo &SInfo = MBBInfoMap[*SuI];
978 if (SInfo.addPassed(MInfo.vregsPassed))
979 todo.insert(*SuI);
980 }
981 }
982}
983
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000984// Calculate the set of virtual registers that must be passed through each basic
985// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000986// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000987void MachineVerifier::calcRegsRequired() {
988 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +0000989 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000990 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
991 MFI != MFE; ++MFI) {
992 const MachineBasicBlock &MBB(*MFI);
993 BBInfo &MInfo = MBBInfoMap[&MBB];
994 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
995 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
996 BBInfo &PInfo = MBBInfoMap[*PrI];
997 if (PInfo.addRequired(MInfo.vregsLiveIn))
998 todo.insert(*PrI);
999 }
1000 }
1001
1002 // Iteratively push vregsRequired to predecessors. This will converge to the
1003 // same final state regardless of DenseSet iteration order.
1004 while (!todo.empty()) {
1005 const MachineBasicBlock *MBB = *todo.begin();
1006 todo.erase(MBB);
1007 BBInfo &MInfo = MBBInfoMap[MBB];
1008 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1009 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1010 if (*PrI == MBB)
1011 continue;
1012 BBInfo &SInfo = MBBInfoMap[*PrI];
1013 if (SInfo.addRequired(MInfo.vregsRequired))
1014 todo.insert(*PrI);
1015 }
1016 }
1017}
1018
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001019// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001020// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001021void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001022 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001023 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +00001024 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001025 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001026
1027 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
1028 unsigned Reg = BBI->getOperand(i).getReg();
1029 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
1030 if (!Pre->isSuccessor(MBB))
1031 continue;
1032 seen.insert(Pre);
1033 BBInfo &PrInfo = MBBInfoMap[Pre];
1034 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1035 report("PHI operand is not live-out from predecessor",
1036 &BBI->getOperand(i), i);
1037 }
1038
1039 // Did we see all predecessors?
1040 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1041 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1042 if (!seen.count(*PrI)) {
1043 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +00001044 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001045 << " is a predecessor according to the CFG.\n";
1046 }
1047 }
1048 }
1049}
1050
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001051void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001052 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001053
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001054 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1055 MFI != MFE; ++MFI) {
1056 BBInfo &MInfo = MBBInfoMap[MFI];
1057
1058 // Skip unreachable MBBs.
1059 if (!MInfo.reachable)
1060 continue;
1061
1062 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001063 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001064
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001065 // Now check liveness info if available
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001066 calcRegsRequired();
1067
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001068 // Check for killed virtual registers that should be live out.
1069 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1070 MFI != MFE; ++MFI) {
1071 BBInfo &MInfo = MBBInfoMap[MFI];
1072 for (RegSet::iterator
1073 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1074 ++I)
1075 if (MInfo.regsKilled.count(*I)) {
Bill Wendling96cb1122012-07-19 00:04:14 +00001076 report("Virtual register killed in block, but needed live out.", MFI);
1077 *OS << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001078 << " is used after the block.\n";
1079 }
1080 }
1081
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001082 if (!MF->empty()) {
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001083 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1084 for (RegSet::iterator
1085 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesenff0275e2012-03-10 00:44:11 +00001086 ++I)
1087 report("Virtual register def doesn't dominate all uses.",
1088 MRI->getVRegDef(*I));
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001089 }
1090
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001091 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001092 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001093 if (LiveInts)
1094 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001095}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001096
1097void MachineVerifier::verifyLiveVariables() {
1098 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001099 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1100 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001101 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1102 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1103 MFI != MFE; ++MFI) {
1104 BBInfo &MInfo = MBBInfoMap[MFI];
1105
1106 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1107 if (MInfo.vregsRequired.count(Reg)) {
1108 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1109 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001110 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001111 << " must be live through the block.\n";
1112 }
1113 } else {
1114 if (VI.AliveBlocks.test(MFI->getNumber())) {
1115 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001116 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001117 << " is not needed live through the block.\n";
1118 }
1119 }
1120 }
1121 }
1122}
1123
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001124void MachineVerifier::verifyLiveIntervals() {
1125 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001126 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1127 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001128
1129 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001130 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001131 continue;
1132
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001133 if (!LiveInts->hasInterval(Reg)) {
1134 report("Missing live interval for virtual register", MF);
1135 *OS << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001136 continue;
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001137 }
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001138
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001139 const LiveInterval &LI = LiveInts->getInterval(Reg);
1140 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001141
1142 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1143 I!=E; ++I) {
1144 VNInfo *VNI = *I;
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001145 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001146
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001147 if (!DefVNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001148 if (!VNI->isUnused()) {
1149 report("Valno not live at def and not marked unused", MF);
1150 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1151 }
1152 continue;
1153 }
1154
1155 if (VNI->isUnused())
1156 continue;
1157
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001158 if (DefVNI != VNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001159 report("Live range at def has different valno", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001160 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001161 << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001162 continue;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001163 }
1164
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001165 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1166 if (!MBB) {
1167 report("Invalid definition index", MF);
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001168 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1169 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001170 continue;
1171 }
1172
1173 if (VNI->isPHIDef()) {
1174 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1175 report("PHIDef value is not defined at MBB start", MF);
1176 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001177 << ", not at the beginning of BB#" << MBB->getNumber()
1178 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001179 }
1180 } else {
1181 // Non-PHI def.
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +00001182 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001183 if (!MI) {
1184 report("No instruction at def index", MF);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001185 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1186 << " in " << LI << '\n';
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001187 continue;
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001188 }
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001189
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001190 bool hasDef = false;
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001191 bool isEarlyClobber = false;
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +00001192 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001193 if (!MOI->isReg() || !MOI->isDef())
1194 continue;
1195 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1196 if (MOI->getReg() != LI.reg)
1197 continue;
1198 } else {
1199 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1200 !TRI->regsOverlap(LI.reg, MOI->getReg()))
1201 continue;
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001202 }
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001203 hasDef = true;
1204 if (MOI->isEarlyClobber())
1205 isEarlyClobber = true;
1206 }
1207
1208 if (!hasDef) {
1209 report("Defining instruction does not modify register", MI);
1210 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001211 }
1212
1213 // Early clobber defs begin at USE slots, but other defs must begin at
1214 // DEF slots.
1215 if (isEarlyClobber) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001216 if (!VNI->def.isEarlyClobber()) {
1217 report("Early clobber def must be at an early-clobber slot", MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001218 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1219 << " in " << LI << '\n';
1220 }
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001221 } else if (!VNI->def.isRegister()) {
1222 report("Non-PHI, non-early clobber def must be at a register slot",
1223 MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001224 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1225 << " in " << LI << '\n';
1226 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001227 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001228 }
1229
1230 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001231 const VNInfo *VNI = I->valno;
1232 assert(VNI && "Live range has no valno");
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001233
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001234 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001235 report("Foreign valno in live range", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001236 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001237 *OS << " has a valno not in " << LI << '\n';
1238 }
1239
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001240 if (VNI->isUnused()) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001241 report("Live range valno is marked unused", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001242 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001243 *OS << " in " << LI << '\n';
1244 }
1245
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001246 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1247 if (!MBB) {
1248 report("Bad start of live segment, no basic block", MF);
1249 I->print(*OS);
1250 *OS << " in " << LI << '\n';
1251 continue;
1252 }
1253 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1254 if (I->start != MBBStartIdx && I->start != VNI->def) {
1255 report("Live segment must begin at MBB entry or valno def", MBB);
1256 I->print(*OS);
1257 *OS << " in " << LI << '\n' << "Basic block starts at "
1258 << MBBStartIdx << '\n';
1259 }
1260
1261 const MachineBasicBlock *EndMBB =
1262 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1263 if (!EndMBB) {
1264 report("Bad end of live segment, no basic block", MF);
1265 I->print(*OS);
1266 *OS << " in " << LI << '\n';
1267 continue;
1268 }
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001269
1270 // No more checks for live-out segments.
1271 if (I->end == LiveInts->getMBBEndIdx(EndMBB))
1272 continue;
1273
1274 // The live segment is ending inside EndMBB
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +00001275 const MachineInstr *MI =
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001276 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1277 if (!MI) {
1278 report("Live segment doesn't end at a valid instruction", EndMBB);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001279 I->print(*OS);
1280 *OS << " in " << LI << '\n' << "Basic block starts at "
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001281 << MBBStartIdx << '\n';
1282 continue;
1283 }
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001284
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001285 // The block slot must refer to a basic block boundary.
1286 if (I->end.isBlock()) {
1287 report("Live segment ends at B slot of an instruction", MI);
1288 I->print(*OS);
1289 *OS << " in " << LI << '\n';
1290 }
1291
1292 if (I->end.isDead()) {
1293 // Segment ends on the dead slot.
1294 // That means there must be a dead def.
1295 if (!SlotIndex::isSameInstr(I->start, I->end)) {
1296 report("Live segment ending at dead slot spans instructions", MI);
1297 I->print(*OS);
1298 *OS << " in " << LI << '\n';
1299 }
1300 }
1301
1302 // A live segment can only end at an early-clobber slot if it is being
1303 // redefined by an early-clobber def.
1304 if (I->end.isEarlyClobber()) {
1305 if (I+1 == E || (I+1)->start != I->end) {
1306 report("Live segment ending at early clobber slot must be "
1307 "redefined by an EC def in the same instruction", MI);
1308 I->print(*OS);
1309 *OS << " in " << LI << '\n';
1310 }
1311 }
1312
1313 // The following checks only apply to virtual registers. Physreg liveness
1314 // is too weird to check.
1315 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1316 // A live range can end with either a redefinition, a kill flag on a
1317 // use, or a dead flag on a def.
1318 bool hasRead = false;
1319 bool hasDeadDef = false;
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +00001320 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001321 if (!MOI->isReg() || MOI->getReg() != LI.reg)
1322 continue;
1323 if (MOI->readsReg())
1324 hasRead = true;
1325 if (MOI->isDef() && MOI->isDead())
1326 hasDeadDef = true;
1327 }
1328
1329 if (I->end.isDead()) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001330 if (!hasDeadDef) {
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001331 report("Instruction doesn't have a dead def operand", MI);
1332 I->print(*OS);
1333 *OS << " in " << LI << '\n';
1334 }
1335 } else {
1336 if (!hasRead) {
1337 report("Instruction ending live range doesn't read the register",
1338 MI);
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001339 I->print(*OS);
1340 *OS << " in " << LI << '\n';
1341 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001342 }
1343 }
1344
1345 // Now check all the basic blocks in this live segment.
1346 MachineFunction::const_iterator MFI = MBB;
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001347 // Is this live range the beginning of a non-PHIDef VN?
1348 if (I->start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001349 // Not live-in to any blocks.
1350 if (MBB == EndMBB)
1351 continue;
1352 // Skip this block.
1353 ++MFI;
1354 }
1355 for (;;) {
1356 assert(LiveInts->isLiveInToMBB(LI, MFI));
Jakob Stoklund Olesene459d552010-10-26 16:49:23 +00001357 // We don't know how to track physregs into a landing pad.
1358 if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1359 MFI->isLandingPad()) {
1360 if (&*MFI == EndMBB)
1361 break;
1362 ++MFI;
1363 continue;
1364 }
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001365
1366 // Is VNI a PHI-def in the current block?
1367 bool IsPHI = VNI->isPHIDef() &&
1368 VNI->def == LiveInts->getMBBStartIdx(MFI);
1369
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001370 // Check that VNI is live-out of all predecessors.
1371 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1372 PE = MFI->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001373 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1374 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001375
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001376 // All predecessors must have a live-out value.
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001377 if (!PVNI) {
1378 report("Register not marked live out of predecessor", *PI);
1379 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001380 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001381 << PEnd << " in " << LI << '\n';
1382 continue;
1383 }
1384
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001385 // Only PHI-defs can take different predecessor values.
1386 if (!IsPHI && PVNI != VNI) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001387 report("Different value live out of predecessor", *PI);
1388 *OS << "Valno #" << PVNI->id << " live out of BB#"
1389 << (*PI)->getNumber() << '@' << PEnd
1390 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001391 << '@' << LiveInts->getMBBStartIdx(MFI) << " in "
1392 << PrintReg(Reg) << ": " << LI << '\n';
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001393 }
1394 }
1395 if (&*MFI == EndMBB)
1396 break;
1397 ++MFI;
1398 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001399 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001400
1401 // Check the LI only has one connected component.
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001402 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1403 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1404 unsigned NumComp = ConEQ.Classify(&LI);
1405 if (NumComp > 1) {
1406 report("Multiple connected components in live interval", MF);
1407 *OS << NumComp << " components in " << LI << '\n';
Jakob Stoklund Olesencb367772010-10-29 00:40:57 +00001408 for (unsigned comp = 0; comp != NumComp; ++comp) {
1409 *OS << comp << ": valnos";
1410 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1411 E = LI.vni_end(); I!=E; ++I)
1412 if (comp == ConEQ.getEqClass(*I))
1413 *OS << ' ' << (*I)->id;
1414 *OS << '\n';
1415 }
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001416 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001417 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001418 }
1419}