blob: 0a2c2f829678d72540cf7cf958b7334f4a2e4bbd [file] [log] [blame]
Bill Wendling5567bb02010-08-19 18:52:17 +00001//===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Pass to verify generated machine code. The following is checked:
11//
12// Operand counts: All explicit operands must be present.
13//
14// Register classes: All physical and virtual register operands must be
15// compatible with the register class required by the instruction descriptor.
16//
17// Register live intervals: Registers must be defined only once, and must be
18// defined before use.
19//
20// The machine code verifier is enabled from LLVMTargetMachine.cpp with the
21// command-line option -verify-machineinstrs, or by defining the environment
22// variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
23// the verifier errors.
24//===----------------------------------------------------------------------===//
25
Bill Wendlingd29052b2011-05-04 22:54:05 +000026#include "llvm/Instructions.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000027#include "llvm/Function.h"
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +000028#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000029#include "llvm/CodeGen/LiveVariables.h"
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +000030#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman2dbc4c82009-10-07 17:36:00 +000033#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
35#include "llvm/CodeGen/Passes.h"
Bill Wendlingd29052b2011-05-04 22:54:05 +000036#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000037#include "llvm/Target/TargetMachine.h"
38#include "llvm/Target/TargetRegisterInfo.h"
39#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000040#include "llvm/ADT/DenseSet.h"
41#include "llvm/ADT/SetOperations.h"
42#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000043#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000044#include "llvm/Support/ErrorHandling.h"
45#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000046using namespace llvm;
47
48namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000049 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000050
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000051 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000052 PASS(pass),
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000053 Banner(b),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000054 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000055 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000056
57 bool runOnMachineFunction(MachineFunction &MF);
58
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000059 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000060 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000061 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000062 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000063 const MachineFunction *MF;
64 const TargetMachine *TM;
Evan Cheng15993f82011-06-27 21:26:13 +000065 const TargetInstrInfo *TII;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000066 const TargetRegisterInfo *TRI;
67 const MachineRegisterInfo *MRI;
68
69 unsigned foundErrors;
70
71 typedef SmallVector<unsigned, 16> RegVector;
72 typedef DenseSet<unsigned> RegSet;
73 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
74
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000075 const MachineInstr *FirstTerminator;
76
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000077 BitVector regsReserved;
78 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000079 RegVector regsDefined, regsDead, regsKilled;
80 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000081
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000082 SlotIndex lastIndex;
83
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000084 // Add Reg and any sub-registers to RV
85 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
86 RV.push_back(Reg);
87 if (TargetRegisterInfo::isPhysicalRegister(Reg))
88 for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
89 RV.push_back(*R);
90 }
91
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000092 struct BBInfo {
93 // Is this MBB reachable from the MF entry point?
94 bool reachable;
95
96 // Vregs that must be live in because they are used without being
97 // defined. Map value is the user.
98 RegMap vregsLiveIn;
99
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000100 // Regs killed in MBB. They may be defined again, and will then be in both
101 // regsKilled and regsLiveOut.
102 RegSet regsKilled;
103
104 // Regs defined in MBB and live out. Note that vregs passing through may
105 // be live out without being mentioned here.
106 RegSet regsLiveOut;
107
108 // Vregs that pass through MBB untouched. This set is disjoint from
109 // regsKilled and regsLiveOut.
110 RegSet vregsPassed;
111
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000112 // Vregs that must pass through MBB because they are needed by a successor
113 // block. This set is disjoint from regsLiveOut.
114 RegSet vregsRequired;
115
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000116 BBInfo() : reachable(false) {}
117
118 // Add register to vregsPassed if it belongs there. Return true if
119 // anything changed.
120 bool addPassed(unsigned Reg) {
121 if (!TargetRegisterInfo::isVirtualRegister(Reg))
122 return false;
123 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
124 return false;
125 return vregsPassed.insert(Reg).second;
126 }
127
128 // Same for a full set.
129 bool addPassed(const RegSet &RS) {
130 bool changed = false;
131 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
132 if (addPassed(*I))
133 changed = true;
134 return changed;
135 }
136
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000137 // Add register to vregsRequired if it belongs there. Return true if
138 // anything changed.
139 bool addRequired(unsigned Reg) {
140 if (!TargetRegisterInfo::isVirtualRegister(Reg))
141 return false;
142 if (regsLiveOut.count(Reg))
143 return false;
144 return vregsRequired.insert(Reg).second;
145 }
146
147 // Same for a full set.
148 bool addRequired(const RegSet &RS) {
149 bool changed = false;
150 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
151 if (addRequired(*I))
152 changed = true;
153 return changed;
154 }
155
156 // Same for a full map.
157 bool addRequired(const RegMap &RM) {
158 bool changed = false;
159 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
160 if (addRequired(I->first))
161 changed = true;
162 return changed;
163 }
164
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000165 // Live-out registers are either in regsLiveOut or vregsPassed.
166 bool isLiveOut(unsigned Reg) const {
167 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
168 }
169 };
170
171 // Extra register info per MBB.
172 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
173
174 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000175 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000176 }
177
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000178 // Analysis information if available
179 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000180 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000181 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000182 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000183
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000184 void visitMachineFunctionBefore();
185 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
186 void visitMachineInstrBefore(const MachineInstr *MI);
187 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
188 void visitMachineInstrAfter(const MachineInstr *MI);
189 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
190 void visitMachineFunctionAfter();
191
192 void report(const char *msg, const MachineFunction *MF);
193 void report(const char *msg, const MachineBasicBlock *MBB);
194 void report(const char *msg, const MachineInstr *MI);
195 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
196
197 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000198 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000199 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000200
201 void calcRegsRequired();
202 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000203 void verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000204 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000205
206 struct MachineVerifierPass : public MachineFunctionPass {
207 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000208 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000209
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000210 MachineVerifierPass(const char *b = 0)
211 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000212 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
213 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000214
215 void getAnalysisUsage(AnalysisUsage &AU) const {
216 AU.setPreservesAll();
217 MachineFunctionPass::getAnalysisUsage(AU);
218 }
219
220 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000221 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000222 return false;
223 }
224 };
225
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000226}
227
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000228char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000229INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000230 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000231
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000232FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
233 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000234}
235
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000236void MachineFunction::verify(Pass *p, const char *Banner) const {
237 MachineVerifier(p, Banner)
238 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000239}
240
Chris Lattner17e9edc2009-08-23 02:51:22 +0000241bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
242 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000243 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000244 std::string ErrorInfo;
245 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
246 raw_fd_ostream::F_Append);
247 if (!ErrorInfo.empty()) {
248 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
249 exit(1);
250 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000251
Chris Lattner17e9edc2009-08-23 02:51:22 +0000252 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000253 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000254 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000255 }
256
257 foundErrors = 0;
258
259 this->MF = &MF;
260 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000261 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000262 TRI = TM->getRegisterInfo();
263 MRI = &MF.getRegInfo();
264
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000265 LiveVars = NULL;
266 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000267 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000268 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000269 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000270 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000271 // We don't want to verify LiveVariables if LiveIntervals is available.
272 if (!LiveInts)
273 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000274 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000275 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000276 }
277
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000278 visitMachineFunctionBefore();
279 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
280 MFI!=MFE; ++MFI) {
281 visitMachineBasicBlockBefore(MFI);
Evan Chengddfd1372011-12-14 02:11:42 +0000282 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
283 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000284 if (MBBI->getParent() != MFI) {
285 report("Bad instruction parent pointer", MFI);
286 *OS << "Instruction: " << *MBBI;
287 continue;
288 }
Evan Chengddfd1372011-12-14 02:11:42 +0000289 // Skip BUNDLE instruction for now. FIXME: We should add code to verify
290 // the BUNDLE's specifically.
291 if (MBBI->isBundle())
292 continue;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000293 visitMachineInstrBefore(MBBI);
294 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
295 visitMachineOperand(&MBBI->getOperand(I), I);
296 visitMachineInstrAfter(MBBI);
297 }
298 visitMachineBasicBlockAfter(MFI);
299 }
300 visitMachineFunctionAfter();
301
Chris Lattner17e9edc2009-08-23 02:51:22 +0000302 if (OutFile)
303 delete OutFile;
304 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000305 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000306
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000307 // Clean up.
308 regsLive.clear();
309 regsDefined.clear();
310 regsDead.clear();
311 regsKilled.clear();
312 regsLiveInButUnused.clear();
313 MBBInfoMap.clear();
314
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000315 return false; // no changes
316}
317
Chris Lattner372fefe2009-08-23 01:03:30 +0000318void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000319 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000320 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000321 if (!foundErrors++) {
322 if (Banner)
323 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000324 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000325 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000326 *OS << "*** Bad machine code: " << msg << " ***\n"
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000327 << "- function: " << MF->getFunction()->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000328}
329
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000330void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000331 assert(MBB);
332 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000333 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000334 << " " << (void*)MBB
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000335 << " (BB#" << MBB->getNumber() << ")";
336 if (Indexes)
337 *OS << " [" << Indexes->getMBBStartIdx(MBB)
338 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
339 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000340}
341
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000342void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000343 assert(MI);
344 report(msg, MI->getParent());
345 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000346 if (Indexes && Indexes->hasIndex(MI))
347 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000348 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000349}
350
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000351void MachineVerifier::report(const char *msg,
352 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000353 assert(MO);
354 report(msg, MO->getParent());
355 *OS << "- operand " << MONum << ": ";
356 MO->print(*OS, TM);
357 *OS << "\n";
358}
359
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000360void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000361 BBInfo &MInfo = MBBInfoMap[MBB];
362 if (!MInfo.reachable) {
363 MInfo.reachable = true;
364 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
365 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
366 markReachable(*SuI);
367 }
368}
369
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000370void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000371 lastIndex = SlotIndex();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000372 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000373
374 // A sub-register of a reserved register is also reserved
375 for (int Reg = regsReserved.find_first(); Reg>=0;
376 Reg = regsReserved.find_next(Reg)) {
377 for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
378 // FIXME: This should probably be:
379 // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
380 regsReserved.set(*Sub);
381 }
382 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000383 markReachable(&MF->front());
384}
385
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000386// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000387static bool matchPair(MachineBasicBlock::const_succ_iterator i,
388 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000389 if (*i == a)
390 return *++i == b;
391 if (*i == b)
392 return *++i == a;
393 return false;
394}
395
396void
397MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000398 FirstTerminator = 0;
399
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000400 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000401 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000402 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000403 E = MBB->succ_end(); I != E; ++I) {
404 if ((*I)->isLandingPad())
405 LandingPadSuccs.insert(*I);
406 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000407
408 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
409 const BasicBlock *BB = MBB->getBasicBlock();
410 if (LandingPadSuccs.size() > 1 &&
411 !(AsmInfo &&
412 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
413 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000414 report("MBB has more than one landing pad successor", MBB);
415
Dan Gohman27920592009-08-27 02:43:49 +0000416 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
417 MachineBasicBlock *TBB = 0, *FBB = 0;
418 SmallVector<MachineOperand, 4> Cond;
419 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
420 TBB, FBB, Cond)) {
421 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
422 // check whether its answers match up with reality.
423 if (!TBB && !FBB) {
424 // Block falls through to its successor.
425 MachineFunction::const_iterator MBBI = MBB;
426 ++MBBI;
427 if (MBBI == MF->end()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000428 // It's possible that the block legitimately ends with a noreturn
429 // call or an unreachable, in which case it won't actually fall
430 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000431 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000432 // It's possible that the block legitimately ends with a noreturn
433 // call or an unreachable, in which case it won't actuall fall
434 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000435 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000436 report("MBB exits via unconditional fall-through but doesn't have "
437 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000438 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000439 report("MBB exits via unconditional fall-through but its successor "
440 "differs from its CFG successor!", MBB);
441 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000442 if (!MBB->empty() && MBB->back().isBarrier() &&
Evan Cheng86050dc2010-06-18 23:09:54 +0000443 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000444 report("MBB exits via unconditional fall-through but ends with a "
445 "barrier instruction!", MBB);
446 }
447 if (!Cond.empty()) {
448 report("MBB exits via unconditional fall-through but has a condition!",
449 MBB);
450 }
451 } else if (TBB && !FBB && Cond.empty()) {
452 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000453 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000454 report("MBB exits via unconditional branch but doesn't have "
455 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000456 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000457 report("MBB exits via unconditional branch but the CFG "
458 "successor doesn't match the actual successor!", MBB);
459 }
460 if (MBB->empty()) {
461 report("MBB exits via unconditional branch but doesn't contain "
462 "any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000463 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000464 report("MBB exits via unconditional branch but doesn't end with a "
465 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000466 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000467 report("MBB exits via unconditional branch but the branch isn't a "
468 "terminator instruction!", MBB);
469 }
470 } else if (TBB && !FBB && !Cond.empty()) {
471 // Block conditionally branches somewhere, otherwise falls through.
472 MachineFunction::const_iterator MBBI = MBB;
473 ++MBBI;
474 if (MBBI == MF->end()) {
475 report("MBB conditionally falls through out of function!", MBB);
476 } if (MBB->succ_size() != 2) {
477 report("MBB exits via conditional branch/fall-through but doesn't have "
478 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000479 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000480 report("MBB exits via conditional branch/fall-through but the CFG "
481 "successors don't match the actual successors!", MBB);
482 }
483 if (MBB->empty()) {
484 report("MBB exits via conditional branch/fall-through but doesn't "
485 "contain any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000486 } else if (MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000487 report("MBB exits via conditional branch/fall-through but ends with a "
488 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000489 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000490 report("MBB exits via conditional branch/fall-through but the branch "
491 "isn't a terminator instruction!", MBB);
492 }
493 } else if (TBB && FBB) {
494 // Block conditionally branches somewhere, otherwise branches
495 // somewhere else.
496 if (MBB->succ_size() != 2) {
497 report("MBB exits via conditional branch/branch but doesn't have "
498 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000499 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000500 report("MBB exits via conditional branch/branch but the CFG "
501 "successors don't match the actual successors!", MBB);
502 }
503 if (MBB->empty()) {
504 report("MBB exits via conditional branch/branch but doesn't "
505 "contain any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000506 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000507 report("MBB exits via conditional branch/branch but doesn't end with a "
508 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000509 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000510 report("MBB exits via conditional branch/branch but the branch "
511 "isn't a terminator instruction!", MBB);
512 }
513 if (Cond.empty()) {
514 report("MBB exits via conditinal branch/branch but there's no "
515 "condition!", MBB);
516 }
517 } else {
518 report("AnalyzeBranch returned invalid data!", MBB);
519 }
520 }
521
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000522 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000523 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000524 E = MBB->livein_end(); I != E; ++I) {
525 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
526 report("MBB live-in list contains non-physical register", MBB);
527 continue;
528 }
529 regsLive.insert(*I);
530 for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++)
531 regsLive.insert(*R);
532 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000533 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000534
535 const MachineFrameInfo *MFI = MF->getFrameInfo();
536 assert(MFI && "Function has no frame info");
537 BitVector PR = MFI->getPristineRegs(MBB);
538 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
539 regsLive.insert(I);
540 for (const unsigned *R = TRI->getSubRegisters(I); *R; R++)
541 regsLive.insert(*R);
542 }
543
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000544 regsKilled.clear();
545 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000546
547 if (Indexes)
548 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000549}
550
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000551void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000552 const MCInstrDesc &MCID = MI->getDesc();
553 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000554 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000555 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000556 << MI->getNumExplicitOperands() << " given.\n";
557 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000558
559 // Check the MachineMemOperands for basic consistency.
560 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
561 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000562 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000563 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000564 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000565 report("Missing mayStore flag", MI);
566 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000567
568 // Debug values must not have a slot index.
569 // Other instructions must have one.
570 if (LiveInts) {
571 bool mapped = !LiveInts->isNotInMIMap(MI);
572 if (MI->isDebugValue()) {
573 if (mapped)
574 report("Debug instruction has a slot index", MI);
575 } else {
576 if (!mapped)
577 report("Missing slot index", MI);
578 }
579 }
580
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000581 // Ensure non-terminators don't follow terminators.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000582 if (MI->isTerminator()) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000583 if (!FirstTerminator)
584 FirstTerminator = MI;
585 } else if (FirstTerminator) {
586 report("Non-terminator instruction after the first terminator", MI);
587 *OS << "First terminator was:\t" << *FirstTerminator;
588 }
589
Andrew Trick3be654f2011-09-21 02:20:46 +0000590 StringRef ErrorInfo;
591 if (!TII->verifyInstruction(MI, ErrorInfo))
592 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000593}
594
595void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000596MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000597 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000598 const MCInstrDesc &MCID = MI->getDesc();
599 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000600
Evan Chenge837dea2011-06-28 19:10:37 +0000601 // The first MCID.NumDefs operands must be explicit register defines
602 if (MONum < MCID.getNumDefs()) {
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000603 if (!MO->isReg())
604 report("Explicit definition must be a register", MO, MONum);
605 else if (!MO->isDef())
606 report("Explicit definition marked as use", MO, MONum);
607 else if (MO->isImplicit())
608 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000609 } else if (MONum < MCID.getNumOperands()) {
Eric Christopher113a06c2010-11-17 00:55:36 +0000610 // Don't check if it's the last operand in a variadic instruction. See,
611 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000612 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000613 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000614 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000615 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000616 if (MO->isImplicit())
617 report("Explicit operand marked as implicit", MO, MONum);
618 }
619 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000620 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000621 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000622 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000623 }
624
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000625 switch (MO->getType()) {
626 case MachineOperand::MO_Register: {
627 const unsigned Reg = MO->getReg();
628 if (!Reg)
629 return;
630
631 // Check Live Variables.
Cameron Zwarich8ec88ba2010-12-20 00:08:10 +0000632 if (MI->isDebugValue()) {
633 // Liveness checks are not valid for debug values.
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000634 } else if (MO->isUse() && !MO->isUndef()) {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000635 regsLiveInButUnused.erase(Reg);
636
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000637 bool isKill = false;
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000638 unsigned defIdx;
639 if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
640 // A two-addr use counts as a kill if use and def are the same.
641 unsigned DefReg = MI->getOperand(defIdx).getReg();
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000642 if (Reg == DefReg)
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000643 isKill = true;
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000644 else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000645 report("Two-address instruction operands must be identical",
646 MO, MONum);
647 }
648 } else
649 isKill = MO->isKill();
650
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000651 if (isKill)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000652 addRegWithSubRegs(regsKilled, Reg);
653
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000654 // Check that LiveVars knows this kill.
655 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
656 MO->isKill()) {
657 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
658 if (std::find(VI.Kills.begin(),
659 VI.Kills.end(), MI) == VI.Kills.end())
660 report("Kill missing from LiveVariables", MO, MONum);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000661 }
662
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000663 // Check LiveInts liveness and kill.
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000664 if (TargetRegisterInfo::isVirtualRegister(Reg) &&
665 LiveInts && !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000666 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getRegSlot(true);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000667 if (LiveInts->hasInterval(Reg)) {
668 const LiveInterval &LI = LiveInts->getInterval(Reg);
669 if (!LI.liveAt(UseIdx)) {
670 report("No live range at use", MO, MONum);
671 *OS << UseIdx << " is not live in " << LI << '\n';
672 }
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000673 // Check for extra kill flags.
674 // Note that we allow missing kill flags for now.
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000675 if (MO->isKill() && !LI.killedAt(UseIdx.getRegSlot())) {
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000676 report("Live range continues after kill flag", MO, MONum);
677 *OS << "Live range: " << LI << '\n';
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000678 }
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000679 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000680 report("Virtual register has no Live interval", MO, MONum);
681 }
682 }
683
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000684 // Use of a dead register.
685 if (!regsLive.count(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000686 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen4af0f5f2011-07-30 00:57:25 +0000687 // Reserved registers may be used even when 'dead'.
688 if (!isReserved(Reg))
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000689 report("Using an undefined physical register", MO, MONum);
690 } else {
691 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
692 // We don't know which virtual registers are live in, so only complain
693 // if vreg was killed in this MBB. Otherwise keep track of vregs that
694 // must be live in. PHI instructions are handled separately.
695 if (MInfo.regsKilled.count(Reg))
696 report("Using a killed virtual register", MO, MONum);
Chris Lattner518bb532010-02-09 19:54:29 +0000697 else if (!MI->isPHI())
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000698 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
699 }
Duncan Sandse5567202009-05-16 03:28:54 +0000700 }
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000701 } else if (MO->isDef()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000702 // Register defined.
703 // TODO: verify that earlyclobber ops are not used.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000704 if (MO->isDead())
705 addRegWithSubRegs(regsDead, Reg);
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000706 else
707 addRegWithSubRegs(regsDefined, Reg);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000708
Jakob Stoklund Olesen93e6f022011-07-29 23:02:48 +0000709 // Verify SSA form.
710 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
711 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
712 report("Multiple virtual register defs in SSA form", MO, MONum);
713
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000714 // Check LiveInts for a live range, but only for virtual registers.
715 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
716 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000717 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getRegSlot();
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000718 if (LiveInts->hasInterval(Reg)) {
719 const LiveInterval &LI = LiveInts->getInterval(Reg);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000720 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
721 assert(VNI && "NULL valno is not allowed");
Cameron Zwarich1b031dd2010-12-19 23:50:53 +0000722 if (VNI->def != DefIdx && !MO->isEarlyClobber()) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000723 report("Inconsistent valno->def", MO, MONum);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000724 *OS << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000725 << DefIdx << " in " << LI << '\n';
726 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000727 } else {
728 report("No live range at def", MO, MONum);
729 *OS << DefIdx << " is not live in " << LI << '\n';
730 }
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000731 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000732 report("Virtual register has no Live interval", MO, MONum);
733 }
734 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000735 }
736
737 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000738 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000739 unsigned SubIdx = MO->getSubReg();
740
741 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000742 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000743 report("Illegal subregister index for physical register", MO, MONum);
744 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000745 }
Evan Chenge837dea2011-06-28 19:10:37 +0000746 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000747 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000748 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000749 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000750 << DRC->getName() << " register.\n";
751 }
752 }
753 } else {
754 // Virtual register.
755 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
756 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000757 const TargetRegisterClass *SRC =
758 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000759 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000760 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000761 *OS << "Register class " << RC->getName()
762 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000763 return;
764 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000765 if (RC != SRC) {
766 report("Invalid register class for subregister index", MO, MONum);
767 *OS << "Register class " << RC->getName()
768 << " does not fully support subreg index " << SubIdx << "\n";
769 return;
770 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000771 }
Evan Chenge837dea2011-06-28 19:10:37 +0000772 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000773 if (SubIdx) {
774 const TargetRegisterClass *SuperRC =
775 TRI->getLargestLegalSuperClass(RC);
776 if (!SuperRC) {
777 report("No largest legal super class exists.", MO, MONum);
778 return;
779 }
780 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
781 if (!DRC) {
782 report("No matching super-reg register class.", MO, MONum);
783 return;
784 }
785 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000786 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000787 report("Illegal virtual register for instruction", MO, MONum);
788 *OS << "Expected a " << DRC->getName() << " register, but got a "
789 << RC->getName() << " register\n";
790 }
791 }
792 }
793 }
794 break;
795 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000796
797 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000798 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
799 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000800 break;
801
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000802 case MachineOperand::MO_FrameIndex:
803 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
804 LiveInts && !LiveInts->isNotInMIMap(MI)) {
805 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
806 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000807 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000808 report("Instruction loads from dead spill slot", MO, MONum);
809 *OS << "Live stack: " << LI << '\n';
810 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000811 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000812 report("Instruction stores to dead spill slot", MO, MONum);
813 *OS << "Live stack: " << LI << '\n';
814 }
815 }
816 break;
817
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000818 default:
819 break;
820 }
821}
822
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000823void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000824 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
825 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000826 set_subtract(regsLive, regsKilled); regsKilled.clear();
827 set_subtract(regsLive, regsDead); regsDead.clear();
828 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000829
830 if (Indexes && Indexes->hasIndex(MI)) {
831 SlotIndex idx = Indexes->getInstructionIndex(MI);
832 if (!(idx > lastIndex)) {
833 report("Instruction index out of order", MI);
834 *OS << "Last instruction was at " << lastIndex << '\n';
835 }
836 lastIndex = idx;
837 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000838}
839
840void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000841MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000842 MBBInfoMap[MBB].regsLiveOut = regsLive;
843 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000844
845 if (Indexes) {
846 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
847 if (!(stop > lastIndex)) {
848 report("Block ends before last instruction index", MBB);
849 *OS << "Block ends at " << stop
850 << " last instruction was at " << lastIndex << '\n';
851 }
852 lastIndex = stop;
853 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000854}
855
856// Calculate the largest possible vregsPassed sets. These are the registers that
857// can pass through an MBB live, but may not be live every time. It is assumed
858// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000859void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000860 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
861 // have any vregsPassed.
862 DenseSet<const MachineBasicBlock*> todo;
863 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
864 MFI != MFE; ++MFI) {
865 const MachineBasicBlock &MBB(*MFI);
866 BBInfo &MInfo = MBBInfoMap[&MBB];
867 if (!MInfo.reachable)
868 continue;
869 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
870 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
871 BBInfo &SInfo = MBBInfoMap[*SuI];
872 if (SInfo.addPassed(MInfo.regsLiveOut))
873 todo.insert(*SuI);
874 }
875 }
876
877 // Iteratively push vregsPassed to successors. This will converge to the same
878 // final state regardless of DenseSet iteration order.
879 while (!todo.empty()) {
880 const MachineBasicBlock *MBB = *todo.begin();
881 todo.erase(MBB);
882 BBInfo &MInfo = MBBInfoMap[MBB];
883 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
884 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
885 if (*SuI == MBB)
886 continue;
887 BBInfo &SInfo = MBBInfoMap[*SuI];
888 if (SInfo.addPassed(MInfo.vregsPassed))
889 todo.insert(*SuI);
890 }
891 }
892}
893
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000894// Calculate the set of virtual registers that must be passed through each basic
895// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000896// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000897void MachineVerifier::calcRegsRequired() {
898 // First push live-in regs to predecessors' vregsRequired.
899 DenseSet<const MachineBasicBlock*> todo;
900 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
901 MFI != MFE; ++MFI) {
902 const MachineBasicBlock &MBB(*MFI);
903 BBInfo &MInfo = MBBInfoMap[&MBB];
904 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
905 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
906 BBInfo &PInfo = MBBInfoMap[*PrI];
907 if (PInfo.addRequired(MInfo.vregsLiveIn))
908 todo.insert(*PrI);
909 }
910 }
911
912 // Iteratively push vregsRequired to predecessors. This will converge to the
913 // same final state regardless of DenseSet iteration order.
914 while (!todo.empty()) {
915 const MachineBasicBlock *MBB = *todo.begin();
916 todo.erase(MBB);
917 BBInfo &MInfo = MBBInfoMap[MBB];
918 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
919 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
920 if (*PrI == MBB)
921 continue;
922 BBInfo &SInfo = MBBInfoMap[*PrI];
923 if (SInfo.addRequired(MInfo.vregsRequired))
924 todo.insert(*PrI);
925 }
926 }
927}
928
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000929// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000930// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000931void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000932 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +0000933 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000934 DenseSet<const MachineBasicBlock*> seen;
935
936 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
937 unsigned Reg = BBI->getOperand(i).getReg();
938 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
939 if (!Pre->isSuccessor(MBB))
940 continue;
941 seen.insert(Pre);
942 BBInfo &PrInfo = MBBInfoMap[Pre];
943 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
944 report("PHI operand is not live-out from predecessor",
945 &BBI->getOperand(i), i);
946 }
947
948 // Did we see all predecessors?
949 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
950 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
951 if (!seen.count(*PrI)) {
952 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000953 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000954 << " is a predecessor according to the CFG.\n";
955 }
956 }
957 }
958}
959
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000960void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000961 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000962
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000963 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
964 MFI != MFE; ++MFI) {
965 BBInfo &MInfo = MBBInfoMap[MFI];
966
967 // Skip unreachable MBBs.
968 if (!MInfo.reachable)
969 continue;
970
971 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000972 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000973
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000974 // Now check liveness info if available
975 if (LiveVars || LiveInts)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000976 calcRegsRequired();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000977 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000978 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000979 if (LiveInts)
980 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000981}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000982
983void MachineVerifier::verifyLiveVariables() {
984 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +0000985 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
986 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000987 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
988 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
989 MFI != MFE; ++MFI) {
990 BBInfo &MInfo = MBBInfoMap[MFI];
991
992 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
993 if (MInfo.vregsRequired.count(Reg)) {
994 if (!VI.AliveBlocks.test(MFI->getNumber())) {
995 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000996 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000997 << " must be live through the block.\n";
998 }
999 } else {
1000 if (VI.AliveBlocks.test(MFI->getNumber())) {
1001 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001002 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001003 << " is not needed live through the block.\n";
1004 }
1005 }
1006 }
1007 }
1008}
1009
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001010void MachineVerifier::verifyLiveIntervals() {
1011 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
1012 for (LiveIntervals::const_iterator LVI = LiveInts->begin(),
1013 LVE = LiveInts->end(); LVI != LVE; ++LVI) {
1014 const LiveInterval &LI = *LVI->second;
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001015
1016 // Spilling and splitting may leave unused registers around. Skip them.
1017 if (MRI->use_empty(LI.reg))
1018 continue;
1019
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001020 // Physical registers have much weirdness going on, mostly from coalescing.
1021 // We should probably fix it, but for now just ignore them.
1022 if (TargetRegisterInfo::isPhysicalRegister(LI.reg))
1023 continue;
1024
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001025 assert(LVI->first == LI.reg && "Invalid reg to interval mapping");
1026
1027 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1028 I!=E; ++I) {
1029 VNInfo *VNI = *I;
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001030 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001031
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001032 if (!DefVNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001033 if (!VNI->isUnused()) {
1034 report("Valno not live at def and not marked unused", MF);
1035 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1036 }
1037 continue;
1038 }
1039
1040 if (VNI->isUnused())
1041 continue;
1042
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001043 if (DefVNI != VNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001044 report("Live range at def has different valno", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001045 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001046 << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001047 continue;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001048 }
1049
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001050 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1051 if (!MBB) {
1052 report("Invalid definition index", MF);
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001053 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1054 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001055 continue;
1056 }
1057
1058 if (VNI->isPHIDef()) {
1059 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1060 report("PHIDef value is not defined at MBB start", MF);
1061 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001062 << ", not at the beginning of BB#" << MBB->getNumber()
1063 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001064 }
1065 } else {
1066 // Non-PHI def.
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001067 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1068 if (!MI) {
1069 report("No instruction at def index", MF);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001070 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1071 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001072 } else if (!MI->modifiesRegister(LI.reg, TRI)) {
1073 report("Defining instruction does not modify register", MI);
1074 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1075 }
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001076
1077 bool isEarlyClobber = false;
1078 if (MI) {
1079 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1080 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1081 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() &&
1082 MOI->isEarlyClobber()) {
1083 isEarlyClobber = true;
1084 break;
1085 }
1086 }
1087 }
1088
1089 // Early clobber defs begin at USE slots, but other defs must begin at
1090 // DEF slots.
1091 if (isEarlyClobber) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001092 if (!VNI->def.isEarlyClobber()) {
1093 report("Early clobber def must be at an early-clobber slot", MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001094 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1095 << " in " << LI << '\n';
1096 }
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001097 } else if (!VNI->def.isRegister()) {
1098 report("Non-PHI, non-early clobber def must be at a register slot",
1099 MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001100 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1101 << " in " << LI << '\n';
1102 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001103 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001104 }
1105
1106 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001107 const VNInfo *VNI = I->valno;
1108 assert(VNI && "Live range has no valno");
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001109
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001110 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001111 report("Foreign valno in live range", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001112 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001113 *OS << " has a valno not in " << LI << '\n';
1114 }
1115
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001116 if (VNI->isUnused()) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001117 report("Live range valno is marked unused", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001118 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001119 *OS << " in " << LI << '\n';
1120 }
1121
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001122 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1123 if (!MBB) {
1124 report("Bad start of live segment, no basic block", MF);
1125 I->print(*OS);
1126 *OS << " in " << LI << '\n';
1127 continue;
1128 }
1129 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1130 if (I->start != MBBStartIdx && I->start != VNI->def) {
1131 report("Live segment must begin at MBB entry or valno def", MBB);
1132 I->print(*OS);
1133 *OS << " in " << LI << '\n' << "Basic block starts at "
1134 << MBBStartIdx << '\n';
1135 }
1136
1137 const MachineBasicBlock *EndMBB =
1138 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1139 if (!EndMBB) {
1140 report("Bad end of live segment, no basic block", MF);
1141 I->print(*OS);
1142 *OS << " in " << LI << '\n';
1143 continue;
1144 }
1145 if (I->end != LiveInts->getMBBEndIdx(EndMBB)) {
1146 // The live segment is ending inside EndMBB
1147 const MachineInstr *MI =
1148 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1149 if (!MI) {
1150 report("Live segment doesn't end at a valid instruction", EndMBB);
1151 I->print(*OS);
1152 *OS << " in " << LI << '\n' << "Basic block starts at "
1153 << MBBStartIdx << '\n';
1154 } else if (TargetRegisterInfo::isVirtualRegister(LI.reg) &&
1155 !MI->readsVirtualRegister(LI.reg)) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001156 // A live range can end with either a redefinition, a kill flag on a
1157 // use, or a dead flag on a def.
1158 // FIXME: Should we check for each of these?
1159 bool hasDeadDef = false;
1160 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1161 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
Cameron Zwarich5e61f992010-12-20 02:59:51 +00001162 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() && MOI->isDead()) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001163 hasDeadDef = true;
1164 break;
1165 }
1166 }
1167
1168 if (!hasDeadDef) {
1169 report("Instruction killing live segment neither defines nor reads "
1170 "register", MI);
1171 I->print(*OS);
1172 *OS << " in " << LI << '\n';
1173 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001174 }
1175 }
1176
1177 // Now check all the basic blocks in this live segment.
1178 MachineFunction::const_iterator MFI = MBB;
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001179 // Is this live range the beginning of a non-PHIDef VN?
1180 if (I->start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001181 // Not live-in to any blocks.
1182 if (MBB == EndMBB)
1183 continue;
1184 // Skip this block.
1185 ++MFI;
1186 }
1187 for (;;) {
1188 assert(LiveInts->isLiveInToMBB(LI, MFI));
Jakob Stoklund Olesene459d552010-10-26 16:49:23 +00001189 // We don't know how to track physregs into a landing pad.
1190 if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1191 MFI->isLandingPad()) {
1192 if (&*MFI == EndMBB)
1193 break;
1194 ++MFI;
1195 continue;
1196 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001197 // Check that VNI is live-out of all predecessors.
1198 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1199 PE = MFI->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001200 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1201 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001202
Jakob Stoklund Olesendf8412c2011-09-15 05:16:30 +00001203 if (VNI->isPHIDef() && VNI->def == LiveInts->getMBBStartIdx(MFI))
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001204 continue;
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001205
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001206 if (!PVNI) {
1207 report("Register not marked live out of predecessor", *PI);
1208 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001209 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001210 << PEnd << " in " << LI << '\n';
1211 continue;
1212 }
1213
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001214 if (PVNI != VNI) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001215 report("Different value live out of predecessor", *PI);
1216 *OS << "Valno #" << PVNI->id << " live out of BB#"
1217 << (*PI)->getNumber() << '@' << PEnd
1218 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1219 << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n';
1220 }
1221 }
1222 if (&*MFI == EndMBB)
1223 break;
1224 ++MFI;
1225 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001226 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001227
1228 // Check the LI only has one connected component.
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001229 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1230 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1231 unsigned NumComp = ConEQ.Classify(&LI);
1232 if (NumComp > 1) {
1233 report("Multiple connected components in live interval", MF);
1234 *OS << NumComp << " components in " << LI << '\n';
Jakob Stoklund Olesencb367772010-10-29 00:40:57 +00001235 for (unsigned comp = 0; comp != NumComp; ++comp) {
1236 *OS << comp << ": valnos";
1237 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1238 E = LI.vni_end(); I!=E; ++I)
1239 if (comp == ConEQ.getEqClass(*I))
1240 *OS << ' ' << (*I)->id;
1241 *OS << '\n';
1242 }
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001243 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001244 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001245 }
1246}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001247