blob: f06394279c957a9881e47d0466316d2846698ed4 [file] [log] [blame]
Bill Wendling68caaaf2010-08-19 18:52:17 +00001//===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Pass to verify generated machine code. The following is checked:
11//
12// Operand counts: All explicit operands must be present.
13//
14// Register classes: All physical and virtual register operands must be
15// compatible with the register class required by the instruction descriptor.
16//
17// Register live intervals: Registers must be defined only once, and must be
18// defined before use.
19//
20// The machine code verifier is enabled from LLVMTargetMachine.cpp with the
21// command-line option -verify-machineinstrs, or by defining the environment
22// variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
23// the verifier errors.
24//===----------------------------------------------------------------------===//
25
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000026#include "llvm/CodeGen/Passes.h"
Chris Lattner565449d2009-08-23 03:13:20 +000027#include "llvm/ADT/DenseSet.h"
Manman Renaa6875b2013-07-15 21:26:31 +000028#include "llvm/ADT/DepthFirstIterator.h"
Chris Lattner565449d2009-08-23 03:13:20 +000029#include "llvm/ADT/SetOperations.h"
30#include "llvm/ADT/SmallVector.h"
David Majnemer70497c62015-12-02 23:06:39 +000031#include "llvm/Analysis/EHPersonalities.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/CodeGen/LiveIntervalAnalysis.h"
33#include "llvm/CodeGen/LiveStackAnalysis.h"
34#include "llvm/CodeGen/LiveVariables.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/CodeGen/MachineMemOperand.h"
38#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/InlineAsm.h"
41#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000043#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000044#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000045#include "llvm/Support/FileSystem.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000046#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Target/TargetInstrInfo.h"
48#include "llvm/Target/TargetMachine.h"
49#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000050#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000051using namespace llvm;
52
53namespace {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000054 struct MachineVerifier {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000055
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000056 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000057 PASS(pass),
Owen Anderson21b17882015-02-04 00:02:59 +000058 Banner(b)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000059 {}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000060
Matthias Braunb3aefc32016-02-15 19:25:31 +000061 unsigned verify(MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000062
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000063 Pass *const PASS;
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000064 const char *Banner;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000065 const MachineFunction *MF;
66 const TargetMachine *TM;
Evan Cheng8d71a752011-06-27 21:26:13 +000067 const TargetInstrInfo *TII;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000068 const TargetRegisterInfo *TRI;
69 const MachineRegisterInfo *MRI;
70
71 unsigned foundErrors;
72
Ahmed Bougacha3681c772016-08-02 16:17:15 +000073 // Avoid querying the MachineFunctionProperties for each operand.
74 bool isFunctionRegBankSelected;
Ahmed Bougachab14e9442016-08-02 16:49:22 +000075 bool isFunctionSelected;
Ahmed Bougacha3681c772016-08-02 16:17:15 +000076
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000077 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000078 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000079 typedef DenseSet<unsigned> RegSet;
80 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000081 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000082
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000083 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000084 BlockSet FunctionBlocks;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000085
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000086 BitVector regsReserved;
87 RegSet regsLive;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000088 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000089 RegMaskVector regMasks;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000090 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000091
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +000092 SlotIndex lastIndex;
93
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000094 // Add Reg and any sub-registers to RV
95 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
96 RV.push_back(Reg);
97 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +000098 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
99 RV.push_back(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000100 }
101
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000102 struct BBInfo {
103 // Is this MBB reachable from the MF entry point?
104 bool reachable;
105
106 // Vregs that must be live in because they are used without being
107 // defined. Map value is the user.
108 RegMap vregsLiveIn;
109
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000110 // Regs killed in MBB. They may be defined again, and will then be in both
111 // regsKilled and regsLiveOut.
112 RegSet regsKilled;
113
114 // Regs defined in MBB and live out. Note that vregs passing through may
115 // be live out without being mentioned here.
116 RegSet regsLiveOut;
117
118 // Vregs that pass through MBB untouched. This set is disjoint from
119 // regsKilled and regsLiveOut.
120 RegSet vregsPassed;
121
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000122 // Vregs that must pass through MBB because they are needed by a successor
123 // block. This set is disjoint from regsLiveOut.
124 RegSet vregsRequired;
125
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000126 // Set versions of block's predecessor and successor lists.
127 BlockSet Preds, Succs;
128
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000129 BBInfo() : reachable(false) {}
130
131 // Add register to vregsPassed if it belongs there. Return true if
132 // anything changed.
133 bool addPassed(unsigned Reg) {
134 if (!TargetRegisterInfo::isVirtualRegister(Reg))
135 return false;
136 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
137 return false;
138 return vregsPassed.insert(Reg).second;
139 }
140
141 // Same for a full set.
142 bool addPassed(const RegSet &RS) {
143 bool changed = false;
144 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
145 if (addPassed(*I))
146 changed = true;
147 return changed;
148 }
149
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000150 // Add register to vregsRequired if it belongs there. Return true if
151 // anything changed.
152 bool addRequired(unsigned Reg) {
153 if (!TargetRegisterInfo::isVirtualRegister(Reg))
154 return false;
155 if (regsLiveOut.count(Reg))
156 return false;
157 return vregsRequired.insert(Reg).second;
158 }
159
160 // Same for a full set.
161 bool addRequired(const RegSet &RS) {
162 bool changed = false;
163 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
164 if (addRequired(*I))
165 changed = true;
166 return changed;
167 }
168
169 // Same for a full map.
170 bool addRequired(const RegMap &RM) {
171 bool changed = false;
172 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
173 if (addRequired(I->first))
174 changed = true;
175 return changed;
176 }
177
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000178 // Live-out registers are either in regsLiveOut or vregsPassed.
179 bool isLiveOut(unsigned Reg) const {
180 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
181 }
182 };
183
184 // Extra register info per MBB.
185 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
186
187 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000188 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000189 }
190
Lang Hames1ce837a2012-02-14 19:17:48 +0000191 bool isAllocatable(unsigned Reg) {
Jakob Stoklund Olesen244beb42012-10-16 00:05:06 +0000192 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
Lang Hames1ce837a2012-02-14 19:17:48 +0000193 }
194
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000195 // Analysis information if available
196 LiveVariables *LiveVars;
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +0000197 LiveIntervals *LiveInts;
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000198 LiveStacks *LiveStks;
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000199 SlotIndexes *Indexes;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000200
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000201 void visitMachineFunctionBefore();
202 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000203 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000204 void visitMachineInstrBefore(const MachineInstr *MI);
205 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
206 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000207 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000208 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
209 void visitMachineFunctionAfter();
210
211 void report(const char *msg, const MachineFunction *MF);
212 void report(const char *msg, const MachineBasicBlock *MBB);
213 void report(const char *msg, const MachineInstr *MI);
214 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Matthias Braun7e624d52015-11-09 23:59:33 +0000215
216 void report_context(const LiveInterval &LI) const;
Matt Arsenault892fcd02016-07-25 19:39:01 +0000217 void report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000218 LaneBitmask LaneMask) const;
219 void report_context(const LiveRange::Segment &S) const;
220 void report_context(const VNInfo &VNI) const;
Matthias Braun579c9cd2016-02-02 02:44:25 +0000221 void report_context(SlotIndex Pos) const;
222 void report_context_liverange(const LiveRange &LR) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000223 void report_context_lanemask(LaneBitmask LaneMask) const;
Matthias Braun30668dd2016-05-11 21:31:39 +0000224 void report_context_vreg(unsigned VReg) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000225 void report_context_vreg_regunit(unsigned VRegOrRegUnit) const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000226
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000227 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000228
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000229 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Matthias Braun1377fd62016-02-02 20:04:51 +0000230 void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
231 SlotIndex UseIdx, const LiveRange &LR, unsigned Reg,
232 LaneBitmask LaneMask = 0);
233 void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
234 SlotIndex DefIdx, const LiveRange &LR, unsigned Reg,
235 LaneBitmask LaneMask = 0);
236
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000237 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000238 void calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000239 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000240
241 void calcRegsRequired();
242 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000243 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000244 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000245 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
246 unsigned);
Matthias Braun364e6e92013-10-10 21:28:54 +0000247 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000248 const LiveRange::const_iterator I, unsigned,
249 unsigned);
Matthias Braune6a24852015-09-25 21:51:14 +0000250 void verifyLiveRange(const LiveRange&, unsigned, LaneBitmask LaneMask = 0);
Manman Renaa6875b2013-07-15 21:26:31 +0000251
252 void verifyStackFrame();
Matthias Braun80595462015-09-09 17:49:46 +0000253
254 void verifySlotIndexes() const;
Derek Schuff42666ee2016-03-29 17:40:22 +0000255 void verifyProperties(const MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000256 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000257
258 struct MachineVerifierPass : public MachineFunctionPass {
259 static char ID; // Pass ID, replacement for typeid
Matthias Brauna4e932d2014-12-11 19:41:51 +0000260 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000261
Matthias Brauna4e932d2014-12-11 19:41:51 +0000262 MachineVerifierPass(const std::string &banner = nullptr)
263 : MachineFunctionPass(ID), Banner(banner) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000264 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
265 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000266
Craig Topper4584cd52014-03-07 09:26:03 +0000267 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000268 AU.setPreservesAll();
269 MachineFunctionPass::getAnalysisUsage(AU);
270 }
271
Craig Topper4584cd52014-03-07 09:26:03 +0000272 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Braunb3aefc32016-02-15 19:25:31 +0000273 unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
274 if (FoundErrors)
275 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000276 return false;
277 }
278 };
279
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000280}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000281
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000282char MachineVerifierPass::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +0000283INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000284 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000285
Matthias Brauna4e932d2014-12-11 19:41:51 +0000286FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000287 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000288}
289
Matthias Braunb3aefc32016-02-15 19:25:31 +0000290bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
291 const {
292 MachineFunction &MF = const_cast<MachineFunction&>(*this);
293 unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
294 if (AbortOnErrors && FoundErrors)
295 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
296 return FoundErrors == 0;
Jakob Stoklund Olesen27440e72009-11-13 21:56:09 +0000297}
298
Matthias Braun80595462015-09-09 17:49:46 +0000299void MachineVerifier::verifySlotIndexes() const {
300 if (Indexes == nullptr)
301 return;
302
303 // Ensure the IdxMBB list is sorted by slot indexes.
304 SlotIndex Last;
305 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
306 E = Indexes->MBBIndexEnd(); I != E; ++I) {
307 assert(!Last.isValid() || I->first > Last);
308 Last = I->first;
309 }
310}
311
Derek Schuff42666ee2016-03-29 17:40:22 +0000312void MachineVerifier::verifyProperties(const MachineFunction &MF) {
313 // If a pass has introduced virtual registers without clearing the
Matthias Braun1eb47362016-08-25 01:27:13 +0000314 // NoVRegs property (or set it without allocating the vregs)
Derek Schuff42666ee2016-03-29 17:40:22 +0000315 // then report an error.
316 if (MF.getProperties().hasProperty(
Matthias Braun1eb47362016-08-25 01:27:13 +0000317 MachineFunctionProperties::Property::NoVRegs) &&
318 MRI->getNumVirtRegs())
319 report("Function has NoVRegs property but there are VReg operands", &MF);
Derek Schuff42666ee2016-03-29 17:40:22 +0000320}
321
Matthias Braunb3aefc32016-02-15 19:25:31 +0000322unsigned MachineVerifier::verify(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000323 foundErrors = 0;
324
325 this->MF = &MF;
326 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000327 TII = MF.getSubtarget().getInstrInfo();
328 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000329 MRI = &MF.getRegInfo();
330
Ahmed Bougacha3681c772016-08-02 16:17:15 +0000331 isFunctionRegBankSelected = MF.getProperties().hasProperty(
332 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab14e9442016-08-02 16:49:22 +0000333 isFunctionSelected = MF.getProperties().hasProperty(
334 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha3681c772016-08-02 16:17:15 +0000335
Craig Topperc0196b12014-04-14 00:51:57 +0000336 LiveVars = nullptr;
337 LiveInts = nullptr;
338 LiveStks = nullptr;
339 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000340 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000341 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000342 // We don't want to verify LiveVariables if LiveIntervals is available.
343 if (!LiveInts)
344 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000345 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000346 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000347 }
348
Matthias Braun80595462015-09-09 17:49:46 +0000349 verifySlotIndexes();
350
Derek Schuff42666ee2016-03-29 17:40:22 +0000351 verifyProperties(MF);
352
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000353 visitMachineFunctionBefore();
354 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
355 MFI!=MFE; ++MFI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000356 visitMachineBasicBlockBefore(&*MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000357 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000358 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000359 // Do we expect the next instruction to be part of the same bundle?
360 bool InBundle = false;
361
Evan Cheng7fae11b2011-12-14 02:11:42 +0000362 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
363 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000364 if (MBBI->getParent() != &*MFI) {
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +0000365 report("Bad instruction parent pointer", &*MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000366 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000367 continue;
368 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000369
370 // Check for consistent bundle flags.
371 if (InBundle && !MBBI->isBundledWithPred())
372 report("Missing BundledPred flag, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000373 "BundledSucc was set on predecessor",
374 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000375 if (!InBundle && MBBI->isBundledWithPred())
376 report("BundledPred flag is set, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000377 "but BundledSucc not set on predecessor",
378 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000379
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000380 // Is this a bundle header?
381 if (!MBBI->isInsideBundle()) {
382 if (CurBundle)
383 visitMachineBundleAfter(CurBundle);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000384 CurBundle = &*MBBI;
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000385 visitMachineBundleBefore(CurBundle);
386 } else if (!CurBundle)
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +0000387 report("No bundle header", &*MBBI);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000388 visitMachineInstrBefore(&*MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000389 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
390 const MachineInstr &MI = *MBBI;
391 const MachineOperand &Op = MI.getOperand(I);
392 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000393 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000394 // functions when replacing operands of a MachineInstr.
395 report("Instruction has operand with wrong parent set", &MI);
396 }
397
398 visitMachineOperand(&Op, I);
399 }
400
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000401 visitMachineInstrAfter(&*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000402
403 // Was this the last bundled instruction?
404 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000405 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000406 if (CurBundle)
407 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000408 if (InBundle)
409 report("BundledSucc flag set on last instruction in block", &MFI->back());
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000410 visitMachineBasicBlockAfter(&*MFI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000411 }
412 visitMachineFunctionAfter();
413
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000414 // Clean up.
415 regsLive.clear();
416 regsDefined.clear();
417 regsDead.clear();
418 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000419 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000420 regsLiveInButUnused.clear();
421 MBBInfoMap.clear();
422
Matthias Braunb3aefc32016-02-15 19:25:31 +0000423 return foundErrors;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000424}
425
Chris Lattner75f40452009-08-23 01:03:30 +0000426void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000427 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000428 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000429 if (!foundErrors++) {
430 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000431 errs() << "# " << Banner << '\n';
Matthias Braun42b4b632015-11-09 23:59:23 +0000432 if (LiveInts != nullptr)
433 LiveInts->print(errs());
434 else
435 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000436 }
Owen Anderson21b17882015-02-04 00:02:59 +0000437 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000438 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000439}
440
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000441void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000442 assert(MBB);
443 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000444 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000445 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000446 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000447 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000448 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000449 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000450 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000451}
452
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000453void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000454 assert(MI);
455 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000456 errs() << "- instruction: ";
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000457 if (Indexes && Indexes->hasIndex(*MI))
458 errs() << Indexes->getInstructionIndex(*MI) << '\t';
Matthias Braun45718db2015-11-09 23:59:25 +0000459 MI->print(errs(), /*SkipOpers=*/true);
Matthias Braun716b4332015-11-09 23:59:29 +0000460 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000461}
462
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000463void MachineVerifier::report(const char *msg,
464 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000465 assert(MO);
466 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000467 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000468 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000469 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000470}
471
Matthias Braun579c9cd2016-02-02 02:44:25 +0000472void MachineVerifier::report_context(SlotIndex Pos) const {
473 errs() << "- at: " << Pos << '\n';
474}
475
Matthias Braun7e624d52015-11-09 23:59:33 +0000476void MachineVerifier::report_context(const LiveInterval &LI) const {
Owen Anderson21b17882015-02-04 00:02:59 +0000477 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000478}
479
Matt Arsenault892fcd02016-07-25 19:39:01 +0000480void MachineVerifier::report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000481 LaneBitmask LaneMask) const {
Matthias Braun579c9cd2016-02-02 02:44:25 +0000482 report_context_liverange(LR);
Matt Arsenault892fcd02016-07-25 19:39:01 +0000483 report_context_vreg_regunit(VRegUnit);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000484 if (LaneMask != 0)
Matthias Braun1377fd62016-02-02 20:04:51 +0000485 report_context_lanemask(LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000486}
487
Matthias Braun7e624d52015-11-09 23:59:33 +0000488void MachineVerifier::report_context(const LiveRange::Segment &S) const {
489 errs() << "- segment: " << S << '\n';
490}
491
492void MachineVerifier::report_context(const VNInfo &VNI) const {
493 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
Matthias Braun364e6e92013-10-10 21:28:54 +0000494}
495
Matthias Braun579c9cd2016-02-02 02:44:25 +0000496void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
497 errs() << "- liverange: " << LR << '\n';
498}
499
Matthias Braun30668dd2016-05-11 21:31:39 +0000500void MachineVerifier::report_context_vreg(unsigned VReg) const {
501 errs() << "- v. register: " << PrintReg(VReg, TRI) << '\n';
502}
503
Matthias Braun1377fd62016-02-02 20:04:51 +0000504void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
505 if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
Matthias Braun30668dd2016-05-11 21:31:39 +0000506 report_context_vreg(VRegOrUnit);
Matthias Braun1377fd62016-02-02 20:04:51 +0000507 } else {
508 errs() << "- regunit: " << PrintRegUnit(VRegOrUnit, TRI) << '\n';
509 }
510}
511
512void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
513 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
514}
515
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000516void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000517 BBInfo &MInfo = MBBInfoMap[MBB];
518 if (!MInfo.reachable) {
519 MInfo.reachable = true;
520 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
521 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
522 markReachable(*SuI);
523 }
524}
525
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000526void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000527 lastIndex = SlotIndex();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000528 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000529
530 // A sub-register of a reserved register is also reserved
531 for (int Reg = regsReserved.find_first(); Reg>=0;
532 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000533 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000534 // FIXME: This should probably be:
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000535 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
536 regsReserved.set(*SubRegs);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000537 }
538 }
Lang Hames1ce837a2012-02-14 19:17:48 +0000539
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000540 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000541
542 // Build a set of the basic blocks in the function.
543 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000544 for (const auto &MBB : *MF) {
545 FunctionBlocks.insert(&MBB);
546 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000547
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000548 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
549 if (MInfo.Preds.size() != MBB.pred_size())
550 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000551
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000552 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
553 if (MInfo.Succs.size() != MBB.succ_size())
554 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000555 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000556
557 // Check that the register use lists are sane.
558 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000559
560 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000561}
562
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000563// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000564static bool matchPair(MachineBasicBlock::const_succ_iterator i,
565 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000566 if (*i == a)
567 return *++i == b;
568 if (*i == b)
569 return *++i == a;
570 return false;
571}
572
573void
574MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000575 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000576
Matthias Braun79f85b32016-08-24 01:32:41 +0000577 if (!MF->getProperties().hasProperty(
578 MachineFunctionProperties::Property::NoPHIs)) {
Lang Hames1ce837a2012-02-14 19:17:48 +0000579 // If this block has allocatable physical registers live-in, check that
580 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000581 for (const auto &LI : MBB->liveins()) {
582 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +0000583 MBB->getIterator() != MBB->getParent()->begin()) {
Lang Hames1ce837a2012-02-14 19:17:48 +0000584 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
585 }
586 }
587 }
588
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000589 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000590 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000591 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000592 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000593 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000594 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000595 if (!FunctionBlocks.count(*I))
596 report("MBB has successor that isn't part of the function.", MBB);
597 if (!MBBInfoMap[*I].Preds.count(MBB)) {
598 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000599 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000600 << (*I)->getNumber() << ".\n";
601 }
602 }
603
604 // Check the predecessor list.
605 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
606 E = MBB->pred_end(); I != E; ++I) {
607 if (!FunctionBlocks.count(*I))
608 report("MBB has predecessor that isn't part of the function.", MBB);
609 if (!MBBInfoMap[*I].Succs.count(MBB)) {
610 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000611 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000612 << (*I)->getNumber() << ".\n";
613 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000614 }
Bill Wendling2a401312011-05-04 22:54:05 +0000615
616 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
617 const BasicBlock *BB = MBB->getBasicBlock();
Reid Kleckner64b003f2015-11-09 21:04:00 +0000618 const Function *Fn = MF->getFunction();
Bill Wendling2a401312011-05-04 22:54:05 +0000619 if (LandingPadSuccs.size() > 1 &&
620 !(AsmInfo &&
621 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
Reid Kleckner64b003f2015-11-09 21:04:00 +0000622 BB && isa<SwitchInst>(BB->getTerminator())) &&
623 !isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000624 report("MBB has more than one landing pad successor", MBB);
625
Dan Gohman352a4952009-08-27 02:43:49 +0000626 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000627 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000628 SmallVector<MachineOperand, 4> Cond;
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000629 if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
630 Cond)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000631 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
632 // check whether its answers match up with reality.
633 if (!TBB && !FBB) {
634 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000635 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000636 ++MBBI;
637 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000638 // It's possible that the block legitimately ends with a noreturn
639 // call or an unreachable, in which case it won't actually fall
640 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000641 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000642 // It's possible that the block legitimately ends with a noreturn
643 // call or an unreachable, in which case it won't actuall fall
644 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000645 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000646 report("MBB exits via unconditional fall-through but doesn't have "
647 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000648 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000649 report("MBB exits via unconditional fall-through but its successor "
650 "differs from its CFG successor!", MBB);
651 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000652 if (!MBB->empty() && MBB->back().isBarrier() &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000653 !TII->isPredicated(MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000654 report("MBB exits via unconditional fall-through but ends with a "
655 "barrier instruction!", MBB);
656 }
657 if (!Cond.empty()) {
658 report("MBB exits via unconditional fall-through but has a condition!",
659 MBB);
660 }
661 } else if (TBB && !FBB && Cond.empty()) {
662 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000663 // If the block has exactly one successor, that happens to be a
664 // landingpad, accept it as valid control flow.
665 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
666 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
667 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000668 report("MBB exits via unconditional branch but doesn't have "
669 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000670 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000671 report("MBB exits via unconditional branch but the CFG "
672 "successor doesn't match the actual successor!", MBB);
673 }
674 if (MBB->empty()) {
675 report("MBB exits via unconditional branch but doesn't contain "
676 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000677 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000678 report("MBB exits via unconditional branch but doesn't end with a "
679 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000680 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000681 report("MBB exits via unconditional branch but the branch isn't a "
682 "terminator instruction!", MBB);
683 }
684 } else if (TBB && !FBB && !Cond.empty()) {
685 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000686 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000687 ++MBBI;
688 if (MBBI == MF->end()) {
689 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000690 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000691 // A conditional branch with only one successor is weird, but allowed.
692 if (&*MBBI != TBB)
693 report("MBB exits via conditional branch/fall-through but only has "
694 "one CFG successor!", MBB);
695 else if (TBB != *MBB->succ_begin())
696 report("MBB exits via conditional branch/fall-through but the CFG "
697 "successor don't match the actual successor!", MBB);
698 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000699 report("MBB exits via conditional branch/fall-through but doesn't have "
700 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000701 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000702 report("MBB exits via conditional branch/fall-through but the CFG "
703 "successors don't match the actual successors!", MBB);
704 }
705 if (MBB->empty()) {
706 report("MBB exits via conditional branch/fall-through but doesn't "
707 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000708 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000709 report("MBB exits via conditional branch/fall-through but ends with a "
710 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000711 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000712 report("MBB exits via conditional branch/fall-through but the branch "
713 "isn't a terminator instruction!", MBB);
714 }
715 } else if (TBB && FBB) {
716 // Block conditionally branches somewhere, otherwise branches
717 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000718 if (MBB->succ_size() == 1) {
719 // A conditional branch with only one successor is weird, but allowed.
720 if (FBB != TBB)
721 report("MBB exits via conditional branch/branch through but only has "
722 "one CFG successor!", MBB);
723 else if (TBB != *MBB->succ_begin())
724 report("MBB exits via conditional branch/branch through but the CFG "
725 "successor don't match the actual successor!", MBB);
726 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000727 report("MBB exits via conditional branch/branch but doesn't have "
728 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000729 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000730 report("MBB exits via conditional branch/branch but the CFG "
731 "successors don't match the actual successors!", MBB);
732 }
733 if (MBB->empty()) {
734 report("MBB exits via conditional branch/branch but doesn't "
735 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000736 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000737 report("MBB exits via conditional branch/branch but doesn't end with a "
738 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000739 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000740 report("MBB exits via conditional branch/branch but the branch "
741 "isn't a terminator instruction!", MBB);
742 }
743 if (Cond.empty()) {
744 report("MBB exits via conditinal branch/branch but there's no "
745 "condition!", MBB);
746 }
747 } else {
748 report("AnalyzeBranch returned invalid data!", MBB);
749 }
750 }
751
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000752 regsLive.clear();
Matthias Braund9da1622015-09-09 18:08:03 +0000753 for (const auto &LI : MBB->liveins()) {
754 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000755 report("MBB live-in list contains non-physical register", MBB);
756 continue;
757 }
Matthias Braund9da1622015-09-09 18:08:03 +0000758 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
Chad Rosierabdb1d62013-05-22 23:17:36 +0000759 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000760 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000761 }
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +0000762 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000763
Matthias Braun941a7052016-07-28 18:40:00 +0000764 const MachineFrameInfo &MFI = MF->getFrameInfo();
765 BitVector PR = MFI.getPristineRegs(*MF);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000766 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000767 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
768 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000769 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000770 }
771
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000772 regsKilled.clear();
773 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000774
775 if (Indexes)
776 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000777}
778
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000779// This function gets called for all bundle headers, including normal
780// stand-alone unbundled instructions.
781void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000782 if (Indexes && Indexes->hasIndex(*MI)) {
783 SlotIndex idx = Indexes->getInstructionIndex(*MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000784 if (!(idx > lastIndex)) {
785 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000786 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000787 }
788 lastIndex = idx;
789 }
Pete Coopercd720162012-06-07 17:41:39 +0000790
791 // Ensure non-terminators don't follow terminators.
792 // Ignore predicated terminators formed by if conversion.
793 // FIXME: If conversion shouldn't need to violate this rule.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000794 if (MI->isTerminator() && !TII->isPredicated(*MI)) {
Pete Coopercd720162012-06-07 17:41:39 +0000795 if (!FirstTerminator)
796 FirstTerminator = MI;
797 } else if (FirstTerminator) {
798 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000799 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000800 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000801}
802
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000803// The operands on an INLINEASM instruction must follow a template.
804// Verify that the flag operands make sense.
805void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
806 // The first two operands on INLINEASM are the asm string and global flags.
807 if (MI->getNumOperands() < 2) {
808 report("Too few operands on inline asm", MI);
809 return;
810 }
811 if (!MI->getOperand(0).isSymbol())
812 report("Asm string must be an external symbol", MI);
813 if (!MI->getOperand(1).isImm())
814 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000815 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
Wei Ding0526e7f2016-06-22 18:51:08 +0000816 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
817 // and Extra_IsConvergent = 32.
818 if (!isUInt<6>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000819 report("Unknown asm flags", &MI->getOperand(1), 1);
820
Gabor Horvathfee04342015-03-16 09:53:42 +0000821 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000822
823 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
824 unsigned NumOps;
825 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
826 const MachineOperand &MO = MI->getOperand(OpNo);
827 // There may be implicit ops after the fixed operands.
828 if (!MO.isImm())
829 break;
830 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
831 }
832
833 if (OpNo > MI->getNumOperands())
834 report("Missing operands in last group", MI);
835
836 // An optional MDNode follows the groups.
837 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
838 ++OpNo;
839
840 // All trailing operands must be implicit registers.
841 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
842 const MachineOperand &MO = MI->getOperand(OpNo);
843 if (!MO.isReg() || !MO.isImplicit())
844 report("Expected implicit register after groups", &MO, OpNo);
845 }
846}
847
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000848void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000849 const MCInstrDesc &MCID = MI->getDesc();
850 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000851 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000852 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000853 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000854 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000855
Matthias Braun90799ce2016-08-23 21:19:49 +0000856 if (MI->isPHI() && MF->getProperties().hasProperty(
857 MachineFunctionProperties::Property::NoPHIs))
858 report("Found PHI instruction with NoPHIs property set", MI);
859
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000860 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000861 if (MI->isInlineAsm())
862 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000863
Dan Gohmandb9493c2009-10-07 17:36:00 +0000864 // Check the MachineMemOperands for basic consistency.
865 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
866 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000867 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000868 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000869 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000870 report("Missing mayStore flag", MI);
871 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000872
873 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000874 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000875 if (LiveInts) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000876 bool mapped = !LiveInts->isNotInMIMap(*MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000877 if (MI->isDebugValue()) {
878 if (mapped)
879 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000880 } else if (MI->isInsideBundle()) {
881 if (mapped)
882 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000883 } else {
884 if (!mapped)
885 report("Missing slot index", MI);
886 }
887 }
888
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000889 // Check types.
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000890 if (isPreISelGenericOpcode(MCID.getOpcode())) {
Ahmed Bougachab14e9442016-08-02 16:49:22 +0000891 if (isFunctionSelected)
892 report("Unexpected generic instruction in a Selected function", MI);
893
Tim Northover0f140c72016-09-09 11:46:34 +0000894 // Generic instructions specify equality constraints between some
895 // of their operands. Make sure these are consistent.
896 SmallVector<LLT, 4> Types;
897 for (unsigned i = 0; i < MCID.getNumOperands(); ++i) {
898 if (!MCID.OpInfo[i].isGenericType())
899 continue;
900 size_t TypeIdx = MCID.OpInfo[i].getGenericTypeIndex();
901 Types.resize(std::max(TypeIdx + 1, Types.size()));
902
903 LLT OpTy = MRI->getType(MI->getOperand(i).getReg());
904 if (Types[TypeIdx].isValid() && Types[TypeIdx] != OpTy)
905 report("type mismatch in generic instruction", MI);
906 Types[TypeIdx] = OpTy;
907 }
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000908 }
909
Tim Northovere5102de2016-08-30 18:52:46 +0000910 // Generic opcodes must not have physical register operands.
Tim Northover11a23542016-08-31 21:24:02 +0000911 if (isPreISelGenericOpcode(MCID.getOpcode()) &&
912 MCID.getOpcode() != TargetOpcode::G_TYPE) {
Tim Northovere5102de2016-08-30 18:52:46 +0000913 for (auto &Op : MI->operands()) {
914 if (Op.isReg() && TargetRegisterInfo::isPhysicalRegister(Op.getReg()))
915 report("Generic instruction cannot have physical register", MI);
916 }
917 }
918
Andrew Trick924123a2011-09-21 02:20:46 +0000919 StringRef ErrorInfo;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000920 if (!TII->verifyInstruction(*MI, ErrorInfo))
Andrew Trick924123a2011-09-21 02:20:46 +0000921 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000922}
923
924void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000925MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000926 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000927 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +0000928 unsigned NumDefs = MCID.getNumDefs();
929 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
930 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000931
Evan Cheng6cc775f2011-06-28 19:10:37 +0000932 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +0000933 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000934 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000935 if (!MO->isReg())
936 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +0000937 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000938 report("Explicit definition marked as use", MO, MONum);
939 else if (MO->isImplicit())
940 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000941 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000942 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +0000943 // Don't check if it's the last operand in a variadic instruction. See,
944 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000945 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000946 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000947 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +0000948 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000949 if (MO->isImplicit())
950 report("Explicit operand marked as implicit", MO, MONum);
951 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000952
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000953 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
954 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000955 if (!MO->isReg())
956 report("Tied use must be a register", MO, MONum);
957 else if (!MO->isTied())
958 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000959 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
960 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000961 } else if (MO->isReg() && MO->isTied())
962 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000963 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +0000964 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000965 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000966 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000967 }
968
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000969 switch (MO->getType()) {
970 case MachineOperand::MO_Register: {
971 const unsigned Reg = MO->getReg();
972 if (!Reg)
973 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000974 if (MRI->tracksLiveness() && !MI->isDebugValue())
975 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000976
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000977 // Verify the consistency of tied operands.
978 if (MO->isTied()) {
979 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
980 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
981 if (!OtherMO.isReg())
982 report("Must be tied to a register", MO, MONum);
983 if (!OtherMO.isTied())
984 report("Missing tie flags on tied operand", MO, MONum);
985 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
986 report("Inconsistent tie links", MO, MONum);
987 if (MONum < MCID.getNumDefs()) {
988 if (OtherIdx < MCID.getNumOperands()) {
989 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
990 report("Explicit def tied to explicit use without tie constraint",
991 MO, MONum);
992 } else {
993 if (!OtherMO.isImplicit())
994 report("Explicit def should be tied to implicit use", MO, MONum);
995 }
996 }
997 }
998
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +0000999 // Verify two-address constraints after leaving SSA form.
1000 unsigned DefIdx;
1001 if (!MRI->isSSA() && MO->isUse() &&
1002 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
1003 Reg != MI->getOperand(DefIdx).getReg())
1004 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001005
1006 // Check register classes.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001007 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001008 unsigned SubIdx = MO->getSubReg();
1009
1010 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001011 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001012 report("Illegal subregister index for physical register", MO, MONum);
1013 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001014 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001015 if (const TargetRegisterClass *DRC =
1016 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001017 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001018 report("Illegal physical register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001019 errs() << TRI->getName(Reg) << " is not a "
Craig Toppercf0444b2014-11-17 05:50:14 +00001020 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001021 }
1022 }
1023 } else {
1024 // Virtual register.
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001025 const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
1026 if (!RC) {
1027 // This is a generic virtual register.
Ahmed Bougachab14e9442016-08-02 16:49:22 +00001028
1029 // If we're post-Select, we can't have gvregs anymore.
1030 if (isFunctionSelected) {
1031 report("Generic virtual register invalid in a Selected function",
1032 MO, MONum);
1033 return;
1034 }
1035
1036 // The gvreg must have a size and it must not have a SubIdx.
Tim Northover0f140c72016-09-09 11:46:34 +00001037 LLT Ty = MRI->getType(Reg);
1038 if (!Ty.isValid()) {
1039 report("Generic virtual register must have a valid type", MO,
1040 MONum);
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001041 return;
1042 }
Ahmed Bougacha3681c772016-08-02 16:17:15 +00001043
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001044 const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
Ahmed Bougacha3681c772016-08-02 16:17:15 +00001045
1046 // If we're post-RegBankSelect, the gvreg must have a bank.
1047 if (!RegBank && isFunctionRegBankSelected) {
1048 report("Generic virtual register must have a bank in a "
1049 "RegBankSelected function",
1050 MO, MONum);
1051 return;
1052 }
1053
1054 // Make sure the register fits into its register bank if any.
Tim Northover0f140c72016-09-09 11:46:34 +00001055 if (RegBank && Ty.isSized() &&
1056 RegBank->getSize() < Ty.getSizeInBits()) {
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001057 report("Register bank is too small for virtual register", MO,
1058 MONum);
1059 errs() << "Register bank " << RegBank->getName() << " too small("
Tim Northover0f140c72016-09-09 11:46:34 +00001060 << RegBank->getSize() << ") to fit " << Ty.getSizeInBits()
1061 << "-bits\n";
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001062 return;
1063 }
1064 if (SubIdx) {
Tim Northover0f140c72016-09-09 11:46:34 +00001065 report("Generic virtual register does not subregister index", MO,
1066 MONum);
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001067 return;
1068 }
1069 break;
1070 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001071 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001072 const TargetRegisterClass *SRC =
1073 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001074 if (!SRC) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001075 report("Invalid subregister index for virtual register", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001076 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001077 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001078 return;
1079 }
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001080 if (RC != SRC) {
1081 report("Invalid register class for subregister index", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001082 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001083 << " does not fully support subreg index " << SubIdx << "\n";
1084 return;
1085 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001086 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001087 if (const TargetRegisterClass *DRC =
1088 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001089 if (SubIdx) {
1090 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +00001091 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001092 if (!SuperRC) {
1093 report("No largest legal super class exists.", MO, MONum);
1094 return;
1095 }
1096 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
1097 if (!DRC) {
1098 report("No matching super-reg register class.", MO, MONum);
1099 return;
1100 }
1101 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +00001102 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001103 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001104 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +00001105 << " register, but got a " << TRI->getRegClassName(RC)
1106 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001107 }
1108 }
1109 }
1110 }
1111 break;
1112 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001113
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001114 case MachineOperand::MO_RegisterMask:
1115 regMasks.push_back(MO->getRegMask());
1116 break;
1117
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001118 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +00001119 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
1120 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001121 break;
1122
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001123 case MachineOperand::MO_FrameIndex:
1124 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001125 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
Jonas Paulsson72640f12015-10-29 08:28:35 +00001126 int FI = MO->getIndex();
1127 LiveInterval &LI = LiveStks->getInterval(FI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001128 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001129
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001130 bool stores = MI->mayStore();
Jonas Paulsson72640f12015-10-29 08:28:35 +00001131 bool loads = MI->mayLoad();
1132 // For a memory-to-memory move, we need to check if the frame
1133 // index is used for storing or loading, by inspecting the
1134 // memory operands.
1135 if (stores && loads) {
1136 for (auto *MMO : MI->memoperands()) {
1137 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1138 if (PSV == nullptr) continue;
1139 const FixedStackPseudoSourceValue *Value =
1140 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1141 if (Value == nullptr) continue;
1142 if (Value->getFrameIndex() != FI) continue;
1143
1144 if (MMO->isStore())
1145 loads = false;
1146 else
1147 stores = false;
1148 break;
1149 }
1150 if (loads == stores)
1151 report("Missing fixed stack memoperand.", MI);
1152 }
1153 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001154 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001155 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001156 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001157 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001158 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001159 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001160 }
1161 }
1162 break;
1163
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001164 default:
1165 break;
1166 }
1167}
1168
Matthias Braun1377fd62016-02-02 20:04:51 +00001169void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
1170 unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
1171 LaneBitmask LaneMask) {
1172 LiveQueryResult LRQ = LR.Query(UseIdx);
1173 // Check if we have a segment at the use, note however that we only need one
1174 // live subregister range, the others may be dead.
1175 if (!LRQ.valueIn() && LaneMask == 0) {
1176 report("No live segment at use", MO, MONum);
1177 report_context_liverange(LR);
1178 report_context_vreg_regunit(VRegOrUnit);
1179 report_context(UseIdx);
1180 }
1181 if (MO->isKill() && !LRQ.isKill()) {
1182 report("Live range continues after kill flag", MO, MONum);
1183 report_context_liverange(LR);
1184 report_context_vreg_regunit(VRegOrUnit);
1185 if (LaneMask != 0)
1186 report_context_lanemask(LaneMask);
1187 report_context(UseIdx);
1188 }
1189}
1190
1191void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
1192 unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
1193 LaneBitmask LaneMask) {
1194 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
1195 assert(VNI && "NULL valno is not allowed");
1196 if (VNI->def != DefIdx) {
1197 report("Inconsistent valno->def", MO, MONum);
1198 report_context_liverange(LR);
1199 report_context_vreg_regunit(VRegOrUnit);
1200 if (LaneMask != 0)
1201 report_context_lanemask(LaneMask);
1202 report_context(*VNI);
1203 report_context(DefIdx);
1204 }
1205 } else {
1206 report("No live segment at def", MO, MONum);
1207 report_context_liverange(LR);
1208 report_context_vreg_regunit(VRegOrUnit);
1209 if (LaneMask != 0)
1210 report_context_lanemask(LaneMask);
1211 report_context(DefIdx);
1212 }
1213 // Check that, if the dead def flag is present, LiveInts agree.
1214 if (MO->isDead()) {
1215 LiveQueryResult LRQ = LR.Query(DefIdx);
1216 if (!LRQ.isDeadDef()) {
1217 // In case of physregs we can have a non-dead definition on another
1218 // operand.
1219 bool otherDef = false;
1220 if (!TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
1221 const MachineInstr &MI = *MO->getParent();
1222 for (const MachineOperand &MO : MI.operands()) {
1223 if (!MO.isReg() || !MO.isDef() || MO.isDead())
1224 continue;
1225 unsigned Reg = MO.getReg();
1226 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1227 if (*Units == VRegOrUnit) {
1228 otherDef = true;
1229 break;
1230 }
1231 }
1232 }
1233 }
1234
1235 if (!otherDef) {
1236 report("Live range continues after dead def flag", MO, MONum);
1237 report_context_liverange(LR);
1238 report_context_vreg_regunit(VRegOrUnit);
1239 if (LaneMask != 0)
1240 report_context_lanemask(LaneMask);
1241 }
1242 }
1243 }
1244}
1245
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001246void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1247 const MachineInstr *MI = MO->getParent();
1248 const unsigned Reg = MO->getReg();
1249
1250 // Both use and def operands can read a register.
1251 if (MO->readsReg()) {
1252 regsLiveInButUnused.erase(Reg);
1253
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001254 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001255 addRegWithSubRegs(regsKilled, Reg);
1256
1257 // Check that LiveVars knows this kill.
1258 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1259 MO->isKill()) {
1260 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
David Majnemer0d955d02016-08-11 22:21:41 +00001261 if (!is_contained(VI.Kills, MI))
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001262 report("Kill missing from LiveVariables", MO, MONum);
1263 }
1264
1265 // Check LiveInts liveness and kill.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001266 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1267 SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001268 // Check the cached regunit intervals.
1269 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1270 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001271 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
1272 checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001273 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001274 }
1275
1276 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1277 if (LiveInts->hasInterval(Reg)) {
1278 // This is a virtual register interval.
1279 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun1377fd62016-02-02 20:04:51 +00001280 checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
1281
1282 if (LI.hasSubRanges() && !MO->isDef()) {
1283 unsigned SubRegIdx = MO->getSubReg();
1284 LaneBitmask MOMask = SubRegIdx != 0
1285 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1286 : MRI->getMaxLaneMaskForVReg(Reg);
1287 LaneBitmask LiveInMask = 0;
1288 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1289 if ((MOMask & SR.LaneMask) == 0)
1290 continue;
1291 checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
1292 LiveQueryResult LRQ = SR.Query(UseIdx);
1293 if (LRQ.valueIn())
1294 LiveInMask |= SR.LaneMask;
1295 }
1296 // At least parts of the register has to be live at the use.
1297 if ((LiveInMask & MOMask) == 0) {
1298 report("No live subrange at use", MO, MONum);
1299 report_context(LI);
1300 report_context(UseIdx);
1301 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001302 }
1303 } else {
1304 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001305 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001306 }
1307 }
1308
1309 // Use of a dead register.
1310 if (!regsLive.count(Reg)) {
1311 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1312 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001313 bool Bad = !isReserved(Reg);
1314 // We are fine if just any subregister has a defined value.
1315 if (Bad) {
1316 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1317 ++SubRegs) {
1318 if (regsLive.count(*SubRegs)) {
1319 Bad = false;
1320 break;
1321 }
1322 }
1323 }
Matthias Braun96a31952015-01-14 22:25:14 +00001324 // If there is an additional implicit-use of a super register we stop
1325 // here. By definition we are fine if the super register is not
1326 // (completely) dead, if the complete super register is dead we will
1327 // get a report for its operand.
1328 if (Bad) {
1329 for (const MachineOperand &MOP : MI->uses()) {
1330 if (!MOP.isReg())
1331 continue;
1332 if (!MOP.isImplicit())
1333 continue;
1334 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1335 ++SubRegs) {
1336 if (*SubRegs == Reg) {
1337 Bad = false;
1338 break;
1339 }
1340 }
1341 }
1342 }
Matthias Braun96d77322014-12-10 01:13:13 +00001343 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001344 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001345 } else if (MRI->def_empty(Reg)) {
1346 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001347 } else {
1348 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1349 // We don't know which virtual registers are live in, so only complain
1350 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1351 // must be live in. PHI instructions are handled separately.
1352 if (MInfo.regsKilled.count(Reg))
1353 report("Using a killed virtual register", MO, MONum);
1354 else if (!MI->isPHI())
1355 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1356 }
1357 }
1358 }
1359
1360 if (MO->isDef()) {
1361 // Register defined.
1362 // TODO: verify that earlyclobber ops are not used.
1363 if (MO->isDead())
1364 addRegWithSubRegs(regsDead, Reg);
1365 else
1366 addRegWithSubRegs(regsDefined, Reg);
1367
1368 // Verify SSA form.
1369 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001370 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001371 report("Multiple virtual register defs in SSA form", MO, MONum);
1372
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001373 // Check LiveInts for a live segment, but only for virtual registers.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001374 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1375 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001376 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Matthias Braun1377fd62016-02-02 20:04:51 +00001377
1378 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1379 if (LiveInts->hasInterval(Reg)) {
1380 const LiveInterval &LI = LiveInts->getInterval(Reg);
1381 checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
1382
1383 if (LI.hasSubRanges()) {
1384 unsigned SubRegIdx = MO->getSubReg();
1385 LaneBitmask MOMask = SubRegIdx != 0
1386 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1387 : MRI->getMaxLaneMaskForVReg(Reg);
1388 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1389 if ((SR.LaneMask & MOMask) == 0)
1390 continue;
1391 checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, SR.LaneMask);
1392 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001393 }
1394 } else {
Matthias Braun1377fd62016-02-02 20:04:51 +00001395 report("Virtual register has no Live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001396 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001397 }
1398 }
1399 }
1400}
1401
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001402void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001403}
1404
1405// This function gets called after visiting all instructions in a bundle. The
1406// argument points to the bundle header.
1407// Normal stand-alone instructions are also considered 'bundles', and this
1408// function is called for all of them.
1409void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001410 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1411 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001412 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001413 // Kill any masked registers.
1414 while (!regMasks.empty()) {
1415 const uint32_t *Mask = regMasks.pop_back_val();
1416 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1417 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1418 MachineOperand::clobbersPhysReg(Mask, *I))
1419 regsDead.push_back(*I);
1420 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001421 set_subtract(regsLive, regsDead); regsDead.clear();
1422 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001423}
1424
1425void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001426MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001427 MBBInfoMap[MBB].regsLiveOut = regsLive;
1428 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001429
1430 if (Indexes) {
1431 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1432 if (!(stop > lastIndex)) {
1433 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001434 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001435 << " last instruction was at " << lastIndex << '\n';
1436 }
1437 lastIndex = stop;
1438 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001439}
1440
1441// Calculate the largest possible vregsPassed sets. These are the registers that
1442// can pass through an MBB live, but may not be live every time. It is assumed
1443// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001444void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001445 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1446 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001447 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001448 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001449 BBInfo &MInfo = MBBInfoMap[&MBB];
1450 if (!MInfo.reachable)
1451 continue;
1452 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1453 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1454 BBInfo &SInfo = MBBInfoMap[*SuI];
1455 if (SInfo.addPassed(MInfo.regsLiveOut))
1456 todo.insert(*SuI);
1457 }
1458 }
1459
1460 // Iteratively push vregsPassed to successors. This will converge to the same
1461 // final state regardless of DenseSet iteration order.
1462 while (!todo.empty()) {
1463 const MachineBasicBlock *MBB = *todo.begin();
1464 todo.erase(MBB);
1465 BBInfo &MInfo = MBBInfoMap[MBB];
1466 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1467 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1468 if (*SuI == MBB)
1469 continue;
1470 BBInfo &SInfo = MBBInfoMap[*SuI];
1471 if (SInfo.addPassed(MInfo.vregsPassed))
1472 todo.insert(*SuI);
1473 }
1474 }
1475}
1476
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001477// Calculate the set of virtual registers that must be passed through each basic
1478// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001479// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001480void MachineVerifier::calcRegsRequired() {
1481 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001482 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001483 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001484 BBInfo &MInfo = MBBInfoMap[&MBB];
1485 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1486 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1487 BBInfo &PInfo = MBBInfoMap[*PrI];
1488 if (PInfo.addRequired(MInfo.vregsLiveIn))
1489 todo.insert(*PrI);
1490 }
1491 }
1492
1493 // Iteratively push vregsRequired to predecessors. This will converge to the
1494 // same final state regardless of DenseSet iteration order.
1495 while (!todo.empty()) {
1496 const MachineBasicBlock *MBB = *todo.begin();
1497 todo.erase(MBB);
1498 BBInfo &MInfo = MBBInfoMap[MBB];
1499 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1500 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1501 if (*PrI == MBB)
1502 continue;
1503 BBInfo &SInfo = MBBInfoMap[*PrI];
1504 if (SInfo.addRequired(MInfo.vregsRequired))
1505 todo.insert(*PrI);
1506 }
1507 }
1508}
1509
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001510// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001511// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001512void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001513 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001514 for (const auto &BBI : *MBB) {
1515 if (!BBI.isPHI())
1516 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001517 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001518
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001519 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1520 unsigned Reg = BBI.getOperand(i).getReg();
1521 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001522 if (!Pre->isSuccessor(MBB))
1523 continue;
1524 seen.insert(Pre);
1525 BBInfo &PrInfo = MBBInfoMap[Pre];
1526 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1527 report("PHI operand is not live-out from predecessor",
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001528 &BBI.getOperand(i), i);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001529 }
1530
1531 // Did we see all predecessors?
1532 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1533 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1534 if (!seen.count(*PrI)) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001535 report("Missing PHI operand", &BBI);
Owen Anderson21b17882015-02-04 00:02:59 +00001536 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001537 << " is a predecessor according to the CFG.\n";
1538 }
1539 }
1540 }
1541}
1542
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001543void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001544 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001545
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001546 for (const auto &MBB : *MF) {
1547 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001548
1549 // Skip unreachable MBBs.
1550 if (!MInfo.reachable)
1551 continue;
1552
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001553 checkPHIOps(&MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001554 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001555
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001556 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001557 calcRegsRequired();
1558
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001559 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001560 for (const auto &MBB : *MF) {
1561 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001562 for (RegSet::iterator
1563 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1564 ++I)
1565 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001566 report("Virtual register killed in block, but needed live out.", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001567 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001568 << " is used after the block.\n";
1569 }
1570 }
1571
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001572 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001573 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1574 for (RegSet::iterator
1575 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Matthias Braun30668dd2016-05-11 21:31:39 +00001576 ++I) {
1577 report("Virtual register defs don't dominate all uses.", MF);
1578 report_context_vreg(*I);
1579 }
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001580 }
1581
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001582 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001583 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001584 if (LiveInts)
1585 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001586}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001587
1588void MachineVerifier::verifyLiveVariables() {
1589 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001590 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1591 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001592 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001593 for (const auto &MBB : *MF) {
1594 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001595
1596 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1597 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001598 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1599 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001600 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001601 << " must be live through the block.\n";
1602 }
1603 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001604 if (VI.AliveBlocks.test(MBB.getNumber())) {
1605 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001606 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001607 << " is not needed live through the block.\n";
1608 }
1609 }
1610 }
1611 }
1612}
1613
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001614void MachineVerifier::verifyLiveIntervals() {
1615 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001616 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1617 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001618
1619 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001620 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001621 continue;
1622
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001623 if (!LiveInts->hasInterval(Reg)) {
1624 report("Missing live interval for virtual register", MF);
Owen Anderson21b17882015-02-04 00:02:59 +00001625 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001626 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001627 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001628
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001629 const LiveInterval &LI = LiveInts->getInterval(Reg);
1630 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001631 verifyLiveInterval(LI);
1632 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001633
1634 // Verify all the cached regunit intervals.
1635 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001636 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1637 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001638}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001639
Matthias Braun364e6e92013-10-10 21:28:54 +00001640void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001641 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001642 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001643 if (VNI->isUnused())
1644 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001645
Matthias Braun364e6e92013-10-10 21:28:54 +00001646 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001647
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001648 if (!DefVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001649 report("Value not live at VNInfo def and not marked unused", MF);
1650 report_context(LR, Reg, LaneMask);
1651 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001652 return;
1653 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001654
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001655 if (DefVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001656 report("Live segment at def has different VNInfo", MF);
1657 report_context(LR, Reg, LaneMask);
1658 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001659 return;
1660 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001661
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001662 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1663 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001664 report("Invalid VNInfo definition index", MF);
1665 report_context(LR, Reg, LaneMask);
1666 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001667 return;
1668 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001669
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001670 if (VNI->isPHIDef()) {
1671 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001672 report("PHIDef VNInfo is not defined at MBB start", MBB);
1673 report_context(LR, Reg, LaneMask);
1674 report_context(*VNI);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001675 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001676 return;
1677 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001678
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001679 // Non-PHI def.
1680 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1681 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001682 report("No instruction at VNInfo def index", MBB);
1683 report_context(LR, Reg, LaneMask);
1684 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001685 return;
1686 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001687
Matthias Braun364e6e92013-10-10 21:28:54 +00001688 if (Reg != 0) {
1689 bool hasDef = false;
1690 bool isEarlyClobber = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001691 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001692 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001693 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001694 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1695 if (MOI->getReg() != Reg)
1696 continue;
1697 } else {
1698 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1699 !TRI->hasRegUnit(MOI->getReg(), Reg))
1700 continue;
1701 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001702 if (LaneMask != 0 &&
1703 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask) == 0)
1704 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001705 hasDef = true;
1706 if (MOI->isEarlyClobber())
1707 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001708 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001709
Matthias Braun364e6e92013-10-10 21:28:54 +00001710 if (!hasDef) {
1711 report("Defining instruction does not modify register", MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001712 report_context(LR, Reg, LaneMask);
1713 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001714 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001715
Matthias Braun364e6e92013-10-10 21:28:54 +00001716 // Early clobber defs begin at USE slots, but other defs must begin at
1717 // DEF slots.
1718 if (isEarlyClobber) {
1719 if (!VNI->def.isEarlyClobber()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001720 report("Early clobber def must be at an early-clobber slot", MBB);
1721 report_context(LR, Reg, LaneMask);
1722 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001723 }
1724 } else if (!VNI->def.isRegister()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001725 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
1726 report_context(LR, Reg, LaneMask);
1727 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001728 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001729 }
1730}
1731
Matthias Braun364e6e92013-10-10 21:28:54 +00001732void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1733 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001734 unsigned Reg, LaneBitmask LaneMask)
1735{
Matthias Braun364e6e92013-10-10 21:28:54 +00001736 const LiveRange::Segment &S = *I;
1737 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001738 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001739
Matthias Braun364e6e92013-10-10 21:28:54 +00001740 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001741 report("Foreign valno in live segment", MF);
1742 report_context(LR, Reg, LaneMask);
1743 report_context(S);
1744 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001745 }
1746
1747 if (VNI->isUnused()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001748 report("Live segment valno is marked unused", MF);
1749 report_context(LR, Reg, LaneMask);
1750 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001751 }
1752
Matthias Braun364e6e92013-10-10 21:28:54 +00001753 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001754 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001755 report("Bad start of live segment, no basic block", MF);
1756 report_context(LR, Reg, LaneMask);
1757 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001758 return;
1759 }
1760 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001761 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001762 report("Live segment must begin at MBB entry or valno def", MBB);
1763 report_context(LR, Reg, LaneMask);
1764 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001765 }
1766
1767 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001768 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001769 if (!EndMBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001770 report("Bad end of live segment, no basic block", MF);
1771 report_context(LR, Reg, LaneMask);
1772 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001773 return;
1774 }
1775
1776 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001777 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001778 return;
1779
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001780 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001781 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1782 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001783 return;
1784
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001785 // The live segment is ending inside EndMBB
1786 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001787 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001788 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001789 report("Live segment doesn't end at a valid instruction", EndMBB);
1790 report_context(LR, Reg, LaneMask);
1791 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001792 return;
1793 }
1794
1795 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001796 if (S.end.isBlock()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001797 report("Live segment ends at B slot of an instruction", EndMBB);
1798 report_context(LR, Reg, LaneMask);
1799 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001800 }
1801
Matthias Braun364e6e92013-10-10 21:28:54 +00001802 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001803 // Segment ends on the dead slot.
1804 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001805 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001806 report("Live segment ending at dead slot spans instructions", EndMBB);
1807 report_context(LR, Reg, LaneMask);
1808 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001809 }
1810 }
1811
1812 // A live segment can only end at an early-clobber slot if it is being
1813 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001814 if (S.end.isEarlyClobber()) {
1815 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001816 report("Live segment ending at early clobber slot must be "
Matthias Braun7e624d52015-11-09 23:59:33 +00001817 "redefined by an EC def in the same instruction", EndMBB);
1818 report_context(LR, Reg, LaneMask);
1819 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001820 }
1821 }
1822
1823 // The following checks only apply to virtual registers. Physreg liveness
1824 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001825 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001826 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001827 // use, or a dead flag on a def.
1828 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001829 bool hasSubRegDef = false;
Matthias Braun72a58c32016-03-29 19:07:43 +00001830 bool hasDeadDef = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001831 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001832 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001833 continue;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001834 unsigned Sub = MOI->getSubReg();
Krzysztof Parzyszek0a955d62016-08-29 13:15:35 +00001835 LaneBitmask SLM = Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub) : ~0U;
Matthias Braun72a58c32016-03-29 19:07:43 +00001836 if (MOI->isDef()) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001837 if (Sub != 0) {
Matthias Braun72a58c32016-03-29 19:07:43 +00001838 hasSubRegDef = true;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001839 // An operand vreg0:sub0<def> reads vreg0:sub1..n. Invert the lane
1840 // mask for subregister defs. Read-undef defs will be handled by
1841 // readsReg below.
Krzysztof Parzyszek0a955d62016-08-29 13:15:35 +00001842 SLM = ~SLM;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001843 }
Matthias Braun72a58c32016-03-29 19:07:43 +00001844 if (MOI->isDead())
1845 hasDeadDef = true;
1846 }
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001847 if (LaneMask != 0 && !(LaneMask & SLM))
1848 continue;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001849 if (MOI->readsReg())
1850 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001851 }
Matthias Braun72a58c32016-03-29 19:07:43 +00001852 if (S.end.isDead()) {
1853 // Make sure that the corresponding machine operand for a "dead" live
1854 // range has the dead flag. We cannot perform this check for subregister
1855 // liveranges as partially dead values are allowed.
1856 if (LaneMask == 0 && !hasDeadDef) {
1857 report("Instruction ending live segment on dead slot has no dead flag",
1858 MI);
1859 report_context(LR, Reg, LaneMask);
1860 report_context(S);
1861 }
1862 } else {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001863 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001864 // When tracking subregister liveness, the main range must start new
1865 // values on partial register writes, even if there is no read.
Matthias Brauna25e13a2015-03-19 00:21:58 +00001866 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask != 0 ||
1867 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001868 report("Instruction ending live segment doesn't read the register",
1869 MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001870 report_context(LR, Reg, LaneMask);
1871 report_context(S);
Matthias Braun21554d92014-12-10 01:13:11 +00001872 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001873 }
1874 }
1875 }
1876
1877 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001878 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001879 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00001880 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001881 // Not live-in to any blocks.
1882 if (MBB == EndMBB)
1883 return;
1884 // Skip this block.
1885 ++MFI;
1886 }
1887 for (;;) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001888 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001889 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00001890 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00001891 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001892 if (&*MFI == EndMBB)
1893 break;
1894 ++MFI;
1895 continue;
1896 }
1897
1898 // Is VNI a PHI-def in the current block?
1899 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001900 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001901
1902 // Check that VNI is live-out of all predecessors.
1903 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1904 PE = MFI->pred_end(); PI != PE; ++PI) {
1905 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001906 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001907
Matthias Braune29b7682016-05-20 23:02:13 +00001908 // All predecessors must have a live-out value if this is not a
1909 // subregister liverange.
1910 if (!PVNI && LaneMask == 0) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001911 report("Register not marked live out of predecessor", *PI);
1912 report_context(LR, Reg, LaneMask);
1913 report_context(*VNI);
1914 errs() << " live into BB#" << MFI->getNumber()
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001915 << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
1916 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001917 continue;
1918 }
1919
1920 // Only PHI-defs can take different predecessor values.
1921 if (!IsPHI && PVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001922 report("Different value live out of predecessor", *PI);
1923 report_context(LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001924 errs() << "Valno #" << PVNI->id << " live out of BB#"
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001925 << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
1926 << " live into BB#" << MFI->getNumber() << '@'
1927 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001928 }
1929 }
1930 if (&*MFI == EndMBB)
1931 break;
1932 ++MFI;
1933 }
1934}
1935
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001936void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001937 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00001938 for (const VNInfo *VNI : LR.valnos)
1939 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001940
Matthias Braun364e6e92013-10-10 21:28:54 +00001941 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001942 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00001943}
1944
1945void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001946 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00001947 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1948 verifyLiveRange(LI, Reg);
1949
Matthias Braune6a24852015-09-25 21:51:14 +00001950 LaneBitmask Mask = 0;
1951 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00001952 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001953 if ((Mask & SR.LaneMask) != 0) {
1954 report("Lane masks of sub ranges overlap in live interval", MF);
1955 report_context(LI);
1956 }
1957 if ((SR.LaneMask & ~MaxMask) != 0) {
1958 report("Subrange lanemask is invalid", MF);
1959 report_context(LI);
1960 }
1961 if (SR.empty()) {
1962 report("Subrange must not be empty", MF);
1963 report_context(SR, LI.reg, SR.LaneMask);
1964 }
Matthias Braune962e522015-03-25 21:18:22 +00001965 Mask |= SR.LaneMask;
1966 verifyLiveRange(SR, LI.reg, SR.LaneMask);
Matthias Braun7e624d52015-11-09 23:59:33 +00001967 if (!LI.covers(SR)) {
1968 report("A Subrange is not covered by the main range", MF);
1969 report_context(LI);
1970 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001971 }
1972
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001973 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00001974 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
Matthias Braunbf47f632016-01-08 01:16:35 +00001975 unsigned NumComp = ConEQ.Classify(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001976 if (NumComp > 1) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001977 report("Multiple connected components in live interval", MF);
1978 report_context(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001979 for (unsigned comp = 0; comp != NumComp; ++comp) {
1980 errs() << comp << ": valnos";
1981 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1982 E = LI.vni_end(); I!=E; ++I)
1983 if (comp == ConEQ.getEqClass(*I))
1984 errs() << ' ' << (*I)->id;
1985 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00001986 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001987 }
1988}
Manman Renaa6875b2013-07-15 21:26:31 +00001989
1990namespace {
1991 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1992 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1993 // value is zero.
1994 // We use a bool plus an integer to capture the stack state.
1995 struct StackStateOfBB {
1996 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1997 ExitIsSetup(false) { }
1998 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1999 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
2000 ExitIsSetup(ExitSetup) { }
2001 // Can be negative, which means we are setting up a frame.
2002 int EntryValue;
2003 int ExitValue;
2004 bool EntryIsSetup;
2005 bool ExitIsSetup;
2006 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002007}
Manman Renaa6875b2013-07-15 21:26:31 +00002008
2009/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
2010/// by a FrameDestroy <n>, stack adjustments are identical on all
2011/// CFG edges to a merge point, and frame is destroyed at end of a return block.
2012void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00002013 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
2014 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Manman Renaa6875b2013-07-15 21:26:31 +00002015
2016 SmallVector<StackStateOfBB, 8> SPState;
2017 SPState.resize(MF->getNumBlockIDs());
2018 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
2019
2020 // Visit the MBBs in DFS order.
2021 for (df_ext_iterator<const MachineFunction*,
2022 SmallPtrSet<const MachineBasicBlock*, 8> >
2023 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
2024 DFI != DFE; ++DFI) {
2025 const MachineBasicBlock *MBB = *DFI;
2026
2027 StackStateOfBB BBState;
2028 // Check the exit state of the DFS stack predecessor.
2029 if (DFI.getPathLength() >= 2) {
2030 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
2031 assert(Reachable.count(StackPred) &&
2032 "DFS stack predecessor is already visited.\n");
2033 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
2034 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
2035 BBState.ExitValue = BBState.EntryValue;
2036 BBState.ExitIsSetup = BBState.EntryIsSetup;
2037 }
2038
2039 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002040 for (const auto &I : *MBB) {
2041 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00002042 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002043 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00002044 assert(Size >= 0 &&
2045 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
2046
2047 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002048 report("FrameSetup is after another FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00002049 BBState.ExitValue -= Size;
2050 BBState.ExitIsSetup = true;
2051 }
2052
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002053 if (I.getOpcode() == FrameDestroyOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00002054 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002055 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00002056 assert(Size >= 0 &&
2057 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
2058
2059 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002060 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00002061 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
2062 BBState.ExitValue;
2063 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002064 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00002065 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00002066 << AbsSPAdj << ">.\n";
2067 }
2068 BBState.ExitValue += Size;
2069 BBState.ExitIsSetup = false;
2070 }
2071 }
2072 SPState[MBB->getNumber()] = BBState;
2073
2074 // Make sure the exit state of any predecessor is consistent with the entry
2075 // state.
2076 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
2077 E = MBB->pred_end(); I != E; ++I) {
2078 if (Reachable.count(*I) &&
2079 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
2080 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
2081 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002082 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002083 << SPState[(*I)->getNumber()].ExitValue << ", "
2084 << SPState[(*I)->getNumber()].ExitIsSetup
2085 << "), while BB#" << MBB->getNumber() << " has entry state ("
2086 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
2087 }
2088 }
2089
2090 // Make sure the entry state of any successor is consistent with the exit
2091 // state.
2092 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
2093 E = MBB->succ_end(); I != E; ++I) {
2094 if (Reachable.count(*I) &&
2095 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
2096 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
2097 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002098 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002099 << SPState[(*I)->getNumber()].EntryValue << ", "
2100 << SPState[(*I)->getNumber()].EntryIsSetup
2101 << "), while BB#" << MBB->getNumber() << " has exit state ("
2102 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
2103 }
2104 }
2105
2106 // Make sure a basic block with return ends with zero stack adjustment.
2107 if (!MBB->empty() && MBB->back().isReturn()) {
2108 if (BBState.ExitIsSetup)
2109 report("A return block ends with a FrameSetup.", MBB);
2110 if (BBState.ExitValue)
2111 report("A return block ends with a nonzero stack adjustment.", MBB);
2112 }
2113 }
2114}