blob: fcb544806dda029a67ab05e3b72bf3175521897d [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
Chris Lattner565449d2009-08-23 03:13:20 +000026#include "llvm/ADT/DenseSet.h"
Manman Renaa6875b2013-07-15 21:26:31 +000027#include "llvm/ADT/DepthFirstIterator.h"
Chris Lattner565449d2009-08-23 03:13:20 +000028#include "llvm/ADT/SetOperations.h"
29#include "llvm/ADT/SmallVector.h"
David Majnemer70497c62015-12-02 23:06:39 +000030#include "llvm/Analysis/EHPersonalities.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/CodeGen/LiveIntervalAnalysis.h"
32#include "llvm/CodeGen/LiveStackAnalysis.h"
33#include "llvm/CodeGen/LiveVariables.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/CodeGen/MachineMemOperand.h"
37#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000038#include "llvm/CodeGen/Passes.h"
Philip Reames94cc4a22017-06-02 16:36:37 +000039#include "llvm/CodeGen/StackMaps.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/InlineAsm.h"
42#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000043#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000044#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000045#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000046#include "llvm/Support/FileSystem.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000047#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000048#include "llvm/Target/TargetInstrInfo.h"
49#include "llvm/Target/TargetMachine.h"
50#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000051#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000052using namespace llvm;
53
54namespace {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000055 struct MachineVerifier {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000056
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000057 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000058 PASS(pass),
Owen Anderson21b17882015-02-04 00:02:59 +000059 Banner(b)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000060 {}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000061
Matthias Braunb3aefc32016-02-15 19:25:31 +000062 unsigned verify(MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000063
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000064 Pass *const PASS;
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000065 const char *Banner;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000066 const MachineFunction *MF;
67 const TargetMachine *TM;
Evan Cheng8d71a752011-06-27 21:26:13 +000068 const TargetInstrInfo *TII;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000069 const TargetRegisterInfo *TRI;
70 const MachineRegisterInfo *MRI;
71
72 unsigned foundErrors;
73
Ahmed Bougacha3681c772016-08-02 16:17:15 +000074 // Avoid querying the MachineFunctionProperties for each operand.
75 bool isFunctionRegBankSelected;
Ahmed Bougachab14e9442016-08-02 16:49:22 +000076 bool isFunctionSelected;
Ahmed Bougacha3681c772016-08-02 16:17:15 +000077
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000078 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000079 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000080 typedef DenseSet<unsigned> RegSet;
81 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000082 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000083
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000084 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000085 BlockSet FunctionBlocks;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000086
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000087 BitVector regsReserved;
88 RegSet regsLive;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000089 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000090 RegMaskVector regMasks;
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 MBBInfoMap.clear();
423
Matthias Braunb3aefc32016-02-15 19:25:31 +0000424 return foundErrors;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000425}
426
Chris Lattner75f40452009-08-23 01:03:30 +0000427void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000428 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000429 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000430 if (!foundErrors++) {
431 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000432 errs() << "# " << Banner << '\n';
Matthias Braun42b4b632015-11-09 23:59:23 +0000433 if (LiveInts != nullptr)
434 LiveInts->print(errs());
435 else
436 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000437 }
Owen Anderson21b17882015-02-04 00:02:59 +0000438 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000439 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000440}
441
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000442void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000443 assert(MBB);
444 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000445 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000446 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000447 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000448 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000449 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000450 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000451 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000452}
453
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000454void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000455 assert(MI);
456 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000457 errs() << "- instruction: ";
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000458 if (Indexes && Indexes->hasIndex(*MI))
459 errs() << Indexes->getInstructionIndex(*MI) << '\t';
Matthias Braun45718db2015-11-09 23:59:25 +0000460 MI->print(errs(), /*SkipOpers=*/true);
Matthias Braun716b4332015-11-09 23:59:29 +0000461 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000462}
463
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000464void MachineVerifier::report(const char *msg,
465 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000466 assert(MO);
467 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000468 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000469 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000470 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000471}
472
Matthias Braun579c9cd2016-02-02 02:44:25 +0000473void MachineVerifier::report_context(SlotIndex Pos) const {
474 errs() << "- at: " << Pos << '\n';
475}
476
Matthias Braun7e624d52015-11-09 23:59:33 +0000477void MachineVerifier::report_context(const LiveInterval &LI) const {
Owen Anderson21b17882015-02-04 00:02:59 +0000478 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000479}
480
Matt Arsenault892fcd02016-07-25 19:39:01 +0000481void MachineVerifier::report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000482 LaneBitmask LaneMask) const {
Matthias Braun579c9cd2016-02-02 02:44:25 +0000483 report_context_liverange(LR);
Matt Arsenault892fcd02016-07-25 19:39:01 +0000484 report_context_vreg_regunit(VRegUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000485 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +0000486 report_context_lanemask(LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000487}
488
Matthias Braun7e624d52015-11-09 23:59:33 +0000489void MachineVerifier::report_context(const LiveRange::Segment &S) const {
490 errs() << "- segment: " << S << '\n';
491}
492
493void MachineVerifier::report_context(const VNInfo &VNI) const {
494 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
Matthias Braun364e6e92013-10-10 21:28:54 +0000495}
496
Matthias Braun579c9cd2016-02-02 02:44:25 +0000497void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
498 errs() << "- liverange: " << LR << '\n';
499}
500
Matthias Braun30668dd2016-05-11 21:31:39 +0000501void MachineVerifier::report_context_vreg(unsigned VReg) const {
502 errs() << "- v. register: " << PrintReg(VReg, TRI) << '\n';
503}
504
Matthias Braun1377fd62016-02-02 20:04:51 +0000505void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
506 if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
Matthias Braun30668dd2016-05-11 21:31:39 +0000507 report_context_vreg(VRegOrUnit);
Matthias Braun1377fd62016-02-02 20:04:51 +0000508 } else {
509 errs() << "- regunit: " << PrintRegUnit(VRegOrUnit, TRI) << '\n';
510 }
511}
512
513void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
514 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
515}
516
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000517void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000518 BBInfo &MInfo = MBBInfoMap[MBB];
519 if (!MInfo.reachable) {
520 MInfo.reachable = true;
521 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
522 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
523 markReachable(*SuI);
524 }
525}
526
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000527void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000528 lastIndex = SlotIndex();
Matthias Braun4682ac62017-05-05 22:04:05 +0000529 regsReserved = MRI->reservedRegsFrozen() ? MRI->getReservedRegs()
530 : TRI->getReservedRegs(*MF);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000531
Justin Bogner20dd36a2017-04-11 19:32:41 +0000532 if (!MF->empty())
533 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000534
535 // Build a set of the basic blocks in the function.
536 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000537 for (const auto &MBB : *MF) {
538 FunctionBlocks.insert(&MBB);
539 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000540
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000541 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
542 if (MInfo.Preds.size() != MBB.pred_size())
543 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000544
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000545 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
546 if (MInfo.Succs.size() != MBB.succ_size())
547 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000548 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000549
550 // Check that the register use lists are sane.
551 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000552
Justin Bogner20dd36a2017-04-11 19:32:41 +0000553 if (!MF->empty())
554 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000555}
556
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000557// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000558static bool matchPair(MachineBasicBlock::const_succ_iterator i,
559 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000560 if (*i == a)
561 return *++i == b;
562 if (*i == b)
563 return *++i == a;
564 return false;
565}
566
567void
568MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000569 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000570
Matthias Braun79f85b32016-08-24 01:32:41 +0000571 if (!MF->getProperties().hasProperty(
Matthias Braun11723322017-01-05 20:01:19 +0000572 MachineFunctionProperties::Property::NoPHIs) && MRI->tracksLiveness()) {
Lang Hames1ce837a2012-02-14 19:17:48 +0000573 // If this block has allocatable physical registers live-in, check that
574 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000575 for (const auto &LI : MBB->liveins()) {
576 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +0000577 MBB->getIterator() != MBB->getParent()->begin()) {
Matt Arsenault900b21c2017-02-15 22:19:06 +0000578 report("MBB has allocatable live-in, but isn't entry or landing-pad.", MBB);
Lang Hames1ce837a2012-02-14 19:17:48 +0000579 }
580 }
581 }
582
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000583 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000584 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000585 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000586 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000587 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000588 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000589 if (!FunctionBlocks.count(*I))
590 report("MBB has successor that isn't part of the function.", MBB);
591 if (!MBBInfoMap[*I].Preds.count(MBB)) {
592 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000593 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000594 << (*I)->getNumber() << ".\n";
595 }
596 }
597
598 // Check the predecessor list.
599 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
600 E = MBB->pred_end(); I != E; ++I) {
601 if (!FunctionBlocks.count(*I))
602 report("MBB has predecessor that isn't part of the function.", MBB);
603 if (!MBBInfoMap[*I].Succs.count(MBB)) {
604 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000605 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000606 << (*I)->getNumber() << ".\n";
607 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000608 }
Bill Wendling2a401312011-05-04 22:54:05 +0000609
610 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
611 const BasicBlock *BB = MBB->getBasicBlock();
Reid Kleckner64b003f2015-11-09 21:04:00 +0000612 const Function *Fn = MF->getFunction();
Bill Wendling2a401312011-05-04 22:54:05 +0000613 if (LandingPadSuccs.size() > 1 &&
614 !(AsmInfo &&
615 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
Reid Kleckner64b003f2015-11-09 21:04:00 +0000616 BB && isa<SwitchInst>(BB->getTerminator())) &&
617 !isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000618 report("MBB has more than one landing pad successor", MBB);
619
Dan Gohman352a4952009-08-27 02:43:49 +0000620 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000621 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000622 SmallVector<MachineOperand, 4> Cond;
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000623 if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
624 Cond)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000625 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
626 // check whether its answers match up with reality.
627 if (!TBB && !FBB) {
628 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000629 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000630 ++MBBI;
631 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000632 // It's possible that the block legitimately ends with a noreturn
633 // call or an unreachable, in which case it won't actually fall
634 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000635 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000636 // It's possible that the block legitimately ends with a noreturn
637 // call or an unreachable, in which case it won't actuall fall
638 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000639 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000640 report("MBB exits via unconditional fall-through but doesn't have "
641 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000642 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000643 report("MBB exits via unconditional fall-through but its successor "
644 "differs from its CFG successor!", MBB);
645 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000646 if (!MBB->empty() && MBB->back().isBarrier() &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000647 !TII->isPredicated(MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000648 report("MBB exits via unconditional fall-through but ends with a "
649 "barrier instruction!", MBB);
650 }
651 if (!Cond.empty()) {
652 report("MBB exits via unconditional fall-through but has a condition!",
653 MBB);
654 }
655 } else if (TBB && !FBB && Cond.empty()) {
656 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000657 // If the block has exactly one successor, that happens to be a
658 // landingpad, accept it as valid control flow.
659 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
660 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
661 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000662 report("MBB exits via unconditional branch but doesn't have "
663 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000664 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000665 report("MBB exits via unconditional branch but the CFG "
666 "successor doesn't match the actual successor!", MBB);
667 }
668 if (MBB->empty()) {
669 report("MBB exits via unconditional branch but doesn't contain "
670 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000671 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000672 report("MBB exits via unconditional branch but doesn't end with a "
673 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000674 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000675 report("MBB exits via unconditional branch but the branch isn't a "
676 "terminator instruction!", MBB);
677 }
678 } else if (TBB && !FBB && !Cond.empty()) {
679 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000680 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000681 ++MBBI;
682 if (MBBI == MF->end()) {
683 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000684 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000685 // A conditional branch with only one successor is weird, but allowed.
686 if (&*MBBI != TBB)
687 report("MBB exits via conditional branch/fall-through but only has "
688 "one CFG successor!", MBB);
689 else if (TBB != *MBB->succ_begin())
690 report("MBB exits via conditional branch/fall-through but the CFG "
691 "successor don't match the actual successor!", MBB);
692 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000693 report("MBB exits via conditional branch/fall-through but doesn't have "
694 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000695 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000696 report("MBB exits via conditional branch/fall-through but the CFG "
697 "successors don't match the actual successors!", MBB);
698 }
699 if (MBB->empty()) {
700 report("MBB exits via conditional branch/fall-through but doesn't "
701 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000702 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000703 report("MBB exits via conditional branch/fall-through but ends with a "
704 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000705 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000706 report("MBB exits via conditional branch/fall-through but the branch "
707 "isn't a terminator instruction!", MBB);
708 }
709 } else if (TBB && FBB) {
710 // Block conditionally branches somewhere, otherwise branches
711 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000712 if (MBB->succ_size() == 1) {
713 // A conditional branch with only one successor is weird, but allowed.
714 if (FBB != TBB)
715 report("MBB exits via conditional branch/branch through but only has "
716 "one CFG successor!", MBB);
717 else if (TBB != *MBB->succ_begin())
718 report("MBB exits via conditional branch/branch through but the CFG "
719 "successor don't match the actual successor!", MBB);
720 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000721 report("MBB exits via conditional branch/branch but doesn't have "
722 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000723 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000724 report("MBB exits via conditional branch/branch but the CFG "
725 "successors don't match the actual successors!", MBB);
726 }
727 if (MBB->empty()) {
728 report("MBB exits via conditional branch/branch but doesn't "
729 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000730 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000731 report("MBB exits via conditional branch/branch but doesn't end with a "
732 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000733 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000734 report("MBB exits via conditional branch/branch but the branch "
735 "isn't a terminator instruction!", MBB);
736 }
737 if (Cond.empty()) {
738 report("MBB exits via conditinal branch/branch but there's no "
739 "condition!", MBB);
740 }
741 } else {
742 report("AnalyzeBranch returned invalid data!", MBB);
743 }
744 }
745
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000746 regsLive.clear();
Matthias Braun11723322017-01-05 20:01:19 +0000747 if (MRI->tracksLiveness()) {
748 for (const auto &LI : MBB->liveins()) {
749 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
750 report("MBB live-in list contains non-physical register", MBB);
751 continue;
752 }
753 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
754 SubRegs.isValid(); ++SubRegs)
755 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000756 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000757 }
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000758
Matthias Braun941a7052016-07-28 18:40:00 +0000759 const MachineFrameInfo &MFI = MF->getFrameInfo();
760 BitVector PR = MFI.getPristineRegs(*MF);
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +0000761 for (unsigned I : PR.set_bits()) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000762 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
763 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000764 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000765 }
766
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000767 regsKilled.clear();
768 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000769
770 if (Indexes)
771 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000772}
773
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000774// This function gets called for all bundle headers, including normal
775// stand-alone unbundled instructions.
776void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000777 if (Indexes && Indexes->hasIndex(*MI)) {
778 SlotIndex idx = Indexes->getInstructionIndex(*MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000779 if (!(idx > lastIndex)) {
780 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000781 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000782 }
783 lastIndex = idx;
784 }
Pete Coopercd720162012-06-07 17:41:39 +0000785
786 // Ensure non-terminators don't follow terminators.
787 // Ignore predicated terminators formed by if conversion.
788 // FIXME: If conversion shouldn't need to violate this rule.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000789 if (MI->isTerminator() && !TII->isPredicated(*MI)) {
Pete Coopercd720162012-06-07 17:41:39 +0000790 if (!FirstTerminator)
791 FirstTerminator = MI;
792 } else if (FirstTerminator) {
793 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000794 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000795 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000796}
797
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000798// The operands on an INLINEASM instruction must follow a template.
799// Verify that the flag operands make sense.
800void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
801 // The first two operands on INLINEASM are the asm string and global flags.
802 if (MI->getNumOperands() < 2) {
803 report("Too few operands on inline asm", MI);
804 return;
805 }
806 if (!MI->getOperand(0).isSymbol())
807 report("Asm string must be an external symbol", MI);
808 if (!MI->getOperand(1).isImm())
809 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000810 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
Wei Ding0526e7f2016-06-22 18:51:08 +0000811 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
812 // and Extra_IsConvergent = 32.
813 if (!isUInt<6>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000814 report("Unknown asm flags", &MI->getOperand(1), 1);
815
Gabor Horvathfee04342015-03-16 09:53:42 +0000816 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000817
818 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
819 unsigned NumOps;
820 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
821 const MachineOperand &MO = MI->getOperand(OpNo);
822 // There may be implicit ops after the fixed operands.
823 if (!MO.isImm())
824 break;
825 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
826 }
827
828 if (OpNo > MI->getNumOperands())
829 report("Missing operands in last group", MI);
830
831 // An optional MDNode follows the groups.
832 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
833 ++OpNo;
834
835 // All trailing operands must be implicit registers.
836 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
837 const MachineOperand &MO = MI->getOperand(OpNo);
838 if (!MO.isReg() || !MO.isImplicit())
839 report("Expected implicit register after groups", &MO, OpNo);
840 }
841}
842
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000843void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000844 const MCInstrDesc &MCID = MI->getDesc();
845 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000846 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000847 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000848 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000849 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000850
Matthias Braun90799ce2016-08-23 21:19:49 +0000851 if (MI->isPHI() && MF->getProperties().hasProperty(
852 MachineFunctionProperties::Property::NoPHIs))
853 report("Found PHI instruction with NoPHIs property set", MI);
854
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000855 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000856 if (MI->isInlineAsm())
857 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000858
Dan Gohmandb9493c2009-10-07 17:36:00 +0000859 // Check the MachineMemOperands for basic consistency.
860 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
861 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000862 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000863 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000864 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000865 report("Missing mayStore flag", MI);
866 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000867
868 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000869 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000870 if (LiveInts) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000871 bool mapped = !LiveInts->isNotInMIMap(*MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000872 if (MI->isDebugValue()) {
873 if (mapped)
874 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000875 } else if (MI->isInsideBundle()) {
876 if (mapped)
877 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000878 } else {
879 if (!mapped)
880 report("Missing slot index", MI);
881 }
882 }
883
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000884 // Check types.
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000885 if (isPreISelGenericOpcode(MCID.getOpcode())) {
Ahmed Bougachab14e9442016-08-02 16:49:22 +0000886 if (isFunctionSelected)
887 report("Unexpected generic instruction in a Selected function", MI);
888
Tim Northover0f140c72016-09-09 11:46:34 +0000889 // Generic instructions specify equality constraints between some
890 // of their operands. Make sure these are consistent.
891 SmallVector<LLT, 4> Types;
892 for (unsigned i = 0; i < MCID.getNumOperands(); ++i) {
893 if (!MCID.OpInfo[i].isGenericType())
894 continue;
895 size_t TypeIdx = MCID.OpInfo[i].getGenericTypeIndex();
896 Types.resize(std::max(TypeIdx + 1, Types.size()));
897
898 LLT OpTy = MRI->getType(MI->getOperand(i).getReg());
899 if (Types[TypeIdx].isValid() && Types[TypeIdx] != OpTy)
900 report("type mismatch in generic instruction", MI);
901 Types[TypeIdx] = OpTy;
902 }
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000903 }
904
Tim Northovere5102de2016-08-30 18:52:46 +0000905 // Generic opcodes must not have physical register operands.
Tim Northover25d12862016-09-09 11:47:31 +0000906 if (isPreISelGenericOpcode(MCID.getOpcode())) {
Tim Northovere5102de2016-08-30 18:52:46 +0000907 for (auto &Op : MI->operands()) {
908 if (Op.isReg() && TargetRegisterInfo::isPhysicalRegister(Op.getReg()))
909 report("Generic instruction cannot have physical register", MI);
910 }
911 }
912
Andrew Trick924123a2011-09-21 02:20:46 +0000913 StringRef ErrorInfo;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000914 if (!TII->verifyInstruction(*MI, ErrorInfo))
Andrew Trick924123a2011-09-21 02:20:46 +0000915 report(ErrorInfo.data(), MI);
Philip Reames94cc4a22017-06-02 16:36:37 +0000916
917 // Verify properties of various specific instruction types
918 switch(MI->getOpcode()) {
919 default:
920 break;
921 case TargetOpcode::G_LOAD:
922 case TargetOpcode::G_STORE:
923 // Generic loads and stores must have a single MachineMemOperand
924 // describing that access.
925 if (!MI->hasOneMemOperand())
926 report("Generic instruction accessing memory must have one mem operand",
927 MI);
928 break;
929 case TargetOpcode::STATEPOINT:
930 if (!MI->getOperand(StatepointOpers::IDPos).isImm() ||
931 !MI->getOperand(StatepointOpers::NBytesPos).isImm() ||
932 !MI->getOperand(StatepointOpers::NCallArgsPos).isImm())
933 report("meta operands to STATEPOINT not constant!", MI);
934 break;
Philip Reames0f02bbc2017-06-02 17:02:33 +0000935
936 auto VerifyStackMapConstant = [&](unsigned Offset) {
937 if (!MI->getOperand(Offset).isImm() ||
938 MI->getOperand(Offset).getImm() != StackMaps::ConstantOp ||
939 !MI->getOperand(Offset + 1).isImm())
940 report("stack map constant to STATEPOINT not well formed!", MI);
941 };
942 const unsigned VarStart = StatepointOpers(MI).getVarIdx();
943 VerifyStackMapConstant(VarStart + StatepointOpers::CCOffset);
944 VerifyStackMapConstant(VarStart + StatepointOpers::FlagsOffset);
945 VerifyStackMapConstant(VarStart + StatepointOpers::NumDeoptOperandsOffset);
946
947 // TODO: verify we have properly encoded deopt arguments
Philip Reames94cc4a22017-06-02 16:36:37 +0000948 };
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000949}
950
951void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000952MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000953 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000954 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +0000955 unsigned NumDefs = MCID.getNumDefs();
956 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
957 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000958
Evan Cheng6cc775f2011-06-28 19:10:37 +0000959 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +0000960 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000961 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000962 if (!MO->isReg())
963 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +0000964 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000965 report("Explicit definition marked as use", MO, MONum);
966 else if (MO->isImplicit())
967 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000968 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000969 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +0000970 // Don't check if it's the last operand in a variadic instruction. See,
971 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000972 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000973 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000974 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +0000975 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000976 if (MO->isImplicit())
977 report("Explicit operand marked as implicit", MO, MONum);
978 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000979
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000980 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
981 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000982 if (!MO->isReg())
983 report("Tied use must be a register", MO, MONum);
984 else if (!MO->isTied())
985 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000986 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
987 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Mikael Holmen9c3e2ea2017-07-06 13:18:21 +0000988 else if (TargetRegisterInfo::isPhysicalRegister(MO->getReg())) {
989 const MachineOperand &MOTied = MI->getOperand(TiedTo);
990 if (!MOTied.isReg())
991 report("Tied counterpart must be a register", &MOTied, TiedTo);
992 else if (TargetRegisterInfo::isPhysicalRegister(MOTied.getReg()) &&
993 MO->getReg() != MOTied.getReg())
994 report("Tied physical registers must match.", &MOTied, TiedTo);
995 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000996 } else if (MO->isReg() && MO->isTied())
997 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000998 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +0000999 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001000 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +00001001 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +00001002 }
1003
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001004 switch (MO->getType()) {
1005 case MachineOperand::MO_Register: {
1006 const unsigned Reg = MO->getReg();
1007 if (!Reg)
1008 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001009 if (MRI->tracksLiveness() && !MI->isDebugValue())
1010 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001011
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +00001012 // Verify the consistency of tied operands.
1013 if (MO->isTied()) {
1014 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
1015 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
1016 if (!OtherMO.isReg())
1017 report("Must be tied to a register", MO, MONum);
1018 if (!OtherMO.isTied())
1019 report("Missing tie flags on tied operand", MO, MONum);
1020 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
1021 report("Inconsistent tie links", MO, MONum);
1022 if (MONum < MCID.getNumDefs()) {
1023 if (OtherIdx < MCID.getNumOperands()) {
1024 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
1025 report("Explicit def tied to explicit use without tie constraint",
1026 MO, MONum);
1027 } else {
1028 if (!OtherMO.isImplicit())
1029 report("Explicit def should be tied to implicit use", MO, MONum);
1030 }
1031 }
1032 }
1033
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001034 // Verify two-address constraints after leaving SSA form.
1035 unsigned DefIdx;
1036 if (!MRI->isSSA() && MO->isUse() &&
1037 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
1038 Reg != MI->getOperand(DefIdx).getReg())
1039 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001040
1041 // Check register classes.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001042 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001043 unsigned SubIdx = MO->getSubReg();
1044
1045 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001046 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001047 report("Illegal subregister index for physical register", MO, MONum);
1048 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001049 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001050 if (const TargetRegisterClass *DRC =
1051 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001052 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001053 report("Illegal physical register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001054 errs() << TRI->getName(Reg) << " is not a "
Craig Toppercf0444b2014-11-17 05:50:14 +00001055 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001056 }
1057 }
1058 } else {
1059 // Virtual register.
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001060 const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
1061 if (!RC) {
1062 // This is a generic virtual register.
Ahmed Bougachab14e9442016-08-02 16:49:22 +00001063
1064 // If we're post-Select, we can't have gvregs anymore.
1065 if (isFunctionSelected) {
1066 report("Generic virtual register invalid in a Selected function",
1067 MO, MONum);
1068 return;
1069 }
1070
Quentin Colombet3749f332016-12-22 22:50:34 +00001071 // The gvreg must have a type and it must not have a SubIdx.
Tim Northover0f140c72016-09-09 11:46:34 +00001072 LLT Ty = MRI->getType(Reg);
1073 if (!Ty.isValid()) {
1074 report("Generic virtual register must have a valid type", MO,
1075 MONum);
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001076 return;
1077 }
Ahmed Bougacha3681c772016-08-02 16:17:15 +00001078
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001079 const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
Ahmed Bougacha3681c772016-08-02 16:17:15 +00001080
1081 // If we're post-RegBankSelect, the gvreg must have a bank.
1082 if (!RegBank && isFunctionRegBankSelected) {
1083 report("Generic virtual register must have a bank in a "
1084 "RegBankSelected function",
1085 MO, MONum);
1086 return;
1087 }
1088
1089 // Make sure the register fits into its register bank if any.
Tim Northover32a078a2016-09-15 10:09:59 +00001090 if (RegBank && Ty.isValid() &&
Tim Northover0f140c72016-09-09 11:46:34 +00001091 RegBank->getSize() < Ty.getSizeInBits()) {
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001092 report("Register bank is too small for virtual register", MO,
1093 MONum);
1094 errs() << "Register bank " << RegBank->getName() << " too small("
Tim Northover0f140c72016-09-09 11:46:34 +00001095 << RegBank->getSize() << ") to fit " << Ty.getSizeInBits()
1096 << "-bits\n";
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001097 return;
1098 }
1099 if (SubIdx) {
Tim Northover0f140c72016-09-09 11:46:34 +00001100 report("Generic virtual register does not subregister index", MO,
1101 MONum);
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001102 return;
1103 }
Quentin Colombetfa5960a2016-12-22 21:56:39 +00001104
1105 // If this is a target specific instruction and this operand
1106 // has register class constraint, the virtual register must
1107 // comply to it.
1108 if (!isPreISelGenericOpcode(MCID.getOpcode()) &&
1109 TII->getRegClass(MCID, MONum, TRI, *MF)) {
1110 report("Virtual register does not match instruction constraint", MO,
1111 MONum);
1112 errs() << "Expect register class "
1113 << TRI->getRegClassName(
1114 TII->getRegClass(MCID, MONum, TRI, *MF))
1115 << " but got nothing\n";
1116 return;
1117 }
1118
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001119 break;
1120 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001121 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001122 const TargetRegisterClass *SRC =
1123 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001124 if (!SRC) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001125 report("Invalid subregister index for virtual register", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001126 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001127 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001128 return;
1129 }
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001130 if (RC != SRC) {
1131 report("Invalid register class for subregister index", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001132 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001133 << " does not fully support subreg index " << SubIdx << "\n";
1134 return;
1135 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001136 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001137 if (const TargetRegisterClass *DRC =
1138 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001139 if (SubIdx) {
1140 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +00001141 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001142 if (!SuperRC) {
1143 report("No largest legal super class exists.", MO, MONum);
1144 return;
1145 }
1146 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
1147 if (!DRC) {
1148 report("No matching super-reg register class.", MO, MONum);
1149 return;
1150 }
1151 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +00001152 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001153 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001154 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +00001155 << " register, but got a " << TRI->getRegClassName(RC)
1156 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001157 }
1158 }
1159 }
1160 }
1161 break;
1162 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001163
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001164 case MachineOperand::MO_RegisterMask:
1165 regMasks.push_back(MO->getRegMask());
1166 break;
1167
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001168 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +00001169 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
1170 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001171 break;
1172
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001173 case MachineOperand::MO_FrameIndex:
1174 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001175 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
Jonas Paulsson72640f12015-10-29 08:28:35 +00001176 int FI = MO->getIndex();
1177 LiveInterval &LI = LiveStks->getInterval(FI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001178 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001179
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001180 bool stores = MI->mayStore();
Jonas Paulsson72640f12015-10-29 08:28:35 +00001181 bool loads = MI->mayLoad();
1182 // For a memory-to-memory move, we need to check if the frame
1183 // index is used for storing or loading, by inspecting the
1184 // memory operands.
1185 if (stores && loads) {
1186 for (auto *MMO : MI->memoperands()) {
1187 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1188 if (PSV == nullptr) continue;
1189 const FixedStackPseudoSourceValue *Value =
1190 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1191 if (Value == nullptr) continue;
1192 if (Value->getFrameIndex() != FI) continue;
1193
1194 if (MMO->isStore())
1195 loads = false;
1196 else
1197 stores = false;
1198 break;
1199 }
1200 if (loads == stores)
1201 report("Missing fixed stack memoperand.", MI);
1202 }
1203 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001204 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001205 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001206 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001207 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001208 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001209 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001210 }
1211 }
1212 break;
1213
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001214 default:
1215 break;
1216 }
1217}
1218
Matthias Braun1377fd62016-02-02 20:04:51 +00001219void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
1220 unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
1221 LaneBitmask LaneMask) {
1222 LiveQueryResult LRQ = LR.Query(UseIdx);
1223 // Check if we have a segment at the use, note however that we only need one
1224 // live subregister range, the others may be dead.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001225 if (!LRQ.valueIn() && LaneMask.none()) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001226 report("No live segment at use", MO, MONum);
1227 report_context_liverange(LR);
1228 report_context_vreg_regunit(VRegOrUnit);
1229 report_context(UseIdx);
1230 }
1231 if (MO->isKill() && !LRQ.isKill()) {
1232 report("Live range continues after kill flag", MO, MONum);
1233 report_context_liverange(LR);
1234 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001235 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001236 report_context_lanemask(LaneMask);
1237 report_context(UseIdx);
1238 }
1239}
1240
1241void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
1242 unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
1243 LaneBitmask LaneMask) {
1244 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
1245 assert(VNI && "NULL valno is not allowed");
1246 if (VNI->def != DefIdx) {
1247 report("Inconsistent valno->def", MO, MONum);
1248 report_context_liverange(LR);
1249 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001250 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001251 report_context_lanemask(LaneMask);
1252 report_context(*VNI);
1253 report_context(DefIdx);
1254 }
1255 } else {
1256 report("No live segment at def", MO, MONum);
1257 report_context_liverange(LR);
1258 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001259 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001260 report_context_lanemask(LaneMask);
1261 report_context(DefIdx);
1262 }
1263 // Check that, if the dead def flag is present, LiveInts agree.
1264 if (MO->isDead()) {
1265 LiveQueryResult LRQ = LR.Query(DefIdx);
1266 if (!LRQ.isDeadDef()) {
1267 // In case of physregs we can have a non-dead definition on another
1268 // operand.
1269 bool otherDef = false;
1270 if (!TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
1271 const MachineInstr &MI = *MO->getParent();
1272 for (const MachineOperand &MO : MI.operands()) {
1273 if (!MO.isReg() || !MO.isDef() || MO.isDead())
1274 continue;
1275 unsigned Reg = MO.getReg();
1276 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1277 if (*Units == VRegOrUnit) {
1278 otherDef = true;
1279 break;
1280 }
1281 }
1282 }
1283 }
1284
1285 if (!otherDef) {
1286 report("Live range continues after dead def flag", MO, MONum);
1287 report_context_liverange(LR);
1288 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001289 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001290 report_context_lanemask(LaneMask);
1291 }
1292 }
1293 }
1294}
1295
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001296void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1297 const MachineInstr *MI = MO->getParent();
1298 const unsigned Reg = MO->getReg();
1299
1300 // Both use and def operands can read a register.
1301 if (MO->readsReg()) {
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001302 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001303 addRegWithSubRegs(regsKilled, Reg);
1304
1305 // Check that LiveVars knows this kill.
1306 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1307 MO->isKill()) {
1308 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
David Majnemer0d955d02016-08-11 22:21:41 +00001309 if (!is_contained(VI.Kills, MI))
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001310 report("Kill missing from LiveVariables", MO, MONum);
1311 }
1312
1313 // Check LiveInts liveness and kill.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001314 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1315 SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001316 // Check the cached regunit intervals.
1317 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1318 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001319 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
1320 checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001321 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001322 }
1323
1324 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1325 if (LiveInts->hasInterval(Reg)) {
1326 // This is a virtual register interval.
1327 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun1377fd62016-02-02 20:04:51 +00001328 checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
1329
1330 if (LI.hasSubRanges() && !MO->isDef()) {
1331 unsigned SubRegIdx = MO->getSubReg();
1332 LaneBitmask MOMask = SubRegIdx != 0
1333 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1334 : MRI->getMaxLaneMaskForVReg(Reg);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001335 LaneBitmask LiveInMask;
Matthias Braun1377fd62016-02-02 20:04:51 +00001336 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001337 if ((MOMask & SR.LaneMask).none())
Matthias Braun1377fd62016-02-02 20:04:51 +00001338 continue;
1339 checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
1340 LiveQueryResult LRQ = SR.Query(UseIdx);
1341 if (LRQ.valueIn())
1342 LiveInMask |= SR.LaneMask;
1343 }
1344 // At least parts of the register has to be live at the use.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001345 if ((LiveInMask & MOMask).none()) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001346 report("No live subrange at use", MO, MONum);
1347 report_context(LI);
1348 report_context(UseIdx);
1349 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001350 }
1351 } else {
1352 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001353 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001354 }
1355 }
1356
1357 // Use of a dead register.
1358 if (!regsLive.count(Reg)) {
1359 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1360 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001361 bool Bad = !isReserved(Reg);
1362 // We are fine if just any subregister has a defined value.
1363 if (Bad) {
1364 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1365 ++SubRegs) {
1366 if (regsLive.count(*SubRegs)) {
1367 Bad = false;
1368 break;
1369 }
1370 }
1371 }
Matthias Braun96a31952015-01-14 22:25:14 +00001372 // If there is an additional implicit-use of a super register we stop
1373 // here. By definition we are fine if the super register is not
1374 // (completely) dead, if the complete super register is dead we will
1375 // get a report for its operand.
1376 if (Bad) {
1377 for (const MachineOperand &MOP : MI->uses()) {
1378 if (!MOP.isReg())
1379 continue;
1380 if (!MOP.isImplicit())
1381 continue;
1382 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1383 ++SubRegs) {
1384 if (*SubRegs == Reg) {
1385 Bad = false;
1386 break;
1387 }
1388 }
1389 }
1390 }
Matthias Braun96d77322014-12-10 01:13:13 +00001391 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001392 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001393 } else if (MRI->def_empty(Reg)) {
1394 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001395 } else {
1396 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1397 // We don't know which virtual registers are live in, so only complain
1398 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1399 // must be live in. PHI instructions are handled separately.
1400 if (MInfo.regsKilled.count(Reg))
1401 report("Using a killed virtual register", MO, MONum);
1402 else if (!MI->isPHI())
1403 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1404 }
1405 }
1406 }
1407
1408 if (MO->isDef()) {
1409 // Register defined.
1410 // TODO: verify that earlyclobber ops are not used.
1411 if (MO->isDead())
1412 addRegWithSubRegs(regsDead, Reg);
1413 else
1414 addRegWithSubRegs(regsDefined, Reg);
1415
1416 // Verify SSA form.
1417 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001418 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001419 report("Multiple virtual register defs in SSA form", MO, MONum);
1420
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001421 // Check LiveInts for a live segment, but only for virtual registers.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001422 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1423 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001424 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Matthias Braun1377fd62016-02-02 20:04:51 +00001425
1426 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1427 if (LiveInts->hasInterval(Reg)) {
1428 const LiveInterval &LI = LiveInts->getInterval(Reg);
1429 checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
1430
1431 if (LI.hasSubRanges()) {
1432 unsigned SubRegIdx = MO->getSubReg();
1433 LaneBitmask MOMask = SubRegIdx != 0
1434 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1435 : MRI->getMaxLaneMaskForVReg(Reg);
1436 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001437 if ((SR.LaneMask & MOMask).none())
Matthias Braun1377fd62016-02-02 20:04:51 +00001438 continue;
1439 checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, SR.LaneMask);
1440 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001441 }
1442 } else {
Matthias Braun1377fd62016-02-02 20:04:51 +00001443 report("Virtual register has no Live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001444 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001445 }
1446 }
1447 }
1448}
1449
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001450void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001451}
1452
1453// This function gets called after visiting all instructions in a bundle. The
1454// argument points to the bundle header.
1455// Normal stand-alone instructions are also considered 'bundles', and this
1456// function is called for all of them.
1457void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001458 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1459 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001460 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001461 // Kill any masked registers.
1462 while (!regMasks.empty()) {
1463 const uint32_t *Mask = regMasks.pop_back_val();
1464 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1465 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1466 MachineOperand::clobbersPhysReg(Mask, *I))
1467 regsDead.push_back(*I);
1468 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001469 set_subtract(regsLive, regsDead); regsDead.clear();
1470 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001471}
1472
1473void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001474MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001475 MBBInfoMap[MBB].regsLiveOut = regsLive;
1476 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001477
1478 if (Indexes) {
1479 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1480 if (!(stop > lastIndex)) {
1481 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001482 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001483 << " last instruction was at " << lastIndex << '\n';
1484 }
1485 lastIndex = stop;
1486 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001487}
1488
1489// Calculate the largest possible vregsPassed sets. These are the registers that
1490// can pass through an MBB live, but may not be live every time. It is assumed
1491// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001492void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001493 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1494 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001495 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001496 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001497 BBInfo &MInfo = MBBInfoMap[&MBB];
1498 if (!MInfo.reachable)
1499 continue;
1500 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1501 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1502 BBInfo &SInfo = MBBInfoMap[*SuI];
1503 if (SInfo.addPassed(MInfo.regsLiveOut))
1504 todo.insert(*SuI);
1505 }
1506 }
1507
1508 // Iteratively push vregsPassed to successors. This will converge to the same
1509 // final state regardless of DenseSet iteration order.
1510 while (!todo.empty()) {
1511 const MachineBasicBlock *MBB = *todo.begin();
1512 todo.erase(MBB);
1513 BBInfo &MInfo = MBBInfoMap[MBB];
1514 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1515 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1516 if (*SuI == MBB)
1517 continue;
1518 BBInfo &SInfo = MBBInfoMap[*SuI];
1519 if (SInfo.addPassed(MInfo.vregsPassed))
1520 todo.insert(*SuI);
1521 }
1522 }
1523}
1524
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001525// Calculate the set of virtual registers that must be passed through each basic
1526// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001527// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001528void MachineVerifier::calcRegsRequired() {
1529 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001530 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001531 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001532 BBInfo &MInfo = MBBInfoMap[&MBB];
1533 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1534 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1535 BBInfo &PInfo = MBBInfoMap[*PrI];
1536 if (PInfo.addRequired(MInfo.vregsLiveIn))
1537 todo.insert(*PrI);
1538 }
1539 }
1540
1541 // Iteratively push vregsRequired to predecessors. This will converge to the
1542 // same final state regardless of DenseSet iteration order.
1543 while (!todo.empty()) {
1544 const MachineBasicBlock *MBB = *todo.begin();
1545 todo.erase(MBB);
1546 BBInfo &MInfo = MBBInfoMap[MBB];
1547 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1548 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1549 if (*PrI == MBB)
1550 continue;
1551 BBInfo &SInfo = MBBInfoMap[*PrI];
1552 if (SInfo.addRequired(MInfo.vregsRequired))
1553 todo.insert(*PrI);
1554 }
1555 }
1556}
1557
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001558// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001559// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001560void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001561 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001562 for (const auto &BBI : *MBB) {
1563 if (!BBI.isPHI())
1564 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001565 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001566
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001567 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1568 unsigned Reg = BBI.getOperand(i).getReg();
1569 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001570 if (!Pre->isSuccessor(MBB))
1571 continue;
1572 seen.insert(Pre);
1573 BBInfo &PrInfo = MBBInfoMap[Pre];
1574 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1575 report("PHI operand is not live-out from predecessor",
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001576 &BBI.getOperand(i), i);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001577 }
1578
1579 // Did we see all predecessors?
1580 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1581 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1582 if (!seen.count(*PrI)) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001583 report("Missing PHI operand", &BBI);
Owen Anderson21b17882015-02-04 00:02:59 +00001584 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001585 << " is a predecessor according to the CFG.\n";
1586 }
1587 }
1588 }
1589}
1590
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001591void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001592 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001593
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001594 for (const auto &MBB : *MF) {
1595 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001596
1597 // Skip unreachable MBBs.
1598 if (!MInfo.reachable)
1599 continue;
1600
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001601 checkPHIOps(&MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001602 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001603
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001604 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001605 calcRegsRequired();
1606
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001607 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001608 for (const auto &MBB : *MF) {
1609 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001610 for (RegSet::iterator
1611 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1612 ++I)
1613 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001614 report("Virtual register killed in block, but needed live out.", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001615 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001616 << " is used after the block.\n";
1617 }
1618 }
1619
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001620 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001621 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1622 for (RegSet::iterator
1623 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Matthias Braun30668dd2016-05-11 21:31:39 +00001624 ++I) {
1625 report("Virtual register defs don't dominate all uses.", MF);
1626 report_context_vreg(*I);
1627 }
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001628 }
1629
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001630 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001631 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001632 if (LiveInts)
1633 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001634}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001635
1636void MachineVerifier::verifyLiveVariables() {
1637 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001638 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1639 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001640 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001641 for (const auto &MBB : *MF) {
1642 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001643
1644 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1645 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001646 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1647 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001648 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001649 << " must be live through the block.\n";
1650 }
1651 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001652 if (VI.AliveBlocks.test(MBB.getNumber())) {
1653 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001654 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001655 << " is not needed live through the block.\n";
1656 }
1657 }
1658 }
1659 }
1660}
1661
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001662void MachineVerifier::verifyLiveIntervals() {
1663 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001664 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1665 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001666
1667 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001668 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001669 continue;
1670
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001671 if (!LiveInts->hasInterval(Reg)) {
1672 report("Missing live interval for virtual register", MF);
Owen Anderson21b17882015-02-04 00:02:59 +00001673 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001674 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001675 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001676
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001677 const LiveInterval &LI = LiveInts->getInterval(Reg);
1678 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001679 verifyLiveInterval(LI);
1680 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001681
1682 // Verify all the cached regunit intervals.
1683 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001684 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1685 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001686}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001687
Matthias Braun364e6e92013-10-10 21:28:54 +00001688void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001689 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001690 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001691 if (VNI->isUnused())
1692 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001693
Matthias Braun364e6e92013-10-10 21:28:54 +00001694 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001695
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001696 if (!DefVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001697 report("Value not live at VNInfo def and not marked unused", MF);
1698 report_context(LR, Reg, LaneMask);
1699 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001700 return;
1701 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001702
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001703 if (DefVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001704 report("Live segment at def has different VNInfo", MF);
1705 report_context(LR, Reg, LaneMask);
1706 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001707 return;
1708 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001709
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001710 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1711 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001712 report("Invalid VNInfo definition index", MF);
1713 report_context(LR, Reg, LaneMask);
1714 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001715 return;
1716 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001717
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001718 if (VNI->isPHIDef()) {
1719 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001720 report("PHIDef VNInfo is not defined at MBB start", MBB);
1721 report_context(LR, Reg, LaneMask);
1722 report_context(*VNI);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001723 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001724 return;
1725 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001726
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001727 // Non-PHI def.
1728 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1729 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001730 report("No instruction at VNInfo def index", MBB);
1731 report_context(LR, Reg, LaneMask);
1732 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001733 return;
1734 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001735
Matthias Braun364e6e92013-10-10 21:28:54 +00001736 if (Reg != 0) {
1737 bool hasDef = false;
1738 bool isEarlyClobber = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001739 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001740 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001741 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001742 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1743 if (MOI->getReg() != Reg)
1744 continue;
1745 } else {
1746 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1747 !TRI->hasRegUnit(MOI->getReg(), Reg))
1748 continue;
1749 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001750 if (LaneMask.any() &&
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001751 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask).none())
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001752 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001753 hasDef = true;
1754 if (MOI->isEarlyClobber())
1755 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001756 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001757
Matthias Braun364e6e92013-10-10 21:28:54 +00001758 if (!hasDef) {
1759 report("Defining instruction does not modify register", MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001760 report_context(LR, Reg, LaneMask);
1761 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001762 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001763
Matthias Braun364e6e92013-10-10 21:28:54 +00001764 // Early clobber defs begin at USE slots, but other defs must begin at
1765 // DEF slots.
1766 if (isEarlyClobber) {
1767 if (!VNI->def.isEarlyClobber()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001768 report("Early clobber def must be at an early-clobber slot", MBB);
1769 report_context(LR, Reg, LaneMask);
1770 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001771 }
1772 } else if (!VNI->def.isRegister()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001773 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
1774 report_context(LR, Reg, LaneMask);
1775 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001776 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001777 }
1778}
1779
Matthias Braun364e6e92013-10-10 21:28:54 +00001780void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1781 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001782 unsigned Reg, LaneBitmask LaneMask)
1783{
Matthias Braun364e6e92013-10-10 21:28:54 +00001784 const LiveRange::Segment &S = *I;
1785 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001786 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001787
Matthias Braun364e6e92013-10-10 21:28:54 +00001788 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001789 report("Foreign valno in live segment", MF);
1790 report_context(LR, Reg, LaneMask);
1791 report_context(S);
1792 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001793 }
1794
1795 if (VNI->isUnused()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001796 report("Live segment valno is marked unused", MF);
1797 report_context(LR, Reg, LaneMask);
1798 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001799 }
1800
Matthias Braun364e6e92013-10-10 21:28:54 +00001801 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001802 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001803 report("Bad start of live segment, no basic block", MF);
1804 report_context(LR, Reg, LaneMask);
1805 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001806 return;
1807 }
1808 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001809 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001810 report("Live segment must begin at MBB entry or valno def", MBB);
1811 report_context(LR, Reg, LaneMask);
1812 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001813 }
1814
1815 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001816 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001817 if (!EndMBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001818 report("Bad end of live segment, no basic block", MF);
1819 report_context(LR, Reg, LaneMask);
1820 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001821 return;
1822 }
1823
1824 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001825 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001826 return;
1827
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001828 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001829 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1830 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001831 return;
1832
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001833 // The live segment is ending inside EndMBB
1834 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001835 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001836 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001837 report("Live segment doesn't end at a valid instruction", EndMBB);
1838 report_context(LR, Reg, LaneMask);
1839 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001840 return;
1841 }
1842
1843 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001844 if (S.end.isBlock()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001845 report("Live segment ends at B slot of an instruction", EndMBB);
1846 report_context(LR, Reg, LaneMask);
1847 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001848 }
1849
Matthias Braun364e6e92013-10-10 21:28:54 +00001850 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001851 // Segment ends on the dead slot.
1852 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001853 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001854 report("Live segment ending at dead slot spans instructions", EndMBB);
1855 report_context(LR, Reg, LaneMask);
1856 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001857 }
1858 }
1859
1860 // A live segment can only end at an early-clobber slot if it is being
1861 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001862 if (S.end.isEarlyClobber()) {
1863 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001864 report("Live segment ending at early clobber slot must be "
Matthias Braun7e624d52015-11-09 23:59:33 +00001865 "redefined by an EC def in the same instruction", EndMBB);
1866 report_context(LR, Reg, LaneMask);
1867 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001868 }
1869 }
1870
1871 // The following checks only apply to virtual registers. Physreg liveness
1872 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001873 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001874 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001875 // use, or a dead flag on a def.
1876 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001877 bool hasSubRegDef = false;
Matthias Braun72a58c32016-03-29 19:07:43 +00001878 bool hasDeadDef = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001879 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001880 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001881 continue;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001882 unsigned Sub = MOI->getSubReg();
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001883 LaneBitmask SLM = Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub)
1884 : LaneBitmask::getAll();
Matthias Braun72a58c32016-03-29 19:07:43 +00001885 if (MOI->isDef()) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001886 if (Sub != 0) {
Matthias Braun72a58c32016-03-29 19:07:43 +00001887 hasSubRegDef = true;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001888 // An operand vreg0:sub0<def> reads vreg0:sub1..n. Invert the lane
1889 // mask for subregister defs. Read-undef defs will be handled by
1890 // readsReg below.
Krzysztof Parzyszek0a955d62016-08-29 13:15:35 +00001891 SLM = ~SLM;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001892 }
Matthias Braun72a58c32016-03-29 19:07:43 +00001893 if (MOI->isDead())
1894 hasDeadDef = true;
1895 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001896 if (LaneMask.any() && (LaneMask & SLM).none())
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001897 continue;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001898 if (MOI->readsReg())
1899 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001900 }
Matthias Braun72a58c32016-03-29 19:07:43 +00001901 if (S.end.isDead()) {
1902 // Make sure that the corresponding machine operand for a "dead" live
1903 // range has the dead flag. We cannot perform this check for subregister
1904 // liveranges as partially dead values are allowed.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001905 if (LaneMask.none() && !hasDeadDef) {
Matthias Braun72a58c32016-03-29 19:07:43 +00001906 report("Instruction ending live segment on dead slot has no dead flag",
1907 MI);
1908 report_context(LR, Reg, LaneMask);
1909 report_context(S);
1910 }
1911 } else {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001912 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001913 // When tracking subregister liveness, the main range must start new
1914 // values on partial register writes, even if there is no read.
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001915 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask.any() ||
Matthias Brauna25e13a2015-03-19 00:21:58 +00001916 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001917 report("Instruction ending live segment doesn't read the register",
1918 MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001919 report_context(LR, Reg, LaneMask);
1920 report_context(S);
Matthias Braun21554d92014-12-10 01:13:11 +00001921 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001922 }
1923 }
1924 }
1925
1926 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001927 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001928 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00001929 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001930 // Not live-in to any blocks.
1931 if (MBB == EndMBB)
1932 return;
1933 // Skip this block.
1934 ++MFI;
1935 }
1936 for (;;) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001937 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001938 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00001939 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00001940 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001941 if (&*MFI == EndMBB)
1942 break;
1943 ++MFI;
1944 continue;
1945 }
1946
1947 // Is VNI a PHI-def in the current block?
1948 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001949 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001950
1951 // Check that VNI is live-out of all predecessors.
1952 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1953 PE = MFI->pred_end(); PI != PE; ++PI) {
1954 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001955 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001956
Matthias Braun1ee25e02017-06-08 21:30:54 +00001957 // All predecessors must have a live-out value. However for a phi
1958 // instruction with subregister intervals
1959 // only one of the subregisters (not necessarily the current one) needs to
1960 // be defined.
1961 if (!PVNI && (LaneMask.none() || !IsPHI) ) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001962 report("Register not marked live out of predecessor", *PI);
1963 report_context(LR, Reg, LaneMask);
1964 report_context(*VNI);
1965 errs() << " live into BB#" << MFI->getNumber()
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001966 << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
1967 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001968 continue;
1969 }
1970
1971 // Only PHI-defs can take different predecessor values.
1972 if (!IsPHI && PVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001973 report("Different value live out of predecessor", *PI);
1974 report_context(LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001975 errs() << "Valno #" << PVNI->id << " live out of BB#"
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001976 << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
1977 << " live into BB#" << MFI->getNumber() << '@'
1978 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001979 }
1980 }
1981 if (&*MFI == EndMBB)
1982 break;
1983 ++MFI;
1984 }
1985}
1986
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001987void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001988 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00001989 for (const VNInfo *VNI : LR.valnos)
1990 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001991
Matthias Braun364e6e92013-10-10 21:28:54 +00001992 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001993 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00001994}
1995
1996void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001997 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00001998 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1999 verifyLiveRange(LI, Reg);
2000
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00002001 LaneBitmask Mask;
Matthias Braune6a24852015-09-25 21:51:14 +00002002 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00002003 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00002004 if ((Mask & SR.LaneMask).any()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002005 report("Lane masks of sub ranges overlap in live interval", MF);
2006 report_context(LI);
2007 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00002008 if ((SR.LaneMask & ~MaxMask).any()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002009 report("Subrange lanemask is invalid", MF);
2010 report_context(LI);
2011 }
2012 if (SR.empty()) {
2013 report("Subrange must not be empty", MF);
2014 report_context(SR, LI.reg, SR.LaneMask);
2015 }
Matthias Braune962e522015-03-25 21:18:22 +00002016 Mask |= SR.LaneMask;
2017 verifyLiveRange(SR, LI.reg, SR.LaneMask);
Matthias Braun7e624d52015-11-09 23:59:33 +00002018 if (!LI.covers(SR)) {
2019 report("A Subrange is not covered by the main range", MF);
2020 report_context(LI);
2021 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002022 }
2023
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002024 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00002025 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
Matthias Braunbf47f632016-01-08 01:16:35 +00002026 unsigned NumComp = ConEQ.Classify(LI);
Matthias Braune962e522015-03-25 21:18:22 +00002027 if (NumComp > 1) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002028 report("Multiple connected components in live interval", MF);
2029 report_context(LI);
Matthias Braune962e522015-03-25 21:18:22 +00002030 for (unsigned comp = 0; comp != NumComp; ++comp) {
2031 errs() << comp << ": valnos";
2032 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
2033 E = LI.vni_end(); I!=E; ++I)
2034 if (comp == ConEQ.getEqClass(*I))
2035 errs() << ' ' << (*I)->id;
2036 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00002037 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002038 }
2039}
Manman Renaa6875b2013-07-15 21:26:31 +00002040
2041namespace {
2042 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
2043 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
2044 // value is zero.
2045 // We use a bool plus an integer to capture the stack state.
2046 struct StackStateOfBB {
2047 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
2048 ExitIsSetup(false) { }
2049 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
2050 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
2051 ExitIsSetup(ExitSetup) { }
2052 // Can be negative, which means we are setting up a frame.
2053 int EntryValue;
2054 int ExitValue;
2055 bool EntryIsSetup;
2056 bool ExitIsSetup;
2057 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002058}
Manman Renaa6875b2013-07-15 21:26:31 +00002059
2060/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
2061/// by a FrameDestroy <n>, stack adjustments are identical on all
2062/// CFG edges to a merge point, and frame is destroyed at end of a return block.
2063void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00002064 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
2065 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Serge Pavlov802aa662017-04-20 01:34:04 +00002066 if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
2067 return;
Manman Renaa6875b2013-07-15 21:26:31 +00002068
2069 SmallVector<StackStateOfBB, 8> SPState;
2070 SPState.resize(MF->getNumBlockIDs());
David Callahanc1051ab2016-10-05 21:36:16 +00002071 df_iterator_default_set<const MachineBasicBlock*> Reachable;
Manman Renaa6875b2013-07-15 21:26:31 +00002072
2073 // Visit the MBBs in DFS order.
2074 for (df_ext_iterator<const MachineFunction*,
David Callahanc1051ab2016-10-05 21:36:16 +00002075 df_iterator_default_set<const MachineBasicBlock*> >
Manman Renaa6875b2013-07-15 21:26:31 +00002076 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
2077 DFI != DFE; ++DFI) {
2078 const MachineBasicBlock *MBB = *DFI;
2079
2080 StackStateOfBB BBState;
2081 // Check the exit state of the DFS stack predecessor.
2082 if (DFI.getPathLength() >= 2) {
2083 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
2084 assert(Reachable.count(StackPred) &&
2085 "DFS stack predecessor is already visited.\n");
2086 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
2087 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
2088 BBState.ExitValue = BBState.EntryValue;
2089 BBState.ExitIsSetup = BBState.EntryIsSetup;
2090 }
2091
2092 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002093 for (const auto &I : *MBB) {
2094 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00002095 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002096 report("FrameSetup is after another FrameSetup", &I);
Serge Pavlovd526b132017-05-09 13:35:13 +00002097 BBState.ExitValue -= TII->getFrameTotalSize(I);
Manman Renaa6875b2013-07-15 21:26:31 +00002098 BBState.ExitIsSetup = true;
2099 }
2100
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002101 if (I.getOpcode() == FrameDestroyOpcode) {
Serge Pavlovd526b132017-05-09 13:35:13 +00002102 int Size = TII->getFrameTotalSize(I);
Manman Renaa6875b2013-07-15 21:26:31 +00002103 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002104 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00002105 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
2106 BBState.ExitValue;
2107 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002108 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00002109 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00002110 << AbsSPAdj << ">.\n";
2111 }
2112 BBState.ExitValue += Size;
2113 BBState.ExitIsSetup = false;
2114 }
2115 }
2116 SPState[MBB->getNumber()] = BBState;
2117
2118 // Make sure the exit state of any predecessor is consistent with the entry
2119 // state.
2120 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
2121 E = MBB->pred_end(); I != E; ++I) {
2122 if (Reachable.count(*I) &&
2123 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
2124 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
2125 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002126 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002127 << SPState[(*I)->getNumber()].ExitValue << ", "
2128 << SPState[(*I)->getNumber()].ExitIsSetup
2129 << "), while BB#" << MBB->getNumber() << " has entry state ("
2130 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
2131 }
2132 }
2133
2134 // Make sure the entry state of any successor is consistent with the exit
2135 // state.
2136 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
2137 E = MBB->succ_end(); I != E; ++I) {
2138 if (Reachable.count(*I) &&
2139 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
2140 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
2141 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002142 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002143 << SPState[(*I)->getNumber()].EntryValue << ", "
2144 << SPState[(*I)->getNumber()].EntryIsSetup
2145 << "), while BB#" << MBB->getNumber() << " has exit state ("
2146 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
2147 }
2148 }
2149
2150 // Make sure a basic block with return ends with zero stack adjustment.
2151 if (!MBB->empty() && MBB->back().isReturn()) {
2152 if (BBState.ExitIsSetup)
2153 report("A return block ends with a FrameSetup.", MBB);
2154 if (BBState.ExitValue)
2155 report("A return block ends with a nonzero stack adjustment.", MBB);
2156 }
2157 }
2158}