blob: a7f8112b6a5fed261b6262ac585b22b5b6af2869 [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
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000026#include "llvm/CodeGen/Passes.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000027#include "llvm/ADT/DenseSet.h"
28#include "llvm/ADT/SetOperations.h"
29#include "llvm/ADT/SmallVector.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000030#include "llvm/CodeGen/LiveIntervalAnalysis.h"
31#include "llvm/CodeGen/LiveStackAnalysis.h"
32#include "llvm/CodeGen/LiveVariables.h"
33#include "llvm/CodeGen/MachineFrameInfo.h"
34#include "llvm/CodeGen/MachineFunctionPass.h"
35#include "llvm/CodeGen/MachineInstrBundle.h"
36#include "llvm/CodeGen/MachineMemOperand.h"
37#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000038#include "llvm/IR/BasicBlock.h"
39#include "llvm/IR/InlineAsm.h"
40#include "llvm/IR/Instructions.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000041#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000042#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000043#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000045#include "llvm/Target/TargetInstrInfo.h"
46#include "llvm/Target/TargetMachine.h"
47#include "llvm/Target/TargetRegisterInfo.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;
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000310 // Do we expect the next instruction to be part of the same bundle?
311 bool InBundle = false;
312
Evan Chengddfd1372011-12-14 02:11:42 +0000313 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
314 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000315 if (MBBI->getParent() != MFI) {
316 report("Bad instruction parent pointer", MFI);
317 *OS << "Instruction: " << *MBBI;
318 continue;
319 }
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000320
321 // Check for consistent bundle flags.
322 if (InBundle && !MBBI->isBundledWithPred())
323 report("Missing BundledPred flag, "
324 "BundledSucc was set on predecessor", MBBI);
325 if (!InBundle && MBBI->isBundledWithPred())
326 report("BundledPred flag is set, "
327 "but BundledSucc not set on predecessor", MBBI);
328
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000329 // Is this a bundle header?
330 if (!MBBI->isInsideBundle()) {
331 if (CurBundle)
332 visitMachineBundleAfter(CurBundle);
333 CurBundle = MBBI;
334 visitMachineBundleBefore(CurBundle);
335 } else if (!CurBundle)
336 report("No bundle header", MBBI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000337 visitMachineInstrBefore(MBBI);
338 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
339 visitMachineOperand(&MBBI->getOperand(I), I);
340 visitMachineInstrAfter(MBBI);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000341
342 // Was this the last bundled instruction?
343 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000344 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000345 if (CurBundle)
346 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000347 if (InBundle)
348 report("BundledSucc flag set on last instruction in block", &MFI->back());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000349 visitMachineBasicBlockAfter(MFI);
350 }
351 visitMachineFunctionAfter();
352
Chris Lattner17e9edc2009-08-23 02:51:22 +0000353 if (OutFile)
354 delete OutFile;
355 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000356 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000357
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000358 // Clean up.
359 regsLive.clear();
360 regsDefined.clear();
361 regsDead.clear();
362 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000363 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000364 regsLiveInButUnused.clear();
365 MBBInfoMap.clear();
366
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000367 return false; // no changes
368}
369
Chris Lattner372fefe2009-08-23 01:03:30 +0000370void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000371 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000372 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000373 if (!foundErrors++) {
374 if (Banner)
375 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000376 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000377 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000378 *OS << "*** Bad machine code: " << msg << " ***\n"
Craig Topper96601ca2012-08-22 06:07:19 +0000379 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000380}
381
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000382void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000383 assert(MBB);
384 report(msg, MBB->getParent());
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000385 *OS << "- basic block: BB#" << MBB->getNumber()
386 << ' ' << MBB->getName()
Roman Divacky59324292012-09-05 22:26:57 +0000387 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000388 if (Indexes)
389 *OS << " [" << Indexes->getMBBStartIdx(MBB)
390 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
391 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000392}
393
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000394void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000395 assert(MI);
396 report(msg, MI->getParent());
397 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000398 if (Indexes && Indexes->hasIndex(MI))
399 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000400 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000401}
402
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000403void MachineVerifier::report(const char *msg,
404 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000405 assert(MO);
406 report(msg, MO->getParent());
407 *OS << "- operand " << MONum << ": ";
408 MO->print(*OS, TM);
409 *OS << "\n";
410}
411
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000412void MachineVerifier::report(const char *msg, const MachineFunction *MF,
413 const LiveInterval &LI) {
414 report(msg, MF);
415 *OS << "- interval: ";
416 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
417 *OS << PrintReg(LI.reg, TRI);
418 else
419 *OS << PrintRegUnit(LI.reg, TRI);
420 *OS << ' ' << LI << '\n';
421}
422
423void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
424 const LiveInterval &LI) {
425 report(msg, MBB);
426 *OS << "- interval: ";
427 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
428 *OS << PrintReg(LI.reg, TRI);
429 else
430 *OS << PrintRegUnit(LI.reg, TRI);
431 *OS << ' ' << LI << '\n';
432}
433
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000434void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000435 BBInfo &MInfo = MBBInfoMap[MBB];
436 if (!MInfo.reachable) {
437 MInfo.reachable = true;
438 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
439 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
440 markReachable(*SuI);
441 }
442}
443
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000444void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000445 lastIndex = SlotIndex();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000446 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000447
448 // A sub-register of a reserved register is also reserved
449 for (int Reg = regsReserved.find_first(); Reg>=0;
450 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000451 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000452 // FIXME: This should probably be:
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000453 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
454 regsReserved.set(*SubRegs);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000455 }
456 }
Lang Hames03698de2012-02-14 19:17:48 +0000457
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000458 markReachable(&MF->front());
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000459
460 // Build a set of the basic blocks in the function.
461 FunctionBlocks.clear();
462 for (MachineFunction::const_iterator
463 I = MF->begin(), E = MF->end(); I != E; ++I) {
464 FunctionBlocks.insert(I);
465 BBInfo &MInfo = MBBInfoMap[I];
466
467 MInfo.Preds.insert(I->pred_begin(), I->pred_end());
468 if (MInfo.Preds.size() != I->pred_size())
469 report("MBB has duplicate entries in its predecessor list.", I);
470
471 MInfo.Succs.insert(I->succ_begin(), I->succ_end());
472 if (MInfo.Succs.size() != I->succ_size())
473 report("MBB has duplicate entries in its successor list.", I);
474 }
Jakob Stoklund Olesena58d67a2013-04-19 21:40:57 +0000475
476 // Check that the register use lists are sane.
477 MRI->verifyUseLists();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000478}
479
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000480// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000481static bool matchPair(MachineBasicBlock::const_succ_iterator i,
482 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000483 if (*i == a)
484 return *++i == b;
485 if (*i == b)
486 return *++i == a;
487 return false;
488}
489
490void
491MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000492 FirstTerminator = 0;
493
Lang Hames03698de2012-02-14 19:17:48 +0000494 if (MRI->isSSA()) {
495 // If this block has allocatable physical registers live-in, check that
496 // it is an entry block or landing pad.
497 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
498 LE = MBB->livein_end();
499 LI != LE; ++LI) {
500 unsigned reg = *LI;
501 if (isAllocatable(reg) && !MBB->isLandingPad() &&
502 MBB != MBB->getParent()->begin()) {
503 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
504 }
505 }
506 }
507
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000508 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000509 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000510 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000511 E = MBB->succ_end(); I != E; ++I) {
512 if ((*I)->isLandingPad())
513 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000514 if (!FunctionBlocks.count(*I))
515 report("MBB has successor that isn't part of the function.", MBB);
516 if (!MBBInfoMap[*I].Preds.count(MBB)) {
517 report("Inconsistent CFG", MBB);
518 *OS << "MBB is not in the predecessor list of the successor BB#"
519 << (*I)->getNumber() << ".\n";
520 }
521 }
522
523 // Check the predecessor list.
524 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
525 E = MBB->pred_end(); I != E; ++I) {
526 if (!FunctionBlocks.count(*I))
527 report("MBB has predecessor that isn't part of the function.", MBB);
528 if (!MBBInfoMap[*I].Succs.count(MBB)) {
529 report("Inconsistent CFG", MBB);
530 *OS << "MBB is not in the successor list of the predecessor BB#"
531 << (*I)->getNumber() << ".\n";
532 }
Cameron Zwarich2100d212010-12-20 04:19:48 +0000533 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000534
535 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
536 const BasicBlock *BB = MBB->getBasicBlock();
537 if (LandingPadSuccs.size() > 1 &&
538 !(AsmInfo &&
539 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
540 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000541 report("MBB has more than one landing pad successor", MBB);
542
Dan Gohman27920592009-08-27 02:43:49 +0000543 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
544 MachineBasicBlock *TBB = 0, *FBB = 0;
545 SmallVector<MachineOperand, 4> Cond;
546 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
547 TBB, FBB, Cond)) {
548 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
549 // check whether its answers match up with reality.
550 if (!TBB && !FBB) {
551 // Block falls through to its successor.
552 MachineFunction::const_iterator MBBI = MBB;
553 ++MBBI;
554 if (MBBI == MF->end()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000555 // It's possible that the block legitimately ends with a noreturn
556 // call or an unreachable, in which case it won't actually fall
557 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000558 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000559 // It's possible that the block legitimately ends with a noreturn
560 // call or an unreachable, in which case it won't actuall fall
561 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000562 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000563 report("MBB exits via unconditional fall-through but doesn't have "
564 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000565 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000566 report("MBB exits via unconditional fall-through but its successor "
567 "differs from its CFG successor!", MBB);
568 }
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000569 if (!MBB->empty() && getBundleStart(&MBB->back())->isBarrier() &&
570 !TII->isPredicated(getBundleStart(&MBB->back()))) {
Dan Gohman27920592009-08-27 02:43:49 +0000571 report("MBB exits via unconditional fall-through but ends with a "
572 "barrier instruction!", MBB);
573 }
574 if (!Cond.empty()) {
575 report("MBB exits via unconditional fall-through but has a condition!",
576 MBB);
577 }
578 } else if (TBB && !FBB && Cond.empty()) {
579 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000580 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000581 report("MBB exits via unconditional branch but doesn't have "
582 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000583 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000584 report("MBB exits via unconditional branch but the CFG "
585 "successor doesn't match the actual successor!", MBB);
586 }
587 if (MBB->empty()) {
588 report("MBB exits via unconditional branch but doesn't contain "
589 "any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000590 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000591 report("MBB exits via unconditional branch but doesn't end with a "
592 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000593 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000594 report("MBB exits via unconditional branch but the branch isn't a "
595 "terminator instruction!", MBB);
596 }
597 } else if (TBB && !FBB && !Cond.empty()) {
598 // Block conditionally branches somewhere, otherwise falls through.
599 MachineFunction::const_iterator MBBI = MBB;
600 ++MBBI;
601 if (MBBI == MF->end()) {
602 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko344df792012-12-19 22:13:01 +0000603 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000604 // A conditional branch with only one successor is weird, but allowed.
605 if (&*MBBI != TBB)
606 report("MBB exits via conditional branch/fall-through but only has "
607 "one CFG successor!", MBB);
608 else if (TBB != *MBB->succ_begin())
609 report("MBB exits via conditional branch/fall-through but the CFG "
610 "successor don't match the actual successor!", MBB);
611 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000612 report("MBB exits via conditional branch/fall-through but doesn't have "
613 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000614 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000615 report("MBB exits via conditional branch/fall-through but the CFG "
616 "successors don't match the actual successors!", MBB);
617 }
618 if (MBB->empty()) {
619 report("MBB exits via conditional branch/fall-through but doesn't "
620 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000621 } else if (getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000622 report("MBB exits via conditional branch/fall-through but ends with a "
623 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000624 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000625 report("MBB exits via conditional branch/fall-through but the branch "
626 "isn't a terminator instruction!", MBB);
627 }
628 } else if (TBB && FBB) {
629 // Block conditionally branches somewhere, otherwise branches
630 // somewhere else.
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000631 if (MBB->succ_size() == 1) {
632 // A conditional branch with only one successor is weird, but allowed.
633 if (FBB != TBB)
634 report("MBB exits via conditional branch/branch through but only has "
635 "one CFG successor!", MBB);
636 else if (TBB != *MBB->succ_begin())
637 report("MBB exits via conditional branch/branch through but the CFG "
638 "successor don't match the actual successor!", MBB);
639 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000640 report("MBB exits via conditional branch/branch but doesn't have "
641 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000642 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000643 report("MBB exits via conditional branch/branch but the CFG "
644 "successors don't match the actual successors!", MBB);
645 }
646 if (MBB->empty()) {
647 report("MBB exits via conditional branch/branch but doesn't "
648 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000649 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000650 report("MBB exits via conditional branch/branch but doesn't end with a "
651 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000652 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000653 report("MBB exits via conditional branch/branch but the branch "
654 "isn't a terminator instruction!", MBB);
655 }
656 if (Cond.empty()) {
657 report("MBB exits via conditinal branch/branch but there's no "
658 "condition!", MBB);
659 }
660 } else {
661 report("AnalyzeBranch returned invalid data!", MBB);
662 }
663 }
664
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000665 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000666 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000667 E = MBB->livein_end(); I != E; ++I) {
668 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
669 report("MBB live-in list contains non-physical register", MBB);
670 continue;
671 }
Chad Rosier62c320a2013-05-22 23:17:36 +0000672 for (MCSubRegIterator SubRegs(*I, TRI, /*IncludeSelf=*/true);
673 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000674 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000675 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000676 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000677
678 const MachineFrameInfo *MFI = MF->getFrameInfo();
679 assert(MFI && "Function has no frame info");
680 BitVector PR = MFI->getPristineRegs(MBB);
681 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosier62c320a2013-05-22 23:17:36 +0000682 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
683 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000684 regsLive.insert(*SubRegs);
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000685 }
686
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000687 regsKilled.clear();
688 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000689
690 if (Indexes)
691 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000692}
693
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000694// This function gets called for all bundle headers, including normal
695// stand-alone unbundled instructions.
696void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
697 if (Indexes && Indexes->hasIndex(MI)) {
698 SlotIndex idx = Indexes->getInstructionIndex(MI);
699 if (!(idx > lastIndex)) {
700 report("Instruction index out of order", MI);
701 *OS << "Last instruction was at " << lastIndex << '\n';
702 }
703 lastIndex = idx;
704 }
Pete Cooper83569cb2012-06-07 17:41:39 +0000705
706 // Ensure non-terminators don't follow terminators.
707 // Ignore predicated terminators formed by if conversion.
708 // FIXME: If conversion shouldn't need to violate this rule.
709 if (MI->isTerminator() && !TII->isPredicated(MI)) {
710 if (!FirstTerminator)
711 FirstTerminator = MI;
712 } else if (FirstTerminator) {
713 report("Non-terminator instruction after the first terminator", MI);
714 *OS << "First terminator was:\t" << *FirstTerminator;
715 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000716}
717
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000718// The operands on an INLINEASM instruction must follow a template.
719// Verify that the flag operands make sense.
720void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
721 // The first two operands on INLINEASM are the asm string and global flags.
722 if (MI->getNumOperands() < 2) {
723 report("Too few operands on inline asm", MI);
724 return;
725 }
726 if (!MI->getOperand(0).isSymbol())
727 report("Asm string must be an external symbol", MI);
728 if (!MI->getOperand(1).isImm())
729 report("Asm flags must be an immediate", MI);
Chad Rosier3d716882012-10-30 19:11:54 +0000730 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
731 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
732 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000733 report("Unknown asm flags", &MI->getOperand(1), 1);
734
735 assert(InlineAsm::MIOp_FirstOperand == 2 && "Asm format changed");
736
737 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
738 unsigned NumOps;
739 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
740 const MachineOperand &MO = MI->getOperand(OpNo);
741 // There may be implicit ops after the fixed operands.
742 if (!MO.isImm())
743 break;
744 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
745 }
746
747 if (OpNo > MI->getNumOperands())
748 report("Missing operands in last group", MI);
749
750 // An optional MDNode follows the groups.
751 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
752 ++OpNo;
753
754 // All trailing operands must be implicit registers.
755 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
756 const MachineOperand &MO = MI->getOperand(OpNo);
757 if (!MO.isReg() || !MO.isImplicit())
758 report("Expected implicit register after groups", &MO, OpNo);
759 }
760}
761
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000762void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000763 const MCInstrDesc &MCID = MI->getDesc();
764 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000765 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000766 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000767 << MI->getNumExplicitOperands() << " given.\n";
768 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000769
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000770 // Check the tied operands.
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000771 if (MI->isInlineAsm())
772 verifyInlineAsm(MI);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000773
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000774 // Check the MachineMemOperands for basic consistency.
775 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
776 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000777 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000778 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000779 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000780 report("Missing mayStore flag", MI);
781 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000782
783 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000784 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000785 if (LiveInts) {
786 bool mapped = !LiveInts->isNotInMIMap(MI);
787 if (MI->isDebugValue()) {
788 if (mapped)
789 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000790 } else if (MI->isInsideBundle()) {
791 if (mapped)
792 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000793 } else {
794 if (!mapped)
795 report("Missing slot index", MI);
796 }
797 }
798
Andrew Trick3be654f2011-09-21 02:20:46 +0000799 StringRef ErrorInfo;
800 if (!TII->verifyInstruction(MI, ErrorInfo))
801 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000802}
803
804void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000805MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000806 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000807 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000808
Evan Chenge837dea2011-06-28 19:10:37 +0000809 // The first MCID.NumDefs operands must be explicit register defines
810 if (MONum < MCID.getNumDefs()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000811 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000812 if (!MO->isReg())
813 report("Explicit definition must be a register", MO, MONum);
Evan Chengcac58aa2012-05-29 19:40:44 +0000814 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000815 report("Explicit definition marked as use", MO, MONum);
816 else if (MO->isImplicit())
817 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000818 } else if (MONum < MCID.getNumOperands()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000819 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopher113a06c2010-11-17 00:55:36 +0000820 // Don't check if it's the last operand in a variadic instruction. See,
821 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000822 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000823 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000824 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000825 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000826 if (MO->isImplicit())
827 report("Explicit operand marked as implicit", MO, MONum);
828 }
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000829
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000830 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
831 if (TiedTo != -1) {
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000832 if (!MO->isReg())
833 report("Tied use must be a register", MO, MONum);
834 else if (!MO->isTied())
835 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000836 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
837 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000838 } else if (MO->isReg() && MO->isTied())
839 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000840 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000841 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000842 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000843 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000844 }
845
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000846 switch (MO->getType()) {
847 case MachineOperand::MO_Register: {
848 const unsigned Reg = MO->getReg();
849 if (!Reg)
850 return;
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000851 if (MRI->tracksLiveness() && !MI->isDebugValue())
852 checkLiveness(MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000853
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000854 // Verify the consistency of tied operands.
855 if (MO->isTied()) {
856 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
857 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
858 if (!OtherMO.isReg())
859 report("Must be tied to a register", MO, MONum);
860 if (!OtherMO.isTied())
861 report("Missing tie flags on tied operand", MO, MONum);
862 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
863 report("Inconsistent tie links", MO, MONum);
864 if (MONum < MCID.getNumDefs()) {
865 if (OtherIdx < MCID.getNumOperands()) {
866 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
867 report("Explicit def tied to explicit use without tie constraint",
868 MO, MONum);
869 } else {
870 if (!OtherMO.isImplicit())
871 report("Explicit def should be tied to implicit use", MO, MONum);
872 }
873 }
874 }
875
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000876 // Verify two-address constraints after leaving SSA form.
877 unsigned DefIdx;
878 if (!MRI->isSSA() && MO->isUse() &&
879 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
880 Reg != MI->getOperand(DefIdx).getReg())
881 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000882
883 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000884 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000885 unsigned SubIdx = MO->getSubReg();
886
887 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000888 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000889 report("Illegal subregister index for physical register", MO, MONum);
890 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000891 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000892 if (const TargetRegisterClass *DRC =
893 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000894 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000895 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000896 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000897 << DRC->getName() << " register.\n";
898 }
899 }
900 } else {
901 // Virtual register.
902 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
903 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000904 const TargetRegisterClass *SRC =
905 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000906 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000907 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000908 *OS << "Register class " << RC->getName()
909 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000910 return;
911 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000912 if (RC != SRC) {
913 report("Invalid register class for subregister index", MO, MONum);
914 *OS << "Register class " << RC->getName()
915 << " does not fully support subreg index " << SubIdx << "\n";
916 return;
917 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000918 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000919 if (const TargetRegisterClass *DRC =
920 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000921 if (SubIdx) {
922 const TargetRegisterClass *SuperRC =
923 TRI->getLargestLegalSuperClass(RC);
924 if (!SuperRC) {
925 report("No largest legal super class exists.", MO, MONum);
926 return;
927 }
928 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
929 if (!DRC) {
930 report("No matching super-reg register class.", MO, MONum);
931 return;
932 }
933 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000934 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000935 report("Illegal virtual register for instruction", MO, MONum);
936 *OS << "Expected a " << DRC->getName() << " register, but got a "
937 << RC->getName() << " register\n";
938 }
939 }
940 }
941 }
942 break;
943 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000944
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000945 case MachineOperand::MO_RegisterMask:
946 regMasks.push_back(MO->getRegMask());
947 break;
948
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000949 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000950 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
951 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000952 break;
953
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000954 case MachineOperand::MO_FrameIndex:
955 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
956 LiveInts && !LiveInts->isNotInMIMap(MI)) {
957 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
958 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000959 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000960 report("Instruction loads from dead spill slot", MO, MONum);
961 *OS << "Live stack: " << LI << '\n';
962 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000963 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000964 report("Instruction stores to dead spill slot", MO, MONum);
965 *OS << "Live stack: " << LI << '\n';
966 }
967 }
968 break;
969
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000970 default:
971 break;
972 }
973}
974
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000975void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
976 const MachineInstr *MI = MO->getParent();
977 const unsigned Reg = MO->getReg();
978
979 // Both use and def operands can read a register.
980 if (MO->readsReg()) {
981 regsLiveInButUnused.erase(Reg);
982
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000983 if (MO->isKill())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000984 addRegWithSubRegs(regsKilled, Reg);
985
986 // Check that LiveVars knows this kill.
987 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
988 MO->isKill()) {
989 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
990 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
991 report("Kill missing from LiveVariables", MO, MONum);
992 }
993
994 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +0000995 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
996 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
997 // Check the cached regunit intervals.
998 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
999 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1000 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(*Units)) {
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 " << PrintRegUnit(*Units, TRI)
1005 << ' ' << *LI << '\n';
1006 }
1007 if (MO->isKill() && !LRQ.isKill()) {
1008 report("Live range continues after kill flag", MO, MONum);
1009 *OS << PrintRegUnit(*Units, TRI) << ' ' << *LI << '\n';
1010 }
1011 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001012 }
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001013 }
1014
1015 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1016 if (LiveInts->hasInterval(Reg)) {
1017 // This is a virtual register interval.
1018 const LiveInterval &LI = LiveInts->getInterval(Reg);
1019 LiveRangeQuery LRQ(LI, UseIdx);
1020 if (!LRQ.valueIn()) {
1021 report("No live range at use", MO, MONum);
1022 *OS << UseIdx << " is not live in " << LI << '\n';
1023 }
1024 // Check for extra kill flags.
1025 // Note that we allow missing kill flags for now.
1026 if (MO->isKill() && !LRQ.isKill()) {
1027 report("Live range continues after kill flag", MO, MONum);
1028 *OS << "Live range: " << LI << '\n';
1029 }
1030 } else {
1031 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001032 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001033 }
1034 }
1035
1036 // Use of a dead register.
1037 if (!regsLive.count(Reg)) {
1038 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1039 // Reserved registers may be used even when 'dead'.
1040 if (!isReserved(Reg))
1041 report("Using an undefined physical register", MO, MONum);
Pete Cooperb97c57a2012-07-19 23:40:38 +00001042 } else if (MRI->def_empty(Reg)) {
1043 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001044 } else {
1045 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1046 // We don't know which virtual registers are live in, so only complain
1047 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1048 // must be live in. PHI instructions are handled separately.
1049 if (MInfo.regsKilled.count(Reg))
1050 report("Using a killed virtual register", MO, MONum);
1051 else if (!MI->isPHI())
1052 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1053 }
1054 }
1055 }
1056
1057 if (MO->isDef()) {
1058 // Register defined.
1059 // TODO: verify that earlyclobber ops are not used.
1060 if (MO->isDead())
1061 addRegWithSubRegs(regsDead, Reg);
1062 else
1063 addRegWithSubRegs(regsDefined, Reg);
1064
1065 // Verify SSA form.
1066 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
1067 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
1068 report("Multiple virtual register defs in SSA form", MO, MONum);
1069
1070 // Check LiveInts for a live range, but only for virtual registers.
1071 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1072 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001073 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1074 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001075 if (LiveInts->hasInterval(Reg)) {
1076 const LiveInterval &LI = LiveInts->getInterval(Reg);
1077 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1078 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001079 if (VNI->def != DefIdx) {
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001080 report("Inconsistent valno->def", MO, MONum);
1081 *OS << "Valno " << VNI->id << " is not defined at "
1082 << DefIdx << " in " << LI << '\n';
1083 }
1084 } else {
1085 report("No live range at def", MO, MONum);
1086 *OS << DefIdx << " is not live in " << LI << '\n';
1087 }
1088 } else {
1089 report("Virtual register has no Live interval", MO, MONum);
1090 }
1091 }
1092 }
1093}
1094
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001095void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +00001096}
1097
1098// This function gets called after visiting all instructions in a bundle. The
1099// argument points to the bundle header.
1100// Normal stand-alone instructions are also considered 'bundles', and this
1101// function is called for all of them.
1102void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001103 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1104 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001105 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +00001106 // Kill any masked registers.
1107 while (!regMasks.empty()) {
1108 const uint32_t *Mask = regMasks.pop_back_val();
1109 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1110 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1111 MachineOperand::clobbersPhysReg(Mask, *I))
1112 regsDead.push_back(*I);
1113 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001114 set_subtract(regsLive, regsDead); regsDead.clear();
1115 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001116}
1117
1118void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001119MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001120 MBBInfoMap[MBB].regsLiveOut = regsLive;
1121 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +00001122
1123 if (Indexes) {
1124 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1125 if (!(stop > lastIndex)) {
1126 report("Block ends before last instruction index", MBB);
1127 *OS << "Block ends at " << stop
1128 << " last instruction was at " << lastIndex << '\n';
1129 }
1130 lastIndex = stop;
1131 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001132}
1133
1134// Calculate the largest possible vregsPassed sets. These are the registers that
1135// can pass through an MBB live, but may not be live every time. It is assumed
1136// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001137void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001138 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1139 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001140 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001141 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1142 MFI != MFE; ++MFI) {
1143 const MachineBasicBlock &MBB(*MFI);
1144 BBInfo &MInfo = MBBInfoMap[&MBB];
1145 if (!MInfo.reachable)
1146 continue;
1147 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1148 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1149 BBInfo &SInfo = MBBInfoMap[*SuI];
1150 if (SInfo.addPassed(MInfo.regsLiveOut))
1151 todo.insert(*SuI);
1152 }
1153 }
1154
1155 // Iteratively push vregsPassed to successors. This will converge to the same
1156 // final state regardless of DenseSet iteration order.
1157 while (!todo.empty()) {
1158 const MachineBasicBlock *MBB = *todo.begin();
1159 todo.erase(MBB);
1160 BBInfo &MInfo = MBBInfoMap[MBB];
1161 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1162 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1163 if (*SuI == MBB)
1164 continue;
1165 BBInfo &SInfo = MBBInfoMap[*SuI];
1166 if (SInfo.addPassed(MInfo.vregsPassed))
1167 todo.insert(*SuI);
1168 }
1169 }
1170}
1171
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001172// Calculate the set of virtual registers that must be passed through each basic
1173// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001174// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001175void MachineVerifier::calcRegsRequired() {
1176 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001177 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001178 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1179 MFI != MFE; ++MFI) {
1180 const MachineBasicBlock &MBB(*MFI);
1181 BBInfo &MInfo = MBBInfoMap[&MBB];
1182 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1183 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1184 BBInfo &PInfo = MBBInfoMap[*PrI];
1185 if (PInfo.addRequired(MInfo.vregsLiveIn))
1186 todo.insert(*PrI);
1187 }
1188 }
1189
1190 // Iteratively push vregsRequired to predecessors. This will converge to the
1191 // same final state regardless of DenseSet iteration order.
1192 while (!todo.empty()) {
1193 const MachineBasicBlock *MBB = *todo.begin();
1194 todo.erase(MBB);
1195 BBInfo &MInfo = MBBInfoMap[MBB];
1196 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1197 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1198 if (*PrI == MBB)
1199 continue;
1200 BBInfo &SInfo = MBBInfoMap[*PrI];
1201 if (SInfo.addRequired(MInfo.vregsRequired))
1202 todo.insert(*PrI);
1203 }
1204 }
1205}
1206
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001207// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001208// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001209void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001210 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001211 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +00001212 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001213 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001214
1215 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
1216 unsigned Reg = BBI->getOperand(i).getReg();
1217 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
1218 if (!Pre->isSuccessor(MBB))
1219 continue;
1220 seen.insert(Pre);
1221 BBInfo &PrInfo = MBBInfoMap[Pre];
1222 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1223 report("PHI operand is not live-out from predecessor",
1224 &BBI->getOperand(i), i);
1225 }
1226
1227 // Did we see all predecessors?
1228 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1229 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1230 if (!seen.count(*PrI)) {
1231 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +00001232 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001233 << " is a predecessor according to the CFG.\n";
1234 }
1235 }
1236 }
1237}
1238
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001239void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001240 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001241
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001242 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1243 MFI != MFE; ++MFI) {
1244 BBInfo &MInfo = MBBInfoMap[MFI];
1245
1246 // Skip unreachable MBBs.
1247 if (!MInfo.reachable)
1248 continue;
1249
1250 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001251 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001252
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001253 // Now check liveness info if available
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001254 calcRegsRequired();
1255
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001256 // Check for killed virtual registers that should be live out.
1257 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1258 MFI != MFE; ++MFI) {
1259 BBInfo &MInfo = MBBInfoMap[MFI];
1260 for (RegSet::iterator
1261 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1262 ++I)
1263 if (MInfo.regsKilled.count(*I)) {
Bill Wendling96cb1122012-07-19 00:04:14 +00001264 report("Virtual register killed in block, but needed live out.", MFI);
1265 *OS << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001266 << " is used after the block.\n";
1267 }
1268 }
1269
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001270 if (!MF->empty()) {
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001271 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1272 for (RegSet::iterator
1273 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesenff0275e2012-03-10 00:44:11 +00001274 ++I)
1275 report("Virtual register def doesn't dominate all uses.",
1276 MRI->getVRegDef(*I));
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001277 }
1278
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001279 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001280 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001281 if (LiveInts)
1282 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001283}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001284
1285void MachineVerifier::verifyLiveVariables() {
1286 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001287 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1288 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001289 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1290 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1291 MFI != MFE; ++MFI) {
1292 BBInfo &MInfo = MBBInfoMap[MFI];
1293
1294 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1295 if (MInfo.vregsRequired.count(Reg)) {
1296 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1297 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001298 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001299 << " must be live through the block.\n";
1300 }
1301 } else {
1302 if (VI.AliveBlocks.test(MFI->getNumber())) {
1303 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001304 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001305 << " is not needed live through the block.\n";
1306 }
1307 }
1308 }
1309 }
1310}
1311
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001312void MachineVerifier::verifyLiveIntervals() {
1313 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001314 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1315 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001316
1317 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001318 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001319 continue;
1320
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001321 if (!LiveInts->hasInterval(Reg)) {
1322 report("Missing live interval for virtual register", MF);
1323 *OS << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001324 continue;
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001325 }
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001326
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001327 const LiveInterval &LI = LiveInts->getInterval(Reg);
1328 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001329 verifyLiveInterval(LI);
1330 }
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001331
1332 // Verify all the cached regunit intervals.
1333 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
1334 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(i))
1335 verifyLiveInterval(*LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001336}
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001337
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001338void MachineVerifier::verifyLiveIntervalValue(const LiveInterval &LI,
1339 VNInfo *VNI) {
1340 if (VNI->isUnused())
1341 return;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001342
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001343 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001344
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001345 if (!DefVNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001346 report("Valno not live at def and not marked unused", MF, LI);
1347 *OS << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001348 return;
1349 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001350
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001351 if (DefVNI != VNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001352 report("Live range at def has different valno", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001353 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001354 << " where valno #" << DefVNI->id << " is live\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001355 return;
1356 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001357
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001358 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1359 if (!MBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001360 report("Invalid definition index", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001361 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1362 << " in " << LI << '\n';
1363 return;
1364 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001365
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001366 if (VNI->isPHIDef()) {
1367 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001368 report("PHIDef value is not defined at MBB start", MBB, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001369 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001370 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001371 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001372 return;
1373 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001374
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001375 // Non-PHI def.
1376 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1377 if (!MI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001378 report("No instruction at def index", MBB, LI);
1379 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001380 return;
1381 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001382
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001383 bool hasDef = false;
1384 bool isEarlyClobber = false;
1385 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1386 if (!MOI->isReg() || !MOI->isDef())
1387 continue;
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001388 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001389 if (MOI->getReg() != LI.reg)
1390 continue;
1391 } else {
1392 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001393 !TRI->hasRegUnit(MOI->getReg(), LI.reg))
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001394 continue;
1395 }
1396 hasDef = true;
1397 if (MOI->isEarlyClobber())
1398 isEarlyClobber = true;
1399 }
1400
1401 if (!hasDef) {
1402 report("Defining instruction does not modify register", MI);
1403 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1404 }
1405
1406 // Early clobber defs begin at USE slots, but other defs must begin at
1407 // DEF slots.
1408 if (isEarlyClobber) {
1409 if (!VNI->def.isEarlyClobber()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001410 report("Early clobber def must be at an early-clobber slot", MBB, LI);
1411 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001412 }
1413 } else if (!VNI->def.isRegister()) {
1414 report("Non-PHI, non-early clobber def must be at a register slot",
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001415 MBB, LI);
1416 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001417 }
1418}
1419
1420void
1421MachineVerifier::verifyLiveIntervalSegment(const LiveInterval &LI,
1422 LiveInterval::const_iterator I) {
1423 const VNInfo *VNI = I->valno;
1424 assert(VNI && "Live range has no valno");
1425
1426 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001427 report("Foreign valno in live range", MF, LI);
1428 *OS << *I << " has a bad valno\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001429 }
1430
1431 if (VNI->isUnused()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001432 report("Live range valno is marked unused", MF, LI);
1433 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001434 }
1435
1436 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1437 if (!MBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001438 report("Bad start of live segment, no basic block", MF, LI);
1439 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001440 return;
1441 }
1442 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1443 if (I->start != MBBStartIdx && I->start != VNI->def) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001444 report("Live segment must begin at MBB entry or valno def", MBB, LI);
1445 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001446 }
1447
1448 const MachineBasicBlock *EndMBB =
1449 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1450 if (!EndMBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001451 report("Bad end of live segment, no basic block", MF, LI);
1452 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001453 return;
1454 }
1455
1456 // No more checks for live-out segments.
1457 if (I->end == LiveInts->getMBBEndIdx(EndMBB))
1458 return;
1459
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001460 // RegUnit intervals are allowed dead phis.
1461 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) && VNI->isPHIDef() &&
1462 I->start == VNI->def && I->end == VNI->def.getDeadSlot())
1463 return;
1464
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001465 // The live segment is ending inside EndMBB
1466 const MachineInstr *MI =
1467 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1468 if (!MI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001469 report("Live segment doesn't end at a valid instruction", EndMBB, LI);
1470 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001471 return;
1472 }
1473
1474 // The block slot must refer to a basic block boundary.
1475 if (I->end.isBlock()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001476 report("Live segment ends at B slot of an instruction", EndMBB, LI);
1477 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001478 }
1479
1480 if (I->end.isDead()) {
1481 // Segment ends on the dead slot.
1482 // That means there must be a dead def.
1483 if (!SlotIndex::isSameInstr(I->start, I->end)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001484 report("Live segment ending at dead slot spans instructions", EndMBB, LI);
1485 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001486 }
1487 }
1488
1489 // A live segment can only end at an early-clobber slot if it is being
1490 // redefined by an early-clobber def.
1491 if (I->end.isEarlyClobber()) {
1492 if (I+1 == LI.end() || (I+1)->start != I->end) {
1493 report("Live segment ending at early clobber slot must be "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001494 "redefined by an EC def in the same instruction", EndMBB, LI);
1495 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001496 }
1497 }
1498
1499 // The following checks only apply to virtual registers. Physreg liveness
1500 // is too weird to check.
1501 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1502 // A live range can end with either a redefinition, a kill flag on a
1503 // use, or a dead flag on a def.
1504 bool hasRead = false;
1505 bool hasDeadDef = false;
1506 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1507 if (!MOI->isReg() || MOI->getReg() != LI.reg)
1508 continue;
1509 if (MOI->readsReg())
1510 hasRead = true;
1511 if (MOI->isDef() && MOI->isDead())
1512 hasDeadDef = true;
1513 }
1514
1515 if (I->end.isDead()) {
1516 if (!hasDeadDef) {
1517 report("Instruction doesn't have a dead def operand", MI);
1518 I->print(*OS);
1519 *OS << " in " << LI << '\n';
1520 }
1521 } else {
1522 if (!hasRead) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001523 report("Instruction ending live range doesn't read the register", MI);
1524 *OS << *I << " in " << LI << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001525 }
1526 }
1527 }
1528
1529 // Now check all the basic blocks in this live segment.
1530 MachineFunction::const_iterator MFI = MBB;
1531 // Is this live range the beginning of a non-PHIDef VN?
1532 if (I->start == VNI->def && !VNI->isPHIDef()) {
1533 // Not live-in to any blocks.
1534 if (MBB == EndMBB)
1535 return;
1536 // Skip this block.
1537 ++MFI;
1538 }
1539 for (;;) {
1540 assert(LiveInts->isLiveInToMBB(LI, MFI));
1541 // We don't know how to track physregs into a landing pad.
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001542 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) &&
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001543 MFI->isLandingPad()) {
1544 if (&*MFI == EndMBB)
1545 break;
1546 ++MFI;
1547 continue;
1548 }
1549
1550 // Is VNI a PHI-def in the current block?
1551 bool IsPHI = VNI->isPHIDef() &&
1552 VNI->def == LiveInts->getMBBStartIdx(MFI);
1553
1554 // Check that VNI is live-out of all predecessors.
1555 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1556 PE = MFI->pred_end(); PI != PE; ++PI) {
1557 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1558 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
1559
1560 // All predecessors must have a live-out value.
1561 if (!PVNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001562 report("Register not marked live out of predecessor", *PI, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001563 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1564 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001565 << PEnd << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001566 continue;
1567 }
1568
1569 // Only PHI-defs can take different predecessor values.
1570 if (!IsPHI && PVNI != VNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001571 report("Different value live out of predecessor", *PI, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001572 *OS << "Valno #" << PVNI->id << " live out of BB#"
1573 << (*PI)->getNumber() << '@' << PEnd
1574 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001575 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001576 }
1577 }
1578 if (&*MFI == EndMBB)
1579 break;
1580 ++MFI;
1581 }
1582}
1583
1584void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1585 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1586 I!=E; ++I)
1587 verifyLiveIntervalValue(LI, *I);
1588
1589 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I)
1590 verifyLiveIntervalSegment(LI, I);
1591
1592 // Check the LI only has one connected component.
1593 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1594 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1595 unsigned NumComp = ConEQ.Classify(&LI);
1596 if (NumComp > 1) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001597 report("Multiple connected components in live interval", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001598 for (unsigned comp = 0; comp != NumComp; ++comp) {
1599 *OS << comp << ": valnos";
1600 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1601 E = LI.vni_end(); I!=E; ++I)
1602 if (comp == ConEQ.getEqClass(*I))
1603 *OS << ' ' << (*I)->id;
1604 *OS << '\n';
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001605 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001606 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001607 }
1608}