blob: ab433273b189692149502b7c68fd1700c7549548 [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"
David Majnemer70497c62015-12-02 23:06:39 +000031#include "llvm/Analysis/EHPersonalities.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/CodeGen/LiveIntervalAnalysis.h"
33#include "llvm/CodeGen/LiveStackAnalysis.h"
34#include "llvm/CodeGen/LiveVariables.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/CodeGen/MachineMemOperand.h"
38#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/InlineAsm.h"
41#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000043#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000044#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000045#include "llvm/Support/FileSystem.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
Matthias Braunb3aefc32016-02-15 19:25:31 +000061 unsigned verify(MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000062
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
Ahmed Bougacha3681c772016-08-02 16:17:15 +000073 // Avoid querying the MachineFunctionProperties for each operand.
74 bool isFunctionRegBankSelected;
Ahmed Bougachab14e9442016-08-02 16:49:22 +000075 bool isFunctionSelected;
Ahmed Bougacha3681c772016-08-02 16:17:15 +000076
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000077 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000078 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000079 typedef DenseSet<unsigned> RegSet;
80 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000081 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000082
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000083 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000084 BlockSet FunctionBlocks;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000085
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000086 BitVector regsReserved;
87 RegSet regsLive;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000088 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000089 RegMaskVector regMasks;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000090 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000091
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +000092 SlotIndex lastIndex;
93
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000094 // Add Reg and any sub-registers to RV
95 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
96 RV.push_back(Reg);
97 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +000098 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
99 RV.push_back(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000100 }
101
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000102 struct BBInfo {
103 // Is this MBB reachable from the MF entry point?
104 bool reachable;
105
106 // Vregs that must be live in because they are used without being
107 // defined. Map value is the user.
108 RegMap vregsLiveIn;
109
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000110 // Regs killed in MBB. They may be defined again, and will then be in both
111 // regsKilled and regsLiveOut.
112 RegSet regsKilled;
113
114 // Regs defined in MBB and live out. Note that vregs passing through may
115 // be live out without being mentioned here.
116 RegSet regsLiveOut;
117
118 // Vregs that pass through MBB untouched. This set is disjoint from
119 // regsKilled and regsLiveOut.
120 RegSet vregsPassed;
121
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000122 // Vregs that must pass through MBB because they are needed by a successor
123 // block. This set is disjoint from regsLiveOut.
124 RegSet vregsRequired;
125
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000126 // Set versions of block's predecessor and successor lists.
127 BlockSet Preds, Succs;
128
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000129 BBInfo() : reachable(false) {}
130
131 // Add register to vregsPassed if it belongs there. Return true if
132 // anything changed.
133 bool addPassed(unsigned Reg) {
134 if (!TargetRegisterInfo::isVirtualRegister(Reg))
135 return false;
136 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
137 return false;
138 return vregsPassed.insert(Reg).second;
139 }
140
141 // Same for a full set.
142 bool addPassed(const RegSet &RS) {
143 bool changed = false;
144 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
145 if (addPassed(*I))
146 changed = true;
147 return changed;
148 }
149
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000150 // Add register to vregsRequired if it belongs there. Return true if
151 // anything changed.
152 bool addRequired(unsigned Reg) {
153 if (!TargetRegisterInfo::isVirtualRegister(Reg))
154 return false;
155 if (regsLiveOut.count(Reg))
156 return false;
157 return vregsRequired.insert(Reg).second;
158 }
159
160 // Same for a full set.
161 bool addRequired(const RegSet &RS) {
162 bool changed = false;
163 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
164 if (addRequired(*I))
165 changed = true;
166 return changed;
167 }
168
169 // Same for a full map.
170 bool addRequired(const RegMap &RM) {
171 bool changed = false;
172 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
173 if (addRequired(I->first))
174 changed = true;
175 return changed;
176 }
177
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000178 // Live-out registers are either in regsLiveOut or vregsPassed.
179 bool isLiveOut(unsigned Reg) const {
180 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
181 }
182 };
183
184 // Extra register info per MBB.
185 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
186
187 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000188 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000189 }
190
Matthias Braun4682ac62017-05-05 22:04:05 +0000191 bool isAllocatable(unsigned Reg) const {
192 return Reg < TRI->getNumRegs() && TRI->isInAllocatableClass(Reg) &&
193 !regsReserved.test(Reg);
Lang Hames1ce837a2012-02-14 19:17:48 +0000194 }
195
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000196 // Analysis information if available
197 LiveVariables *LiveVars;
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +0000198 LiveIntervals *LiveInts;
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000199 LiveStacks *LiveStks;
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000200 SlotIndexes *Indexes;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000201
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000202 void visitMachineFunctionBefore();
203 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000204 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000205 void visitMachineInstrBefore(const MachineInstr *MI);
206 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
207 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000208 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000209 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
210 void visitMachineFunctionAfter();
211
212 void report(const char *msg, const MachineFunction *MF);
213 void report(const char *msg, const MachineBasicBlock *MBB);
214 void report(const char *msg, const MachineInstr *MI);
215 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Matthias Braun7e624d52015-11-09 23:59:33 +0000216
217 void report_context(const LiveInterval &LI) const;
Matt Arsenault892fcd02016-07-25 19:39:01 +0000218 void report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000219 LaneBitmask LaneMask) const;
220 void report_context(const LiveRange::Segment &S) const;
221 void report_context(const VNInfo &VNI) const;
Matthias Braun579c9cd2016-02-02 02:44:25 +0000222 void report_context(SlotIndex Pos) const;
223 void report_context_liverange(const LiveRange &LR) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000224 void report_context_lanemask(LaneBitmask LaneMask) const;
Matthias Braun30668dd2016-05-11 21:31:39 +0000225 void report_context_vreg(unsigned VReg) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000226 void report_context_vreg_regunit(unsigned VRegOrRegUnit) const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000227
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000228 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000229
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000230 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Matthias Braun1377fd62016-02-02 20:04:51 +0000231 void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
232 SlotIndex UseIdx, const LiveRange &LR, unsigned Reg,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000233 LaneBitmask LaneMask = LaneBitmask::getNone());
Matthias Braun1377fd62016-02-02 20:04:51 +0000234 void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
235 SlotIndex DefIdx, const LiveRange &LR, unsigned Reg,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000236 LaneBitmask LaneMask = LaneBitmask::getNone());
Matthias Braun1377fd62016-02-02 20:04:51 +0000237
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000238 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000239 void calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000240 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000241
242 void calcRegsRequired();
243 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000244 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000245 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000246 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000247 LaneBitmask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000248 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000249 const LiveRange::const_iterator I, unsigned,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000250 LaneBitmask);
251 void verifyLiveRange(const LiveRange&, unsigned,
252 LaneBitmask LaneMask = LaneBitmask::getNone());
Manman Renaa6875b2013-07-15 21:26:31 +0000253
254 void verifyStackFrame();
Matthias Braun80595462015-09-09 17:49:46 +0000255
256 void verifySlotIndexes() const;
Derek Schuff42666ee2016-03-29 17:40:22 +0000257 void verifyProperties(const MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000258 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000259
260 struct MachineVerifierPass : public MachineFunctionPass {
261 static char ID; // Pass ID, replacement for typeid
Matthias Brauna4e932d2014-12-11 19:41:51 +0000262 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000263
Sven van Haastregt04bfa872017-03-29 15:25:06 +0000264 MachineVerifierPass(std::string banner = std::string())
Sven van Haastregt039a6d92017-03-29 09:08:25 +0000265 : MachineFunctionPass(ID), Banner(std::move(banner)) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000266 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
267 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000268
Craig Topper4584cd52014-03-07 09:26:03 +0000269 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000270 AU.setPreservesAll();
271 MachineFunctionPass::getAnalysisUsage(AU);
272 }
273
Craig Topper4584cd52014-03-07 09:26:03 +0000274 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Braunb3aefc32016-02-15 19:25:31 +0000275 unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
276 if (FoundErrors)
277 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000278 return false;
279 }
280 };
281
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000282}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000283
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000284char MachineVerifierPass::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +0000285INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000286 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000287
Matthias Brauna4e932d2014-12-11 19:41:51 +0000288FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000289 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000290}
291
Matthias Braunb3aefc32016-02-15 19:25:31 +0000292bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
293 const {
294 MachineFunction &MF = const_cast<MachineFunction&>(*this);
295 unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
296 if (AbortOnErrors && FoundErrors)
297 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
298 return FoundErrors == 0;
Jakob Stoklund Olesen27440e72009-11-13 21:56:09 +0000299}
300
Matthias Braun80595462015-09-09 17:49:46 +0000301void MachineVerifier::verifySlotIndexes() const {
302 if (Indexes == nullptr)
303 return;
304
305 // Ensure the IdxMBB list is sorted by slot indexes.
306 SlotIndex Last;
307 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
308 E = Indexes->MBBIndexEnd(); I != E; ++I) {
309 assert(!Last.isValid() || I->first > Last);
310 Last = I->first;
311 }
312}
313
Derek Schuff42666ee2016-03-29 17:40:22 +0000314void MachineVerifier::verifyProperties(const MachineFunction &MF) {
315 // If a pass has introduced virtual registers without clearing the
Matthias Braun1eb47362016-08-25 01:27:13 +0000316 // NoVRegs property (or set it without allocating the vregs)
Derek Schuff42666ee2016-03-29 17:40:22 +0000317 // then report an error.
318 if (MF.getProperties().hasProperty(
Matthias Braun1eb47362016-08-25 01:27:13 +0000319 MachineFunctionProperties::Property::NoVRegs) &&
320 MRI->getNumVirtRegs())
321 report("Function has NoVRegs property but there are VReg operands", &MF);
Derek Schuff42666ee2016-03-29 17:40:22 +0000322}
323
Matthias Braunb3aefc32016-02-15 19:25:31 +0000324unsigned MachineVerifier::verify(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000325 foundErrors = 0;
326
327 this->MF = &MF;
328 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000329 TII = MF.getSubtarget().getInstrInfo();
330 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000331 MRI = &MF.getRegInfo();
332
Ahmed Bougacha3681c772016-08-02 16:17:15 +0000333 isFunctionRegBankSelected = MF.getProperties().hasProperty(
334 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab14e9442016-08-02 16:49:22 +0000335 isFunctionSelected = MF.getProperties().hasProperty(
336 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha3681c772016-08-02 16:17:15 +0000337
Craig Topperc0196b12014-04-14 00:51:57 +0000338 LiveVars = nullptr;
339 LiveInts = nullptr;
340 LiveStks = nullptr;
341 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000342 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000343 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000344 // We don't want to verify LiveVariables if LiveIntervals is available.
345 if (!LiveInts)
346 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000347 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000348 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000349 }
350
Matthias Braun80595462015-09-09 17:49:46 +0000351 verifySlotIndexes();
352
Derek Schuff42666ee2016-03-29 17:40:22 +0000353 verifyProperties(MF);
354
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000355 visitMachineFunctionBefore();
356 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
357 MFI!=MFE; ++MFI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000358 visitMachineBasicBlockBefore(&*MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000359 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000360 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000361 // Do we expect the next instruction to be part of the same bundle?
362 bool InBundle = false;
363
Evan Cheng7fae11b2011-12-14 02:11:42 +0000364 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
365 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000366 if (MBBI->getParent() != &*MFI) {
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +0000367 report("Bad instruction parent pointer", &*MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000368 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000369 continue;
370 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000371
372 // Check for consistent bundle flags.
373 if (InBundle && !MBBI->isBundledWithPred())
374 report("Missing BundledPred flag, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000375 "BundledSucc was set on predecessor",
376 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000377 if (!InBundle && MBBI->isBundledWithPred())
378 report("BundledPred flag is set, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000379 "but BundledSucc not set on predecessor",
380 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000381
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000382 // Is this a bundle header?
383 if (!MBBI->isInsideBundle()) {
384 if (CurBundle)
385 visitMachineBundleAfter(CurBundle);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000386 CurBundle = &*MBBI;
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000387 visitMachineBundleBefore(CurBundle);
388 } else if (!CurBundle)
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +0000389 report("No bundle header", &*MBBI);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000390 visitMachineInstrBefore(&*MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000391 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
392 const MachineInstr &MI = *MBBI;
393 const MachineOperand &Op = MI.getOperand(I);
394 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000395 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000396 // functions when replacing operands of a MachineInstr.
397 report("Instruction has operand with wrong parent set", &MI);
398 }
399
400 visitMachineOperand(&Op, I);
401 }
402
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000403 visitMachineInstrAfter(&*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000404
405 // Was this the last bundled instruction?
406 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000407 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000408 if (CurBundle)
409 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000410 if (InBundle)
411 report("BundledSucc flag set on last instruction in block", &MFI->back());
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000412 visitMachineBasicBlockAfter(&*MFI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000413 }
414 visitMachineFunctionAfter();
415
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000416 // Clean up.
417 regsLive.clear();
418 regsDefined.clear();
419 regsDead.clear();
420 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000421 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000422 regsLiveInButUnused.clear();
423 MBBInfoMap.clear();
424
Matthias Braunb3aefc32016-02-15 19:25:31 +0000425 return foundErrors;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000426}
427
Chris Lattner75f40452009-08-23 01:03:30 +0000428void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000429 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000430 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000431 if (!foundErrors++) {
432 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000433 errs() << "# " << Banner << '\n';
Matthias Braun42b4b632015-11-09 23:59:23 +0000434 if (LiveInts != nullptr)
435 LiveInts->print(errs());
436 else
437 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000438 }
Owen Anderson21b17882015-02-04 00:02:59 +0000439 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000440 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000441}
442
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000443void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000444 assert(MBB);
445 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000446 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000447 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000448 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000449 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000450 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000451 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000452 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000453}
454
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000455void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000456 assert(MI);
457 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000458 errs() << "- instruction: ";
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000459 if (Indexes && Indexes->hasIndex(*MI))
460 errs() << Indexes->getInstructionIndex(*MI) << '\t';
Matthias Braun45718db2015-11-09 23:59:25 +0000461 MI->print(errs(), /*SkipOpers=*/true);
Matthias Braun716b4332015-11-09 23:59:29 +0000462 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000463}
464
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000465void MachineVerifier::report(const char *msg,
466 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000467 assert(MO);
468 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000469 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000470 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000471 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000472}
473
Matthias Braun579c9cd2016-02-02 02:44:25 +0000474void MachineVerifier::report_context(SlotIndex Pos) const {
475 errs() << "- at: " << Pos << '\n';
476}
477
Matthias Braun7e624d52015-11-09 23:59:33 +0000478void MachineVerifier::report_context(const LiveInterval &LI) const {
Owen Anderson21b17882015-02-04 00:02:59 +0000479 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000480}
481
Matt Arsenault892fcd02016-07-25 19:39:01 +0000482void MachineVerifier::report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000483 LaneBitmask LaneMask) const {
Matthias Braun579c9cd2016-02-02 02:44:25 +0000484 report_context_liverange(LR);
Matt Arsenault892fcd02016-07-25 19:39:01 +0000485 report_context_vreg_regunit(VRegUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000486 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +0000487 report_context_lanemask(LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000488}
489
Matthias Braun7e624d52015-11-09 23:59:33 +0000490void MachineVerifier::report_context(const LiveRange::Segment &S) const {
491 errs() << "- segment: " << S << '\n';
492}
493
494void MachineVerifier::report_context(const VNInfo &VNI) const {
495 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
Matthias Braun364e6e92013-10-10 21:28:54 +0000496}
497
Matthias Braun579c9cd2016-02-02 02:44:25 +0000498void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
499 errs() << "- liverange: " << LR << '\n';
500}
501
Matthias Braun30668dd2016-05-11 21:31:39 +0000502void MachineVerifier::report_context_vreg(unsigned VReg) const {
503 errs() << "- v. register: " << PrintReg(VReg, TRI) << '\n';
504}
505
Matthias Braun1377fd62016-02-02 20:04:51 +0000506void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
507 if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
Matthias Braun30668dd2016-05-11 21:31:39 +0000508 report_context_vreg(VRegOrUnit);
Matthias Braun1377fd62016-02-02 20:04:51 +0000509 } else {
510 errs() << "- regunit: " << PrintRegUnit(VRegOrUnit, TRI) << '\n';
511 }
512}
513
514void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
515 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
516}
517
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000518void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000519 BBInfo &MInfo = MBBInfoMap[MBB];
520 if (!MInfo.reachable) {
521 MInfo.reachable = true;
522 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
523 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
524 markReachable(*SuI);
525 }
526}
527
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000528void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000529 lastIndex = SlotIndex();
Matthias Braun4682ac62017-05-05 22:04:05 +0000530 regsReserved = MRI->reservedRegsFrozen() ? MRI->getReservedRegs()
531 : TRI->getReservedRegs(*MF);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000532
Justin Bogner20dd36a2017-04-11 19:32:41 +0000533 if (!MF->empty())
534 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000535
536 // Build a set of the basic blocks in the function.
537 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000538 for (const auto &MBB : *MF) {
539 FunctionBlocks.insert(&MBB);
540 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000541
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000542 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
543 if (MInfo.Preds.size() != MBB.pred_size())
544 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000545
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000546 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
547 if (MInfo.Succs.size() != MBB.succ_size())
548 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000549 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000550
551 // Check that the register use lists are sane.
552 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000553
Justin Bogner20dd36a2017-04-11 19:32:41 +0000554 if (!MF->empty())
555 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000556}
557
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000558// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000559static bool matchPair(MachineBasicBlock::const_succ_iterator i,
560 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000561 if (*i == a)
562 return *++i == b;
563 if (*i == b)
564 return *++i == a;
565 return false;
566}
567
568void
569MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000570 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000571
Matthias Braun79f85b32016-08-24 01:32:41 +0000572 if (!MF->getProperties().hasProperty(
Matthias Braun11723322017-01-05 20:01:19 +0000573 MachineFunctionProperties::Property::NoPHIs) && MRI->tracksLiveness()) {
Lang Hames1ce837a2012-02-14 19:17:48 +0000574 // If this block has allocatable physical registers live-in, check that
575 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000576 for (const auto &LI : MBB->liveins()) {
577 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +0000578 MBB->getIterator() != MBB->getParent()->begin()) {
Matt Arsenault900b21c2017-02-15 22:19:06 +0000579 report("MBB has allocatable live-in, but isn't entry or landing-pad.", MBB);
Lang Hames1ce837a2012-02-14 19:17:48 +0000580 }
581 }
582 }
583
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000584 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000585 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000586 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000587 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000588 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000589 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000590 if (!FunctionBlocks.count(*I))
591 report("MBB has successor that isn't part of the function.", MBB);
592 if (!MBBInfoMap[*I].Preds.count(MBB)) {
593 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000594 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000595 << (*I)->getNumber() << ".\n";
596 }
597 }
598
599 // Check the predecessor list.
600 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
601 E = MBB->pred_end(); I != E; ++I) {
602 if (!FunctionBlocks.count(*I))
603 report("MBB has predecessor that isn't part of the function.", MBB);
604 if (!MBBInfoMap[*I].Succs.count(MBB)) {
605 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000606 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000607 << (*I)->getNumber() << ".\n";
608 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000609 }
Bill Wendling2a401312011-05-04 22:54:05 +0000610
611 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
612 const BasicBlock *BB = MBB->getBasicBlock();
Reid Kleckner64b003f2015-11-09 21:04:00 +0000613 const Function *Fn = MF->getFunction();
Bill Wendling2a401312011-05-04 22:54:05 +0000614 if (LandingPadSuccs.size() > 1 &&
615 !(AsmInfo &&
616 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
Reid Kleckner64b003f2015-11-09 21:04:00 +0000617 BB && isa<SwitchInst>(BB->getTerminator())) &&
618 !isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000619 report("MBB has more than one landing pad successor", MBB);
620
Dan Gohman352a4952009-08-27 02:43:49 +0000621 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000622 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000623 SmallVector<MachineOperand, 4> Cond;
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000624 if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
625 Cond)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000626 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
627 // check whether its answers match up with reality.
628 if (!TBB && !FBB) {
629 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000630 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000631 ++MBBI;
632 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000633 // It's possible that the block legitimately ends with a noreturn
634 // call or an unreachable, in which case it won't actually fall
635 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000636 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000637 // It's possible that the block legitimately ends with a noreturn
638 // call or an unreachable, in which case it won't actuall fall
639 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000640 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000641 report("MBB exits via unconditional fall-through but doesn't have "
642 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000643 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000644 report("MBB exits via unconditional fall-through but its successor "
645 "differs from its CFG successor!", MBB);
646 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000647 if (!MBB->empty() && MBB->back().isBarrier() &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000648 !TII->isPredicated(MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000649 report("MBB exits via unconditional fall-through but ends with a "
650 "barrier instruction!", MBB);
651 }
652 if (!Cond.empty()) {
653 report("MBB exits via unconditional fall-through but has a condition!",
654 MBB);
655 }
656 } else if (TBB && !FBB && Cond.empty()) {
657 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000658 // If the block has exactly one successor, that happens to be a
659 // landingpad, accept it as valid control flow.
660 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
661 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
662 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000663 report("MBB exits via unconditional branch but doesn't have "
664 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000665 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000666 report("MBB exits via unconditional branch but the CFG "
667 "successor doesn't match the actual successor!", MBB);
668 }
669 if (MBB->empty()) {
670 report("MBB exits via unconditional branch but doesn't contain "
671 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000672 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000673 report("MBB exits via unconditional branch but doesn't end with a "
674 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000675 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000676 report("MBB exits via unconditional branch but the branch isn't a "
677 "terminator instruction!", MBB);
678 }
679 } else if (TBB && !FBB && !Cond.empty()) {
680 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000681 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000682 ++MBBI;
683 if (MBBI == MF->end()) {
684 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000685 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000686 // A conditional branch with only one successor is weird, but allowed.
687 if (&*MBBI != TBB)
688 report("MBB exits via conditional branch/fall-through but only has "
689 "one CFG successor!", MBB);
690 else if (TBB != *MBB->succ_begin())
691 report("MBB exits via conditional branch/fall-through but the CFG "
692 "successor don't match the actual successor!", MBB);
693 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000694 report("MBB exits via conditional branch/fall-through but doesn't have "
695 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000696 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000697 report("MBB exits via conditional branch/fall-through but the CFG "
698 "successors don't match the actual successors!", MBB);
699 }
700 if (MBB->empty()) {
701 report("MBB exits via conditional branch/fall-through but doesn't "
702 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000703 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000704 report("MBB exits via conditional branch/fall-through but ends with a "
705 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000706 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000707 report("MBB exits via conditional branch/fall-through but the branch "
708 "isn't a terminator instruction!", MBB);
709 }
710 } else if (TBB && FBB) {
711 // Block conditionally branches somewhere, otherwise branches
712 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000713 if (MBB->succ_size() == 1) {
714 // A conditional branch with only one successor is weird, but allowed.
715 if (FBB != TBB)
716 report("MBB exits via conditional branch/branch through but only has "
717 "one CFG successor!", MBB);
718 else if (TBB != *MBB->succ_begin())
719 report("MBB exits via conditional branch/branch through but the CFG "
720 "successor don't match the actual successor!", MBB);
721 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000722 report("MBB exits via conditional branch/branch but doesn't have "
723 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000724 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000725 report("MBB exits via conditional branch/branch but the CFG "
726 "successors don't match the actual successors!", MBB);
727 }
728 if (MBB->empty()) {
729 report("MBB exits via conditional branch/branch but doesn't "
730 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000731 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000732 report("MBB exits via conditional branch/branch but doesn't end with a "
733 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000734 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000735 report("MBB exits via conditional branch/branch but the branch "
736 "isn't a terminator instruction!", MBB);
737 }
738 if (Cond.empty()) {
739 report("MBB exits via conditinal branch/branch but there's no "
740 "condition!", MBB);
741 }
742 } else {
743 report("AnalyzeBranch returned invalid data!", MBB);
744 }
745 }
746
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000747 regsLive.clear();
Matthias Braun11723322017-01-05 20:01:19 +0000748 if (MRI->tracksLiveness()) {
749 for (const auto &LI : MBB->liveins()) {
750 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
751 report("MBB live-in list contains non-physical register", MBB);
752 continue;
753 }
754 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
755 SubRegs.isValid(); ++SubRegs)
756 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000757 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000758 }
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +0000759 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000760
Matthias Braun941a7052016-07-28 18:40:00 +0000761 const MachineFrameInfo &MFI = MF->getFrameInfo();
762 BitVector PR = MFI.getPristineRegs(*MF);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000763 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000764 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
765 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000766 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000767 }
768
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000769 regsKilled.clear();
770 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000771
772 if (Indexes)
773 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000774}
775
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000776// This function gets called for all bundle headers, including normal
777// stand-alone unbundled instructions.
778void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000779 if (Indexes && Indexes->hasIndex(*MI)) {
780 SlotIndex idx = Indexes->getInstructionIndex(*MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000781 if (!(idx > lastIndex)) {
782 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000783 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000784 }
785 lastIndex = idx;
786 }
Pete Coopercd720162012-06-07 17:41:39 +0000787
788 // Ensure non-terminators don't follow terminators.
789 // Ignore predicated terminators formed by if conversion.
790 // FIXME: If conversion shouldn't need to violate this rule.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000791 if (MI->isTerminator() && !TII->isPredicated(*MI)) {
Pete Coopercd720162012-06-07 17:41:39 +0000792 if (!FirstTerminator)
793 FirstTerminator = MI;
794 } else if (FirstTerminator) {
795 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000796 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000797 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000798}
799
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000800// The operands on an INLINEASM instruction must follow a template.
801// Verify that the flag operands make sense.
802void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
803 // The first two operands on INLINEASM are the asm string and global flags.
804 if (MI->getNumOperands() < 2) {
805 report("Too few operands on inline asm", MI);
806 return;
807 }
808 if (!MI->getOperand(0).isSymbol())
809 report("Asm string must be an external symbol", MI);
810 if (!MI->getOperand(1).isImm())
811 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000812 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
Wei Ding0526e7f2016-06-22 18:51:08 +0000813 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
814 // and Extra_IsConvergent = 32.
815 if (!isUInt<6>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000816 report("Unknown asm flags", &MI->getOperand(1), 1);
817
Gabor Horvathfee04342015-03-16 09:53:42 +0000818 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000819
820 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
821 unsigned NumOps;
822 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
823 const MachineOperand &MO = MI->getOperand(OpNo);
824 // There may be implicit ops after the fixed operands.
825 if (!MO.isImm())
826 break;
827 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
828 }
829
830 if (OpNo > MI->getNumOperands())
831 report("Missing operands in last group", MI);
832
833 // An optional MDNode follows the groups.
834 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
835 ++OpNo;
836
837 // All trailing operands must be implicit registers.
838 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
839 const MachineOperand &MO = MI->getOperand(OpNo);
840 if (!MO.isReg() || !MO.isImplicit())
841 report("Expected implicit register after groups", &MO, OpNo);
842 }
843}
844
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000845void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000846 const MCInstrDesc &MCID = MI->getDesc();
847 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000848 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000849 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000850 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000851 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000852
Matthias Braun90799ce2016-08-23 21:19:49 +0000853 if (MI->isPHI() && MF->getProperties().hasProperty(
854 MachineFunctionProperties::Property::NoPHIs))
855 report("Found PHI instruction with NoPHIs property set", MI);
856
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000857 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000858 if (MI->isInlineAsm())
859 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000860
Dan Gohmandb9493c2009-10-07 17:36:00 +0000861 // Check the MachineMemOperands for basic consistency.
862 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
863 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000864 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000865 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000866 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000867 report("Missing mayStore flag", MI);
868 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000869
870 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000871 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000872 if (LiveInts) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000873 bool mapped = !LiveInts->isNotInMIMap(*MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000874 if (MI->isDebugValue()) {
875 if (mapped)
876 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000877 } else if (MI->isInsideBundle()) {
878 if (mapped)
879 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000880 } else {
881 if (!mapped)
882 report("Missing slot index", MI);
883 }
884 }
885
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000886 // Check types.
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000887 if (isPreISelGenericOpcode(MCID.getOpcode())) {
Ahmed Bougachab14e9442016-08-02 16:49:22 +0000888 if (isFunctionSelected)
889 report("Unexpected generic instruction in a Selected function", MI);
890
Tim Northover0f140c72016-09-09 11:46:34 +0000891 // Generic instructions specify equality constraints between some
892 // of their operands. Make sure these are consistent.
893 SmallVector<LLT, 4> Types;
894 for (unsigned i = 0; i < MCID.getNumOperands(); ++i) {
895 if (!MCID.OpInfo[i].isGenericType())
896 continue;
897 size_t TypeIdx = MCID.OpInfo[i].getGenericTypeIndex();
898 Types.resize(std::max(TypeIdx + 1, Types.size()));
899
900 LLT OpTy = MRI->getType(MI->getOperand(i).getReg());
901 if (Types[TypeIdx].isValid() && Types[TypeIdx] != OpTy)
902 report("type mismatch in generic instruction", MI);
903 Types[TypeIdx] = OpTy;
904 }
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000905 }
906
Tim Northovere5102de2016-08-30 18:52:46 +0000907 // Generic opcodes must not have physical register operands.
Tim Northover25d12862016-09-09 11:47:31 +0000908 if (isPreISelGenericOpcode(MCID.getOpcode())) {
Tim Northovere5102de2016-08-30 18:52:46 +0000909 for (auto &Op : MI->operands()) {
910 if (Op.isReg() && TargetRegisterInfo::isPhysicalRegister(Op.getReg()))
911 report("Generic instruction cannot have physical register", MI);
912 }
913 }
914
Tim Northover88634992017-02-17 18:50:15 +0000915 // Generic loads and stores must have a single MachineMemOperand
916 // describing that access.
917 if ((MI->getOpcode() == TargetOpcode::G_LOAD ||
918 MI->getOpcode() == TargetOpcode::G_STORE) &&
919 !MI->hasOneMemOperand())
920 report("Generic instruction accessing memory must have one mem operand",
921 MI);
922
Andrew Trick924123a2011-09-21 02:20:46 +0000923 StringRef ErrorInfo;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000924 if (!TII->verifyInstruction(*MI, ErrorInfo))
Andrew Trick924123a2011-09-21 02:20:46 +0000925 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000926}
927
928void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000929MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000930 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000931 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +0000932 unsigned NumDefs = MCID.getNumDefs();
933 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
934 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000935
Evan Cheng6cc775f2011-06-28 19:10:37 +0000936 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +0000937 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000938 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000939 if (!MO->isReg())
940 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +0000941 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000942 report("Explicit definition marked as use", MO, MONum);
943 else if (MO->isImplicit())
944 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000945 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000946 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +0000947 // Don't check if it's the last operand in a variadic instruction. See,
948 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000949 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000950 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000951 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +0000952 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000953 if (MO->isImplicit())
954 report("Explicit operand marked as implicit", MO, MONum);
955 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000956
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000957 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
958 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000959 if (!MO->isReg())
960 report("Tied use must be a register", MO, MONum);
961 else if (!MO->isTied())
962 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000963 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
964 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000965 } else if (MO->isReg() && MO->isTied())
966 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000967 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +0000968 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000969 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000970 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000971 }
972
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000973 switch (MO->getType()) {
974 case MachineOperand::MO_Register: {
975 const unsigned Reg = MO->getReg();
976 if (!Reg)
977 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000978 if (MRI->tracksLiveness() && !MI->isDebugValue())
979 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000980
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000981 // Verify the consistency of tied operands.
982 if (MO->isTied()) {
983 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
984 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
985 if (!OtherMO.isReg())
986 report("Must be tied to a register", MO, MONum);
987 if (!OtherMO.isTied())
988 report("Missing tie flags on tied operand", MO, MONum);
989 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
990 report("Inconsistent tie links", MO, MONum);
991 if (MONum < MCID.getNumDefs()) {
992 if (OtherIdx < MCID.getNumOperands()) {
993 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
994 report("Explicit def tied to explicit use without tie constraint",
995 MO, MONum);
996 } else {
997 if (!OtherMO.isImplicit())
998 report("Explicit def should be tied to implicit use", MO, MONum);
999 }
1000 }
1001 }
1002
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001003 // Verify two-address constraints after leaving SSA form.
1004 unsigned DefIdx;
1005 if (!MRI->isSSA() && MO->isUse() &&
1006 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
1007 Reg != MI->getOperand(DefIdx).getReg())
1008 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001009
1010 // Check register classes.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001011 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001012 unsigned SubIdx = MO->getSubReg();
1013
1014 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001015 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001016 report("Illegal subregister index for physical register", MO, MONum);
1017 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001018 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001019 if (const TargetRegisterClass *DRC =
1020 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001021 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001022 report("Illegal physical register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001023 errs() << TRI->getName(Reg) << " is not a "
Craig Toppercf0444b2014-11-17 05:50:14 +00001024 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001025 }
1026 }
1027 } else {
1028 // Virtual register.
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001029 const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
1030 if (!RC) {
1031 // This is a generic virtual register.
Ahmed Bougachab14e9442016-08-02 16:49:22 +00001032
1033 // If we're post-Select, we can't have gvregs anymore.
1034 if (isFunctionSelected) {
1035 report("Generic virtual register invalid in a Selected function",
1036 MO, MONum);
1037 return;
1038 }
1039
Quentin Colombet3749f332016-12-22 22:50:34 +00001040 // The gvreg must have a type and it must not have a SubIdx.
Tim Northover0f140c72016-09-09 11:46:34 +00001041 LLT Ty = MRI->getType(Reg);
1042 if (!Ty.isValid()) {
1043 report("Generic virtual register must have a valid type", MO,
1044 MONum);
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001045 return;
1046 }
Ahmed Bougacha3681c772016-08-02 16:17:15 +00001047
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001048 const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
Ahmed Bougacha3681c772016-08-02 16:17:15 +00001049
1050 // If we're post-RegBankSelect, the gvreg must have a bank.
1051 if (!RegBank && isFunctionRegBankSelected) {
1052 report("Generic virtual register must have a bank in a "
1053 "RegBankSelected function",
1054 MO, MONum);
1055 return;
1056 }
1057
1058 // Make sure the register fits into its register bank if any.
Tim Northover32a078a2016-09-15 10:09:59 +00001059 if (RegBank && Ty.isValid() &&
Tim Northover0f140c72016-09-09 11:46:34 +00001060 RegBank->getSize() < Ty.getSizeInBits()) {
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001061 report("Register bank is too small for virtual register", MO,
1062 MONum);
1063 errs() << "Register bank " << RegBank->getName() << " too small("
Tim Northover0f140c72016-09-09 11:46:34 +00001064 << RegBank->getSize() << ") to fit " << Ty.getSizeInBits()
1065 << "-bits\n";
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001066 return;
1067 }
1068 if (SubIdx) {
Tim Northover0f140c72016-09-09 11:46:34 +00001069 report("Generic virtual register does not subregister index", MO,
1070 MONum);
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001071 return;
1072 }
Quentin Colombetfa5960a2016-12-22 21:56:39 +00001073
1074 // If this is a target specific instruction and this operand
1075 // has register class constraint, the virtual register must
1076 // comply to it.
1077 if (!isPreISelGenericOpcode(MCID.getOpcode()) &&
1078 TII->getRegClass(MCID, MONum, TRI, *MF)) {
1079 report("Virtual register does not match instruction constraint", MO,
1080 MONum);
1081 errs() << "Expect register class "
1082 << TRI->getRegClassName(
1083 TII->getRegClass(MCID, MONum, TRI, *MF))
1084 << " but got nothing\n";
1085 return;
1086 }
1087
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001088 break;
1089 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001090 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001091 const TargetRegisterClass *SRC =
1092 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001093 if (!SRC) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001094 report("Invalid subregister index for virtual register", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001095 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001096 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001097 return;
1098 }
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001099 if (RC != SRC) {
1100 report("Invalid register class for subregister index", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001101 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001102 << " does not fully support subreg index " << SubIdx << "\n";
1103 return;
1104 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001105 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001106 if (const TargetRegisterClass *DRC =
1107 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001108 if (SubIdx) {
1109 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +00001110 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001111 if (!SuperRC) {
1112 report("No largest legal super class exists.", MO, MONum);
1113 return;
1114 }
1115 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
1116 if (!DRC) {
1117 report("No matching super-reg register class.", MO, MONum);
1118 return;
1119 }
1120 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +00001121 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001122 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001123 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +00001124 << " register, but got a " << TRI->getRegClassName(RC)
1125 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001126 }
1127 }
1128 }
1129 }
1130 break;
1131 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001132
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001133 case MachineOperand::MO_RegisterMask:
1134 regMasks.push_back(MO->getRegMask());
1135 break;
1136
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001137 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +00001138 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
1139 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001140 break;
1141
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001142 case MachineOperand::MO_FrameIndex:
1143 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001144 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
Jonas Paulsson72640f12015-10-29 08:28:35 +00001145 int FI = MO->getIndex();
1146 LiveInterval &LI = LiveStks->getInterval(FI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001147 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001148
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001149 bool stores = MI->mayStore();
Jonas Paulsson72640f12015-10-29 08:28:35 +00001150 bool loads = MI->mayLoad();
1151 // For a memory-to-memory move, we need to check if the frame
1152 // index is used for storing or loading, by inspecting the
1153 // memory operands.
1154 if (stores && loads) {
1155 for (auto *MMO : MI->memoperands()) {
1156 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1157 if (PSV == nullptr) continue;
1158 const FixedStackPseudoSourceValue *Value =
1159 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1160 if (Value == nullptr) continue;
1161 if (Value->getFrameIndex() != FI) continue;
1162
1163 if (MMO->isStore())
1164 loads = false;
1165 else
1166 stores = false;
1167 break;
1168 }
1169 if (loads == stores)
1170 report("Missing fixed stack memoperand.", MI);
1171 }
1172 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001173 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001174 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001175 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001176 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001177 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001178 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001179 }
1180 }
1181 break;
1182
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001183 default:
1184 break;
1185 }
1186}
1187
Matthias Braun1377fd62016-02-02 20:04:51 +00001188void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
1189 unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
1190 LaneBitmask LaneMask) {
1191 LiveQueryResult LRQ = LR.Query(UseIdx);
1192 // Check if we have a segment at the use, note however that we only need one
1193 // live subregister range, the others may be dead.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001194 if (!LRQ.valueIn() && LaneMask.none()) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001195 report("No live segment at use", MO, MONum);
1196 report_context_liverange(LR);
1197 report_context_vreg_regunit(VRegOrUnit);
1198 report_context(UseIdx);
1199 }
1200 if (MO->isKill() && !LRQ.isKill()) {
1201 report("Live range continues after kill flag", MO, MONum);
1202 report_context_liverange(LR);
1203 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001204 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001205 report_context_lanemask(LaneMask);
1206 report_context(UseIdx);
1207 }
1208}
1209
1210void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
1211 unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
1212 LaneBitmask LaneMask) {
1213 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
1214 assert(VNI && "NULL valno is not allowed");
1215 if (VNI->def != DefIdx) {
1216 report("Inconsistent valno->def", MO, MONum);
1217 report_context_liverange(LR);
1218 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001219 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001220 report_context_lanemask(LaneMask);
1221 report_context(*VNI);
1222 report_context(DefIdx);
1223 }
1224 } else {
1225 report("No live segment at def", MO, MONum);
1226 report_context_liverange(LR);
1227 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001228 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001229 report_context_lanemask(LaneMask);
1230 report_context(DefIdx);
1231 }
1232 // Check that, if the dead def flag is present, LiveInts agree.
1233 if (MO->isDead()) {
1234 LiveQueryResult LRQ = LR.Query(DefIdx);
1235 if (!LRQ.isDeadDef()) {
1236 // In case of physregs we can have a non-dead definition on another
1237 // operand.
1238 bool otherDef = false;
1239 if (!TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
1240 const MachineInstr &MI = *MO->getParent();
1241 for (const MachineOperand &MO : MI.operands()) {
1242 if (!MO.isReg() || !MO.isDef() || MO.isDead())
1243 continue;
1244 unsigned Reg = MO.getReg();
1245 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1246 if (*Units == VRegOrUnit) {
1247 otherDef = true;
1248 break;
1249 }
1250 }
1251 }
1252 }
1253
1254 if (!otherDef) {
1255 report("Live range continues after dead def flag", MO, MONum);
1256 report_context_liverange(LR);
1257 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001258 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001259 report_context_lanemask(LaneMask);
1260 }
1261 }
1262 }
1263}
1264
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001265void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1266 const MachineInstr *MI = MO->getParent();
1267 const unsigned Reg = MO->getReg();
1268
1269 // Both use and def operands can read a register.
1270 if (MO->readsReg()) {
1271 regsLiveInButUnused.erase(Reg);
1272
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001273 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001274 addRegWithSubRegs(regsKilled, Reg);
1275
1276 // Check that LiveVars knows this kill.
1277 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1278 MO->isKill()) {
1279 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
David Majnemer0d955d02016-08-11 22:21:41 +00001280 if (!is_contained(VI.Kills, MI))
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001281 report("Kill missing from LiveVariables", MO, MONum);
1282 }
1283
1284 // Check LiveInts liveness and kill.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001285 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1286 SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001287 // Check the cached regunit intervals.
1288 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1289 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001290 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
1291 checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001292 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001293 }
1294
1295 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1296 if (LiveInts->hasInterval(Reg)) {
1297 // This is a virtual register interval.
1298 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun1377fd62016-02-02 20:04:51 +00001299 checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
1300
1301 if (LI.hasSubRanges() && !MO->isDef()) {
1302 unsigned SubRegIdx = MO->getSubReg();
1303 LaneBitmask MOMask = SubRegIdx != 0
1304 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1305 : MRI->getMaxLaneMaskForVReg(Reg);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001306 LaneBitmask LiveInMask;
Matthias Braun1377fd62016-02-02 20:04:51 +00001307 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001308 if ((MOMask & SR.LaneMask).none())
Matthias Braun1377fd62016-02-02 20:04:51 +00001309 continue;
1310 checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
1311 LiveQueryResult LRQ = SR.Query(UseIdx);
1312 if (LRQ.valueIn())
1313 LiveInMask |= SR.LaneMask;
1314 }
1315 // At least parts of the register has to be live at the use.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001316 if ((LiveInMask & MOMask).none()) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001317 report("No live subrange at use", MO, MONum);
1318 report_context(LI);
1319 report_context(UseIdx);
1320 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001321 }
1322 } else {
1323 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001324 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001325 }
1326 }
1327
1328 // Use of a dead register.
1329 if (!regsLive.count(Reg)) {
1330 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1331 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001332 bool Bad = !isReserved(Reg);
1333 // We are fine if just any subregister has a defined value.
1334 if (Bad) {
1335 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1336 ++SubRegs) {
1337 if (regsLive.count(*SubRegs)) {
1338 Bad = false;
1339 break;
1340 }
1341 }
1342 }
Matthias Braun96a31952015-01-14 22:25:14 +00001343 // If there is an additional implicit-use of a super register we stop
1344 // here. By definition we are fine if the super register is not
1345 // (completely) dead, if the complete super register is dead we will
1346 // get a report for its operand.
1347 if (Bad) {
1348 for (const MachineOperand &MOP : MI->uses()) {
1349 if (!MOP.isReg())
1350 continue;
1351 if (!MOP.isImplicit())
1352 continue;
1353 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1354 ++SubRegs) {
1355 if (*SubRegs == Reg) {
1356 Bad = false;
1357 break;
1358 }
1359 }
1360 }
1361 }
Matthias Braun96d77322014-12-10 01:13:13 +00001362 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001363 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001364 } else if (MRI->def_empty(Reg)) {
1365 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001366 } else {
1367 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1368 // We don't know which virtual registers are live in, so only complain
1369 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1370 // must be live in. PHI instructions are handled separately.
1371 if (MInfo.regsKilled.count(Reg))
1372 report("Using a killed virtual register", MO, MONum);
1373 else if (!MI->isPHI())
1374 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1375 }
1376 }
1377 }
1378
1379 if (MO->isDef()) {
1380 // Register defined.
1381 // TODO: verify that earlyclobber ops are not used.
1382 if (MO->isDead())
1383 addRegWithSubRegs(regsDead, Reg);
1384 else
1385 addRegWithSubRegs(regsDefined, Reg);
1386
1387 // Verify SSA form.
1388 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001389 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001390 report("Multiple virtual register defs in SSA form", MO, MONum);
1391
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001392 // Check LiveInts for a live segment, but only for virtual registers.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001393 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1394 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001395 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Matthias Braun1377fd62016-02-02 20:04:51 +00001396
1397 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1398 if (LiveInts->hasInterval(Reg)) {
1399 const LiveInterval &LI = LiveInts->getInterval(Reg);
1400 checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
1401
1402 if (LI.hasSubRanges()) {
1403 unsigned SubRegIdx = MO->getSubReg();
1404 LaneBitmask MOMask = SubRegIdx != 0
1405 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1406 : MRI->getMaxLaneMaskForVReg(Reg);
1407 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001408 if ((SR.LaneMask & MOMask).none())
Matthias Braun1377fd62016-02-02 20:04:51 +00001409 continue;
1410 checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, SR.LaneMask);
1411 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001412 }
1413 } else {
Matthias Braun1377fd62016-02-02 20:04:51 +00001414 report("Virtual register has no Live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001415 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001416 }
1417 }
1418 }
1419}
1420
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001421void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001422}
1423
1424// This function gets called after visiting all instructions in a bundle. The
1425// argument points to the bundle header.
1426// Normal stand-alone instructions are also considered 'bundles', and this
1427// function is called for all of them.
1428void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001429 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1430 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001431 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001432 // Kill any masked registers.
1433 while (!regMasks.empty()) {
1434 const uint32_t *Mask = regMasks.pop_back_val();
1435 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1436 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1437 MachineOperand::clobbersPhysReg(Mask, *I))
1438 regsDead.push_back(*I);
1439 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001440 set_subtract(regsLive, regsDead); regsDead.clear();
1441 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001442}
1443
1444void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001445MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001446 MBBInfoMap[MBB].regsLiveOut = regsLive;
1447 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001448
1449 if (Indexes) {
1450 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1451 if (!(stop > lastIndex)) {
1452 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001453 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001454 << " last instruction was at " << lastIndex << '\n';
1455 }
1456 lastIndex = stop;
1457 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001458}
1459
1460// Calculate the largest possible vregsPassed sets. These are the registers that
1461// can pass through an MBB live, but may not be live every time. It is assumed
1462// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001463void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001464 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1465 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001466 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001467 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001468 BBInfo &MInfo = MBBInfoMap[&MBB];
1469 if (!MInfo.reachable)
1470 continue;
1471 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1472 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1473 BBInfo &SInfo = MBBInfoMap[*SuI];
1474 if (SInfo.addPassed(MInfo.regsLiveOut))
1475 todo.insert(*SuI);
1476 }
1477 }
1478
1479 // Iteratively push vregsPassed to successors. This will converge to the same
1480 // final state regardless of DenseSet iteration order.
1481 while (!todo.empty()) {
1482 const MachineBasicBlock *MBB = *todo.begin();
1483 todo.erase(MBB);
1484 BBInfo &MInfo = MBBInfoMap[MBB];
1485 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1486 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1487 if (*SuI == MBB)
1488 continue;
1489 BBInfo &SInfo = MBBInfoMap[*SuI];
1490 if (SInfo.addPassed(MInfo.vregsPassed))
1491 todo.insert(*SuI);
1492 }
1493 }
1494}
1495
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001496// Calculate the set of virtual registers that must be passed through each basic
1497// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001498// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001499void MachineVerifier::calcRegsRequired() {
1500 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001501 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001502 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001503 BBInfo &MInfo = MBBInfoMap[&MBB];
1504 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1505 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1506 BBInfo &PInfo = MBBInfoMap[*PrI];
1507 if (PInfo.addRequired(MInfo.vregsLiveIn))
1508 todo.insert(*PrI);
1509 }
1510 }
1511
1512 // Iteratively push vregsRequired to predecessors. This will converge to the
1513 // same final state regardless of DenseSet iteration order.
1514 while (!todo.empty()) {
1515 const MachineBasicBlock *MBB = *todo.begin();
1516 todo.erase(MBB);
1517 BBInfo &MInfo = MBBInfoMap[MBB];
1518 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1519 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1520 if (*PrI == MBB)
1521 continue;
1522 BBInfo &SInfo = MBBInfoMap[*PrI];
1523 if (SInfo.addRequired(MInfo.vregsRequired))
1524 todo.insert(*PrI);
1525 }
1526 }
1527}
1528
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001529// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001530// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001531void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001532 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001533 for (const auto &BBI : *MBB) {
1534 if (!BBI.isPHI())
1535 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001536 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001537
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001538 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1539 unsigned Reg = BBI.getOperand(i).getReg();
1540 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001541 if (!Pre->isSuccessor(MBB))
1542 continue;
1543 seen.insert(Pre);
1544 BBInfo &PrInfo = MBBInfoMap[Pre];
1545 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1546 report("PHI operand is not live-out from predecessor",
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001547 &BBI.getOperand(i), i);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001548 }
1549
1550 // Did we see all predecessors?
1551 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1552 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1553 if (!seen.count(*PrI)) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001554 report("Missing PHI operand", &BBI);
Owen Anderson21b17882015-02-04 00:02:59 +00001555 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001556 << " is a predecessor according to the CFG.\n";
1557 }
1558 }
1559 }
1560}
1561
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001562void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001563 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001564
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001565 for (const auto &MBB : *MF) {
1566 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001567
1568 // Skip unreachable MBBs.
1569 if (!MInfo.reachable)
1570 continue;
1571
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001572 checkPHIOps(&MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001573 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001574
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001575 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001576 calcRegsRequired();
1577
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001578 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001579 for (const auto &MBB : *MF) {
1580 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001581 for (RegSet::iterator
1582 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1583 ++I)
1584 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001585 report("Virtual register killed in block, but needed live out.", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001586 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001587 << " is used after the block.\n";
1588 }
1589 }
1590
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001591 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001592 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1593 for (RegSet::iterator
1594 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Matthias Braun30668dd2016-05-11 21:31:39 +00001595 ++I) {
1596 report("Virtual register defs don't dominate all uses.", MF);
1597 report_context_vreg(*I);
1598 }
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001599 }
1600
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001601 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001602 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001603 if (LiveInts)
1604 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001605}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001606
1607void MachineVerifier::verifyLiveVariables() {
1608 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001609 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1610 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001611 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001612 for (const auto &MBB : *MF) {
1613 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001614
1615 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1616 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001617 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1618 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001619 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001620 << " must be live through the block.\n";
1621 }
1622 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001623 if (VI.AliveBlocks.test(MBB.getNumber())) {
1624 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001625 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001626 << " is not needed live through the block.\n";
1627 }
1628 }
1629 }
1630 }
1631}
1632
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001633void MachineVerifier::verifyLiveIntervals() {
1634 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001635 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1636 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001637
1638 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001639 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001640 continue;
1641
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001642 if (!LiveInts->hasInterval(Reg)) {
1643 report("Missing live interval for virtual register", MF);
Owen Anderson21b17882015-02-04 00:02:59 +00001644 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001645 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001646 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001647
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001648 const LiveInterval &LI = LiveInts->getInterval(Reg);
1649 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001650 verifyLiveInterval(LI);
1651 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001652
1653 // Verify all the cached regunit intervals.
1654 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001655 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1656 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001657}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001658
Matthias Braun364e6e92013-10-10 21:28:54 +00001659void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001660 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001661 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001662 if (VNI->isUnused())
1663 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001664
Matthias Braun364e6e92013-10-10 21:28:54 +00001665 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001666
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001667 if (!DefVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001668 report("Value not live at VNInfo def and not marked unused", MF);
1669 report_context(LR, Reg, LaneMask);
1670 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001671 return;
1672 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001673
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001674 if (DefVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001675 report("Live segment at def has different VNInfo", MF);
1676 report_context(LR, Reg, LaneMask);
1677 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001678 return;
1679 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001680
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001681 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1682 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001683 report("Invalid VNInfo definition index", MF);
1684 report_context(LR, Reg, LaneMask);
1685 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001686 return;
1687 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001688
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001689 if (VNI->isPHIDef()) {
1690 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001691 report("PHIDef VNInfo is not defined at MBB start", MBB);
1692 report_context(LR, Reg, LaneMask);
1693 report_context(*VNI);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001694 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001695 return;
1696 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001697
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001698 // Non-PHI def.
1699 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1700 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001701 report("No instruction at VNInfo def index", MBB);
1702 report_context(LR, Reg, LaneMask);
1703 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001704 return;
1705 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001706
Matthias Braun364e6e92013-10-10 21:28:54 +00001707 if (Reg != 0) {
1708 bool hasDef = false;
1709 bool isEarlyClobber = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001710 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001711 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001712 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001713 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1714 if (MOI->getReg() != Reg)
1715 continue;
1716 } else {
1717 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1718 !TRI->hasRegUnit(MOI->getReg(), Reg))
1719 continue;
1720 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001721 if (LaneMask.any() &&
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001722 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask).none())
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001723 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001724 hasDef = true;
1725 if (MOI->isEarlyClobber())
1726 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001727 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001728
Matthias Braun364e6e92013-10-10 21:28:54 +00001729 if (!hasDef) {
1730 report("Defining instruction does not modify register", MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001731 report_context(LR, Reg, LaneMask);
1732 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001733 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001734
Matthias Braun364e6e92013-10-10 21:28:54 +00001735 // Early clobber defs begin at USE slots, but other defs must begin at
1736 // DEF slots.
1737 if (isEarlyClobber) {
1738 if (!VNI->def.isEarlyClobber()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001739 report("Early clobber def must be at an early-clobber slot", MBB);
1740 report_context(LR, Reg, LaneMask);
1741 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001742 }
1743 } else if (!VNI->def.isRegister()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001744 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
1745 report_context(LR, Reg, LaneMask);
1746 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001747 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001748 }
1749}
1750
Matthias Braun364e6e92013-10-10 21:28:54 +00001751void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1752 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001753 unsigned Reg, LaneBitmask LaneMask)
1754{
Matthias Braun364e6e92013-10-10 21:28:54 +00001755 const LiveRange::Segment &S = *I;
1756 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001757 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001758
Matthias Braun364e6e92013-10-10 21:28:54 +00001759 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001760 report("Foreign valno in live segment", MF);
1761 report_context(LR, Reg, LaneMask);
1762 report_context(S);
1763 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001764 }
1765
1766 if (VNI->isUnused()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001767 report("Live segment valno is marked unused", MF);
1768 report_context(LR, Reg, LaneMask);
1769 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001770 }
1771
Matthias Braun364e6e92013-10-10 21:28:54 +00001772 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001773 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001774 report("Bad start of live segment, no basic block", MF);
1775 report_context(LR, Reg, LaneMask);
1776 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001777 return;
1778 }
1779 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001780 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001781 report("Live segment must begin at MBB entry or valno def", MBB);
1782 report_context(LR, Reg, LaneMask);
1783 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001784 }
1785
1786 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001787 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001788 if (!EndMBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001789 report("Bad end of live segment, no basic block", MF);
1790 report_context(LR, Reg, LaneMask);
1791 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001792 return;
1793 }
1794
1795 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001796 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001797 return;
1798
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001799 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001800 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1801 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001802 return;
1803
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001804 // The live segment is ending inside EndMBB
1805 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001806 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001807 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001808 report("Live segment doesn't end at a valid instruction", EndMBB);
1809 report_context(LR, Reg, LaneMask);
1810 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001811 return;
1812 }
1813
1814 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001815 if (S.end.isBlock()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001816 report("Live segment ends at B slot of an instruction", EndMBB);
1817 report_context(LR, Reg, LaneMask);
1818 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001819 }
1820
Matthias Braun364e6e92013-10-10 21:28:54 +00001821 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001822 // Segment ends on the dead slot.
1823 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001824 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001825 report("Live segment ending at dead slot spans instructions", EndMBB);
1826 report_context(LR, Reg, LaneMask);
1827 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001828 }
1829 }
1830
1831 // A live segment can only end at an early-clobber slot if it is being
1832 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001833 if (S.end.isEarlyClobber()) {
1834 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001835 report("Live segment ending at early clobber slot must be "
Matthias Braun7e624d52015-11-09 23:59:33 +00001836 "redefined by an EC def in the same instruction", EndMBB);
1837 report_context(LR, Reg, LaneMask);
1838 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001839 }
1840 }
1841
1842 // The following checks only apply to virtual registers. Physreg liveness
1843 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001844 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001845 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001846 // use, or a dead flag on a def.
1847 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001848 bool hasSubRegDef = false;
Matthias Braun72a58c32016-03-29 19:07:43 +00001849 bool hasDeadDef = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001850 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001851 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001852 continue;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001853 unsigned Sub = MOI->getSubReg();
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001854 LaneBitmask SLM = Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub)
1855 : LaneBitmask::getAll();
Matthias Braun72a58c32016-03-29 19:07:43 +00001856 if (MOI->isDef()) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001857 if (Sub != 0) {
Matthias Braun72a58c32016-03-29 19:07:43 +00001858 hasSubRegDef = true;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001859 // An operand vreg0:sub0<def> reads vreg0:sub1..n. Invert the lane
1860 // mask for subregister defs. Read-undef defs will be handled by
1861 // readsReg below.
Krzysztof Parzyszek0a955d62016-08-29 13:15:35 +00001862 SLM = ~SLM;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001863 }
Matthias Braun72a58c32016-03-29 19:07:43 +00001864 if (MOI->isDead())
1865 hasDeadDef = true;
1866 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001867 if (LaneMask.any() && (LaneMask & SLM).none())
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001868 continue;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001869 if (MOI->readsReg())
1870 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001871 }
Matthias Braun72a58c32016-03-29 19:07:43 +00001872 if (S.end.isDead()) {
1873 // Make sure that the corresponding machine operand for a "dead" live
1874 // range has the dead flag. We cannot perform this check for subregister
1875 // liveranges as partially dead values are allowed.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001876 if (LaneMask.none() && !hasDeadDef) {
Matthias Braun72a58c32016-03-29 19:07:43 +00001877 report("Instruction ending live segment on dead slot has no dead flag",
1878 MI);
1879 report_context(LR, Reg, LaneMask);
1880 report_context(S);
1881 }
1882 } else {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001883 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001884 // When tracking subregister liveness, the main range must start new
1885 // values on partial register writes, even if there is no read.
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001886 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask.any() ||
Matthias Brauna25e13a2015-03-19 00:21:58 +00001887 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001888 report("Instruction ending live segment doesn't read the register",
1889 MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001890 report_context(LR, Reg, LaneMask);
1891 report_context(S);
Matthias Braun21554d92014-12-10 01:13:11 +00001892 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001893 }
1894 }
1895 }
1896
1897 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001898 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001899 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00001900 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001901 // Not live-in to any blocks.
1902 if (MBB == EndMBB)
1903 return;
1904 // Skip this block.
1905 ++MFI;
1906 }
1907 for (;;) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001908 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001909 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00001910 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00001911 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001912 if (&*MFI == EndMBB)
1913 break;
1914 ++MFI;
1915 continue;
1916 }
1917
1918 // Is VNI a PHI-def in the current block?
1919 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001920 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001921
1922 // Check that VNI is live-out of all predecessors.
1923 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1924 PE = MFI->pred_end(); PI != PE; ++PI) {
1925 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001926 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001927
Matthias Braune29b7682016-05-20 23:02:13 +00001928 // All predecessors must have a live-out value if this is not a
1929 // subregister liverange.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001930 if (!PVNI && LaneMask.none()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001931 report("Register not marked live out of predecessor", *PI);
1932 report_context(LR, Reg, LaneMask);
1933 report_context(*VNI);
1934 errs() << " live into BB#" << MFI->getNumber()
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001935 << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
1936 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001937 continue;
1938 }
1939
1940 // Only PHI-defs can take different predecessor values.
1941 if (!IsPHI && PVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001942 report("Different value live out of predecessor", *PI);
1943 report_context(LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001944 errs() << "Valno #" << PVNI->id << " live out of BB#"
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001945 << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
1946 << " live into BB#" << MFI->getNumber() << '@'
1947 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001948 }
1949 }
1950 if (&*MFI == EndMBB)
1951 break;
1952 ++MFI;
1953 }
1954}
1955
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001956void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001957 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00001958 for (const VNInfo *VNI : LR.valnos)
1959 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001960
Matthias Braun364e6e92013-10-10 21:28:54 +00001961 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001962 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00001963}
1964
1965void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001966 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00001967 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1968 verifyLiveRange(LI, Reg);
1969
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001970 LaneBitmask Mask;
Matthias Braune6a24852015-09-25 21:51:14 +00001971 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00001972 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001973 if ((Mask & SR.LaneMask).any()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001974 report("Lane masks of sub ranges overlap in live interval", MF);
1975 report_context(LI);
1976 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001977 if ((SR.LaneMask & ~MaxMask).any()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001978 report("Subrange lanemask is invalid", MF);
1979 report_context(LI);
1980 }
1981 if (SR.empty()) {
1982 report("Subrange must not be empty", MF);
1983 report_context(SR, LI.reg, SR.LaneMask);
1984 }
Matthias Braune962e522015-03-25 21:18:22 +00001985 Mask |= SR.LaneMask;
1986 verifyLiveRange(SR, LI.reg, SR.LaneMask);
Matthias Braun7e624d52015-11-09 23:59:33 +00001987 if (!LI.covers(SR)) {
1988 report("A Subrange is not covered by the main range", MF);
1989 report_context(LI);
1990 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001991 }
1992
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001993 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00001994 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
Matthias Braunbf47f632016-01-08 01:16:35 +00001995 unsigned NumComp = ConEQ.Classify(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001996 if (NumComp > 1) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001997 report("Multiple connected components in live interval", MF);
1998 report_context(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001999 for (unsigned comp = 0; comp != NumComp; ++comp) {
2000 errs() << comp << ": valnos";
2001 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
2002 E = LI.vni_end(); I!=E; ++I)
2003 if (comp == ConEQ.getEqClass(*I))
2004 errs() << ' ' << (*I)->id;
2005 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00002006 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002007 }
2008}
Manman Renaa6875b2013-07-15 21:26:31 +00002009
2010namespace {
2011 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
2012 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
2013 // value is zero.
2014 // We use a bool plus an integer to capture the stack state.
2015 struct StackStateOfBB {
2016 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
2017 ExitIsSetup(false) { }
2018 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
2019 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
2020 ExitIsSetup(ExitSetup) { }
2021 // Can be negative, which means we are setting up a frame.
2022 int EntryValue;
2023 int ExitValue;
2024 bool EntryIsSetup;
2025 bool ExitIsSetup;
2026 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002027}
Manman Renaa6875b2013-07-15 21:26:31 +00002028
2029/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
2030/// by a FrameDestroy <n>, stack adjustments are identical on all
2031/// CFG edges to a merge point, and frame is destroyed at end of a return block.
2032void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00002033 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
2034 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Serge Pavlov802aa662017-04-20 01:34:04 +00002035 if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
2036 return;
Manman Renaa6875b2013-07-15 21:26:31 +00002037
2038 SmallVector<StackStateOfBB, 8> SPState;
2039 SPState.resize(MF->getNumBlockIDs());
David Callahanc1051ab2016-10-05 21:36:16 +00002040 df_iterator_default_set<const MachineBasicBlock*> Reachable;
Manman Renaa6875b2013-07-15 21:26:31 +00002041
2042 // Visit the MBBs in DFS order.
2043 for (df_ext_iterator<const MachineFunction*,
David Callahanc1051ab2016-10-05 21:36:16 +00002044 df_iterator_default_set<const MachineBasicBlock*> >
Manman Renaa6875b2013-07-15 21:26:31 +00002045 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
2046 DFI != DFE; ++DFI) {
2047 const MachineBasicBlock *MBB = *DFI;
2048
2049 StackStateOfBB BBState;
2050 // Check the exit state of the DFS stack predecessor.
2051 if (DFI.getPathLength() >= 2) {
2052 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
2053 assert(Reachable.count(StackPred) &&
2054 "DFS stack predecessor is already visited.\n");
2055 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
2056 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
2057 BBState.ExitValue = BBState.EntryValue;
2058 BBState.ExitIsSetup = BBState.EntryIsSetup;
2059 }
2060
2061 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002062 for (const auto &I : *MBB) {
2063 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00002064 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002065 report("FrameSetup is after another FrameSetup", &I);
Serge Pavlovd526b132017-05-09 13:35:13 +00002066 BBState.ExitValue -= TII->getFrameTotalSize(I);
Manman Renaa6875b2013-07-15 21:26:31 +00002067 BBState.ExitIsSetup = true;
2068 }
2069
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002070 if (I.getOpcode() == FrameDestroyOpcode) {
Serge Pavlovd526b132017-05-09 13:35:13 +00002071 int Size = TII->getFrameTotalSize(I);
Manman Renaa6875b2013-07-15 21:26:31 +00002072 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002073 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00002074 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
2075 BBState.ExitValue;
2076 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002077 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00002078 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00002079 << AbsSPAdj << ">.\n";
2080 }
2081 BBState.ExitValue += Size;
2082 BBState.ExitIsSetup = false;
2083 }
2084 }
2085 SPState[MBB->getNumber()] = BBState;
2086
2087 // Make sure the exit state of any predecessor is consistent with the entry
2088 // state.
2089 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
2090 E = MBB->pred_end(); I != E; ++I) {
2091 if (Reachable.count(*I) &&
2092 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
2093 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
2094 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002095 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002096 << SPState[(*I)->getNumber()].ExitValue << ", "
2097 << SPState[(*I)->getNumber()].ExitIsSetup
2098 << "), while BB#" << MBB->getNumber() << " has entry state ("
2099 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
2100 }
2101 }
2102
2103 // Make sure the entry state of any successor is consistent with the exit
2104 // state.
2105 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
2106 E = MBB->succ_end(); I != E; ++I) {
2107 if (Reachable.count(*I) &&
2108 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
2109 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
2110 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002111 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002112 << SPState[(*I)->getNumber()].EntryValue << ", "
2113 << SPState[(*I)->getNumber()].EntryIsSetup
2114 << "), while BB#" << MBB->getNumber() << " has exit state ("
2115 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
2116 }
2117 }
2118
2119 // Make sure a basic block with return ends with zero stack adjustment.
2120 if (!MBB->empty() && MBB->back().isReturn()) {
2121 if (BBState.ExitIsSetup)
2122 report("A return block ends with a FrameSetup.", MBB);
2123 if (BBState.ExitValue)
2124 report("A return block ends with a nonzero stack adjustment.", MBB);
2125 }
2126 }
2127}