blob: dca68da2f3e6fff6ef66d9d1e87c5ddfd75dd3a1 [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;
83 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000084 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000085 RegMaskVector regMasks;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000086 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000087
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000088 SlotIndex lastIndex;
89
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000090 // Add Reg and any sub-registers to RV
91 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
92 RV.push_back(Reg);
93 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +000094 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
95 RV.push_back(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000096 }
97
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000098 struct BBInfo {
99 // Is this MBB reachable from the MF entry point?
100 bool reachable;
101
102 // Vregs that must be live in because they are used without being
103 // defined. Map value is the user.
104 RegMap vregsLiveIn;
105
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000106 // Regs killed in MBB. They may be defined again, and will then be in both
107 // regsKilled and regsLiveOut.
108 RegSet regsKilled;
109
110 // Regs defined in MBB and live out. Note that vregs passing through may
111 // be live out without being mentioned here.
112 RegSet regsLiveOut;
113
114 // Vregs that pass through MBB untouched. This set is disjoint from
115 // regsKilled and regsLiveOut.
116 RegSet vregsPassed;
117
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000118 // Vregs that must pass through MBB because they are needed by a successor
119 // block. This set is disjoint from regsLiveOut.
120 RegSet vregsRequired;
121
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000122 // Set versions of block's predecessor and successor lists.
123 BlockSet Preds, Succs;
124
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000125 BBInfo() : reachable(false) {}
126
127 // Add register to vregsPassed if it belongs there. Return true if
128 // anything changed.
129 bool addPassed(unsigned Reg) {
130 if (!TargetRegisterInfo::isVirtualRegister(Reg))
131 return false;
132 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
133 return false;
134 return vregsPassed.insert(Reg).second;
135 }
136
137 // Same for a full set.
138 bool addPassed(const RegSet &RS) {
139 bool changed = false;
140 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
141 if (addPassed(*I))
142 changed = true;
143 return changed;
144 }
145
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000146 // Add register to vregsRequired if it belongs there. Return true if
147 // anything changed.
148 bool addRequired(unsigned Reg) {
149 if (!TargetRegisterInfo::isVirtualRegister(Reg))
150 return false;
151 if (regsLiveOut.count(Reg))
152 return false;
153 return vregsRequired.insert(Reg).second;
154 }
155
156 // Same for a full set.
157 bool addRequired(const RegSet &RS) {
158 bool changed = false;
159 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
160 if (addRequired(*I))
161 changed = true;
162 return changed;
163 }
164
165 // Same for a full map.
166 bool addRequired(const RegMap &RM) {
167 bool changed = false;
168 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
169 if (addRequired(I->first))
170 changed = true;
171 return changed;
172 }
173
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000174 // Live-out registers are either in regsLiveOut or vregsPassed.
175 bool isLiveOut(unsigned Reg) const {
176 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
177 }
178 };
179
180 // Extra register info per MBB.
181 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
182
183 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000184 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000185 }
186
Lang Hames03698de2012-02-14 19:17:48 +0000187 bool isAllocatable(unsigned Reg) {
Jakob Stoklund Olesenfeab72c2012-10-16 00:05:06 +0000188 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
Lang Hames03698de2012-02-14 19:17:48 +0000189 }
190
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000191 // Analysis information if available
192 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000193 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000194 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000195 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000196
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000197 void visitMachineFunctionBefore();
198 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000199 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000200 void visitMachineInstrBefore(const MachineInstr *MI);
201 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
202 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000203 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000204 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
205 void visitMachineFunctionAfter();
206
207 void report(const char *msg, const MachineFunction *MF);
208 void report(const char *msg, const MachineBasicBlock *MBB);
209 void report(const char *msg, const MachineInstr *MI);
210 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000211 void report(const char *msg, const MachineFunction *MF,
212 const LiveInterval &LI);
213 void report(const char *msg, const MachineBasicBlock *MBB,
214 const LiveInterval &LI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000215
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000216 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000217
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000218 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000219 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000220 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000221 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000222
223 void calcRegsRequired();
224 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000225 void verifyLiveIntervals();
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +0000226 void verifyLiveInterval(const LiveInterval&);
227 void verifyLiveIntervalValue(const LiveInterval&, VNInfo*);
228 void verifyLiveIntervalSegment(const LiveInterval&,
229 LiveInterval::const_iterator);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000230 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000231
232 struct MachineVerifierPass : public MachineFunctionPass {
233 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000234 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000235
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000236 MachineVerifierPass(const char *b = 0)
237 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000238 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
239 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000240
241 void getAnalysisUsage(AnalysisUsage &AU) const {
242 AU.setPreservesAll();
243 MachineFunctionPass::getAnalysisUsage(AU);
244 }
245
246 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000247 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000248 return false;
249 }
250 };
251
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000252}
253
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000254char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000255INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000256 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000257
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000258FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
259 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000260}
261
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000262void MachineFunction::verify(Pass *p, const char *Banner) const {
263 MachineVerifier(p, Banner)
264 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000265}
266
Chris Lattner17e9edc2009-08-23 02:51:22 +0000267bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
268 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000269 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000270 std::string ErrorInfo;
271 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
272 raw_fd_ostream::F_Append);
273 if (!ErrorInfo.empty()) {
274 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
275 exit(1);
276 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000277
Chris Lattner17e9edc2009-08-23 02:51:22 +0000278 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000279 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000280 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000281 }
282
283 foundErrors = 0;
284
285 this->MF = &MF;
286 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000287 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000288 TRI = TM->getRegisterInfo();
289 MRI = &MF.getRegInfo();
290
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000291 LiveVars = NULL;
292 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000293 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000294 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000295 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000296 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000297 // We don't want to verify LiveVariables if LiveIntervals is available.
298 if (!LiveInts)
299 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000300 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000301 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000302 }
303
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000304 visitMachineFunctionBefore();
305 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
306 MFI!=MFE; ++MFI) {
307 visitMachineBasicBlockBefore(MFI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000308 // Keep track of the current bundle header.
309 const MachineInstr *CurBundle = 0;
Evan Chengddfd1372011-12-14 02:11:42 +0000310 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
311 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000312 if (MBBI->getParent() != MFI) {
313 report("Bad instruction parent pointer", MFI);
314 *OS << "Instruction: " << *MBBI;
315 continue;
316 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000317 // Is this a bundle header?
318 if (!MBBI->isInsideBundle()) {
319 if (CurBundle)
320 visitMachineBundleAfter(CurBundle);
321 CurBundle = MBBI;
322 visitMachineBundleBefore(CurBundle);
323 } else if (!CurBundle)
324 report("No bundle header", MBBI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000325 visitMachineInstrBefore(MBBI);
326 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
327 visitMachineOperand(&MBBI->getOperand(I), I);
328 visitMachineInstrAfter(MBBI);
329 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000330 if (CurBundle)
331 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000332 visitMachineBasicBlockAfter(MFI);
333 }
334 visitMachineFunctionAfter();
335
Chris Lattner17e9edc2009-08-23 02:51:22 +0000336 if (OutFile)
337 delete OutFile;
338 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000339 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000340
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000341 // Clean up.
342 regsLive.clear();
343 regsDefined.clear();
344 regsDead.clear();
345 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000346 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000347 regsLiveInButUnused.clear();
348 MBBInfoMap.clear();
349
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000350 return false; // no changes
351}
352
Chris Lattner372fefe2009-08-23 01:03:30 +0000353void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000354 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000355 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000356 if (!foundErrors++) {
357 if (Banner)
358 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000359 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000360 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000361 *OS << "*** Bad machine code: " << msg << " ***\n"
Craig Topper96601ca2012-08-22 06:07:19 +0000362 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000363}
364
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000365void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000366 assert(MBB);
367 report(msg, MBB->getParent());
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000368 *OS << "- basic block: BB#" << MBB->getNumber()
369 << ' ' << MBB->getName()
Roman Divacky59324292012-09-05 22:26:57 +0000370 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000371 if (Indexes)
372 *OS << " [" << Indexes->getMBBStartIdx(MBB)
373 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
374 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000375}
376
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000377void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000378 assert(MI);
379 report(msg, MI->getParent());
380 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000381 if (Indexes && Indexes->hasIndex(MI))
382 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000383 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000384}
385
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000386void MachineVerifier::report(const char *msg,
387 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000388 assert(MO);
389 report(msg, MO->getParent());
390 *OS << "- operand " << MONum << ": ";
391 MO->print(*OS, TM);
392 *OS << "\n";
393}
394
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000395void MachineVerifier::report(const char *msg, const MachineFunction *MF,
396 const LiveInterval &LI) {
397 report(msg, MF);
398 *OS << "- interval: ";
399 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
400 *OS << PrintReg(LI.reg, TRI);
401 else
402 *OS << PrintRegUnit(LI.reg, TRI);
403 *OS << ' ' << LI << '\n';
404}
405
406void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
407 const LiveInterval &LI) {
408 report(msg, MBB);
409 *OS << "- interval: ";
410 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
411 *OS << PrintReg(LI.reg, TRI);
412 else
413 *OS << PrintRegUnit(LI.reg, TRI);
414 *OS << ' ' << LI << '\n';
415}
416
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000417void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000418 BBInfo &MInfo = MBBInfoMap[MBB];
419 if (!MInfo.reachable) {
420 MInfo.reachable = true;
421 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
422 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
423 markReachable(*SuI);
424 }
425}
426
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000427void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000428 lastIndex = SlotIndex();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000429 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000430
431 // A sub-register of a reserved register is also reserved
432 for (int Reg = regsReserved.find_first(); Reg>=0;
433 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000434 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000435 // FIXME: This should probably be:
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000436 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
437 regsReserved.set(*SubRegs);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000438 }
439 }
Lang Hames03698de2012-02-14 19:17:48 +0000440
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000441 markReachable(&MF->front());
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000442
443 // Build a set of the basic blocks in the function.
444 FunctionBlocks.clear();
445 for (MachineFunction::const_iterator
446 I = MF->begin(), E = MF->end(); I != E; ++I) {
447 FunctionBlocks.insert(I);
448 BBInfo &MInfo = MBBInfoMap[I];
449
450 MInfo.Preds.insert(I->pred_begin(), I->pred_end());
451 if (MInfo.Preds.size() != I->pred_size())
452 report("MBB has duplicate entries in its predecessor list.", I);
453
454 MInfo.Succs.insert(I->succ_begin(), I->succ_end());
455 if (MInfo.Succs.size() != I->succ_size())
456 report("MBB has duplicate entries in its successor list.", I);
457 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000458}
459
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000460// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000461static bool matchPair(MachineBasicBlock::const_succ_iterator i,
462 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000463 if (*i == a)
464 return *++i == b;
465 if (*i == b)
466 return *++i == a;
467 return false;
468}
469
470void
471MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000472 FirstTerminator = 0;
473
Lang Hames03698de2012-02-14 19:17:48 +0000474 if (MRI->isSSA()) {
475 // If this block has allocatable physical registers live-in, check that
476 // it is an entry block or landing pad.
477 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
478 LE = MBB->livein_end();
479 LI != LE; ++LI) {
480 unsigned reg = *LI;
481 if (isAllocatable(reg) && !MBB->isLandingPad() &&
482 MBB != MBB->getParent()->begin()) {
483 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
484 }
485 }
486 }
487
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000488 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000489 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000490 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000491 E = MBB->succ_end(); I != E; ++I) {
492 if ((*I)->isLandingPad())
493 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000494 if (!FunctionBlocks.count(*I))
495 report("MBB has successor that isn't part of the function.", MBB);
496 if (!MBBInfoMap[*I].Preds.count(MBB)) {
497 report("Inconsistent CFG", MBB);
498 *OS << "MBB is not in the predecessor list of the successor BB#"
499 << (*I)->getNumber() << ".\n";
500 }
501 }
502
503 // Check the predecessor list.
504 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
505 E = MBB->pred_end(); I != E; ++I) {
506 if (!FunctionBlocks.count(*I))
507 report("MBB has predecessor that isn't part of the function.", MBB);
508 if (!MBBInfoMap[*I].Succs.count(MBB)) {
509 report("Inconsistent CFG", MBB);
510 *OS << "MBB is not in the successor list of the predecessor BB#"
511 << (*I)->getNumber() << ".\n";
512 }
Cameron Zwarich2100d212010-12-20 04:19:48 +0000513 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000514
515 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
516 const BasicBlock *BB = MBB->getBasicBlock();
517 if (LandingPadSuccs.size() > 1 &&
518 !(AsmInfo &&
519 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
520 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000521 report("MBB has more than one landing pad successor", MBB);
522
Dan Gohman27920592009-08-27 02:43:49 +0000523 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
524 MachineBasicBlock *TBB = 0, *FBB = 0;
525 SmallVector<MachineOperand, 4> Cond;
526 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
527 TBB, FBB, Cond)) {
528 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
529 // check whether its answers match up with reality.
530 if (!TBB && !FBB) {
531 // Block falls through to its successor.
532 MachineFunction::const_iterator MBBI = MBB;
533 ++MBBI;
534 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000535 // It's possible that the block legitimately ends with a noreturn
536 // call or an unreachable, in which case it won't actually fall
537 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000538 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000539 // It's possible that the block legitimately ends with a noreturn
540 // call or an unreachable, in which case it won't actuall fall
541 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000542 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000543 report("MBB exits via unconditional fall-through but doesn't have "
544 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000545 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000546 report("MBB exits via unconditional fall-through but its successor "
547 "differs from its CFG successor!", MBB);
548 }
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000549 if (!MBB->empty() && getBundleStart(&MBB->back())->isBarrier() &&
550 !TII->isPredicated(getBundleStart(&MBB->back()))) {
Dan Gohman27920592009-08-27 02:43:49 +0000551 report("MBB exits via unconditional fall-through but ends with a "
552 "barrier instruction!", MBB);
553 }
554 if (!Cond.empty()) {
555 report("MBB exits via unconditional fall-through but has a condition!",
556 MBB);
557 }
558 } else if (TBB && !FBB && Cond.empty()) {
559 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000560 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000561 report("MBB exits via unconditional branch but doesn't have "
562 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000563 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000564 report("MBB exits via unconditional branch but the CFG "
565 "successor doesn't match the actual successor!", MBB);
566 }
567 if (MBB->empty()) {
568 report("MBB exits via unconditional branch but doesn't contain "
569 "any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000570 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000571 report("MBB exits via unconditional branch but doesn't end with a "
572 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000573 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000574 report("MBB exits via unconditional branch but the branch isn't a "
575 "terminator instruction!", MBB);
576 }
577 } else if (TBB && !FBB && !Cond.empty()) {
578 // Block conditionally branches somewhere, otherwise falls through.
579 MachineFunction::const_iterator MBBI = MBB;
580 ++MBBI;
581 if (MBBI == MF->end()) {
582 report("MBB conditionally falls through out of function!", MBB);
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000583 } if (MBB->succ_size() == 1) {
584 // A conditional branch with only one successor is weird, but allowed.
585 if (&*MBBI != TBB)
586 report("MBB exits via conditional branch/fall-through but only has "
587 "one CFG successor!", MBB);
588 else if (TBB != *MBB->succ_begin())
589 report("MBB exits via conditional branch/fall-through but the CFG "
590 "successor don't match the actual successor!", MBB);
591 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000592 report("MBB exits via conditional branch/fall-through but doesn't have "
593 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000594 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000595 report("MBB exits via conditional branch/fall-through but the CFG "
596 "successors don't match the actual successors!", MBB);
597 }
598 if (MBB->empty()) {
599 report("MBB exits via conditional branch/fall-through but doesn't "
600 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000601 } else if (getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000602 report("MBB exits via conditional branch/fall-through but ends with a "
603 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000604 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000605 report("MBB exits via conditional branch/fall-through but the branch "
606 "isn't a terminator instruction!", MBB);
607 }
608 } else if (TBB && FBB) {
609 // Block conditionally branches somewhere, otherwise branches
610 // somewhere else.
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000611 if (MBB->succ_size() == 1) {
612 // A conditional branch with only one successor is weird, but allowed.
613 if (FBB != TBB)
614 report("MBB exits via conditional branch/branch through but only has "
615 "one CFG successor!", MBB);
616 else if (TBB != *MBB->succ_begin())
617 report("MBB exits via conditional branch/branch through but the CFG "
618 "successor don't match the actual successor!", MBB);
619 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000620 report("MBB exits via conditional branch/branch but doesn't have "
621 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000622 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000623 report("MBB exits via conditional branch/branch but the CFG "
624 "successors don't match the actual successors!", MBB);
625 }
626 if (MBB->empty()) {
627 report("MBB exits via conditional branch/branch but doesn't "
628 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000629 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000630 report("MBB exits via conditional branch/branch but doesn't end with a "
631 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000632 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000633 report("MBB exits via conditional branch/branch but the branch "
634 "isn't a terminator instruction!", MBB);
635 }
636 if (Cond.empty()) {
637 report("MBB exits via conditinal branch/branch but there's no "
638 "condition!", MBB);
639 }
640 } else {
641 report("AnalyzeBranch returned invalid data!", MBB);
642 }
643 }
644
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000645 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000646 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000647 E = MBB->livein_end(); I != E; ++I) {
648 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
649 report("MBB live-in list contains non-physical register", MBB);
650 continue;
651 }
652 regsLive.insert(*I);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000653 for (MCSubRegIterator SubRegs(*I, TRI); SubRegs.isValid(); ++SubRegs)
654 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000655 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000656 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000657
658 const MachineFrameInfo *MFI = MF->getFrameInfo();
659 assert(MFI && "Function has no frame info");
660 BitVector PR = MFI->getPristineRegs(MBB);
661 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
662 regsLive.insert(I);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000663 for (MCSubRegIterator SubRegs(I, TRI); SubRegs.isValid(); ++SubRegs)
664 regsLive.insert(*SubRegs);
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000665 }
666
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000667 regsKilled.clear();
668 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000669
670 if (Indexes)
671 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000672}
673
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000674// This function gets called for all bundle headers, including normal
675// stand-alone unbundled instructions.
676void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
677 if (Indexes && Indexes->hasIndex(MI)) {
678 SlotIndex idx = Indexes->getInstructionIndex(MI);
679 if (!(idx > lastIndex)) {
680 report("Instruction index out of order", MI);
681 *OS << "Last instruction was at " << lastIndex << '\n';
682 }
683 lastIndex = idx;
684 }
Pete Cooper83569cb2012-06-07 17:41:39 +0000685
686 // Ensure non-terminators don't follow terminators.
687 // Ignore predicated terminators formed by if conversion.
688 // FIXME: If conversion shouldn't need to violate this rule.
689 if (MI->isTerminator() && !TII->isPredicated(MI)) {
690 if (!FirstTerminator)
691 FirstTerminator = MI;
692 } else if (FirstTerminator) {
693 report("Non-terminator instruction after the first terminator", MI);
694 *OS << "First terminator was:\t" << *FirstTerminator;
695 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000696}
697
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000698// The operands on an INLINEASM instruction must follow a template.
699// Verify that the flag operands make sense.
700void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
701 // The first two operands on INLINEASM are the asm string and global flags.
702 if (MI->getNumOperands() < 2) {
703 report("Too few operands on inline asm", MI);
704 return;
705 }
706 if (!MI->getOperand(0).isSymbol())
707 report("Asm string must be an external symbol", MI);
708 if (!MI->getOperand(1).isImm())
709 report("Asm flags must be an immediate", MI);
710 // Allowed flags are Extra_HasSideEffects = 1, and Extra_IsAlignStack = 2.
711 if (!isUInt<2>(MI->getOperand(1).getImm()))
712 report("Unknown asm flags", &MI->getOperand(1), 1);
713
714 assert(InlineAsm::MIOp_FirstOperand == 2 && "Asm format changed");
715
716 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
717 unsigned NumOps;
718 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
719 const MachineOperand &MO = MI->getOperand(OpNo);
720 // There may be implicit ops after the fixed operands.
721 if (!MO.isImm())
722 break;
723 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
724 }
725
726 if (OpNo > MI->getNumOperands())
727 report("Missing operands in last group", MI);
728
729 // An optional MDNode follows the groups.
730 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
731 ++OpNo;
732
733 // All trailing operands must be implicit registers.
734 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
735 const MachineOperand &MO = MI->getOperand(OpNo);
736 if (!MO.isReg() || !MO.isImplicit())
737 report("Expected implicit register after groups", &MO, OpNo);
738 }
739}
740
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000741void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000742 const MCInstrDesc &MCID = MI->getDesc();
743 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000744 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000745 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000746 << MI->getNumExplicitOperands() << " given.\n";
747 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000748
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000749 // Check the tied operands.
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000750 if (MI->isInlineAsm())
751 verifyInlineAsm(MI);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000752
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000753 // Check the MachineMemOperands for basic consistency.
754 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
755 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000756 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000757 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000758 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000759 report("Missing mayStore flag", MI);
760 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000761
762 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000763 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000764 if (LiveInts) {
765 bool mapped = !LiveInts->isNotInMIMap(MI);
766 if (MI->isDebugValue()) {
767 if (mapped)
768 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000769 } else if (MI->isInsideBundle()) {
770 if (mapped)
771 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000772 } else {
773 if (!mapped)
774 report("Missing slot index", MI);
775 }
776 }
777
Andrew Trick3be654f2011-09-21 02:20:46 +0000778 StringRef ErrorInfo;
779 if (!TII->verifyInstruction(MI, ErrorInfo))
780 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000781}
782
783void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000784MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000785 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000786 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000787
Evan Chenge837dea2011-06-28 19:10:37 +0000788 // The first MCID.NumDefs operands must be explicit register defines
789 if (MONum < MCID.getNumDefs()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000790 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000791 if (!MO->isReg())
792 report("Explicit definition must be a register", MO, MONum);
Evan Chengcac58aa2012-05-29 19:40:44 +0000793 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000794 report("Explicit definition marked as use", MO, MONum);
795 else if (MO->isImplicit())
796 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000797 } else if (MONum < MCID.getNumOperands()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000798 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopher113a06c2010-11-17 00:55:36 +0000799 // Don't check if it's the last operand in a variadic instruction. See,
800 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000801 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000802 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000803 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000804 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000805 if (MO->isImplicit())
806 report("Explicit operand marked as implicit", MO, MONum);
807 }
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000808
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000809 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
810 if (TiedTo != -1) {
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000811 if (!MO->isReg())
812 report("Tied use must be a register", MO, MONum);
813 else if (!MO->isTied())
814 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000815 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
816 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000817 } else if (MO->isReg() && MO->isTied())
818 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000819 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000820 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000821 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000822 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000823 }
824
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000825 switch (MO->getType()) {
826 case MachineOperand::MO_Register: {
827 const unsigned Reg = MO->getReg();
828 if (!Reg)
829 return;
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000830 if (MRI->tracksLiveness() && !MI->isDebugValue())
831 checkLiveness(MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000832
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000833 // Verify the consistency of tied operands.
834 if (MO->isTied()) {
835 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
836 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
837 if (!OtherMO.isReg())
838 report("Must be tied to a register", MO, MONum);
839 if (!OtherMO.isTied())
840 report("Missing tie flags on tied operand", MO, MONum);
841 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
842 report("Inconsistent tie links", MO, MONum);
843 if (MONum < MCID.getNumDefs()) {
844 if (OtherIdx < MCID.getNumOperands()) {
845 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
846 report("Explicit def tied to explicit use without tie constraint",
847 MO, MONum);
848 } else {
849 if (!OtherMO.isImplicit())
850 report("Explicit def should be tied to implicit use", MO, MONum);
851 }
852 }
853 }
854
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000855 // Verify two-address constraints after leaving SSA form.
856 unsigned DefIdx;
857 if (!MRI->isSSA() && MO->isUse() &&
858 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
859 Reg != MI->getOperand(DefIdx).getReg())
860 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000861
862 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000863 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000864 unsigned SubIdx = MO->getSubReg();
865
866 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000867 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000868 report("Illegal subregister index for physical register", MO, MONum);
869 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000870 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000871 if (const TargetRegisterClass *DRC =
872 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000873 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000874 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000875 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000876 << DRC->getName() << " register.\n";
877 }
878 }
879 } else {
880 // Virtual register.
881 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
882 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000883 const TargetRegisterClass *SRC =
884 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000885 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000886 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000887 *OS << "Register class " << RC->getName()
888 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000889 return;
890 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000891 if (RC != SRC) {
892 report("Invalid register class for subregister index", MO, MONum);
893 *OS << "Register class " << RC->getName()
894 << " does not fully support subreg index " << SubIdx << "\n";
895 return;
896 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000897 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000898 if (const TargetRegisterClass *DRC =
899 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000900 if (SubIdx) {
901 const TargetRegisterClass *SuperRC =
902 TRI->getLargestLegalSuperClass(RC);
903 if (!SuperRC) {
904 report("No largest legal super class exists.", MO, MONum);
905 return;
906 }
907 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
908 if (!DRC) {
909 report("No matching super-reg register class.", MO, MONum);
910 return;
911 }
912 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000913 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000914 report("Illegal virtual register for instruction", MO, MONum);
915 *OS << "Expected a " << DRC->getName() << " register, but got a "
916 << RC->getName() << " register\n";
917 }
918 }
919 }
920 }
921 break;
922 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000923
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000924 case MachineOperand::MO_RegisterMask:
925 regMasks.push_back(MO->getRegMask());
926 break;
927
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000928 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000929 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
930 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000931 break;
932
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000933 case MachineOperand::MO_FrameIndex:
934 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
935 LiveInts && !LiveInts->isNotInMIMap(MI)) {
936 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
937 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000938 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000939 report("Instruction loads from dead spill slot", MO, MONum);
940 *OS << "Live stack: " << LI << '\n';
941 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000942 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000943 report("Instruction stores to dead spill slot", MO, MONum);
944 *OS << "Live stack: " << LI << '\n';
945 }
946 }
947 break;
948
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000949 default:
950 break;
951 }
952}
953
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000954void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
955 const MachineInstr *MI = MO->getParent();
956 const unsigned Reg = MO->getReg();
957
958 // Both use and def operands can read a register.
959 if (MO->readsReg()) {
960 regsLiveInButUnused.erase(Reg);
961
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000962 if (MO->isKill())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000963 addRegWithSubRegs(regsKilled, Reg);
964
965 // Check that LiveVars knows this kill.
966 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
967 MO->isKill()) {
968 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
969 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
970 report("Kill missing from LiveVariables", MO, MONum);
971 }
972
973 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +0000974 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
975 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
976 // Check the cached regunit intervals.
977 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
978 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
979 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(*Units)) {
980 LiveRangeQuery LRQ(*LI, UseIdx);
981 if (!LRQ.valueIn()) {
982 report("No live range at use", MO, MONum);
983 *OS << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
984 << ' ' << *LI << '\n';
985 }
986 if (MO->isKill() && !LRQ.isKill()) {
987 report("Live range continues after kill flag", MO, MONum);
988 *OS << PrintRegUnit(*Units, TRI) << ' ' << *LI << '\n';
989 }
990 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000991 }
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +0000992 }
993
994 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
995 if (LiveInts->hasInterval(Reg)) {
996 // This is a virtual register interval.
997 const LiveInterval &LI = LiveInts->getInterval(Reg);
998 LiveRangeQuery LRQ(LI, UseIdx);
999 if (!LRQ.valueIn()) {
1000 report("No live range at use", MO, MONum);
1001 *OS << UseIdx << " is not live in " << LI << '\n';
1002 }
1003 // Check for extra kill flags.
1004 // Note that we allow missing kill flags for now.
1005 if (MO->isKill() && !LRQ.isKill()) {
1006 report("Live range continues after kill flag", MO, MONum);
1007 *OS << "Live range: " << LI << '\n';
1008 }
1009 } else {
1010 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001011 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001012 }
1013 }
1014
1015 // Use of a dead register.
1016 if (!regsLive.count(Reg)) {
1017 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1018 // Reserved registers may be used even when 'dead'.
1019 if (!isReserved(Reg))
1020 report("Using an undefined physical register", MO, MONum);
Pete Cooperb97c57a2012-07-19 23:40:38 +00001021 } else if (MRI->def_empty(Reg)) {
1022 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001023 } else {
1024 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1025 // We don't know which virtual registers are live in, so only complain
1026 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1027 // must be live in. PHI instructions are handled separately.
1028 if (MInfo.regsKilled.count(Reg))
1029 report("Using a killed virtual register", MO, MONum);
1030 else if (!MI->isPHI())
1031 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1032 }
1033 }
1034 }
1035
1036 if (MO->isDef()) {
1037 // Register defined.
1038 // TODO: verify that earlyclobber ops are not used.
1039 if (MO->isDead())
1040 addRegWithSubRegs(regsDead, Reg);
1041 else
1042 addRegWithSubRegs(regsDefined, Reg);
1043
1044 // Verify SSA form.
1045 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
1046 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
1047 report("Multiple virtual register defs in SSA form", MO, MONum);
1048
1049 // Check LiveInts for a live range, but only for virtual registers.
1050 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1051 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001052 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1053 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001054 if (LiveInts->hasInterval(Reg)) {
1055 const LiveInterval &LI = LiveInts->getInterval(Reg);
1056 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1057 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001058 if (VNI->def != DefIdx) {
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001059 report("Inconsistent valno->def", MO, MONum);
1060 *OS << "Valno " << VNI->id << " is not defined at "
1061 << DefIdx << " in " << LI << '\n';
1062 }
1063 } else {
1064 report("No live range at def", MO, MONum);
1065 *OS << DefIdx << " is not live in " << LI << '\n';
1066 }
1067 } else {
1068 report("Virtual register has no Live interval", MO, MONum);
1069 }
1070 }
1071 }
1072}
1073
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001074void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +00001075}
1076
1077// This function gets called after visiting all instructions in a bundle. The
1078// argument points to the bundle header.
1079// Normal stand-alone instructions are also considered 'bundles', and this
1080// function is called for all of them.
1081void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001082 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1083 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001084 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +00001085 // Kill any masked registers.
1086 while (!regMasks.empty()) {
1087 const uint32_t *Mask = regMasks.pop_back_val();
1088 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1089 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1090 MachineOperand::clobbersPhysReg(Mask, *I))
1091 regsDead.push_back(*I);
1092 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001093 set_subtract(regsLive, regsDead); regsDead.clear();
1094 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001095}
1096
1097void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001098MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001099 MBBInfoMap[MBB].regsLiveOut = regsLive;
1100 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +00001101
1102 if (Indexes) {
1103 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1104 if (!(stop > lastIndex)) {
1105 report("Block ends before last instruction index", MBB);
1106 *OS << "Block ends at " << stop
1107 << " last instruction was at " << lastIndex << '\n';
1108 }
1109 lastIndex = stop;
1110 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001111}
1112
1113// Calculate the largest possible vregsPassed sets. These are the registers that
1114// can pass through an MBB live, but may not be live every time. It is assumed
1115// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001116void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001117 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1118 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001119 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001120 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1121 MFI != MFE; ++MFI) {
1122 const MachineBasicBlock &MBB(*MFI);
1123 BBInfo &MInfo = MBBInfoMap[&MBB];
1124 if (!MInfo.reachable)
1125 continue;
1126 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1127 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1128 BBInfo &SInfo = MBBInfoMap[*SuI];
1129 if (SInfo.addPassed(MInfo.regsLiveOut))
1130 todo.insert(*SuI);
1131 }
1132 }
1133
1134 // Iteratively push vregsPassed to successors. This will converge to the same
1135 // final state regardless of DenseSet iteration order.
1136 while (!todo.empty()) {
1137 const MachineBasicBlock *MBB = *todo.begin();
1138 todo.erase(MBB);
1139 BBInfo &MInfo = MBBInfoMap[MBB];
1140 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1141 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1142 if (*SuI == MBB)
1143 continue;
1144 BBInfo &SInfo = MBBInfoMap[*SuI];
1145 if (SInfo.addPassed(MInfo.vregsPassed))
1146 todo.insert(*SuI);
1147 }
1148 }
1149}
1150
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001151// Calculate the set of virtual registers that must be passed through each basic
1152// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001153// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001154void MachineVerifier::calcRegsRequired() {
1155 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001156 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001157 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1158 MFI != MFE; ++MFI) {
1159 const MachineBasicBlock &MBB(*MFI);
1160 BBInfo &MInfo = MBBInfoMap[&MBB];
1161 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1162 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1163 BBInfo &PInfo = MBBInfoMap[*PrI];
1164 if (PInfo.addRequired(MInfo.vregsLiveIn))
1165 todo.insert(*PrI);
1166 }
1167 }
1168
1169 // Iteratively push vregsRequired to predecessors. This will converge to the
1170 // same final state regardless of DenseSet iteration order.
1171 while (!todo.empty()) {
1172 const MachineBasicBlock *MBB = *todo.begin();
1173 todo.erase(MBB);
1174 BBInfo &MInfo = MBBInfoMap[MBB];
1175 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1176 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1177 if (*PrI == MBB)
1178 continue;
1179 BBInfo &SInfo = MBBInfoMap[*PrI];
1180 if (SInfo.addRequired(MInfo.vregsRequired))
1181 todo.insert(*PrI);
1182 }
1183 }
1184}
1185
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001186// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001187// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001188void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001189 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001190 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +00001191 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001192 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001193
1194 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
1195 unsigned Reg = BBI->getOperand(i).getReg();
1196 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
1197 if (!Pre->isSuccessor(MBB))
1198 continue;
1199 seen.insert(Pre);
1200 BBInfo &PrInfo = MBBInfoMap[Pre];
1201 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1202 report("PHI operand is not live-out from predecessor",
1203 &BBI->getOperand(i), i);
1204 }
1205
1206 // Did we see all predecessors?
1207 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1208 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1209 if (!seen.count(*PrI)) {
1210 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +00001211 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001212 << " is a predecessor according to the CFG.\n";
1213 }
1214 }
1215 }
1216}
1217
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001218void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001219 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001220
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001221 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1222 MFI != MFE; ++MFI) {
1223 BBInfo &MInfo = MBBInfoMap[MFI];
1224
1225 // Skip unreachable MBBs.
1226 if (!MInfo.reachable)
1227 continue;
1228
1229 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001230 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001231
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001232 // Now check liveness info if available
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001233 calcRegsRequired();
1234
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001235 // Check for killed virtual registers that should be live out.
1236 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1237 MFI != MFE; ++MFI) {
1238 BBInfo &MInfo = MBBInfoMap[MFI];
1239 for (RegSet::iterator
1240 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1241 ++I)
1242 if (MInfo.regsKilled.count(*I)) {
Bill Wendling96cb1122012-07-19 00:04:14 +00001243 report("Virtual register killed in block, but needed live out.", MFI);
1244 *OS << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001245 << " is used after the block.\n";
1246 }
1247 }
1248
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001249 if (!MF->empty()) {
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001250 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1251 for (RegSet::iterator
1252 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesenff0275e2012-03-10 00:44:11 +00001253 ++I)
1254 report("Virtual register def doesn't dominate all uses.",
1255 MRI->getVRegDef(*I));
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001256 }
1257
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001258 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001259 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001260 if (LiveInts)
1261 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001262}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001263
1264void MachineVerifier::verifyLiveVariables() {
1265 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001266 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1267 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001268 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1269 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1270 MFI != MFE; ++MFI) {
1271 BBInfo &MInfo = MBBInfoMap[MFI];
1272
1273 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1274 if (MInfo.vregsRequired.count(Reg)) {
1275 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1276 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001277 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001278 << " must be live through the block.\n";
1279 }
1280 } else {
1281 if (VI.AliveBlocks.test(MFI->getNumber())) {
1282 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001283 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001284 << " is not needed live through the block.\n";
1285 }
1286 }
1287 }
1288 }
1289}
1290
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001291void MachineVerifier::verifyLiveIntervals() {
1292 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001293 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1294 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001295
1296 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001297 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001298 continue;
1299
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001300 if (!LiveInts->hasInterval(Reg)) {
1301 report("Missing live interval for virtual register", MF);
1302 *OS << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001303 continue;
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001304 }
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001305
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001306 const LiveInterval &LI = LiveInts->getInterval(Reg);
1307 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001308 verifyLiveInterval(LI);
1309 }
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001310
1311 // Verify all the cached regunit intervals.
1312 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
1313 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(i))
1314 verifyLiveInterval(*LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001315}
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001316
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001317void MachineVerifier::verifyLiveIntervalValue(const LiveInterval &LI,
1318 VNInfo *VNI) {
1319 if (VNI->isUnused())
1320 return;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001321
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001322 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001323
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001324 if (!DefVNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001325 report("Valno not live at def and not marked unused", MF, LI);
1326 *OS << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001327 return;
1328 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001329
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001330 if (DefVNI != VNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001331 report("Live range at def has different valno", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001332 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001333 << " where valno #" << DefVNI->id << " is live\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001334 return;
1335 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001336
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001337 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1338 if (!MBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001339 report("Invalid definition index", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001340 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1341 << " in " << LI << '\n';
1342 return;
1343 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001344
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001345 if (VNI->isPHIDef()) {
1346 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001347 report("PHIDef value is not defined at MBB start", MBB, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001348 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001349 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001350 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001351 return;
1352 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001353
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001354 // Non-PHI def.
1355 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1356 if (!MI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001357 report("No instruction at def index", MBB, LI);
1358 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001359 return;
1360 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001361
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001362 bool hasDef = false;
1363 bool isEarlyClobber = false;
1364 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1365 if (!MOI->isReg() || !MOI->isDef())
1366 continue;
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001367 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001368 if (MOI->getReg() != LI.reg)
1369 continue;
1370 } else {
1371 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001372 !TRI->hasRegUnit(MOI->getReg(), LI.reg))
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001373 continue;
1374 }
1375 hasDef = true;
1376 if (MOI->isEarlyClobber())
1377 isEarlyClobber = true;
1378 }
1379
1380 if (!hasDef) {
1381 report("Defining instruction does not modify register", MI);
1382 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1383 }
1384
1385 // Early clobber defs begin at USE slots, but other defs must begin at
1386 // DEF slots.
1387 if (isEarlyClobber) {
1388 if (!VNI->def.isEarlyClobber()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001389 report("Early clobber def must be at an early-clobber slot", MBB, LI);
1390 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001391 }
1392 } else if (!VNI->def.isRegister()) {
1393 report("Non-PHI, non-early clobber def must be at a register slot",
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001394 MBB, LI);
1395 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001396 }
1397}
1398
1399void
1400MachineVerifier::verifyLiveIntervalSegment(const LiveInterval &LI,
1401 LiveInterval::const_iterator I) {
1402 const VNInfo *VNI = I->valno;
1403 assert(VNI && "Live range has no valno");
1404
1405 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001406 report("Foreign valno in live range", MF, LI);
1407 *OS << *I << " has a bad valno\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001408 }
1409
1410 if (VNI->isUnused()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001411 report("Live range valno is marked unused", MF, LI);
1412 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001413 }
1414
1415 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1416 if (!MBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001417 report("Bad start of live segment, no basic block", MF, LI);
1418 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001419 return;
1420 }
1421 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1422 if (I->start != MBBStartIdx && I->start != VNI->def) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001423 report("Live segment must begin at MBB entry or valno def", MBB, LI);
1424 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001425 }
1426
1427 const MachineBasicBlock *EndMBB =
1428 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1429 if (!EndMBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001430 report("Bad end of live segment, no basic block", MF, LI);
1431 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001432 return;
1433 }
1434
1435 // No more checks for live-out segments.
1436 if (I->end == LiveInts->getMBBEndIdx(EndMBB))
1437 return;
1438
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001439 // RegUnit intervals are allowed dead phis.
1440 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) && VNI->isPHIDef() &&
1441 I->start == VNI->def && I->end == VNI->def.getDeadSlot())
1442 return;
1443
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001444 // The live segment is ending inside EndMBB
1445 const MachineInstr *MI =
1446 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1447 if (!MI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001448 report("Live segment doesn't end at a valid instruction", EndMBB, LI);
1449 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001450 return;
1451 }
1452
1453 // The block slot must refer to a basic block boundary.
1454 if (I->end.isBlock()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001455 report("Live segment ends at B slot of an instruction", EndMBB, LI);
1456 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001457 }
1458
1459 if (I->end.isDead()) {
1460 // Segment ends on the dead slot.
1461 // That means there must be a dead def.
1462 if (!SlotIndex::isSameInstr(I->start, I->end)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001463 report("Live segment ending at dead slot spans instructions", EndMBB, LI);
1464 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001465 }
1466 }
1467
1468 // A live segment can only end at an early-clobber slot if it is being
1469 // redefined by an early-clobber def.
1470 if (I->end.isEarlyClobber()) {
1471 if (I+1 == LI.end() || (I+1)->start != I->end) {
1472 report("Live segment ending at early clobber slot must be "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001473 "redefined by an EC def in the same instruction", EndMBB, LI);
1474 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001475 }
1476 }
1477
1478 // The following checks only apply to virtual registers. Physreg liveness
1479 // is too weird to check.
1480 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1481 // A live range can end with either a redefinition, a kill flag on a
1482 // use, or a dead flag on a def.
1483 bool hasRead = false;
1484 bool hasDeadDef = false;
1485 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1486 if (!MOI->isReg() || MOI->getReg() != LI.reg)
1487 continue;
1488 if (MOI->readsReg())
1489 hasRead = true;
1490 if (MOI->isDef() && MOI->isDead())
1491 hasDeadDef = true;
1492 }
1493
1494 if (I->end.isDead()) {
1495 if (!hasDeadDef) {
1496 report("Instruction doesn't have a dead def operand", MI);
1497 I->print(*OS);
1498 *OS << " in " << LI << '\n';
1499 }
1500 } else {
1501 if (!hasRead) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001502 report("Instruction ending live range doesn't read the register", MI);
1503 *OS << *I << " in " << LI << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001504 }
1505 }
1506 }
1507
1508 // Now check all the basic blocks in this live segment.
1509 MachineFunction::const_iterator MFI = MBB;
1510 // Is this live range the beginning of a non-PHIDef VN?
1511 if (I->start == VNI->def && !VNI->isPHIDef()) {
1512 // Not live-in to any blocks.
1513 if (MBB == EndMBB)
1514 return;
1515 // Skip this block.
1516 ++MFI;
1517 }
1518 for (;;) {
1519 assert(LiveInts->isLiveInToMBB(LI, MFI));
1520 // We don't know how to track physregs into a landing pad.
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001521 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) &&
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001522 MFI->isLandingPad()) {
1523 if (&*MFI == EndMBB)
1524 break;
1525 ++MFI;
1526 continue;
1527 }
1528
1529 // Is VNI a PHI-def in the current block?
1530 bool IsPHI = VNI->isPHIDef() &&
1531 VNI->def == LiveInts->getMBBStartIdx(MFI);
1532
1533 // Check that VNI is live-out of all predecessors.
1534 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1535 PE = MFI->pred_end(); PI != PE; ++PI) {
1536 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1537 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
1538
1539 // All predecessors must have a live-out value.
1540 if (!PVNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001541 report("Register not marked live out of predecessor", *PI, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001542 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1543 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001544 << PEnd << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001545 continue;
1546 }
1547
1548 // Only PHI-defs can take different predecessor values.
1549 if (!IsPHI && PVNI != VNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001550 report("Different value live out of predecessor", *PI, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001551 *OS << "Valno #" << PVNI->id << " live out of BB#"
1552 << (*PI)->getNumber() << '@' << PEnd
1553 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001554 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001555 }
1556 }
1557 if (&*MFI == EndMBB)
1558 break;
1559 ++MFI;
1560 }
1561}
1562
1563void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1564 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1565 I!=E; ++I)
1566 verifyLiveIntervalValue(LI, *I);
1567
1568 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I)
1569 verifyLiveIntervalSegment(LI, I);
1570
1571 // Check the LI only has one connected component.
1572 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1573 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1574 unsigned NumComp = ConEQ.Classify(&LI);
1575 if (NumComp > 1) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001576 report("Multiple connected components in live interval", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001577 for (unsigned comp = 0; comp != NumComp; ++comp) {
1578 *OS << comp << ": valnos";
1579 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1580 E = LI.vni_end(); I!=E; ++I)
1581 if (comp == ConEQ.getEqClass(*I))
1582 *OS << ' ' << (*I)->id;
1583 *OS << '\n';
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001584 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001585 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001586 }
1587}