blob: ca35ec5fdcf8238e243122b1351777f0b55c4cec [file] [log] [blame]
Bill Wendling68caaaf2010-08-19 18:52:17 +00001//===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
Jakob Stoklund Olesen36c027a2009-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 Olesen36c027a2009-05-16 00:33:53 +000026#include "llvm/CodeGen/Passes.h"
Chris Lattner565449d2009-08-23 03:13:20 +000027#include "llvm/ADT/DenseSet.h"
Manman Renaa6875b2013-07-15 21:26:31 +000028#include "llvm/ADT/DepthFirstIterator.h"
Chris Lattner565449d2009-08-23 03:13:20 +000029#include "llvm/ADT/SetOperations.h"
30#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-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 Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/CodeGen/MachineMemOperand.h"
37#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000038#include "llvm/IR/BasicBlock.h"
39#include "llvm/IR/InlineAsm.h"
40#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000042#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000043#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000044#include "llvm/Support/FileSystem.h"
Matthias Braun3f1d8fd2014-12-10 01:12:10 +000045#include "llvm/Support/Format.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000046#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Target/TargetInstrInfo.h"
48#include "llvm/Target/TargetMachine.h"
49#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000050#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000051using namespace llvm;
52
53namespace {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000054 struct MachineVerifier {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000055
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000056 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000057 PASS(pass),
Owen Anderson21b17882015-02-04 00:02:59 +000058 Banner(b)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000059 {}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000060
61 bool runOnMachineFunction(MachineFunction &MF);
62
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000063 Pass *const PASS;
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000064 const char *Banner;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000065 const MachineFunction *MF;
66 const TargetMachine *TM;
Evan Cheng8d71a752011-06-27 21:26:13 +000067 const TargetInstrInfo *TII;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000068 const TargetRegisterInfo *TRI;
69 const MachineRegisterInfo *MRI;
70
71 unsigned foundErrors;
72
73 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000074 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000075 typedef DenseSet<unsigned> RegSet;
76 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000077 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000078
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000079 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000080 BlockSet FunctionBlocks;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000081
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000082 BitVector regsReserved;
83 RegSet regsLive;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000084 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000085 RegMaskVector regMasks;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000086 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000087
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +000088 SlotIndex lastIndex;
89
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000090 // Add Reg and any sub-registers to RV
91 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
92 RV.push_back(Reg);
93 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +000094 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
95 RV.push_back(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000096 }
97
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000098 struct BBInfo {
99 // Is this MBB reachable from the MF entry point?
100 bool reachable;
101
102 // Vregs that must be live in because they are used without being
103 // defined. Map value is the user.
104 RegMap vregsLiveIn;
105
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000106 // Regs killed in MBB. They may be defined again, and will then be in both
107 // regsKilled and regsLiveOut.
108 RegSet regsKilled;
109
110 // Regs defined in MBB and live out. Note that vregs passing through may
111 // be live out without being mentioned here.
112 RegSet regsLiveOut;
113
114 // Vregs that pass through MBB untouched. This set is disjoint from
115 // regsKilled and regsLiveOut.
116 RegSet vregsPassed;
117
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000118 // Vregs that must pass through MBB because they are needed by a successor
119 // block. This set is disjoint from regsLiveOut.
120 RegSet vregsRequired;
121
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000122 // Set versions of block's predecessor and successor lists.
123 BlockSet Preds, Succs;
124
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000125 BBInfo() : reachable(false) {}
126
127 // Add register to vregsPassed if it belongs there. Return true if
128 // anything changed.
129 bool addPassed(unsigned Reg) {
130 if (!TargetRegisterInfo::isVirtualRegister(Reg))
131 return false;
132 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
133 return false;
134 return vregsPassed.insert(Reg).second;
135 }
136
137 // Same for a full set.
138 bool addPassed(const RegSet &RS) {
139 bool changed = false;
140 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
141 if (addPassed(*I))
142 changed = true;
143 return changed;
144 }
145
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000146 // Add register to vregsRequired if it belongs there. Return true if
147 // anything changed.
148 bool addRequired(unsigned Reg) {
149 if (!TargetRegisterInfo::isVirtualRegister(Reg))
150 return false;
151 if (regsLiveOut.count(Reg))
152 return false;
153 return vregsRequired.insert(Reg).second;
154 }
155
156 // Same for a full set.
157 bool addRequired(const RegSet &RS) {
158 bool changed = false;
159 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
160 if (addRequired(*I))
161 changed = true;
162 return changed;
163 }
164
165 // Same for a full map.
166 bool addRequired(const RegMap &RM) {
167 bool changed = false;
168 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
169 if (addRequired(I->first))
170 changed = true;
171 return changed;
172 }
173
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000174 // Live-out registers are either in regsLiveOut or vregsPassed.
175 bool isLiveOut(unsigned Reg) const {
176 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
177 }
178 };
179
180 // Extra register info per MBB.
181 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
182
183 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000184 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000185 }
186
Lang Hames1ce837a2012-02-14 19:17:48 +0000187 bool isAllocatable(unsigned Reg) {
Jakob Stoklund Olesen244beb42012-10-16 00:05:06 +0000188 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
Lang Hames1ce837a2012-02-14 19:17:48 +0000189 }
190
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000191 // Analysis information if available
192 LiveVariables *LiveVars;
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +0000193 LiveIntervals *LiveInts;
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000194 LiveStacks *LiveStks;
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000195 SlotIndexes *Indexes;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000196
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000197 void visitMachineFunctionBefore();
198 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000199 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000200 void visitMachineInstrBefore(const MachineInstr *MI);
201 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
202 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000203 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000204 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
205 void visitMachineFunctionAfter();
206
207 void report(const char *msg, const MachineFunction *MF);
208 void report(const char *msg, const MachineBasicBlock *MBB);
209 void report(const char *msg, const MachineInstr *MI);
210 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000211 void report(const char *msg, const MachineFunction *MF,
212 const LiveInterval &LI);
213 void report(const char *msg, const MachineBasicBlock *MBB,
214 const LiveInterval &LI);
Matthias Braun364e6e92013-10-10 21:28:54 +0000215 void report(const char *msg, const MachineFunction *MF,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000216 const LiveRange &LR, unsigned Reg, unsigned LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000217 void report(const char *msg, const MachineBasicBlock *MBB,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000218 const LiveRange &LR, unsigned Reg, unsigned LaneMask);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000219
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000220 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000221
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000222 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000223 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000224 void calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000225 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000226
227 void calcRegsRequired();
228 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000229 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000230 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000231 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
232 unsigned);
Matthias Braun364e6e92013-10-10 21:28:54 +0000233 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000234 const LiveRange::const_iterator I, unsigned,
235 unsigned);
236 void verifyLiveRange(const LiveRange&, unsigned, unsigned LaneMask = 0);
Manman Renaa6875b2013-07-15 21:26:31 +0000237
238 void verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000239 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000240
241 struct MachineVerifierPass : public MachineFunctionPass {
242 static char ID; // Pass ID, replacement for typeid
Matthias Brauna4e932d2014-12-11 19:41:51 +0000243 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000244
Matthias Brauna4e932d2014-12-11 19:41:51 +0000245 MachineVerifierPass(const std::string &banner = nullptr)
246 : MachineFunctionPass(ID), Banner(banner) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000247 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
248 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000249
Craig Topper4584cd52014-03-07 09:26:03 +0000250 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000251 AU.setPreservesAll();
252 MachineFunctionPass::getAnalysisUsage(AU);
253 }
254
Craig Topper4584cd52014-03-07 09:26:03 +0000255 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Brauna4e932d2014-12-11 19:41:51 +0000256 MF.verify(this, Banner.c_str());
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000257 return false;
258 }
259 };
260
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000261}
262
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000263char MachineVerifierPass::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +0000264INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000265 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000266
Matthias Brauna4e932d2014-12-11 19:41:51 +0000267FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000268 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000269}
270
Jakob Stoklund Olesenbf4550e2010-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 Olesen27440e72009-11-13 21:56:09 +0000274}
275
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000276bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000277 foundErrors = 0;
278
279 this->MF = &MF;
280 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000281 TII = MF.getSubtarget().getInstrInfo();
282 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000283 MRI = &MF.getRegInfo();
284
Craig Topperc0196b12014-04-14 00:51:57 +0000285 LiveVars = nullptr;
286 LiveInts = nullptr;
287 LiveStks = nullptr;
288 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000289 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000290 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000291 // We don't want to verify LiveVariables if LiveIntervals is available.
292 if (!LiveInts)
293 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000294 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000295 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000296 }
297
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000298 visitMachineFunctionBefore();
299 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
300 MFI!=MFE; ++MFI) {
301 visitMachineBasicBlockBefore(MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000302 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000303 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000304 // Do we expect the next instruction to be part of the same bundle?
305 bool InBundle = false;
306
Evan Cheng7fae11b2011-12-14 02:11:42 +0000307 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
308 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000309 if (MBBI->getParent() != MFI) {
310 report("Bad instruction parent pointer", MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000311 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000312 continue;
313 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000314
315 // Check for consistent bundle flags.
316 if (InBundle && !MBBI->isBundledWithPred())
317 report("Missing BundledPred flag, "
318 "BundledSucc was set on predecessor", MBBI);
319 if (!InBundle && MBBI->isBundledWithPred())
320 report("BundledPred flag is set, "
321 "but BundledSucc not set on predecessor", MBBI);
322
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000323 // Is this a bundle header?
324 if (!MBBI->isInsideBundle()) {
325 if (CurBundle)
326 visitMachineBundleAfter(CurBundle);
327 CurBundle = MBBI;
328 visitMachineBundleBefore(CurBundle);
329 } else if (!CurBundle)
330 report("No bundle header", MBBI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000331 visitMachineInstrBefore(MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000332 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
333 const MachineInstr &MI = *MBBI;
334 const MachineOperand &Op = MI.getOperand(I);
335 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000336 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000337 // functions when replacing operands of a MachineInstr.
338 report("Instruction has operand with wrong parent set", &MI);
339 }
340
341 visitMachineOperand(&Op, I);
342 }
343
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000344 visitMachineInstrAfter(MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000345
346 // Was this the last bundled instruction?
347 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000348 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000349 if (CurBundle)
350 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000351 if (InBundle)
352 report("BundledSucc flag set on last instruction in block", &MFI->back());
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000353 visitMachineBasicBlockAfter(MFI);
354 }
355 visitMachineFunctionAfter();
356
Owen Anderson21b17882015-02-04 00:02:59 +0000357 if (foundErrors)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000358 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000359
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000360 // Clean up.
361 regsLive.clear();
362 regsDefined.clear();
363 regsDead.clear();
364 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000365 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000366 regsLiveInButUnused.clear();
367 MBBInfoMap.clear();
368
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000369 return false; // no changes
370}
371
Chris Lattner75f40452009-08-23 01:03:30 +0000372void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000373 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000374 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000375 if (!foundErrors++) {
376 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000377 errs() << "# " << Banner << '\n';
378 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000379 }
Owen Anderson21b17882015-02-04 00:02:59 +0000380 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000381 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000382}
383
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000384void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000385 assert(MBB);
386 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000387 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000388 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000389 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000390 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000391 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000392 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000393 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000394}
395
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000396void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000397 assert(MI);
398 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000399 errs() << "- instruction: ";
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000400 if (Indexes && Indexes->hasIndex(MI))
Owen Anderson21b17882015-02-04 00:02:59 +0000401 errs() << Indexes->getInstructionIndex(MI) << '\t';
402 MI->print(errs(), TM);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000403}
404
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000405void MachineVerifier::report(const char *msg,
406 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000407 assert(MO);
408 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000409 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000410 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000411 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000412}
413
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000414void MachineVerifier::report(const char *msg, const MachineFunction *MF,
415 const LiveInterval &LI) {
416 report(msg, MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000417 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000418}
419
420void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
421 const LiveInterval &LI) {
422 report(msg, MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000423 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000424}
425
Matthias Braun364e6e92013-10-10 21:28:54 +0000426void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000427 const LiveRange &LR, unsigned Reg,
428 unsigned LaneMask) {
Matthias Braun364e6e92013-10-10 21:28:54 +0000429 report(msg, MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000430 errs() << "- liverange: " << LR << '\n';
431 errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000432 if (LaneMask != 0)
Owen Anderson21b17882015-02-04 00:02:59 +0000433 errs() << "- lanemask: " << format("%04X\n", LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000434}
435
436void MachineVerifier::report(const char *msg, const MachineFunction *MF,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000437 const LiveRange &LR, unsigned Reg,
438 unsigned LaneMask) {
Matthias Braun364e6e92013-10-10 21:28:54 +0000439 report(msg, MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000440 errs() << "- liverange: " << LR << '\n';
441 errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000442 if (LaneMask != 0)
Owen Anderson21b17882015-02-04 00:02:59 +0000443 errs() << "- lanemask: " << format("%04X\n", LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000444}
445
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000446void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000447 BBInfo &MInfo = MBBInfoMap[MBB];
448 if (!MInfo.reachable) {
449 MInfo.reachable = true;
450 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
451 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
452 markReachable(*SuI);
453 }
454}
455
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000456void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000457 lastIndex = SlotIndex();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000458 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000459
460 // A sub-register of a reserved register is also reserved
461 for (int Reg = regsReserved.find_first(); Reg>=0;
462 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000463 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000464 // FIXME: This should probably be:
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000465 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
466 regsReserved.set(*SubRegs);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000467 }
468 }
Lang Hames1ce837a2012-02-14 19:17:48 +0000469
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000470 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000471
472 // Build a set of the basic blocks in the function.
473 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000474 for (const auto &MBB : *MF) {
475 FunctionBlocks.insert(&MBB);
476 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000477
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000478 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
479 if (MInfo.Preds.size() != MBB.pred_size())
480 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000481
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000482 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
483 if (MInfo.Succs.size() != MBB.succ_size())
484 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000485 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000486
487 // Check that the register use lists are sane.
488 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000489
490 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000491}
492
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000493// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000494static bool matchPair(MachineBasicBlock::const_succ_iterator i,
495 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000496 if (*i == a)
497 return *++i == b;
498 if (*i == b)
499 return *++i == a;
500 return false;
501}
502
503void
504MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000505 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000506
Lang Hames1ce837a2012-02-14 19:17:48 +0000507 if (MRI->isSSA()) {
508 // If this block has allocatable physical registers live-in, check that
509 // it is an entry block or landing pad.
510 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
511 LE = MBB->livein_end();
512 LI != LE; ++LI) {
513 unsigned reg = *LI;
514 if (isAllocatable(reg) && !MBB->isLandingPad() &&
515 MBB != MBB->getParent()->begin()) {
516 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
517 }
518 }
519 }
520
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000521 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000522 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000523 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000524 E = MBB->succ_end(); I != E; ++I) {
525 if ((*I)->isLandingPad())
526 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000527 if (!FunctionBlocks.count(*I))
528 report("MBB has successor that isn't part of the function.", MBB);
529 if (!MBBInfoMap[*I].Preds.count(MBB)) {
530 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000531 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000532 << (*I)->getNumber() << ".\n";
533 }
534 }
535
536 // Check the predecessor list.
537 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
538 E = MBB->pred_end(); I != E; ++I) {
539 if (!FunctionBlocks.count(*I))
540 report("MBB has predecessor that isn't part of the function.", MBB);
541 if (!MBBInfoMap[*I].Succs.count(MBB)) {
542 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000543 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000544 << (*I)->getNumber() << ".\n";
545 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000546 }
Bill Wendling2a401312011-05-04 22:54:05 +0000547
548 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
549 const BasicBlock *BB = MBB->getBasicBlock();
550 if (LandingPadSuccs.size() > 1 &&
551 !(AsmInfo &&
552 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
553 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000554 report("MBB has more than one landing pad successor", MBB);
555
Dan Gohman352a4952009-08-27 02:43:49 +0000556 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000557 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000558 SmallVector<MachineOperand, 4> Cond;
559 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
560 TBB, FBB, Cond)) {
561 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
562 // check whether its answers match up with reality.
563 if (!TBB && !FBB) {
564 // Block falls through to its successor.
565 MachineFunction::const_iterator MBBI = MBB;
566 ++MBBI;
567 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000568 // It's possible that the block legitimately ends with a noreturn
569 // call or an unreachable, in which case it won't actually fall
570 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000571 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000572 // It's possible that the block legitimately ends with a noreturn
573 // call or an unreachable, in which case it won't actuall fall
574 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000575 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000576 report("MBB exits via unconditional fall-through but doesn't have "
577 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000578 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000579 report("MBB exits via unconditional fall-through but its successor "
580 "differs from its CFG successor!", MBB);
581 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000582 if (!MBB->empty() && MBB->back().isBarrier() &&
583 !TII->isPredicated(&MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000584 report("MBB exits via unconditional fall-through but ends with a "
585 "barrier instruction!", MBB);
586 }
587 if (!Cond.empty()) {
588 report("MBB exits via unconditional fall-through but has a condition!",
589 MBB);
590 }
591 } else if (TBB && !FBB && Cond.empty()) {
592 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000593 // If the block has exactly one successor, that happens to be a
594 // landingpad, accept it as valid control flow.
595 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
596 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
597 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000598 report("MBB exits via unconditional branch but doesn't have "
599 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000600 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000601 report("MBB exits via unconditional branch but the CFG "
602 "successor doesn't match the actual successor!", MBB);
603 }
604 if (MBB->empty()) {
605 report("MBB exits via unconditional branch but doesn't contain "
606 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000607 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000608 report("MBB exits via unconditional branch but doesn't end with a "
609 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000610 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000611 report("MBB exits via unconditional branch but the branch isn't a "
612 "terminator instruction!", MBB);
613 }
614 } else if (TBB && !FBB && !Cond.empty()) {
615 // Block conditionally branches somewhere, otherwise falls through.
616 MachineFunction::const_iterator MBBI = MBB;
617 ++MBBI;
618 if (MBBI == MF->end()) {
619 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000620 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000621 // A conditional branch with only one successor is weird, but allowed.
622 if (&*MBBI != TBB)
623 report("MBB exits via conditional branch/fall-through but only has "
624 "one CFG successor!", MBB);
625 else if (TBB != *MBB->succ_begin())
626 report("MBB exits via conditional branch/fall-through but the CFG "
627 "successor don't match the actual successor!", MBB);
628 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000629 report("MBB exits via conditional branch/fall-through but doesn't have "
630 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000631 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000632 report("MBB exits via conditional branch/fall-through but the CFG "
633 "successors don't match the actual successors!", MBB);
634 }
635 if (MBB->empty()) {
636 report("MBB exits via conditional branch/fall-through but doesn't "
637 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000638 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000639 report("MBB exits via conditional branch/fall-through but ends with a "
640 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000641 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000642 report("MBB exits via conditional branch/fall-through but the branch "
643 "isn't a terminator instruction!", MBB);
644 }
645 } else if (TBB && FBB) {
646 // Block conditionally branches somewhere, otherwise branches
647 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000648 if (MBB->succ_size() == 1) {
649 // A conditional branch with only one successor is weird, but allowed.
650 if (FBB != TBB)
651 report("MBB exits via conditional branch/branch through but only has "
652 "one CFG successor!", MBB);
653 else if (TBB != *MBB->succ_begin())
654 report("MBB exits via conditional branch/branch through but the CFG "
655 "successor don't match the actual successor!", MBB);
656 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000657 report("MBB exits via conditional branch/branch but doesn't have "
658 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000659 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000660 report("MBB exits via conditional branch/branch but the CFG "
661 "successors don't match the actual successors!", MBB);
662 }
663 if (MBB->empty()) {
664 report("MBB exits via conditional branch/branch but doesn't "
665 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000666 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000667 report("MBB exits via conditional branch/branch but doesn't end with a "
668 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000669 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000670 report("MBB exits via conditional branch/branch but the branch "
671 "isn't a terminator instruction!", MBB);
672 }
673 if (Cond.empty()) {
674 report("MBB exits via conditinal branch/branch but there's no "
675 "condition!", MBB);
676 }
677 } else {
678 report("AnalyzeBranch returned invalid data!", MBB);
679 }
680 }
681
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000682 regsLive.clear();
Dan Gohman9d2d0532010-04-13 16:57:55 +0000683 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000684 E = MBB->livein_end(); I != E; ++I) {
685 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
686 report("MBB live-in list contains non-physical register", MBB);
687 continue;
688 }
Chad Rosierabdb1d62013-05-22 23:17:36 +0000689 for (MCSubRegIterator SubRegs(*I, TRI, /*IncludeSelf=*/true);
690 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000691 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000692 }
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +0000693 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000694
695 const MachineFrameInfo *MFI = MF->getFrameInfo();
696 assert(MFI && "Function has no frame info");
Matthias Braun111f5d82015-05-28 23:20:35 +0000697 BitVector PR = MFI->getPristineRegs(*MF);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000698 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000699 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
700 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000701 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000702 }
703
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000704 regsKilled.clear();
705 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000706
707 if (Indexes)
708 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000709}
710
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000711// This function gets called for all bundle headers, including normal
712// stand-alone unbundled instructions.
713void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
714 if (Indexes && Indexes->hasIndex(MI)) {
715 SlotIndex idx = Indexes->getInstructionIndex(MI);
716 if (!(idx > lastIndex)) {
717 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000718 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000719 }
720 lastIndex = idx;
721 }
Pete Coopercd720162012-06-07 17:41:39 +0000722
723 // Ensure non-terminators don't follow terminators.
724 // Ignore predicated terminators formed by if conversion.
725 // FIXME: If conversion shouldn't need to violate this rule.
726 if (MI->isTerminator() && !TII->isPredicated(MI)) {
727 if (!FirstTerminator)
728 FirstTerminator = MI;
729 } else if (FirstTerminator) {
730 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000731 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000732 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000733}
734
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000735// The operands on an INLINEASM instruction must follow a template.
736// Verify that the flag operands make sense.
737void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
738 // The first two operands on INLINEASM are the asm string and global flags.
739 if (MI->getNumOperands() < 2) {
740 report("Too few operands on inline asm", MI);
741 return;
742 }
743 if (!MI->getOperand(0).isSymbol())
744 report("Asm string must be an external symbol", MI);
745 if (!MI->getOperand(1).isImm())
746 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000747 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
748 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
749 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000750 report("Unknown asm flags", &MI->getOperand(1), 1);
751
Gabor Horvathfee04342015-03-16 09:53:42 +0000752 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000753
754 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
755 unsigned NumOps;
756 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
757 const MachineOperand &MO = MI->getOperand(OpNo);
758 // There may be implicit ops after the fixed operands.
759 if (!MO.isImm())
760 break;
761 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
762 }
763
764 if (OpNo > MI->getNumOperands())
765 report("Missing operands in last group", MI);
766
767 // An optional MDNode follows the groups.
768 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
769 ++OpNo;
770
771 // All trailing operands must be implicit registers.
772 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
773 const MachineOperand &MO = MI->getOperand(OpNo);
774 if (!MO.isReg() || !MO.isImplicit())
775 report("Expected implicit register after groups", &MO, OpNo);
776 }
777}
778
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000779void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000780 const MCInstrDesc &MCID = MI->getDesc();
781 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000782 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000783 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000784 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000785 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000786
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000787 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000788 if (MI->isInlineAsm())
789 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000790
Dan Gohmandb9493c2009-10-07 17:36:00 +0000791 // Check the MachineMemOperands for basic consistency.
792 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
793 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000794 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000795 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000796 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000797 report("Missing mayStore flag", MI);
798 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000799
800 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000801 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000802 if (LiveInts) {
803 bool mapped = !LiveInts->isNotInMIMap(MI);
804 if (MI->isDebugValue()) {
805 if (mapped)
806 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000807 } else if (MI->isInsideBundle()) {
808 if (mapped)
809 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000810 } else {
811 if (!mapped)
812 report("Missing slot index", MI);
813 }
814 }
815
Andrew Trick924123a2011-09-21 02:20:46 +0000816 StringRef ErrorInfo;
817 if (!TII->verifyInstruction(MI, ErrorInfo))
818 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000819}
820
821void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000822MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000823 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000824 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000825
Evan Cheng6cc775f2011-06-28 19:10:37 +0000826 // The first MCID.NumDefs operands must be explicit register defines
827 if (MONum < MCID.getNumDefs()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000828 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000829 if (!MO->isReg())
830 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +0000831 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000832 report("Explicit definition marked as use", MO, MONum);
833 else if (MO->isImplicit())
834 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000835 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000836 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +0000837 // Don't check if it's the last operand in a variadic instruction. See,
838 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000839 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000840 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000841 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +0000842 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000843 if (MO->isImplicit())
844 report("Explicit operand marked as implicit", MO, MONum);
845 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000846
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000847 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
848 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000849 if (!MO->isReg())
850 report("Tied use must be a register", MO, MONum);
851 else if (!MO->isTied())
852 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000853 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
854 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000855 } else if (MO->isReg() && MO->isTied())
856 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000857 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +0000858 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000859 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000860 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000861 }
862
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000863 switch (MO->getType()) {
864 case MachineOperand::MO_Register: {
865 const unsigned Reg = MO->getReg();
866 if (!Reg)
867 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000868 if (MRI->tracksLiveness() && !MI->isDebugValue())
869 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000870
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000871 // Verify the consistency of tied operands.
872 if (MO->isTied()) {
873 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
874 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
875 if (!OtherMO.isReg())
876 report("Must be tied to a register", MO, MONum);
877 if (!OtherMO.isTied())
878 report("Missing tie flags on tied operand", MO, MONum);
879 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
880 report("Inconsistent tie links", MO, MONum);
881 if (MONum < MCID.getNumDefs()) {
882 if (OtherIdx < MCID.getNumOperands()) {
883 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
884 report("Explicit def tied to explicit use without tie constraint",
885 MO, MONum);
886 } else {
887 if (!OtherMO.isImplicit())
888 report("Explicit def should be tied to implicit use", MO, MONum);
889 }
890 }
891 }
892
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +0000893 // Verify two-address constraints after leaving SSA form.
894 unsigned DefIdx;
895 if (!MRI->isSSA() && MO->isUse() &&
896 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
897 Reg != MI->getOperand(DefIdx).getReg())
898 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000899
900 // Check register classes.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000901 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000902 unsigned SubIdx = MO->getSubReg();
903
904 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000905 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000906 report("Illegal subregister index for physical register", MO, MONum);
907 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000908 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000909 if (const TargetRegisterClass *DRC =
910 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000911 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000912 report("Illegal physical register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000913 errs() << TRI->getName(Reg) << " is not a "
Craig Toppercf0444b2014-11-17 05:50:14 +0000914 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000915 }
916 }
917 } else {
918 // Virtual register.
919 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
920 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000921 const TargetRegisterClass *SRC =
922 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +0000923 if (!SRC) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000924 report("Invalid subregister index for virtual register", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000925 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +0000926 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000927 return;
928 }
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000929 if (RC != SRC) {
930 report("Invalid register class for subregister index", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000931 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000932 << " does not fully support subreg index " << SubIdx << "\n";
933 return;
934 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000935 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000936 if (const TargetRegisterClass *DRC =
937 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000938 if (SubIdx) {
939 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +0000940 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000941 if (!SuperRC) {
942 report("No largest legal super class exists.", MO, MONum);
943 return;
944 }
945 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
946 if (!DRC) {
947 report("No matching super-reg register class.", MO, MONum);
948 return;
949 }
950 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +0000951 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000952 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000953 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +0000954 << " register, but got a " << TRI->getRegClassName(RC)
955 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000956 }
957 }
958 }
959 }
960 break;
961 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000962
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000963 case MachineOperand::MO_RegisterMask:
964 regMasks.push_back(MO->getRegMask());
965 break;
966
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000967 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +0000968 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
969 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000970 break;
971
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000972 case MachineOperand::MO_FrameIndex:
973 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
974 LiveInts && !LiveInts->isNotInMIMap(MI)) {
975 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
976 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000977 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000978 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000979 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000980 }
Evan Cheng7f8e5632011-12-07 07:15:52 +0000981 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000982 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000983 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000984 }
985 }
986 break;
987
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000988 default:
989 break;
990 }
991}
992
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000993void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
994 const MachineInstr *MI = MO->getParent();
995 const unsigned Reg = MO->getReg();
996
997 // Both use and def operands can read a register.
998 if (MO->readsReg()) {
999 regsLiveInButUnused.erase(Reg);
1000
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001001 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001002 addRegWithSubRegs(regsKilled, Reg);
1003
1004 // Check that LiveVars knows this kill.
1005 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1006 MO->isKill()) {
1007 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1008 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
1009 report("Kill missing from LiveVariables", MO, MONum);
1010 }
1011
1012 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001013 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
1014 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
1015 // Check the cached regunit intervals.
1016 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1017 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun34e1be92013-10-10 21:29:02 +00001018 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units)) {
1019 LiveQueryResult LRQ = LR->Query(UseIdx);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001020 if (!LRQ.valueIn()) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001021 report("No live segment at use", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001022 errs() << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
Matthias Braun34e1be92013-10-10 21:29:02 +00001023 << ' ' << *LR << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001024 }
1025 if (MO->isKill() && !LRQ.isKill()) {
1026 report("Live range continues after kill flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001027 errs() << PrintRegUnit(*Units, TRI) << ' ' << *LR << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001028 }
1029 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001030 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001031 }
1032
1033 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1034 if (LiveInts->hasInterval(Reg)) {
1035 // This is a virtual register interval.
1036 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun88dd0ab2013-10-10 21:28:52 +00001037 LiveQueryResult LRQ = LI.Query(UseIdx);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001038 if (!LRQ.valueIn()) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001039 report("No live segment at use", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001040 errs() << UseIdx << " is not live in " << LI << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001041 }
1042 // Check for extra kill flags.
1043 // Note that we allow missing kill flags for now.
1044 if (MO->isKill() && !LRQ.isKill()) {
1045 report("Live range continues after kill flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001046 errs() << "Live range: " << LI << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001047 }
1048 } else {
1049 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001050 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001051 }
1052 }
1053
1054 // Use of a dead register.
1055 if (!regsLive.count(Reg)) {
1056 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1057 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001058 bool Bad = !isReserved(Reg);
1059 // We are fine if just any subregister has a defined value.
1060 if (Bad) {
1061 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1062 ++SubRegs) {
1063 if (regsLive.count(*SubRegs)) {
1064 Bad = false;
1065 break;
1066 }
1067 }
1068 }
Matthias Braun96a31952015-01-14 22:25:14 +00001069 // If there is an additional implicit-use of a super register we stop
1070 // here. By definition we are fine if the super register is not
1071 // (completely) dead, if the complete super register is dead we will
1072 // get a report for its operand.
1073 if (Bad) {
1074 for (const MachineOperand &MOP : MI->uses()) {
1075 if (!MOP.isReg())
1076 continue;
1077 if (!MOP.isImplicit())
1078 continue;
1079 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1080 ++SubRegs) {
1081 if (*SubRegs == Reg) {
1082 Bad = false;
1083 break;
1084 }
1085 }
1086 }
1087 }
Matthias Braun96d77322014-12-10 01:13:13 +00001088 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001089 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001090 } else if (MRI->def_empty(Reg)) {
1091 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001092 } else {
1093 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1094 // We don't know which virtual registers are live in, so only complain
1095 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1096 // must be live in. PHI instructions are handled separately.
1097 if (MInfo.regsKilled.count(Reg))
1098 report("Using a killed virtual register", MO, MONum);
1099 else if (!MI->isPHI())
1100 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1101 }
1102 }
1103 }
1104
1105 if (MO->isDef()) {
1106 // Register defined.
1107 // TODO: verify that earlyclobber ops are not used.
1108 if (MO->isDead())
1109 addRegWithSubRegs(regsDead, Reg);
1110 else
1111 addRegWithSubRegs(regsDefined, Reg);
1112
1113 // Verify SSA form.
1114 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001115 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001116 report("Multiple virtual register defs in SSA form", MO, MONum);
1117
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001118 // Check LiveInts for a live segment, but only for virtual registers.
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001119 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1120 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001121 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1122 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001123 if (LiveInts->hasInterval(Reg)) {
1124 const LiveInterval &LI = LiveInts->getInterval(Reg);
1125 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1126 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001127 if (VNI->def != DefIdx) {
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001128 report("Inconsistent valno->def", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001129 errs() << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001130 << DefIdx << " in " << LI << '\n';
1131 }
1132 } else {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001133 report("No live segment at def", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001134 errs() << DefIdx << " is not live in " << LI << '\n';
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001135 }
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001136 // Check that, if the dead def flag is present, LiveInts agree.
1137 if (MO->isDead()) {
1138 LiveQueryResult LRQ = LI.Query(DefIdx);
1139 if (!LRQ.isDeadDef()) {
1140 report("Live range continues after dead def flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001141 errs() << "Live range: " << LI << '\n';
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001142 }
1143 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001144 } else {
1145 report("Virtual register has no Live interval", MO, MONum);
1146 }
1147 }
1148 }
1149}
1150
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001151void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001152}
1153
1154// This function gets called after visiting all instructions in a bundle. The
1155// argument points to the bundle header.
1156// Normal stand-alone instructions are also considered 'bundles', and this
1157// function is called for all of them.
1158void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001159 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1160 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001161 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001162 // Kill any masked registers.
1163 while (!regMasks.empty()) {
1164 const uint32_t *Mask = regMasks.pop_back_val();
1165 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1166 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1167 MachineOperand::clobbersPhysReg(Mask, *I))
1168 regsDead.push_back(*I);
1169 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001170 set_subtract(regsLive, regsDead); regsDead.clear();
1171 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001172}
1173
1174void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001175MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001176 MBBInfoMap[MBB].regsLiveOut = regsLive;
1177 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001178
1179 if (Indexes) {
1180 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1181 if (!(stop > lastIndex)) {
1182 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001183 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001184 << " last instruction was at " << lastIndex << '\n';
1185 }
1186 lastIndex = stop;
1187 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001188}
1189
1190// Calculate the largest possible vregsPassed sets. These are the registers that
1191// can pass through an MBB live, but may not be live every time. It is assumed
1192// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001193void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001194 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1195 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001196 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001197 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001198 BBInfo &MInfo = MBBInfoMap[&MBB];
1199 if (!MInfo.reachable)
1200 continue;
1201 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1202 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1203 BBInfo &SInfo = MBBInfoMap[*SuI];
1204 if (SInfo.addPassed(MInfo.regsLiveOut))
1205 todo.insert(*SuI);
1206 }
1207 }
1208
1209 // Iteratively push vregsPassed to successors. This will converge to the same
1210 // final state regardless of DenseSet iteration order.
1211 while (!todo.empty()) {
1212 const MachineBasicBlock *MBB = *todo.begin();
1213 todo.erase(MBB);
1214 BBInfo &MInfo = MBBInfoMap[MBB];
1215 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1216 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1217 if (*SuI == MBB)
1218 continue;
1219 BBInfo &SInfo = MBBInfoMap[*SuI];
1220 if (SInfo.addPassed(MInfo.vregsPassed))
1221 todo.insert(*SuI);
1222 }
1223 }
1224}
1225
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001226// Calculate the set of virtual registers that must be passed through each basic
1227// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001228// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001229void MachineVerifier::calcRegsRequired() {
1230 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001231 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001232 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001233 BBInfo &MInfo = MBBInfoMap[&MBB];
1234 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1235 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1236 BBInfo &PInfo = MBBInfoMap[*PrI];
1237 if (PInfo.addRequired(MInfo.vregsLiveIn))
1238 todo.insert(*PrI);
1239 }
1240 }
1241
1242 // Iteratively push vregsRequired to predecessors. This will converge to the
1243 // same final state regardless of DenseSet iteration order.
1244 while (!todo.empty()) {
1245 const MachineBasicBlock *MBB = *todo.begin();
1246 todo.erase(MBB);
1247 BBInfo &MInfo = MBBInfoMap[MBB];
1248 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1249 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1250 if (*PrI == MBB)
1251 continue;
1252 BBInfo &SInfo = MBBInfoMap[*PrI];
1253 if (SInfo.addRequired(MInfo.vregsRequired))
1254 todo.insert(*PrI);
1255 }
1256 }
1257}
1258
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001259// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001260// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001261void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001262 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001263 for (const auto &BBI : *MBB) {
1264 if (!BBI.isPHI())
1265 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001266 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001267
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001268 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1269 unsigned Reg = BBI.getOperand(i).getReg();
1270 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001271 if (!Pre->isSuccessor(MBB))
1272 continue;
1273 seen.insert(Pre);
1274 BBInfo &PrInfo = MBBInfoMap[Pre];
1275 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1276 report("PHI operand is not live-out from predecessor",
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001277 &BBI.getOperand(i), i);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001278 }
1279
1280 // Did we see all predecessors?
1281 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1282 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1283 if (!seen.count(*PrI)) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001284 report("Missing PHI operand", &BBI);
Owen Anderson21b17882015-02-04 00:02:59 +00001285 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001286 << " is a predecessor according to the CFG.\n";
1287 }
1288 }
1289 }
1290}
1291
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001292void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001293 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001294
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001295 for (const auto &MBB : *MF) {
1296 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001297
1298 // Skip unreachable MBBs.
1299 if (!MInfo.reachable)
1300 continue;
1301
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001302 checkPHIOps(&MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001303 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001304
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001305 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001306 calcRegsRequired();
1307
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001308 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001309 for (const auto &MBB : *MF) {
1310 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001311 for (RegSet::iterator
1312 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1313 ++I)
1314 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001315 report("Virtual register killed in block, but needed live out.", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001316 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001317 << " is used after the block.\n";
1318 }
1319 }
1320
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001321 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001322 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1323 for (RegSet::iterator
1324 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesen99014ff2012-03-10 00:44:11 +00001325 ++I)
1326 report("Virtual register def doesn't dominate all uses.",
1327 MRI->getVRegDef(*I));
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001328 }
1329
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001330 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001331 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001332 if (LiveInts)
1333 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001334}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001335
1336void MachineVerifier::verifyLiveVariables() {
1337 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001338 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1339 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001340 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001341 for (const auto &MBB : *MF) {
1342 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001343
1344 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1345 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001346 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1347 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001348 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001349 << " must be live through the block.\n";
1350 }
1351 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001352 if (VI.AliveBlocks.test(MBB.getNumber())) {
1353 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001354 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001355 << " is not needed live through the block.\n";
1356 }
1357 }
1358 }
1359 }
1360}
1361
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001362void MachineVerifier::verifyLiveIntervals() {
1363 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001364 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1365 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001366
1367 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001368 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001369 continue;
1370
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001371 if (!LiveInts->hasInterval(Reg)) {
1372 report("Missing live interval for virtual register", MF);
Owen Anderson21b17882015-02-04 00:02:59 +00001373 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001374 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001375 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001376
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001377 const LiveInterval &LI = LiveInts->getInterval(Reg);
1378 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001379 verifyLiveInterval(LI);
1380 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001381
1382 // Verify all the cached regunit intervals.
1383 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001384 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1385 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001386}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001387
Matthias Braun364e6e92013-10-10 21:28:54 +00001388void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001389 const VNInfo *VNI, unsigned Reg,
1390 unsigned LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001391 if (VNI->isUnused())
1392 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001393
Matthias Braun364e6e92013-10-10 21:28:54 +00001394 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001395
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001396 if (!DefVNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001397 report("Valno not live at def and not marked unused", MF, LR, Reg,
1398 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001399 errs() << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001400 return;
1401 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001402
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001403 if (DefVNI != VNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001404 report("Live segment at def has different valno", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001405 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +00001406 << " where valno #" << DefVNI->id << " is live\n";
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001407 return;
1408 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001409
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001410 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1411 if (!MBB) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001412 report("Invalid definition index", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001413 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Matthias Braun364e6e92013-10-10 21:28:54 +00001414 << " in " << LR << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001415 return;
1416 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001417
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001418 if (VNI->isPHIDef()) {
1419 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001420 report("PHIDef value is not defined at MBB start", MBB, LR, Reg,
1421 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001422 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +00001423 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001424 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001425 return;
1426 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001427
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001428 // Non-PHI def.
1429 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1430 if (!MI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001431 report("No instruction at def index", MBB, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001432 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001433 return;
1434 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001435
Matthias Braun364e6e92013-10-10 21:28:54 +00001436 if (Reg != 0) {
1437 bool hasDef = false;
1438 bool isEarlyClobber = false;
1439 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1440 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001441 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001442 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1443 if (MOI->getReg() != Reg)
1444 continue;
1445 } else {
1446 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1447 !TRI->hasRegUnit(MOI->getReg(), Reg))
1448 continue;
1449 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001450 if (LaneMask != 0 &&
1451 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask) == 0)
1452 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001453 hasDef = true;
1454 if (MOI->isEarlyClobber())
1455 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001456 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001457
Matthias Braun364e6e92013-10-10 21:28:54 +00001458 if (!hasDef) {
1459 report("Defining instruction does not modify register", MI);
Owen Anderson21b17882015-02-04 00:02:59 +00001460 errs() << "Valno #" << VNI->id << " in " << LR << '\n';
Matthias Braun364e6e92013-10-10 21:28:54 +00001461 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001462
Matthias Braun364e6e92013-10-10 21:28:54 +00001463 // Early clobber defs begin at USE slots, but other defs must begin at
1464 // DEF slots.
1465 if (isEarlyClobber) {
1466 if (!VNI->def.isEarlyClobber()) {
Matthias Braun47760d92014-11-19 19:46:13 +00001467 report("Early clobber def must be at an early-clobber slot", MBB, LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001468 Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001469 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Matthias Braun364e6e92013-10-10 21:28:54 +00001470 }
1471 } else if (!VNI->def.isRegister()) {
1472 report("Non-PHI, non-early clobber def must be at a register slot",
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001473 MBB, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001474 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001475 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001476 }
1477}
1478
Matthias Braun364e6e92013-10-10 21:28:54 +00001479void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1480 const LiveRange::const_iterator I,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001481 unsigned Reg, unsigned LaneMask) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001482 const LiveRange::Segment &S = *I;
1483 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001484 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001485
Matthias Braun364e6e92013-10-10 21:28:54 +00001486 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001487 report("Foreign valno in live segment", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001488 errs() << S << " has a bad valno\n";
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001489 }
1490
1491 if (VNI->isUnused()) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001492 report("Live segment valno is marked unused", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001493 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001494 }
1495
Matthias Braun364e6e92013-10-10 21:28:54 +00001496 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001497 if (!MBB) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001498 report("Bad start of live segment, no basic block", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001499 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001500 return;
1501 }
1502 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001503 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001504 report("Live segment must begin at MBB entry or valno def", MBB, LR, Reg,
1505 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001506 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001507 }
1508
1509 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001510 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001511 if (!EndMBB) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001512 report("Bad end of live segment, no basic block", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001513 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001514 return;
1515 }
1516
1517 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001518 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001519 return;
1520
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001521 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001522 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1523 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001524 return;
1525
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001526 // The live segment is ending inside EndMBB
1527 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001528 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001529 if (!MI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001530 report("Live segment doesn't end at a valid instruction", EndMBB, LR, Reg,
1531 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001532 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001533 return;
1534 }
1535
1536 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001537 if (S.end.isBlock()) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001538 report("Live segment ends at B slot of an instruction", EndMBB, LR, Reg,
1539 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001540 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001541 }
1542
Matthias Braun364e6e92013-10-10 21:28:54 +00001543 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001544 // Segment ends on the dead slot.
1545 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001546 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun47760d92014-11-19 19:46:13 +00001547 report("Live segment ending at dead slot spans instructions", EndMBB, LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001548 Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001549 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001550 }
1551 }
1552
1553 // A live segment can only end at an early-clobber slot if it is being
1554 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001555 if (S.end.isEarlyClobber()) {
1556 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001557 report("Live segment ending at early clobber slot must be "
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001558 "redefined by an EC def in the same instruction", EndMBB, LR, Reg,
1559 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001560 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001561 }
1562 }
1563
1564 // The following checks only apply to virtual registers. Physreg liveness
1565 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001566 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001567 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001568 // use, or a dead flag on a def.
1569 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001570 bool hasSubRegDef = false;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001571 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001572 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001573 continue;
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001574 if (LaneMask != 0 &&
1575 (LaneMask & TRI->getSubRegIndexLaneMask(MOI->getSubReg())) == 0)
1576 continue;
Matthias Braun21554d92014-12-10 01:13:11 +00001577 if (MOI->isDef() && MOI->getSubReg() != 0)
1578 hasSubRegDef = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001579 if (MOI->readsReg())
1580 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001581 }
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001582 if (!S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001583 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001584 // When tracking subregister liveness, the main range must start new
1585 // values on partial register writes, even if there is no read.
Matthias Brauna25e13a2015-03-19 00:21:58 +00001586 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask != 0 ||
1587 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001588 report("Instruction ending live segment doesn't read the register",
1589 MI);
Owen Anderson21b17882015-02-04 00:02:59 +00001590 errs() << S << " in " << LR << '\n';
Matthias Braun21554d92014-12-10 01:13:11 +00001591 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001592 }
1593 }
1594 }
1595
1596 // Now check all the basic blocks in this live segment.
1597 MachineFunction::const_iterator MFI = MBB;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001598 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00001599 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001600 // Not live-in to any blocks.
1601 if (MBB == EndMBB)
1602 return;
1603 // Skip this block.
1604 ++MFI;
1605 }
1606 for (;;) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001607 assert(LiveInts->isLiveInToMBB(LR, MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001608 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00001609 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001610 MFI->isLandingPad()) {
1611 if (&*MFI == EndMBB)
1612 break;
1613 ++MFI;
1614 continue;
1615 }
1616
1617 // Is VNI a PHI-def in the current block?
1618 bool IsPHI = VNI->isPHIDef() &&
1619 VNI->def == LiveInts->getMBBStartIdx(MFI);
1620
1621 // Check that VNI is live-out of all predecessors.
1622 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1623 PE = MFI->pred_end(); PI != PE; ++PI) {
1624 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001625 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001626
1627 // All predecessors must have a live-out value.
1628 if (!PVNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001629 report("Register not marked live out of predecessor", *PI, LR, Reg,
1630 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001631 errs() << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001632 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +00001633 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001634 continue;
1635 }
1636
1637 // Only PHI-defs can take different predecessor values.
1638 if (!IsPHI && PVNI != VNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001639 report("Different value live out of predecessor", *PI, LR, Reg,
1640 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001641 errs() << "Valno #" << PVNI->id << " live out of BB#"
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001642 << (*PI)->getNumber() << '@' << PEnd
1643 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +00001644 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001645 }
1646 }
1647 if (&*MFI == EndMBB)
1648 break;
1649 ++MFI;
1650 }
1651}
1652
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001653void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
1654 unsigned LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00001655 for (const VNInfo *VNI : LR.valnos)
1656 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001657
Matthias Braun364e6e92013-10-10 21:28:54 +00001658 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001659 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00001660}
1661
1662void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001663 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00001664 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1665 verifyLiveRange(LI, Reg);
1666
1667 unsigned Mask = 0;
1668 unsigned MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
1669 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1670 if ((Mask & SR.LaneMask) != 0)
1671 report("Lane masks of sub ranges overlap in live interval", MF, LI);
1672 if ((SR.LaneMask & ~MaxMask) != 0)
1673 report("Subrange lanemask is invalid", MF, LI);
1674 Mask |= SR.LaneMask;
1675 verifyLiveRange(SR, LI.reg, SR.LaneMask);
1676 if (!LI.covers(SR))
1677 report("A Subrange is not covered by the main range", MF, LI);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001678 }
1679
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001680 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00001681 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1682 unsigned NumComp = ConEQ.Classify(&LI);
1683 if (NumComp > 1) {
1684 report("Multiple connected components in live interval", MF, LI);
1685 for (unsigned comp = 0; comp != NumComp; ++comp) {
1686 errs() << comp << ": valnos";
1687 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1688 E = LI.vni_end(); I!=E; ++I)
1689 if (comp == ConEQ.getEqClass(*I))
1690 errs() << ' ' << (*I)->id;
1691 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00001692 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001693 }
1694}
Manman Renaa6875b2013-07-15 21:26:31 +00001695
1696namespace {
1697 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1698 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1699 // value is zero.
1700 // We use a bool plus an integer to capture the stack state.
1701 struct StackStateOfBB {
1702 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1703 ExitIsSetup(false) { }
1704 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1705 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1706 ExitIsSetup(ExitSetup) { }
1707 // Can be negative, which means we are setting up a frame.
1708 int EntryValue;
1709 int ExitValue;
1710 bool EntryIsSetup;
1711 bool ExitIsSetup;
1712 };
1713}
1714
1715/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1716/// by a FrameDestroy <n>, stack adjustments are identical on all
1717/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1718void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001719 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1720 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Manman Renaa6875b2013-07-15 21:26:31 +00001721
1722 SmallVector<StackStateOfBB, 8> SPState;
1723 SPState.resize(MF->getNumBlockIDs());
1724 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1725
1726 // Visit the MBBs in DFS order.
1727 for (df_ext_iterator<const MachineFunction*,
1728 SmallPtrSet<const MachineBasicBlock*, 8> >
1729 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1730 DFI != DFE; ++DFI) {
1731 const MachineBasicBlock *MBB = *DFI;
1732
1733 StackStateOfBB BBState;
1734 // Check the exit state of the DFS stack predecessor.
1735 if (DFI.getPathLength() >= 2) {
1736 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1737 assert(Reachable.count(StackPred) &&
1738 "DFS stack predecessor is already visited.\n");
1739 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1740 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1741 BBState.ExitValue = BBState.EntryValue;
1742 BBState.ExitIsSetup = BBState.EntryIsSetup;
1743 }
1744
1745 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001746 for (const auto &I : *MBB) {
1747 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001748 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001749 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001750 assert(Size >= 0 &&
1751 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1752
1753 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001754 report("FrameSetup is after another FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001755 BBState.ExitValue -= Size;
1756 BBState.ExitIsSetup = true;
1757 }
1758
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001759 if (I.getOpcode() == FrameDestroyOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001760 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001761 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001762 assert(Size >= 0 &&
1763 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1764
1765 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001766 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001767 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1768 BBState.ExitValue;
1769 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001770 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00001771 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00001772 << AbsSPAdj << ">.\n";
1773 }
1774 BBState.ExitValue += Size;
1775 BBState.ExitIsSetup = false;
1776 }
1777 }
1778 SPState[MBB->getNumber()] = BBState;
1779
1780 // Make sure the exit state of any predecessor is consistent with the entry
1781 // state.
1782 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
1783 E = MBB->pred_end(); I != E; ++I) {
1784 if (Reachable.count(*I) &&
1785 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
1786 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
1787 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001788 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00001789 << SPState[(*I)->getNumber()].ExitValue << ", "
1790 << SPState[(*I)->getNumber()].ExitIsSetup
1791 << "), while BB#" << MBB->getNumber() << " has entry state ("
1792 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
1793 }
1794 }
1795
1796 // Make sure the entry state of any successor is consistent with the exit
1797 // state.
1798 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
1799 E = MBB->succ_end(); I != E; ++I) {
1800 if (Reachable.count(*I) &&
1801 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
1802 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
1803 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001804 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00001805 << SPState[(*I)->getNumber()].EntryValue << ", "
1806 << SPState[(*I)->getNumber()].EntryIsSetup
1807 << "), while BB#" << MBB->getNumber() << " has exit state ("
1808 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
1809 }
1810 }
1811
1812 // Make sure a basic block with return ends with zero stack adjustment.
1813 if (!MBB->empty() && MBB->back().isReturn()) {
1814 if (BBState.ExitIsSetup)
1815 report("A return block ends with a FrameSetup.", MBB);
1816 if (BBState.ExitValue)
1817 report("A return block ends with a nonzero stack adjustment.", MBB);
1818 }
1819 }
1820}