blob: 4edb604456730b940b38f2505ffee1bee0094f61 [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;
274 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
275 raw_fd_ostream::F_Append);
276 if (!ErrorInfo.empty()) {
277 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
278 exit(1);
279 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000280
Chris Lattner17e9edc2009-08-23 02:51:22 +0000281 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000282 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000283 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000284 }
285
286 foundErrors = 0;
287
288 this->MF = &MF;
289 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000290 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000291 TRI = TM->getRegisterInfo();
292 MRI = &MF.getRegInfo();
293
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000294 LiveVars = NULL;
295 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000296 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000297 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000298 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000299 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000300 // We don't want to verify LiveVariables if LiveIntervals is available.
301 if (!LiveInts)
302 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000303 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000304 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000305 }
306
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000307 visitMachineFunctionBefore();
308 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
309 MFI!=MFE; ++MFI) {
310 visitMachineBasicBlockBefore(MFI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000311 // Keep track of the current bundle header.
312 const MachineInstr *CurBundle = 0;
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000313 // Do we expect the next instruction to be part of the same bundle?
314 bool InBundle = false;
315
Evan Chengddfd1372011-12-14 02:11:42 +0000316 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
317 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000318 if (MBBI->getParent() != MFI) {
319 report("Bad instruction parent pointer", MFI);
320 *OS << "Instruction: " << *MBBI;
321 continue;
322 }
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000323
324 // Check for consistent bundle flags.
325 if (InBundle && !MBBI->isBundledWithPred())
326 report("Missing BundledPred flag, "
327 "BundledSucc was set on predecessor", MBBI);
328 if (!InBundle && MBBI->isBundledWithPred())
329 report("BundledPred flag is set, "
330 "but BundledSucc not set on predecessor", MBBI);
331
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000332 // Is this a bundle header?
333 if (!MBBI->isInsideBundle()) {
334 if (CurBundle)
335 visitMachineBundleAfter(CurBundle);
336 CurBundle = MBBI;
337 visitMachineBundleBefore(CurBundle);
338 } else if (!CurBundle)
339 report("No bundle header", MBBI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000340 visitMachineInstrBefore(MBBI);
341 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
342 visitMachineOperand(&MBBI->getOperand(I), I);
343 visitMachineInstrAfter(MBBI);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000344
345 // Was this the last bundled instruction?
346 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000347 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000348 if (CurBundle)
349 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000350 if (InBundle)
351 report("BundledSucc flag set on last instruction in block", &MFI->back());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000352 visitMachineBasicBlockAfter(MFI);
353 }
354 visitMachineFunctionAfter();
355
Chris Lattner17e9edc2009-08-23 02:51:22 +0000356 if (OutFile)
357 delete OutFile;
358 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000359 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000360
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000361 // Clean up.
362 regsLive.clear();
363 regsDefined.clear();
364 regsDead.clear();
365 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000366 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000367 regsLiveInButUnused.clear();
368 MBBInfoMap.clear();
369
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000370 return false; // no changes
371}
372
Chris Lattner372fefe2009-08-23 01:03:30 +0000373void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000374 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000375 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000376 if (!foundErrors++) {
377 if (Banner)
378 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000379 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000380 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000381 *OS << "*** Bad machine code: " << msg << " ***\n"
Craig Topper96601ca2012-08-22 06:07:19 +0000382 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000383}
384
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000385void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000386 assert(MBB);
387 report(msg, MBB->getParent());
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000388 *OS << "- basic block: BB#" << MBB->getNumber()
389 << ' ' << MBB->getName()
Roman Divacky59324292012-09-05 22:26:57 +0000390 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000391 if (Indexes)
392 *OS << " [" << Indexes->getMBBStartIdx(MBB)
393 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
394 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000395}
396
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000397void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000398 assert(MI);
399 report(msg, MI->getParent());
400 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000401 if (Indexes && Indexes->hasIndex(MI))
402 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000403 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000404}
405
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000406void MachineVerifier::report(const char *msg,
407 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000408 assert(MO);
409 report(msg, MO->getParent());
410 *OS << "- operand " << MONum << ": ";
411 MO->print(*OS, TM);
412 *OS << "\n";
413}
414
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000415void MachineVerifier::report(const char *msg, const MachineFunction *MF,
416 const LiveInterval &LI) {
417 report(msg, MF);
418 *OS << "- interval: ";
419 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
420 *OS << PrintReg(LI.reg, TRI);
421 else
422 *OS << PrintRegUnit(LI.reg, TRI);
423 *OS << ' ' << LI << '\n';
424}
425
426void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
427 const LiveInterval &LI) {
428 report(msg, MBB);
429 *OS << "- interval: ";
430 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
431 *OS << PrintReg(LI.reg, TRI);
432 else
433 *OS << PrintRegUnit(LI.reg, TRI);
434 *OS << ' ' << LI << '\n';
435}
436
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000437void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000438 BBInfo &MInfo = MBBInfoMap[MBB];
439 if (!MInfo.reachable) {
440 MInfo.reachable = true;
441 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
442 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
443 markReachable(*SuI);
444 }
445}
446
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000447void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000448 lastIndex = SlotIndex();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000449 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000450
451 // A sub-register of a reserved register is also reserved
452 for (int Reg = regsReserved.find_first(); Reg>=0;
453 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000454 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000455 // FIXME: This should probably be:
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000456 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
457 regsReserved.set(*SubRegs);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000458 }
459 }
Lang Hames03698de2012-02-14 19:17:48 +0000460
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000461 markReachable(&MF->front());
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000462
463 // Build a set of the basic blocks in the function.
464 FunctionBlocks.clear();
465 for (MachineFunction::const_iterator
466 I = MF->begin(), E = MF->end(); I != E; ++I) {
467 FunctionBlocks.insert(I);
468 BBInfo &MInfo = MBBInfoMap[I];
469
470 MInfo.Preds.insert(I->pred_begin(), I->pred_end());
471 if (MInfo.Preds.size() != I->pred_size())
472 report("MBB has duplicate entries in its predecessor list.", I);
473
474 MInfo.Succs.insert(I->succ_begin(), I->succ_end());
475 if (MInfo.Succs.size() != I->succ_size())
476 report("MBB has duplicate entries in its successor list.", I);
477 }
Jakob Stoklund Olesena58d67a2013-04-19 21:40:57 +0000478
479 // Check that the register use lists are sane.
480 MRI->verifyUseLists();
Manman Ren7310b752013-07-15 21:26:31 +0000481
482 verifyStackFrame();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000483}
484
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000485// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000486static bool matchPair(MachineBasicBlock::const_succ_iterator i,
487 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000488 if (*i == a)
489 return *++i == b;
490 if (*i == b)
491 return *++i == a;
492 return false;
493}
494
495void
496MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000497 FirstTerminator = 0;
498
Lang Hames03698de2012-02-14 19:17:48 +0000499 if (MRI->isSSA()) {
500 // If this block has allocatable physical registers live-in, check that
501 // it is an entry block or landing pad.
502 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
503 LE = MBB->livein_end();
504 LI != LE; ++LI) {
505 unsigned reg = *LI;
506 if (isAllocatable(reg) && !MBB->isLandingPad() &&
507 MBB != MBB->getParent()->begin()) {
508 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
509 }
510 }
511 }
512
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000513 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000514 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000515 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000516 E = MBB->succ_end(); I != E; ++I) {
517 if ((*I)->isLandingPad())
518 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000519 if (!FunctionBlocks.count(*I))
520 report("MBB has successor that isn't part of the function.", MBB);
521 if (!MBBInfoMap[*I].Preds.count(MBB)) {
522 report("Inconsistent CFG", MBB);
523 *OS << "MBB is not in the predecessor list of the successor BB#"
524 << (*I)->getNumber() << ".\n";
525 }
526 }
527
528 // Check the predecessor list.
529 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
530 E = MBB->pred_end(); I != E; ++I) {
531 if (!FunctionBlocks.count(*I))
532 report("MBB has predecessor that isn't part of the function.", MBB);
533 if (!MBBInfoMap[*I].Succs.count(MBB)) {
534 report("Inconsistent CFG", MBB);
535 *OS << "MBB is not in the successor list of the predecessor BB#"
536 << (*I)->getNumber() << ".\n";
537 }
Cameron Zwarich2100d212010-12-20 04:19:48 +0000538 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000539
540 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
541 const BasicBlock *BB = MBB->getBasicBlock();
542 if (LandingPadSuccs.size() > 1 &&
543 !(AsmInfo &&
544 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
545 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000546 report("MBB has more than one landing pad successor", MBB);
547
Dan Gohman27920592009-08-27 02:43:49 +0000548 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
549 MachineBasicBlock *TBB = 0, *FBB = 0;
550 SmallVector<MachineOperand, 4> Cond;
551 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
552 TBB, FBB, Cond)) {
553 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
554 // check whether its answers match up with reality.
555 if (!TBB && !FBB) {
556 // Block falls through to its successor.
557 MachineFunction::const_iterator MBBI = MBB;
558 ++MBBI;
559 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000560 // It's possible that the block legitimately ends with a noreturn
561 // call or an unreachable, in which case it won't actually fall
562 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000563 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000564 // It's possible that the block legitimately ends with a noreturn
565 // call or an unreachable, in which case it won't actuall fall
566 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000567 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000568 report("MBB exits via unconditional fall-through but doesn't have "
569 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000570 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000571 report("MBB exits via unconditional fall-through but its successor "
572 "differs from its CFG successor!", MBB);
573 }
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000574 if (!MBB->empty() && getBundleStart(&MBB->back())->isBarrier() &&
575 !TII->isPredicated(getBundleStart(&MBB->back()))) {
Dan Gohman27920592009-08-27 02:43:49 +0000576 report("MBB exits via unconditional fall-through but ends with a "
577 "barrier instruction!", MBB);
578 }
579 if (!Cond.empty()) {
580 report("MBB exits via unconditional fall-through but has a condition!",
581 MBB);
582 }
583 } else if (TBB && !FBB && Cond.empty()) {
584 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000585 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000586 report("MBB exits via unconditional branch but doesn't have "
587 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000588 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000589 report("MBB exits via unconditional branch but the CFG "
590 "successor doesn't match the actual successor!", MBB);
591 }
592 if (MBB->empty()) {
593 report("MBB exits via unconditional branch but doesn't contain "
594 "any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000595 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000596 report("MBB exits via unconditional branch but doesn't end with a "
597 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000598 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000599 report("MBB exits via unconditional branch but the branch isn't a "
600 "terminator instruction!", MBB);
601 }
602 } else if (TBB && !FBB && !Cond.empty()) {
603 // Block conditionally branches somewhere, otherwise falls through.
604 MachineFunction::const_iterator MBBI = MBB;
605 ++MBBI;
606 if (MBBI == MF->end()) {
607 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko344df792012-12-19 22:13:01 +0000608 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000609 // A conditional branch with only one successor is weird, but allowed.
610 if (&*MBBI != TBB)
611 report("MBB exits via conditional branch/fall-through but only has "
612 "one CFG successor!", MBB);
613 else if (TBB != *MBB->succ_begin())
614 report("MBB exits via conditional branch/fall-through but the CFG "
615 "successor don't match the actual successor!", MBB);
616 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000617 report("MBB exits via conditional branch/fall-through but doesn't have "
618 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000619 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000620 report("MBB exits via conditional branch/fall-through but the CFG "
621 "successors don't match the actual successors!", MBB);
622 }
623 if (MBB->empty()) {
624 report("MBB exits via conditional branch/fall-through but doesn't "
625 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000626 } else if (getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000627 report("MBB exits via conditional branch/fall-through but ends with a "
628 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000629 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000630 report("MBB exits via conditional branch/fall-through but the branch "
631 "isn't a terminator instruction!", MBB);
632 }
633 } else if (TBB && FBB) {
634 // Block conditionally branches somewhere, otherwise branches
635 // somewhere else.
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000636 if (MBB->succ_size() == 1) {
637 // A conditional branch with only one successor is weird, but allowed.
638 if (FBB != TBB)
639 report("MBB exits via conditional branch/branch through but only has "
640 "one CFG successor!", MBB);
641 else if (TBB != *MBB->succ_begin())
642 report("MBB exits via conditional branch/branch through but the CFG "
643 "successor don't match the actual successor!", MBB);
644 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000645 report("MBB exits via conditional branch/branch but doesn't have "
646 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000647 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000648 report("MBB exits via conditional branch/branch but the CFG "
649 "successors don't match the actual successors!", MBB);
650 }
651 if (MBB->empty()) {
652 report("MBB exits via conditional branch/branch but doesn't "
653 "contain any instructions!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000654 } else if (!getBundleStart(&MBB->back())->isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000655 report("MBB exits via conditional branch/branch but doesn't end with a "
656 "barrier instruction!", MBB);
Akira Hatanaka6b0cd9b2012-06-14 20:51:13 +0000657 } else if (!getBundleStart(&MBB->back())->isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000658 report("MBB exits via conditional branch/branch but the branch "
659 "isn't a terminator instruction!", MBB);
660 }
661 if (Cond.empty()) {
662 report("MBB exits via conditinal branch/branch but there's no "
663 "condition!", MBB);
664 }
665 } else {
666 report("AnalyzeBranch returned invalid data!", MBB);
667 }
668 }
669
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000670 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000671 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000672 E = MBB->livein_end(); I != E; ++I) {
673 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
674 report("MBB live-in list contains non-physical register", MBB);
675 continue;
676 }
Chad Rosier62c320a2013-05-22 23:17:36 +0000677 for (MCSubRegIterator SubRegs(*I, TRI, /*IncludeSelf=*/true);
678 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000679 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000680 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000681 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000682
683 const MachineFrameInfo *MFI = MF->getFrameInfo();
684 assert(MFI && "Function has no frame info");
685 BitVector PR = MFI->getPristineRegs(MBB);
686 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosier62c320a2013-05-22 23:17:36 +0000687 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
688 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000689 regsLive.insert(*SubRegs);
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000690 }
691
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000692 regsKilled.clear();
693 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000694
695 if (Indexes)
696 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000697}
698
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000699// This function gets called for all bundle headers, including normal
700// stand-alone unbundled instructions.
701void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
702 if (Indexes && Indexes->hasIndex(MI)) {
703 SlotIndex idx = Indexes->getInstructionIndex(MI);
704 if (!(idx > lastIndex)) {
705 report("Instruction index out of order", MI);
706 *OS << "Last instruction was at " << lastIndex << '\n';
707 }
708 lastIndex = idx;
709 }
Pete Cooper83569cb2012-06-07 17:41:39 +0000710
711 // Ensure non-terminators don't follow terminators.
712 // Ignore predicated terminators formed by if conversion.
713 // FIXME: If conversion shouldn't need to violate this rule.
714 if (MI->isTerminator() && !TII->isPredicated(MI)) {
715 if (!FirstTerminator)
716 FirstTerminator = MI;
717 } else if (FirstTerminator) {
718 report("Non-terminator instruction after the first terminator", MI);
719 *OS << "First terminator was:\t" << *FirstTerminator;
720 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000721}
722
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000723// The operands on an INLINEASM instruction must follow a template.
724// Verify that the flag operands make sense.
725void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
726 // The first two operands on INLINEASM are the asm string and global flags.
727 if (MI->getNumOperands() < 2) {
728 report("Too few operands on inline asm", MI);
729 return;
730 }
731 if (!MI->getOperand(0).isSymbol())
732 report("Asm string must be an external symbol", MI);
733 if (!MI->getOperand(1).isImm())
734 report("Asm flags must be an immediate", MI);
Chad Rosier3d716882012-10-30 19:11:54 +0000735 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
736 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
737 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000738 report("Unknown asm flags", &MI->getOperand(1), 1);
739
740 assert(InlineAsm::MIOp_FirstOperand == 2 && "Asm format changed");
741
742 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
743 unsigned NumOps;
744 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
745 const MachineOperand &MO = MI->getOperand(OpNo);
746 // There may be implicit ops after the fixed operands.
747 if (!MO.isImm())
748 break;
749 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
750 }
751
752 if (OpNo > MI->getNumOperands())
753 report("Missing operands in last group", MI);
754
755 // An optional MDNode follows the groups.
756 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
757 ++OpNo;
758
759 // All trailing operands must be implicit registers.
760 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
761 const MachineOperand &MO = MI->getOperand(OpNo);
762 if (!MO.isReg() || !MO.isImplicit())
763 report("Expected implicit register after groups", &MO, OpNo);
764 }
765}
766
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000767void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000768 const MCInstrDesc &MCID = MI->getDesc();
769 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000770 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000771 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000772 << MI->getNumExplicitOperands() << " given.\n";
773 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000774
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000775 // Check the tied operands.
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000776 if (MI->isInlineAsm())
777 verifyInlineAsm(MI);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000778
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000779 // Check the MachineMemOperands for basic consistency.
780 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
781 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000782 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000783 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000784 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000785 report("Missing mayStore flag", MI);
786 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000787
788 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000789 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000790 if (LiveInts) {
791 bool mapped = !LiveInts->isNotInMIMap(MI);
792 if (MI->isDebugValue()) {
793 if (mapped)
794 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000795 } else if (MI->isInsideBundle()) {
796 if (mapped)
797 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000798 } else {
799 if (!mapped)
800 report("Missing slot index", MI);
801 }
802 }
803
Andrew Trick3be654f2011-09-21 02:20:46 +0000804 StringRef ErrorInfo;
805 if (!TII->verifyInstruction(MI, ErrorInfo))
806 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000807}
808
809void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000810MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000811 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000812 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000813
Evan Chenge837dea2011-06-28 19:10:37 +0000814 // The first MCID.NumDefs operands must be explicit register defines
815 if (MONum < MCID.getNumDefs()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000816 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000817 if (!MO->isReg())
818 report("Explicit definition must be a register", MO, MONum);
Evan Chengcac58aa2012-05-29 19:40:44 +0000819 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000820 report("Explicit definition marked as use", MO, MONum);
821 else if (MO->isImplicit())
822 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000823 } else if (MONum < MCID.getNumOperands()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000824 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopher113a06c2010-11-17 00:55:36 +0000825 // Don't check if it's the last operand in a variadic instruction. See,
826 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000827 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000828 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000829 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000830 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000831 if (MO->isImplicit())
832 report("Explicit operand marked as implicit", MO, MONum);
833 }
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000834
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000835 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
836 if (TiedTo != -1) {
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000837 if (!MO->isReg())
838 report("Tied use must be a register", MO, MONum);
839 else if (!MO->isTied())
840 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000841 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
842 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000843 } else if (MO->isReg() && MO->isTied())
844 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000845 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000846 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000847 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000848 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000849 }
850
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000851 switch (MO->getType()) {
852 case MachineOperand::MO_Register: {
853 const unsigned Reg = MO->getReg();
854 if (!Reg)
855 return;
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000856 if (MRI->tracksLiveness() && !MI->isDebugValue())
857 checkLiveness(MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000858
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000859 // Verify the consistency of tied operands.
860 if (MO->isTied()) {
861 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
862 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
863 if (!OtherMO.isReg())
864 report("Must be tied to a register", MO, MONum);
865 if (!OtherMO.isTied())
866 report("Missing tie flags on tied operand", MO, MONum);
867 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
868 report("Inconsistent tie links", MO, MONum);
869 if (MONum < MCID.getNumDefs()) {
870 if (OtherIdx < MCID.getNumOperands()) {
871 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
872 report("Explicit def tied to explicit use without tie constraint",
873 MO, MONum);
874 } else {
875 if (!OtherMO.isImplicit())
876 report("Explicit def should be tied to implicit use", MO, MONum);
877 }
878 }
879 }
880
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000881 // Verify two-address constraints after leaving SSA form.
882 unsigned DefIdx;
883 if (!MRI->isSSA() && MO->isUse() &&
884 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
885 Reg != MI->getOperand(DefIdx).getReg())
886 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000887
888 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000889 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000890 unsigned SubIdx = MO->getSubReg();
891
892 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000893 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000894 report("Illegal subregister index for physical register", MO, MONum);
895 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000896 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000897 if (const TargetRegisterClass *DRC =
898 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000899 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000900 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000901 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000902 << DRC->getName() << " register.\n";
903 }
904 }
905 } else {
906 // Virtual register.
907 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
908 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000909 const TargetRegisterClass *SRC =
910 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000911 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000912 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000913 *OS << "Register class " << RC->getName()
914 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000915 return;
916 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000917 if (RC != SRC) {
918 report("Invalid register class for subregister index", MO, MONum);
919 *OS << "Register class " << RC->getName()
920 << " does not fully support subreg index " << SubIdx << "\n";
921 return;
922 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000923 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000924 if (const TargetRegisterClass *DRC =
925 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000926 if (SubIdx) {
927 const TargetRegisterClass *SuperRC =
928 TRI->getLargestLegalSuperClass(RC);
929 if (!SuperRC) {
930 report("No largest legal super class exists.", MO, MONum);
931 return;
932 }
933 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
934 if (!DRC) {
935 report("No matching super-reg register class.", MO, MONum);
936 return;
937 }
938 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000939 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000940 report("Illegal virtual register for instruction", MO, MONum);
941 *OS << "Expected a " << DRC->getName() << " register, but got a "
942 << RC->getName() << " register\n";
943 }
944 }
945 }
946 }
947 break;
948 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000949
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000950 case MachineOperand::MO_RegisterMask:
951 regMasks.push_back(MO->getRegMask());
952 break;
953
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000954 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000955 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
956 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000957 break;
958
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000959 case MachineOperand::MO_FrameIndex:
960 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
961 LiveInts && !LiveInts->isNotInMIMap(MI)) {
962 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
963 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000964 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000965 report("Instruction loads from dead spill slot", MO, MONum);
966 *OS << "Live stack: " << LI << '\n';
967 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000968 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000969 report("Instruction stores to dead spill slot", MO, MONum);
970 *OS << "Live stack: " << LI << '\n';
971 }
972 }
973 break;
974
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000975 default:
976 break;
977 }
978}
979
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000980void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
981 const MachineInstr *MI = MO->getParent();
982 const unsigned Reg = MO->getReg();
983
984 // Both use and def operands can read a register.
985 if (MO->readsReg()) {
986 regsLiveInButUnused.erase(Reg);
987
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000988 if (MO->isKill())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000989 addRegWithSubRegs(regsKilled, Reg);
990
991 // Check that LiveVars knows this kill.
992 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
993 MO->isKill()) {
994 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
995 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
996 report("Kill missing from LiveVariables", MO, MONum);
997 }
998
999 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001000 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
1001 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
1002 // Check the cached regunit intervals.
1003 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1004 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1005 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(*Units)) {
1006 LiveRangeQuery LRQ(*LI, UseIdx);
1007 if (!LRQ.valueIn()) {
1008 report("No live range at use", MO, MONum);
1009 *OS << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
1010 << ' ' << *LI << '\n';
1011 }
1012 if (MO->isKill() && !LRQ.isKill()) {
1013 report("Live range continues after kill flag", MO, MONum);
1014 *OS << PrintRegUnit(*Units, TRI) << ' ' << *LI << '\n';
1015 }
1016 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001017 }
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001018 }
1019
1020 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1021 if (LiveInts->hasInterval(Reg)) {
1022 // This is a virtual register interval.
1023 const LiveInterval &LI = LiveInts->getInterval(Reg);
1024 LiveRangeQuery LRQ(LI, UseIdx);
1025 if (!LRQ.valueIn()) {
1026 report("No live range at use", MO, MONum);
1027 *OS << UseIdx << " is not live in " << LI << '\n';
1028 }
1029 // Check for extra kill flags.
1030 // Note that we allow missing kill flags for now.
1031 if (MO->isKill() && !LRQ.isKill()) {
1032 report("Live range continues after kill flag", MO, MONum);
1033 *OS << "Live range: " << LI << '\n';
1034 }
1035 } else {
1036 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001037 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001038 }
1039 }
1040
1041 // Use of a dead register.
1042 if (!regsLive.count(Reg)) {
1043 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1044 // Reserved registers may be used even when 'dead'.
1045 if (!isReserved(Reg))
1046 report("Using an undefined physical register", MO, MONum);
Pete Cooperb97c57a2012-07-19 23:40:38 +00001047 } else if (MRI->def_empty(Reg)) {
1048 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001049 } else {
1050 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1051 // We don't know which virtual registers are live in, so only complain
1052 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1053 // must be live in. PHI instructions are handled separately.
1054 if (MInfo.regsKilled.count(Reg))
1055 report("Using a killed virtual register", MO, MONum);
1056 else if (!MI->isPHI())
1057 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1058 }
1059 }
1060 }
1061
1062 if (MO->isDef()) {
1063 // Register defined.
1064 // TODO: verify that earlyclobber ops are not used.
1065 if (MO->isDead())
1066 addRegWithSubRegs(regsDead, Reg);
1067 else
1068 addRegWithSubRegs(regsDefined, Reg);
1069
1070 // Verify SSA form.
1071 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
1072 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
1073 report("Multiple virtual register defs in SSA form", MO, MONum);
1074
1075 // Check LiveInts for a live range, but only for virtual registers.
1076 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1077 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001078 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1079 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001080 if (LiveInts->hasInterval(Reg)) {
1081 const LiveInterval &LI = LiveInts->getInterval(Reg);
1082 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1083 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001084 if (VNI->def != DefIdx) {
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001085 report("Inconsistent valno->def", MO, MONum);
1086 *OS << "Valno " << VNI->id << " is not defined at "
1087 << DefIdx << " in " << LI << '\n';
1088 }
1089 } else {
1090 report("No live range at def", MO, MONum);
1091 *OS << DefIdx << " is not live in " << LI << '\n';
1092 }
1093 } else {
1094 report("Virtual register has no Live interval", MO, MONum);
1095 }
1096 }
1097 }
1098}
1099
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001100void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +00001101}
1102
1103// This function gets called after visiting all instructions in a bundle. The
1104// argument points to the bundle header.
1105// Normal stand-alone instructions are also considered 'bundles', and this
1106// function is called for all of them.
1107void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001108 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1109 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001110 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +00001111 // Kill any masked registers.
1112 while (!regMasks.empty()) {
1113 const uint32_t *Mask = regMasks.pop_back_val();
1114 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1115 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1116 MachineOperand::clobbersPhysReg(Mask, *I))
1117 regsDead.push_back(*I);
1118 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001119 set_subtract(regsLive, regsDead); regsDead.clear();
1120 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001121}
1122
1123void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001124MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001125 MBBInfoMap[MBB].regsLiveOut = regsLive;
1126 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +00001127
1128 if (Indexes) {
1129 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1130 if (!(stop > lastIndex)) {
1131 report("Block ends before last instruction index", MBB);
1132 *OS << "Block ends at " << stop
1133 << " last instruction was at " << lastIndex << '\n';
1134 }
1135 lastIndex = stop;
1136 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001137}
1138
1139// Calculate the largest possible vregsPassed sets. These are the registers that
1140// can pass through an MBB live, but may not be live every time. It is assumed
1141// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001142void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001143 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1144 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001145 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001146 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1147 MFI != MFE; ++MFI) {
1148 const MachineBasicBlock &MBB(*MFI);
1149 BBInfo &MInfo = MBBInfoMap[&MBB];
1150 if (!MInfo.reachable)
1151 continue;
1152 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1153 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1154 BBInfo &SInfo = MBBInfoMap[*SuI];
1155 if (SInfo.addPassed(MInfo.regsLiveOut))
1156 todo.insert(*SuI);
1157 }
1158 }
1159
1160 // Iteratively push vregsPassed to successors. This will converge to the same
1161 // final state regardless of DenseSet iteration order.
1162 while (!todo.empty()) {
1163 const MachineBasicBlock *MBB = *todo.begin();
1164 todo.erase(MBB);
1165 BBInfo &MInfo = MBBInfoMap[MBB];
1166 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1167 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1168 if (*SuI == MBB)
1169 continue;
1170 BBInfo &SInfo = MBBInfoMap[*SuI];
1171 if (SInfo.addPassed(MInfo.vregsPassed))
1172 todo.insert(*SuI);
1173 }
1174 }
1175}
1176
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001177// Calculate the set of virtual registers that must be passed through each basic
1178// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001179// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001180void MachineVerifier::calcRegsRequired() {
1181 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001182 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001183 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1184 MFI != MFE; ++MFI) {
1185 const MachineBasicBlock &MBB(*MFI);
1186 BBInfo &MInfo = MBBInfoMap[&MBB];
1187 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1188 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1189 BBInfo &PInfo = MBBInfoMap[*PrI];
1190 if (PInfo.addRequired(MInfo.vregsLiveIn))
1191 todo.insert(*PrI);
1192 }
1193 }
1194
1195 // Iteratively push vregsRequired to predecessors. This will converge to the
1196 // same final state regardless of DenseSet iteration order.
1197 while (!todo.empty()) {
1198 const MachineBasicBlock *MBB = *todo.begin();
1199 todo.erase(MBB);
1200 BBInfo &MInfo = MBBInfoMap[MBB];
1201 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1202 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1203 if (*PrI == MBB)
1204 continue;
1205 BBInfo &SInfo = MBBInfoMap[*PrI];
1206 if (SInfo.addRequired(MInfo.vregsRequired))
1207 todo.insert(*PrI);
1208 }
1209 }
1210}
1211
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001212// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001213// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001214void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001215 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001216 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +00001217 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001218 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001219
1220 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
1221 unsigned Reg = BBI->getOperand(i).getReg();
1222 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
1223 if (!Pre->isSuccessor(MBB))
1224 continue;
1225 seen.insert(Pre);
1226 BBInfo &PrInfo = MBBInfoMap[Pre];
1227 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1228 report("PHI operand is not live-out from predecessor",
1229 &BBI->getOperand(i), i);
1230 }
1231
1232 // Did we see all predecessors?
1233 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1234 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1235 if (!seen.count(*PrI)) {
1236 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +00001237 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001238 << " is a predecessor according to the CFG.\n";
1239 }
1240 }
1241 }
1242}
1243
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001244void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001245 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001246
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001247 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1248 MFI != MFE; ++MFI) {
1249 BBInfo &MInfo = MBBInfoMap[MFI];
1250
1251 // Skip unreachable MBBs.
1252 if (!MInfo.reachable)
1253 continue;
1254
1255 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001256 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001257
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001258 // Now check liveness info if available
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001259 calcRegsRequired();
1260
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001261 // Check for killed virtual registers that should be live out.
1262 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1263 MFI != MFE; ++MFI) {
1264 BBInfo &MInfo = MBBInfoMap[MFI];
1265 for (RegSet::iterator
1266 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1267 ++I)
1268 if (MInfo.regsKilled.count(*I)) {
Bill Wendling96cb1122012-07-19 00:04:14 +00001269 report("Virtual register killed in block, but needed live out.", MFI);
1270 *OS << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001271 << " is used after the block.\n";
1272 }
1273 }
1274
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001275 if (!MF->empty()) {
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001276 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1277 for (RegSet::iterator
1278 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesenff0275e2012-03-10 00:44:11 +00001279 ++I)
1280 report("Virtual register def doesn't dominate all uses.",
1281 MRI->getVRegDef(*I));
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001282 }
1283
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001284 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001285 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001286 if (LiveInts)
1287 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001288}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001289
1290void MachineVerifier::verifyLiveVariables() {
1291 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001292 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1293 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001294 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1295 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1296 MFI != MFE; ++MFI) {
1297 BBInfo &MInfo = MBBInfoMap[MFI];
1298
1299 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1300 if (MInfo.vregsRequired.count(Reg)) {
1301 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1302 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001303 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001304 << " must be live through the block.\n";
1305 }
1306 } else {
1307 if (VI.AliveBlocks.test(MFI->getNumber())) {
1308 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001309 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001310 << " is not needed live through the block.\n";
1311 }
1312 }
1313 }
1314 }
1315}
1316
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001317void MachineVerifier::verifyLiveIntervals() {
1318 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001319 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1320 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001321
1322 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001323 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001324 continue;
1325
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001326 if (!LiveInts->hasInterval(Reg)) {
1327 report("Missing live interval for virtual register", MF);
1328 *OS << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001329 continue;
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001330 }
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001331
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001332 const LiveInterval &LI = LiveInts->getInterval(Reg);
1333 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001334 verifyLiveInterval(LI);
1335 }
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001336
1337 // Verify all the cached regunit intervals.
1338 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
1339 if (const LiveInterval *LI = LiveInts->getCachedRegUnit(i))
1340 verifyLiveInterval(*LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001341}
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001342
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001343void MachineVerifier::verifyLiveIntervalValue(const LiveInterval &LI,
1344 VNInfo *VNI) {
1345 if (VNI->isUnused())
1346 return;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001347
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001348 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001349
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001350 if (!DefVNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001351 report("Valno not live at def and not marked unused", MF, LI);
1352 *OS << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001353 return;
1354 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001355
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001356 if (DefVNI != VNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001357 report("Live range at def has different valno", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001358 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001359 << " where valno #" << DefVNI->id << " is live\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001360 return;
1361 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001362
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001363 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1364 if (!MBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001365 report("Invalid definition index", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001366 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1367 << " in " << LI << '\n';
1368 return;
1369 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001370
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001371 if (VNI->isPHIDef()) {
1372 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001373 report("PHIDef value is not defined at MBB start", MBB, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001374 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001375 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001376 }
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 // Non-PHI def.
1381 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1382 if (!MI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001383 report("No instruction at def index", MBB, LI);
1384 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001385 return;
1386 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001387
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001388 bool hasDef = false;
1389 bool isEarlyClobber = false;
1390 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1391 if (!MOI->isReg() || !MOI->isDef())
1392 continue;
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001393 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001394 if (MOI->getReg() != LI.reg)
1395 continue;
1396 } else {
1397 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001398 !TRI->hasRegUnit(MOI->getReg(), LI.reg))
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001399 continue;
1400 }
1401 hasDef = true;
1402 if (MOI->isEarlyClobber())
1403 isEarlyClobber = true;
1404 }
1405
1406 if (!hasDef) {
1407 report("Defining instruction does not modify register", MI);
1408 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1409 }
1410
1411 // Early clobber defs begin at USE slots, but other defs must begin at
1412 // DEF slots.
1413 if (isEarlyClobber) {
1414 if (!VNI->def.isEarlyClobber()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001415 report("Early clobber def must be at an early-clobber slot", MBB, LI);
1416 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001417 }
1418 } else if (!VNI->def.isRegister()) {
1419 report("Non-PHI, non-early clobber def must be at a register slot",
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001420 MBB, LI);
1421 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001422 }
1423}
1424
1425void
1426MachineVerifier::verifyLiveIntervalSegment(const LiveInterval &LI,
1427 LiveInterval::const_iterator I) {
1428 const VNInfo *VNI = I->valno;
1429 assert(VNI && "Live range has no valno");
1430
1431 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001432 report("Foreign valno in live range", MF, LI);
1433 *OS << *I << " has a bad valno\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001434 }
1435
1436 if (VNI->isUnused()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001437 report("Live range valno is marked unused", MF, LI);
1438 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001439 }
1440
1441 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1442 if (!MBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001443 report("Bad start of live segment, no basic block", MF, LI);
1444 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001445 return;
1446 }
1447 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1448 if (I->start != MBBStartIdx && I->start != VNI->def) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001449 report("Live segment must begin at MBB entry or valno def", MBB, LI);
1450 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001451 }
1452
1453 const MachineBasicBlock *EndMBB =
1454 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1455 if (!EndMBB) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001456 report("Bad end of live segment, no basic block", MF, LI);
1457 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001458 return;
1459 }
1460
1461 // No more checks for live-out segments.
1462 if (I->end == LiveInts->getMBBEndIdx(EndMBB))
1463 return;
1464
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001465 // RegUnit intervals are allowed dead phis.
1466 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) && VNI->isPHIDef() &&
1467 I->start == VNI->def && I->end == VNI->def.getDeadSlot())
1468 return;
1469
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001470 // The live segment is ending inside EndMBB
1471 const MachineInstr *MI =
1472 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1473 if (!MI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001474 report("Live segment doesn't end at a valid instruction", EndMBB, LI);
1475 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001476 return;
1477 }
1478
1479 // The block slot must refer to a basic block boundary.
1480 if (I->end.isBlock()) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001481 report("Live segment ends at B slot of an instruction", EndMBB, LI);
1482 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001483 }
1484
1485 if (I->end.isDead()) {
1486 // Segment ends on the dead slot.
1487 // That means there must be a dead def.
1488 if (!SlotIndex::isSameInstr(I->start, I->end)) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001489 report("Live segment ending at dead slot spans instructions", EndMBB, LI);
1490 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001491 }
1492 }
1493
1494 // A live segment can only end at an early-clobber slot if it is being
1495 // redefined by an early-clobber def.
1496 if (I->end.isEarlyClobber()) {
1497 if (I+1 == LI.end() || (I+1)->start != I->end) {
1498 report("Live segment ending at early clobber slot must be "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001499 "redefined by an EC def in the same instruction", EndMBB, LI);
1500 *OS << *I << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001501 }
1502 }
1503
1504 // The following checks only apply to virtual registers. Physreg liveness
1505 // is too weird to check.
1506 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1507 // A live range can end with either a redefinition, a kill flag on a
1508 // use, or a dead flag on a def.
1509 bool hasRead = false;
1510 bool hasDeadDef = false;
1511 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1512 if (!MOI->isReg() || MOI->getReg() != LI.reg)
1513 continue;
1514 if (MOI->readsReg())
1515 hasRead = true;
1516 if (MOI->isDef() && MOI->isDead())
1517 hasDeadDef = true;
1518 }
1519
1520 if (I->end.isDead()) {
1521 if (!hasDeadDef) {
1522 report("Instruction doesn't have a dead def operand", MI);
1523 I->print(*OS);
1524 *OS << " in " << LI << '\n';
1525 }
1526 } else {
1527 if (!hasRead) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001528 report("Instruction ending live range doesn't read the register", MI);
1529 *OS << *I << " in " << LI << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001530 }
1531 }
1532 }
1533
1534 // Now check all the basic blocks in this live segment.
1535 MachineFunction::const_iterator MFI = MBB;
1536 // Is this live range the beginning of a non-PHIDef VN?
1537 if (I->start == VNI->def && !VNI->isPHIDef()) {
1538 // Not live-in to any blocks.
1539 if (MBB == EndMBB)
1540 return;
1541 // Skip this block.
1542 ++MFI;
1543 }
1544 for (;;) {
1545 assert(LiveInts->isLiveInToMBB(LI, MFI));
1546 // We don't know how to track physregs into a landing pad.
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001547 if (!TargetRegisterInfo::isVirtualRegister(LI.reg) &&
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001548 MFI->isLandingPad()) {
1549 if (&*MFI == EndMBB)
1550 break;
1551 ++MFI;
1552 continue;
1553 }
1554
1555 // Is VNI a PHI-def in the current block?
1556 bool IsPHI = VNI->isPHIDef() &&
1557 VNI->def == LiveInts->getMBBStartIdx(MFI);
1558
1559 // Check that VNI is live-out of all predecessors.
1560 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1561 PE = MFI->pred_end(); PI != PE; ++PI) {
1562 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1563 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
1564
1565 // All predecessors must have a live-out value.
1566 if (!PVNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001567 report("Register not marked live out of predecessor", *PI, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001568 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1569 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001570 << PEnd << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001571 continue;
1572 }
1573
1574 // Only PHI-defs can take different predecessor values.
1575 if (!IsPHI && PVNI != VNI) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001576 report("Different value live out of predecessor", *PI, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001577 *OS << "Valno #" << PVNI->id << " live out of BB#"
1578 << (*PI)->getNumber() << '@' << PEnd
1579 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001580 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001581 }
1582 }
1583 if (&*MFI == EndMBB)
1584 break;
1585 ++MFI;
1586 }
1587}
1588
1589void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1590 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1591 I!=E; ++I)
1592 verifyLiveIntervalValue(LI, *I);
1593
1594 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I)
1595 verifyLiveIntervalSegment(LI, I);
1596
1597 // Check the LI only has one connected component.
1598 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1599 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1600 unsigned NumComp = ConEQ.Classify(&LI);
1601 if (NumComp > 1) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001602 report("Multiple connected components in live interval", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001603 for (unsigned comp = 0; comp != NumComp; ++comp) {
1604 *OS << comp << ": valnos";
1605 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1606 E = LI.vni_end(); I!=E; ++I)
1607 if (comp == ConEQ.getEqClass(*I))
1608 *OS << ' ' << (*I)->id;
1609 *OS << '\n';
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001610 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001611 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001612 }
1613}
Manman Ren7310b752013-07-15 21:26:31 +00001614
1615namespace {
1616 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1617 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1618 // value is zero.
1619 // We use a bool plus an integer to capture the stack state.
1620 struct StackStateOfBB {
1621 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1622 ExitIsSetup(false) { }
1623 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1624 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1625 ExitIsSetup(ExitSetup) { }
1626 // Can be negative, which means we are setting up a frame.
1627 int EntryValue;
1628 int ExitValue;
1629 bool EntryIsSetup;
1630 bool ExitIsSetup;
1631 };
1632}
1633
1634/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1635/// by a FrameDestroy <n>, stack adjustments are identical on all
1636/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1637void MachineVerifier::verifyStackFrame() {
1638 int FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1639 int FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
1640
1641 SmallVector<StackStateOfBB, 8> SPState;
1642 SPState.resize(MF->getNumBlockIDs());
1643 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1644
1645 // Visit the MBBs in DFS order.
1646 for (df_ext_iterator<const MachineFunction*,
1647 SmallPtrSet<const MachineBasicBlock*, 8> >
1648 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1649 DFI != DFE; ++DFI) {
1650 const MachineBasicBlock *MBB = *DFI;
1651
1652 StackStateOfBB BBState;
1653 // Check the exit state of the DFS stack predecessor.
1654 if (DFI.getPathLength() >= 2) {
1655 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1656 assert(Reachable.count(StackPred) &&
1657 "DFS stack predecessor is already visited.\n");
1658 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1659 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1660 BBState.ExitValue = BBState.EntryValue;
1661 BBState.ExitIsSetup = BBState.EntryIsSetup;
1662 }
1663
1664 // Update stack state by checking contents of MBB.
1665 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
1666 I != E; ++I) {
1667 if (I->getOpcode() == FrameSetupOpcode) {
1668 // The first operand of a FrameOpcode should be i32.
1669 int Size = I->getOperand(0).getImm();
1670 assert(Size >= 0 &&
1671 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1672
1673 if (BBState.ExitIsSetup)
1674 report("FrameSetup is after another FrameSetup", I);
1675 BBState.ExitValue -= Size;
1676 BBState.ExitIsSetup = true;
1677 }
1678
1679 if (I->getOpcode() == FrameDestroyOpcode) {
1680 // The first operand of a FrameOpcode should be i32.
1681 int Size = I->getOperand(0).getImm();
1682 assert(Size >= 0 &&
1683 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1684
1685 if (!BBState.ExitIsSetup)
1686 report("FrameDestroy is not after a FrameSetup", I);
1687 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1688 BBState.ExitValue;
1689 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
1690 report("FrameDestroy <n> is after FrameSetup <m>", I);
1691 *OS << "FrameDestroy <" << Size << "> is after FrameSetup <"
1692 << AbsSPAdj << ">.\n";
1693 }
1694 BBState.ExitValue += Size;
1695 BBState.ExitIsSetup = false;
1696 }
1697 }
1698 SPState[MBB->getNumber()] = BBState;
1699
1700 // Make sure the exit state of any predecessor is consistent with the entry
1701 // state.
1702 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
1703 E = MBB->pred_end(); I != E; ++I) {
1704 if (Reachable.count(*I) &&
1705 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
1706 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
1707 report("The exit stack state of a predecessor is inconsistent.", MBB);
1708 *OS << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
1709 << SPState[(*I)->getNumber()].ExitValue << ", "
1710 << SPState[(*I)->getNumber()].ExitIsSetup
1711 << "), while BB#" << MBB->getNumber() << " has entry state ("
1712 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
1713 }
1714 }
1715
1716 // Make sure the entry state of any successor is consistent with the exit
1717 // state.
1718 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
1719 E = MBB->succ_end(); I != E; ++I) {
1720 if (Reachable.count(*I) &&
1721 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
1722 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
1723 report("The entry stack state of a successor is inconsistent.", MBB);
1724 *OS << "Successor BB#" << (*I)->getNumber() << " has entry state ("
1725 << SPState[(*I)->getNumber()].EntryValue << ", "
1726 << SPState[(*I)->getNumber()].EntryIsSetup
1727 << "), while BB#" << MBB->getNumber() << " has exit state ("
1728 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
1729 }
1730 }
1731
1732 // Make sure a basic block with return ends with zero stack adjustment.
1733 if (!MBB->empty() && MBB->back().isReturn()) {
1734 if (BBState.ExitIsSetup)
1735 report("A return block ends with a FrameSetup.", MBB);
1736 if (BBState.ExitValue)
1737 report("A return block ends with a nonzero stack adjustment.", MBB);
1738 }
1739 }
1740}