blob: e9bf6173490d7bc3c60431372ae954068c49f4d0 [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);
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000216 void report(const char *msg, const MachineFunction *MF,
217 const LiveRange &LR);
218 void report(const char *msg, const MachineBasicBlock *MBB,
219 const LiveRange &LR);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000220
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000221 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000222
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000223 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000224 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000225 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000226 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000227
228 void calcRegsRequired();
229 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000230 void verifyLiveIntervals();
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +0000231 void verifyLiveInterval(const LiveInterval&);
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000232 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned);
233 void verifyLiveRangeSegment(const LiveRange&,
234 const LiveRange::const_iterator I, unsigned);
235 void verifyLiveRange(const LiveRange&, unsigned);
Manman Ren7310b752013-07-15 21:26:31 +0000236
237 void verifyStackFrame();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000238 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000239
240 struct MachineVerifierPass : public MachineFunctionPass {
241 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000242 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000243
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000244 MachineVerifierPass(const char *b = 0)
245 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000246 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
247 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000248
249 void getAnalysisUsage(AnalysisUsage &AU) const {
250 AU.setPreservesAll();
251 MachineFunctionPass::getAnalysisUsage(AU);
252 }
253
254 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000255 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000256 return false;
257 }
258 };
259
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000260}
261
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000262char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000263INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000264 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000265
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000266FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
267 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000268}
269
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000270void MachineFunction::verify(Pass *p, const char *Banner) const {
271 MachineVerifier(p, Banner)
272 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000273}
274
Chris Lattner17e9edc2009-08-23 02:51:22 +0000275bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
276 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000277 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000278 std::string ErrorInfo;
Rafael Espindolac1b49b52013-07-16 19:44:17 +0000279 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo, sys::fs::F_Append);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000280 if (!ErrorInfo.empty()) {
281 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
282 exit(1);
283 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000284
Chris Lattner17e9edc2009-08-23 02:51:22 +0000285 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000286 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000287 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000288 }
289
290 foundErrors = 0;
291
292 this->MF = &MF;
293 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000294 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000295 TRI = TM->getRegisterInfo();
296 MRI = &MF.getRegInfo();
297
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000298 LiveVars = NULL;
299 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000300 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000301 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000302 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000303 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000304 // We don't want to verify LiveVariables if LiveIntervals is available.
305 if (!LiveInts)
306 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000307 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000308 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000309 }
310
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000311 visitMachineFunctionBefore();
312 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
313 MFI!=MFE; ++MFI) {
314 visitMachineBasicBlockBefore(MFI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000315 // Keep track of the current bundle header.
316 const MachineInstr *CurBundle = 0;
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000317 // Do we expect the next instruction to be part of the same bundle?
318 bool InBundle = false;
319
Evan Chengddfd1372011-12-14 02:11:42 +0000320 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
321 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000322 if (MBBI->getParent() != MFI) {
323 report("Bad instruction parent pointer", MFI);
324 *OS << "Instruction: " << *MBBI;
325 continue;
326 }
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000327
328 // Check for consistent bundle flags.
329 if (InBundle && !MBBI->isBundledWithPred())
330 report("Missing BundledPred flag, "
331 "BundledSucc was set on predecessor", MBBI);
332 if (!InBundle && MBBI->isBundledWithPred())
333 report("BundledPred flag is set, "
334 "but BundledSucc not set on predecessor", MBBI);
335
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000336 // Is this a bundle header?
337 if (!MBBI->isInsideBundle()) {
338 if (CurBundle)
339 visitMachineBundleAfter(CurBundle);
340 CurBundle = MBBI;
341 visitMachineBundleBefore(CurBundle);
342 } else if (!CurBundle)
343 report("No bundle header", MBBI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000344 visitMachineInstrBefore(MBBI);
345 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
346 visitMachineOperand(&MBBI->getOperand(I), I);
347 visitMachineInstrAfter(MBBI);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000348
349 // Was this the last bundled instruction?
350 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000351 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000352 if (CurBundle)
353 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000354 if (InBundle)
355 report("BundledSucc flag set on last instruction in block", &MFI->back());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000356 visitMachineBasicBlockAfter(MFI);
357 }
358 visitMachineFunctionAfter();
359
Chris Lattner17e9edc2009-08-23 02:51:22 +0000360 if (OutFile)
361 delete OutFile;
362 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000363 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000364
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000365 // Clean up.
366 regsLive.clear();
367 regsDefined.clear();
368 regsDead.clear();
369 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000370 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000371 regsLiveInButUnused.clear();
372 MBBInfoMap.clear();
373
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000374 return false; // no changes
375}
376
Chris Lattner372fefe2009-08-23 01:03:30 +0000377void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000378 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000379 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000380 if (!foundErrors++) {
381 if (Banner)
382 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000383 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000384 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000385 *OS << "*** Bad machine code: " << msg << " ***\n"
Craig Topper96601ca2012-08-22 06:07:19 +0000386 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000387}
388
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000389void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000390 assert(MBB);
391 report(msg, MBB->getParent());
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000392 *OS << "- basic block: BB#" << MBB->getNumber()
393 << ' ' << MBB->getName()
Roman Divacky59324292012-09-05 22:26:57 +0000394 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000395 if (Indexes)
396 *OS << " [" << Indexes->getMBBStartIdx(MBB)
397 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
398 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000399}
400
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000401void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000402 assert(MI);
403 report(msg, MI->getParent());
404 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000405 if (Indexes && Indexes->hasIndex(MI))
406 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000407 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000408}
409
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000410void MachineVerifier::report(const char *msg,
411 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000412 assert(MO);
413 report(msg, MO->getParent());
414 *OS << "- operand " << MONum << ": ";
415 MO->print(*OS, TM);
416 *OS << "\n";
417}
418
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000419void MachineVerifier::report(const char *msg, const MachineFunction *MF,
420 const LiveInterval &LI) {
421 report(msg, MF);
422 *OS << "- interval: ";
423 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
424 *OS << PrintReg(LI.reg, TRI);
425 else
426 *OS << PrintRegUnit(LI.reg, TRI);
427 *OS << ' ' << LI << '\n';
428}
429
430void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
431 const LiveInterval &LI) {
432 report(msg, MBB);
433 *OS << "- interval: ";
434 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
435 *OS << PrintReg(LI.reg, TRI);
436 else
437 *OS << PrintRegUnit(LI.reg, TRI);
438 *OS << ' ' << LI << '\n';
439}
440
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000441void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
442 const LiveRange &LR) {
443 report(msg, MBB);
444 *OS << "- liverange: " << LR << "\n";
445}
446
447void MachineVerifier::report(const char *msg, const MachineFunction *MF,
448 const LiveRange &LR) {
449 report(msg, MF);
450 *OS << "- liverange: " << LR << "\n";
451}
452
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000453void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000454 BBInfo &MInfo = MBBInfoMap[MBB];
455 if (!MInfo.reachable) {
456 MInfo.reachable = true;
457 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
458 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
459 markReachable(*SuI);
460 }
461}
462
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000463void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000464 lastIndex = SlotIndex();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000465 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000466
467 // A sub-register of a reserved register is also reserved
468 for (int Reg = regsReserved.find_first(); Reg>=0;
469 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000470 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000471 // FIXME: This should probably be:
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000472 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
473 regsReserved.set(*SubRegs);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000474 }
475 }
Lang Hames03698de2012-02-14 19:17:48 +0000476
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000477 markReachable(&MF->front());
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000478
479 // Build a set of the basic blocks in the function.
480 FunctionBlocks.clear();
481 for (MachineFunction::const_iterator
482 I = MF->begin(), E = MF->end(); I != E; ++I) {
483 FunctionBlocks.insert(I);
484 BBInfo &MInfo = MBBInfoMap[I];
485
486 MInfo.Preds.insert(I->pred_begin(), I->pred_end());
487 if (MInfo.Preds.size() != I->pred_size())
488 report("MBB has duplicate entries in its predecessor list.", I);
489
490 MInfo.Succs.insert(I->succ_begin(), I->succ_end());
491 if (MInfo.Succs.size() != I->succ_size())
492 report("MBB has duplicate entries in its successor list.", I);
493 }
Jakob Stoklund Olesena58d67a2013-04-19 21:40:57 +0000494
495 // Check that the register use lists are sane.
496 MRI->verifyUseLists();
Manman Ren7310b752013-07-15 21:26:31 +0000497
498 verifyStackFrame();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000499}
500
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000501// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000502static bool matchPair(MachineBasicBlock::const_succ_iterator i,
503 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000504 if (*i == a)
505 return *++i == b;
506 if (*i == b)
507 return *++i == a;
508 return false;
509}
510
511void
512MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000513 FirstTerminator = 0;
514
Lang Hames03698de2012-02-14 19:17:48 +0000515 if (MRI->isSSA()) {
516 // If this block has allocatable physical registers live-in, check that
517 // it is an entry block or landing pad.
518 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
519 LE = MBB->livein_end();
520 LI != LE; ++LI) {
521 unsigned reg = *LI;
522 if (isAllocatable(reg) && !MBB->isLandingPad() &&
523 MBB != MBB->getParent()->begin()) {
524 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
525 }
526 }
527 }
528
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000529 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000530 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000531 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000532 E = MBB->succ_end(); I != E; ++I) {
533 if ((*I)->isLandingPad())
534 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000535 if (!FunctionBlocks.count(*I))
536 report("MBB has successor that isn't part of the function.", MBB);
537 if (!MBBInfoMap[*I].Preds.count(MBB)) {
538 report("Inconsistent CFG", MBB);
539 *OS << "MBB is not in the predecessor list of the successor BB#"
540 << (*I)->getNumber() << ".\n";
541 }
542 }
543
544 // Check the predecessor list.
545 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
546 E = MBB->pred_end(); I != E; ++I) {
547 if (!FunctionBlocks.count(*I))
548 report("MBB has predecessor that isn't part of the function.", MBB);
549 if (!MBBInfoMap[*I].Succs.count(MBB)) {
550 report("Inconsistent CFG", MBB);
551 *OS << "MBB is not in the successor list of the predecessor BB#"
552 << (*I)->getNumber() << ".\n";
553 }
Cameron Zwarich2100d212010-12-20 04:19:48 +0000554 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000555
556 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
557 const BasicBlock *BB = MBB->getBasicBlock();
558 if (LandingPadSuccs.size() > 1 &&
559 !(AsmInfo &&
560 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
561 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000562 report("MBB has more than one landing pad successor", MBB);
563
Dan Gohman27920592009-08-27 02:43:49 +0000564 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
565 MachineBasicBlock *TBB = 0, *FBB = 0;
566 SmallVector<MachineOperand, 4> Cond;
567 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
568 TBB, FBB, Cond)) {
569 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
570 // check whether its answers match up with reality.
571 if (!TBB && !FBB) {
572 // Block falls through to its successor.
573 MachineFunction::const_iterator MBBI = MBB;
574 ++MBBI;
575 if (MBBI == MF->end()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000576 // It's possible that the block legitimately ends with a noreturn
577 // call or an unreachable, in which case it won't actually fall
578 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000579 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80fa2009-08-27 18:14:26 +0000580 // It's possible that the block legitimately ends with a noreturn
581 // call or an unreachable, in which case it won't actuall fall
582 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000583 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000584 report("MBB exits via unconditional fall-through but doesn't have "
585 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000586 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000587 report("MBB exits via unconditional fall-through but its successor "
588 "differs from its CFG successor!", MBB);
589 }
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000590 if (!MBB->empty() && getBundleStart(&MBB->back())->isBarrier() &&
591 !TII->isPredicated(getBundleStart(&MBB->back()))) {
Dan Gohman27920592009-08-27 02:43:49 +0000592 report("MBB exits via unconditional fall-through but ends with a "
593 "barrier instruction!", MBB);
594 }
595 if (!Cond.empty()) {
596 report("MBB exits via unconditional fall-through but has a condition!",
597 MBB);
598 }
599 } else if (TBB && !FBB && Cond.empty()) {
600 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000601 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000602 report("MBB exits via unconditional branch but doesn't have "
603 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000604 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000605 report("MBB exits via unconditional branch but the CFG "
606 "successor doesn't match the actual successor!", MBB);
607 }
608 if (MBB->empty()) {
609 report("MBB exits via unconditional branch but doesn't contain "
610 "any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000611 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000612 report("MBB exits via unconditional branch but doesn't end with a "
613 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000614 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000615 report("MBB exits via unconditional branch but the branch isn't a "
616 "terminator instruction!", MBB);
617 }
618 } else if (TBB && !FBB && !Cond.empty()) {
619 // Block conditionally branches somewhere, otherwise falls through.
620 MachineFunction::const_iterator MBBI = MBB;
621 ++MBBI;
622 if (MBBI == MF->end()) {
623 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko344df792012-12-19 22:13:01 +0000624 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000625 // A conditional branch with only one successor is weird, but allowed.
626 if (&*MBBI != TBB)
627 report("MBB exits via conditional branch/fall-through but only has "
628 "one CFG successor!", MBB);
629 else if (TBB != *MBB->succ_begin())
630 report("MBB exits via conditional branch/fall-through but the CFG "
631 "successor don't match the actual successor!", MBB);
632 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000633 report("MBB exits via conditional branch/fall-through but doesn't have "
634 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000635 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000636 report("MBB exits via conditional branch/fall-through but the CFG "
637 "successors don't match the actual successors!", MBB);
638 }
639 if (MBB->empty()) {
640 report("MBB exits via conditional branch/fall-through but doesn't "
641 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000642 } else if (getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000643 report("MBB exits via conditional branch/fall-through but ends with a "
644 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000645 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000646 report("MBB exits via conditional branch/fall-through but the branch "
647 "isn't a terminator instruction!", MBB);
648 }
649 } else if (TBB && FBB) {
650 // Block conditionally branches somewhere, otherwise branches
651 // somewhere else.
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000652 if (MBB->succ_size() == 1) {
653 // A conditional branch with only one successor is weird, but allowed.
654 if (FBB != TBB)
655 report("MBB exits via conditional branch/branch through but only has "
656 "one CFG successor!", MBB);
657 else if (TBB != *MBB->succ_begin())
658 report("MBB exits via conditional branch/branch through but the CFG "
659 "successor don't match the actual successor!", MBB);
660 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000661 report("MBB exits via conditional branch/branch but doesn't have "
662 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000663 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000664 report("MBB exits via conditional branch/branch but the CFG "
665 "successors don't match the actual successors!", MBB);
666 }
667 if (MBB->empty()) {
668 report("MBB exits via conditional branch/branch but doesn't "
669 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000670 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000671 report("MBB exits via conditional branch/branch but doesn't end with a "
672 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000673 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000674 report("MBB exits via conditional branch/branch but the branch "
675 "isn't a terminator instruction!", MBB);
676 }
677 if (Cond.empty()) {
678 report("MBB exits via conditinal branch/branch but there's no "
679 "condition!", MBB);
680 }
681 } else {
682 report("AnalyzeBranch returned invalid data!", MBB);
683 }
684 }
685
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000686 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000687 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000688 E = MBB->livein_end(); I != E; ++I) {
689 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
690 report("MBB live-in list contains non-physical register", MBB);
691 continue;
692 }
Chad Rosier62c320a2013-05-22 23:17:36 +0000693 for (MCSubRegIterator SubRegs(*I, TRI, /*IncludeSelf=*/true);
694 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000695 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000696 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000697 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000698
699 const MachineFrameInfo *MFI = MF->getFrameInfo();
700 assert(MFI && "Function has no frame info");
701 BitVector PR = MFI->getPristineRegs(MBB);
702 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosier62c320a2013-05-22 23:17:36 +0000703 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
704 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000705 regsLive.insert(*SubRegs);
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000706 }
707
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000708 regsKilled.clear();
709 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000710
711 if (Indexes)
712 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000713}
714
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000715// This function gets called for all bundle headers, including normal
716// stand-alone unbundled instructions.
717void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
718 if (Indexes && Indexes->hasIndex(MI)) {
719 SlotIndex idx = Indexes->getInstructionIndex(MI);
720 if (!(idx > lastIndex)) {
721 report("Instruction index out of order", MI);
722 *OS << "Last instruction was at " << lastIndex << '\n';
723 }
724 lastIndex = idx;
725 }
Pete Cooper83569cb2012-06-07 17:41:39 +0000726
727 // Ensure non-terminators don't follow terminators.
728 // Ignore predicated terminators formed by if conversion.
729 // FIXME: If conversion shouldn't need to violate this rule.
730 if (MI->isTerminator() && !TII->isPredicated(MI)) {
731 if (!FirstTerminator)
732 FirstTerminator = MI;
733 } else if (FirstTerminator) {
734 report("Non-terminator instruction after the first terminator", MI);
735 *OS << "First terminator was:\t" << *FirstTerminator;
736 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000737}
738
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000739// The operands on an INLINEASM instruction must follow a template.
740// Verify that the flag operands make sense.
741void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
742 // The first two operands on INLINEASM are the asm string and global flags.
743 if (MI->getNumOperands() < 2) {
744 report("Too few operands on inline asm", MI);
745 return;
746 }
747 if (!MI->getOperand(0).isSymbol())
748 report("Asm string must be an external symbol", MI);
749 if (!MI->getOperand(1).isImm())
750 report("Asm flags must be an immediate", MI);
Chad Rosier3d716882012-10-30 19:11:54 +0000751 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
752 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
753 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000754 report("Unknown asm flags", &MI->getOperand(1), 1);
755
756 assert(InlineAsm::MIOp_FirstOperand == 2 && "Asm format changed");
757
758 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
759 unsigned NumOps;
760 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
761 const MachineOperand &MO = MI->getOperand(OpNo);
762 // There may be implicit ops after the fixed operands.
763 if (!MO.isImm())
764 break;
765 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
766 }
767
768 if (OpNo > MI->getNumOperands())
769 report("Missing operands in last group", MI);
770
771 // An optional MDNode follows the groups.
772 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
773 ++OpNo;
774
775 // All trailing operands must be implicit registers.
776 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
777 const MachineOperand &MO = MI->getOperand(OpNo);
778 if (!MO.isReg() || !MO.isImplicit())
779 report("Expected implicit register after groups", &MO, OpNo);
780 }
781}
782
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000783void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000784 const MCInstrDesc &MCID = MI->getDesc();
785 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000786 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000787 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000788 << MI->getNumExplicitOperands() << " given.\n";
789 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000790
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000791 // Check the tied operands.
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000792 if (MI->isInlineAsm())
793 verifyInlineAsm(MI);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000794
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000795 // Check the MachineMemOperands for basic consistency.
796 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
797 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000798 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000799 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000800 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000801 report("Missing mayStore flag", MI);
802 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000803
804 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000805 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000806 if (LiveInts) {
807 bool mapped = !LiveInts->isNotInMIMap(MI);
808 if (MI->isDebugValue()) {
809 if (mapped)
810 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000811 } else if (MI->isInsideBundle()) {
812 if (mapped)
813 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000814 } else {
815 if (!mapped)
816 report("Missing slot index", MI);
817 }
818 }
819
Andrew Trick3be654f2011-09-21 02:20:46 +0000820 StringRef ErrorInfo;
821 if (!TII->verifyInstruction(MI, ErrorInfo))
822 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000823}
824
825void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000826MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000827 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000828 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000829
Evan Chenge837dea2011-06-28 19:10:37 +0000830 // The first MCID.NumDefs operands must be explicit register defines
831 if (MONum < MCID.getNumDefs()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000832 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000833 if (!MO->isReg())
834 report("Explicit definition must be a register", MO, MONum);
Evan Chengcac58aa2012-05-29 19:40:44 +0000835 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000836 report("Explicit definition marked as use", MO, MONum);
837 else if (MO->isImplicit())
838 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000839 } else if (MONum < MCID.getNumOperands()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000840 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopher113a06c2010-11-17 00:55:36 +0000841 // Don't check if it's the last operand in a variadic instruction. See,
842 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000843 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000844 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000845 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braunb38d9872013-10-04 16:53:00 +0000846 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000847 if (MO->isImplicit())
848 report("Explicit operand marked as implicit", MO, MONum);
849 }
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000850
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000851 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
852 if (TiedTo != -1) {
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000853 if (!MO->isReg())
854 report("Tied use must be a register", MO, MONum);
855 else if (!MO->isTied())
856 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000857 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
858 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000859 } else if (MO->isReg() && MO->isTied())
860 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000861 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000862 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000863 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000864 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000865 }
866
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000867 switch (MO->getType()) {
868 case MachineOperand::MO_Register: {
869 const unsigned Reg = MO->getReg();
870 if (!Reg)
871 return;
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000872 if (MRI->tracksLiveness() && !MI->isDebugValue())
873 checkLiveness(MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000874
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000875 // Verify the consistency of tied operands.
876 if (MO->isTied()) {
877 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
878 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
879 if (!OtherMO.isReg())
880 report("Must be tied to a register", MO, MONum);
881 if (!OtherMO.isTied())
882 report("Missing tie flags on tied operand", MO, MONum);
883 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
884 report("Inconsistent tie links", MO, MONum);
885 if (MONum < MCID.getNumDefs()) {
886 if (OtherIdx < MCID.getNumOperands()) {
887 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
888 report("Explicit def tied to explicit use without tie constraint",
889 MO, MONum);
890 } else {
891 if (!OtherMO.isImplicit())
892 report("Explicit def should be tied to implicit use", MO, MONum);
893 }
894 }
895 }
896
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000897 // Verify two-address constraints after leaving SSA form.
898 unsigned DefIdx;
899 if (!MRI->isSSA() && MO->isUse() &&
900 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
901 Reg != MI->getOperand(DefIdx).getReg())
902 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000903
904 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000905 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000906 unsigned SubIdx = MO->getSubReg();
907
908 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000909 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000910 report("Illegal subregister index for physical register", MO, MONum);
911 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000912 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000913 if (const TargetRegisterClass *DRC =
914 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000915 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000916 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000917 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000918 << DRC->getName() << " register.\n";
919 }
920 }
921 } else {
922 // Virtual register.
923 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
924 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000925 const TargetRegisterClass *SRC =
926 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000927 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000928 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000929 *OS << "Register class " << RC->getName()
930 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000931 return;
932 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000933 if (RC != SRC) {
934 report("Invalid register class for subregister index", MO, MONum);
935 *OS << "Register class " << RC->getName()
936 << " does not fully support subreg index " << SubIdx << "\n";
937 return;
938 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000939 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000940 if (const TargetRegisterClass *DRC =
941 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000942 if (SubIdx) {
943 const TargetRegisterClass *SuperRC =
944 TRI->getLargestLegalSuperClass(RC);
945 if (!SuperRC) {
946 report("No largest legal super class exists.", MO, MONum);
947 return;
948 }
949 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
950 if (!DRC) {
951 report("No matching super-reg register class.", MO, MONum);
952 return;
953 }
954 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000955 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000956 report("Illegal virtual register for instruction", MO, MONum);
957 *OS << "Expected a " << DRC->getName() << " register, but got a "
958 << RC->getName() << " register\n";
959 }
960 }
961 }
962 }
963 break;
964 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000965
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000966 case MachineOperand::MO_RegisterMask:
967 regMasks.push_back(MO->getRegMask());
968 break;
969
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000970 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000971 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
972 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000973 break;
974
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000975 case MachineOperand::MO_FrameIndex:
976 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
977 LiveInts && !LiveInts->isNotInMIMap(MI)) {
978 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
979 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000980 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000981 report("Instruction loads from dead spill slot", MO, MONum);
982 *OS << "Live stack: " << LI << '\n';
983 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000984 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000985 report("Instruction stores to dead spill slot", MO, MONum);
986 *OS << "Live stack: " << LI << '\n';
987 }
988 }
989 break;
990
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000991 default:
992 break;
993 }
994}
995
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000996void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
997 const MachineInstr *MI = MO->getParent();
998 const unsigned Reg = MO->getReg();
999
1000 // Both use and def operands can read a register.
1001 if (MO->readsReg()) {
1002 regsLiveInButUnused.erase(Reg);
1003
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +00001004 if (MO->isKill())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001005 addRegWithSubRegs(regsKilled, Reg);
1006
1007 // Check that LiveVars knows this kill.
1008 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1009 MO->isKill()) {
1010 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1011 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
1012 report("Kill missing from LiveVariables", MO, MONum);
1013 }
1014
1015 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001016 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
1017 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
1018 // Check the cached regunit intervals.
1019 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1020 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001021 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units)) {
1022 LiveQueryResult LRQ = LR->Query(UseIdx);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001023 if (!LRQ.valueIn()) {
Matthias Braun331de112013-10-10 21:28:43 +00001024 report("No live segment at use", MO, MONum);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001025 *OS << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001026 << ' ' << *LR << '\n';
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001027 }
1028 if (MO->isKill() && !LRQ.isKill()) {
1029 report("Live range continues after kill flag", MO, MONum);
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001030 *OS << PrintRegUnit(*Units, TRI) << ' ' << *LR << '\n';
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001031 }
1032 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001033 }
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001034 }
1035
1036 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1037 if (LiveInts->hasInterval(Reg)) {
1038 // This is a virtual register interval.
1039 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun5649e252013-10-10 21:28:52 +00001040 LiveQueryResult LRQ = LI.Query(UseIdx);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001041 if (!LRQ.valueIn()) {
Matthias Braun331de112013-10-10 21:28:43 +00001042 report("No live segment at use", MO, MONum);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001043 *OS << UseIdx << " is not live in " << LI << '\n';
1044 }
1045 // Check for extra kill flags.
1046 // Note that we allow missing kill flags for now.
1047 if (MO->isKill() && !LRQ.isKill()) {
1048 report("Live range continues after kill flag", MO, MONum);
1049 *OS << "Live range: " << LI << '\n';
1050 }
1051 } else {
1052 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001053 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001054 }
1055 }
1056
1057 // Use of a dead register.
1058 if (!regsLive.count(Reg)) {
1059 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1060 // Reserved registers may be used even when 'dead'.
1061 if (!isReserved(Reg))
1062 report("Using an undefined physical register", MO, MONum);
Pete Cooperb97c57a2012-07-19 23:40:38 +00001063 } else if (MRI->def_empty(Reg)) {
1064 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001065 } else {
1066 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1067 // We don't know which virtual registers are live in, so only complain
1068 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1069 // must be live in. PHI instructions are handled separately.
1070 if (MInfo.regsKilled.count(Reg))
1071 report("Using a killed virtual register", MO, MONum);
1072 else if (!MI->isPHI())
1073 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1074 }
1075 }
1076 }
1077
1078 if (MO->isDef()) {
1079 // Register defined.
1080 // TODO: verify that earlyclobber ops are not used.
1081 if (MO->isDead())
1082 addRegWithSubRegs(regsDead, Reg);
1083 else
1084 addRegWithSubRegs(regsDefined, Reg);
1085
1086 // Verify SSA form.
1087 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
1088 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
1089 report("Multiple virtual register defs in SSA form", MO, MONum);
1090
Matthias Braun331de112013-10-10 21:28:43 +00001091 // Check LiveInts for a live segment, but only for virtual registers.
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001092 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1093 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001094 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1095 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001096 if (LiveInts->hasInterval(Reg)) {
1097 const LiveInterval &LI = LiveInts->getInterval(Reg);
1098 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1099 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001100 if (VNI->def != DefIdx) {
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001101 report("Inconsistent valno->def", MO, MONum);
1102 *OS << "Valno " << VNI->id << " is not defined at "
1103 << DefIdx << " in " << LI << '\n';
1104 }
1105 } else {
Matthias Braun331de112013-10-10 21:28:43 +00001106 report("No live segment at def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001107 *OS << DefIdx << " is not live in " << LI << '\n';
1108 }
1109 } else {
1110 report("Virtual register has no Live interval", MO, MONum);
1111 }
1112 }
1113 }
1114}
1115
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001116void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +00001117}
1118
1119// This function gets called after visiting all instructions in a bundle. The
1120// argument points to the bundle header.
1121// Normal stand-alone instructions are also considered 'bundles', and this
1122// function is called for all of them.
1123void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001124 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1125 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001126 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +00001127 // Kill any masked registers.
1128 while (!regMasks.empty()) {
1129 const uint32_t *Mask = regMasks.pop_back_val();
1130 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1131 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1132 MachineOperand::clobbersPhysReg(Mask, *I))
1133 regsDead.push_back(*I);
1134 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001135 set_subtract(regsLive, regsDead); regsDead.clear();
1136 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001137}
1138
1139void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001140MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001141 MBBInfoMap[MBB].regsLiveOut = regsLive;
1142 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +00001143
1144 if (Indexes) {
1145 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1146 if (!(stop > lastIndex)) {
1147 report("Block ends before last instruction index", MBB);
1148 *OS << "Block ends at " << stop
1149 << " last instruction was at " << lastIndex << '\n';
1150 }
1151 lastIndex = stop;
1152 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001153}
1154
1155// Calculate the largest possible vregsPassed sets. These are the registers that
1156// can pass through an MBB live, but may not be live every time. It is assumed
1157// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001158void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001159 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1160 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001161 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001162 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1163 MFI != MFE; ++MFI) {
1164 const MachineBasicBlock &MBB(*MFI);
1165 BBInfo &MInfo = MBBInfoMap[&MBB];
1166 if (!MInfo.reachable)
1167 continue;
1168 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1169 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1170 BBInfo &SInfo = MBBInfoMap[*SuI];
1171 if (SInfo.addPassed(MInfo.regsLiveOut))
1172 todo.insert(*SuI);
1173 }
1174 }
1175
1176 // Iteratively push vregsPassed to successors. This will converge to the same
1177 // final state regardless of DenseSet iteration order.
1178 while (!todo.empty()) {
1179 const MachineBasicBlock *MBB = *todo.begin();
1180 todo.erase(MBB);
1181 BBInfo &MInfo = MBBInfoMap[MBB];
1182 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1183 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1184 if (*SuI == MBB)
1185 continue;
1186 BBInfo &SInfo = MBBInfoMap[*SuI];
1187 if (SInfo.addPassed(MInfo.vregsPassed))
1188 todo.insert(*SuI);
1189 }
1190 }
1191}
1192
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001193// Calculate the set of virtual registers that must be passed through each basic
1194// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001195// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001196void MachineVerifier::calcRegsRequired() {
1197 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001198 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001199 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1200 MFI != MFE; ++MFI) {
1201 const MachineBasicBlock &MBB(*MFI);
1202 BBInfo &MInfo = MBBInfoMap[&MBB];
1203 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1204 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1205 BBInfo &PInfo = MBBInfoMap[*PrI];
1206 if (PInfo.addRequired(MInfo.vregsLiveIn))
1207 todo.insert(*PrI);
1208 }
1209 }
1210
1211 // Iteratively push vregsRequired to predecessors. This will converge to the
1212 // same final state regardless of DenseSet iteration order.
1213 while (!todo.empty()) {
1214 const MachineBasicBlock *MBB = *todo.begin();
1215 todo.erase(MBB);
1216 BBInfo &MInfo = MBBInfoMap[MBB];
1217 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1218 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1219 if (*PrI == MBB)
1220 continue;
1221 BBInfo &SInfo = MBBInfoMap[*PrI];
1222 if (SInfo.addRequired(MInfo.vregsRequired))
1223 todo.insert(*PrI);
1224 }
1225 }
1226}
1227
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001228// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001229// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001230void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001231 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001232 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +00001233 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001234 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001235
1236 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
1237 unsigned Reg = BBI->getOperand(i).getReg();
1238 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
1239 if (!Pre->isSuccessor(MBB))
1240 continue;
1241 seen.insert(Pre);
1242 BBInfo &PrInfo = MBBInfoMap[Pre];
1243 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1244 report("PHI operand is not live-out from predecessor",
1245 &BBI->getOperand(i), i);
1246 }
1247
1248 // Did we see all predecessors?
1249 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1250 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1251 if (!seen.count(*PrI)) {
1252 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +00001253 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001254 << " is a predecessor according to the CFG.\n";
1255 }
1256 }
1257 }
1258}
1259
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001260void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001261 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001262
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001263 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1264 MFI != MFE; ++MFI) {
1265 BBInfo &MInfo = MBBInfoMap[MFI];
1266
1267 // Skip unreachable MBBs.
1268 if (!MInfo.reachable)
1269 continue;
1270
1271 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001272 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001273
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001274 // Now check liveness info if available
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001275 calcRegsRequired();
1276
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001277 // Check for killed virtual registers that should be live out.
1278 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1279 MFI != MFE; ++MFI) {
1280 BBInfo &MInfo = MBBInfoMap[MFI];
1281 for (RegSet::iterator
1282 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1283 ++I)
1284 if (MInfo.regsKilled.count(*I)) {
Bill Wendling96cb1122012-07-19 00:04:14 +00001285 report("Virtual register killed in block, but needed live out.", MFI);
1286 *OS << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001287 << " is used after the block.\n";
1288 }
1289 }
1290
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001291 if (!MF->empty()) {
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001292 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1293 for (RegSet::iterator
1294 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesenff0275e2012-03-10 00:44:11 +00001295 ++I)
1296 report("Virtual register def doesn't dominate all uses.",
1297 MRI->getVRegDef(*I));
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001298 }
1299
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001300 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001301 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001302 if (LiveInts)
1303 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001304}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001305
1306void MachineVerifier::verifyLiveVariables() {
1307 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001308 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1309 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001310 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1311 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1312 MFI != MFE; ++MFI) {
1313 BBInfo &MInfo = MBBInfoMap[MFI];
1314
1315 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1316 if (MInfo.vregsRequired.count(Reg)) {
1317 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1318 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001319 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001320 << " must be live through the block.\n";
1321 }
1322 } else {
1323 if (VI.AliveBlocks.test(MFI->getNumber())) {
1324 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001325 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001326 << " is not needed live through the block.\n";
1327 }
1328 }
1329 }
1330 }
1331}
1332
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001333void MachineVerifier::verifyLiveIntervals() {
1334 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001335 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1336 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001337
1338 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001339 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001340 continue;
1341
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001342 if (!LiveInts->hasInterval(Reg)) {
1343 report("Missing live interval for virtual register", MF);
1344 *OS << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001345 continue;
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001346 }
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001347
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001348 const LiveInterval &LI = LiveInts->getInterval(Reg);
1349 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001350 verifyLiveInterval(LI);
1351 }
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001352
1353 // Verify all the cached regunit intervals.
1354 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001355 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1356 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001357}
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001358
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001359void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
1360 const VNInfo *VNI,
1361 unsigned Reg) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001362 if (VNI->isUnused())
1363 return;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001364
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001365 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001366
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001367 if (!DefVNI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001368 report("Valno not live at def and not marked unused", MF, LR);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001369 *OS << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001370 return;
1371 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001372
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001373 if (DefVNI != VNI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001374 report("Live segment at def has different valno", MF, LR);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001375 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001376 << " where valno #" << DefVNI->id << " is live\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001377 return;
1378 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001379
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001380 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1381 if (!MBB) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001382 report("Invalid definition index", MF, LR);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001383 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001384 << " in " << LR << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001385 return;
1386 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001387
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001388 if (VNI->isPHIDef()) {
1389 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001390 report("PHIDef value is not defined at MBB start", MBB, LR);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001391 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001392 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001393 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001394 return;
1395 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001396
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001397 // Non-PHI def.
1398 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1399 if (!MI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001400 report("No instruction at def index", MBB, LR);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001401 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001402 return;
1403 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001404
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001405 if (Reg != 0) {
1406 bool hasDef = false;
1407 bool isEarlyClobber = false;
1408 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1409 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001410 continue;
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001411 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1412 if (MOI->getReg() != Reg)
1413 continue;
1414 } else {
1415 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1416 !TRI->hasRegUnit(MOI->getReg(), Reg))
1417 continue;
1418 }
1419 hasDef = true;
1420 if (MOI->isEarlyClobber())
1421 isEarlyClobber = true;
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001422 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001423
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001424 if (!hasDef) {
1425 report("Defining instruction does not modify register", MI);
1426 *OS << "Valno #" << VNI->id << " in " << LR << '\n';
1427 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001428
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001429 // Early clobber defs begin at USE slots, but other defs must begin at
1430 // DEF slots.
1431 if (isEarlyClobber) {
1432 if (!VNI->def.isEarlyClobber()) {
1433 report("Early clobber def must be at an early-clobber slot", MBB, LR);
1434 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
1435 }
1436 } else if (!VNI->def.isRegister()) {
1437 report("Non-PHI, non-early clobber def must be at a register slot",
1438 MBB, LR);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001439 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001440 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001441 }
1442}
1443
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001444void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1445 const LiveRange::const_iterator I,
1446 unsigned Reg) {
1447 const LiveRange::Segment &S = *I;
1448 const VNInfo *VNI = S.valno;
Matthias Braun331de112013-10-10 21:28:43 +00001449 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001450
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001451 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
1452 report("Foreign valno in live segment", MF, LR);
1453 *OS << S << " has a bad valno\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001454 }
1455
1456 if (VNI->isUnused()) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001457 report("Live segment valno is marked unused", MF, LR);
1458 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001459 }
1460
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001461 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001462 if (!MBB) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001463 report("Bad start of live segment, no basic block", MF, LR);
1464 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001465 return;
1466 }
1467 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001468 if (S.start != MBBStartIdx && S.start != VNI->def) {
1469 report("Live segment must begin at MBB entry or valno def", MBB, LR);
1470 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001471 }
1472
1473 const MachineBasicBlock *EndMBB =
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001474 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001475 if (!EndMBB) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001476 report("Bad end of live segment, no basic block", MF, LR);
1477 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001478 return;
1479 }
1480
1481 // No more checks for live-out segments.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001482 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001483 return;
1484
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001485 // RegUnit intervals are allowed dead phis.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001486 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1487 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001488 return;
1489
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001490 // The live segment is ending inside EndMBB
1491 const MachineInstr *MI =
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001492 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001493 if (!MI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001494 report("Live segment doesn't end at a valid instruction", EndMBB, LR);
1495 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001496 return;
1497 }
1498
1499 // The block slot must refer to a basic block boundary.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001500 if (S.end.isBlock()) {
1501 report("Live segment ends at B slot of an instruction", EndMBB, LR);
1502 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001503 }
1504
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001505 if (S.end.isDead()) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001506 // Segment ends on the dead slot.
1507 // That means there must be a dead def.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001508 if (!SlotIndex::isSameInstr(S.start, S.end)) {
1509 report("Live segment ending at dead slot spans instructions", EndMBB, LR);
1510 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001511 }
1512 }
1513
1514 // A live segment can only end at an early-clobber slot if it is being
1515 // redefined by an early-clobber def.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001516 if (S.end.isEarlyClobber()) {
1517 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001518 report("Live segment ending at early clobber slot must be "
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001519 "redefined by an EC def in the same instruction", EndMBB, LR);
1520 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001521 }
1522 }
1523
1524 // The following checks only apply to virtual registers. Physreg liveness
1525 // is too weird to check.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001526 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun331de112013-10-10 21:28:43 +00001527 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001528 // use, or a dead flag on a def.
1529 bool hasRead = false;
1530 bool hasDeadDef = false;
1531 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001532 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001533 continue;
1534 if (MOI->readsReg())
1535 hasRead = true;
1536 if (MOI->isDef() && MOI->isDead())
1537 hasDeadDef = true;
1538 }
1539
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001540 if (S.end.isDead()) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001541 if (!hasDeadDef) {
1542 report("Instruction doesn't have a dead def operand", MI);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001543 *OS << S << " in " << LR << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001544 }
1545 } else {
1546 if (!hasRead) {
Matthias Braun331de112013-10-10 21:28:43 +00001547 report("Instruction ending live segment doesn't read the register", MI);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001548 *OS << S << " in " << LR << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001549 }
1550 }
1551 }
1552
1553 // Now check all the basic blocks in this live segment.
1554 MachineFunction::const_iterator MFI = MBB;
Matthias Braun331de112013-10-10 21:28:43 +00001555 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001556 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001557 // Not live-in to any blocks.
1558 if (MBB == EndMBB)
1559 return;
1560 // Skip this block.
1561 ++MFI;
1562 }
1563 for (;;) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001564 assert(LiveInts->isLiveInToMBB(LR, MFI));
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001565 // We don't know how to track physregs into a landing pad.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001566 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001567 MFI->isLandingPad()) {
1568 if (&*MFI == EndMBB)
1569 break;
1570 ++MFI;
1571 continue;
1572 }
1573
1574 // Is VNI a PHI-def in the current block?
1575 bool IsPHI = VNI->isPHIDef() &&
1576 VNI->def == LiveInts->getMBBStartIdx(MFI);
1577
1578 // Check that VNI is live-out of all predecessors.
1579 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1580 PE = MFI->pred_end(); PI != PE; ++PI) {
1581 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001582 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001583
1584 // All predecessors must have a live-out value.
1585 if (!PVNI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001586 report("Register not marked live out of predecessor", *PI, LR);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001587 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1588 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001589 << PEnd << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001590 continue;
1591 }
1592
1593 // Only PHI-defs can take different predecessor values.
1594 if (!IsPHI && PVNI != VNI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001595 report("Different value live out of predecessor", *PI, LR);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001596 *OS << "Valno #" << PVNI->id << " live out of BB#"
1597 << (*PI)->getNumber() << '@' << PEnd
1598 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001599 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001600 }
1601 }
1602 if (&*MFI == EndMBB)
1603 break;
1604 ++MFI;
1605 }
1606}
1607
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001608void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg) {
1609 for (LiveRange::const_vni_iterator I = LR.vni_begin(), E = LR.vni_end();
1610 I != E; ++I)
1611 verifyLiveRangeValue(LR, *I, Reg);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001612
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001613 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
1614 verifyLiveRangeSegment(LR, I, Reg);
1615}
1616
1617void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1618 verifyLiveRange(LI, LI.reg);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001619
1620 // Check the LI only has one connected component.
1621 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1622 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1623 unsigned NumComp = ConEQ.Classify(&LI);
1624 if (NumComp > 1) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001625 report("Multiple connected components in live interval", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001626 for (unsigned comp = 0; comp != NumComp; ++comp) {
1627 *OS << comp << ": valnos";
1628 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1629 E = LI.vni_end(); I!=E; ++I)
1630 if (comp == ConEQ.getEqClass(*I))
1631 *OS << ' ' << (*I)->id;
1632 *OS << '\n';
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001633 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001634 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001635 }
1636}
Manman Ren7310b752013-07-15 21:26:31 +00001637
1638namespace {
1639 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1640 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1641 // value is zero.
1642 // We use a bool plus an integer to capture the stack state.
1643 struct StackStateOfBB {
1644 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1645 ExitIsSetup(false) { }
1646 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1647 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1648 ExitIsSetup(ExitSetup) { }
1649 // Can be negative, which means we are setting up a frame.
1650 int EntryValue;
1651 int ExitValue;
1652 bool EntryIsSetup;
1653 bool ExitIsSetup;
1654 };
1655}
1656
1657/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1658/// by a FrameDestroy <n>, stack adjustments are identical on all
1659/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1660void MachineVerifier::verifyStackFrame() {
1661 int FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1662 int FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
1663
1664 SmallVector<StackStateOfBB, 8> SPState;
1665 SPState.resize(MF->getNumBlockIDs());
1666 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1667
1668 // Visit the MBBs in DFS order.
1669 for (df_ext_iterator<const MachineFunction*,
1670 SmallPtrSet<const MachineBasicBlock*, 8> >
1671 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1672 DFI != DFE; ++DFI) {
1673 const MachineBasicBlock *MBB = *DFI;
1674
1675 StackStateOfBB BBState;
1676 // Check the exit state of the DFS stack predecessor.
1677 if (DFI.getPathLength() >= 2) {
1678 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1679 assert(Reachable.count(StackPred) &&
1680 "DFS stack predecessor is already visited.\n");
1681 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1682 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1683 BBState.ExitValue = BBState.EntryValue;
1684 BBState.ExitIsSetup = BBState.EntryIsSetup;
1685 }
1686
1687 // Update stack state by checking contents of MBB.
1688 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
1689 I != E; ++I) {
1690 if (I->getOpcode() == FrameSetupOpcode) {
1691 // The first operand of a FrameOpcode should be i32.
1692 int Size = I->getOperand(0).getImm();
1693 assert(Size >= 0 &&
1694 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1695
1696 if (BBState.ExitIsSetup)
1697 report("FrameSetup is after another FrameSetup", I);
1698 BBState.ExitValue -= Size;
1699 BBState.ExitIsSetup = true;
1700 }
1701
1702 if (I->getOpcode() == FrameDestroyOpcode) {
1703 // The first operand of a FrameOpcode should be i32.
1704 int Size = I->getOperand(0).getImm();
1705 assert(Size >= 0 &&
1706 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1707
1708 if (!BBState.ExitIsSetup)
1709 report("FrameDestroy is not after a FrameSetup", I);
1710 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1711 BBState.ExitValue;
1712 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
1713 report("FrameDestroy <n> is after FrameSetup <m>", I);
1714 *OS << "FrameDestroy <" << Size << "> is after FrameSetup <"
1715 << AbsSPAdj << ">.\n";
1716 }
1717 BBState.ExitValue += Size;
1718 BBState.ExitIsSetup = false;
1719 }
1720 }
1721 SPState[MBB->getNumber()] = BBState;
1722
1723 // Make sure the exit state of any predecessor is consistent with the entry
1724 // state.
1725 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
1726 E = MBB->pred_end(); I != E; ++I) {
1727 if (Reachable.count(*I) &&
1728 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
1729 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
1730 report("The exit stack state of a predecessor is inconsistent.", MBB);
1731 *OS << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
1732 << SPState[(*I)->getNumber()].ExitValue << ", "
1733 << SPState[(*I)->getNumber()].ExitIsSetup
1734 << "), while BB#" << MBB->getNumber() << " has entry state ("
1735 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
1736 }
1737 }
1738
1739 // Make sure the entry state of any successor is consistent with the exit
1740 // state.
1741 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
1742 E = MBB->succ_end(); I != E; ++I) {
1743 if (Reachable.count(*I) &&
1744 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
1745 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
1746 report("The entry stack state of a successor is inconsistent.", MBB);
1747 *OS << "Successor BB#" << (*I)->getNumber() << " has entry state ("
1748 << SPState[(*I)->getNumber()].EntryValue << ", "
1749 << SPState[(*I)->getNumber()].EntryIsSetup
1750 << "), while BB#" << MBB->getNumber() << " has exit state ("
1751 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
1752 }
1753 }
1754
1755 // Make sure a basic block with return ends with zero stack adjustment.
1756 if (!MBB->empty() && MBB->back().isReturn()) {
1757 if (BBState.ExitIsSetup)
1758 report("A return block ends with a FrameSetup.", MBB);
1759 if (BBState.ExitValue)
1760 report("A return block ends with a nonzero stack adjustment.", MBB);
1761 }
1762 }
1763}