blob: 5c2976e1fd6ba34220ab9439deeaa2c147659a53 [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"
Manman Ren7310b752013-07-15 21:26:31 +000028#include "llvm/ADT/DepthFirstIterator.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000029#include "llvm/ADT/SetOperations.h"
30#include "llvm/ADT/SmallVector.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/CodeGen/LiveIntervalAnalysis.h"
32#include "llvm/CodeGen/LiveStackAnalysis.h"
33#include "llvm/CodeGen/LiveVariables.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunctionPass.h"
36#include "llvm/CodeGen/MachineInstrBundle.h"
37#include "llvm/CodeGen/MachineMemOperand.h"
38#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000039#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/InlineAsm.h"
41#include "llvm/IR/Instructions.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000042#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000043#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000044#include "llvm/Support/ErrorHandling.h"
45#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000046#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetMachine.h"
48#include "llvm/Target/TargetRegisterInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000049using namespace llvm;
50
51namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000052 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000053
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000054 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000055 PASS(pass),
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000056 Banner(b),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000057 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000058 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000059
60 bool runOnMachineFunction(MachineFunction &MF);
61
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000062 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000063 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000064 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000065 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000066 const MachineFunction *MF;
67 const TargetMachine *TM;
Evan Cheng15993f82011-06-27 21:26:13 +000068 const TargetInstrInfo *TII;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000069 const TargetRegisterInfo *TRI;
70 const MachineRegisterInfo *MRI;
71
72 unsigned foundErrors;
73
74 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000075 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000076 typedef DenseSet<unsigned> RegSet;
77 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +000078 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000079
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000080 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +000081 BlockSet FunctionBlocks;
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000082
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000083 BitVector regsReserved;
84 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000085 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000086 RegMaskVector regMasks;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000087 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000088
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000089 SlotIndex lastIndex;
90
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000091 // Add Reg and any sub-registers to RV
92 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
93 RV.push_back(Reg);
94 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +000095 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
96 RV.push_back(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000097 }
98
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000099 struct BBInfo {
100 // Is this MBB reachable from the MF entry point?
101 bool reachable;
102
103 // Vregs that must be live in because they are used without being
104 // defined. Map value is the user.
105 RegMap vregsLiveIn;
106
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000107 // Regs killed in MBB. They may be defined again, and will then be in both
108 // regsKilled and regsLiveOut.
109 RegSet regsKilled;
110
111 // Regs defined in MBB and live out. Note that vregs passing through may
112 // be live out without being mentioned here.
113 RegSet regsLiveOut;
114
115 // Vregs that pass through MBB untouched. This set is disjoint from
116 // regsKilled and regsLiveOut.
117 RegSet vregsPassed;
118
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000119 // Vregs that must pass through MBB because they are needed by a successor
120 // block. This set is disjoint from regsLiveOut.
121 RegSet vregsRequired;
122
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000123 // Set versions of block's predecessor and successor lists.
124 BlockSet Preds, Succs;
125
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000126 BBInfo() : reachable(false) {}
127
128 // Add register to vregsPassed if it belongs there. Return true if
129 // anything changed.
130 bool addPassed(unsigned Reg) {
131 if (!TargetRegisterInfo::isVirtualRegister(Reg))
132 return false;
133 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
134 return false;
135 return vregsPassed.insert(Reg).second;
136 }
137
138 // Same for a full set.
139 bool addPassed(const RegSet &RS) {
140 bool changed = false;
141 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
142 if (addPassed(*I))
143 changed = true;
144 return changed;
145 }
146
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000147 // Add register to vregsRequired if it belongs there. Return true if
148 // anything changed.
149 bool addRequired(unsigned Reg) {
150 if (!TargetRegisterInfo::isVirtualRegister(Reg))
151 return false;
152 if (regsLiveOut.count(Reg))
153 return false;
154 return vregsRequired.insert(Reg).second;
155 }
156
157 // Same for a full set.
158 bool addRequired(const RegSet &RS) {
159 bool changed = false;
160 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
161 if (addRequired(*I))
162 changed = true;
163 return changed;
164 }
165
166 // Same for a full map.
167 bool addRequired(const RegMap &RM) {
168 bool changed = false;
169 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
170 if (addRequired(I->first))
171 changed = true;
172 return changed;
173 }
174
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000175 // Live-out registers are either in regsLiveOut or vregsPassed.
176 bool isLiveOut(unsigned Reg) const {
177 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
178 }
179 };
180
181 // Extra register info per MBB.
182 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
183
184 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000185 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000186 }
187
Lang Hames03698de2012-02-14 19:17:48 +0000188 bool isAllocatable(unsigned Reg) {
Jakob Stoklund Olesenfeab72c2012-10-16 00:05:06 +0000189 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
Lang Hames03698de2012-02-14 19:17:48 +0000190 }
191
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000192 // Analysis information if available
193 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000194 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000195 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000196 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000197
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000198 void visitMachineFunctionBefore();
199 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000200 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000201 void visitMachineInstrBefore(const MachineInstr *MI);
202 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
203 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000204 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000205 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
206 void visitMachineFunctionAfter();
207
208 void report(const char *msg, const MachineFunction *MF);
209 void report(const char *msg, const MachineBasicBlock *MBB);
210 void report(const char *msg, const MachineInstr *MI);
211 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000212 void report(const char *msg, const MachineFunction *MF,
213 const LiveInterval &LI);
214 void report(const char *msg, const MachineBasicBlock *MBB,
215 const LiveInterval &LI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000216
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000217 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000218
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000219 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000220 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000221 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000222 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000223
224 void calcRegsRequired();
225 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000226 void verifyLiveIntervals();
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +0000227 void verifyLiveInterval(const LiveInterval&);
228 void verifyLiveIntervalValue(const LiveInterval&, VNInfo*);
229 void verifyLiveIntervalSegment(const LiveInterval&,
230 LiveInterval::const_iterator);
Manman Ren7310b752013-07-15 21:26:31 +0000231
232 void verifyStackFrame();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000233 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000234
235 struct MachineVerifierPass : public MachineFunctionPass {
236 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000237 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000238
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000239 MachineVerifierPass(const char *b = 0)
240 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000241 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
242 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000243
244 void getAnalysisUsage(AnalysisUsage &AU) const {
245 AU.setPreservesAll();
246 MachineFunctionPass::getAnalysisUsage(AU);
247 }
248
249 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000250 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000251 return false;
252 }
253 };
254
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000255}
256
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000257char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000258INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000259 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000260
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000261FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
262 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000263}
264
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000265void MachineFunction::verify(Pass *p, const char *Banner) const {
266 MachineVerifier(p, Banner)
267 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000268}
269
Chris Lattner17e9edc2009-08-23 02:51:22 +0000270bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
271 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000272 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000273 std::string ErrorInfo;
Rafael Espindolac1b49b52013-07-16 19:44:17 +0000274 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo, sys::fs::F_Append);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000275 if (!ErrorInfo.empty()) {
276 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
277 exit(1);
278 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000279
Chris Lattner17e9edc2009-08-23 02:51:22 +0000280 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000281 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000282 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000283 }
284
285 foundErrors = 0;
286
287 this->MF = &MF;
288 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000289 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000290 TRI = TM->getRegisterInfo();
291 MRI = &MF.getRegInfo();
292
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000293 LiveVars = NULL;
294 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000295 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000296 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000297 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000298 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000299 // We don't want to verify LiveVariables if LiveIntervals is available.
300 if (!LiveInts)
301 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000302 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000303 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000304 }
305
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000306 visitMachineFunctionBefore();
307 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
308 MFI!=MFE; ++MFI) {
309 visitMachineBasicBlockBefore(MFI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000310 // Keep track of the current bundle header.
311 const MachineInstr *CurBundle = 0;
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000312 // Do we expect the next instruction to be part of the same bundle?
313 bool InBundle = false;
314
Evan Chengddfd1372011-12-14 02:11:42 +0000315 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
316 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000317 if (MBBI->getParent() != MFI) {
318 report("Bad instruction parent pointer", MFI);
319 *OS << "Instruction: " << *MBBI;
320 continue;
321 }
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000322
323 // Check for consistent bundle flags.
324 if (InBundle && !MBBI->isBundledWithPred())
325 report("Missing BundledPred flag, "
326 "BundledSucc was set on predecessor", MBBI);
327 if (!InBundle && MBBI->isBundledWithPred())
328 report("BundledPred flag is set, "
329 "but BundledSucc not set on predecessor", MBBI);
330
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000331 // Is this a bundle header?
332 if (!MBBI->isInsideBundle()) {
333 if (CurBundle)
334 visitMachineBundleAfter(CurBundle);
335 CurBundle = MBBI;
336 visitMachineBundleBefore(CurBundle);
337 } else if (!CurBundle)
338 report("No bundle header", MBBI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000339 visitMachineInstrBefore(MBBI);
340 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
341 visitMachineOperand(&MBBI->getOperand(I), I);
342 visitMachineInstrAfter(MBBI);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000343
344 // Was this the last bundled instruction?
345 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000346 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000347 if (CurBundle)
348 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000349 if (InBundle)
350 report("BundledSucc flag set on last instruction in block", &MFI->back());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000351 visitMachineBasicBlockAfter(MFI);
352 }
353 visitMachineFunctionAfter();
354
Chris Lattner17e9edc2009-08-23 02:51:22 +0000355 if (OutFile)
356 delete OutFile;
357 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000358 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000359
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000360 // Clean up.
361 regsLive.clear();
362 regsDefined.clear();
363 regsDead.clear();
364 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000365 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000366 regsLiveInButUnused.clear();
367 MBBInfoMap.clear();
368
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000369 return false; // no changes
370}
371
Chris Lattner372fefe2009-08-23 01:03:30 +0000372void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000373 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000374 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000375 if (!foundErrors++) {
376 if (Banner)
377 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000378 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000379 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000380 *OS << "*** Bad machine code: " << msg << " ***\n"
Craig Topper96601ca2012-08-22 06:07:19 +0000381 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000382}
383
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000384void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000385 assert(MBB);
386 report(msg, MBB->getParent());
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000387 *OS << "- basic block: BB#" << MBB->getNumber()
388 << ' ' << MBB->getName()
Roman Divacky59324292012-09-05 22:26:57 +0000389 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000390 if (Indexes)
391 *OS << " [" << Indexes->getMBBStartIdx(MBB)
392 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
393 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000394}
395
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000396void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000397 assert(MI);
398 report(msg, MI->getParent());
399 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000400 if (Indexes && Indexes->hasIndex(MI))
401 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000402 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000403}
404
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000405void MachineVerifier::report(const char *msg,
406 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000407 assert(MO);
408 report(msg, MO->getParent());
409 *OS << "- operand " << MONum << ": ";
410 MO->print(*OS, TM);
411 *OS << "\n";
412}
413
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000414void MachineVerifier::report(const char *msg, const MachineFunction *MF,
415 const LiveInterval &LI) {
416 report(msg, MF);
417 *OS << "- interval: ";
418 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
419 *OS << PrintReg(LI.reg, TRI);
420 else
421 *OS << PrintRegUnit(LI.reg, TRI);
422 *OS << ' ' << LI << '\n';
423}
424
425void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
426 const LiveInterval &LI) {
427 report(msg, MBB);
428 *OS << "- interval: ";
429 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
430 *OS << PrintReg(LI.reg, TRI);
431 else
432 *OS << PrintRegUnit(LI.reg, TRI);
433 *OS << ' ' << LI << '\n';
434}
435
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000436void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000437 BBInfo &MInfo = MBBInfoMap[MBB];
438 if (!MInfo.reachable) {
439 MInfo.reachable = true;
440 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
441 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
442 markReachable(*SuI);
443 }
444}
445
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000446void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000447 lastIndex = SlotIndex();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000448 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000449
450 // A sub-register of a reserved register is also reserved
451 for (int Reg = regsReserved.find_first(); Reg>=0;
452 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000453 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000454 // FIXME: This should probably be:
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000455 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
456 regsReserved.set(*SubRegs);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000457 }
458 }
Lang Hames03698de2012-02-14 19:17:48 +0000459
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000460 markReachable(&MF->front());
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000461
462 // Build a set of the basic blocks in the function.
463 FunctionBlocks.clear();
464 for (MachineFunction::const_iterator
465 I = MF->begin(), E = MF->end(); I != E; ++I) {
466 FunctionBlocks.insert(I);
467 BBInfo &MInfo = MBBInfoMap[I];
468
469 MInfo.Preds.insert(I->pred_begin(), I->pred_end());
470 if (MInfo.Preds.size() != I->pred_size())
471 report("MBB has duplicate entries in its predecessor list.", I);
472
473 MInfo.Succs.insert(I->succ_begin(), I->succ_end());
474 if (MInfo.Succs.size() != I->succ_size())
475 report("MBB has duplicate entries in its successor list.", I);
476 }
Jakob Stoklund Olesena58d67a2013-04-19 21:40:57 +0000477
478 // Check that the register use lists are sane.
479 MRI->verifyUseLists();
Manman Ren7310b752013-07-15 21:26:31 +0000480
481 verifyStackFrame();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000482}
483
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000484// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000485static bool matchPair(MachineBasicBlock::const_succ_iterator i,
486 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000487 if (*i == a)
488 return *++i == b;
489 if (*i == b)
490 return *++i == a;
491 return false;
492}
493
494void
495MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000496 FirstTerminator = 0;
497
Lang Hames03698de2012-02-14 19:17:48 +0000498 if (MRI->isSSA()) {
499 // If this block has allocatable physical registers live-in, check that
500 // it is an entry block or landing pad.
501 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
502 LE = MBB->livein_end();
503 LI != LE; ++LI) {
504 unsigned reg = *LI;
505 if (isAllocatable(reg) && !MBB->isLandingPad() &&
506 MBB != MBB->getParent()->begin()) {
507 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
508 }
509 }
510 }
511
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000512 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000513 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000514 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000515 E = MBB->succ_end(); I != E; ++I) {
516 if ((*I)->isLandingPad())
517 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000518 if (!FunctionBlocks.count(*I))
519 report("MBB has successor that isn't part of the function.", MBB);
520 if (!MBBInfoMap[*I].Preds.count(MBB)) {
521 report("Inconsistent CFG", MBB);
522 *OS << "MBB is not in the predecessor list of the successor BB#"
523 << (*I)->getNumber() << ".\n";
524 }
525 }
526
527 // Check the predecessor list.
528 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
529 E = MBB->pred_end(); I != E; ++I) {
530 if (!FunctionBlocks.count(*I))
531 report("MBB has predecessor that isn't part of the function.", MBB);
532 if (!MBBInfoMap[*I].Succs.count(MBB)) {
533 report("Inconsistent CFG", MBB);
534 *OS << "MBB is not in the successor list of the predecessor BB#"
535 << (*I)->getNumber() << ".\n";
536 }
Cameron Zwarich2100d212010-12-20 04:19:48 +0000537 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000538
539 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
540 const BasicBlock *BB = MBB->getBasicBlock();
541 if (LandingPadSuccs.size() > 1 &&
542 !(AsmInfo &&
543 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
544 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000545 report("MBB has more than one landing pad successor", MBB);
546
Dan Gohman27920592009-08-27 02:43:49 +0000547 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
548 MachineBasicBlock *TBB = 0, *FBB = 0;
549 SmallVector<MachineOperand, 4> Cond;
550 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
551 TBB, FBB, Cond)) {
552 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
553 // check whether its answers match up with reality.
554 if (!TBB && !FBB) {
555 // Block falls through to its successor.
556 MachineFunction::const_iterator MBBI = MBB;
557 ++MBBI;
558 if (MBBI == MF->end()) {
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 actually fall
561 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000562 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000563 // It's possible that the block legitimately ends with a noreturn
564 // call or an unreachable, in which case it won't actuall fall
565 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000566 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000567 report("MBB exits via unconditional fall-through but doesn't have "
568 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000569 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000570 report("MBB exits via unconditional fall-through but its successor "
571 "differs from its CFG successor!", MBB);
572 }
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000573 if (!MBB->empty() && getBundleStart(&MBB->back())->isBarrier() &&
574 !TII->isPredicated(getBundleStart(&MBB->back()))) {
Dan Gohman27920592009-08-27 02:43:49 +0000575 report("MBB exits via unconditional fall-through but ends with a "
576 "barrier instruction!", MBB);
577 }
578 if (!Cond.empty()) {
579 report("MBB exits via unconditional fall-through but has a condition!",
580 MBB);
581 }
582 } else if (TBB && !FBB && Cond.empty()) {
583 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000584 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000585 report("MBB exits via unconditional branch but doesn't have "
586 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000587 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000588 report("MBB exits via unconditional branch but the CFG "
589 "successor doesn't match the actual successor!", MBB);
590 }
591 if (MBB->empty()) {
592 report("MBB exits via unconditional branch but doesn't contain "
593 "any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000594 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000595 report("MBB exits via unconditional branch but doesn't end with a "
596 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000597 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000598 report("MBB exits via unconditional branch but the branch isn't a "
599 "terminator instruction!", MBB);
600 }
601 } else if (TBB && !FBB && !Cond.empty()) {
602 // Block conditionally branches somewhere, otherwise falls through.
603 MachineFunction::const_iterator MBBI = MBB;
604 ++MBBI;
605 if (MBBI == MF->end()) {
606 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko344df792012-12-19 22:13:01 +0000607 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000608 // A conditional branch with only one successor is weird, but allowed.
609 if (&*MBBI != TBB)
610 report("MBB exits via conditional branch/fall-through but only has "
611 "one CFG successor!", MBB);
612 else if (TBB != *MBB->succ_begin())
613 report("MBB exits via conditional branch/fall-through but the CFG "
614 "successor don't match the actual successor!", MBB);
615 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000616 report("MBB exits via conditional branch/fall-through but doesn't have "
617 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000618 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000619 report("MBB exits via conditional branch/fall-through but the CFG "
620 "successors don't match the actual successors!", MBB);
621 }
622 if (MBB->empty()) {
623 report("MBB exits via conditional branch/fall-through but doesn't "
624 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000625 } else if (getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000626 report("MBB exits via conditional branch/fall-through but ends with a "
627 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000628 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000629 report("MBB exits via conditional branch/fall-through but the branch "
630 "isn't a terminator instruction!", MBB);
631 }
632 } else if (TBB && FBB) {
633 // Block conditionally branches somewhere, otherwise branches
634 // somewhere else.
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000635 if (MBB->succ_size() == 1) {
636 // A conditional branch with only one successor is weird, but allowed.
637 if (FBB != TBB)
638 report("MBB exits via conditional branch/branch through but only has "
639 "one CFG successor!", MBB);
640 else if (TBB != *MBB->succ_begin())
641 report("MBB exits via conditional branch/branch through but the CFG "
642 "successor don't match the actual successor!", MBB);
643 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000644 report("MBB exits via conditional branch/branch but doesn't have "
645 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000646 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000647 report("MBB exits via conditional branch/branch but the CFG "
648 "successors don't match the actual successors!", MBB);
649 }
650 if (MBB->empty()) {
651 report("MBB exits via conditional branch/branch but doesn't "
652 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000653 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000654 report("MBB exits via conditional branch/branch but doesn't end with a "
655 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000656 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000657 report("MBB exits via conditional branch/branch but the branch "
658 "isn't a terminator instruction!", MBB);
659 }
660 if (Cond.empty()) {
661 report("MBB exits via conditinal branch/branch but there's no "
662 "condition!", MBB);
663 }
664 } else {
665 report("AnalyzeBranch returned invalid data!", MBB);
666 }
667 }
668
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000669 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000670 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000671 E = MBB->livein_end(); I != E; ++I) {
672 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
673 report("MBB live-in list contains non-physical register", MBB);
674 continue;
675 }
Chad Rosier62c320a2013-05-22 23:17:36 +0000676 for (MCSubRegIterator SubRegs(*I, TRI, /*IncludeSelf=*/true);
677 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000678 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000679 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000680 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000681
682 const MachineFrameInfo *MFI = MF->getFrameInfo();
683 assert(MFI && "Function has no frame info");
684 BitVector PR = MFI->getPristineRegs(MBB);
685 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosier62c320a2013-05-22 23:17:36 +0000686 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
687 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000688 regsLive.insert(*SubRegs);
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000689 }
690
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000691 regsKilled.clear();
692 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000693
694 if (Indexes)
695 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000696}
697
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000698// This function gets called for all bundle headers, including normal
699// stand-alone unbundled instructions.
700void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
701 if (Indexes && Indexes->hasIndex(MI)) {
702 SlotIndex idx = Indexes->getInstructionIndex(MI);
703 if (!(idx > lastIndex)) {
704 report("Instruction index out of order", MI);
705 *OS << "Last instruction was at " << lastIndex << '\n';
706 }
707 lastIndex = idx;
708 }
Pete Cooper83569cb2012-06-07 17:41:39 +0000709
710 // Ensure non-terminators don't follow terminators.
711 // Ignore predicated terminators formed by if conversion.
712 // FIXME: If conversion shouldn't need to violate this rule.
713 if (MI->isTerminator() && !TII->isPredicated(MI)) {
714 if (!FirstTerminator)
715 FirstTerminator = MI;
716 } else if (FirstTerminator) {
717 report("Non-terminator instruction after the first terminator", MI);
718 *OS << "First terminator was:\t" << *FirstTerminator;
719 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000720}
721
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000722// The operands on an INLINEASM instruction must follow a template.
723// Verify that the flag operands make sense.
724void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
725 // The first two operands on INLINEASM are the asm string and global flags.
726 if (MI->getNumOperands() < 2) {
727 report("Too few operands on inline asm", MI);
728 return;
729 }
730 if (!MI->getOperand(0).isSymbol())
731 report("Asm string must be an external symbol", MI);
732 if (!MI->getOperand(1).isImm())
733 report("Asm flags must be an immediate", MI);
Chad Rosier3d716882012-10-30 19:11:54 +0000734 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
735 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
736 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000737 report("Unknown asm flags", &MI->getOperand(1), 1);
738
739 assert(InlineAsm::MIOp_FirstOperand == 2 && "Asm format changed");
740
741 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
742 unsigned NumOps;
743 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
744 const MachineOperand &MO = MI->getOperand(OpNo);
745 // There may be implicit ops after the fixed operands.
746 if (!MO.isImm())
747 break;
748 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
749 }
750
751 if (OpNo > MI->getNumOperands())
752 report("Missing operands in last group", MI);
753
754 // An optional MDNode follows the groups.
755 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
756 ++OpNo;
757
758 // All trailing operands must be implicit registers.
759 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
760 const MachineOperand &MO = MI->getOperand(OpNo);
761 if (!MO.isReg() || !MO.isImplicit())
762 report("Expected implicit register after groups", &MO, OpNo);
763 }
764}
765
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000766void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000767 const MCInstrDesc &MCID = MI->getDesc();
768 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000769 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000770 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000771 << MI->getNumExplicitOperands() << " given.\n";
772 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000773
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000774 // Check the tied operands.
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000775 if (MI->isInlineAsm())
776 verifyInlineAsm(MI);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000777
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000778 // Check the MachineMemOperands for basic consistency.
779 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
780 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000781 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000782 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000783 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000784 report("Missing mayStore flag", MI);
785 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000786
787 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000788 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000789 if (LiveInts) {
790 bool mapped = !LiveInts->isNotInMIMap(MI);
791 if (MI->isDebugValue()) {
792 if (mapped)
793 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000794 } else if (MI->isInsideBundle()) {
795 if (mapped)
796 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000797 } else {
798 if (!mapped)
799 report("Missing slot index", MI);
800 }
801 }
802
Andrew Trick3be654f2011-09-21 02:20:46 +0000803 StringRef ErrorInfo;
804 if (!TII->verifyInstruction(MI, ErrorInfo))
805 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000806}
807
808void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000809MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000810 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000811 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000812
Evan Chenge837dea2011-06-28 19:10:37 +0000813 // The first MCID.NumDefs operands must be explicit register defines
814 if (MONum < MCID.getNumDefs()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000815 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000816 if (!MO->isReg())
817 report("Explicit definition must be a register", MO, MONum);
Evan Chengcac58aa2012-05-29 19:40:44 +0000818 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000819 report("Explicit definition marked as use", MO, MONum);
820 else if (MO->isImplicit())
821 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000822 } else if (MONum < MCID.getNumOperands()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000823 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopher113a06c2010-11-17 00:55:36 +0000824 // Don't check if it's the last operand in a variadic instruction. See,
825 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000826 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000827 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000828 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braunb38d9872013-10-04 16:53:00 +0000829 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000830 if (MO->isImplicit())
831 report("Explicit operand marked as implicit", MO, MONum);
832 }
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000833
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000834 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
835 if (TiedTo != -1) {
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000836 if (!MO->isReg())
837 report("Tied use must be a register", MO, MONum);
838 else if (!MO->isTied())
839 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000840 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
841 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000842 } else if (MO->isReg() && MO->isTied())
843 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000844 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000845 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000846 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000847 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000848 }
849
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000850 switch (MO->getType()) {
851 case MachineOperand::MO_Register: {
852 const unsigned Reg = MO->getReg();
853 if (!Reg)
854 return;
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000855 if (MRI->tracksLiveness() && !MI->isDebugValue())
856 checkLiveness(MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000857
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000858 // Verify the consistency of tied operands.
859 if (MO->isTied()) {
860 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
861 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
862 if (!OtherMO.isReg())
863 report("Must be tied to a register", MO, MONum);
864 if (!OtherMO.isTied())
865 report("Missing tie flags on tied operand", MO, MONum);
866 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
867 report("Inconsistent tie links", MO, MONum);
868 if (MONum < MCID.getNumDefs()) {
869 if (OtherIdx < MCID.getNumOperands()) {
870 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
871 report("Explicit def tied to explicit use without tie constraint",
872 MO, MONum);
873 } else {
874 if (!OtherMO.isImplicit())
875 report("Explicit def should be tied to implicit use", MO, MONum);
876 }
877 }
878 }
879
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000880 // Verify two-address constraints after leaving SSA form.
881 unsigned DefIdx;
882 if (!MRI->isSSA() && MO->isUse() &&
883 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
884 Reg != MI->getOperand(DefIdx).getReg())
885 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000886
887 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000888 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000889 unsigned SubIdx = MO->getSubReg();
890
891 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000892 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000893 report("Illegal subregister index for physical register", MO, MONum);
894 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000895 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000896 if (const TargetRegisterClass *DRC =
897 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000898 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000899 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000900 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000901 << DRC->getName() << " register.\n";
902 }
903 }
904 } else {
905 // Virtual register.
906 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
907 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000908 const TargetRegisterClass *SRC =
909 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000910 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000911 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000912 *OS << "Register class " << RC->getName()
913 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000914 return;
915 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000916 if (RC != SRC) {
917 report("Invalid register class for subregister index", MO, MONum);
918 *OS << "Register class " << RC->getName()
919 << " does not fully support subreg index " << SubIdx << "\n";
920 return;
921 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000922 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000923 if (const TargetRegisterClass *DRC =
924 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000925 if (SubIdx) {
926 const TargetRegisterClass *SuperRC =
927 TRI->getLargestLegalSuperClass(RC);
928 if (!SuperRC) {
929 report("No largest legal super class exists.", MO, MONum);
930 return;
931 }
932 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
933 if (!DRC) {
934 report("No matching super-reg register class.", MO, MONum);
935 return;
936 }
937 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000938 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000939 report("Illegal virtual register for instruction", MO, MONum);
940 *OS << "Expected a " << DRC->getName() << " register, but got a "
941 << RC->getName() << " register\n";
942 }
943 }
944 }
945 }
946 break;
947 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000948
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000949 case MachineOperand::MO_RegisterMask:
950 regMasks.push_back(MO->getRegMask());
951 break;
952
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000953 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000954 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
955 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000956 break;
957
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000958 case MachineOperand::MO_FrameIndex:
959 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
960 LiveInts && !LiveInts->isNotInMIMap(MI)) {
961 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
962 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000963 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000964 report("Instruction loads from dead spill slot", MO, MONum);
965 *OS << "Live stack: " << LI << '\n';
966 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000967 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000968 report("Instruction stores to dead spill slot", MO, MONum);
969 *OS << "Live stack: " << LI << '\n';
970 }
971 }
972 break;
973
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000974 default:
975 break;
976 }
977}
978
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000979void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
980 const MachineInstr *MI = MO->getParent();
981 const unsigned Reg = MO->getReg();
982
983 // Both use and def operands can read a register.
984 if (MO->readsReg()) {
985 regsLiveInButUnused.erase(Reg);
986
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000987 if (MO->isKill())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000988 addRegWithSubRegs(regsKilled, Reg);
989
990 // Check that LiveVars knows this kill.
991 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
992 MO->isKill()) {
993 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
994 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
995 report("Kill missing from LiveVariables", MO, MONum);
996 }
997
998 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +0000999 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
1000 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
1001 // Check the cached regunit intervals.
1002 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1003 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1004 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(*Units)) {
Matthias Braun5649e252013-10-10 21:28:52 +00001005 LiveQueryResult LRQ = LI->Query(UseIdx);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001006 if (!LRQ.valueIn()) {
Matthias Braun331de112013-10-10 21:28:43 +00001007 report("No live segment at use", MO, MONum);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001008 *OS << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
1009 << ' ' << *LI << '\n';
1010 }
1011 if (MO->isKill() && !LRQ.isKill()) {
1012 report("Live range continues after kill flag", MO, MONum);
1013 *OS << PrintRegUnit(*Units, TRI) << ' ' << *LI << '\n';
1014 }
1015 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001016 }
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001017 }
1018
1019 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1020 if (LiveInts->hasInterval(Reg)) {
1021 // This is a virtual register interval.
1022 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun5649e252013-10-10 21:28:52 +00001023 LiveQueryResult LRQ = LI.Query(UseIdx);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001024 if (!LRQ.valueIn()) {
Matthias Braun331de112013-10-10 21:28:43 +00001025 report("No live segment at use", MO, MONum);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001026 *OS << UseIdx << " is not live in " << LI << '\n';
1027 }
1028 // Check for extra kill flags.
1029 // Note that we allow missing kill flags for now.
1030 if (MO->isKill() && !LRQ.isKill()) {
1031 report("Live range continues after kill flag", MO, MONum);
1032 *OS << "Live range: " << LI << '\n';
1033 }
1034 } else {
1035 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001036 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001037 }
1038 }
1039
1040 // Use of a dead register.
1041 if (!regsLive.count(Reg)) {
1042 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1043 // Reserved registers may be used even when 'dead'.
1044 if (!isReserved(Reg))
1045 report("Using an undefined physical register", MO, MONum);
Pete Cooperb97c57a2012-07-19 23:40:38 +00001046 } else if (MRI->def_empty(Reg)) {
1047 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001048 } else {
1049 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1050 // We don't know which virtual registers are live in, so only complain
1051 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1052 // must be live in. PHI instructions are handled separately.
1053 if (MInfo.regsKilled.count(Reg))
1054 report("Using a killed virtual register", MO, MONum);
1055 else if (!MI->isPHI())
1056 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1057 }
1058 }
1059 }
1060
1061 if (MO->isDef()) {
1062 // Register defined.
1063 // TODO: verify that earlyclobber ops are not used.
1064 if (MO->isDead())
1065 addRegWithSubRegs(regsDead, Reg);
1066 else
1067 addRegWithSubRegs(regsDefined, Reg);
1068
1069 // Verify SSA form.
1070 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
1071 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
1072 report("Multiple virtual register defs in SSA form", MO, MONum);
1073
Matthias Braun331de112013-10-10 21:28:43 +00001074 // Check LiveInts for a live segment, but only for virtual registers.
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001075 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1076 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001077 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1078 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001079 if (LiveInts->hasInterval(Reg)) {
1080 const LiveInterval &LI = LiveInts->getInterval(Reg);
1081 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1082 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001083 if (VNI->def != DefIdx) {
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001084 report("Inconsistent valno->def", MO, MONum);
1085 *OS << "Valno " << VNI->id << " is not defined at "
1086 << DefIdx << " in " << LI << '\n';
1087 }
1088 } else {
Matthias Braun331de112013-10-10 21:28:43 +00001089 report("No live segment at def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001090 *OS << DefIdx << " is not live in " << LI << '\n';
1091 }
1092 } else {
1093 report("Virtual register has no Live interval", MO, MONum);
1094 }
1095 }
1096 }
1097}
1098
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001099void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +00001100}
1101
1102// This function gets called after visiting all instructions in a bundle. The
1103// argument points to the bundle header.
1104// Normal stand-alone instructions are also considered 'bundles', and this
1105// function is called for all of them.
1106void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001107 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1108 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001109 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +00001110 // Kill any masked registers.
1111 while (!regMasks.empty()) {
1112 const uint32_t *Mask = regMasks.pop_back_val();
1113 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1114 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1115 MachineOperand::clobbersPhysReg(Mask, *I))
1116 regsDead.push_back(*I);
1117 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001118 set_subtract(regsLive, regsDead); regsDead.clear();
1119 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001120}
1121
1122void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001123MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001124 MBBInfoMap[MBB].regsLiveOut = regsLive;
1125 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +00001126
1127 if (Indexes) {
1128 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1129 if (!(stop > lastIndex)) {
1130 report("Block ends before last instruction index", MBB);
1131 *OS << "Block ends at " << stop
1132 << " last instruction was at " << lastIndex << '\n';
1133 }
1134 lastIndex = stop;
1135 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001136}
1137
1138// Calculate the largest possible vregsPassed sets. These are the registers that
1139// can pass through an MBB live, but may not be live every time. It is assumed
1140// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001141void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001142 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1143 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001144 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001145 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1146 MFI != MFE; ++MFI) {
1147 const MachineBasicBlock &MBB(*MFI);
1148 BBInfo &MInfo = MBBInfoMap[&MBB];
1149 if (!MInfo.reachable)
1150 continue;
1151 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1152 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1153 BBInfo &SInfo = MBBInfoMap[*SuI];
1154 if (SInfo.addPassed(MInfo.regsLiveOut))
1155 todo.insert(*SuI);
1156 }
1157 }
1158
1159 // Iteratively push vregsPassed to successors. This will converge to the same
1160 // final state regardless of DenseSet iteration order.
1161 while (!todo.empty()) {
1162 const MachineBasicBlock *MBB = *todo.begin();
1163 todo.erase(MBB);
1164 BBInfo &MInfo = MBBInfoMap[MBB];
1165 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1166 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1167 if (*SuI == MBB)
1168 continue;
1169 BBInfo &SInfo = MBBInfoMap[*SuI];
1170 if (SInfo.addPassed(MInfo.vregsPassed))
1171 todo.insert(*SuI);
1172 }
1173 }
1174}
1175
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001176// Calculate the set of virtual registers that must be passed through each basic
1177// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001178// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001179void MachineVerifier::calcRegsRequired() {
1180 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001181 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001182 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1183 MFI != MFE; ++MFI) {
1184 const MachineBasicBlock &MBB(*MFI);
1185 BBInfo &MInfo = MBBInfoMap[&MBB];
1186 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1187 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1188 BBInfo &PInfo = MBBInfoMap[*PrI];
1189 if (PInfo.addRequired(MInfo.vregsLiveIn))
1190 todo.insert(*PrI);
1191 }
1192 }
1193
1194 // Iteratively push vregsRequired to predecessors. This will converge to the
1195 // same final state regardless of DenseSet iteration order.
1196 while (!todo.empty()) {
1197 const MachineBasicBlock *MBB = *todo.begin();
1198 todo.erase(MBB);
1199 BBInfo &MInfo = MBBInfoMap[MBB];
1200 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1201 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1202 if (*PrI == MBB)
1203 continue;
1204 BBInfo &SInfo = MBBInfoMap[*PrI];
1205 if (SInfo.addRequired(MInfo.vregsRequired))
1206 todo.insert(*PrI);
1207 }
1208 }
1209}
1210
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001211// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001212// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001213void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001214 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001215 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +00001216 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001217 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001218
1219 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
1220 unsigned Reg = BBI->getOperand(i).getReg();
1221 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
1222 if (!Pre->isSuccessor(MBB))
1223 continue;
1224 seen.insert(Pre);
1225 BBInfo &PrInfo = MBBInfoMap[Pre];
1226 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1227 report("PHI operand is not live-out from predecessor",
1228 &BBI->getOperand(i), i);
1229 }
1230
1231 // Did we see all predecessors?
1232 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1233 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1234 if (!seen.count(*PrI)) {
1235 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +00001236 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001237 << " is a predecessor according to the CFG.\n";
1238 }
1239 }
1240 }
1241}
1242
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001243void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001244 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001245
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001246 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1247 MFI != MFE; ++MFI) {
1248 BBInfo &MInfo = MBBInfoMap[MFI];
1249
1250 // Skip unreachable MBBs.
1251 if (!MInfo.reachable)
1252 continue;
1253
1254 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001255 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001256
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001257 // Now check liveness info if available
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001258 calcRegsRequired();
1259
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001260 // Check for killed virtual registers that should be live out.
1261 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1262 MFI != MFE; ++MFI) {
1263 BBInfo &MInfo = MBBInfoMap[MFI];
1264 for (RegSet::iterator
1265 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1266 ++I)
1267 if (MInfo.regsKilled.count(*I)) {
Bill Wendling96cb1122012-07-19 00:04:14 +00001268 report("Virtual register killed in block, but needed live out.", MFI);
1269 *OS << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001270 << " is used after the block.\n";
1271 }
1272 }
1273
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001274 if (!MF->empty()) {
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001275 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1276 for (RegSet::iterator
1277 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesenff0275e2012-03-10 00:44:11 +00001278 ++I)
1279 report("Virtual register def doesn't dominate all uses.",
1280 MRI->getVRegDef(*I));
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001281 }
1282
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001283 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001284 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001285 if (LiveInts)
1286 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001287}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001288
1289void MachineVerifier::verifyLiveVariables() {
1290 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001291 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1292 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001293 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1294 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1295 MFI != MFE; ++MFI) {
1296 BBInfo &MInfo = MBBInfoMap[MFI];
1297
1298 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1299 if (MInfo.vregsRequired.count(Reg)) {
1300 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1301 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001302 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001303 << " must be live through the block.\n";
1304 }
1305 } else {
1306 if (VI.AliveBlocks.test(MFI->getNumber())) {
1307 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001308 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001309 << " is not needed live through the block.\n";
1310 }
1311 }
1312 }
1313 }
1314}
1315
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001316void MachineVerifier::verifyLiveIntervals() {
1317 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001318 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1319 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001320
1321 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001322 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001323 continue;
1324
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001325 if (!LiveInts->hasInterval(Reg)) {
1326 report("Missing live interval for virtual register", MF);
1327 *OS << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001328 continue;
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001329 }
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001330
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001331 const LiveInterval &LI = LiveInts->getInterval(Reg);
1332 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001333 verifyLiveInterval(LI);
1334 }
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001335
1336 // Verify all the cached regunit intervals.
1337 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
1338 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(i))
1339 verifyLiveInterval(*LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001340}
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001341
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001342void MachineVerifier::verifyLiveIntervalValue(const LiveInterval &LI,
1343 VNInfo *VNI) {
1344 if (VNI->isUnused())
1345 return;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001346
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001347 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001348
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001349 if (!DefVNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001350 report("Valno not live at def and not marked unused", MF, LI);
1351 *OS << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001352 return;
1353 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001354
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001355 if (DefVNI != VNI) {
Matthias Braun331de112013-10-10 21:28:43 +00001356 report("Live segment at def has different valno", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001357 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001358 << " where valno #" << DefVNI->id << " is live\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 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1363 if (!MBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001364 report("Invalid definition index", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001365 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1366 << " in " << LI << '\n';
1367 return;
1368 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001369
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001370 if (VNI->isPHIDef()) {
1371 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001372 report("PHIDef value is not defined at MBB start", MBB, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001373 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001374 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001375 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001376 return;
1377 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001378
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001379 // Non-PHI def.
1380 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1381 if (!MI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001382 report("No instruction at def index", MBB, LI);
1383 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001384 return;
1385 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001386
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001387 bool hasDef = false;
1388 bool isEarlyClobber = false;
1389 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1390 if (!MOI->isReg() || !MOI->isDef())
1391 continue;
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001392 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001393 if (MOI->getReg() != LI.reg)
1394 continue;
1395 } else {
1396 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001397 !TRI->hasRegUnit(MOI->getReg(), LI.reg))
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001398 continue;
1399 }
1400 hasDef = true;
1401 if (MOI->isEarlyClobber())
1402 isEarlyClobber = true;
1403 }
1404
1405 if (!hasDef) {
1406 report("Defining instruction does not modify register", MI);
1407 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1408 }
1409
1410 // Early clobber defs begin at USE slots, but other defs must begin at
1411 // DEF slots.
1412 if (isEarlyClobber) {
1413 if (!VNI->def.isEarlyClobber()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001414 report("Early clobber def must be at an early-clobber slot", MBB, LI);
1415 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001416 }
1417 } else if (!VNI->def.isRegister()) {
1418 report("Non-PHI, non-early clobber def must be at a register slot",
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001419 MBB, LI);
1420 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001421 }
1422}
1423
1424void
1425MachineVerifier::verifyLiveIntervalSegment(const LiveInterval &LI,
1426 LiveInterval::const_iterator I) {
1427 const VNInfo *VNI = I->valno;
Matthias Braun331de112013-10-10 21:28:43 +00001428 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001429
1430 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Matthias Braun331de112013-10-10 21:28:43 +00001431 report("Foreign valno in live segment", MF, LI);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001432 *OS << *I << " has a bad valno\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001433 }
1434
1435 if (VNI->isUnused()) {
Matthias Braun331de112013-10-10 21:28:43 +00001436 report("Live segment valno is marked unused", MF, LI);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001437 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001438 }
1439
1440 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1441 if (!MBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001442 report("Bad start of live segment, no basic block", MF, LI);
1443 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001444 return;
1445 }
1446 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1447 if (I->start != MBBStartIdx && I->start != VNI->def) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001448 report("Live segment must begin at MBB entry or valno def", MBB, LI);
1449 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001450 }
1451
1452 const MachineBasicBlock *EndMBB =
1453 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1454 if (!EndMBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001455 report("Bad end of live segment, no basic block", MF, LI);
1456 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001457 return;
1458 }
1459
1460 // No more checks for live-out segments.
1461 if (I->end == LiveInts->getMBBEndIdx(EndMBB))
1462 return;
1463
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001464 // RegUnit intervals are allowed dead phis.
1465 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) && VNI->isPHIDef() &&
1466 I->start == VNI->def && I->end == VNI->def.getDeadSlot())
1467 return;
1468
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001469 // The live segment is ending inside EndMBB
1470 const MachineInstr *MI =
1471 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1472 if (!MI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001473 report("Live segment doesn't end at a valid instruction", EndMBB, LI);
1474 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001475 return;
1476 }
1477
1478 // The block slot must refer to a basic block boundary.
1479 if (I->end.isBlock()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001480 report("Live segment ends at B slot of an instruction", EndMBB, LI);
1481 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001482 }
1483
1484 if (I->end.isDead()) {
1485 // Segment ends on the dead slot.
1486 // That means there must be a dead def.
1487 if (!SlotIndex::isSameInstr(I->start, I->end)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001488 report("Live segment ending at dead slot spans instructions", EndMBB, LI);
1489 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001490 }
1491 }
1492
1493 // A live segment can only end at an early-clobber slot if it is being
1494 // redefined by an early-clobber def.
1495 if (I->end.isEarlyClobber()) {
1496 if (I+1 == LI.end() || (I+1)->start != I->end) {
1497 report("Live segment ending at early clobber slot must be "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001498 "redefined by an EC def in the same instruction", EndMBB, LI);
1499 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001500 }
1501 }
1502
1503 // The following checks only apply to virtual registers. Physreg liveness
1504 // is too weird to check.
1505 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
Matthias Braun331de112013-10-10 21:28:43 +00001506 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001507 // use, or a dead flag on a def.
1508 bool hasRead = false;
1509 bool hasDeadDef = false;
1510 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1511 if (!MOI->isReg() || MOI->getReg() != LI.reg)
1512 continue;
1513 if (MOI->readsReg())
1514 hasRead = true;
1515 if (MOI->isDef() && MOI->isDead())
1516 hasDeadDef = true;
1517 }
1518
1519 if (I->end.isDead()) {
1520 if (!hasDeadDef) {
1521 report("Instruction doesn't have a dead def operand", MI);
Matthias Braun331de112013-10-10 21:28:43 +00001522 *OS << *I << " in " << LI << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001523 }
1524 } else {
1525 if (!hasRead) {
Matthias Braun331de112013-10-10 21:28:43 +00001526 report("Instruction ending live segment doesn't read the register", MI);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001527 *OS << *I << " in " << LI << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001528 }
1529 }
1530 }
1531
1532 // Now check all the basic blocks in this live segment.
1533 MachineFunction::const_iterator MFI = MBB;
Matthias Braun331de112013-10-10 21:28:43 +00001534 // Is this live segment the beginning of a non-PHIDef VN?
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001535 if (I->start == VNI->def && !VNI->isPHIDef()) {
1536 // Not live-in to any blocks.
1537 if (MBB == EndMBB)
1538 return;
1539 // Skip this block.
1540 ++MFI;
1541 }
1542 for (;;) {
1543 assert(LiveInts->isLiveInToMBB(LI, MFI));
1544 // We don't know how to track physregs into a landing pad.
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001545 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) &&
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001546 MFI->isLandingPad()) {
1547 if (&*MFI == EndMBB)
1548 break;
1549 ++MFI;
1550 continue;
1551 }
1552
1553 // Is VNI a PHI-def in the current block?
1554 bool IsPHI = VNI->isPHIDef() &&
1555 VNI->def == LiveInts->getMBBStartIdx(MFI);
1556
1557 // Check that VNI is live-out of all predecessors.
1558 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1559 PE = MFI->pred_end(); PI != PE; ++PI) {
1560 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1561 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
1562
1563 // All predecessors must have a live-out value.
1564 if (!PVNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001565 report("Register not marked live out of predecessor", *PI, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001566 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1567 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001568 << PEnd << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001569 continue;
1570 }
1571
1572 // Only PHI-defs can take different predecessor values.
1573 if (!IsPHI && PVNI != VNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001574 report("Different value live out of predecessor", *PI, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001575 *OS << "Valno #" << PVNI->id << " live out of BB#"
1576 << (*PI)->getNumber() << '@' << PEnd
1577 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001578 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001579 }
1580 }
1581 if (&*MFI == EndMBB)
1582 break;
1583 ++MFI;
1584 }
1585}
1586
1587void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1588 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1589 I!=E; ++I)
1590 verifyLiveIntervalValue(LI, *I);
1591
1592 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I)
1593 verifyLiveIntervalSegment(LI, I);
1594
1595 // Check the LI only has one connected component.
1596 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1597 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1598 unsigned NumComp = ConEQ.Classify(&LI);
1599 if (NumComp > 1) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001600 report("Multiple connected components in live interval", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001601 for (unsigned comp = 0; comp != NumComp; ++comp) {
1602 *OS << comp << ": valnos";
1603 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1604 E = LI.vni_end(); I!=E; ++I)
1605 if (comp == ConEQ.getEqClass(*I))
1606 *OS << ' ' << (*I)->id;
1607 *OS << '\n';
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001608 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001609 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001610 }
1611}
Manman Ren7310b752013-07-15 21:26:31 +00001612
1613namespace {
1614 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1615 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1616 // value is zero.
1617 // We use a bool plus an integer to capture the stack state.
1618 struct StackStateOfBB {
1619 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1620 ExitIsSetup(false) { }
1621 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1622 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1623 ExitIsSetup(ExitSetup) { }
1624 // Can be negative, which means we are setting up a frame.
1625 int EntryValue;
1626 int ExitValue;
1627 bool EntryIsSetup;
1628 bool ExitIsSetup;
1629 };
1630}
1631
1632/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1633/// by a FrameDestroy <n>, stack adjustments are identical on all
1634/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1635void MachineVerifier::verifyStackFrame() {
1636 int FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1637 int FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
1638
1639 SmallVector<StackStateOfBB, 8> SPState;
1640 SPState.resize(MF->getNumBlockIDs());
1641 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1642
1643 // Visit the MBBs in DFS order.
1644 for (df_ext_iterator<const MachineFunction*,
1645 SmallPtrSet<const MachineBasicBlock*, 8> >
1646 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1647 DFI != DFE; ++DFI) {
1648 const MachineBasicBlock *MBB = *DFI;
1649
1650 StackStateOfBB BBState;
1651 // Check the exit state of the DFS stack predecessor.
1652 if (DFI.getPathLength() >= 2) {
1653 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1654 assert(Reachable.count(StackPred) &&
1655 "DFS stack predecessor is already visited.\n");
1656 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1657 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1658 BBState.ExitValue = BBState.EntryValue;
1659 BBState.ExitIsSetup = BBState.EntryIsSetup;
1660 }
1661
1662 // Update stack state by checking contents of MBB.
1663 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
1664 I != E; ++I) {
1665 if (I->getOpcode() == FrameSetupOpcode) {
1666 // The first operand of a FrameOpcode should be i32.
1667 int Size = I->getOperand(0).getImm();
1668 assert(Size >= 0 &&
1669 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1670
1671 if (BBState.ExitIsSetup)
1672 report("FrameSetup is after another FrameSetup", I);
1673 BBState.ExitValue -= Size;
1674 BBState.ExitIsSetup = true;
1675 }
1676
1677 if (I->getOpcode() == FrameDestroyOpcode) {
1678 // The first operand of a FrameOpcode should be i32.
1679 int Size = I->getOperand(0).getImm();
1680 assert(Size >= 0 &&
1681 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1682
1683 if (!BBState.ExitIsSetup)
1684 report("FrameDestroy is not after a FrameSetup", I);
1685 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1686 BBState.ExitValue;
1687 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
1688 report("FrameDestroy <n> is after FrameSetup <m>", I);
1689 *OS << "FrameDestroy <" << Size << "> is after FrameSetup <"
1690 << AbsSPAdj << ">.\n";
1691 }
1692 BBState.ExitValue += Size;
1693 BBState.ExitIsSetup = false;
1694 }
1695 }
1696 SPState[MBB->getNumber()] = BBState;
1697
1698 // Make sure the exit state of any predecessor is consistent with the entry
1699 // state.
1700 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
1701 E = MBB->pred_end(); I != E; ++I) {
1702 if (Reachable.count(*I) &&
1703 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
1704 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
1705 report("The exit stack state of a predecessor is inconsistent.", MBB);
1706 *OS << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
1707 << SPState[(*I)->getNumber()].ExitValue << ", "
1708 << SPState[(*I)->getNumber()].ExitIsSetup
1709 << "), while BB#" << MBB->getNumber() << " has entry state ("
1710 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
1711 }
1712 }
1713
1714 // Make sure the entry state of any successor is consistent with the exit
1715 // state.
1716 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
1717 E = MBB->succ_end(); I != E; ++I) {
1718 if (Reachable.count(*I) &&
1719 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
1720 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
1721 report("The entry stack state of a successor is inconsistent.", MBB);
1722 *OS << "Successor BB#" << (*I)->getNumber() << " has entry state ("
1723 << SPState[(*I)->getNumber()].EntryValue << ", "
1724 << SPState[(*I)->getNumber()].EntryIsSetup
1725 << "), while BB#" << MBB->getNumber() << " has exit state ("
1726 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
1727 }
1728 }
1729
1730 // Make sure a basic block with return ends with zero stack adjustment.
1731 if (!MBB->empty() && MBB->back().isReturn()) {
1732 if (BBState.ExitIsSetup)
1733 report("A return block ends with a FrameSetup.", MBB);
1734 if (BBState.ExitValue)
1735 report("A return block ends with a nonzero stack adjustment.", MBB);
1736 }
1737 }
1738}