blob: 99f058312e637afdea91c570ae67187dd7ac0383 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000036#include "llvm/CodeGen/MachineMemOperand.h"
37#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000038#include "llvm/IR/BasicBlock.h"
39#include "llvm/IR/InlineAsm.h"
40#include "llvm/IR/Instructions.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000041#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000042#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000043#include "llvm/Support/ErrorHandling.h"
Stephen Hinesdce4a402014-05-29 02:49:00 -070044#include "llvm/Support/FileSystem.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000045#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"
Stephen Hines37ed9c12014-12-01 14:51:49 -080049#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000050using namespace llvm;
51
52namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000053 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000054
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000055 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000056 PASS(pass),
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000057 Banner(b),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000058 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000059 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000060
61 bool runOnMachineFunction(MachineFunction &MF);
62
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000063 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000064 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000065 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000066 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000067 const MachineFunction *MF;
68 const TargetMachine *TM;
Evan Cheng15993f82011-06-27 21:26:13 +000069 const TargetInstrInfo *TII;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000070 const TargetRegisterInfo *TRI;
71 const MachineRegisterInfo *MRI;
72
73 unsigned foundErrors;
74
75 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000076 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000077 typedef DenseSet<unsigned> RegSet;
78 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +000079 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000080
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000081 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +000082 BlockSet FunctionBlocks;
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000083
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000084 BitVector regsReserved;
85 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000086 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000087 RegMaskVector regMasks;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000088 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000089
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000090 SlotIndex lastIndex;
91
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000092 // Add Reg and any sub-registers to RV
93 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
94 RV.push_back(Reg);
95 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +000096 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
97 RV.push_back(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000098 }
99
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000100 struct BBInfo {
101 // Is this MBB reachable from the MF entry point?
102 bool reachable;
103
104 // Vregs that must be live in because they are used without being
105 // defined. Map value is the user.
106 RegMap vregsLiveIn;
107
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000108 // Regs killed in MBB. They may be defined again, and will then be in both
109 // regsKilled and regsLiveOut.
110 RegSet regsKilled;
111
112 // Regs defined in MBB and live out. Note that vregs passing through may
113 // be live out without being mentioned here.
114 RegSet regsLiveOut;
115
116 // Vregs that pass through MBB untouched. This set is disjoint from
117 // regsKilled and regsLiveOut.
118 RegSet vregsPassed;
119
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000120 // Vregs that must pass through MBB because they are needed by a successor
121 // block. This set is disjoint from regsLiveOut.
122 RegSet vregsRequired;
123
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000124 // Set versions of block's predecessor and successor lists.
125 BlockSet Preds, Succs;
126
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000127 BBInfo() : reachable(false) {}
128
129 // Add register to vregsPassed if it belongs there. Return true if
130 // anything changed.
131 bool addPassed(unsigned Reg) {
132 if (!TargetRegisterInfo::isVirtualRegister(Reg))
133 return false;
134 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
135 return false;
136 return vregsPassed.insert(Reg).second;
137 }
138
139 // Same for a full set.
140 bool addPassed(const RegSet &RS) {
141 bool changed = false;
142 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
143 if (addPassed(*I))
144 changed = true;
145 return changed;
146 }
147
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000148 // Add register to vregsRequired if it belongs there. Return true if
149 // anything changed.
150 bool addRequired(unsigned Reg) {
151 if (!TargetRegisterInfo::isVirtualRegister(Reg))
152 return false;
153 if (regsLiveOut.count(Reg))
154 return false;
155 return vregsRequired.insert(Reg).second;
156 }
157
158 // Same for a full set.
159 bool addRequired(const RegSet &RS) {
160 bool changed = false;
161 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
162 if (addRequired(*I))
163 changed = true;
164 return changed;
165 }
166
167 // Same for a full map.
168 bool addRequired(const RegMap &RM) {
169 bool changed = false;
170 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
171 if (addRequired(I->first))
172 changed = true;
173 return changed;
174 }
175
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000176 // Live-out registers are either in regsLiveOut or vregsPassed.
177 bool isLiveOut(unsigned Reg) const {
178 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
179 }
180 };
181
182 // Extra register info per MBB.
183 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
184
185 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000186 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000187 }
188
Lang Hames03698de2012-02-14 19:17:48 +0000189 bool isAllocatable(unsigned Reg) {
Jakob Stoklund Olesenfeab72c2012-10-16 00:05:06 +0000190 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
Lang Hames03698de2012-02-14 19:17:48 +0000191 }
192
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000193 // Analysis information if available
194 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000195 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000196 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000197 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000198
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000199 void visitMachineFunctionBefore();
200 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000201 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000202 void visitMachineInstrBefore(const MachineInstr *MI);
203 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
204 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000205 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000206 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
207 void visitMachineFunctionAfter();
208
209 void report(const char *msg, const MachineFunction *MF);
210 void report(const char *msg, const MachineBasicBlock *MBB);
211 void report(const char *msg, const MachineInstr *MI);
212 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000213 void report(const char *msg, const MachineFunction *MF,
214 const LiveInterval &LI);
215 void report(const char *msg, const MachineBasicBlock *MBB,
216 const LiveInterval &LI);
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000217 void report(const char *msg, const MachineFunction *MF,
Stephen Hines37ed9c12014-12-01 14:51:49 -0800218 const LiveRange &LR, unsigned Reg);
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000219 void report(const char *msg, const MachineBasicBlock *MBB,
Stephen Hines37ed9c12014-12-01 14:51:49 -0800220 const LiveRange &LR, unsigned Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000221
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000222 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000223
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000224 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000225 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000226 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000227 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000228
229 void calcRegsRequired();
230 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000231 void verifyLiveIntervals();
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +0000232 void verifyLiveInterval(const LiveInterval&);
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000233 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned);
234 void verifyLiveRangeSegment(const LiveRange&,
235 const LiveRange::const_iterator I, unsigned);
236 void verifyLiveRange(const LiveRange&, unsigned);
Manman Ren7310b752013-07-15 21:26:31 +0000237
238 void verifyStackFrame();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000239 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000240
241 struct MachineVerifierPass : public MachineFunctionPass {
242 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000243 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000244
Stephen Hinesdce4a402014-05-29 02:49:00 -0700245 MachineVerifierPass(const char *b = nullptr)
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000246 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000247 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
248 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000249
Stephen Hines36b56882014-04-23 16:57:46 -0700250 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000251 AU.setPreservesAll();
252 MachineFunctionPass::getAnalysisUsage(AU);
253 }
254
Stephen Hines36b56882014-04-23 16:57:46 -0700255 bool runOnMachineFunction(MachineFunction &MF) override {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000256 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000257 return false;
258 }
259 };
260
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000261}
262
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000263char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000264INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000265 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000266
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000267FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
268 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000269}
270
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000271void MachineFunction::verify(Pass *p, const char *Banner) const {
272 MachineVerifier(p, Banner)
273 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000274}
275
Chris Lattner17e9edc2009-08-23 02:51:22 +0000276bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700277 raw_ostream *OutFile = nullptr;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000278 if (OutFileName) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800279 std::error_code EC;
280 OutFile = new raw_fd_ostream(OutFileName, EC,
Stephen Hines36b56882014-04-23 16:57:46 -0700281 sys::fs::F_Append | sys::fs::F_Text);
Stephen Hines37ed9c12014-12-01 14:51:49 -0800282 if (EC) {
283 errs() << "Error opening '" << OutFileName << "': " << EC.message()
284 << '\n';
Chris Lattner17e9edc2009-08-23 02:51:22 +0000285 exit(1);
286 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000287
Chris Lattner17e9edc2009-08-23 02:51:22 +0000288 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000289 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000290 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000291 }
292
293 foundErrors = 0;
294
295 this->MF = &MF;
296 TM = &MF.getTarget();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800297 TII = MF.getSubtarget().getInstrInfo();
298 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000299 MRI = &MF.getRegInfo();
300
Stephen Hinesdce4a402014-05-29 02:49:00 -0700301 LiveVars = nullptr;
302 LiveInts = nullptr;
303 LiveStks = nullptr;
304 Indexes = nullptr;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000305 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000306 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000307 // We don't want to verify LiveVariables if LiveIntervals is available.
308 if (!LiveInts)
309 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000310 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000311 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000312 }
313
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000314 visitMachineFunctionBefore();
315 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
316 MFI!=MFE; ++MFI) {
317 visitMachineBasicBlockBefore(MFI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000318 // Keep track of the current bundle header.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700319 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000320 // Do we expect the next instruction to be part of the same bundle?
321 bool InBundle = false;
322
Evan Chengddfd1372011-12-14 02:11:42 +0000323 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
324 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000325 if (MBBI->getParent() != MFI) {
326 report("Bad instruction parent pointer", MFI);
327 *OS << "Instruction: " << *MBBI;
328 continue;
329 }
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000330
331 // Check for consistent bundle flags.
332 if (InBundle && !MBBI->isBundledWithPred())
333 report("Missing BundledPred flag, "
334 "BundledSucc was set on predecessor", MBBI);
335 if (!InBundle && MBBI->isBundledWithPred())
336 report("BundledPred flag is set, "
337 "but BundledSucc not set on predecessor", MBBI);
338
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000339 // Is this a bundle header?
340 if (!MBBI->isInsideBundle()) {
341 if (CurBundle)
342 visitMachineBundleAfter(CurBundle);
343 CurBundle = MBBI;
344 visitMachineBundleBefore(CurBundle);
345 } else if (!CurBundle)
346 report("No bundle header", MBBI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000347 visitMachineInstrBefore(MBBI);
348 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
349 visitMachineOperand(&MBBI->getOperand(I), I);
350 visitMachineInstrAfter(MBBI);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000351
352 // Was this the last bundled instruction?
353 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000354 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000355 if (CurBundle)
356 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000357 if (InBundle)
358 report("BundledSucc flag set on last instruction in block", &MFI->back());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000359 visitMachineBasicBlockAfter(MFI);
360 }
361 visitMachineFunctionAfter();
362
Chris Lattner17e9edc2009-08-23 02:51:22 +0000363 if (OutFile)
364 delete OutFile;
365 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000366 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000367
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000368 // Clean up.
369 regsLive.clear();
370 regsDefined.clear();
371 regsDead.clear();
372 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000373 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000374 regsLiveInButUnused.clear();
375 MBBInfoMap.clear();
376
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000377 return false; // no changes
378}
379
Chris Lattner372fefe2009-08-23 01:03:30 +0000380void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000381 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000382 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000383 if (!foundErrors++) {
384 if (Banner)
385 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000386 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000387 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000388 *OS << "*** Bad machine code: " << msg << " ***\n"
Craig Topper96601ca2012-08-22 06:07:19 +0000389 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000390}
391
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000392void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000393 assert(MBB);
394 report(msg, MBB->getParent());
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000395 *OS << "- basic block: BB#" << MBB->getNumber()
396 << ' ' << MBB->getName()
Roman Divacky59324292012-09-05 22:26:57 +0000397 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000398 if (Indexes)
399 *OS << " [" << Indexes->getMBBStartIdx(MBB)
400 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
401 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000402}
403
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000404void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000405 assert(MI);
406 report(msg, MI->getParent());
407 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000408 if (Indexes && Indexes->hasIndex(MI))
409 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000410 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000411}
412
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000413void MachineVerifier::report(const char *msg,
414 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000415 assert(MO);
416 report(msg, MO->getParent());
417 *OS << "- operand " << MONum << ": ";
418 MO->print(*OS, TM);
419 *OS << "\n";
420}
421
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000422void MachineVerifier::report(const char *msg, const MachineFunction *MF,
423 const LiveInterval &LI) {
424 report(msg, MF);
Matthias Braun03d96092013-10-10 21:29:05 +0000425 *OS << "- interval: " << LI << '\n';
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000426}
427
428void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
429 const LiveInterval &LI) {
430 report(msg, MBB);
Matthias Braun03d96092013-10-10 21:29:05 +0000431 *OS << "- interval: " << LI << '\n';
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000432}
433
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000434void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
Stephen Hines37ed9c12014-12-01 14:51:49 -0800435 const LiveRange &LR, unsigned Reg) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000436 report(msg, MBB);
Stephen Hines37ed9c12014-12-01 14:51:49 -0800437 *OS << "- liverange: " << LR << '\n';
438 *OS << "- register: " << PrintReg(Reg, TRI) << '\n';
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000439}
440
441void MachineVerifier::report(const char *msg, const MachineFunction *MF,
Stephen Hines37ed9c12014-12-01 14:51:49 -0800442 const LiveRange &LR, unsigned Reg) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000443 report(msg, MF);
Stephen Hines37ed9c12014-12-01 14:51:49 -0800444 *OS << "- liverange: " << LR << '\n';
445 *OS << "- register: " << PrintReg(Reg, TRI) << '\n';
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000446}
447
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000448void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000449 BBInfo &MInfo = MBBInfoMap[MBB];
450 if (!MInfo.reachable) {
451 MInfo.reachable = true;
452 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
453 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
454 markReachable(*SuI);
455 }
456}
457
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000458void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000459 lastIndex = SlotIndex();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000460 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000461
462 // A sub-register of a reserved register is also reserved
463 for (int Reg = regsReserved.find_first(); Reg>=0;
464 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000465 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000466 // FIXME: This should probably be:
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000467 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
468 regsReserved.set(*SubRegs);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000469 }
470 }
Lang Hames03698de2012-02-14 19:17:48 +0000471
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000472 markReachable(&MF->front());
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000473
474 // Build a set of the basic blocks in the function.
475 FunctionBlocks.clear();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700476 for (const auto &MBB : *MF) {
477 FunctionBlocks.insert(&MBB);
478 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000479
Stephen Hinesdce4a402014-05-29 02:49:00 -0700480 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
481 if (MInfo.Preds.size() != MBB.pred_size())
482 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000483
Stephen Hinesdce4a402014-05-29 02:49:00 -0700484 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
485 if (MInfo.Succs.size() != MBB.succ_size())
486 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000487 }
Jakob Stoklund Olesena58d67a2013-04-19 21:40:57 +0000488
489 // Check that the register use lists are sane.
490 MRI->verifyUseLists();
Manman Ren7310b752013-07-15 21:26:31 +0000491
492 verifyStackFrame();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000493}
494
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000495// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000496static bool matchPair(MachineBasicBlock::const_succ_iterator i,
497 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000498 if (*i == a)
499 return *++i == b;
500 if (*i == b)
501 return *++i == a;
502 return false;
503}
504
505void
506MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700507 FirstTerminator = nullptr;
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000508
Lang Hames03698de2012-02-14 19:17:48 +0000509 if (MRI->isSSA()) {
510 // If this block has allocatable physical registers live-in, check that
511 // it is an entry block or landing pad.
512 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
513 LE = MBB->livein_end();
514 LI != LE; ++LI) {
515 unsigned reg = *LI;
516 if (isAllocatable(reg) && !MBB->isLandingPad() &&
517 MBB != MBB->getParent()->begin()) {
518 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
519 }
520 }
521 }
522
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000523 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000524 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000525 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000526 E = MBB->succ_end(); I != E; ++I) {
527 if ((*I)->isLandingPad())
528 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000529 if (!FunctionBlocks.count(*I))
530 report("MBB has successor that isn't part of the function.", MBB);
531 if (!MBBInfoMap[*I].Preds.count(MBB)) {
532 report("Inconsistent CFG", MBB);
533 *OS << "MBB is not in the predecessor list of the successor BB#"
534 << (*I)->getNumber() << ".\n";
535 }
536 }
537
538 // Check the predecessor list.
539 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
540 E = MBB->pred_end(); I != E; ++I) {
541 if (!FunctionBlocks.count(*I))
542 report("MBB has predecessor that isn't part of the function.", MBB);
543 if (!MBBInfoMap[*I].Succs.count(MBB)) {
544 report("Inconsistent CFG", MBB);
545 *OS << "MBB is not in the successor list of the predecessor BB#"
546 << (*I)->getNumber() << ".\n";
547 }
Cameron Zwarich2100d212010-12-20 04:19:48 +0000548 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000549
550 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
551 const BasicBlock *BB = MBB->getBasicBlock();
552 if (LandingPadSuccs.size() > 1 &&
553 !(AsmInfo &&
554 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
555 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000556 report("MBB has more than one landing pad successor", MBB);
557
Dan Gohman27920592009-08-27 02:43:49 +0000558 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700559 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman27920592009-08-27 02:43:49 +0000560 SmallVector<MachineOperand, 4> Cond;
561 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
562 TBB, FBB, Cond)) {
563 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
564 // check whether its answers match up with reality.
565 if (!TBB && !FBB) {
566 // Block falls through to its successor.
567 MachineFunction::const_iterator MBBI = MBB;
568 ++MBBI;
569 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000570 // It's possible that the block legitimately ends with a noreturn
571 // call or an unreachable, in which case it won't actually fall
572 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000573 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000574 // It's possible that the block legitimately ends with a noreturn
575 // call or an unreachable, in which case it won't actuall fall
576 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000577 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000578 report("MBB exits via unconditional fall-through but doesn't have "
579 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000580 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000581 report("MBB exits via unconditional fall-through but its successor "
582 "differs from its CFG successor!", MBB);
583 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700584 if (!MBB->empty() && MBB->back().isBarrier() &&
585 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000586 report("MBB exits via unconditional fall-through but ends with a "
587 "barrier instruction!", MBB);
588 }
589 if (!Cond.empty()) {
590 report("MBB exits via unconditional fall-through but has a condition!",
591 MBB);
592 }
593 } else if (TBB && !FBB && Cond.empty()) {
594 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000595 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000596 report("MBB exits via unconditional branch but doesn't have "
597 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000598 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000599 report("MBB exits via unconditional branch but the CFG "
600 "successor doesn't match the actual successor!", MBB);
601 }
602 if (MBB->empty()) {
603 report("MBB exits via unconditional branch but doesn't contain "
604 "any instructions!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700605 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000606 report("MBB exits via unconditional branch but doesn't end with a "
607 "barrier instruction!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700608 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000609 report("MBB exits via unconditional branch but the branch isn't a "
610 "terminator instruction!", MBB);
611 }
612 } else if (TBB && !FBB && !Cond.empty()) {
613 // Block conditionally branches somewhere, otherwise falls through.
614 MachineFunction::const_iterator MBBI = MBB;
615 ++MBBI;
616 if (MBBI == MF->end()) {
617 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko344df792012-12-19 22:13:01 +0000618 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000619 // A conditional branch with only one successor is weird, but allowed.
620 if (&*MBBI != TBB)
621 report("MBB exits via conditional branch/fall-through but only has "
622 "one CFG successor!", MBB);
623 else if (TBB != *MBB->succ_begin())
624 report("MBB exits via conditional branch/fall-through but the CFG "
625 "successor don't match the actual successor!", MBB);
626 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000627 report("MBB exits via conditional branch/fall-through but doesn't have "
628 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000629 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000630 report("MBB exits via conditional branch/fall-through but the CFG "
631 "successors don't match the actual successors!", MBB);
632 }
633 if (MBB->empty()) {
634 report("MBB exits via conditional branch/fall-through but doesn't "
635 "contain any instructions!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700636 } else if (MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000637 report("MBB exits via conditional branch/fall-through but ends with a "
638 "barrier instruction!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700639 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000640 report("MBB exits via conditional branch/fall-through but the branch "
641 "isn't a terminator instruction!", MBB);
642 }
643 } else if (TBB && FBB) {
644 // Block conditionally branches somewhere, otherwise branches
645 // somewhere else.
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000646 if (MBB->succ_size() == 1) {
647 // A conditional branch with only one successor is weird, but allowed.
648 if (FBB != TBB)
649 report("MBB exits via conditional branch/branch through but only has "
650 "one CFG successor!", MBB);
651 else if (TBB != *MBB->succ_begin())
652 report("MBB exits via conditional branch/branch through but the CFG "
653 "successor don't match the actual successor!", MBB);
654 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000655 report("MBB exits via conditional branch/branch but doesn't have "
656 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000657 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000658 report("MBB exits via conditional branch/branch but the CFG "
659 "successors don't match the actual successors!", MBB);
660 }
661 if (MBB->empty()) {
662 report("MBB exits via conditional branch/branch but doesn't "
663 "contain any instructions!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700664 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000665 report("MBB exits via conditional branch/branch but doesn't end with a "
666 "barrier instruction!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700667 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000668 report("MBB exits via conditional branch/branch but the branch "
669 "isn't a terminator instruction!", MBB);
670 }
671 if (Cond.empty()) {
672 report("MBB exits via conditinal branch/branch but there's no "
673 "condition!", MBB);
674 }
675 } else {
676 report("AnalyzeBranch returned invalid data!", MBB);
677 }
678 }
679
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000680 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000681 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000682 E = MBB->livein_end(); I != E; ++I) {
683 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
684 report("MBB live-in list contains non-physical register", MBB);
685 continue;
686 }
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 Olesen48872e02009-05-16 00:33:53 +0000690 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000691 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000692
693 const MachineFrameInfo *MFI = MF->getFrameInfo();
694 assert(MFI && "Function has no frame info");
695 BitVector PR = MFI->getPristineRegs(MBB);
696 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosier62c320a2013-05-22 23:17:36 +0000697 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
698 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000699 regsLive.insert(*SubRegs);
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000700 }
701
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000702 regsKilled.clear();
703 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000704
705 if (Indexes)
706 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000707}
708
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000709// This function gets called for all bundle headers, including normal
710// stand-alone unbundled instructions.
711void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
712 if (Indexes && Indexes->hasIndex(MI)) {
713 SlotIndex idx = Indexes->getInstructionIndex(MI);
714 if (!(idx > lastIndex)) {
715 report("Instruction index out of order", MI);
716 *OS << "Last instruction was at " << lastIndex << '\n';
717 }
718 lastIndex = idx;
719 }
Pete Cooper83569cb2012-06-07 17:41:39 +0000720
721 // Ensure non-terminators don't follow terminators.
722 // Ignore predicated terminators formed by if conversion.
723 // FIXME: If conversion shouldn't need to violate this rule.
724 if (MI->isTerminator() && !TII->isPredicated(MI)) {
725 if (!FirstTerminator)
726 FirstTerminator = MI;
727 } else if (FirstTerminator) {
728 report("Non-terminator instruction after the first terminator", MI);
729 *OS << "First terminator was:\t" << *FirstTerminator;
730 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000731}
732
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000733// The operands on an INLINEASM instruction must follow a template.
734// Verify that the flag operands make sense.
735void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
736 // The first two operands on INLINEASM are the asm string and global flags.
737 if (MI->getNumOperands() < 2) {
738 report("Too few operands on inline asm", MI);
739 return;
740 }
741 if (!MI->getOperand(0).isSymbol())
742 report("Asm string must be an external symbol", MI);
743 if (!MI->getOperand(1).isImm())
744 report("Asm flags must be an immediate", MI);
Chad Rosier3d716882012-10-30 19:11:54 +0000745 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
746 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
747 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000748 report("Unknown asm flags", &MI->getOperand(1), 1);
749
750 assert(InlineAsm::MIOp_FirstOperand == 2 && "Asm format changed");
751
752 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
753 unsigned NumOps;
754 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
755 const MachineOperand &MO = MI->getOperand(OpNo);
756 // There may be implicit ops after the fixed operands.
757 if (!MO.isImm())
758 break;
759 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
760 }
761
762 if (OpNo > MI->getNumOperands())
763 report("Missing operands in last group", MI);
764
765 // An optional MDNode follows the groups.
766 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
767 ++OpNo;
768
769 // All trailing operands must be implicit registers.
770 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
771 const MachineOperand &MO = MI->getOperand(OpNo);
772 if (!MO.isReg() || !MO.isImplicit())
773 report("Expected implicit register after groups", &MO, OpNo);
774 }
775}
776
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000777void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000778 const MCInstrDesc &MCID = MI->getDesc();
779 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000780 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000781 *OS << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault17d4ac82013-11-15 22:18:19 +0000782 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000783 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000784
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000785 // Check the tied operands.
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000786 if (MI->isInlineAsm())
787 verifyInlineAsm(MI);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000788
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000789 // Check the MachineMemOperands for basic consistency.
790 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
791 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000792 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000793 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000794 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000795 report("Missing mayStore flag", MI);
796 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000797
798 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000799 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000800 if (LiveInts) {
801 bool mapped = !LiveInts->isNotInMIMap(MI);
802 if (MI->isDebugValue()) {
803 if (mapped)
804 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000805 } else if (MI->isInsideBundle()) {
806 if (mapped)
807 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000808 } else {
809 if (!mapped)
810 report("Missing slot index", MI);
811 }
812 }
813
Andrew Trick3be654f2011-09-21 02:20:46 +0000814 StringRef ErrorInfo;
815 if (!TII->verifyInstruction(MI, ErrorInfo))
816 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000817}
818
819void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000820MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000821 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000822 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000823
Evan Chenge837dea2011-06-28 19:10:37 +0000824 // The first MCID.NumDefs operands must be explicit register defines
825 if (MONum < MCID.getNumDefs()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000826 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000827 if (!MO->isReg())
828 report("Explicit definition must be a register", MO, MONum);
Evan Chengcac58aa2012-05-29 19:40:44 +0000829 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000830 report("Explicit definition marked as use", MO, MONum);
831 else if (MO->isImplicit())
832 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000833 } else if (MONum < MCID.getNumOperands()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000834 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopher113a06c2010-11-17 00:55:36 +0000835 // Don't check if it's the last operand in a variadic instruction. See,
836 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000837 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000838 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000839 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braunb38d9872013-10-04 16:53:00 +0000840 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000841 if (MO->isImplicit())
842 report("Explicit operand marked as implicit", MO, MONum);
843 }
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000844
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000845 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
846 if (TiedTo != -1) {
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000847 if (!MO->isReg())
848 report("Tied use must be a register", MO, MONum);
849 else if (!MO->isTied())
850 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000851 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
852 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000853 } else if (MO->isReg() && MO->isTied())
854 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000855 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000856 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000857 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000858 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000859 }
860
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000861 switch (MO->getType()) {
862 case MachineOperand::MO_Register: {
863 const unsigned Reg = MO->getReg();
864 if (!Reg)
865 return;
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000866 if (MRI->tracksLiveness() && !MI->isDebugValue())
867 checkLiveness(MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000868
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000869 // Verify the consistency of tied operands.
870 if (MO->isTied()) {
871 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
872 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
873 if (!OtherMO.isReg())
874 report("Must be tied to a register", MO, MONum);
875 if (!OtherMO.isTied())
876 report("Missing tie flags on tied operand", MO, MONum);
877 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
878 report("Inconsistent tie links", MO, MONum);
879 if (MONum < MCID.getNumDefs()) {
880 if (OtherIdx < MCID.getNumOperands()) {
881 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
882 report("Explicit def tied to explicit use without tie constraint",
883 MO, MONum);
884 } else {
885 if (!OtherMO.isImplicit())
886 report("Explicit def should be tied to implicit use", MO, MONum);
887 }
888 }
889 }
890
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000891 // Verify two-address constraints after leaving SSA form.
892 unsigned DefIdx;
893 if (!MRI->isSSA() && MO->isUse() &&
894 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
895 Reg != MI->getOperand(DefIdx).getReg())
896 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000897
898 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000899 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000900 unsigned SubIdx = MO->getSubReg();
901
902 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000903 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000904 report("Illegal subregister index for physical register", MO, MONum);
905 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000906 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000907 if (const TargetRegisterClass *DRC =
908 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000909 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000910 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000911 *OS << TRI->getName(Reg) << " is not a "
Stephen Hines37ed9c12014-12-01 14:51:49 -0800912 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000913 }
914 }
915 } else {
916 // Virtual register.
917 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
918 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000919 const TargetRegisterClass *SRC =
920 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000921 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000922 report("Invalid subregister index for virtual register", MO, MONum);
Stephen Hines37ed9c12014-12-01 14:51:49 -0800923 *OS << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000924 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000925 return;
926 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000927 if (RC != SRC) {
928 report("Invalid register class for subregister index", MO, MONum);
Stephen Hines37ed9c12014-12-01 14:51:49 -0800929 *OS << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000930 << " does not fully support subreg index " << SubIdx << "\n";
931 return;
932 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000933 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000934 if (const TargetRegisterClass *DRC =
935 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000936 if (SubIdx) {
937 const TargetRegisterClass *SuperRC =
938 TRI->getLargestLegalSuperClass(RC);
939 if (!SuperRC) {
940 report("No largest legal super class exists.", MO, MONum);
941 return;
942 }
943 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
944 if (!DRC) {
945 report("No matching super-reg register class.", MO, MONum);
946 return;
947 }
948 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000949 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000950 report("Illegal virtual register for instruction", MO, MONum);
Stephen Hines37ed9c12014-12-01 14:51:49 -0800951 *OS << "Expected a " << TRI->getRegClassName(DRC)
952 << " register, but got a " << TRI->getRegClassName(RC)
953 << " register\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000954 }
955 }
956 }
957 }
958 break;
959 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000960
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000961 case MachineOperand::MO_RegisterMask:
962 regMasks.push_back(MO->getRegMask());
963 break;
964
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000965 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000966 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
967 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000968 break;
969
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000970 case MachineOperand::MO_FrameIndex:
971 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
972 LiveInts && !LiveInts->isNotInMIMap(MI)) {
973 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
974 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000975 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000976 report("Instruction loads from dead spill slot", MO, MONum);
977 *OS << "Live stack: " << LI << '\n';
978 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000979 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000980 report("Instruction stores to dead spill slot", MO, MONum);
981 *OS << "Live stack: " << LI << '\n';
982 }
983 }
984 break;
985
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000986 default:
987 break;
988 }
989}
990
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000991void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
992 const MachineInstr *MI = MO->getParent();
993 const unsigned Reg = MO->getReg();
994
995 // Both use and def operands can read a register.
996 if (MO->readsReg()) {
997 regsLiveInButUnused.erase(Reg);
998
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000999 if (MO->isKill())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001000 addRegWithSubRegs(regsKilled, Reg);
1001
1002 // Check that LiveVars knows this kill.
1003 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1004 MO->isKill()) {
1005 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1006 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
1007 report("Kill missing from LiveVariables", MO, MONum);
1008 }
1009
1010 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001011 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
1012 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
1013 // Check the cached regunit intervals.
1014 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1015 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001016 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units)) {
1017 LiveQueryResult LRQ = LR->Query(UseIdx);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001018 if (!LRQ.valueIn()) {
Matthias Braun331de112013-10-10 21:28:43 +00001019 report("No live segment at use", MO, MONum);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001020 *OS << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001021 << ' ' << *LR << '\n';
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001022 }
1023 if (MO->isKill() && !LRQ.isKill()) {
1024 report("Live range continues after kill flag", MO, MONum);
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001025 *OS << PrintRegUnit(*Units, TRI) << ' ' << *LR << '\n';
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001026 }
1027 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001028 }
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001029 }
1030
1031 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1032 if (LiveInts->hasInterval(Reg)) {
1033 // This is a virtual register interval.
1034 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun5649e252013-10-10 21:28:52 +00001035 LiveQueryResult LRQ = LI.Query(UseIdx);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001036 if (!LRQ.valueIn()) {
Matthias Braun331de112013-10-10 21:28:43 +00001037 report("No live segment at use", MO, MONum);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001038 *OS << UseIdx << " is not live in " << LI << '\n';
1039 }
1040 // Check for extra kill flags.
1041 // Note that we allow missing kill flags for now.
1042 if (MO->isKill() && !LRQ.isKill()) {
1043 report("Live range continues after kill flag", MO, MONum);
1044 *OS << "Live range: " << LI << '\n';
1045 }
1046 } else {
1047 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001048 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001049 }
1050 }
1051
1052 // Use of a dead register.
1053 if (!regsLive.count(Reg)) {
1054 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1055 // Reserved registers may be used even when 'dead'.
1056 if (!isReserved(Reg))
1057 report("Using an undefined physical register", MO, MONum);
Pete Cooperb97c57a2012-07-19 23:40:38 +00001058 } else if (MRI->def_empty(Reg)) {
1059 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001060 } else {
1061 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1062 // We don't know which virtual registers are live in, so only complain
1063 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1064 // must be live in. PHI instructions are handled separately.
1065 if (MInfo.regsKilled.count(Reg))
1066 report("Using a killed virtual register", MO, MONum);
1067 else if (!MI->isPHI())
1068 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1069 }
1070 }
1071 }
1072
1073 if (MO->isDef()) {
1074 // Register defined.
1075 // TODO: verify that earlyclobber ops are not used.
1076 if (MO->isDead())
1077 addRegWithSubRegs(regsDead, Reg);
1078 else
1079 addRegWithSubRegs(regsDefined, Reg);
1080
1081 // Verify SSA form.
1082 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Stephen Hines36b56882014-04-23 16:57:46 -07001083 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001084 report("Multiple virtual register defs in SSA form", MO, MONum);
1085
Matthias Braun331de112013-10-10 21:28:43 +00001086 // Check LiveInts for a live segment, but only for virtual registers.
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001087 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1088 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001089 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1090 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001091 if (LiveInts->hasInterval(Reg)) {
1092 const LiveInterval &LI = LiveInts->getInterval(Reg);
1093 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1094 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001095 if (VNI->def != DefIdx) {
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001096 report("Inconsistent valno->def", MO, MONum);
1097 *OS << "Valno " << VNI->id << " is not defined at "
1098 << DefIdx << " in " << LI << '\n';
1099 }
1100 } else {
Matthias Braun331de112013-10-10 21:28:43 +00001101 report("No live segment at def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001102 *OS << DefIdx << " is not live in " << LI << '\n';
1103 }
Pedro Artigasd900b112013-11-08 22:46:28 +00001104 // Check that, if the dead def flag is present, LiveInts agree.
1105 if (MO->isDead()) {
1106 LiveQueryResult LRQ = LI.Query(DefIdx);
1107 if (!LRQ.isDeadDef()) {
1108 report("Live range continues after dead def flag", MO, MONum);
1109 *OS << "Live range: " << LI << '\n';
1110 }
1111 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001112 } else {
1113 report("Virtual register has no Live interval", MO, MONum);
1114 }
1115 }
1116 }
1117}
1118
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001119void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +00001120}
1121
1122// This function gets called after visiting all instructions in a bundle. The
1123// argument points to the bundle header.
1124// Normal stand-alone instructions are also considered 'bundles', and this
1125// function is called for all of them.
1126void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001127 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1128 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001129 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +00001130 // Kill any masked registers.
1131 while (!regMasks.empty()) {
1132 const uint32_t *Mask = regMasks.pop_back_val();
1133 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1134 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1135 MachineOperand::clobbersPhysReg(Mask, *I))
1136 regsDead.push_back(*I);
1137 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001138 set_subtract(regsLive, regsDead); regsDead.clear();
1139 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001140}
1141
1142void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001143MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001144 MBBInfoMap[MBB].regsLiveOut = regsLive;
1145 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +00001146
1147 if (Indexes) {
1148 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1149 if (!(stop > lastIndex)) {
1150 report("Block ends before last instruction index", MBB);
1151 *OS << "Block ends at " << stop
1152 << " last instruction was at " << lastIndex << '\n';
1153 }
1154 lastIndex = stop;
1155 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001156}
1157
1158// Calculate the largest possible vregsPassed sets. These are the registers that
1159// can pass through an MBB live, but may not be live every time. It is assumed
1160// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001161void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001162 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1163 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001164 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001165 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001166 BBInfo &MInfo = MBBInfoMap[&MBB];
1167 if (!MInfo.reachable)
1168 continue;
1169 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1170 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1171 BBInfo &SInfo = MBBInfoMap[*SuI];
1172 if (SInfo.addPassed(MInfo.regsLiveOut))
1173 todo.insert(*SuI);
1174 }
1175 }
1176
1177 // Iteratively push vregsPassed to successors. This will converge to the same
1178 // final state regardless of DenseSet iteration order.
1179 while (!todo.empty()) {
1180 const MachineBasicBlock *MBB = *todo.begin();
1181 todo.erase(MBB);
1182 BBInfo &MInfo = MBBInfoMap[MBB];
1183 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1184 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1185 if (*SuI == MBB)
1186 continue;
1187 BBInfo &SInfo = MBBInfoMap[*SuI];
1188 if (SInfo.addPassed(MInfo.vregsPassed))
1189 todo.insert(*SuI);
1190 }
1191 }
1192}
1193
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001194// Calculate the set of virtual registers that must be passed through each basic
1195// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001196// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001197void MachineVerifier::calcRegsRequired() {
1198 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001199 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001200 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001201 BBInfo &MInfo = MBBInfoMap[&MBB];
1202 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1203 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1204 BBInfo &PInfo = MBBInfoMap[*PrI];
1205 if (PInfo.addRequired(MInfo.vregsLiveIn))
1206 todo.insert(*PrI);
1207 }
1208 }
1209
1210 // Iteratively push vregsRequired to predecessors. This will converge to the
1211 // same final state regardless of DenseSet iteration order.
1212 while (!todo.empty()) {
1213 const MachineBasicBlock *MBB = *todo.begin();
1214 todo.erase(MBB);
1215 BBInfo &MInfo = MBBInfoMap[MBB];
1216 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1217 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1218 if (*PrI == MBB)
1219 continue;
1220 BBInfo &SInfo = MBBInfoMap[*PrI];
1221 if (SInfo.addRequired(MInfo.vregsRequired))
1222 todo.insert(*PrI);
1223 }
1224 }
1225}
1226
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001227// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001228// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001229void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001230 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001231 for (const auto &BBI : *MBB) {
1232 if (!BBI.isPHI())
1233 break;
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001234 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001235
Stephen Hinesdce4a402014-05-29 02:49:00 -07001236 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();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001239 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",
Stephen Hinesdce4a402014-05-29 02:49:00 -07001245 &BBI.getOperand(i), i);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001246 }
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)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001252 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
Stephen Hinesdce4a402014-05-29 02:49:00 -07001263 for (const auto &MBB : *MF) {
1264 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001265
1266 // Skip unreachable MBBs.
1267 if (!MInfo.reachable)
1268 continue;
1269
Stephen Hinesdce4a402014-05-29 02:49:00 -07001270 checkPHIOps(&MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001271 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001272
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001273 // Now check liveness info if available
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001274 calcRegsRequired();
1275
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001276 // Check for killed virtual registers that should be live out.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001277 for (const auto &MBB : *MF) {
1278 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001279 for (RegSet::iterator
1280 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1281 ++I)
1282 if (MInfo.regsKilled.count(*I)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001283 report("Virtual register killed in block, but needed live out.", &MBB);
Bill Wendling96cb1122012-07-19 00:04:14 +00001284 *OS << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001285 << " is used after the block.\n";
1286 }
1287 }
1288
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001289 if (!MF->empty()) {
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001290 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1291 for (RegSet::iterator
1292 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesenff0275e2012-03-10 00:44:11 +00001293 ++I)
1294 report("Virtual register def doesn't dominate all uses.",
1295 MRI->getVRegDef(*I));
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001296 }
1297
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001298 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001299 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001300 if (LiveInts)
1301 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001302}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001303
1304void MachineVerifier::verifyLiveVariables() {
1305 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001306 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1307 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001308 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001309 for (const auto &MBB : *MF) {
1310 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001311
1312 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1313 if (MInfo.vregsRequired.count(Reg)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001314 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1315 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001316 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001317 << " must be live through the block.\n";
1318 }
1319 } else {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001320 if (VI.AliveBlocks.test(MBB.getNumber())) {
1321 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001322 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001323 << " is not needed live through the block.\n";
1324 }
1325 }
1326 }
1327 }
1328}
1329
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001330void MachineVerifier::verifyLiveIntervals() {
1331 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001332 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1333 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001334
1335 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001336 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001337 continue;
1338
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001339 if (!LiveInts->hasInterval(Reg)) {
1340 report("Missing live interval for virtual register", MF);
1341 *OS << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001342 continue;
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001343 }
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001344
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001345 const LiveInterval &LI = LiveInts->getInterval(Reg);
1346 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001347 verifyLiveInterval(LI);
1348 }
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001349
1350 // Verify all the cached regunit intervals.
1351 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001352 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1353 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001354}
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001355
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001356void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
1357 const VNInfo *VNI,
1358 unsigned Reg) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001359 if (VNI->isUnused())
1360 return;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001361
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001362 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001363
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001364 if (!DefVNI) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001365 report("Valno not live at def and not marked unused", MF, LR, Reg);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001366 *OS << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001367 return;
1368 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001369
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001370 if (DefVNI != VNI) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001371 report("Live segment at def has different valno", MF, LR, Reg);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001372 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001373 << " where valno #" << DefVNI->id << " is live\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001374 return;
1375 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001376
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001377 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1378 if (!MBB) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001379 report("Invalid definition index", MF, LR, Reg);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001380 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001381 << " in " << LR << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001382 return;
1383 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001384
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001385 if (VNI->isPHIDef()) {
1386 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001387 report("PHIDef value is not defined at MBB start", MBB, LR, Reg);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001388 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001389 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001390 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001391 return;
1392 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001393
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001394 // Non-PHI def.
1395 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1396 if (!MI) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001397 report("No instruction at def index", MBB, LR, Reg);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001398 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001399 return;
1400 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001401
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001402 if (Reg != 0) {
1403 bool hasDef = false;
1404 bool isEarlyClobber = false;
1405 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1406 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001407 continue;
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001408 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1409 if (MOI->getReg() != Reg)
1410 continue;
1411 } else {
1412 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1413 !TRI->hasRegUnit(MOI->getReg(), Reg))
1414 continue;
1415 }
1416 hasDef = true;
1417 if (MOI->isEarlyClobber())
1418 isEarlyClobber = true;
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001419 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001420
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001421 if (!hasDef) {
1422 report("Defining instruction does not modify register", MI);
1423 *OS << "Valno #" << VNI->id << " in " << LR << '\n';
1424 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001425
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001426 // Early clobber defs begin at USE slots, but other defs must begin at
1427 // DEF slots.
1428 if (isEarlyClobber) {
1429 if (!VNI->def.isEarlyClobber()) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001430 report("Early clobber def must be at an early-clobber slot", MBB, LR,
1431 Reg);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001432 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
1433 }
1434 } else if (!VNI->def.isRegister()) {
1435 report("Non-PHI, non-early clobber def must be at a register slot",
Stephen Hines37ed9c12014-12-01 14:51:49 -08001436 MBB, LR, Reg);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001437 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001438 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001439 }
1440}
1441
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001442void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1443 const LiveRange::const_iterator I,
1444 unsigned Reg) {
1445 const LiveRange::Segment &S = *I;
1446 const VNInfo *VNI = S.valno;
Matthias Braun331de112013-10-10 21:28:43 +00001447 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001448
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001449 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001450 report("Foreign valno in live segment", MF, LR, Reg);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001451 *OS << S << " has a bad valno\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001452 }
1453
1454 if (VNI->isUnused()) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001455 report("Live segment valno is marked unused", MF, LR, Reg);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001456 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001457 }
1458
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001459 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001460 if (!MBB) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001461 report("Bad start of live segment, no basic block", MF, LR, Reg);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001462 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001463 return;
1464 }
1465 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001466 if (S.start != MBBStartIdx && S.start != VNI->def) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001467 report("Live segment must begin at MBB entry or valno def", MBB, LR, Reg);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001468 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001469 }
1470
1471 const MachineBasicBlock *EndMBB =
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001472 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001473 if (!EndMBB) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001474 report("Bad end of live segment, no basic block", MF, LR, Reg);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001475 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001476 return;
1477 }
1478
1479 // No more checks for live-out segments.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001480 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001481 return;
1482
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001483 // RegUnit intervals are allowed dead phis.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001484 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1485 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001486 return;
1487
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001488 // The live segment is ending inside EndMBB
1489 const MachineInstr *MI =
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001490 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001491 if (!MI) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001492 report("Live segment doesn't end at a valid instruction", EndMBB, LR, Reg);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001493 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001494 return;
1495 }
1496
1497 // The block slot must refer to a basic block boundary.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001498 if (S.end.isBlock()) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001499 report("Live segment ends at B slot of an instruction", EndMBB, LR, Reg);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001500 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001501 }
1502
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001503 if (S.end.isDead()) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001504 // Segment ends on the dead slot.
1505 // That means there must be a dead def.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001506 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001507 report("Live segment ending at dead slot spans instructions", EndMBB, LR,
1508 Reg);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001509 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001510 }
1511 }
1512
1513 // A live segment can only end at an early-clobber slot if it is being
1514 // redefined by an early-clobber def.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001515 if (S.end.isEarlyClobber()) {
1516 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001517 report("Live segment ending at early clobber slot must be "
Stephen Hines37ed9c12014-12-01 14:51:49 -08001518 "redefined by an EC def in the same instruction", EndMBB, LR, Reg);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001519 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001520 }
1521 }
1522
1523 // The following checks only apply to virtual registers. Physreg liveness
1524 // is too weird to check.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001525 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun331de112013-10-10 21:28:43 +00001526 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001527 // use, or a dead flag on a def.
1528 bool hasRead = false;
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001529 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001530 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001531 continue;
1532 if (MOI->readsReg())
1533 hasRead = true;
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001534 }
Pedro Artigasd900b112013-11-08 22:46:28 +00001535 if (!S.end.isDead()) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001536 if (!hasRead) {
Matthias Braun331de112013-10-10 21:28:43 +00001537 report("Instruction ending live segment doesn't read the register", MI);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001538 *OS << S << " in " << LR << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001539 }
1540 }
1541 }
1542
1543 // Now check all the basic blocks in this live segment.
1544 MachineFunction::const_iterator MFI = MBB;
Matthias Braun331de112013-10-10 21:28:43 +00001545 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001546 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001547 // Not live-in to any blocks.
1548 if (MBB == EndMBB)
1549 return;
1550 // Skip this block.
1551 ++MFI;
1552 }
1553 for (;;) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001554 assert(LiveInts->isLiveInToMBB(LR, MFI));
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001555 // We don't know how to track physregs into a landing pad.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001556 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001557 MFI->isLandingPad()) {
1558 if (&*MFI == EndMBB)
1559 break;
1560 ++MFI;
1561 continue;
1562 }
1563
1564 // Is VNI a PHI-def in the current block?
1565 bool IsPHI = VNI->isPHIDef() &&
1566 VNI->def == LiveInts->getMBBStartIdx(MFI);
1567
1568 // Check that VNI is live-out of all predecessors.
1569 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1570 PE = MFI->pred_end(); PI != PE; ++PI) {
1571 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001572 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001573
1574 // All predecessors must have a live-out value.
1575 if (!PVNI) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001576 report("Register not marked live out of predecessor", *PI, LR, Reg);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001577 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1578 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001579 << PEnd << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001580 continue;
1581 }
1582
1583 // Only PHI-defs can take different predecessor values.
1584 if (!IsPHI && PVNI != VNI) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001585 report("Different value live out of predecessor", *PI, LR, Reg);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001586 *OS << "Valno #" << PVNI->id << " live out of BB#"
1587 << (*PI)->getNumber() << '@' << PEnd
1588 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001589 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001590 }
1591 }
1592 if (&*MFI == EndMBB)
1593 break;
1594 ++MFI;
1595 }
1596}
1597
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001598void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg) {
1599 for (LiveRange::const_vni_iterator I = LR.vni_begin(), E = LR.vni_end();
1600 I != E; ++I)
1601 verifyLiveRangeValue(LR, *I, Reg);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001602
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001603 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
1604 verifyLiveRangeSegment(LR, I, Reg);
1605}
1606
1607void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1608 verifyLiveRange(LI, LI.reg);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001609
1610 // Check the LI only has one connected component.
1611 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1612 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1613 unsigned NumComp = ConEQ.Classify(&LI);
1614 if (NumComp > 1) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001615 report("Multiple connected components in live interval", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001616 for (unsigned comp = 0; comp != NumComp; ++comp) {
1617 *OS << comp << ": valnos";
1618 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1619 E = LI.vni_end(); I!=E; ++I)
1620 if (comp == ConEQ.getEqClass(*I))
1621 *OS << ' ' << (*I)->id;
1622 *OS << '\n';
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001623 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001624 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001625 }
1626}
Manman Ren7310b752013-07-15 21:26:31 +00001627
1628namespace {
1629 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1630 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1631 // value is zero.
1632 // We use a bool plus an integer to capture the stack state.
1633 struct StackStateOfBB {
1634 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1635 ExitIsSetup(false) { }
1636 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1637 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1638 ExitIsSetup(ExitSetup) { }
1639 // Can be negative, which means we are setting up a frame.
1640 int EntryValue;
1641 int ExitValue;
1642 bool EntryIsSetup;
1643 bool ExitIsSetup;
1644 };
1645}
1646
1647/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1648/// by a FrameDestroy <n>, stack adjustments are identical on all
1649/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1650void MachineVerifier::verifyStackFrame() {
1651 int FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1652 int FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
1653
1654 SmallVector<StackStateOfBB, 8> SPState;
1655 SPState.resize(MF->getNumBlockIDs());
1656 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1657
1658 // Visit the MBBs in DFS order.
1659 for (df_ext_iterator<const MachineFunction*,
1660 SmallPtrSet<const MachineBasicBlock*, 8> >
1661 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1662 DFI != DFE; ++DFI) {
1663 const MachineBasicBlock *MBB = *DFI;
1664
1665 StackStateOfBB BBState;
1666 // Check the exit state of the DFS stack predecessor.
1667 if (DFI.getPathLength() >= 2) {
1668 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1669 assert(Reachable.count(StackPred) &&
1670 "DFS stack predecessor is already visited.\n");
1671 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1672 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1673 BBState.ExitValue = BBState.EntryValue;
1674 BBState.ExitIsSetup = BBState.EntryIsSetup;
1675 }
1676
1677 // Update stack state by checking contents of MBB.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001678 for (const auto &I : *MBB) {
1679 if (I.getOpcode() == FrameSetupOpcode) {
Manman Ren7310b752013-07-15 21:26:31 +00001680 // The first operand of a FrameOpcode should be i32.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001681 int Size = I.getOperand(0).getImm();
Manman Ren7310b752013-07-15 21:26:31 +00001682 assert(Size >= 0 &&
1683 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1684
1685 if (BBState.ExitIsSetup)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001686 report("FrameSetup is after another FrameSetup", &I);
Manman Ren7310b752013-07-15 21:26:31 +00001687 BBState.ExitValue -= Size;
1688 BBState.ExitIsSetup = true;
1689 }
1690
Stephen Hinesdce4a402014-05-29 02:49:00 -07001691 if (I.getOpcode() == FrameDestroyOpcode) {
Manman Ren7310b752013-07-15 21:26:31 +00001692 // The first operand of a FrameOpcode should be i32.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001693 int Size = I.getOperand(0).getImm();
Manman Ren7310b752013-07-15 21:26:31 +00001694 assert(Size >= 0 &&
1695 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1696
1697 if (!BBState.ExitIsSetup)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001698 report("FrameDestroy is not after a FrameSetup", &I);
Manman Ren7310b752013-07-15 21:26:31 +00001699 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1700 BBState.ExitValue;
1701 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001702 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Manman Ren7310b752013-07-15 21:26:31 +00001703 *OS << "FrameDestroy <" << Size << "> is after FrameSetup <"
1704 << AbsSPAdj << ">.\n";
1705 }
1706 BBState.ExitValue += Size;
1707 BBState.ExitIsSetup = false;
1708 }
1709 }
1710 SPState[MBB->getNumber()] = BBState;
1711
1712 // Make sure the exit state of any predecessor is consistent with the entry
1713 // state.
1714 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
1715 E = MBB->pred_end(); I != E; ++I) {
1716 if (Reachable.count(*I) &&
1717 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
1718 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
1719 report("The exit stack state of a predecessor is inconsistent.", MBB);
1720 *OS << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
1721 << SPState[(*I)->getNumber()].ExitValue << ", "
1722 << SPState[(*I)->getNumber()].ExitIsSetup
1723 << "), while BB#" << MBB->getNumber() << " has entry state ("
1724 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
1725 }
1726 }
1727
1728 // Make sure the entry state of any successor is consistent with the exit
1729 // state.
1730 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
1731 E = MBB->succ_end(); I != E; ++I) {
1732 if (Reachable.count(*I) &&
1733 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
1734 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
1735 report("The entry stack state of a successor is inconsistent.", MBB);
1736 *OS << "Successor BB#" << (*I)->getNumber() << " has entry state ("
1737 << SPState[(*I)->getNumber()].EntryValue << ", "
1738 << SPState[(*I)->getNumber()].EntryIsSetup
1739 << "), while BB#" << MBB->getNumber() << " has exit state ("
1740 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
1741 }
1742 }
1743
1744 // Make sure a basic block with return ends with zero stack adjustment.
1745 if (!MBB->empty() && MBB->back().isReturn()) {
1746 if (BBState.ExitIsSetup)
1747 report("A return block ends with a FrameSetup.", MBB);
1748 if (BBState.ExitValue)
1749 report("A return block ends with a nonzero stack adjustment.", MBB);
1750 }
1751 }
1752}