blob: 265f93c363ca1f073659e67e06e5ea04b0b5443b [file] [log] [blame]
Bill Wendling68caaaf2010-08-19 18:52:17 +00001//===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Pass to verify generated machine code. The following is checked:
11//
12// Operand counts: All explicit operands must be present.
13//
14// Register classes: All physical and virtual register operands must be
15// compatible with the register class required by the instruction descriptor.
16//
17// Register live intervals: Registers must be defined only once, and must be
18// defined before use.
19//
20// The machine code verifier is enabled from LLVMTargetMachine.cpp with the
21// command-line option -verify-machineinstrs, or by defining the environment
22// variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
23// the verifier errors.
24//===----------------------------------------------------------------------===//
25
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000026#include "llvm/CodeGen/Passes.h"
Chris Lattner565449d2009-08-23 03:13:20 +000027#include "llvm/ADT/DenseSet.h"
Manman Renaa6875b2013-07-15 21:26:31 +000028#include "llvm/ADT/DepthFirstIterator.h"
Chris Lattner565449d2009-08-23 03:13:20 +000029#include "llvm/ADT/SetOperations.h"
30#include "llvm/ADT/SmallVector.h"
David Majnemer70497c62015-12-02 23:06:39 +000031#include "llvm/Analysis/EHPersonalities.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/CodeGen/LiveIntervalAnalysis.h"
33#include "llvm/CodeGen/LiveStackAnalysis.h"
34#include "llvm/CodeGen/LiveVariables.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/CodeGen/MachineMemOperand.h"
38#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/InlineAsm.h"
41#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000043#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000044#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000045#include "llvm/Support/FileSystem.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000046#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Target/TargetInstrInfo.h"
48#include "llvm/Target/TargetMachine.h"
49#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000050#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000051using namespace llvm;
52
53namespace {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000054 struct MachineVerifier {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000055
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000056 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000057 PASS(pass),
Owen Anderson21b17882015-02-04 00:02:59 +000058 Banner(b)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000059 {}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000060
Matthias Braunb3aefc32016-02-15 19:25:31 +000061 unsigned verify(MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000062
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000063 Pass *const PASS;
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000064 const char *Banner;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000065 const MachineFunction *MF;
66 const TargetMachine *TM;
Evan Cheng8d71a752011-06-27 21:26:13 +000067 const TargetInstrInfo *TII;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000068 const TargetRegisterInfo *TRI;
69 const MachineRegisterInfo *MRI;
70
71 unsigned foundErrors;
72
Ahmed Bougacha3681c772016-08-02 16:17:15 +000073 // Avoid querying the MachineFunctionProperties for each operand.
74 bool isFunctionRegBankSelected;
Ahmed Bougachab14e9442016-08-02 16:49:22 +000075 bool isFunctionSelected;
Ahmed Bougacha3681c772016-08-02 16:17:15 +000076
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000077 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000078 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000079 typedef DenseSet<unsigned> RegSet;
80 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000081 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000082
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000083 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000084 BlockSet FunctionBlocks;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000085
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000086 BitVector regsReserved;
87 RegSet regsLive;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000088 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000089 RegMaskVector regMasks;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000090
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +000091 SlotIndex lastIndex;
92
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000093 // Add Reg and any sub-registers to RV
94 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
95 RV.push_back(Reg);
96 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +000097 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
98 RV.push_back(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000099 }
100
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000101 struct BBInfo {
102 // Is this MBB reachable from the MF entry point?
103 bool reachable;
104
105 // Vregs that must be live in because they are used without being
106 // defined. Map value is the user.
107 RegMap vregsLiveIn;
108
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000109 // Regs killed in MBB. They may be defined again, and will then be in both
110 // regsKilled and regsLiveOut.
111 RegSet regsKilled;
112
113 // Regs defined in MBB and live out. Note that vregs passing through may
114 // be live out without being mentioned here.
115 RegSet regsLiveOut;
116
117 // Vregs that pass through MBB untouched. This set is disjoint from
118 // regsKilled and regsLiveOut.
119 RegSet vregsPassed;
120
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000121 // Vregs that must pass through MBB because they are needed by a successor
122 // block. This set is disjoint from regsLiveOut.
123 RegSet vregsRequired;
124
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000125 // Set versions of block's predecessor and successor lists.
126 BlockSet Preds, Succs;
127
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000128 BBInfo() : reachable(false) {}
129
130 // Add register to vregsPassed if it belongs there. Return true if
131 // anything changed.
132 bool addPassed(unsigned Reg) {
133 if (!TargetRegisterInfo::isVirtualRegister(Reg))
134 return false;
135 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
136 return false;
137 return vregsPassed.insert(Reg).second;
138 }
139
140 // Same for a full set.
141 bool addPassed(const RegSet &RS) {
142 bool changed = false;
143 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
144 if (addPassed(*I))
145 changed = true;
146 return changed;
147 }
148
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000149 // Add register to vregsRequired if it belongs there. Return true if
150 // anything changed.
151 bool addRequired(unsigned Reg) {
152 if (!TargetRegisterInfo::isVirtualRegister(Reg))
153 return false;
154 if (regsLiveOut.count(Reg))
155 return false;
156 return vregsRequired.insert(Reg).second;
157 }
158
159 // Same for a full set.
160 bool addRequired(const RegSet &RS) {
161 bool changed = false;
162 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
163 if (addRequired(*I))
164 changed = true;
165 return changed;
166 }
167
168 // Same for a full map.
169 bool addRequired(const RegMap &RM) {
170 bool changed = false;
171 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
172 if (addRequired(I->first))
173 changed = true;
174 return changed;
175 }
176
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000177 // Live-out registers are either in regsLiveOut or vregsPassed.
178 bool isLiveOut(unsigned Reg) const {
179 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
180 }
181 };
182
183 // Extra register info per MBB.
184 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
185
186 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000187 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000188 }
189
Matthias Braun4682ac62017-05-05 22:04:05 +0000190 bool isAllocatable(unsigned Reg) const {
191 return Reg < TRI->getNumRegs() && TRI->isInAllocatableClass(Reg) &&
192 !regsReserved.test(Reg);
Lang Hames1ce837a2012-02-14 19:17:48 +0000193 }
194
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000195 // Analysis information if available
196 LiveVariables *LiveVars;
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +0000197 LiveIntervals *LiveInts;
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000198 LiveStacks *LiveStks;
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000199 SlotIndexes *Indexes;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000200
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000201 void visitMachineFunctionBefore();
202 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000203 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000204 void visitMachineInstrBefore(const MachineInstr *MI);
205 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
206 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000207 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000208 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
209 void visitMachineFunctionAfter();
210
211 void report(const char *msg, const MachineFunction *MF);
212 void report(const char *msg, const MachineBasicBlock *MBB);
213 void report(const char *msg, const MachineInstr *MI);
214 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Matthias Braun7e624d52015-11-09 23:59:33 +0000215
216 void report_context(const LiveInterval &LI) const;
Matt Arsenault892fcd02016-07-25 19:39:01 +0000217 void report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000218 LaneBitmask LaneMask) const;
219 void report_context(const LiveRange::Segment &S) const;
220 void report_context(const VNInfo &VNI) const;
Matthias Braun579c9cd2016-02-02 02:44:25 +0000221 void report_context(SlotIndex Pos) const;
222 void report_context_liverange(const LiveRange &LR) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000223 void report_context_lanemask(LaneBitmask LaneMask) const;
Matthias Braun30668dd2016-05-11 21:31:39 +0000224 void report_context_vreg(unsigned VReg) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000225 void report_context_vreg_regunit(unsigned VRegOrRegUnit) const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000226
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000227 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000228
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000229 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Matthias Braun1377fd62016-02-02 20:04:51 +0000230 void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
231 SlotIndex UseIdx, const LiveRange &LR, unsigned Reg,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000232 LaneBitmask LaneMask = LaneBitmask::getNone());
Matthias Braun1377fd62016-02-02 20:04:51 +0000233 void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
234 SlotIndex DefIdx, const LiveRange &LR, unsigned Reg,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000235 LaneBitmask LaneMask = LaneBitmask::getNone());
Matthias Braun1377fd62016-02-02 20:04:51 +0000236
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000237 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000238 void calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000239 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000240
241 void calcRegsRequired();
242 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000243 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000244 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000245 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000246 LaneBitmask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000247 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000248 const LiveRange::const_iterator I, unsigned,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000249 LaneBitmask);
250 void verifyLiveRange(const LiveRange&, unsigned,
251 LaneBitmask LaneMask = LaneBitmask::getNone());
Manman Renaa6875b2013-07-15 21:26:31 +0000252
253 void verifyStackFrame();
Matthias Braun80595462015-09-09 17:49:46 +0000254
255 void verifySlotIndexes() const;
Derek Schuff42666ee2016-03-29 17:40:22 +0000256 void verifyProperties(const MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000257 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000258
259 struct MachineVerifierPass : public MachineFunctionPass {
260 static char ID; // Pass ID, replacement for typeid
Matthias Brauna4e932d2014-12-11 19:41:51 +0000261 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000262
Sven van Haastregt04bfa872017-03-29 15:25:06 +0000263 MachineVerifierPass(std::string banner = std::string())
Sven van Haastregt039a6d92017-03-29 09:08:25 +0000264 : MachineFunctionPass(ID), Banner(std::move(banner)) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000265 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
266 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000267
Craig Topper4584cd52014-03-07 09:26:03 +0000268 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000269 AU.setPreservesAll();
270 MachineFunctionPass::getAnalysisUsage(AU);
271 }
272
Craig Topper4584cd52014-03-07 09:26:03 +0000273 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Braunb3aefc32016-02-15 19:25:31 +0000274 unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
275 if (FoundErrors)
276 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000277 return false;
278 }
279 };
280
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000281}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000282
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000283char MachineVerifierPass::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +0000284INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000285 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000286
Matthias Brauna4e932d2014-12-11 19:41:51 +0000287FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000288 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000289}
290
Matthias Braunb3aefc32016-02-15 19:25:31 +0000291bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
292 const {
293 MachineFunction &MF = const_cast<MachineFunction&>(*this);
294 unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
295 if (AbortOnErrors && FoundErrors)
296 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
297 return FoundErrors == 0;
Jakob Stoklund Olesen27440e72009-11-13 21:56:09 +0000298}
299
Matthias Braun80595462015-09-09 17:49:46 +0000300void MachineVerifier::verifySlotIndexes() const {
301 if (Indexes == nullptr)
302 return;
303
304 // Ensure the IdxMBB list is sorted by slot indexes.
305 SlotIndex Last;
306 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
307 E = Indexes->MBBIndexEnd(); I != E; ++I) {
308 assert(!Last.isValid() || I->first > Last);
309 Last = I->first;
310 }
311}
312
Derek Schuff42666ee2016-03-29 17:40:22 +0000313void MachineVerifier::verifyProperties(const MachineFunction &MF) {
314 // If a pass has introduced virtual registers without clearing the
Matthias Braun1eb47362016-08-25 01:27:13 +0000315 // NoVRegs property (or set it without allocating the vregs)
Derek Schuff42666ee2016-03-29 17:40:22 +0000316 // then report an error.
317 if (MF.getProperties().hasProperty(
Matthias Braun1eb47362016-08-25 01:27:13 +0000318 MachineFunctionProperties::Property::NoVRegs) &&
319 MRI->getNumVirtRegs())
320 report("Function has NoVRegs property but there are VReg operands", &MF);
Derek Schuff42666ee2016-03-29 17:40:22 +0000321}
322
Matthias Braunb3aefc32016-02-15 19:25:31 +0000323unsigned MachineVerifier::verify(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000324 foundErrors = 0;
325
326 this->MF = &MF;
327 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000328 TII = MF.getSubtarget().getInstrInfo();
329 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000330 MRI = &MF.getRegInfo();
331
Ahmed Bougacha3681c772016-08-02 16:17:15 +0000332 isFunctionRegBankSelected = MF.getProperties().hasProperty(
333 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab14e9442016-08-02 16:49:22 +0000334 isFunctionSelected = MF.getProperties().hasProperty(
335 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha3681c772016-08-02 16:17:15 +0000336
Craig Topperc0196b12014-04-14 00:51:57 +0000337 LiveVars = nullptr;
338 LiveInts = nullptr;
339 LiveStks = nullptr;
340 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000341 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000342 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000343 // We don't want to verify LiveVariables if LiveIntervals is available.
344 if (!LiveInts)
345 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000346 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000347 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000348 }
349
Matthias Braun80595462015-09-09 17:49:46 +0000350 verifySlotIndexes();
351
Derek Schuff42666ee2016-03-29 17:40:22 +0000352 verifyProperties(MF);
353
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000354 visitMachineFunctionBefore();
355 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
356 MFI!=MFE; ++MFI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000357 visitMachineBasicBlockBefore(&*MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000358 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000359 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000360 // Do we expect the next instruction to be part of the same bundle?
361 bool InBundle = false;
362
Evan Cheng7fae11b2011-12-14 02:11:42 +0000363 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
364 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000365 if (MBBI->getParent() != &*MFI) {
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +0000366 report("Bad instruction parent pointer", &*MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000367 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000368 continue;
369 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000370
371 // Check for consistent bundle flags.
372 if (InBundle && !MBBI->isBundledWithPred())
373 report("Missing BundledPred flag, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000374 "BundledSucc was set on predecessor",
375 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000376 if (!InBundle && MBBI->isBundledWithPred())
377 report("BundledPred flag is set, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000378 "but BundledSucc not set on predecessor",
379 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000380
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000381 // Is this a bundle header?
382 if (!MBBI->isInsideBundle()) {
383 if (CurBundle)
384 visitMachineBundleAfter(CurBundle);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000385 CurBundle = &*MBBI;
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000386 visitMachineBundleBefore(CurBundle);
387 } else if (!CurBundle)
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +0000388 report("No bundle header", &*MBBI);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000389 visitMachineInstrBefore(&*MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000390 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
391 const MachineInstr &MI = *MBBI;
392 const MachineOperand &Op = MI.getOperand(I);
393 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000394 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000395 // functions when replacing operands of a MachineInstr.
396 report("Instruction has operand with wrong parent set", &MI);
397 }
398
399 visitMachineOperand(&Op, I);
400 }
401
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000402 visitMachineInstrAfter(&*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000403
404 // Was this the last bundled instruction?
405 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000406 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000407 if (CurBundle)
408 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000409 if (InBundle)
410 report("BundledSucc flag set on last instruction in block", &MFI->back());
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000411 visitMachineBasicBlockAfter(&*MFI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000412 }
413 visitMachineFunctionAfter();
414
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000415 // Clean up.
416 regsLive.clear();
417 regsDefined.clear();
418 regsDead.clear();
419 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000420 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000421 MBBInfoMap.clear();
422
Matthias Braunb3aefc32016-02-15 19:25:31 +0000423 return foundErrors;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000424}
425
Chris Lattner75f40452009-08-23 01:03:30 +0000426void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000427 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000428 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000429 if (!foundErrors++) {
430 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000431 errs() << "# " << Banner << '\n';
Matthias Braun42b4b632015-11-09 23:59:23 +0000432 if (LiveInts != nullptr)
433 LiveInts->print(errs());
434 else
435 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000436 }
Owen Anderson21b17882015-02-04 00:02:59 +0000437 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000438 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000439}
440
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000441void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000442 assert(MBB);
443 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000444 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000445 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000446 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000447 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000448 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000449 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000450 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000451}
452
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000453void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000454 assert(MI);
455 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000456 errs() << "- instruction: ";
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000457 if (Indexes && Indexes->hasIndex(*MI))
458 errs() << Indexes->getInstructionIndex(*MI) << '\t';
Matthias Braun45718db2015-11-09 23:59:25 +0000459 MI->print(errs(), /*SkipOpers=*/true);
Matthias Braun716b4332015-11-09 23:59:29 +0000460 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000461}
462
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000463void MachineVerifier::report(const char *msg,
464 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000465 assert(MO);
466 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000467 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000468 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000469 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000470}
471
Matthias Braun579c9cd2016-02-02 02:44:25 +0000472void MachineVerifier::report_context(SlotIndex Pos) const {
473 errs() << "- at: " << Pos << '\n';
474}
475
Matthias Braun7e624d52015-11-09 23:59:33 +0000476void MachineVerifier::report_context(const LiveInterval &LI) const {
Owen Anderson21b17882015-02-04 00:02:59 +0000477 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000478}
479
Matt Arsenault892fcd02016-07-25 19:39:01 +0000480void MachineVerifier::report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000481 LaneBitmask LaneMask) const {
Matthias Braun579c9cd2016-02-02 02:44:25 +0000482 report_context_liverange(LR);
Matt Arsenault892fcd02016-07-25 19:39:01 +0000483 report_context_vreg_regunit(VRegUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000484 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +0000485 report_context_lanemask(LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000486}
487
Matthias Braun7e624d52015-11-09 23:59:33 +0000488void MachineVerifier::report_context(const LiveRange::Segment &S) const {
489 errs() << "- segment: " << S << '\n';
490}
491
492void MachineVerifier::report_context(const VNInfo &VNI) const {
493 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
Matthias Braun364e6e92013-10-10 21:28:54 +0000494}
495
Matthias Braun579c9cd2016-02-02 02:44:25 +0000496void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
497 errs() << "- liverange: " << LR << '\n';
498}
499
Matthias Braun30668dd2016-05-11 21:31:39 +0000500void MachineVerifier::report_context_vreg(unsigned VReg) const {
501 errs() << "- v. register: " << PrintReg(VReg, TRI) << '\n';
502}
503
Matthias Braun1377fd62016-02-02 20:04:51 +0000504void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
505 if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
Matthias Braun30668dd2016-05-11 21:31:39 +0000506 report_context_vreg(VRegOrUnit);
Matthias Braun1377fd62016-02-02 20:04:51 +0000507 } else {
508 errs() << "- regunit: " << PrintRegUnit(VRegOrUnit, TRI) << '\n';
509 }
510}
511
512void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
513 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
514}
515
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000516void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000517 BBInfo &MInfo = MBBInfoMap[MBB];
518 if (!MInfo.reachable) {
519 MInfo.reachable = true;
520 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
521 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
522 markReachable(*SuI);
523 }
524}
525
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000526void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000527 lastIndex = SlotIndex();
Matthias Braun4682ac62017-05-05 22:04:05 +0000528 regsReserved = MRI->reservedRegsFrozen() ? MRI->getReservedRegs()
529 : TRI->getReservedRegs(*MF);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000530
Justin Bogner20dd36a2017-04-11 19:32:41 +0000531 if (!MF->empty())
532 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000533
534 // Build a set of the basic blocks in the function.
535 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000536 for (const auto &MBB : *MF) {
537 FunctionBlocks.insert(&MBB);
538 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000539
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000540 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
541 if (MInfo.Preds.size() != MBB.pred_size())
542 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000543
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000544 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
545 if (MInfo.Succs.size() != MBB.succ_size())
546 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000547 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000548
549 // Check that the register use lists are sane.
550 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000551
Justin Bogner20dd36a2017-04-11 19:32:41 +0000552 if (!MF->empty())
553 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000554}
555
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000556// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000557static bool matchPair(MachineBasicBlock::const_succ_iterator i,
558 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000559 if (*i == a)
560 return *++i == b;
561 if (*i == b)
562 return *++i == a;
563 return false;
564}
565
566void
567MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000568 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000569
Matthias Braun79f85b32016-08-24 01:32:41 +0000570 if (!MF->getProperties().hasProperty(
Matthias Braun11723322017-01-05 20:01:19 +0000571 MachineFunctionProperties::Property::NoPHIs) && MRI->tracksLiveness()) {
Lang Hames1ce837a2012-02-14 19:17:48 +0000572 // If this block has allocatable physical registers live-in, check that
573 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000574 for (const auto &LI : MBB->liveins()) {
575 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +0000576 MBB->getIterator() != MBB->getParent()->begin()) {
Matt Arsenault900b21c2017-02-15 22:19:06 +0000577 report("MBB has allocatable live-in, but isn't entry or landing-pad.", MBB);
Lang Hames1ce837a2012-02-14 19:17:48 +0000578 }
579 }
580 }
581
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000582 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000583 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000584 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000585 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000586 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000587 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000588 if (!FunctionBlocks.count(*I))
589 report("MBB has successor that isn't part of the function.", MBB);
590 if (!MBBInfoMap[*I].Preds.count(MBB)) {
591 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000592 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000593 << (*I)->getNumber() << ".\n";
594 }
595 }
596
597 // Check the predecessor list.
598 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
599 E = MBB->pred_end(); I != E; ++I) {
600 if (!FunctionBlocks.count(*I))
601 report("MBB has predecessor that isn't part of the function.", MBB);
602 if (!MBBInfoMap[*I].Succs.count(MBB)) {
603 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000604 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000605 << (*I)->getNumber() << ".\n";
606 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000607 }
Bill Wendling2a401312011-05-04 22:54:05 +0000608
609 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
610 const BasicBlock *BB = MBB->getBasicBlock();
Reid Kleckner64b003f2015-11-09 21:04:00 +0000611 const Function *Fn = MF->getFunction();
Bill Wendling2a401312011-05-04 22:54:05 +0000612 if (LandingPadSuccs.size() > 1 &&
613 !(AsmInfo &&
614 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
Reid Kleckner64b003f2015-11-09 21:04:00 +0000615 BB && isa<SwitchInst>(BB->getTerminator())) &&
616 !isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000617 report("MBB has more than one landing pad successor", MBB);
618
Dan Gohman352a4952009-08-27 02:43:49 +0000619 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000620 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000621 SmallVector<MachineOperand, 4> Cond;
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000622 if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
623 Cond)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000624 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
625 // check whether its answers match up with reality.
626 if (!TBB && !FBB) {
627 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000628 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000629 ++MBBI;
630 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000631 // It's possible that the block legitimately ends with a noreturn
632 // call or an unreachable, in which case it won't actually fall
633 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000634 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000635 // It's possible that the block legitimately ends with a noreturn
636 // call or an unreachable, in which case it won't actuall fall
637 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000638 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000639 report("MBB exits via unconditional fall-through but doesn't have "
640 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000641 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000642 report("MBB exits via unconditional fall-through but its successor "
643 "differs from its CFG successor!", MBB);
644 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000645 if (!MBB->empty() && MBB->back().isBarrier() &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000646 !TII->isPredicated(MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000647 report("MBB exits via unconditional fall-through but ends with a "
648 "barrier instruction!", MBB);
649 }
650 if (!Cond.empty()) {
651 report("MBB exits via unconditional fall-through but has a condition!",
652 MBB);
653 }
654 } else if (TBB && !FBB && Cond.empty()) {
655 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000656 // If the block has exactly one successor, that happens to be a
657 // landingpad, accept it as valid control flow.
658 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
659 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
660 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000661 report("MBB exits via unconditional branch but doesn't have "
662 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000663 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000664 report("MBB exits via unconditional branch but the CFG "
665 "successor doesn't match the actual successor!", MBB);
666 }
667 if (MBB->empty()) {
668 report("MBB exits via unconditional branch but doesn't contain "
669 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000670 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000671 report("MBB exits via unconditional branch but doesn't end with a "
672 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000673 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000674 report("MBB exits via unconditional branch but the branch isn't a "
675 "terminator instruction!", MBB);
676 }
677 } else if (TBB && !FBB && !Cond.empty()) {
678 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000679 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000680 ++MBBI;
681 if (MBBI == MF->end()) {
682 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000683 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000684 // A conditional branch with only one successor is weird, but allowed.
685 if (&*MBBI != TBB)
686 report("MBB exits via conditional branch/fall-through but only has "
687 "one CFG successor!", MBB);
688 else if (TBB != *MBB->succ_begin())
689 report("MBB exits via conditional branch/fall-through but the CFG "
690 "successor don't match the actual successor!", MBB);
691 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000692 report("MBB exits via conditional branch/fall-through but doesn't have "
693 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000694 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000695 report("MBB exits via conditional branch/fall-through but the CFG "
696 "successors don't match the actual successors!", MBB);
697 }
698 if (MBB->empty()) {
699 report("MBB exits via conditional branch/fall-through but doesn't "
700 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000701 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000702 report("MBB exits via conditional branch/fall-through but ends with a "
703 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000704 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000705 report("MBB exits via conditional branch/fall-through but the branch "
706 "isn't a terminator instruction!", MBB);
707 }
708 } else if (TBB && FBB) {
709 // Block conditionally branches somewhere, otherwise branches
710 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000711 if (MBB->succ_size() == 1) {
712 // A conditional branch with only one successor is weird, but allowed.
713 if (FBB != TBB)
714 report("MBB exits via conditional branch/branch through but only has "
715 "one CFG successor!", MBB);
716 else if (TBB != *MBB->succ_begin())
717 report("MBB exits via conditional branch/branch through but the CFG "
718 "successor don't match the actual successor!", MBB);
719 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000720 report("MBB exits via conditional branch/branch but doesn't have "
721 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000722 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000723 report("MBB exits via conditional branch/branch but the CFG "
724 "successors don't match the actual successors!", MBB);
725 }
726 if (MBB->empty()) {
727 report("MBB exits via conditional branch/branch but doesn't "
728 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000729 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000730 report("MBB exits via conditional branch/branch but doesn't end with a "
731 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000732 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000733 report("MBB exits via conditional branch/branch but the branch "
734 "isn't a terminator instruction!", MBB);
735 }
736 if (Cond.empty()) {
737 report("MBB exits via conditinal branch/branch but there's no "
738 "condition!", MBB);
739 }
740 } else {
741 report("AnalyzeBranch returned invalid data!", MBB);
742 }
743 }
744
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000745 regsLive.clear();
Matthias Braun11723322017-01-05 20:01:19 +0000746 if (MRI->tracksLiveness()) {
747 for (const auto &LI : MBB->liveins()) {
748 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
749 report("MBB live-in list contains non-physical register", MBB);
750 continue;
751 }
752 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
753 SubRegs.isValid(); ++SubRegs)
754 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000755 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000756 }
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000757
Matthias Braun941a7052016-07-28 18:40:00 +0000758 const MachineFrameInfo &MFI = MF->getFrameInfo();
759 BitVector PR = MFI.getPristineRegs(*MF);
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +0000760 for (unsigned I : PR.set_bits()) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000761 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
762 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000763 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000764 }
765
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000766 regsKilled.clear();
767 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000768
769 if (Indexes)
770 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000771}
772
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000773// This function gets called for all bundle headers, including normal
774// stand-alone unbundled instructions.
775void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000776 if (Indexes && Indexes->hasIndex(*MI)) {
777 SlotIndex idx = Indexes->getInstructionIndex(*MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000778 if (!(idx > lastIndex)) {
779 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000780 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000781 }
782 lastIndex = idx;
783 }
Pete Coopercd720162012-06-07 17:41:39 +0000784
785 // Ensure non-terminators don't follow terminators.
786 // Ignore predicated terminators formed by if conversion.
787 // FIXME: If conversion shouldn't need to violate this rule.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000788 if (MI->isTerminator() && !TII->isPredicated(*MI)) {
Pete Coopercd720162012-06-07 17:41:39 +0000789 if (!FirstTerminator)
790 FirstTerminator = MI;
791 } else if (FirstTerminator) {
792 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000793 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000794 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000795}
796
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000797// The operands on an INLINEASM instruction must follow a template.
798// Verify that the flag operands make sense.
799void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
800 // The first two operands on INLINEASM are the asm string and global flags.
801 if (MI->getNumOperands() < 2) {
802 report("Too few operands on inline asm", MI);
803 return;
804 }
805 if (!MI->getOperand(0).isSymbol())
806 report("Asm string must be an external symbol", MI);
807 if (!MI->getOperand(1).isImm())
808 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000809 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
Wei Ding0526e7f2016-06-22 18:51:08 +0000810 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
811 // and Extra_IsConvergent = 32.
812 if (!isUInt<6>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000813 report("Unknown asm flags", &MI->getOperand(1), 1);
814
Gabor Horvathfee04342015-03-16 09:53:42 +0000815 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000816
817 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
818 unsigned NumOps;
819 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
820 const MachineOperand &MO = MI->getOperand(OpNo);
821 // There may be implicit ops after the fixed operands.
822 if (!MO.isImm())
823 break;
824 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
825 }
826
827 if (OpNo > MI->getNumOperands())
828 report("Missing operands in last group", MI);
829
830 // An optional MDNode follows the groups.
831 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
832 ++OpNo;
833
834 // All trailing operands must be implicit registers.
835 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
836 const MachineOperand &MO = MI->getOperand(OpNo);
837 if (!MO.isReg() || !MO.isImplicit())
838 report("Expected implicit register after groups", &MO, OpNo);
839 }
840}
841
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000842void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000843 const MCInstrDesc &MCID = MI->getDesc();
844 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000845 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000846 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000847 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000848 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000849
Matthias Braun90799ce2016-08-23 21:19:49 +0000850 if (MI->isPHI() && MF->getProperties().hasProperty(
851 MachineFunctionProperties::Property::NoPHIs))
852 report("Found PHI instruction with NoPHIs property set", MI);
853
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000854 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000855 if (MI->isInlineAsm())
856 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000857
Dan Gohmandb9493c2009-10-07 17:36:00 +0000858 // Check the MachineMemOperands for basic consistency.
859 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
860 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000861 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000862 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000863 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000864 report("Missing mayStore flag", MI);
865 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000866
867 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000868 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000869 if (LiveInts) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000870 bool mapped = !LiveInts->isNotInMIMap(*MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000871 if (MI->isDebugValue()) {
872 if (mapped)
873 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000874 } else if (MI->isInsideBundle()) {
875 if (mapped)
876 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000877 } else {
878 if (!mapped)
879 report("Missing slot index", MI);
880 }
881 }
882
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000883 // Check types.
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000884 if (isPreISelGenericOpcode(MCID.getOpcode())) {
Ahmed Bougachab14e9442016-08-02 16:49:22 +0000885 if (isFunctionSelected)
886 report("Unexpected generic instruction in a Selected function", MI);
887
Tim Northover0f140c72016-09-09 11:46:34 +0000888 // Generic instructions specify equality constraints between some
889 // of their operands. Make sure these are consistent.
890 SmallVector<LLT, 4> Types;
891 for (unsigned i = 0; i < MCID.getNumOperands(); ++i) {
892 if (!MCID.OpInfo[i].isGenericType())
893 continue;
894 size_t TypeIdx = MCID.OpInfo[i].getGenericTypeIndex();
895 Types.resize(std::max(TypeIdx + 1, Types.size()));
896
897 LLT OpTy = MRI->getType(MI->getOperand(i).getReg());
898 if (Types[TypeIdx].isValid() && Types[TypeIdx] != OpTy)
899 report("type mismatch in generic instruction", MI);
900 Types[TypeIdx] = OpTy;
901 }
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000902 }
903
Tim Northovere5102de2016-08-30 18:52:46 +0000904 // Generic opcodes must not have physical register operands.
Tim Northover25d12862016-09-09 11:47:31 +0000905 if (isPreISelGenericOpcode(MCID.getOpcode())) {
Tim Northovere5102de2016-08-30 18:52:46 +0000906 for (auto &Op : MI->operands()) {
907 if (Op.isReg() && TargetRegisterInfo::isPhysicalRegister(Op.getReg()))
908 report("Generic instruction cannot have physical register", MI);
909 }
910 }
911
Tim Northover88634992017-02-17 18:50:15 +0000912 // Generic loads and stores must have a single MachineMemOperand
913 // describing that access.
914 if ((MI->getOpcode() == TargetOpcode::G_LOAD ||
915 MI->getOpcode() == TargetOpcode::G_STORE) &&
916 !MI->hasOneMemOperand())
917 report("Generic instruction accessing memory must have one mem operand",
918 MI);
919
Andrew Trick924123a2011-09-21 02:20:46 +0000920 StringRef ErrorInfo;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000921 if (!TII->verifyInstruction(*MI, ErrorInfo))
Andrew Trick924123a2011-09-21 02:20:46 +0000922 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000923}
924
925void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000926MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000927 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000928 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +0000929 unsigned NumDefs = MCID.getNumDefs();
930 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
931 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000932
Evan Cheng6cc775f2011-06-28 19:10:37 +0000933 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +0000934 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000935 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000936 if (!MO->isReg())
937 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +0000938 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000939 report("Explicit definition marked as use", MO, MONum);
940 else if (MO->isImplicit())
941 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000942 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000943 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +0000944 // Don't check if it's the last operand in a variadic instruction. See,
945 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000946 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000947 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000948 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +0000949 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000950 if (MO->isImplicit())
951 report("Explicit operand marked as implicit", MO, MONum);
952 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000953
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000954 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
955 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000956 if (!MO->isReg())
957 report("Tied use must be a register", MO, MONum);
958 else if (!MO->isTied())
959 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000960 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
961 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000962 } else if (MO->isReg() && MO->isTied())
963 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000964 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +0000965 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000966 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000967 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000968 }
969
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000970 switch (MO->getType()) {
971 case MachineOperand::MO_Register: {
972 const unsigned Reg = MO->getReg();
973 if (!Reg)
974 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000975 if (MRI->tracksLiveness() && !MI->isDebugValue())
976 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000977
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000978 // Verify the consistency of tied operands.
979 if (MO->isTied()) {
980 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
981 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
982 if (!OtherMO.isReg())
983 report("Must be tied to a register", MO, MONum);
984 if (!OtherMO.isTied())
985 report("Missing tie flags on tied operand", MO, MONum);
986 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
987 report("Inconsistent tie links", MO, MONum);
988 if (MONum < MCID.getNumDefs()) {
989 if (OtherIdx < MCID.getNumOperands()) {
990 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
991 report("Explicit def tied to explicit use without tie constraint",
992 MO, MONum);
993 } else {
994 if (!OtherMO.isImplicit())
995 report("Explicit def should be tied to implicit use", MO, MONum);
996 }
997 }
998 }
999
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001000 // Verify two-address constraints after leaving SSA form.
1001 unsigned DefIdx;
1002 if (!MRI->isSSA() && MO->isUse() &&
1003 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
1004 Reg != MI->getOperand(DefIdx).getReg())
1005 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001006
1007 // Check register classes.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001008 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001009 unsigned SubIdx = MO->getSubReg();
1010
1011 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001012 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001013 report("Illegal subregister index for physical register", MO, MONum);
1014 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001015 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001016 if (const TargetRegisterClass *DRC =
1017 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001018 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001019 report("Illegal physical register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001020 errs() << TRI->getName(Reg) << " is not a "
Craig Toppercf0444b2014-11-17 05:50:14 +00001021 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001022 }
1023 }
1024 } else {
1025 // Virtual register.
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001026 const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
1027 if (!RC) {
1028 // This is a generic virtual register.
Ahmed Bougachab14e9442016-08-02 16:49:22 +00001029
1030 // If we're post-Select, we can't have gvregs anymore.
1031 if (isFunctionSelected) {
1032 report("Generic virtual register invalid in a Selected function",
1033 MO, MONum);
1034 return;
1035 }
1036
Quentin Colombet3749f332016-12-22 22:50:34 +00001037 // The gvreg must have a type and it must not have a SubIdx.
Tim Northover0f140c72016-09-09 11:46:34 +00001038 LLT Ty = MRI->getType(Reg);
1039 if (!Ty.isValid()) {
1040 report("Generic virtual register must have a valid type", MO,
1041 MONum);
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001042 return;
1043 }
Ahmed Bougacha3681c772016-08-02 16:17:15 +00001044
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001045 const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
Ahmed Bougacha3681c772016-08-02 16:17:15 +00001046
1047 // If we're post-RegBankSelect, the gvreg must have a bank.
1048 if (!RegBank && isFunctionRegBankSelected) {
1049 report("Generic virtual register must have a bank in a "
1050 "RegBankSelected function",
1051 MO, MONum);
1052 return;
1053 }
1054
1055 // Make sure the register fits into its register bank if any.
Tim Northover32a078a2016-09-15 10:09:59 +00001056 if (RegBank && Ty.isValid() &&
Tim Northover0f140c72016-09-09 11:46:34 +00001057 RegBank->getSize() < Ty.getSizeInBits()) {
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001058 report("Register bank is too small for virtual register", MO,
1059 MONum);
1060 errs() << "Register bank " << RegBank->getName() << " too small("
Tim Northover0f140c72016-09-09 11:46:34 +00001061 << RegBank->getSize() << ") to fit " << Ty.getSizeInBits()
1062 << "-bits\n";
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001063 return;
1064 }
1065 if (SubIdx) {
Tim Northover0f140c72016-09-09 11:46:34 +00001066 report("Generic virtual register does not subregister index", MO,
1067 MONum);
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001068 return;
1069 }
Quentin Colombetfa5960a2016-12-22 21:56:39 +00001070
1071 // If this is a target specific instruction and this operand
1072 // has register class constraint, the virtual register must
1073 // comply to it.
1074 if (!isPreISelGenericOpcode(MCID.getOpcode()) &&
1075 TII->getRegClass(MCID, MONum, TRI, *MF)) {
1076 report("Virtual register does not match instruction constraint", MO,
1077 MONum);
1078 errs() << "Expect register class "
1079 << TRI->getRegClassName(
1080 TII->getRegClass(MCID, MONum, TRI, *MF))
1081 << " but got nothing\n";
1082 return;
1083 }
1084
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001085 break;
1086 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001087 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001088 const TargetRegisterClass *SRC =
1089 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001090 if (!SRC) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001091 report("Invalid subregister index for virtual register", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001092 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001093 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001094 return;
1095 }
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001096 if (RC != SRC) {
1097 report("Invalid register class for subregister index", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001098 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001099 << " does not fully support subreg index " << SubIdx << "\n";
1100 return;
1101 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001102 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001103 if (const TargetRegisterClass *DRC =
1104 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001105 if (SubIdx) {
1106 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +00001107 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001108 if (!SuperRC) {
1109 report("No largest legal super class exists.", MO, MONum);
1110 return;
1111 }
1112 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
1113 if (!DRC) {
1114 report("No matching super-reg register class.", MO, MONum);
1115 return;
1116 }
1117 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +00001118 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001119 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001120 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +00001121 << " register, but got a " << TRI->getRegClassName(RC)
1122 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001123 }
1124 }
1125 }
1126 }
1127 break;
1128 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001129
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001130 case MachineOperand::MO_RegisterMask:
1131 regMasks.push_back(MO->getRegMask());
1132 break;
1133
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001134 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +00001135 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
1136 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001137 break;
1138
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001139 case MachineOperand::MO_FrameIndex:
1140 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001141 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
Jonas Paulsson72640f12015-10-29 08:28:35 +00001142 int FI = MO->getIndex();
1143 LiveInterval &LI = LiveStks->getInterval(FI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001144 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001145
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001146 bool stores = MI->mayStore();
Jonas Paulsson72640f12015-10-29 08:28:35 +00001147 bool loads = MI->mayLoad();
1148 // For a memory-to-memory move, we need to check if the frame
1149 // index is used for storing or loading, by inspecting the
1150 // memory operands.
1151 if (stores && loads) {
1152 for (auto *MMO : MI->memoperands()) {
1153 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1154 if (PSV == nullptr) continue;
1155 const FixedStackPseudoSourceValue *Value =
1156 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1157 if (Value == nullptr) continue;
1158 if (Value->getFrameIndex() != FI) continue;
1159
1160 if (MMO->isStore())
1161 loads = false;
1162 else
1163 stores = false;
1164 break;
1165 }
1166 if (loads == stores)
1167 report("Missing fixed stack memoperand.", MI);
1168 }
1169 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001170 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001171 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001172 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001173 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001174 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001175 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001176 }
1177 }
1178 break;
1179
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001180 default:
1181 break;
1182 }
1183}
1184
Matthias Braun1377fd62016-02-02 20:04:51 +00001185void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
1186 unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
1187 LaneBitmask LaneMask) {
1188 LiveQueryResult LRQ = LR.Query(UseIdx);
1189 // Check if we have a segment at the use, note however that we only need one
1190 // live subregister range, the others may be dead.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001191 if (!LRQ.valueIn() && LaneMask.none()) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001192 report("No live segment at use", MO, MONum);
1193 report_context_liverange(LR);
1194 report_context_vreg_regunit(VRegOrUnit);
1195 report_context(UseIdx);
1196 }
1197 if (MO->isKill() && !LRQ.isKill()) {
1198 report("Live range continues after kill flag", MO, MONum);
1199 report_context_liverange(LR);
1200 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001201 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001202 report_context_lanemask(LaneMask);
1203 report_context(UseIdx);
1204 }
1205}
1206
1207void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
1208 unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
1209 LaneBitmask LaneMask) {
1210 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
1211 assert(VNI && "NULL valno is not allowed");
1212 if (VNI->def != DefIdx) {
1213 report("Inconsistent valno->def", MO, MONum);
1214 report_context_liverange(LR);
1215 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001216 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001217 report_context_lanemask(LaneMask);
1218 report_context(*VNI);
1219 report_context(DefIdx);
1220 }
1221 } else {
1222 report("No live segment at def", MO, MONum);
1223 report_context_liverange(LR);
1224 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001225 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001226 report_context_lanemask(LaneMask);
1227 report_context(DefIdx);
1228 }
1229 // Check that, if the dead def flag is present, LiveInts agree.
1230 if (MO->isDead()) {
1231 LiveQueryResult LRQ = LR.Query(DefIdx);
1232 if (!LRQ.isDeadDef()) {
1233 // In case of physregs we can have a non-dead definition on another
1234 // operand.
1235 bool otherDef = false;
1236 if (!TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
1237 const MachineInstr &MI = *MO->getParent();
1238 for (const MachineOperand &MO : MI.operands()) {
1239 if (!MO.isReg() || !MO.isDef() || MO.isDead())
1240 continue;
1241 unsigned Reg = MO.getReg();
1242 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1243 if (*Units == VRegOrUnit) {
1244 otherDef = true;
1245 break;
1246 }
1247 }
1248 }
1249 }
1250
1251 if (!otherDef) {
1252 report("Live range continues after dead def flag", MO, MONum);
1253 report_context_liverange(LR);
1254 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001255 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001256 report_context_lanemask(LaneMask);
1257 }
1258 }
1259 }
1260}
1261
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001262void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1263 const MachineInstr *MI = MO->getParent();
1264 const unsigned Reg = MO->getReg();
1265
1266 // Both use and def operands can read a register.
1267 if (MO->readsReg()) {
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001268 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001269 addRegWithSubRegs(regsKilled, Reg);
1270
1271 // Check that LiveVars knows this kill.
1272 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1273 MO->isKill()) {
1274 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
David Majnemer0d955d02016-08-11 22:21:41 +00001275 if (!is_contained(VI.Kills, MI))
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001276 report("Kill missing from LiveVariables", MO, MONum);
1277 }
1278
1279 // Check LiveInts liveness and kill.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001280 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1281 SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001282 // Check the cached regunit intervals.
1283 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1284 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001285 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
1286 checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001287 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001288 }
1289
1290 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1291 if (LiveInts->hasInterval(Reg)) {
1292 // This is a virtual register interval.
1293 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun1377fd62016-02-02 20:04:51 +00001294 checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
1295
1296 if (LI.hasSubRanges() && !MO->isDef()) {
1297 unsigned SubRegIdx = MO->getSubReg();
1298 LaneBitmask MOMask = SubRegIdx != 0
1299 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1300 : MRI->getMaxLaneMaskForVReg(Reg);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001301 LaneBitmask LiveInMask;
Matthias Braun1377fd62016-02-02 20:04:51 +00001302 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001303 if ((MOMask & SR.LaneMask).none())
Matthias Braun1377fd62016-02-02 20:04:51 +00001304 continue;
1305 checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
1306 LiveQueryResult LRQ = SR.Query(UseIdx);
1307 if (LRQ.valueIn())
1308 LiveInMask |= SR.LaneMask;
1309 }
1310 // At least parts of the register has to be live at the use.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001311 if ((LiveInMask & MOMask).none()) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001312 report("No live subrange at use", MO, MONum);
1313 report_context(LI);
1314 report_context(UseIdx);
1315 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001316 }
1317 } else {
1318 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001319 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001320 }
1321 }
1322
1323 // Use of a dead register.
1324 if (!regsLive.count(Reg)) {
1325 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1326 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001327 bool Bad = !isReserved(Reg);
1328 // We are fine if just any subregister has a defined value.
1329 if (Bad) {
1330 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1331 ++SubRegs) {
1332 if (regsLive.count(*SubRegs)) {
1333 Bad = false;
1334 break;
1335 }
1336 }
1337 }
Matthias Braun96a31952015-01-14 22:25:14 +00001338 // If there is an additional implicit-use of a super register we stop
1339 // here. By definition we are fine if the super register is not
1340 // (completely) dead, if the complete super register is dead we will
1341 // get a report for its operand.
1342 if (Bad) {
1343 for (const MachineOperand &MOP : MI->uses()) {
1344 if (!MOP.isReg())
1345 continue;
1346 if (!MOP.isImplicit())
1347 continue;
1348 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1349 ++SubRegs) {
1350 if (*SubRegs == Reg) {
1351 Bad = false;
1352 break;
1353 }
1354 }
1355 }
1356 }
Matthias Braun96d77322014-12-10 01:13:13 +00001357 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001358 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001359 } else if (MRI->def_empty(Reg)) {
1360 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001361 } else {
1362 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1363 // We don't know which virtual registers are live in, so only complain
1364 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1365 // must be live in. PHI instructions are handled separately.
1366 if (MInfo.regsKilled.count(Reg))
1367 report("Using a killed virtual register", MO, MONum);
1368 else if (!MI->isPHI())
1369 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1370 }
1371 }
1372 }
1373
1374 if (MO->isDef()) {
1375 // Register defined.
1376 // TODO: verify that earlyclobber ops are not used.
1377 if (MO->isDead())
1378 addRegWithSubRegs(regsDead, Reg);
1379 else
1380 addRegWithSubRegs(regsDefined, Reg);
1381
1382 // Verify SSA form.
1383 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001384 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001385 report("Multiple virtual register defs in SSA form", MO, MONum);
1386
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001387 // Check LiveInts for a live segment, but only for virtual registers.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001388 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1389 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001390 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Matthias Braun1377fd62016-02-02 20:04:51 +00001391
1392 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1393 if (LiveInts->hasInterval(Reg)) {
1394 const LiveInterval &LI = LiveInts->getInterval(Reg);
1395 checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
1396
1397 if (LI.hasSubRanges()) {
1398 unsigned SubRegIdx = MO->getSubReg();
1399 LaneBitmask MOMask = SubRegIdx != 0
1400 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1401 : MRI->getMaxLaneMaskForVReg(Reg);
1402 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001403 if ((SR.LaneMask & MOMask).none())
Matthias Braun1377fd62016-02-02 20:04:51 +00001404 continue;
1405 checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, SR.LaneMask);
1406 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001407 }
1408 } else {
Matthias Braun1377fd62016-02-02 20:04:51 +00001409 report("Virtual register has no Live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001410 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001411 }
1412 }
1413 }
1414}
1415
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001416void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001417}
1418
1419// This function gets called after visiting all instructions in a bundle. The
1420// argument points to the bundle header.
1421// Normal stand-alone instructions are also considered 'bundles', and this
1422// function is called for all of them.
1423void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001424 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1425 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001426 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001427 // Kill any masked registers.
1428 while (!regMasks.empty()) {
1429 const uint32_t *Mask = regMasks.pop_back_val();
1430 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1431 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1432 MachineOperand::clobbersPhysReg(Mask, *I))
1433 regsDead.push_back(*I);
1434 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001435 set_subtract(regsLive, regsDead); regsDead.clear();
1436 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001437}
1438
1439void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001440MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001441 MBBInfoMap[MBB].regsLiveOut = regsLive;
1442 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001443
1444 if (Indexes) {
1445 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1446 if (!(stop > lastIndex)) {
1447 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001448 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001449 << " last instruction was at " << lastIndex << '\n';
1450 }
1451 lastIndex = stop;
1452 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001453}
1454
1455// Calculate the largest possible vregsPassed sets. These are the registers that
1456// can pass through an MBB live, but may not be live every time. It is assumed
1457// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001458void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001459 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1460 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001461 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001462 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001463 BBInfo &MInfo = MBBInfoMap[&MBB];
1464 if (!MInfo.reachable)
1465 continue;
1466 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1467 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1468 BBInfo &SInfo = MBBInfoMap[*SuI];
1469 if (SInfo.addPassed(MInfo.regsLiveOut))
1470 todo.insert(*SuI);
1471 }
1472 }
1473
1474 // Iteratively push vregsPassed to successors. This will converge to the same
1475 // final state regardless of DenseSet iteration order.
1476 while (!todo.empty()) {
1477 const MachineBasicBlock *MBB = *todo.begin();
1478 todo.erase(MBB);
1479 BBInfo &MInfo = MBBInfoMap[MBB];
1480 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1481 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1482 if (*SuI == MBB)
1483 continue;
1484 BBInfo &SInfo = MBBInfoMap[*SuI];
1485 if (SInfo.addPassed(MInfo.vregsPassed))
1486 todo.insert(*SuI);
1487 }
1488 }
1489}
1490
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001491// Calculate the set of virtual registers that must be passed through each basic
1492// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001493// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001494void MachineVerifier::calcRegsRequired() {
1495 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001496 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001497 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001498 BBInfo &MInfo = MBBInfoMap[&MBB];
1499 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1500 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1501 BBInfo &PInfo = MBBInfoMap[*PrI];
1502 if (PInfo.addRequired(MInfo.vregsLiveIn))
1503 todo.insert(*PrI);
1504 }
1505 }
1506
1507 // Iteratively push vregsRequired to predecessors. This will converge to the
1508 // same final state regardless of DenseSet iteration order.
1509 while (!todo.empty()) {
1510 const MachineBasicBlock *MBB = *todo.begin();
1511 todo.erase(MBB);
1512 BBInfo &MInfo = MBBInfoMap[MBB];
1513 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1514 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1515 if (*PrI == MBB)
1516 continue;
1517 BBInfo &SInfo = MBBInfoMap[*PrI];
1518 if (SInfo.addRequired(MInfo.vregsRequired))
1519 todo.insert(*PrI);
1520 }
1521 }
1522}
1523
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001524// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001525// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001526void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001527 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001528 for (const auto &BBI : *MBB) {
1529 if (!BBI.isPHI())
1530 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001531 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001532
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001533 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1534 unsigned Reg = BBI.getOperand(i).getReg();
1535 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001536 if (!Pre->isSuccessor(MBB))
1537 continue;
1538 seen.insert(Pre);
1539 BBInfo &PrInfo = MBBInfoMap[Pre];
1540 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1541 report("PHI operand is not live-out from predecessor",
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001542 &BBI.getOperand(i), i);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001543 }
1544
1545 // Did we see all predecessors?
1546 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1547 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1548 if (!seen.count(*PrI)) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001549 report("Missing PHI operand", &BBI);
Owen Anderson21b17882015-02-04 00:02:59 +00001550 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001551 << " is a predecessor according to the CFG.\n";
1552 }
1553 }
1554 }
1555}
1556
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001557void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001558 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001559
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001560 for (const auto &MBB : *MF) {
1561 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001562
1563 // Skip unreachable MBBs.
1564 if (!MInfo.reachable)
1565 continue;
1566
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001567 checkPHIOps(&MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001568 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001569
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001570 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001571 calcRegsRequired();
1572
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001573 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001574 for (const auto &MBB : *MF) {
1575 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001576 for (RegSet::iterator
1577 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1578 ++I)
1579 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001580 report("Virtual register killed in block, but needed live out.", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001581 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001582 << " is used after the block.\n";
1583 }
1584 }
1585
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001586 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001587 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1588 for (RegSet::iterator
1589 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Matthias Braun30668dd2016-05-11 21:31:39 +00001590 ++I) {
1591 report("Virtual register defs don't dominate all uses.", MF);
1592 report_context_vreg(*I);
1593 }
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001594 }
1595
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001596 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001597 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001598 if (LiveInts)
1599 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001600}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001601
1602void MachineVerifier::verifyLiveVariables() {
1603 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001604 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1605 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001606 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001607 for (const auto &MBB : *MF) {
1608 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001609
1610 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1611 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001612 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1613 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001614 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001615 << " must be live through the block.\n";
1616 }
1617 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001618 if (VI.AliveBlocks.test(MBB.getNumber())) {
1619 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001620 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001621 << " is not needed live through the block.\n";
1622 }
1623 }
1624 }
1625 }
1626}
1627
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001628void MachineVerifier::verifyLiveIntervals() {
1629 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001630 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1631 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001632
1633 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001634 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001635 continue;
1636
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001637 if (!LiveInts->hasInterval(Reg)) {
1638 report("Missing live interval for virtual register", MF);
Owen Anderson21b17882015-02-04 00:02:59 +00001639 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001640 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001641 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001642
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001643 const LiveInterval &LI = LiveInts->getInterval(Reg);
1644 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001645 verifyLiveInterval(LI);
1646 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001647
1648 // Verify all the cached regunit intervals.
1649 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001650 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1651 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001652}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001653
Matthias Braun364e6e92013-10-10 21:28:54 +00001654void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001655 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001656 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001657 if (VNI->isUnused())
1658 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001659
Matthias Braun364e6e92013-10-10 21:28:54 +00001660 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001661
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001662 if (!DefVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001663 report("Value not live at VNInfo def and not marked unused", MF);
1664 report_context(LR, Reg, LaneMask);
1665 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001666 return;
1667 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001668
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001669 if (DefVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001670 report("Live segment at def has different VNInfo", MF);
1671 report_context(LR, Reg, LaneMask);
1672 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001673 return;
1674 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001675
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001676 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1677 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001678 report("Invalid VNInfo definition index", MF);
1679 report_context(LR, Reg, LaneMask);
1680 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001681 return;
1682 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001683
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001684 if (VNI->isPHIDef()) {
1685 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001686 report("PHIDef VNInfo is not defined at MBB start", MBB);
1687 report_context(LR, Reg, LaneMask);
1688 report_context(*VNI);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001689 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001690 return;
1691 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001692
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001693 // Non-PHI def.
1694 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1695 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001696 report("No instruction at VNInfo def index", MBB);
1697 report_context(LR, Reg, LaneMask);
1698 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001699 return;
1700 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001701
Matthias Braun364e6e92013-10-10 21:28:54 +00001702 if (Reg != 0) {
1703 bool hasDef = false;
1704 bool isEarlyClobber = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001705 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001706 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001707 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001708 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1709 if (MOI->getReg() != Reg)
1710 continue;
1711 } else {
1712 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1713 !TRI->hasRegUnit(MOI->getReg(), Reg))
1714 continue;
1715 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001716 if (LaneMask.any() &&
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001717 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask).none())
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001718 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001719 hasDef = true;
1720 if (MOI->isEarlyClobber())
1721 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001722 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001723
Matthias Braun364e6e92013-10-10 21:28:54 +00001724 if (!hasDef) {
1725 report("Defining instruction does not modify register", MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001726 report_context(LR, Reg, LaneMask);
1727 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001728 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001729
Matthias Braun364e6e92013-10-10 21:28:54 +00001730 // Early clobber defs begin at USE slots, but other defs must begin at
1731 // DEF slots.
1732 if (isEarlyClobber) {
1733 if (!VNI->def.isEarlyClobber()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001734 report("Early clobber def must be at an early-clobber slot", MBB);
1735 report_context(LR, Reg, LaneMask);
1736 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001737 }
1738 } else if (!VNI->def.isRegister()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001739 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
1740 report_context(LR, Reg, LaneMask);
1741 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001742 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001743 }
1744}
1745
Matthias Braun364e6e92013-10-10 21:28:54 +00001746void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1747 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001748 unsigned Reg, LaneBitmask LaneMask)
1749{
Matthias Braun364e6e92013-10-10 21:28:54 +00001750 const LiveRange::Segment &S = *I;
1751 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001752 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001753
Matthias Braun364e6e92013-10-10 21:28:54 +00001754 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001755 report("Foreign valno in live segment", MF);
1756 report_context(LR, Reg, LaneMask);
1757 report_context(S);
1758 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001759 }
1760
1761 if (VNI->isUnused()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001762 report("Live segment valno is marked unused", MF);
1763 report_context(LR, Reg, LaneMask);
1764 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001765 }
1766
Matthias Braun364e6e92013-10-10 21:28:54 +00001767 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001768 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001769 report("Bad start of live segment, no basic block", MF);
1770 report_context(LR, Reg, LaneMask);
1771 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001772 return;
1773 }
1774 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001775 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001776 report("Live segment must begin at MBB entry or valno def", MBB);
1777 report_context(LR, Reg, LaneMask);
1778 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001779 }
1780
1781 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001782 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001783 if (!EndMBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001784 report("Bad end of live segment, no basic block", MF);
1785 report_context(LR, Reg, LaneMask);
1786 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001787 return;
1788 }
1789
1790 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001791 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001792 return;
1793
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001794 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001795 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1796 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001797 return;
1798
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001799 // The live segment is ending inside EndMBB
1800 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001801 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001802 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001803 report("Live segment doesn't end at a valid instruction", EndMBB);
1804 report_context(LR, Reg, LaneMask);
1805 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001806 return;
1807 }
1808
1809 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001810 if (S.end.isBlock()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001811 report("Live segment ends at B slot of an instruction", EndMBB);
1812 report_context(LR, Reg, LaneMask);
1813 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001814 }
1815
Matthias Braun364e6e92013-10-10 21:28:54 +00001816 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001817 // Segment ends on the dead slot.
1818 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001819 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001820 report("Live segment ending at dead slot spans instructions", EndMBB);
1821 report_context(LR, Reg, LaneMask);
1822 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001823 }
1824 }
1825
1826 // A live segment can only end at an early-clobber slot if it is being
1827 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001828 if (S.end.isEarlyClobber()) {
1829 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001830 report("Live segment ending at early clobber slot must be "
Matthias Braun7e624d52015-11-09 23:59:33 +00001831 "redefined by an EC def in the same instruction", EndMBB);
1832 report_context(LR, Reg, LaneMask);
1833 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001834 }
1835 }
1836
1837 // The following checks only apply to virtual registers. Physreg liveness
1838 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001839 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001840 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001841 // use, or a dead flag on a def.
1842 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001843 bool hasSubRegDef = false;
Matthias Braun72a58c32016-03-29 19:07:43 +00001844 bool hasDeadDef = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001845 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001846 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001847 continue;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001848 unsigned Sub = MOI->getSubReg();
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001849 LaneBitmask SLM = Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub)
1850 : LaneBitmask::getAll();
Matthias Braun72a58c32016-03-29 19:07:43 +00001851 if (MOI->isDef()) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001852 if (Sub != 0) {
Matthias Braun72a58c32016-03-29 19:07:43 +00001853 hasSubRegDef = true;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001854 // An operand vreg0:sub0<def> reads vreg0:sub1..n. Invert the lane
1855 // mask for subregister defs. Read-undef defs will be handled by
1856 // readsReg below.
Krzysztof Parzyszek0a955d62016-08-29 13:15:35 +00001857 SLM = ~SLM;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001858 }
Matthias Braun72a58c32016-03-29 19:07:43 +00001859 if (MOI->isDead())
1860 hasDeadDef = true;
1861 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001862 if (LaneMask.any() && (LaneMask & SLM).none())
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001863 continue;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001864 if (MOI->readsReg())
1865 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001866 }
Matthias Braun72a58c32016-03-29 19:07:43 +00001867 if (S.end.isDead()) {
1868 // Make sure that the corresponding machine operand for a "dead" live
1869 // range has the dead flag. We cannot perform this check for subregister
1870 // liveranges as partially dead values are allowed.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001871 if (LaneMask.none() && !hasDeadDef) {
Matthias Braun72a58c32016-03-29 19:07:43 +00001872 report("Instruction ending live segment on dead slot has no dead flag",
1873 MI);
1874 report_context(LR, Reg, LaneMask);
1875 report_context(S);
1876 }
1877 } else {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001878 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001879 // When tracking subregister liveness, the main range must start new
1880 // values on partial register writes, even if there is no read.
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001881 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask.any() ||
Matthias Brauna25e13a2015-03-19 00:21:58 +00001882 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001883 report("Instruction ending live segment doesn't read the register",
1884 MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001885 report_context(LR, Reg, LaneMask);
1886 report_context(S);
Matthias Braun21554d92014-12-10 01:13:11 +00001887 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001888 }
1889 }
1890 }
1891
1892 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001893 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001894 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00001895 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001896 // Not live-in to any blocks.
1897 if (MBB == EndMBB)
1898 return;
1899 // Skip this block.
1900 ++MFI;
1901 }
1902 for (;;) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001903 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001904 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00001905 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00001906 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001907 if (&*MFI == EndMBB)
1908 break;
1909 ++MFI;
1910 continue;
1911 }
1912
1913 // Is VNI a PHI-def in the current block?
1914 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001915 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001916
1917 // Check that VNI is live-out of all predecessors.
1918 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1919 PE = MFI->pred_end(); PI != PE; ++PI) {
1920 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001921 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001922
Matthias Braune29b7682016-05-20 23:02:13 +00001923 // All predecessors must have a live-out value if this is not a
1924 // subregister liverange.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001925 if (!PVNI && LaneMask.none()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001926 report("Register not marked live out of predecessor", *PI);
1927 report_context(LR, Reg, LaneMask);
1928 report_context(*VNI);
1929 errs() << " live into BB#" << MFI->getNumber()
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001930 << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
1931 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001932 continue;
1933 }
1934
1935 // Only PHI-defs can take different predecessor values.
1936 if (!IsPHI && PVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001937 report("Different value live out of predecessor", *PI);
1938 report_context(LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001939 errs() << "Valno #" << PVNI->id << " live out of BB#"
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001940 << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
1941 << " live into BB#" << MFI->getNumber() << '@'
1942 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001943 }
1944 }
1945 if (&*MFI == EndMBB)
1946 break;
1947 ++MFI;
1948 }
1949}
1950
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001951void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001952 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00001953 for (const VNInfo *VNI : LR.valnos)
1954 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001955
Matthias Braun364e6e92013-10-10 21:28:54 +00001956 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001957 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00001958}
1959
1960void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001961 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00001962 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1963 verifyLiveRange(LI, Reg);
1964
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001965 LaneBitmask Mask;
Matthias Braune6a24852015-09-25 21:51:14 +00001966 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00001967 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001968 if ((Mask & SR.LaneMask).any()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001969 report("Lane masks of sub ranges overlap in live interval", MF);
1970 report_context(LI);
1971 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001972 if ((SR.LaneMask & ~MaxMask).any()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001973 report("Subrange lanemask is invalid", MF);
1974 report_context(LI);
1975 }
1976 if (SR.empty()) {
1977 report("Subrange must not be empty", MF);
1978 report_context(SR, LI.reg, SR.LaneMask);
1979 }
Matthias Braune962e522015-03-25 21:18:22 +00001980 Mask |= SR.LaneMask;
1981 verifyLiveRange(SR, LI.reg, SR.LaneMask);
Matthias Braun7e624d52015-11-09 23:59:33 +00001982 if (!LI.covers(SR)) {
1983 report("A Subrange is not covered by the main range", MF);
1984 report_context(LI);
1985 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001986 }
1987
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001988 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00001989 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
Matthias Braunbf47f632016-01-08 01:16:35 +00001990 unsigned NumComp = ConEQ.Classify(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001991 if (NumComp > 1) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001992 report("Multiple connected components in live interval", MF);
1993 report_context(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001994 for (unsigned comp = 0; comp != NumComp; ++comp) {
1995 errs() << comp << ": valnos";
1996 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1997 E = LI.vni_end(); I!=E; ++I)
1998 if (comp == ConEQ.getEqClass(*I))
1999 errs() << ' ' << (*I)->id;
2000 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00002001 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002002 }
2003}
Manman Renaa6875b2013-07-15 21:26:31 +00002004
2005namespace {
2006 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
2007 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
2008 // value is zero.
2009 // We use a bool plus an integer to capture the stack state.
2010 struct StackStateOfBB {
2011 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
2012 ExitIsSetup(false) { }
2013 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
2014 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
2015 ExitIsSetup(ExitSetup) { }
2016 // Can be negative, which means we are setting up a frame.
2017 int EntryValue;
2018 int ExitValue;
2019 bool EntryIsSetup;
2020 bool ExitIsSetup;
2021 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002022}
Manman Renaa6875b2013-07-15 21:26:31 +00002023
2024/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
2025/// by a FrameDestroy <n>, stack adjustments are identical on all
2026/// CFG edges to a merge point, and frame is destroyed at end of a return block.
2027void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00002028 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
2029 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Serge Pavlov802aa662017-04-20 01:34:04 +00002030 if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
2031 return;
Manman Renaa6875b2013-07-15 21:26:31 +00002032
2033 SmallVector<StackStateOfBB, 8> SPState;
2034 SPState.resize(MF->getNumBlockIDs());
David Callahanc1051ab2016-10-05 21:36:16 +00002035 df_iterator_default_set<const MachineBasicBlock*> Reachable;
Manman Renaa6875b2013-07-15 21:26:31 +00002036
2037 // Visit the MBBs in DFS order.
2038 for (df_ext_iterator<const MachineFunction*,
David Callahanc1051ab2016-10-05 21:36:16 +00002039 df_iterator_default_set<const MachineBasicBlock*> >
Manman Renaa6875b2013-07-15 21:26:31 +00002040 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
2041 DFI != DFE; ++DFI) {
2042 const MachineBasicBlock *MBB = *DFI;
2043
2044 StackStateOfBB BBState;
2045 // Check the exit state of the DFS stack predecessor.
2046 if (DFI.getPathLength() >= 2) {
2047 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
2048 assert(Reachable.count(StackPred) &&
2049 "DFS stack predecessor is already visited.\n");
2050 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
2051 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
2052 BBState.ExitValue = BBState.EntryValue;
2053 BBState.ExitIsSetup = BBState.EntryIsSetup;
2054 }
2055
2056 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002057 for (const auto &I : *MBB) {
2058 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00002059 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002060 report("FrameSetup is after another FrameSetup", &I);
Serge Pavlovd526b132017-05-09 13:35:13 +00002061 BBState.ExitValue -= TII->getFrameTotalSize(I);
Manman Renaa6875b2013-07-15 21:26:31 +00002062 BBState.ExitIsSetup = true;
2063 }
2064
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002065 if (I.getOpcode() == FrameDestroyOpcode) {
Serge Pavlovd526b132017-05-09 13:35:13 +00002066 int Size = TII->getFrameTotalSize(I);
Manman Renaa6875b2013-07-15 21:26:31 +00002067 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002068 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00002069 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
2070 BBState.ExitValue;
2071 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002072 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00002073 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00002074 << AbsSPAdj << ">.\n";
2075 }
2076 BBState.ExitValue += Size;
2077 BBState.ExitIsSetup = false;
2078 }
2079 }
2080 SPState[MBB->getNumber()] = BBState;
2081
2082 // Make sure the exit state of any predecessor is consistent with the entry
2083 // state.
2084 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
2085 E = MBB->pred_end(); I != E; ++I) {
2086 if (Reachable.count(*I) &&
2087 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
2088 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
2089 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002090 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002091 << SPState[(*I)->getNumber()].ExitValue << ", "
2092 << SPState[(*I)->getNumber()].ExitIsSetup
2093 << "), while BB#" << MBB->getNumber() << " has entry state ("
2094 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
2095 }
2096 }
2097
2098 // Make sure the entry state of any successor is consistent with the exit
2099 // state.
2100 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
2101 E = MBB->succ_end(); I != E; ++I) {
2102 if (Reachable.count(*I) &&
2103 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
2104 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
2105 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002106 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002107 << SPState[(*I)->getNumber()].EntryValue << ", "
2108 << SPState[(*I)->getNumber()].EntryIsSetup
2109 << "), while BB#" << MBB->getNumber() << " has exit state ("
2110 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
2111 }
2112 }
2113
2114 // Make sure a basic block with return ends with zero stack adjustment.
2115 if (!MBB->empty() && MBB->back().isReturn()) {
2116 if (BBState.ExitIsSetup)
2117 report("A return block ends with a FrameSetup.", MBB);
2118 if (BBState.ExitValue)
2119 report("A return block ends with a nonzero stack adjustment.", MBB);
2120 }
2121 }
2122}