blob: 14bf53e3a85e05f6b93688bc0a52e83b59bda14f [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
Craig Topper96601ca2012-08-22 06:07:19 +000026#include "llvm/BasicBlock.h"
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +000027#include "llvm/InlineAsm.h"
Bill Wendlingd29052b2011-05-04 22:54:05 +000028#include "llvm/Instructions.h"
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +000029#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000030#include "llvm/CodeGen/LiveVariables.h"
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +000031#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +000032#include "llvm/CodeGen/MachineInstrBundle.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000033#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +000034#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman2dbc4c82009-10-07 17:36:00 +000035#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
37#include "llvm/CodeGen/Passes.h"
Bill Wendlingd29052b2011-05-04 22:54:05 +000038#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000039#include "llvm/Target/TargetMachine.h"
40#include "llvm/Target/TargetRegisterInfo.h"
41#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000042#include "llvm/ADT/DenseSet.h"
43#include "llvm/ADT/SetOperations.h"
44#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000045#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000046#include "llvm/Support/ErrorHandling.h"
47#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000048using namespace llvm;
49
50namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000051 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000052
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000053 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000054 PASS(pass),
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000055 Banner(b),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000056 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000057 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000058
59 bool runOnMachineFunction(MachineFunction &MF);
60
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000061 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000062 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000063 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000064 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000065 const MachineFunction *MF;
66 const TargetMachine *TM;
Evan Cheng15993f82011-06-27 21:26:13 +000067 const TargetInstrInfo *TII;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000068 const TargetRegisterInfo *TRI;
69 const MachineRegisterInfo *MRI;
70
71 unsigned foundErrors;
72
73 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000074 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000075 typedef DenseSet<unsigned> RegSet;
76 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +000077 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000078
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000079 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +000080 BlockSet FunctionBlocks;
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000081
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000082 BitVector regsReserved;
Lang Hames03698de2012-02-14 19:17:48 +000083 BitVector regsAllocatable;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000084 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000085 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000086 RegMaskVector regMasks;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000087 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000088
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000089 SlotIndex lastIndex;
90
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000091 // Add Reg and any sub-registers to RV
92 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
93 RV.push_back(Reg);
94 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +000095 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
96 RV.push_back(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000097 }
98
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000099 struct BBInfo {
100 // Is this MBB reachable from the MF entry point?
101 bool reachable;
102
103 // Vregs that must be live in because they are used without being
104 // defined. Map value is the user.
105 RegMap vregsLiveIn;
106
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000107 // Regs killed in MBB. They may be defined again, and will then be in both
108 // regsKilled and regsLiveOut.
109 RegSet regsKilled;
110
111 // Regs defined in MBB and live out. Note that vregs passing through may
112 // be live out without being mentioned here.
113 RegSet regsLiveOut;
114
115 // Vregs that pass through MBB untouched. This set is disjoint from
116 // regsKilled and regsLiveOut.
117 RegSet vregsPassed;
118
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000119 // Vregs that must pass through MBB because they are needed by a successor
120 // block. This set is disjoint from regsLiveOut.
121 RegSet vregsRequired;
122
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000123 // Set versions of block's predecessor and successor lists.
124 BlockSet Preds, Succs;
125
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000126 BBInfo() : reachable(false) {}
127
128 // Add register to vregsPassed if it belongs there. Return true if
129 // anything changed.
130 bool addPassed(unsigned Reg) {
131 if (!TargetRegisterInfo::isVirtualRegister(Reg))
132 return false;
133 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
134 return false;
135 return vregsPassed.insert(Reg).second;
136 }
137
138 // Same for a full set.
139 bool addPassed(const RegSet &RS) {
140 bool changed = false;
141 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
142 if (addPassed(*I))
143 changed = true;
144 return changed;
145 }
146
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000147 // Add register to vregsRequired if it belongs there. Return true if
148 // anything changed.
149 bool addRequired(unsigned Reg) {
150 if (!TargetRegisterInfo::isVirtualRegister(Reg))
151 return false;
152 if (regsLiveOut.count(Reg))
153 return false;
154 return vregsRequired.insert(Reg).second;
155 }
156
157 // Same for a full set.
158 bool addRequired(const RegSet &RS) {
159 bool changed = false;
160 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
161 if (addRequired(*I))
162 changed = true;
163 return changed;
164 }
165
166 // Same for a full map.
167 bool addRequired(const RegMap &RM) {
168 bool changed = false;
169 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
170 if (addRequired(I->first))
171 changed = true;
172 return changed;
173 }
174
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000175 // Live-out registers are either in regsLiveOut or vregsPassed.
176 bool isLiveOut(unsigned Reg) const {
177 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
178 }
179 };
180
181 // Extra register info per MBB.
182 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
183
184 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000185 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000186 }
187
Lang Hames03698de2012-02-14 19:17:48 +0000188 bool isAllocatable(unsigned Reg) {
189 return Reg < regsAllocatable.size() && regsAllocatable.test(Reg);
190 }
191
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000192 // Analysis information if available
193 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000194 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000195 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000196 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000197
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000198 void visitMachineFunctionBefore();
199 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000200 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000201 void visitMachineInstrBefore(const MachineInstr *MI);
202 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
203 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000204 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000205 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
206 void visitMachineFunctionAfter();
207
208 void report(const char *msg, const MachineFunction *MF);
209 void report(const char *msg, const MachineBasicBlock *MBB);
210 void report(const char *msg, const MachineInstr *MI);
211 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000212 void report(const char *msg, const MachineFunction *MF,
213 const LiveInterval &LI);
214 void report(const char *msg, const MachineBasicBlock *MBB,
215 const LiveInterval &LI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000216
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000217 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000218
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000219 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000220 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000221 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000222 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000223
224 void calcRegsRequired();
225 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000226 void verifyLiveIntervals();
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +0000227 void verifyLiveInterval(const LiveInterval&);
228 void verifyLiveIntervalValue(const LiveInterval&, VNInfo*);
229 void verifyLiveIntervalSegment(const LiveInterval&,
230 LiveInterval::const_iterator);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000231 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000232
233 struct MachineVerifierPass : public MachineFunctionPass {
234 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000235 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000236
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000237 MachineVerifierPass(const char *b = 0)
238 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000239 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
240 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000241
242 void getAnalysisUsage(AnalysisUsage &AU) const {
243 AU.setPreservesAll();
244 MachineFunctionPass::getAnalysisUsage(AU);
245 }
246
247 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000248 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000249 return false;
250 }
251 };
252
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000253}
254
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000255char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000256INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000257 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000258
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000259FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
260 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000261}
262
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000263void MachineFunction::verify(Pass *p, const char *Banner) const {
264 MachineVerifier(p, Banner)
265 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000266}
267
Chris Lattner17e9edc2009-08-23 02:51:22 +0000268bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
269 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000270 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000271 std::string ErrorInfo;
272 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
273 raw_fd_ostream::F_Append);
274 if (!ErrorInfo.empty()) {
275 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
276 exit(1);
277 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000278
Chris Lattner17e9edc2009-08-23 02:51:22 +0000279 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000280 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000281 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000282 }
283
284 foundErrors = 0;
285
286 this->MF = &MF;
287 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000288 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000289 TRI = TM->getRegisterInfo();
290 MRI = &MF.getRegInfo();
291
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000292 LiveVars = NULL;
293 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000294 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000295 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000296 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000297 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000298 // We don't want to verify LiveVariables if LiveIntervals is available.
299 if (!LiveInts)
300 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000301 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000302 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000303 }
304
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000305 visitMachineFunctionBefore();
306 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
307 MFI!=MFE; ++MFI) {
308 visitMachineBasicBlockBefore(MFI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000309 // Keep track of the current bundle header.
310 const MachineInstr *CurBundle = 0;
Evan Chengddfd1372011-12-14 02:11:42 +0000311 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
312 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000313 if (MBBI->getParent() != MFI) {
314 report("Bad instruction parent pointer", MFI);
315 *OS << "Instruction: " << *MBBI;
316 continue;
317 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000318 // Is this a bundle header?
319 if (!MBBI->isInsideBundle()) {
320 if (CurBundle)
321 visitMachineBundleAfter(CurBundle);
322 CurBundle = MBBI;
323 visitMachineBundleBefore(CurBundle);
324 } else if (!CurBundle)
325 report("No bundle header", MBBI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000326 visitMachineInstrBefore(MBBI);
327 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
328 visitMachineOperand(&MBBI->getOperand(I), I);
329 visitMachineInstrAfter(MBBI);
330 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000331 if (CurBundle)
332 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000333 visitMachineBasicBlockAfter(MFI);
334 }
335 visitMachineFunctionAfter();
336
Chris Lattner17e9edc2009-08-23 02:51:22 +0000337 if (OutFile)
338 delete OutFile;
339 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000340 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000341
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000342 // Clean up.
343 regsLive.clear();
344 regsDefined.clear();
345 regsDead.clear();
346 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000347 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000348 regsLiveInButUnused.clear();
349 MBBInfoMap.clear();
350
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000351 return false; // no changes
352}
353
Chris Lattner372fefe2009-08-23 01:03:30 +0000354void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000355 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000356 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000357 if (!foundErrors++) {
358 if (Banner)
359 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000360 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000361 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000362 *OS << "*** Bad machine code: " << msg << " ***\n"
Craig Topper96601ca2012-08-22 06:07:19 +0000363 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000364}
365
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000366void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000367 assert(MBB);
368 report(msg, MBB->getParent());
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000369 *OS << "- basic block: BB#" << MBB->getNumber()
370 << ' ' << MBB->getName()
Roman Divacky59324292012-09-05 22:26:57 +0000371 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000372 if (Indexes)
373 *OS << " [" << Indexes->getMBBStartIdx(MBB)
374 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
375 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000376}
377
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000378void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000379 assert(MI);
380 report(msg, MI->getParent());
381 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000382 if (Indexes && Indexes->hasIndex(MI))
383 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000384 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000385}
386
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000387void MachineVerifier::report(const char *msg,
388 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000389 assert(MO);
390 report(msg, MO->getParent());
391 *OS << "- operand " << MONum << ": ";
392 MO->print(*OS, TM);
393 *OS << "\n";
394}
395
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000396void MachineVerifier::report(const char *msg, const MachineFunction *MF,
397 const LiveInterval &LI) {
398 report(msg, MF);
399 *OS << "- interval: ";
400 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
401 *OS << PrintReg(LI.reg, TRI);
402 else
403 *OS << PrintRegUnit(LI.reg, TRI);
404 *OS << ' ' << LI << '\n';
405}
406
407void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
408 const LiveInterval &LI) {
409 report(msg, MBB);
410 *OS << "- interval: ";
411 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
412 *OS << PrintReg(LI.reg, TRI);
413 else
414 *OS << PrintRegUnit(LI.reg, TRI);
415 *OS << ' ' << LI << '\n';
416}
417
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000418void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000419 BBInfo &MInfo = MBBInfoMap[MBB];
420 if (!MInfo.reachable) {
421 MInfo.reachable = true;
422 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
423 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
424 markReachable(*SuI);
425 }
426}
427
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000428void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000429 lastIndex = SlotIndex();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000430 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000431
432 // A sub-register of a reserved register is also reserved
433 for (int Reg = regsReserved.find_first(); Reg>=0;
434 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000435 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000436 // FIXME: This should probably be:
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000437 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
438 regsReserved.set(*SubRegs);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000439 }
440 }
Lang Hames03698de2012-02-14 19:17:48 +0000441
442 regsAllocatable = TRI->getAllocatableSet(*MF);
443
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000444 markReachable(&MF->front());
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000445
446 // Build a set of the basic blocks in the function.
447 FunctionBlocks.clear();
448 for (MachineFunction::const_iterator
449 I = MF->begin(), E = MF->end(); I != E; ++I) {
450 FunctionBlocks.insert(I);
451 BBInfo &MInfo = MBBInfoMap[I];
452
453 MInfo.Preds.insert(I->pred_begin(), I->pred_end());
454 if (MInfo.Preds.size() != I->pred_size())
455 report("MBB has duplicate entries in its predecessor list.", I);
456
457 MInfo.Succs.insert(I->succ_begin(), I->succ_end());
458 if (MInfo.Succs.size() != I->succ_size())
459 report("MBB has duplicate entries in its successor list.", I);
460 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000461}
462
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000463// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000464static bool matchPair(MachineBasicBlock::const_succ_iterator i,
465 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000466 if (*i == a)
467 return *++i == b;
468 if (*i == b)
469 return *++i == a;
470 return false;
471}
472
473void
474MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000475 FirstTerminator = 0;
476
Lang Hames03698de2012-02-14 19:17:48 +0000477 if (MRI->isSSA()) {
478 // If this block has allocatable physical registers live-in, check that
479 // it is an entry block or landing pad.
480 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
481 LE = MBB->livein_end();
482 LI != LE; ++LI) {
483 unsigned reg = *LI;
484 if (isAllocatable(reg) && !MBB->isLandingPad() &&
485 MBB != MBB->getParent()->begin()) {
486 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
487 }
488 }
489 }
490
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000491 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000492 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000493 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000494 E = MBB->succ_end(); I != E; ++I) {
495 if ((*I)->isLandingPad())
496 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000497 if (!FunctionBlocks.count(*I))
498 report("MBB has successor that isn't part of the function.", MBB);
499 if (!MBBInfoMap[*I].Preds.count(MBB)) {
500 report("Inconsistent CFG", MBB);
501 *OS << "MBB is not in the predecessor list of the successor BB#"
502 << (*I)->getNumber() << ".\n";
503 }
504 }
505
506 // Check the predecessor list.
507 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
508 E = MBB->pred_end(); I != E; ++I) {
509 if (!FunctionBlocks.count(*I))
510 report("MBB has predecessor that isn't part of the function.", MBB);
511 if (!MBBInfoMap[*I].Succs.count(MBB)) {
512 report("Inconsistent CFG", MBB);
513 *OS << "MBB is not in the successor list of the predecessor BB#"
514 << (*I)->getNumber() << ".\n";
515 }
Cameron Zwarich2100d212010-12-20 04:19:48 +0000516 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000517
518 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
519 const BasicBlock *BB = MBB->getBasicBlock();
520 if (LandingPadSuccs.size() > 1 &&
521 !(AsmInfo &&
522 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
523 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000524 report("MBB has more than one landing pad successor", MBB);
525
Dan Gohman27920592009-08-27 02:43:49 +0000526 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
527 MachineBasicBlock *TBB = 0, *FBB = 0;
528 SmallVector<MachineOperand, 4> Cond;
529 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
530 TBB, FBB, Cond)) {
531 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
532 // check whether its answers match up with reality.
533 if (!TBB && !FBB) {
534 // Block falls through to its successor.
535 MachineFunction::const_iterator MBBI = MBB;
536 ++MBBI;
537 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000538 // It's possible that the block legitimately ends with a noreturn
539 // call or an unreachable, in which case it won't actually fall
540 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000541 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000542 // It's possible that the block legitimately ends with a noreturn
543 // call or an unreachable, in which case it won't actuall fall
544 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000545 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000546 report("MBB exits via unconditional fall-through but doesn't have "
547 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000548 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000549 report("MBB exits via unconditional fall-through but its successor "
550 "differs from its CFG successor!", MBB);
551 }
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000552 if (!MBB->empty() && getBundleStart(&MBB->back())->isBarrier() &&
553 !TII->isPredicated(getBundleStart(&MBB->back()))) {
Dan Gohman27920592009-08-27 02:43:49 +0000554 report("MBB exits via unconditional fall-through but ends with a "
555 "barrier instruction!", MBB);
556 }
557 if (!Cond.empty()) {
558 report("MBB exits via unconditional fall-through but has a condition!",
559 MBB);
560 }
561 } else if (TBB && !FBB && Cond.empty()) {
562 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000563 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000564 report("MBB exits via unconditional branch but doesn't have "
565 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000566 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000567 report("MBB exits via unconditional branch but the CFG "
568 "successor doesn't match the actual successor!", MBB);
569 }
570 if (MBB->empty()) {
571 report("MBB exits via unconditional branch but doesn't contain "
572 "any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000573 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000574 report("MBB exits via unconditional branch but doesn't end with a "
575 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000576 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000577 report("MBB exits via unconditional branch but the branch isn't a "
578 "terminator instruction!", MBB);
579 }
580 } else if (TBB && !FBB && !Cond.empty()) {
581 // Block conditionally branches somewhere, otherwise falls through.
582 MachineFunction::const_iterator MBBI = MBB;
583 ++MBBI;
584 if (MBBI == MF->end()) {
585 report("MBB conditionally falls through out of function!", MBB);
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000586 } if (MBB->succ_size() == 1) {
587 // A conditional branch with only one successor is weird, but allowed.
588 if (&*MBBI != TBB)
589 report("MBB exits via conditional branch/fall-through but only has "
590 "one CFG successor!", MBB);
591 else if (TBB != *MBB->succ_begin())
592 report("MBB exits via conditional branch/fall-through but the CFG "
593 "successor don't match the actual successor!", MBB);
594 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000595 report("MBB exits via conditional branch/fall-through but doesn't have "
596 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000597 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000598 report("MBB exits via conditional branch/fall-through but the CFG "
599 "successors don't match the actual successors!", MBB);
600 }
601 if (MBB->empty()) {
602 report("MBB exits via conditional branch/fall-through but doesn't "
603 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000604 } else if (getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000605 report("MBB exits via conditional branch/fall-through but ends with a "
606 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000607 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000608 report("MBB exits via conditional branch/fall-through but the branch "
609 "isn't a terminator instruction!", MBB);
610 }
611 } else if (TBB && FBB) {
612 // Block conditionally branches somewhere, otherwise branches
613 // somewhere else.
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000614 if (MBB->succ_size() == 1) {
615 // A conditional branch with only one successor is weird, but allowed.
616 if (FBB != TBB)
617 report("MBB exits via conditional branch/branch through but only has "
618 "one CFG successor!", MBB);
619 else if (TBB != *MBB->succ_begin())
620 report("MBB exits via conditional branch/branch through but the CFG "
621 "successor don't match the actual successor!", MBB);
622 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000623 report("MBB exits via conditional branch/branch but doesn't have "
624 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000625 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000626 report("MBB exits via conditional branch/branch but the CFG "
627 "successors don't match the actual successors!", MBB);
628 }
629 if (MBB->empty()) {
630 report("MBB exits via conditional branch/branch but doesn't "
631 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000632 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000633 report("MBB exits via conditional branch/branch but doesn't end with a "
634 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000635 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000636 report("MBB exits via conditional branch/branch but the branch "
637 "isn't a terminator instruction!", MBB);
638 }
639 if (Cond.empty()) {
640 report("MBB exits via conditinal branch/branch but there's no "
641 "condition!", MBB);
642 }
643 } else {
644 report("AnalyzeBranch returned invalid data!", MBB);
645 }
646 }
647
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000648 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000649 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000650 E = MBB->livein_end(); I != E; ++I) {
651 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
652 report("MBB live-in list contains non-physical register", MBB);
653 continue;
654 }
655 regsLive.insert(*I);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000656 for (MCSubRegIterator SubRegs(*I, TRI); SubRegs.isValid(); ++SubRegs)
657 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000658 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000659 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000660
661 const MachineFrameInfo *MFI = MF->getFrameInfo();
662 assert(MFI && "Function has no frame info");
663 BitVector PR = MFI->getPristineRegs(MBB);
664 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
665 regsLive.insert(I);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000666 for (MCSubRegIterator SubRegs(I, TRI); SubRegs.isValid(); ++SubRegs)
667 regsLive.insert(*SubRegs);
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000668 }
669
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000670 regsKilled.clear();
671 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000672
673 if (Indexes)
674 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000675}
676
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000677// This function gets called for all bundle headers, including normal
678// stand-alone unbundled instructions.
679void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
680 if (Indexes && Indexes->hasIndex(MI)) {
681 SlotIndex idx = Indexes->getInstructionIndex(MI);
682 if (!(idx > lastIndex)) {
683 report("Instruction index out of order", MI);
684 *OS << "Last instruction was at " << lastIndex << '\n';
685 }
686 lastIndex = idx;
687 }
Pete Cooper83569cb2012-06-07 17:41:39 +0000688
689 // Ensure non-terminators don't follow terminators.
690 // Ignore predicated terminators formed by if conversion.
691 // FIXME: If conversion shouldn't need to violate this rule.
692 if (MI->isTerminator() && !TII->isPredicated(MI)) {
693 if (!FirstTerminator)
694 FirstTerminator = MI;
695 } else if (FirstTerminator) {
696 report("Non-terminator instruction after the first terminator", MI);
697 *OS << "First terminator was:\t" << *FirstTerminator;
698 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000699}
700
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000701// The operands on an INLINEASM instruction must follow a template.
702// Verify that the flag operands make sense.
703void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
704 // The first two operands on INLINEASM are the asm string and global flags.
705 if (MI->getNumOperands() < 2) {
706 report("Too few operands on inline asm", MI);
707 return;
708 }
709 if (!MI->getOperand(0).isSymbol())
710 report("Asm string must be an external symbol", MI);
711 if (!MI->getOperand(1).isImm())
712 report("Asm flags must be an immediate", MI);
713 // Allowed flags are Extra_HasSideEffects = 1, and Extra_IsAlignStack = 2.
714 if (!isUInt<2>(MI->getOperand(1).getImm()))
715 report("Unknown asm flags", &MI->getOperand(1), 1);
716
717 assert(InlineAsm::MIOp_FirstOperand == 2 && "Asm format changed");
718
719 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
720 unsigned NumOps;
721 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
722 const MachineOperand &MO = MI->getOperand(OpNo);
723 // There may be implicit ops after the fixed operands.
724 if (!MO.isImm())
725 break;
726 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
727 }
728
729 if (OpNo > MI->getNumOperands())
730 report("Missing operands in last group", MI);
731
732 // An optional MDNode follows the groups.
733 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
734 ++OpNo;
735
736 // All trailing operands must be implicit registers.
737 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
738 const MachineOperand &MO = MI->getOperand(OpNo);
739 if (!MO.isReg() || !MO.isImplicit())
740 report("Expected implicit register after groups", &MO, OpNo);
741 }
742}
743
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000744void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000745 const MCInstrDesc &MCID = MI->getDesc();
746 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000747 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000748 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000749 << MI->getNumExplicitOperands() << " given.\n";
750 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000751
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000752 // Check the tied operands.
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000753 if (MI->isInlineAsm())
754 verifyInlineAsm(MI);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000755
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000756 // Check the MachineMemOperands for basic consistency.
757 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
758 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000759 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000760 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000761 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000762 report("Missing mayStore flag", MI);
763 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000764
765 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000766 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000767 if (LiveInts) {
768 bool mapped = !LiveInts->isNotInMIMap(MI);
769 if (MI->isDebugValue()) {
770 if (mapped)
771 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000772 } else if (MI->isInsideBundle()) {
773 if (mapped)
774 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000775 } else {
776 if (!mapped)
777 report("Missing slot index", MI);
778 }
779 }
780
Andrew Trick3be654f2011-09-21 02:20:46 +0000781 StringRef ErrorInfo;
782 if (!TII->verifyInstruction(MI, ErrorInfo))
783 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000784}
785
786void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000787MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000788 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000789 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000790
Evan Chenge837dea2011-06-28 19:10:37 +0000791 // The first MCID.NumDefs operands must be explicit register defines
792 if (MONum < MCID.getNumDefs()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000793 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000794 if (!MO->isReg())
795 report("Explicit definition must be a register", MO, MONum);
Evan Chengcac58aa2012-05-29 19:40:44 +0000796 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000797 report("Explicit definition marked as use", MO, MONum);
798 else if (MO->isImplicit())
799 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000800 } else if (MONum < MCID.getNumOperands()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000801 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopher113a06c2010-11-17 00:55:36 +0000802 // Don't check if it's the last operand in a variadic instruction. See,
803 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000804 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000805 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000806 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000807 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000808 if (MO->isImplicit())
809 report("Explicit operand marked as implicit", MO, MONum);
810 }
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000811
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000812 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
813 if (TiedTo != -1) {
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000814 if (!MO->isReg())
815 report("Tied use must be a register", MO, MONum);
816 else if (!MO->isTied())
817 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000818 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
819 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000820 } else if (MO->isReg() && MO->isTied())
821 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000822 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000823 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000824 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000825 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000826 }
827
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000828 switch (MO->getType()) {
829 case MachineOperand::MO_Register: {
830 const unsigned Reg = MO->getReg();
831 if (!Reg)
832 return;
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000833 if (MRI->tracksLiveness() && !MI->isDebugValue())
834 checkLiveness(MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000835
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000836 // Verify the consistency of tied operands.
837 if (MO->isTied()) {
838 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
839 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
840 if (!OtherMO.isReg())
841 report("Must be tied to a register", MO, MONum);
842 if (!OtherMO.isTied())
843 report("Missing tie flags on tied operand", MO, MONum);
844 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
845 report("Inconsistent tie links", MO, MONum);
846 if (MONum < MCID.getNumDefs()) {
847 if (OtherIdx < MCID.getNumOperands()) {
848 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
849 report("Explicit def tied to explicit use without tie constraint",
850 MO, MONum);
851 } else {
852 if (!OtherMO.isImplicit())
853 report("Explicit def should be tied to implicit use", MO, MONum);
854 }
855 }
856 }
857
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000858 // Verify two-address constraints after leaving SSA form.
859 unsigned DefIdx;
860 if (!MRI->isSSA() && MO->isUse() &&
861 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
862 Reg != MI->getOperand(DefIdx).getReg())
863 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000864
865 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000866 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000867 unsigned SubIdx = MO->getSubReg();
868
869 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000870 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000871 report("Illegal subregister index for physical register", MO, MONum);
872 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000873 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000874 if (const TargetRegisterClass *DRC =
875 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000876 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000877 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000878 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000879 << DRC->getName() << " register.\n";
880 }
881 }
882 } else {
883 // Virtual register.
884 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
885 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000886 const TargetRegisterClass *SRC =
887 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000888 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000889 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000890 *OS << "Register class " << RC->getName()
891 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000892 return;
893 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000894 if (RC != SRC) {
895 report("Invalid register class for subregister index", MO, MONum);
896 *OS << "Register class " << RC->getName()
897 << " does not fully support subreg index " << SubIdx << "\n";
898 return;
899 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000900 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000901 if (const TargetRegisterClass *DRC =
902 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000903 if (SubIdx) {
904 const TargetRegisterClass *SuperRC =
905 TRI->getLargestLegalSuperClass(RC);
906 if (!SuperRC) {
907 report("No largest legal super class exists.", MO, MONum);
908 return;
909 }
910 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
911 if (!DRC) {
912 report("No matching super-reg register class.", MO, MONum);
913 return;
914 }
915 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000916 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000917 report("Illegal virtual register for instruction", MO, MONum);
918 *OS << "Expected a " << DRC->getName() << " register, but got a "
919 << RC->getName() << " register\n";
920 }
921 }
922 }
923 }
924 break;
925 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000926
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000927 case MachineOperand::MO_RegisterMask:
928 regMasks.push_back(MO->getRegMask());
929 break;
930
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000931 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000932 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
933 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000934 break;
935
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000936 case MachineOperand::MO_FrameIndex:
937 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
938 LiveInts && !LiveInts->isNotInMIMap(MI)) {
939 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
940 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000941 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000942 report("Instruction loads from dead spill slot", MO, MONum);
943 *OS << "Live stack: " << LI << '\n';
944 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000945 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000946 report("Instruction stores to dead spill slot", MO, MONum);
947 *OS << "Live stack: " << LI << '\n';
948 }
949 }
950 break;
951
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000952 default:
953 break;
954 }
955}
956
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000957void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
958 const MachineInstr *MI = MO->getParent();
959 const unsigned Reg = MO->getReg();
960
961 // Both use and def operands can read a register.
962 if (MO->readsReg()) {
963 regsLiveInButUnused.erase(Reg);
964
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000965 if (MO->isKill())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000966 addRegWithSubRegs(regsKilled, Reg);
967
968 // Check that LiveVars knows this kill.
969 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
970 MO->isKill()) {
971 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
972 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
973 report("Kill missing from LiveVariables", MO, MONum);
974 }
975
976 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +0000977 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
978 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
979 // Check the cached regunit intervals.
980 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
981 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
982 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(*Units)) {
983 LiveRangeQuery LRQ(*LI, UseIdx);
984 if (!LRQ.valueIn()) {
985 report("No live range at use", MO, MONum);
986 *OS << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
987 << ' ' << *LI << '\n';
988 }
989 if (MO->isKill() && !LRQ.isKill()) {
990 report("Live range continues after kill flag", MO, MONum);
991 *OS << PrintRegUnit(*Units, TRI) << ' ' << *LI << '\n';
992 }
993 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000994 }
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +0000995 }
996
997 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
998 if (LiveInts->hasInterval(Reg)) {
999 // This is a virtual register interval.
1000 const LiveInterval &LI = LiveInts->getInterval(Reg);
1001 LiveRangeQuery LRQ(LI, UseIdx);
1002 if (!LRQ.valueIn()) {
1003 report("No live range at use", MO, MONum);
1004 *OS << UseIdx << " is not live in " << LI << '\n';
1005 }
1006 // Check for extra kill flags.
1007 // Note that we allow missing kill flags for now.
1008 if (MO->isKill() && !LRQ.isKill()) {
1009 report("Live range continues after kill flag", MO, MONum);
1010 *OS << "Live range: " << LI << '\n';
1011 }
1012 } else {
1013 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001014 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001015 }
1016 }
1017
1018 // Use of a dead register.
1019 if (!regsLive.count(Reg)) {
1020 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1021 // Reserved registers may be used even when 'dead'.
1022 if (!isReserved(Reg))
1023 report("Using an undefined physical register", MO, MONum);
Pete Cooperb97c57a2012-07-19 23:40:38 +00001024 } else if (MRI->def_empty(Reg)) {
1025 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001026 } else {
1027 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1028 // We don't know which virtual registers are live in, so only complain
1029 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1030 // must be live in. PHI instructions are handled separately.
1031 if (MInfo.regsKilled.count(Reg))
1032 report("Using a killed virtual register", MO, MONum);
1033 else if (!MI->isPHI())
1034 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1035 }
1036 }
1037 }
1038
1039 if (MO->isDef()) {
1040 // Register defined.
1041 // TODO: verify that earlyclobber ops are not used.
1042 if (MO->isDead())
1043 addRegWithSubRegs(regsDead, Reg);
1044 else
1045 addRegWithSubRegs(regsDefined, Reg);
1046
1047 // Verify SSA form.
1048 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
1049 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
1050 report("Multiple virtual register defs in SSA form", MO, MONum);
1051
1052 // Check LiveInts for a live range, but only for virtual registers.
1053 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1054 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001055 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1056 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001057 if (LiveInts->hasInterval(Reg)) {
1058 const LiveInterval &LI = LiveInts->getInterval(Reg);
1059 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1060 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001061 if (VNI->def != DefIdx) {
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001062 report("Inconsistent valno->def", MO, MONum);
1063 *OS << "Valno " << VNI->id << " is not defined at "
1064 << DefIdx << " in " << LI << '\n';
1065 }
1066 } else {
1067 report("No live range at def", MO, MONum);
1068 *OS << DefIdx << " is not live in " << LI << '\n';
1069 }
1070 } else {
1071 report("Virtual register has no Live interval", MO, MONum);
1072 }
1073 }
1074 }
1075}
1076
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001077void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +00001078}
1079
1080// This function gets called after visiting all instructions in a bundle. The
1081// argument points to the bundle header.
1082// Normal stand-alone instructions are also considered 'bundles', and this
1083// function is called for all of them.
1084void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001085 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1086 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001087 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +00001088 // Kill any masked registers.
1089 while (!regMasks.empty()) {
1090 const uint32_t *Mask = regMasks.pop_back_val();
1091 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1092 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1093 MachineOperand::clobbersPhysReg(Mask, *I))
1094 regsDead.push_back(*I);
1095 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001096 set_subtract(regsLive, regsDead); regsDead.clear();
1097 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001098}
1099
1100void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001101MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001102 MBBInfoMap[MBB].regsLiveOut = regsLive;
1103 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +00001104
1105 if (Indexes) {
1106 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1107 if (!(stop > lastIndex)) {
1108 report("Block ends before last instruction index", MBB);
1109 *OS << "Block ends at " << stop
1110 << " last instruction was at " << lastIndex << '\n';
1111 }
1112 lastIndex = stop;
1113 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001114}
1115
1116// Calculate the largest possible vregsPassed sets. These are the registers that
1117// can pass through an MBB live, but may not be live every time. It is assumed
1118// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001119void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001120 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1121 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001122 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001123 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1124 MFI != MFE; ++MFI) {
1125 const MachineBasicBlock &MBB(*MFI);
1126 BBInfo &MInfo = MBBInfoMap[&MBB];
1127 if (!MInfo.reachable)
1128 continue;
1129 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1130 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1131 BBInfo &SInfo = MBBInfoMap[*SuI];
1132 if (SInfo.addPassed(MInfo.regsLiveOut))
1133 todo.insert(*SuI);
1134 }
1135 }
1136
1137 // Iteratively push vregsPassed to successors. This will converge to the same
1138 // final state regardless of DenseSet iteration order.
1139 while (!todo.empty()) {
1140 const MachineBasicBlock *MBB = *todo.begin();
1141 todo.erase(MBB);
1142 BBInfo &MInfo = MBBInfoMap[MBB];
1143 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1144 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1145 if (*SuI == MBB)
1146 continue;
1147 BBInfo &SInfo = MBBInfoMap[*SuI];
1148 if (SInfo.addPassed(MInfo.vregsPassed))
1149 todo.insert(*SuI);
1150 }
1151 }
1152}
1153
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001154// Calculate the set of virtual registers that must be passed through each basic
1155// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001156// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001157void MachineVerifier::calcRegsRequired() {
1158 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001159 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001160 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1161 MFI != MFE; ++MFI) {
1162 const MachineBasicBlock &MBB(*MFI);
1163 BBInfo &MInfo = MBBInfoMap[&MBB];
1164 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1165 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1166 BBInfo &PInfo = MBBInfoMap[*PrI];
1167 if (PInfo.addRequired(MInfo.vregsLiveIn))
1168 todo.insert(*PrI);
1169 }
1170 }
1171
1172 // Iteratively push vregsRequired to predecessors. This will converge to the
1173 // same final state regardless of DenseSet iteration order.
1174 while (!todo.empty()) {
1175 const MachineBasicBlock *MBB = *todo.begin();
1176 todo.erase(MBB);
1177 BBInfo &MInfo = MBBInfoMap[MBB];
1178 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1179 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1180 if (*PrI == MBB)
1181 continue;
1182 BBInfo &SInfo = MBBInfoMap[*PrI];
1183 if (SInfo.addRequired(MInfo.vregsRequired))
1184 todo.insert(*PrI);
1185 }
1186 }
1187}
1188
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001189// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001190// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001191void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001192 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001193 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +00001194 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001195 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001196
1197 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
1198 unsigned Reg = BBI->getOperand(i).getReg();
1199 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
1200 if (!Pre->isSuccessor(MBB))
1201 continue;
1202 seen.insert(Pre);
1203 BBInfo &PrInfo = MBBInfoMap[Pre];
1204 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1205 report("PHI operand is not live-out from predecessor",
1206 &BBI->getOperand(i), i);
1207 }
1208
1209 // Did we see all predecessors?
1210 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1211 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1212 if (!seen.count(*PrI)) {
1213 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +00001214 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001215 << " is a predecessor according to the CFG.\n";
1216 }
1217 }
1218 }
1219}
1220
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001221void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001222 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001223
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001224 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1225 MFI != MFE; ++MFI) {
1226 BBInfo &MInfo = MBBInfoMap[MFI];
1227
1228 // Skip unreachable MBBs.
1229 if (!MInfo.reachable)
1230 continue;
1231
1232 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001233 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001234
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001235 // Now check liveness info if available
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001236 calcRegsRequired();
1237
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001238 // Check for killed virtual registers that should be live out.
1239 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1240 MFI != MFE; ++MFI) {
1241 BBInfo &MInfo = MBBInfoMap[MFI];
1242 for (RegSet::iterator
1243 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1244 ++I)
1245 if (MInfo.regsKilled.count(*I)) {
Bill Wendling96cb1122012-07-19 00:04:14 +00001246 report("Virtual register killed in block, but needed live out.", MFI);
1247 *OS << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001248 << " is used after the block.\n";
1249 }
1250 }
1251
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001252 if (!MF->empty()) {
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001253 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1254 for (RegSet::iterator
1255 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesenff0275e2012-03-10 00:44:11 +00001256 ++I)
1257 report("Virtual register def doesn't dominate all uses.",
1258 MRI->getVRegDef(*I));
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001259 }
1260
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001261 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001262 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001263 if (LiveInts)
1264 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001265}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001266
1267void MachineVerifier::verifyLiveVariables() {
1268 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001269 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1270 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001271 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1272 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1273 MFI != MFE; ++MFI) {
1274 BBInfo &MInfo = MBBInfoMap[MFI];
1275
1276 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1277 if (MInfo.vregsRequired.count(Reg)) {
1278 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1279 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001280 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001281 << " must be live through the block.\n";
1282 }
1283 } else {
1284 if (VI.AliveBlocks.test(MFI->getNumber())) {
1285 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001286 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001287 << " is not needed live through the block.\n";
1288 }
1289 }
1290 }
1291 }
1292}
1293
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001294void MachineVerifier::verifyLiveIntervals() {
1295 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001296 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1297 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001298
1299 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001300 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001301 continue;
1302
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001303 if (!LiveInts->hasInterval(Reg)) {
1304 report("Missing live interval for virtual register", MF);
1305 *OS << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001306 continue;
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001307 }
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001308
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001309 const LiveInterval &LI = LiveInts->getInterval(Reg);
1310 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001311 verifyLiveInterval(LI);
1312 }
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001313
1314 // Verify all the cached regunit intervals.
1315 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
1316 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(i))
1317 verifyLiveInterval(*LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001318}
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001319
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001320void MachineVerifier::verifyLiveIntervalValue(const LiveInterval &LI,
1321 VNInfo *VNI) {
1322 if (VNI->isUnused())
1323 return;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001324
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001325 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001326
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001327 if (!DefVNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001328 report("Valno not live at def and not marked unused", MF, LI);
1329 *OS << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001330 return;
1331 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001332
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001333 if (DefVNI != VNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001334 report("Live range at def has different valno", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001335 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001336 << " where valno #" << DefVNI->id << " is live\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001337 return;
1338 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001339
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001340 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1341 if (!MBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001342 report("Invalid definition index", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001343 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1344 << " in " << LI << '\n';
1345 return;
1346 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001347
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001348 if (VNI->isPHIDef()) {
1349 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001350 report("PHIDef value is not defined at MBB start", MBB, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001351 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001352 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001353 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001354 return;
1355 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001356
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001357 // Non-PHI def.
1358 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1359 if (!MI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001360 report("No instruction at def index", MBB, LI);
1361 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001362 return;
1363 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001364
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001365 bool hasDef = false;
1366 bool isEarlyClobber = false;
1367 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1368 if (!MOI->isReg() || !MOI->isDef())
1369 continue;
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001370 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001371 if (MOI->getReg() != LI.reg)
1372 continue;
1373 } else {
1374 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001375 !TRI->hasRegUnit(MOI->getReg(), LI.reg))
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001376 continue;
1377 }
1378 hasDef = true;
1379 if (MOI->isEarlyClobber())
1380 isEarlyClobber = true;
1381 }
1382
1383 if (!hasDef) {
1384 report("Defining instruction does not modify register", MI);
1385 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1386 }
1387
1388 // Early clobber defs begin at USE slots, but other defs must begin at
1389 // DEF slots.
1390 if (isEarlyClobber) {
1391 if (!VNI->def.isEarlyClobber()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001392 report("Early clobber def must be at an early-clobber slot", MBB, LI);
1393 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001394 }
1395 } else if (!VNI->def.isRegister()) {
1396 report("Non-PHI, non-early clobber def must be at a register slot",
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001397 MBB, LI);
1398 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001399 }
1400}
1401
1402void
1403MachineVerifier::verifyLiveIntervalSegment(const LiveInterval &LI,
1404 LiveInterval::const_iterator I) {
1405 const VNInfo *VNI = I->valno;
1406 assert(VNI && "Live range has no valno");
1407
1408 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001409 report("Foreign valno in live range", MF, LI);
1410 *OS << *I << " has a bad valno\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001411 }
1412
1413 if (VNI->isUnused()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001414 report("Live range valno is marked unused", MF, LI);
1415 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001416 }
1417
1418 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1419 if (!MBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001420 report("Bad start of live segment, no basic block", MF, LI);
1421 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001422 return;
1423 }
1424 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1425 if (I->start != MBBStartIdx && I->start != VNI->def) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001426 report("Live segment must begin at MBB entry or valno def", MBB, LI);
1427 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001428 }
1429
1430 const MachineBasicBlock *EndMBB =
1431 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1432 if (!EndMBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001433 report("Bad end of live segment, no basic block", MF, LI);
1434 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001435 return;
1436 }
1437
1438 // No more checks for live-out segments.
1439 if (I->end == LiveInts->getMBBEndIdx(EndMBB))
1440 return;
1441
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001442 // RegUnit intervals are allowed dead phis.
1443 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) && VNI->isPHIDef() &&
1444 I->start == VNI->def && I->end == VNI->def.getDeadSlot())
1445 return;
1446
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001447 // The live segment is ending inside EndMBB
1448 const MachineInstr *MI =
1449 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1450 if (!MI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001451 report("Live segment doesn't end at a valid instruction", EndMBB, LI);
1452 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001453 return;
1454 }
1455
1456 // The block slot must refer to a basic block boundary.
1457 if (I->end.isBlock()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001458 report("Live segment ends at B slot of an instruction", EndMBB, LI);
1459 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001460 }
1461
1462 if (I->end.isDead()) {
1463 // Segment ends on the dead slot.
1464 // That means there must be a dead def.
1465 if (!SlotIndex::isSameInstr(I->start, I->end)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001466 report("Live segment ending at dead slot spans instructions", EndMBB, LI);
1467 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001468 }
1469 }
1470
1471 // A live segment can only end at an early-clobber slot if it is being
1472 // redefined by an early-clobber def.
1473 if (I->end.isEarlyClobber()) {
1474 if (I+1 == LI.end() || (I+1)->start != I->end) {
1475 report("Live segment ending at early clobber slot must be "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001476 "redefined by an EC def in the same instruction", EndMBB, LI);
1477 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001478 }
1479 }
1480
1481 // The following checks only apply to virtual registers. Physreg liveness
1482 // is too weird to check.
1483 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1484 // A live range can end with either a redefinition, a kill flag on a
1485 // use, or a dead flag on a def.
1486 bool hasRead = false;
1487 bool hasDeadDef = false;
1488 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1489 if (!MOI->isReg() || MOI->getReg() != LI.reg)
1490 continue;
1491 if (MOI->readsReg())
1492 hasRead = true;
1493 if (MOI->isDef() && MOI->isDead())
1494 hasDeadDef = true;
1495 }
1496
1497 if (I->end.isDead()) {
1498 if (!hasDeadDef) {
1499 report("Instruction doesn't have a dead def operand", MI);
1500 I->print(*OS);
1501 *OS << " in " << LI << '\n';
1502 }
1503 } else {
1504 if (!hasRead) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001505 report("Instruction ending live range doesn't read the register", MI);
1506 *OS << *I << " in " << LI << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001507 }
1508 }
1509 }
1510
1511 // Now check all the basic blocks in this live segment.
1512 MachineFunction::const_iterator MFI = MBB;
1513 // Is this live range the beginning of a non-PHIDef VN?
1514 if (I->start == VNI->def && !VNI->isPHIDef()) {
1515 // Not live-in to any blocks.
1516 if (MBB == EndMBB)
1517 return;
1518 // Skip this block.
1519 ++MFI;
1520 }
1521 for (;;) {
1522 assert(LiveInts->isLiveInToMBB(LI, MFI));
1523 // We don't know how to track physregs into a landing pad.
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001524 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) &&
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001525 MFI->isLandingPad()) {
1526 if (&*MFI == EndMBB)
1527 break;
1528 ++MFI;
1529 continue;
1530 }
1531
1532 // Is VNI a PHI-def in the current block?
1533 bool IsPHI = VNI->isPHIDef() &&
1534 VNI->def == LiveInts->getMBBStartIdx(MFI);
1535
1536 // Check that VNI is live-out of all predecessors.
1537 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1538 PE = MFI->pred_end(); PI != PE; ++PI) {
1539 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1540 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
1541
1542 // All predecessors must have a live-out value.
1543 if (!PVNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001544 report("Register not marked live out of predecessor", *PI, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001545 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1546 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001547 << PEnd << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001548 continue;
1549 }
1550
1551 // Only PHI-defs can take different predecessor values.
1552 if (!IsPHI && PVNI != VNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001553 report("Different value live out of predecessor", *PI, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001554 *OS << "Valno #" << PVNI->id << " live out of BB#"
1555 << (*PI)->getNumber() << '@' << PEnd
1556 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001557 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001558 }
1559 }
1560 if (&*MFI == EndMBB)
1561 break;
1562 ++MFI;
1563 }
1564}
1565
1566void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1567 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1568 I!=E; ++I)
1569 verifyLiveIntervalValue(LI, *I);
1570
1571 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I)
1572 verifyLiveIntervalSegment(LI, I);
1573
1574 // Check the LI only has one connected component.
1575 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1576 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1577 unsigned NumComp = ConEQ.Classify(&LI);
1578 if (NumComp > 1) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001579 report("Multiple connected components in live interval", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001580 for (unsigned comp = 0; comp != NumComp; ++comp) {
1581 *OS << comp << ": valnos";
1582 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1583 E = LI.vni_end(); I!=E; ++I)
1584 if (comp == ConEQ.getEqClass(*I))
1585 *OS << ' ' << (*I)->id;
1586 *OS << '\n';
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001587 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001588 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001589 }
1590}