blob: 9161cda5184d234688967499cb22e77b6fcb87a9 [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;
75
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000076 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000077 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000078 typedef DenseSet<unsigned> RegSet;
79 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000080 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000081
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000082 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000083 BlockSet FunctionBlocks;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000084
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000085 BitVector regsReserved;
86 RegSet regsLive;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000087 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000088 RegMaskVector regMasks;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000089 RegSet regsLiveInButUnused;
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
Lang Hames1ce837a2012-02-14 19:17:48 +0000190 bool isAllocatable(unsigned Reg) {
Jakob Stoklund Olesen244beb42012-10-16 00:05:06 +0000191 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
Lang Hames1ce837a2012-02-14 19:17:48 +0000192 }
193
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000194 // Analysis information if available
195 LiveVariables *LiveVars;
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +0000196 LiveIntervals *LiveInts;
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000197 LiveStacks *LiveStks;
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000198 SlotIndexes *Indexes;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000199
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000200 void visitMachineFunctionBefore();
201 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000202 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000203 void visitMachineInstrBefore(const MachineInstr *MI);
204 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
205 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000206 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000207 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
208 void visitMachineFunctionAfter();
209
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000210 template <typename T> void report(const char *msg, ilist_iterator<T> I) {
211 report(msg, &*I);
212 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000213 void report(const char *msg, const MachineFunction *MF);
214 void report(const char *msg, const MachineBasicBlock *MBB);
215 void report(const char *msg, const MachineInstr *MI);
216 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Matthias Braun7e624d52015-11-09 23:59:33 +0000217
218 void report_context(const LiveInterval &LI) const;
Matt Arsenault892fcd02016-07-25 19:39:01 +0000219 void report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000220 LaneBitmask LaneMask) const;
221 void report_context(const LiveRange::Segment &S) const;
222 void report_context(const VNInfo &VNI) const;
Matthias Braun579c9cd2016-02-02 02:44:25 +0000223 void report_context(SlotIndex Pos) const;
224 void report_context_liverange(const LiveRange &LR) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000225 void report_context_lanemask(LaneBitmask LaneMask) const;
Matthias Braun30668dd2016-05-11 21:31:39 +0000226 void report_context_vreg(unsigned VReg) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000227 void report_context_vreg_regunit(unsigned VRegOrRegUnit) const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000228
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000229 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000230
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000231 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Matthias Braun1377fd62016-02-02 20:04:51 +0000232 void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
233 SlotIndex UseIdx, const LiveRange &LR, unsigned Reg,
234 LaneBitmask LaneMask = 0);
235 void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
236 SlotIndex DefIdx, const LiveRange &LR, unsigned Reg,
237 LaneBitmask LaneMask = 0);
238
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000239 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000240 void calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000241 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000242
243 void calcRegsRequired();
244 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000245 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000246 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000247 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
248 unsigned);
Matthias Braun364e6e92013-10-10 21:28:54 +0000249 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000250 const LiveRange::const_iterator I, unsigned,
251 unsigned);
Matthias Braune6a24852015-09-25 21:51:14 +0000252 void verifyLiveRange(const LiveRange&, unsigned, LaneBitmask LaneMask = 0);
Manman Renaa6875b2013-07-15 21:26:31 +0000253
254 void verifyStackFrame();
Matthias Braun80595462015-09-09 17:49:46 +0000255
256 void verifySlotIndexes() const;
Derek Schuff42666ee2016-03-29 17:40:22 +0000257 void verifyProperties(const MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000258 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000259
260 struct MachineVerifierPass : public MachineFunctionPass {
261 static char ID; // Pass ID, replacement for typeid
Matthias Brauna4e932d2014-12-11 19:41:51 +0000262 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000263
Matthias Brauna4e932d2014-12-11 19:41:51 +0000264 MachineVerifierPass(const std::string &banner = nullptr)
265 : MachineFunctionPass(ID), Banner(banner) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000266 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
267 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000268
Craig Topper4584cd52014-03-07 09:26:03 +0000269 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000270 AU.setPreservesAll();
271 MachineFunctionPass::getAnalysisUsage(AU);
272 }
273
Craig Topper4584cd52014-03-07 09:26:03 +0000274 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Braunb3aefc32016-02-15 19:25:31 +0000275 unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
276 if (FoundErrors)
277 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000278 return false;
279 }
280 };
281
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000282}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000283
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000284char MachineVerifierPass::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +0000285INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000286 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000287
Matthias Brauna4e932d2014-12-11 19:41:51 +0000288FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000289 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000290}
291
Matthias Braunb3aefc32016-02-15 19:25:31 +0000292bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
293 const {
294 MachineFunction &MF = const_cast<MachineFunction&>(*this);
295 unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
296 if (AbortOnErrors && FoundErrors)
297 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
298 return FoundErrors == 0;
Jakob Stoklund Olesen27440e72009-11-13 21:56:09 +0000299}
300
Matthias Braun80595462015-09-09 17:49:46 +0000301void MachineVerifier::verifySlotIndexes() const {
302 if (Indexes == nullptr)
303 return;
304
305 // Ensure the IdxMBB list is sorted by slot indexes.
306 SlotIndex Last;
307 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
308 E = Indexes->MBBIndexEnd(); I != E; ++I) {
309 assert(!Last.isValid() || I->first > Last);
310 Last = I->first;
311 }
312}
313
Derek Schuff42666ee2016-03-29 17:40:22 +0000314void MachineVerifier::verifyProperties(const MachineFunction &MF) {
315 // If a pass has introduced virtual registers without clearing the
316 // AllVRegsAllocated property (or set it without allocating the vregs)
317 // then report an error.
318 if (MF.getProperties().hasProperty(
319 MachineFunctionProperties::Property::AllVRegsAllocated) &&
320 MRI->getNumVirtRegs()) {
321 report(
322 "Function has AllVRegsAllocated property but there are VReg operands",
323 &MF);
324 }
325}
326
Matthias Braunb3aefc32016-02-15 19:25:31 +0000327unsigned MachineVerifier::verify(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000328 foundErrors = 0;
329
330 this->MF = &MF;
331 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000332 TII = MF.getSubtarget().getInstrInfo();
333 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000334 MRI = &MF.getRegInfo();
335
Ahmed Bougacha3681c772016-08-02 16:17:15 +0000336 isFunctionRegBankSelected = MF.getProperties().hasProperty(
337 MachineFunctionProperties::Property::RegBankSelected);
338
Craig Topperc0196b12014-04-14 00:51:57 +0000339 LiveVars = nullptr;
340 LiveInts = nullptr;
341 LiveStks = nullptr;
342 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000343 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000344 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000345 // We don't want to verify LiveVariables if LiveIntervals is available.
346 if (!LiveInts)
347 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000348 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000349 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000350 }
351
Matthias Braun80595462015-09-09 17:49:46 +0000352 verifySlotIndexes();
353
Derek Schuff42666ee2016-03-29 17:40:22 +0000354 verifyProperties(MF);
355
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000356 visitMachineFunctionBefore();
357 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
358 MFI!=MFE; ++MFI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000359 visitMachineBasicBlockBefore(&*MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000360 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000361 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000362 // Do we expect the next instruction to be part of the same bundle?
363 bool InBundle = false;
364
Evan Cheng7fae11b2011-12-14 02:11:42 +0000365 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
366 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000367 if (MBBI->getParent() != &*MFI) {
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000368 report("Bad instruction parent pointer", MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000369 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000370 continue;
371 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000372
373 // Check for consistent bundle flags.
374 if (InBundle && !MBBI->isBundledWithPred())
375 report("Missing BundledPred flag, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000376 "BundledSucc was set on predecessor",
377 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000378 if (!InBundle && MBBI->isBundledWithPred())
379 report("BundledPred flag is set, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000380 "but BundledSucc not set on predecessor",
381 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000382
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000383 // Is this a bundle header?
384 if (!MBBI->isInsideBundle()) {
385 if (CurBundle)
386 visitMachineBundleAfter(CurBundle);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000387 CurBundle = &*MBBI;
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000388 visitMachineBundleBefore(CurBundle);
389 } else if (!CurBundle)
390 report("No bundle header", MBBI);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000391 visitMachineInstrBefore(&*MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000392 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
393 const MachineInstr &MI = *MBBI;
394 const MachineOperand &Op = MI.getOperand(I);
395 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000396 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000397 // functions when replacing operands of a MachineInstr.
398 report("Instruction has operand with wrong parent set", &MI);
399 }
400
401 visitMachineOperand(&Op, I);
402 }
403
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000404 visitMachineInstrAfter(&*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000405
406 // Was this the last bundled instruction?
407 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000408 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000409 if (CurBundle)
410 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000411 if (InBundle)
412 report("BundledSucc flag set on last instruction in block", &MFI->back());
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000413 visitMachineBasicBlockAfter(&*MFI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000414 }
415 visitMachineFunctionAfter();
416
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000417 // Clean up.
418 regsLive.clear();
419 regsDefined.clear();
420 regsDead.clear();
421 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000422 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000423 regsLiveInButUnused.clear();
424 MBBInfoMap.clear();
425
Matthias Braunb3aefc32016-02-15 19:25:31 +0000426 return foundErrors;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000427}
428
Chris Lattner75f40452009-08-23 01:03:30 +0000429void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000430 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000431 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000432 if (!foundErrors++) {
433 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000434 errs() << "# " << Banner << '\n';
Matthias Braun42b4b632015-11-09 23:59:23 +0000435 if (LiveInts != nullptr)
436 LiveInts->print(errs());
437 else
438 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000439 }
Owen Anderson21b17882015-02-04 00:02:59 +0000440 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000441 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000442}
443
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000444void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000445 assert(MBB);
446 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000447 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000448 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000449 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000450 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000451 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000452 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000453 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000454}
455
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000456void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000457 assert(MI);
458 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000459 errs() << "- instruction: ";
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000460 if (Indexes && Indexes->hasIndex(*MI))
461 errs() << Indexes->getInstructionIndex(*MI) << '\t';
Matthias Braun45718db2015-11-09 23:59:25 +0000462 MI->print(errs(), /*SkipOpers=*/true);
Matthias Braun716b4332015-11-09 23:59:29 +0000463 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000464}
465
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000466void MachineVerifier::report(const char *msg,
467 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000468 assert(MO);
469 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000470 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000471 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000472 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000473}
474
Matthias Braun579c9cd2016-02-02 02:44:25 +0000475void MachineVerifier::report_context(SlotIndex Pos) const {
476 errs() << "- at: " << Pos << '\n';
477}
478
Matthias Braun7e624d52015-11-09 23:59:33 +0000479void MachineVerifier::report_context(const LiveInterval &LI) const {
Owen Anderson21b17882015-02-04 00:02:59 +0000480 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000481}
482
Matt Arsenault892fcd02016-07-25 19:39:01 +0000483void MachineVerifier::report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000484 LaneBitmask LaneMask) const {
Matthias Braun579c9cd2016-02-02 02:44:25 +0000485 report_context_liverange(LR);
Matt Arsenault892fcd02016-07-25 19:39:01 +0000486 report_context_vreg_regunit(VRegUnit);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000487 if (LaneMask != 0)
Matthias Braun1377fd62016-02-02 20:04:51 +0000488 report_context_lanemask(LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000489}
490
Matthias Braun7e624d52015-11-09 23:59:33 +0000491void MachineVerifier::report_context(const LiveRange::Segment &S) const {
492 errs() << "- segment: " << S << '\n';
493}
494
495void MachineVerifier::report_context(const VNInfo &VNI) const {
496 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
Matthias Braun364e6e92013-10-10 21:28:54 +0000497}
498
Matthias Braun579c9cd2016-02-02 02:44:25 +0000499void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
500 errs() << "- liverange: " << LR << '\n';
501}
502
Matthias Braun30668dd2016-05-11 21:31:39 +0000503void MachineVerifier::report_context_vreg(unsigned VReg) const {
504 errs() << "- v. register: " << PrintReg(VReg, TRI) << '\n';
505}
506
Matthias Braun1377fd62016-02-02 20:04:51 +0000507void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
508 if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
Matthias Braun30668dd2016-05-11 21:31:39 +0000509 report_context_vreg(VRegOrUnit);
Matthias Braun1377fd62016-02-02 20:04:51 +0000510 } else {
511 errs() << "- regunit: " << PrintRegUnit(VRegOrUnit, TRI) << '\n';
512 }
513}
514
515void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
516 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
517}
518
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000519void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000520 BBInfo &MInfo = MBBInfoMap[MBB];
521 if (!MInfo.reachable) {
522 MInfo.reachable = true;
523 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
524 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
525 markReachable(*SuI);
526 }
527}
528
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000529void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000530 lastIndex = SlotIndex();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000531 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000532
533 // A sub-register of a reserved register is also reserved
534 for (int Reg = regsReserved.find_first(); Reg>=0;
535 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000536 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000537 // FIXME: This should probably be:
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000538 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
539 regsReserved.set(*SubRegs);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000540 }
541 }
Lang Hames1ce837a2012-02-14 19:17:48 +0000542
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000543 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000544
545 // Build a set of the basic blocks in the function.
546 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000547 for (const auto &MBB : *MF) {
548 FunctionBlocks.insert(&MBB);
549 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000550
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000551 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
552 if (MInfo.Preds.size() != MBB.pred_size())
553 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000554
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000555 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
556 if (MInfo.Succs.size() != MBB.succ_size())
557 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000558 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000559
560 // Check that the register use lists are sane.
561 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000562
563 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000564}
565
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000566// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000567static bool matchPair(MachineBasicBlock::const_succ_iterator i,
568 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000569 if (*i == a)
570 return *++i == b;
571 if (*i == b)
572 return *++i == a;
573 return false;
574}
575
576void
577MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000578 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000579
Lang Hames1ce837a2012-02-14 19:17:48 +0000580 if (MRI->isSSA()) {
581 // If this block has allocatable physical registers live-in, check that
582 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000583 for (const auto &LI : MBB->liveins()) {
584 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +0000585 MBB->getIterator() != MBB->getParent()->begin()) {
Lang Hames1ce837a2012-02-14 19:17:48 +0000586 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
587 }
588 }
589 }
590
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000591 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000592 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000593 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000594 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000595 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000596 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000597 if (!FunctionBlocks.count(*I))
598 report("MBB has successor that isn't part of the function.", MBB);
599 if (!MBBInfoMap[*I].Preds.count(MBB)) {
600 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000601 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000602 << (*I)->getNumber() << ".\n";
603 }
604 }
605
606 // Check the predecessor list.
607 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
608 E = MBB->pred_end(); I != E; ++I) {
609 if (!FunctionBlocks.count(*I))
610 report("MBB has predecessor that isn't part of the function.", MBB);
611 if (!MBBInfoMap[*I].Succs.count(MBB)) {
612 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000613 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000614 << (*I)->getNumber() << ".\n";
615 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000616 }
Bill Wendling2a401312011-05-04 22:54:05 +0000617
618 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
619 const BasicBlock *BB = MBB->getBasicBlock();
Reid Kleckner64b003f2015-11-09 21:04:00 +0000620 const Function *Fn = MF->getFunction();
Bill Wendling2a401312011-05-04 22:54:05 +0000621 if (LandingPadSuccs.size() > 1 &&
622 !(AsmInfo &&
623 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
Reid Kleckner64b003f2015-11-09 21:04:00 +0000624 BB && isa<SwitchInst>(BB->getTerminator())) &&
625 !isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000626 report("MBB has more than one landing pad successor", MBB);
627
Dan Gohman352a4952009-08-27 02:43:49 +0000628 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000629 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000630 SmallVector<MachineOperand, 4> Cond;
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000631 if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
632 Cond)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000633 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
634 // check whether its answers match up with reality.
635 if (!TBB && !FBB) {
636 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000637 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000638 ++MBBI;
639 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000640 // It's possible that the block legitimately ends with a noreturn
641 // call or an unreachable, in which case it won't actually fall
642 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000643 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000644 // It's possible that the block legitimately ends with a noreturn
645 // call or an unreachable, in which case it won't actuall fall
646 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000647 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000648 report("MBB exits via unconditional fall-through but doesn't have "
649 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000650 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000651 report("MBB exits via unconditional fall-through but its successor "
652 "differs from its CFG successor!", MBB);
653 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000654 if (!MBB->empty() && MBB->back().isBarrier() &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000655 !TII->isPredicated(MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000656 report("MBB exits via unconditional fall-through but ends with a "
657 "barrier instruction!", MBB);
658 }
659 if (!Cond.empty()) {
660 report("MBB exits via unconditional fall-through but has a condition!",
661 MBB);
662 }
663 } else if (TBB && !FBB && Cond.empty()) {
664 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000665 // If the block has exactly one successor, that happens to be a
666 // landingpad, accept it as valid control flow.
667 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
668 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
669 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000670 report("MBB exits via unconditional branch but doesn't have "
671 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000672 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000673 report("MBB exits via unconditional branch but the CFG "
674 "successor doesn't match the actual successor!", MBB);
675 }
676 if (MBB->empty()) {
677 report("MBB exits via unconditional branch but doesn't contain "
678 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000679 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000680 report("MBB exits via unconditional branch but doesn't end with a "
681 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000682 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000683 report("MBB exits via unconditional branch but the branch isn't a "
684 "terminator instruction!", MBB);
685 }
686 } else if (TBB && !FBB && !Cond.empty()) {
687 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000688 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000689 ++MBBI;
690 if (MBBI == MF->end()) {
691 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000692 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000693 // A conditional branch with only one successor is weird, but allowed.
694 if (&*MBBI != TBB)
695 report("MBB exits via conditional branch/fall-through but only has "
696 "one CFG successor!", MBB);
697 else if (TBB != *MBB->succ_begin())
698 report("MBB exits via conditional branch/fall-through but the CFG "
699 "successor don't match the actual successor!", MBB);
700 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000701 report("MBB exits via conditional branch/fall-through but doesn't have "
702 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000703 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000704 report("MBB exits via conditional branch/fall-through but the CFG "
705 "successors don't match the actual successors!", MBB);
706 }
707 if (MBB->empty()) {
708 report("MBB exits via conditional branch/fall-through but doesn't "
709 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000710 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000711 report("MBB exits via conditional branch/fall-through but ends with a "
712 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000713 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000714 report("MBB exits via conditional branch/fall-through but the branch "
715 "isn't a terminator instruction!", MBB);
716 }
717 } else if (TBB && FBB) {
718 // Block conditionally branches somewhere, otherwise branches
719 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000720 if (MBB->succ_size() == 1) {
721 // A conditional branch with only one successor is weird, but allowed.
722 if (FBB != TBB)
723 report("MBB exits via conditional branch/branch through but only has "
724 "one CFG successor!", MBB);
725 else if (TBB != *MBB->succ_begin())
726 report("MBB exits via conditional branch/branch through but the CFG "
727 "successor don't match the actual successor!", MBB);
728 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000729 report("MBB exits via conditional branch/branch but doesn't have "
730 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000731 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000732 report("MBB exits via conditional branch/branch but the CFG "
733 "successors don't match the actual successors!", MBB);
734 }
735 if (MBB->empty()) {
736 report("MBB exits via conditional branch/branch but doesn't "
737 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000738 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000739 report("MBB exits via conditional branch/branch but doesn't end with a "
740 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000741 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000742 report("MBB exits via conditional branch/branch but the branch "
743 "isn't a terminator instruction!", MBB);
744 }
745 if (Cond.empty()) {
746 report("MBB exits via conditinal branch/branch but there's no "
747 "condition!", MBB);
748 }
749 } else {
750 report("AnalyzeBranch returned invalid data!", MBB);
751 }
752 }
753
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000754 regsLive.clear();
Matthias Braund9da1622015-09-09 18:08:03 +0000755 for (const auto &LI : MBB->liveins()) {
756 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000757 report("MBB live-in list contains non-physical register", MBB);
758 continue;
759 }
Matthias Braund9da1622015-09-09 18:08:03 +0000760 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
Chad Rosierabdb1d62013-05-22 23:17:36 +0000761 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000762 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000763 }
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +0000764 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000765
Matthias Braun941a7052016-07-28 18:40:00 +0000766 const MachineFrameInfo &MFI = MF->getFrameInfo();
767 BitVector PR = MFI.getPristineRegs(*MF);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000768 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000769 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
770 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000771 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000772 }
773
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000774 regsKilled.clear();
775 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000776
777 if (Indexes)
778 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000779}
780
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000781// This function gets called for all bundle headers, including normal
782// stand-alone unbundled instructions.
783void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000784 if (Indexes && Indexes->hasIndex(*MI)) {
785 SlotIndex idx = Indexes->getInstructionIndex(*MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000786 if (!(idx > lastIndex)) {
787 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000788 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000789 }
790 lastIndex = idx;
791 }
Pete Coopercd720162012-06-07 17:41:39 +0000792
793 // Ensure non-terminators don't follow terminators.
794 // Ignore predicated terminators formed by if conversion.
795 // FIXME: If conversion shouldn't need to violate this rule.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000796 if (MI->isTerminator() && !TII->isPredicated(*MI)) {
Pete Coopercd720162012-06-07 17:41:39 +0000797 if (!FirstTerminator)
798 FirstTerminator = MI;
799 } else if (FirstTerminator) {
800 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000801 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000802 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000803}
804
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000805// The operands on an INLINEASM instruction must follow a template.
806// Verify that the flag operands make sense.
807void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
808 // The first two operands on INLINEASM are the asm string and global flags.
809 if (MI->getNumOperands() < 2) {
810 report("Too few operands on inline asm", MI);
811 return;
812 }
813 if (!MI->getOperand(0).isSymbol())
814 report("Asm string must be an external symbol", MI);
815 if (!MI->getOperand(1).isImm())
816 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000817 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
Wei Ding0526e7f2016-06-22 18:51:08 +0000818 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
819 // and Extra_IsConvergent = 32.
820 if (!isUInt<6>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000821 report("Unknown asm flags", &MI->getOperand(1), 1);
822
Gabor Horvathfee04342015-03-16 09:53:42 +0000823 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000824
825 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
826 unsigned NumOps;
827 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
828 const MachineOperand &MO = MI->getOperand(OpNo);
829 // There may be implicit ops after the fixed operands.
830 if (!MO.isImm())
831 break;
832 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
833 }
834
835 if (OpNo > MI->getNumOperands())
836 report("Missing operands in last group", MI);
837
838 // An optional MDNode follows the groups.
839 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
840 ++OpNo;
841
842 // All trailing operands must be implicit registers.
843 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
844 const MachineOperand &MO = MI->getOperand(OpNo);
845 if (!MO.isReg() || !MO.isImplicit())
846 report("Expected implicit register after groups", &MO, OpNo);
847 }
848}
849
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000850void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000851 const MCInstrDesc &MCID = MI->getDesc();
852 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000853 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000854 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000855 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000856 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000857
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000858 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000859 if (MI->isInlineAsm())
860 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000861
Dan Gohmandb9493c2009-10-07 17:36:00 +0000862 // Check the MachineMemOperands for basic consistency.
863 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
864 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000865 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000866 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000867 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000868 report("Missing mayStore flag", MI);
869 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000870
871 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000872 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000873 if (LiveInts) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000874 bool mapped = !LiveInts->isNotInMIMap(*MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000875 if (MI->isDebugValue()) {
876 if (mapped)
877 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000878 } else if (MI->isInsideBundle()) {
879 if (mapped)
880 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000881 } else {
882 if (!mapped)
883 report("Missing slot index", MI);
884 }
885 }
886
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000887 // Check types.
888 const unsigned NumTypes = MI->getNumTypes();
889 if (isPreISelGenericOpcode(MCID.getOpcode())) {
890 if (NumTypes == 0)
891 report("Generic instruction must have a type", MI);
892 } else {
893 if (NumTypes != 0)
894 report("Non-generic instruction cannot have a type", MI);
895 }
896
Andrew Trick924123a2011-09-21 02:20:46 +0000897 StringRef ErrorInfo;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000898 if (!TII->verifyInstruction(*MI, ErrorInfo))
Andrew Trick924123a2011-09-21 02:20:46 +0000899 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000900}
901
902void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000903MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000904 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000905 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +0000906 unsigned NumDefs = MCID.getNumDefs();
907 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
908 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000909
Evan Cheng6cc775f2011-06-28 19:10:37 +0000910 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +0000911 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000912 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000913 if (!MO->isReg())
914 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +0000915 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000916 report("Explicit definition marked as use", MO, MONum);
917 else if (MO->isImplicit())
918 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000919 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000920 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +0000921 // Don't check if it's the last operand in a variadic instruction. See,
922 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000923 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000924 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000925 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +0000926 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000927 if (MO->isImplicit())
928 report("Explicit operand marked as implicit", MO, MONum);
929 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000930
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000931 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
932 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000933 if (!MO->isReg())
934 report("Tied use must be a register", MO, MONum);
935 else if (!MO->isTied())
936 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000937 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
938 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000939 } else if (MO->isReg() && MO->isTied())
940 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000941 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +0000942 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000943 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000944 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000945 }
946
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000947 switch (MO->getType()) {
948 case MachineOperand::MO_Register: {
949 const unsigned Reg = MO->getReg();
950 if (!Reg)
951 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000952 if (MRI->tracksLiveness() && !MI->isDebugValue())
953 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000954
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000955 // Verify the consistency of tied operands.
956 if (MO->isTied()) {
957 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
958 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
959 if (!OtherMO.isReg())
960 report("Must be tied to a register", MO, MONum);
961 if (!OtherMO.isTied())
962 report("Missing tie flags on tied operand", MO, MONum);
963 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
964 report("Inconsistent tie links", MO, MONum);
965 if (MONum < MCID.getNumDefs()) {
966 if (OtherIdx < MCID.getNumOperands()) {
967 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
968 report("Explicit def tied to explicit use without tie constraint",
969 MO, MONum);
970 } else {
971 if (!OtherMO.isImplicit())
972 report("Explicit def should be tied to implicit use", MO, MONum);
973 }
974 }
975 }
976
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +0000977 // Verify two-address constraints after leaving SSA form.
978 unsigned DefIdx;
979 if (!MRI->isSSA() && MO->isUse() &&
980 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
981 Reg != MI->getOperand(DefIdx).getReg())
982 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000983
984 // Check register classes.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000985 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000986 unsigned SubIdx = MO->getSubReg();
987
988 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000989 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000990 report("Illegal subregister index for physical register", MO, MONum);
991 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000992 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000993 if (const TargetRegisterClass *DRC =
994 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000995 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000996 report("Illegal physical register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000997 errs() << TRI->getName(Reg) << " is not a "
Craig Toppercf0444b2014-11-17 05:50:14 +0000998 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000999 }
1000 }
1001 } else {
1002 // Virtual register.
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001003 const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
1004 if (!RC) {
1005 // This is a generic virtual register.
1006 // It must have a size and it must not have a SubIdx.
1007 unsigned Size = MRI->getSize(Reg);
1008 if (!Size) {
1009 report("Generic virtual register must have a size", MO, MONum);
1010 return;
1011 }
Ahmed Bougacha3681c772016-08-02 16:17:15 +00001012
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001013 const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
Ahmed Bougacha3681c772016-08-02 16:17:15 +00001014
1015 // If we're post-RegBankSelect, the gvreg must have a bank.
1016 if (!RegBank && isFunctionRegBankSelected) {
1017 report("Generic virtual register must have a bank in a "
1018 "RegBankSelected function",
1019 MO, MONum);
1020 return;
1021 }
1022
1023 // Make sure the register fits into its register bank if any.
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001024 if (RegBank && RegBank->getSize() < Size) {
1025 report("Register bank is too small for virtual register", MO,
1026 MONum);
1027 errs() << "Register bank " << RegBank->getName() << " too small("
1028 << RegBank->getSize() << ") to fit " << Size << "-bits\n";
1029 return;
1030 }
1031 if (SubIdx) {
1032 report("Generic virtual register does not subregister index", MO, MONum);
1033 return;
1034 }
1035 break;
1036 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001037 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001038 const TargetRegisterClass *SRC =
1039 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001040 if (!SRC) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001041 report("Invalid subregister index for virtual register", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001042 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001043 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001044 return;
1045 }
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001046 if (RC != SRC) {
1047 report("Invalid register class for subregister index", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001048 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001049 << " does not fully support subreg index " << SubIdx << "\n";
1050 return;
1051 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001052 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001053 if (const TargetRegisterClass *DRC =
1054 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001055 if (SubIdx) {
1056 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +00001057 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001058 if (!SuperRC) {
1059 report("No largest legal super class exists.", MO, MONum);
1060 return;
1061 }
1062 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
1063 if (!DRC) {
1064 report("No matching super-reg register class.", MO, MONum);
1065 return;
1066 }
1067 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +00001068 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001069 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001070 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +00001071 << " register, but got a " << TRI->getRegClassName(RC)
1072 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001073 }
1074 }
1075 }
1076 }
1077 break;
1078 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001079
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001080 case MachineOperand::MO_RegisterMask:
1081 regMasks.push_back(MO->getRegMask());
1082 break;
1083
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001084 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +00001085 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
1086 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001087 break;
1088
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001089 case MachineOperand::MO_FrameIndex:
1090 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001091 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
Jonas Paulsson72640f12015-10-29 08:28:35 +00001092 int FI = MO->getIndex();
1093 LiveInterval &LI = LiveStks->getInterval(FI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001094 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001095
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001096 bool stores = MI->mayStore();
Jonas Paulsson72640f12015-10-29 08:28:35 +00001097 bool loads = MI->mayLoad();
1098 // For a memory-to-memory move, we need to check if the frame
1099 // index is used for storing or loading, by inspecting the
1100 // memory operands.
1101 if (stores && loads) {
1102 for (auto *MMO : MI->memoperands()) {
1103 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1104 if (PSV == nullptr) continue;
1105 const FixedStackPseudoSourceValue *Value =
1106 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1107 if (Value == nullptr) continue;
1108 if (Value->getFrameIndex() != FI) continue;
1109
1110 if (MMO->isStore())
1111 loads = false;
1112 else
1113 stores = false;
1114 break;
1115 }
1116 if (loads == stores)
1117 report("Missing fixed stack memoperand.", MI);
1118 }
1119 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001120 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001121 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001122 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001123 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001124 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001125 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001126 }
1127 }
1128 break;
1129
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001130 default:
1131 break;
1132 }
1133}
1134
Matthias Braun1377fd62016-02-02 20:04:51 +00001135void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
1136 unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
1137 LaneBitmask LaneMask) {
1138 LiveQueryResult LRQ = LR.Query(UseIdx);
1139 // Check if we have a segment at the use, note however that we only need one
1140 // live subregister range, the others may be dead.
1141 if (!LRQ.valueIn() && LaneMask == 0) {
1142 report("No live segment at use", MO, MONum);
1143 report_context_liverange(LR);
1144 report_context_vreg_regunit(VRegOrUnit);
1145 report_context(UseIdx);
1146 }
1147 if (MO->isKill() && !LRQ.isKill()) {
1148 report("Live range continues after kill flag", MO, MONum);
1149 report_context_liverange(LR);
1150 report_context_vreg_regunit(VRegOrUnit);
1151 if (LaneMask != 0)
1152 report_context_lanemask(LaneMask);
1153 report_context(UseIdx);
1154 }
1155}
1156
1157void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
1158 unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
1159 LaneBitmask LaneMask) {
1160 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
1161 assert(VNI && "NULL valno is not allowed");
1162 if (VNI->def != DefIdx) {
1163 report("Inconsistent valno->def", MO, MONum);
1164 report_context_liverange(LR);
1165 report_context_vreg_regunit(VRegOrUnit);
1166 if (LaneMask != 0)
1167 report_context_lanemask(LaneMask);
1168 report_context(*VNI);
1169 report_context(DefIdx);
1170 }
1171 } else {
1172 report("No live segment at def", MO, MONum);
1173 report_context_liverange(LR);
1174 report_context_vreg_regunit(VRegOrUnit);
1175 if (LaneMask != 0)
1176 report_context_lanemask(LaneMask);
1177 report_context(DefIdx);
1178 }
1179 // Check that, if the dead def flag is present, LiveInts agree.
1180 if (MO->isDead()) {
1181 LiveQueryResult LRQ = LR.Query(DefIdx);
1182 if (!LRQ.isDeadDef()) {
1183 // In case of physregs we can have a non-dead definition on another
1184 // operand.
1185 bool otherDef = false;
1186 if (!TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
1187 const MachineInstr &MI = *MO->getParent();
1188 for (const MachineOperand &MO : MI.operands()) {
1189 if (!MO.isReg() || !MO.isDef() || MO.isDead())
1190 continue;
1191 unsigned Reg = MO.getReg();
1192 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1193 if (*Units == VRegOrUnit) {
1194 otherDef = true;
1195 break;
1196 }
1197 }
1198 }
1199 }
1200
1201 if (!otherDef) {
1202 report("Live range continues after dead def flag", MO, MONum);
1203 report_context_liverange(LR);
1204 report_context_vreg_regunit(VRegOrUnit);
1205 if (LaneMask != 0)
1206 report_context_lanemask(LaneMask);
1207 }
1208 }
1209 }
1210}
1211
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001212void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1213 const MachineInstr *MI = MO->getParent();
1214 const unsigned Reg = MO->getReg();
1215
1216 // Both use and def operands can read a register.
1217 if (MO->readsReg()) {
1218 regsLiveInButUnused.erase(Reg);
1219
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001220 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001221 addRegWithSubRegs(regsKilled, Reg);
1222
1223 // Check that LiveVars knows this kill.
1224 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1225 MO->isKill()) {
1226 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1227 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
1228 report("Kill missing from LiveVariables", MO, MONum);
1229 }
1230
1231 // Check LiveInts liveness and kill.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001232 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1233 SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001234 // Check the cached regunit intervals.
1235 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1236 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001237 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
1238 checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001239 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001240 }
1241
1242 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1243 if (LiveInts->hasInterval(Reg)) {
1244 // This is a virtual register interval.
1245 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun1377fd62016-02-02 20:04:51 +00001246 checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
1247
1248 if (LI.hasSubRanges() && !MO->isDef()) {
1249 unsigned SubRegIdx = MO->getSubReg();
1250 LaneBitmask MOMask = SubRegIdx != 0
1251 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1252 : MRI->getMaxLaneMaskForVReg(Reg);
1253 LaneBitmask LiveInMask = 0;
1254 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1255 if ((MOMask & SR.LaneMask) == 0)
1256 continue;
1257 checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
1258 LiveQueryResult LRQ = SR.Query(UseIdx);
1259 if (LRQ.valueIn())
1260 LiveInMask |= SR.LaneMask;
1261 }
1262 // At least parts of the register has to be live at the use.
1263 if ((LiveInMask & MOMask) == 0) {
1264 report("No live subrange at use", MO, MONum);
1265 report_context(LI);
1266 report_context(UseIdx);
1267 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001268 }
1269 } else {
1270 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001271 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001272 }
1273 }
1274
1275 // Use of a dead register.
1276 if (!regsLive.count(Reg)) {
1277 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1278 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001279 bool Bad = !isReserved(Reg);
1280 // We are fine if just any subregister has a defined value.
1281 if (Bad) {
1282 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1283 ++SubRegs) {
1284 if (regsLive.count(*SubRegs)) {
1285 Bad = false;
1286 break;
1287 }
1288 }
1289 }
Matthias Braun96a31952015-01-14 22:25:14 +00001290 // If there is an additional implicit-use of a super register we stop
1291 // here. By definition we are fine if the super register is not
1292 // (completely) dead, if the complete super register is dead we will
1293 // get a report for its operand.
1294 if (Bad) {
1295 for (const MachineOperand &MOP : MI->uses()) {
1296 if (!MOP.isReg())
1297 continue;
1298 if (!MOP.isImplicit())
1299 continue;
1300 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1301 ++SubRegs) {
1302 if (*SubRegs == Reg) {
1303 Bad = false;
1304 break;
1305 }
1306 }
1307 }
1308 }
Matthias Braun96d77322014-12-10 01:13:13 +00001309 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001310 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001311 } else if (MRI->def_empty(Reg)) {
1312 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001313 } else {
1314 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1315 // We don't know which virtual registers are live in, so only complain
1316 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1317 // must be live in. PHI instructions are handled separately.
1318 if (MInfo.regsKilled.count(Reg))
1319 report("Using a killed virtual register", MO, MONum);
1320 else if (!MI->isPHI())
1321 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1322 }
1323 }
1324 }
1325
1326 if (MO->isDef()) {
1327 // Register defined.
1328 // TODO: verify that earlyclobber ops are not used.
1329 if (MO->isDead())
1330 addRegWithSubRegs(regsDead, Reg);
1331 else
1332 addRegWithSubRegs(regsDefined, Reg);
1333
1334 // Verify SSA form.
1335 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001336 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001337 report("Multiple virtual register defs in SSA form", MO, MONum);
1338
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001339 // Check LiveInts for a live segment, but only for virtual registers.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001340 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1341 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001342 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Matthias Braun1377fd62016-02-02 20:04:51 +00001343
1344 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1345 if (LiveInts->hasInterval(Reg)) {
1346 const LiveInterval &LI = LiveInts->getInterval(Reg);
1347 checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
1348
1349 if (LI.hasSubRanges()) {
1350 unsigned SubRegIdx = MO->getSubReg();
1351 LaneBitmask MOMask = SubRegIdx != 0
1352 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1353 : MRI->getMaxLaneMaskForVReg(Reg);
1354 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1355 if ((SR.LaneMask & MOMask) == 0)
1356 continue;
1357 checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, SR.LaneMask);
1358 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001359 }
1360 } else {
Matthias Braun1377fd62016-02-02 20:04:51 +00001361 report("Virtual register has no Live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001362 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001363 }
1364 }
1365 }
1366}
1367
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001368void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001369}
1370
1371// This function gets called after visiting all instructions in a bundle. The
1372// argument points to the bundle header.
1373// Normal stand-alone instructions are also considered 'bundles', and this
1374// function is called for all of them.
1375void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001376 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1377 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001378 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001379 // Kill any masked registers.
1380 while (!regMasks.empty()) {
1381 const uint32_t *Mask = regMasks.pop_back_val();
1382 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1383 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1384 MachineOperand::clobbersPhysReg(Mask, *I))
1385 regsDead.push_back(*I);
1386 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001387 set_subtract(regsLive, regsDead); regsDead.clear();
1388 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001389}
1390
1391void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001392MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001393 MBBInfoMap[MBB].regsLiveOut = regsLive;
1394 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001395
1396 if (Indexes) {
1397 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1398 if (!(stop > lastIndex)) {
1399 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001400 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001401 << " last instruction was at " << lastIndex << '\n';
1402 }
1403 lastIndex = stop;
1404 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001405}
1406
1407// Calculate the largest possible vregsPassed sets. These are the registers that
1408// can pass through an MBB live, but may not be live every time. It is assumed
1409// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001410void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001411 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1412 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001413 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001414 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001415 BBInfo &MInfo = MBBInfoMap[&MBB];
1416 if (!MInfo.reachable)
1417 continue;
1418 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1419 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1420 BBInfo &SInfo = MBBInfoMap[*SuI];
1421 if (SInfo.addPassed(MInfo.regsLiveOut))
1422 todo.insert(*SuI);
1423 }
1424 }
1425
1426 // Iteratively push vregsPassed to successors. This will converge to the same
1427 // final state regardless of DenseSet iteration order.
1428 while (!todo.empty()) {
1429 const MachineBasicBlock *MBB = *todo.begin();
1430 todo.erase(MBB);
1431 BBInfo &MInfo = MBBInfoMap[MBB];
1432 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1433 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1434 if (*SuI == MBB)
1435 continue;
1436 BBInfo &SInfo = MBBInfoMap[*SuI];
1437 if (SInfo.addPassed(MInfo.vregsPassed))
1438 todo.insert(*SuI);
1439 }
1440 }
1441}
1442
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001443// Calculate the set of virtual registers that must be passed through each basic
1444// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001445// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001446void MachineVerifier::calcRegsRequired() {
1447 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001448 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001449 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001450 BBInfo &MInfo = MBBInfoMap[&MBB];
1451 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1452 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1453 BBInfo &PInfo = MBBInfoMap[*PrI];
1454 if (PInfo.addRequired(MInfo.vregsLiveIn))
1455 todo.insert(*PrI);
1456 }
1457 }
1458
1459 // Iteratively push vregsRequired to predecessors. This will converge to the
1460 // same final state regardless of DenseSet iteration order.
1461 while (!todo.empty()) {
1462 const MachineBasicBlock *MBB = *todo.begin();
1463 todo.erase(MBB);
1464 BBInfo &MInfo = MBBInfoMap[MBB];
1465 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1466 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1467 if (*PrI == MBB)
1468 continue;
1469 BBInfo &SInfo = MBBInfoMap[*PrI];
1470 if (SInfo.addRequired(MInfo.vregsRequired))
1471 todo.insert(*PrI);
1472 }
1473 }
1474}
1475
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001476// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001477// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001478void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001479 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001480 for (const auto &BBI : *MBB) {
1481 if (!BBI.isPHI())
1482 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001483 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001484
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001485 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1486 unsigned Reg = BBI.getOperand(i).getReg();
1487 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001488 if (!Pre->isSuccessor(MBB))
1489 continue;
1490 seen.insert(Pre);
1491 BBInfo &PrInfo = MBBInfoMap[Pre];
1492 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1493 report("PHI operand is not live-out from predecessor",
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001494 &BBI.getOperand(i), i);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001495 }
1496
1497 // Did we see all predecessors?
1498 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1499 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1500 if (!seen.count(*PrI)) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001501 report("Missing PHI operand", &BBI);
Owen Anderson21b17882015-02-04 00:02:59 +00001502 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001503 << " is a predecessor according to the CFG.\n";
1504 }
1505 }
1506 }
1507}
1508
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001509void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001510 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001511
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001512 for (const auto &MBB : *MF) {
1513 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001514
1515 // Skip unreachable MBBs.
1516 if (!MInfo.reachable)
1517 continue;
1518
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001519 checkPHIOps(&MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001520 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001521
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001522 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001523 calcRegsRequired();
1524
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001525 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001526 for (const auto &MBB : *MF) {
1527 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001528 for (RegSet::iterator
1529 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1530 ++I)
1531 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001532 report("Virtual register killed in block, but needed live out.", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001533 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001534 << " is used after the block.\n";
1535 }
1536 }
1537
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001538 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001539 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1540 for (RegSet::iterator
1541 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Matthias Braun30668dd2016-05-11 21:31:39 +00001542 ++I) {
1543 report("Virtual register defs don't dominate all uses.", MF);
1544 report_context_vreg(*I);
1545 }
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001546 }
1547
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001548 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001549 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001550 if (LiveInts)
1551 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001552}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001553
1554void MachineVerifier::verifyLiveVariables() {
1555 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001556 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1557 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001558 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001559 for (const auto &MBB : *MF) {
1560 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001561
1562 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1563 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001564 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1565 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001566 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001567 << " must be live through the block.\n";
1568 }
1569 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001570 if (VI.AliveBlocks.test(MBB.getNumber())) {
1571 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001572 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001573 << " is not needed live through the block.\n";
1574 }
1575 }
1576 }
1577 }
1578}
1579
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001580void MachineVerifier::verifyLiveIntervals() {
1581 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001582 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1583 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001584
1585 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001586 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001587 continue;
1588
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001589 if (!LiveInts->hasInterval(Reg)) {
1590 report("Missing live interval for virtual register", MF);
Owen Anderson21b17882015-02-04 00:02:59 +00001591 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001592 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001593 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001594
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001595 const LiveInterval &LI = LiveInts->getInterval(Reg);
1596 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001597 verifyLiveInterval(LI);
1598 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001599
1600 // Verify all the cached regunit intervals.
1601 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001602 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1603 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001604}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001605
Matthias Braun364e6e92013-10-10 21:28:54 +00001606void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001607 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001608 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001609 if (VNI->isUnused())
1610 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001611
Matthias Braun364e6e92013-10-10 21:28:54 +00001612 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001613
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001614 if (!DefVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001615 report("Value not live at VNInfo def and not marked unused", MF);
1616 report_context(LR, Reg, LaneMask);
1617 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001618 return;
1619 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001620
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001621 if (DefVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001622 report("Live segment at def has different VNInfo", MF);
1623 report_context(LR, Reg, LaneMask);
1624 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001625 return;
1626 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001627
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001628 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1629 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001630 report("Invalid VNInfo definition index", MF);
1631 report_context(LR, Reg, LaneMask);
1632 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001633 return;
1634 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001635
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001636 if (VNI->isPHIDef()) {
1637 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001638 report("PHIDef VNInfo is not defined at MBB start", MBB);
1639 report_context(LR, Reg, LaneMask);
1640 report_context(*VNI);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001641 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001642 return;
1643 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001644
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001645 // Non-PHI def.
1646 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1647 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001648 report("No instruction at VNInfo def index", MBB);
1649 report_context(LR, Reg, LaneMask);
1650 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001651 return;
1652 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001653
Matthias Braun364e6e92013-10-10 21:28:54 +00001654 if (Reg != 0) {
1655 bool hasDef = false;
1656 bool isEarlyClobber = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001657 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001658 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001659 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001660 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1661 if (MOI->getReg() != Reg)
1662 continue;
1663 } else {
1664 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1665 !TRI->hasRegUnit(MOI->getReg(), Reg))
1666 continue;
1667 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001668 if (LaneMask != 0 &&
1669 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask) == 0)
1670 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001671 hasDef = true;
1672 if (MOI->isEarlyClobber())
1673 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001674 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001675
Matthias Braun364e6e92013-10-10 21:28:54 +00001676 if (!hasDef) {
1677 report("Defining instruction does not modify register", MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001678 report_context(LR, Reg, LaneMask);
1679 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001680 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001681
Matthias Braun364e6e92013-10-10 21:28:54 +00001682 // Early clobber defs begin at USE slots, but other defs must begin at
1683 // DEF slots.
1684 if (isEarlyClobber) {
1685 if (!VNI->def.isEarlyClobber()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001686 report("Early clobber def must be at an early-clobber slot", MBB);
1687 report_context(LR, Reg, LaneMask);
1688 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001689 }
1690 } else if (!VNI->def.isRegister()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001691 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
1692 report_context(LR, Reg, LaneMask);
1693 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001694 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001695 }
1696}
1697
Matthias Braun364e6e92013-10-10 21:28:54 +00001698void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1699 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001700 unsigned Reg, LaneBitmask LaneMask)
1701{
Matthias Braun364e6e92013-10-10 21:28:54 +00001702 const LiveRange::Segment &S = *I;
1703 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001704 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001705
Matthias Braun364e6e92013-10-10 21:28:54 +00001706 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001707 report("Foreign valno in live segment", MF);
1708 report_context(LR, Reg, LaneMask);
1709 report_context(S);
1710 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001711 }
1712
1713 if (VNI->isUnused()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001714 report("Live segment valno is marked unused", MF);
1715 report_context(LR, Reg, LaneMask);
1716 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001717 }
1718
Matthias Braun364e6e92013-10-10 21:28:54 +00001719 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001720 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001721 report("Bad start of live segment, no basic block", MF);
1722 report_context(LR, Reg, LaneMask);
1723 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001724 return;
1725 }
1726 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001727 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001728 report("Live segment must begin at MBB entry or valno def", MBB);
1729 report_context(LR, Reg, LaneMask);
1730 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001731 }
1732
1733 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001734 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001735 if (!EndMBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001736 report("Bad end of live segment, no basic block", MF);
1737 report_context(LR, Reg, LaneMask);
1738 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001739 return;
1740 }
1741
1742 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001743 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001744 return;
1745
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001746 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001747 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1748 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001749 return;
1750
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001751 // The live segment is ending inside EndMBB
1752 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001753 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001754 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001755 report("Live segment doesn't end at a valid instruction", EndMBB);
1756 report_context(LR, Reg, LaneMask);
1757 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001758 return;
1759 }
1760
1761 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001762 if (S.end.isBlock()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001763 report("Live segment ends at B slot of an instruction", EndMBB);
1764 report_context(LR, Reg, LaneMask);
1765 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001766 }
1767
Matthias Braun364e6e92013-10-10 21:28:54 +00001768 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001769 // Segment ends on the dead slot.
1770 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001771 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001772 report("Live segment ending at dead slot spans instructions", EndMBB);
1773 report_context(LR, Reg, LaneMask);
1774 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001775 }
1776 }
1777
1778 // A live segment can only end at an early-clobber slot if it is being
1779 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001780 if (S.end.isEarlyClobber()) {
1781 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001782 report("Live segment ending at early clobber slot must be "
Matthias Braun7e624d52015-11-09 23:59:33 +00001783 "redefined by an EC def in the same instruction", EndMBB);
1784 report_context(LR, Reg, LaneMask);
1785 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001786 }
1787 }
1788
1789 // The following checks only apply to virtual registers. Physreg liveness
1790 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001791 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001792 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001793 // use, or a dead flag on a def.
1794 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001795 bool hasSubRegDef = false;
Matthias Braun72a58c32016-03-29 19:07:43 +00001796 bool hasDeadDef = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001797 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001798 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001799 continue;
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001800 if (LaneMask != 0 &&
1801 (LaneMask & TRI->getSubRegIndexLaneMask(MOI->getSubReg())) == 0)
1802 continue;
Matthias Braun72a58c32016-03-29 19:07:43 +00001803 if (MOI->isDef()) {
1804 if (MOI->getSubReg() != 0)
1805 hasSubRegDef = true;
1806 if (MOI->isDead())
1807 hasDeadDef = true;
1808 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001809 if (MOI->readsReg())
1810 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001811 }
Matthias Braun72a58c32016-03-29 19:07:43 +00001812 if (S.end.isDead()) {
1813 // Make sure that the corresponding machine operand for a "dead" live
1814 // range has the dead flag. We cannot perform this check for subregister
1815 // liveranges as partially dead values are allowed.
1816 if (LaneMask == 0 && !hasDeadDef) {
1817 report("Instruction ending live segment on dead slot has no dead flag",
1818 MI);
1819 report_context(LR, Reg, LaneMask);
1820 report_context(S);
1821 }
1822 } else {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001823 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001824 // When tracking subregister liveness, the main range must start new
1825 // values on partial register writes, even if there is no read.
Matthias Brauna25e13a2015-03-19 00:21:58 +00001826 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask != 0 ||
1827 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001828 report("Instruction ending live segment doesn't read the register",
1829 MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001830 report_context(LR, Reg, LaneMask);
1831 report_context(S);
Matthias Braun21554d92014-12-10 01:13:11 +00001832 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001833 }
1834 }
1835 }
1836
1837 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001838 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001839 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00001840 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001841 // Not live-in to any blocks.
1842 if (MBB == EndMBB)
1843 return;
1844 // Skip this block.
1845 ++MFI;
1846 }
1847 for (;;) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001848 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001849 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00001850 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00001851 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001852 if (&*MFI == EndMBB)
1853 break;
1854 ++MFI;
1855 continue;
1856 }
1857
1858 // Is VNI a PHI-def in the current block?
1859 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001860 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001861
1862 // Check that VNI is live-out of all predecessors.
1863 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1864 PE = MFI->pred_end(); PI != PE; ++PI) {
1865 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001866 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001867
Matthias Braune29b7682016-05-20 23:02:13 +00001868 // All predecessors must have a live-out value if this is not a
1869 // subregister liverange.
1870 if (!PVNI && LaneMask == 0) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001871 report("Register not marked live out of predecessor", *PI);
1872 report_context(LR, Reg, LaneMask);
1873 report_context(*VNI);
1874 errs() << " live into BB#" << MFI->getNumber()
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001875 << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
1876 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001877 continue;
1878 }
1879
1880 // Only PHI-defs can take different predecessor values.
1881 if (!IsPHI && PVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001882 report("Different value live out of predecessor", *PI);
1883 report_context(LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001884 errs() << "Valno #" << PVNI->id << " live out of BB#"
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001885 << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
1886 << " live into BB#" << MFI->getNumber() << '@'
1887 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001888 }
1889 }
1890 if (&*MFI == EndMBB)
1891 break;
1892 ++MFI;
1893 }
1894}
1895
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001896void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001897 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00001898 for (const VNInfo *VNI : LR.valnos)
1899 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001900
Matthias Braun364e6e92013-10-10 21:28:54 +00001901 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001902 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00001903}
1904
1905void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001906 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00001907 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1908 verifyLiveRange(LI, Reg);
1909
Matthias Braune6a24852015-09-25 21:51:14 +00001910 LaneBitmask Mask = 0;
1911 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00001912 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001913 if ((Mask & SR.LaneMask) != 0) {
1914 report("Lane masks of sub ranges overlap in live interval", MF);
1915 report_context(LI);
1916 }
1917 if ((SR.LaneMask & ~MaxMask) != 0) {
1918 report("Subrange lanemask is invalid", MF);
1919 report_context(LI);
1920 }
1921 if (SR.empty()) {
1922 report("Subrange must not be empty", MF);
1923 report_context(SR, LI.reg, SR.LaneMask);
1924 }
Matthias Braune962e522015-03-25 21:18:22 +00001925 Mask |= SR.LaneMask;
1926 verifyLiveRange(SR, LI.reg, SR.LaneMask);
Matthias Braun7e624d52015-11-09 23:59:33 +00001927 if (!LI.covers(SR)) {
1928 report("A Subrange is not covered by the main range", MF);
1929 report_context(LI);
1930 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001931 }
1932
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001933 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00001934 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
Matthias Braunbf47f632016-01-08 01:16:35 +00001935 unsigned NumComp = ConEQ.Classify(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001936 if (NumComp > 1) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001937 report("Multiple connected components in live interval", MF);
1938 report_context(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001939 for (unsigned comp = 0; comp != NumComp; ++comp) {
1940 errs() << comp << ": valnos";
1941 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1942 E = LI.vni_end(); I!=E; ++I)
1943 if (comp == ConEQ.getEqClass(*I))
1944 errs() << ' ' << (*I)->id;
1945 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00001946 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001947 }
1948}
Manman Renaa6875b2013-07-15 21:26:31 +00001949
1950namespace {
1951 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1952 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1953 // value is zero.
1954 // We use a bool plus an integer to capture the stack state.
1955 struct StackStateOfBB {
1956 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1957 ExitIsSetup(false) { }
1958 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1959 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1960 ExitIsSetup(ExitSetup) { }
1961 // Can be negative, which means we are setting up a frame.
1962 int EntryValue;
1963 int ExitValue;
1964 bool EntryIsSetup;
1965 bool ExitIsSetup;
1966 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001967}
Manman Renaa6875b2013-07-15 21:26:31 +00001968
1969/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1970/// by a FrameDestroy <n>, stack adjustments are identical on all
1971/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1972void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001973 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1974 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Manman Renaa6875b2013-07-15 21:26:31 +00001975
1976 SmallVector<StackStateOfBB, 8> SPState;
1977 SPState.resize(MF->getNumBlockIDs());
1978 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1979
1980 // Visit the MBBs in DFS order.
1981 for (df_ext_iterator<const MachineFunction*,
1982 SmallPtrSet<const MachineBasicBlock*, 8> >
1983 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1984 DFI != DFE; ++DFI) {
1985 const MachineBasicBlock *MBB = *DFI;
1986
1987 StackStateOfBB BBState;
1988 // Check the exit state of the DFS stack predecessor.
1989 if (DFI.getPathLength() >= 2) {
1990 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1991 assert(Reachable.count(StackPred) &&
1992 "DFS stack predecessor is already visited.\n");
1993 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1994 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1995 BBState.ExitValue = BBState.EntryValue;
1996 BBState.ExitIsSetup = BBState.EntryIsSetup;
1997 }
1998
1999 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002000 for (const auto &I : *MBB) {
2001 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00002002 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002003 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00002004 assert(Size >= 0 &&
2005 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
2006
2007 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002008 report("FrameSetup is after another FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00002009 BBState.ExitValue -= Size;
2010 BBState.ExitIsSetup = true;
2011 }
2012
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002013 if (I.getOpcode() == FrameDestroyOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00002014 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002015 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00002016 assert(Size >= 0 &&
2017 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
2018
2019 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002020 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00002021 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
2022 BBState.ExitValue;
2023 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002024 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00002025 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00002026 << AbsSPAdj << ">.\n";
2027 }
2028 BBState.ExitValue += Size;
2029 BBState.ExitIsSetup = false;
2030 }
2031 }
2032 SPState[MBB->getNumber()] = BBState;
2033
2034 // Make sure the exit state of any predecessor is consistent with the entry
2035 // state.
2036 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
2037 E = MBB->pred_end(); I != E; ++I) {
2038 if (Reachable.count(*I) &&
2039 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
2040 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
2041 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002042 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002043 << SPState[(*I)->getNumber()].ExitValue << ", "
2044 << SPState[(*I)->getNumber()].ExitIsSetup
2045 << "), while BB#" << MBB->getNumber() << " has entry state ("
2046 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
2047 }
2048 }
2049
2050 // Make sure the entry state of any successor is consistent with the exit
2051 // state.
2052 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
2053 E = MBB->succ_end(); I != E; ++I) {
2054 if (Reachable.count(*I) &&
2055 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
2056 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
2057 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002058 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002059 << SPState[(*I)->getNumber()].EntryValue << ", "
2060 << SPState[(*I)->getNumber()].EntryIsSetup
2061 << "), while BB#" << MBB->getNumber() << " has exit state ("
2062 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
2063 }
2064 }
2065
2066 // Make sure a basic block with return ends with zero stack adjustment.
2067 if (!MBB->empty() && MBB->back().isReturn()) {
2068 if (BBState.ExitIsSetup)
2069 report("A return block ends with a FrameSetup.", MBB);
2070 if (BBState.ExitValue)
2071 report("A return block ends with a nonzero stack adjustment.", MBB);
2072 }
2073 }
2074}