blob: 873039523710b12f4a0beb61ee33439b81ce6dea [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
73 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000074 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000075 typedef DenseSet<unsigned> RegSet;
76 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000077 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000078
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000079 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000080 BlockSet FunctionBlocks;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000081
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000082 BitVector regsReserved;
83 RegSet regsLive;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000084 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000085 RegMaskVector regMasks;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000086 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000087
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +000088 SlotIndex lastIndex;
89
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000090 // Add Reg and any sub-registers to RV
91 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
92 RV.push_back(Reg);
93 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +000094 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
95 RV.push_back(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000096 }
97
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000098 struct BBInfo {
99 // Is this MBB reachable from the MF entry point?
100 bool reachable;
101
102 // Vregs that must be live in because they are used without being
103 // defined. Map value is the user.
104 RegMap vregsLiveIn;
105
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000106 // Regs killed in MBB. They may be defined again, and will then be in both
107 // regsKilled and regsLiveOut.
108 RegSet regsKilled;
109
110 // Regs defined in MBB and live out. Note that vregs passing through may
111 // be live out without being mentioned here.
112 RegSet regsLiveOut;
113
114 // Vregs that pass through MBB untouched. This set is disjoint from
115 // regsKilled and regsLiveOut.
116 RegSet vregsPassed;
117
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000118 // Vregs that must pass through MBB because they are needed by a successor
119 // block. This set is disjoint from regsLiveOut.
120 RegSet vregsRequired;
121
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000122 // Set versions of block's predecessor and successor lists.
123 BlockSet Preds, Succs;
124
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000125 BBInfo() : reachable(false) {}
126
127 // Add register to vregsPassed if it belongs there. Return true if
128 // anything changed.
129 bool addPassed(unsigned Reg) {
130 if (!TargetRegisterInfo::isVirtualRegister(Reg))
131 return false;
132 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
133 return false;
134 return vregsPassed.insert(Reg).second;
135 }
136
137 // Same for a full set.
138 bool addPassed(const RegSet &RS) {
139 bool changed = false;
140 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
141 if (addPassed(*I))
142 changed = true;
143 return changed;
144 }
145
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000146 // Add register to vregsRequired if it belongs there. Return true if
147 // anything changed.
148 bool addRequired(unsigned Reg) {
149 if (!TargetRegisterInfo::isVirtualRegister(Reg))
150 return false;
151 if (regsLiveOut.count(Reg))
152 return false;
153 return vregsRequired.insert(Reg).second;
154 }
155
156 // Same for a full set.
157 bool addRequired(const RegSet &RS) {
158 bool changed = false;
159 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
160 if (addRequired(*I))
161 changed = true;
162 return changed;
163 }
164
165 // Same for a full map.
166 bool addRequired(const RegMap &RM) {
167 bool changed = false;
168 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
169 if (addRequired(I->first))
170 changed = true;
171 return changed;
172 }
173
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000174 // Live-out registers are either in regsLiveOut or vregsPassed.
175 bool isLiveOut(unsigned Reg) const {
176 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
177 }
178 };
179
180 // Extra register info per MBB.
181 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
182
183 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000184 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000185 }
186
Lang Hames1ce837a2012-02-14 19:17:48 +0000187 bool isAllocatable(unsigned Reg) {
Jakob Stoklund Olesen244beb42012-10-16 00:05:06 +0000188 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
Lang Hames1ce837a2012-02-14 19:17:48 +0000189 }
190
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000191 // Analysis information if available
192 LiveVariables *LiveVars;
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +0000193 LiveIntervals *LiveInts;
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000194 LiveStacks *LiveStks;
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000195 SlotIndexes *Indexes;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000196
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000197 void visitMachineFunctionBefore();
198 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000199 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000200 void visitMachineInstrBefore(const MachineInstr *MI);
201 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
202 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000203 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000204 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
205 void visitMachineFunctionAfter();
206
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000207 template <typename T> void report(const char *msg, ilist_iterator<T> I) {
208 report(msg, &*I);
209 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000210 void report(const char *msg, const MachineFunction *MF);
211 void report(const char *msg, const MachineBasicBlock *MBB);
212 void report(const char *msg, const MachineInstr *MI);
213 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Matthias Braun7e624d52015-11-09 23:59:33 +0000214
215 void report_context(const LiveInterval &LI) const;
216 void report_context(const LiveRange &LR, unsigned Reg,
217 LaneBitmask LaneMask) const;
218 void report_context(const LiveRange::Segment &S) const;
219 void report_context(const VNInfo &VNI) const;
Matthias Braun579c9cd2016-02-02 02:44:25 +0000220 void report_context(SlotIndex Pos) const;
221 void report_context_liverange(const LiveRange &LR) const;
222 void report_context_regunit(unsigned RegUnit) 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
314 // AllVRegsAllocated property (or set it without allocating the vregs)
315 // then report an error.
316 if (MF.getProperties().hasProperty(
317 MachineFunctionProperties::Property::AllVRegsAllocated) &&
318 MRI->getNumVirtRegs()) {
319 report(
320 "Function has AllVRegsAllocated property but there are VReg operands",
321 &MF);
322 }
323}
324
Matthias Braunb3aefc32016-02-15 19:25:31 +0000325unsigned MachineVerifier::verify(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000326 foundErrors = 0;
327
328 this->MF = &MF;
329 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000330 TII = MF.getSubtarget().getInstrInfo();
331 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000332 MRI = &MF.getRegInfo();
333
Craig Topperc0196b12014-04-14 00:51:57 +0000334 LiveVars = nullptr;
335 LiveInts = nullptr;
336 LiveStks = nullptr;
337 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000338 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000339 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000340 // We don't want to verify LiveVariables if LiveIntervals is available.
341 if (!LiveInts)
342 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000343 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000344 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000345 }
346
Matthias Braun80595462015-09-09 17:49:46 +0000347 verifySlotIndexes();
348
Derek Schuff42666ee2016-03-29 17:40:22 +0000349 verifyProperties(MF);
350
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000351 visitMachineFunctionBefore();
352 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
353 MFI!=MFE; ++MFI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000354 visitMachineBasicBlockBefore(&*MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000355 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000356 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000357 // Do we expect the next instruction to be part of the same bundle?
358 bool InBundle = false;
359
Evan Cheng7fae11b2011-12-14 02:11:42 +0000360 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
361 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000362 if (MBBI->getParent() != &*MFI) {
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000363 report("Bad instruction parent pointer", MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000364 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000365 continue;
366 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000367
368 // Check for consistent bundle flags.
369 if (InBundle && !MBBI->isBundledWithPred())
370 report("Missing BundledPred flag, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000371 "BundledSucc was set on predecessor",
372 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000373 if (!InBundle && MBBI->isBundledWithPred())
374 report("BundledPred flag is set, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000375 "but BundledSucc not set on predecessor",
376 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000377
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000378 // Is this a bundle header?
379 if (!MBBI->isInsideBundle()) {
380 if (CurBundle)
381 visitMachineBundleAfter(CurBundle);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000382 CurBundle = &*MBBI;
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000383 visitMachineBundleBefore(CurBundle);
384 } else if (!CurBundle)
385 report("No bundle header", MBBI);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000386 visitMachineInstrBefore(&*MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000387 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
388 const MachineInstr &MI = *MBBI;
389 const MachineOperand &Op = MI.getOperand(I);
390 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000391 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000392 // functions when replacing operands of a MachineInstr.
393 report("Instruction has operand with wrong parent set", &MI);
394 }
395
396 visitMachineOperand(&Op, I);
397 }
398
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000399 visitMachineInstrAfter(&*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000400
401 // Was this the last bundled instruction?
402 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000403 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000404 if (CurBundle)
405 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000406 if (InBundle)
407 report("BundledSucc flag set on last instruction in block", &MFI->back());
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000408 visitMachineBasicBlockAfter(&*MFI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000409 }
410 visitMachineFunctionAfter();
411
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000412 // Clean up.
413 regsLive.clear();
414 regsDefined.clear();
415 regsDead.clear();
416 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000417 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000418 regsLiveInButUnused.clear();
419 MBBInfoMap.clear();
420
Matthias Braunb3aefc32016-02-15 19:25:31 +0000421 return foundErrors;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000422}
423
Chris Lattner75f40452009-08-23 01:03:30 +0000424void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000425 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000426 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000427 if (!foundErrors++) {
428 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000429 errs() << "# " << Banner << '\n';
Matthias Braun42b4b632015-11-09 23:59:23 +0000430 if (LiveInts != nullptr)
431 LiveInts->print(errs());
432 else
433 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000434 }
Owen Anderson21b17882015-02-04 00:02:59 +0000435 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000436 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000437}
438
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000439void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000440 assert(MBB);
441 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000442 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000443 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000444 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000445 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000446 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000447 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000448 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000449}
450
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000451void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000452 assert(MI);
453 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000454 errs() << "- instruction: ";
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000455 if (Indexes && Indexes->hasIndex(*MI))
456 errs() << Indexes->getInstructionIndex(*MI) << '\t';
Matthias Braun45718db2015-11-09 23:59:25 +0000457 MI->print(errs(), /*SkipOpers=*/true);
Matthias Braun716b4332015-11-09 23:59:29 +0000458 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000459}
460
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000461void MachineVerifier::report(const char *msg,
462 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000463 assert(MO);
464 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000465 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000466 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000467 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000468}
469
Matthias Braun579c9cd2016-02-02 02:44:25 +0000470void MachineVerifier::report_context(SlotIndex Pos) const {
471 errs() << "- at: " << Pos << '\n';
472}
473
Matthias Braun7e624d52015-11-09 23:59:33 +0000474void MachineVerifier::report_context(const LiveInterval &LI) const {
Owen Anderson21b17882015-02-04 00:02:59 +0000475 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000476}
477
Matthias Braun7e624d52015-11-09 23:59:33 +0000478void MachineVerifier::report_context(const LiveRange &LR, unsigned Reg,
479 LaneBitmask LaneMask) const {
Matthias Braun579c9cd2016-02-02 02:44:25 +0000480 report_context_liverange(LR);
Owen Anderson21b17882015-02-04 00:02:59 +0000481 errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000482 if (LaneMask != 0)
Matthias Braun1377fd62016-02-02 20:04:51 +0000483 report_context_lanemask(LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000484}
485
Matthias Braun7e624d52015-11-09 23:59:33 +0000486void MachineVerifier::report_context(const LiveRange::Segment &S) const {
487 errs() << "- segment: " << S << '\n';
488}
489
490void MachineVerifier::report_context(const VNInfo &VNI) const {
491 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
Matthias Braun364e6e92013-10-10 21:28:54 +0000492}
493
Matthias Braun579c9cd2016-02-02 02:44:25 +0000494void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
495 errs() << "- liverange: " << LR << '\n';
496}
497
498void MachineVerifier::report_context_regunit(unsigned RegUnit) const {
499 errs() << "- regunit: " << PrintRegUnit(RegUnit, TRI) << '\n';
500}
501
Matthias Braun30668dd2016-05-11 21:31:39 +0000502void MachineVerifier::report_context_vreg(unsigned VReg) const {
503 errs() << "- v. register: " << PrintReg(VReg, TRI) << '\n';
504}
505
Matthias Braun1377fd62016-02-02 20:04:51 +0000506void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
507 if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
Matthias Braun30668dd2016-05-11 21:31:39 +0000508 report_context_vreg(VRegOrUnit);
Matthias Braun1377fd62016-02-02 20:04:51 +0000509 } else {
510 errs() << "- regunit: " << PrintRegUnit(VRegOrUnit, TRI) << '\n';
511 }
512}
513
514void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
515 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
516}
517
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000518void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000519 BBInfo &MInfo = MBBInfoMap[MBB];
520 if (!MInfo.reachable) {
521 MInfo.reachable = true;
522 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
523 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
524 markReachable(*SuI);
525 }
526}
527
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000528void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000529 lastIndex = SlotIndex();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000530 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000531
532 // A sub-register of a reserved register is also reserved
533 for (int Reg = regsReserved.find_first(); Reg>=0;
534 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000535 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000536 // FIXME: This should probably be:
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000537 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
538 regsReserved.set(*SubRegs);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000539 }
540 }
Lang Hames1ce837a2012-02-14 19:17:48 +0000541
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000542 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000543
544 // Build a set of the basic blocks in the function.
545 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000546 for (const auto &MBB : *MF) {
547 FunctionBlocks.insert(&MBB);
548 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000549
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000550 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
551 if (MInfo.Preds.size() != MBB.pred_size())
552 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000553
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000554 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
555 if (MInfo.Succs.size() != MBB.succ_size())
556 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000557 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000558
559 // Check that the register use lists are sane.
560 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000561
562 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000563}
564
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000565// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000566static bool matchPair(MachineBasicBlock::const_succ_iterator i,
567 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000568 if (*i == a)
569 return *++i == b;
570 if (*i == b)
571 return *++i == a;
572 return false;
573}
574
575void
576MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000577 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000578
Lang Hames1ce837a2012-02-14 19:17:48 +0000579 if (MRI->isSSA()) {
580 // If this block has allocatable physical registers live-in, check that
581 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000582 for (const auto &LI : MBB->liveins()) {
583 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +0000584 MBB->getIterator() != MBB->getParent()->begin()) {
Lang Hames1ce837a2012-02-14 19:17:48 +0000585 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
586 }
587 }
588 }
589
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000590 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000591 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000592 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000593 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000594 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000595 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000596 if (!FunctionBlocks.count(*I))
597 report("MBB has successor that isn't part of the function.", MBB);
598 if (!MBBInfoMap[*I].Preds.count(MBB)) {
599 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000600 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000601 << (*I)->getNumber() << ".\n";
602 }
603 }
604
605 // Check the predecessor list.
606 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
607 E = MBB->pred_end(); I != E; ++I) {
608 if (!FunctionBlocks.count(*I))
609 report("MBB has predecessor that isn't part of the function.", MBB);
610 if (!MBBInfoMap[*I].Succs.count(MBB)) {
611 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000612 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000613 << (*I)->getNumber() << ".\n";
614 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000615 }
Bill Wendling2a401312011-05-04 22:54:05 +0000616
617 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
618 const BasicBlock *BB = MBB->getBasicBlock();
Reid Kleckner64b003f2015-11-09 21:04:00 +0000619 const Function *Fn = MF->getFunction();
Bill Wendling2a401312011-05-04 22:54:05 +0000620 if (LandingPadSuccs.size() > 1 &&
621 !(AsmInfo &&
622 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
Reid Kleckner64b003f2015-11-09 21:04:00 +0000623 BB && isa<SwitchInst>(BB->getTerminator())) &&
624 !isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000625 report("MBB has more than one landing pad successor", MBB);
626
Dan Gohman352a4952009-08-27 02:43:49 +0000627 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000628 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000629 SmallVector<MachineOperand, 4> Cond;
630 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
631 TBB, FBB, Cond)) {
632 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
633 // check whether its answers match up with reality.
634 if (!TBB && !FBB) {
635 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000636 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000637 ++MBBI;
638 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000639 // It's possible that the block legitimately ends with a noreturn
640 // call or an unreachable, in which case it won't actually fall
641 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000642 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000643 // It's possible that the block legitimately ends with a noreturn
644 // call or an unreachable, in which case it won't actuall fall
645 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000646 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000647 report("MBB exits via unconditional fall-through but doesn't have "
648 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000649 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000650 report("MBB exits via unconditional fall-through but its successor "
651 "differs from its CFG successor!", MBB);
652 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000653 if (!MBB->empty() && MBB->back().isBarrier() &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000654 !TII->isPredicated(MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000655 report("MBB exits via unconditional fall-through but ends with a "
656 "barrier instruction!", MBB);
657 }
658 if (!Cond.empty()) {
659 report("MBB exits via unconditional fall-through but has a condition!",
660 MBB);
661 }
662 } else if (TBB && !FBB && Cond.empty()) {
663 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000664 // If the block has exactly one successor, that happens to be a
665 // landingpad, accept it as valid control flow.
666 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
667 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
668 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000669 report("MBB exits via unconditional branch but doesn't have "
670 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000671 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000672 report("MBB exits via unconditional branch but the CFG "
673 "successor doesn't match the actual successor!", MBB);
674 }
675 if (MBB->empty()) {
676 report("MBB exits via unconditional branch but doesn't contain "
677 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000678 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000679 report("MBB exits via unconditional branch but doesn't end with a "
680 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000681 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000682 report("MBB exits via unconditional branch but the branch isn't a "
683 "terminator instruction!", MBB);
684 }
685 } else if (TBB && !FBB && !Cond.empty()) {
686 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000687 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000688 ++MBBI;
689 if (MBBI == MF->end()) {
690 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000691 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000692 // A conditional branch with only one successor is weird, but allowed.
693 if (&*MBBI != TBB)
694 report("MBB exits via conditional branch/fall-through but only has "
695 "one CFG successor!", MBB);
696 else if (TBB != *MBB->succ_begin())
697 report("MBB exits via conditional branch/fall-through but the CFG "
698 "successor don't match the actual successor!", MBB);
699 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000700 report("MBB exits via conditional branch/fall-through but doesn't have "
701 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000702 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000703 report("MBB exits via conditional branch/fall-through but the CFG "
704 "successors don't match the actual successors!", MBB);
705 }
706 if (MBB->empty()) {
707 report("MBB exits via conditional branch/fall-through but doesn't "
708 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000709 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000710 report("MBB exits via conditional branch/fall-through but ends with a "
711 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000712 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000713 report("MBB exits via conditional branch/fall-through but the branch "
714 "isn't a terminator instruction!", MBB);
715 }
716 } else if (TBB && FBB) {
717 // Block conditionally branches somewhere, otherwise branches
718 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000719 if (MBB->succ_size() == 1) {
720 // A conditional branch with only one successor is weird, but allowed.
721 if (FBB != TBB)
722 report("MBB exits via conditional branch/branch through but only has "
723 "one CFG successor!", MBB);
724 else if (TBB != *MBB->succ_begin())
725 report("MBB exits via conditional branch/branch through but the CFG "
726 "successor don't match the actual successor!", MBB);
727 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000728 report("MBB exits via conditional branch/branch but doesn't have "
729 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000730 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000731 report("MBB exits via conditional branch/branch but the CFG "
732 "successors don't match the actual successors!", MBB);
733 }
734 if (MBB->empty()) {
735 report("MBB exits via conditional branch/branch but doesn't "
736 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000737 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000738 report("MBB exits via conditional branch/branch but doesn't end with a "
739 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000740 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000741 report("MBB exits via conditional branch/branch but the branch "
742 "isn't a terminator instruction!", MBB);
743 }
744 if (Cond.empty()) {
745 report("MBB exits via conditinal branch/branch but there's no "
746 "condition!", MBB);
747 }
748 } else {
749 report("AnalyzeBranch returned invalid data!", MBB);
750 }
751 }
752
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000753 regsLive.clear();
Matthias Braund9da1622015-09-09 18:08:03 +0000754 for (const auto &LI : MBB->liveins()) {
755 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000756 report("MBB live-in list contains non-physical register", MBB);
757 continue;
758 }
Matthias Braund9da1622015-09-09 18:08:03 +0000759 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
Chad Rosierabdb1d62013-05-22 23:17:36 +0000760 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000761 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000762 }
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +0000763 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000764
765 const MachineFrameInfo *MFI = MF->getFrameInfo();
766 assert(MFI && "Function has no frame info");
Matthias Braun111f5d82015-05-28 23:20:35 +0000767 BitVector PR = MFI->getPristineRegs(*MF);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000768 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000769 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
770 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000771 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000772 }
773
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000774 regsKilled.clear();
775 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000776
777 if (Indexes)
778 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000779}
780
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000781// This function gets called for all bundle headers, including normal
782// stand-alone unbundled instructions.
783void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000784 if (Indexes && Indexes->hasIndex(*MI)) {
785 SlotIndex idx = Indexes->getInstructionIndex(*MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000786 if (!(idx > lastIndex)) {
787 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000788 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000789 }
790 lastIndex = idx;
791 }
Pete Coopercd720162012-06-07 17:41:39 +0000792
793 // Ensure non-terminators don't follow terminators.
794 // Ignore predicated terminators formed by if conversion.
795 // FIXME: If conversion shouldn't need to violate this rule.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000796 if (MI->isTerminator() && !TII->isPredicated(*MI)) {
Pete Coopercd720162012-06-07 17:41:39 +0000797 if (!FirstTerminator)
798 FirstTerminator = MI;
799 } else if (FirstTerminator) {
800 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000801 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000802 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000803}
804
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000805// The operands on an INLINEASM instruction must follow a template.
806// Verify that the flag operands make sense.
807void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
808 // The first two operands on INLINEASM are the asm string and global flags.
809 if (MI->getNumOperands() < 2) {
810 report("Too few operands on inline asm", MI);
811 return;
812 }
813 if (!MI->getOperand(0).isSymbol())
814 report("Asm string must be an external symbol", MI);
815 if (!MI->getOperand(1).isImm())
816 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000817 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
818 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
819 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000820 report("Unknown asm flags", &MI->getOperand(1), 1);
821
Gabor Horvathfee04342015-03-16 09:53:42 +0000822 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000823
824 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
825 unsigned NumOps;
826 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
827 const MachineOperand &MO = MI->getOperand(OpNo);
828 // There may be implicit ops after the fixed operands.
829 if (!MO.isImm())
830 break;
831 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
832 }
833
834 if (OpNo > MI->getNumOperands())
835 report("Missing operands in last group", MI);
836
837 // An optional MDNode follows the groups.
838 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
839 ++OpNo;
840
841 // All trailing operands must be implicit registers.
842 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
843 const MachineOperand &MO = MI->getOperand(OpNo);
844 if (!MO.isReg() || !MO.isImplicit())
845 report("Expected implicit register after groups", &MO, OpNo);
846 }
847}
848
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000849void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000850 const MCInstrDesc &MCID = MI->getDesc();
851 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000852 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000853 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000854 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000855 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000856
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000857 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000858 if (MI->isInlineAsm())
859 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000860
Dan Gohmandb9493c2009-10-07 17:36:00 +0000861 // Check the MachineMemOperands for basic consistency.
862 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
863 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000864 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000865 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000866 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000867 report("Missing mayStore flag", MI);
868 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000869
870 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000871 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000872 if (LiveInts) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000873 bool mapped = !LiveInts->isNotInMIMap(*MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000874 if (MI->isDebugValue()) {
875 if (mapped)
876 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000877 } else if (MI->isInsideBundle()) {
878 if (mapped)
879 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000880 } else {
881 if (!mapped)
882 report("Missing slot index", MI);
883 }
884 }
885
Andrew Trick924123a2011-09-21 02:20:46 +0000886 StringRef ErrorInfo;
887 if (!TII->verifyInstruction(MI, ErrorInfo))
888 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000889}
890
891void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000892MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000893 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000894 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +0000895 unsigned NumDefs = MCID.getNumDefs();
896 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
897 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000898
Evan Cheng6cc775f2011-06-28 19:10:37 +0000899 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +0000900 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000901 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000902 if (!MO->isReg())
903 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +0000904 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000905 report("Explicit definition marked as use", MO, MONum);
906 else if (MO->isImplicit())
907 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000908 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000909 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +0000910 // Don't check if it's the last operand in a variadic instruction. See,
911 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000912 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000913 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000914 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +0000915 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000916 if (MO->isImplicit())
917 report("Explicit operand marked as implicit", MO, MONum);
918 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000919
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000920 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
921 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000922 if (!MO->isReg())
923 report("Tied use must be a register", MO, MONum);
924 else if (!MO->isTied())
925 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000926 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
927 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000928 } else if (MO->isReg() && MO->isTied())
929 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000930 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +0000931 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000932 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000933 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000934 }
935
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000936 switch (MO->getType()) {
937 case MachineOperand::MO_Register: {
938 const unsigned Reg = MO->getReg();
939 if (!Reg)
940 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000941 if (MRI->tracksLiveness() && !MI->isDebugValue())
942 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000943
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000944 // Verify the consistency of tied operands.
945 if (MO->isTied()) {
946 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
947 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
948 if (!OtherMO.isReg())
949 report("Must be tied to a register", MO, MONum);
950 if (!OtherMO.isTied())
951 report("Missing tie flags on tied operand", MO, MONum);
952 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
953 report("Inconsistent tie links", MO, MONum);
954 if (MONum < MCID.getNumDefs()) {
955 if (OtherIdx < MCID.getNumOperands()) {
956 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
957 report("Explicit def tied to explicit use without tie constraint",
958 MO, MONum);
959 } else {
960 if (!OtherMO.isImplicit())
961 report("Explicit def should be tied to implicit use", MO, MONum);
962 }
963 }
964 }
965
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +0000966 // Verify two-address constraints after leaving SSA form.
967 unsigned DefIdx;
968 if (!MRI->isSSA() && MO->isUse() &&
969 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
970 Reg != MI->getOperand(DefIdx).getReg())
971 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000972
973 // Check register classes.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000974 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000975 unsigned SubIdx = MO->getSubReg();
976
977 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000978 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000979 report("Illegal subregister index for physical register", MO, MONum);
980 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000981 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000982 if (const TargetRegisterClass *DRC =
983 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000984 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000985 report("Illegal physical register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000986 errs() << TRI->getName(Reg) << " is not a "
Craig Toppercf0444b2014-11-17 05:50:14 +0000987 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000988 }
989 }
990 } else {
991 // Virtual register.
Quentin Colombetc1c94bc2016-04-08 16:35:22 +0000992 const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
993 if (!RC) {
994 // This is a generic virtual register.
995 // It must have a size and it must not have a SubIdx.
996 unsigned Size = MRI->getSize(Reg);
997 if (!Size) {
998 report("Generic virtual register must have a size", MO, MONum);
999 return;
1000 }
1001 // Make sure the register fits into its register bank if any.
1002 const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
1003 if (RegBank && RegBank->getSize() < Size) {
1004 report("Register bank is too small for virtual register", MO,
1005 MONum);
1006 errs() << "Register bank " << RegBank->getName() << " too small("
1007 << RegBank->getSize() << ") to fit " << Size << "-bits\n";
1008 return;
1009 }
1010 if (SubIdx) {
1011 report("Generic virtual register does not subregister index", MO, MONum);
1012 return;
1013 }
1014 break;
1015 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001016 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001017 const TargetRegisterClass *SRC =
1018 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001019 if (!SRC) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001020 report("Invalid subregister index for virtual register", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001021 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001022 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001023 return;
1024 }
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001025 if (RC != SRC) {
1026 report("Invalid register class for subregister index", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001027 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001028 << " does not fully support subreg index " << SubIdx << "\n";
1029 return;
1030 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001031 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001032 if (const TargetRegisterClass *DRC =
1033 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001034 if (SubIdx) {
1035 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +00001036 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001037 if (!SuperRC) {
1038 report("No largest legal super class exists.", MO, MONum);
1039 return;
1040 }
1041 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
1042 if (!DRC) {
1043 report("No matching super-reg register class.", MO, MONum);
1044 return;
1045 }
1046 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +00001047 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001048 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001049 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +00001050 << " register, but got a " << TRI->getRegClassName(RC)
1051 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001052 }
1053 }
1054 }
1055 }
1056 break;
1057 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001058
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001059 case MachineOperand::MO_RegisterMask:
1060 regMasks.push_back(MO->getRegMask());
1061 break;
1062
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001063 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +00001064 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
1065 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001066 break;
1067
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001068 case MachineOperand::MO_FrameIndex:
1069 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001070 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
Jonas Paulsson72640f12015-10-29 08:28:35 +00001071 int FI = MO->getIndex();
1072 LiveInterval &LI = LiveStks->getInterval(FI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001073 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001074
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001075 bool stores = MI->mayStore();
Jonas Paulsson72640f12015-10-29 08:28:35 +00001076 bool loads = MI->mayLoad();
1077 // For a memory-to-memory move, we need to check if the frame
1078 // index is used for storing or loading, by inspecting the
1079 // memory operands.
1080 if (stores && loads) {
1081 for (auto *MMO : MI->memoperands()) {
1082 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1083 if (PSV == nullptr) continue;
1084 const FixedStackPseudoSourceValue *Value =
1085 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1086 if (Value == nullptr) continue;
1087 if (Value->getFrameIndex() != FI) continue;
1088
1089 if (MMO->isStore())
1090 loads = false;
1091 else
1092 stores = false;
1093 break;
1094 }
1095 if (loads == stores)
1096 report("Missing fixed stack memoperand.", MI);
1097 }
1098 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001099 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001100 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001101 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001102 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001103 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001104 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001105 }
1106 }
1107 break;
1108
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001109 default:
1110 break;
1111 }
1112}
1113
Matthias Braun1377fd62016-02-02 20:04:51 +00001114void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
1115 unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
1116 LaneBitmask LaneMask) {
1117 LiveQueryResult LRQ = LR.Query(UseIdx);
1118 // Check if we have a segment at the use, note however that we only need one
1119 // live subregister range, the others may be dead.
1120 if (!LRQ.valueIn() && LaneMask == 0) {
1121 report("No live segment at use", MO, MONum);
1122 report_context_liverange(LR);
1123 report_context_vreg_regunit(VRegOrUnit);
1124 report_context(UseIdx);
1125 }
1126 if (MO->isKill() && !LRQ.isKill()) {
1127 report("Live range continues after kill flag", MO, MONum);
1128 report_context_liverange(LR);
1129 report_context_vreg_regunit(VRegOrUnit);
1130 if (LaneMask != 0)
1131 report_context_lanemask(LaneMask);
1132 report_context(UseIdx);
1133 }
1134}
1135
1136void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
1137 unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
1138 LaneBitmask LaneMask) {
1139 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
1140 assert(VNI && "NULL valno is not allowed");
1141 if (VNI->def != DefIdx) {
1142 report("Inconsistent valno->def", MO, MONum);
1143 report_context_liverange(LR);
1144 report_context_vreg_regunit(VRegOrUnit);
1145 if (LaneMask != 0)
1146 report_context_lanemask(LaneMask);
1147 report_context(*VNI);
1148 report_context(DefIdx);
1149 }
1150 } else {
1151 report("No live segment at def", MO, MONum);
1152 report_context_liverange(LR);
1153 report_context_vreg_regunit(VRegOrUnit);
1154 if (LaneMask != 0)
1155 report_context_lanemask(LaneMask);
1156 report_context(DefIdx);
1157 }
1158 // Check that, if the dead def flag is present, LiveInts agree.
1159 if (MO->isDead()) {
1160 LiveQueryResult LRQ = LR.Query(DefIdx);
1161 if (!LRQ.isDeadDef()) {
1162 // In case of physregs we can have a non-dead definition on another
1163 // operand.
1164 bool otherDef = false;
1165 if (!TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
1166 const MachineInstr &MI = *MO->getParent();
1167 for (const MachineOperand &MO : MI.operands()) {
1168 if (!MO.isReg() || !MO.isDef() || MO.isDead())
1169 continue;
1170 unsigned Reg = MO.getReg();
1171 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1172 if (*Units == VRegOrUnit) {
1173 otherDef = true;
1174 break;
1175 }
1176 }
1177 }
1178 }
1179
1180 if (!otherDef) {
1181 report("Live range continues after dead def flag", MO, MONum);
1182 report_context_liverange(LR);
1183 report_context_vreg_regunit(VRegOrUnit);
1184 if (LaneMask != 0)
1185 report_context_lanemask(LaneMask);
1186 }
1187 }
1188 }
1189}
1190
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001191void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1192 const MachineInstr *MI = MO->getParent();
1193 const unsigned Reg = MO->getReg();
1194
1195 // Both use and def operands can read a register.
1196 if (MO->readsReg()) {
1197 regsLiveInButUnused.erase(Reg);
1198
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001199 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001200 addRegWithSubRegs(regsKilled, Reg);
1201
1202 // Check that LiveVars knows this kill.
1203 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1204 MO->isKill()) {
1205 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1206 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
1207 report("Kill missing from LiveVariables", MO, MONum);
1208 }
1209
1210 // Check LiveInts liveness and kill.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001211 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1212 SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001213 // Check the cached regunit intervals.
1214 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1215 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001216 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
1217 checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001218 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001219 }
1220
1221 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1222 if (LiveInts->hasInterval(Reg)) {
1223 // This is a virtual register interval.
1224 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun1377fd62016-02-02 20:04:51 +00001225 checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
1226
1227 if (LI.hasSubRanges() && !MO->isDef()) {
1228 unsigned SubRegIdx = MO->getSubReg();
1229 LaneBitmask MOMask = SubRegIdx != 0
1230 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1231 : MRI->getMaxLaneMaskForVReg(Reg);
1232 LaneBitmask LiveInMask = 0;
1233 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1234 if ((MOMask & SR.LaneMask) == 0)
1235 continue;
1236 checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
1237 LiveQueryResult LRQ = SR.Query(UseIdx);
1238 if (LRQ.valueIn())
1239 LiveInMask |= SR.LaneMask;
1240 }
1241 // At least parts of the register has to be live at the use.
1242 if ((LiveInMask & MOMask) == 0) {
1243 report("No live subrange at use", MO, MONum);
1244 report_context(LI);
1245 report_context(UseIdx);
1246 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001247 }
1248 } else {
1249 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001250 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001251 }
1252 }
1253
1254 // Use of a dead register.
1255 if (!regsLive.count(Reg)) {
1256 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1257 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001258 bool Bad = !isReserved(Reg);
1259 // We are fine if just any subregister has a defined value.
1260 if (Bad) {
1261 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1262 ++SubRegs) {
1263 if (regsLive.count(*SubRegs)) {
1264 Bad = false;
1265 break;
1266 }
1267 }
1268 }
Matthias Braun96a31952015-01-14 22:25:14 +00001269 // If there is an additional implicit-use of a super register we stop
1270 // here. By definition we are fine if the super register is not
1271 // (completely) dead, if the complete super register is dead we will
1272 // get a report for its operand.
1273 if (Bad) {
1274 for (const MachineOperand &MOP : MI->uses()) {
1275 if (!MOP.isReg())
1276 continue;
1277 if (!MOP.isImplicit())
1278 continue;
1279 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1280 ++SubRegs) {
1281 if (*SubRegs == Reg) {
1282 Bad = false;
1283 break;
1284 }
1285 }
1286 }
1287 }
Matthias Braun96d77322014-12-10 01:13:13 +00001288 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001289 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001290 } else if (MRI->def_empty(Reg)) {
1291 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001292 } else {
1293 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1294 // We don't know which virtual registers are live in, so only complain
1295 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1296 // must be live in. PHI instructions are handled separately.
1297 if (MInfo.regsKilled.count(Reg))
1298 report("Using a killed virtual register", MO, MONum);
1299 else if (!MI->isPHI())
1300 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1301 }
1302 }
1303 }
1304
1305 if (MO->isDef()) {
1306 // Register defined.
1307 // TODO: verify that earlyclobber ops are not used.
1308 if (MO->isDead())
1309 addRegWithSubRegs(regsDead, Reg);
1310 else
1311 addRegWithSubRegs(regsDefined, Reg);
1312
1313 // Verify SSA form.
1314 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001315 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001316 report("Multiple virtual register defs in SSA form", MO, MONum);
1317
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001318 // Check LiveInts for a live segment, but only for virtual registers.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001319 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1320 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001321 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Matthias Braun1377fd62016-02-02 20:04:51 +00001322
1323 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1324 if (LiveInts->hasInterval(Reg)) {
1325 const LiveInterval &LI = LiveInts->getInterval(Reg);
1326 checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
1327
1328 if (LI.hasSubRanges()) {
1329 unsigned SubRegIdx = MO->getSubReg();
1330 LaneBitmask MOMask = SubRegIdx != 0
1331 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1332 : MRI->getMaxLaneMaskForVReg(Reg);
1333 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1334 if ((SR.LaneMask & MOMask) == 0)
1335 continue;
1336 checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, SR.LaneMask);
1337 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001338 }
1339 } else {
Matthias Braun1377fd62016-02-02 20:04:51 +00001340 report("Virtual register has no Live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001341 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001342 }
1343 }
1344 }
1345}
1346
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001347void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001348}
1349
1350// This function gets called after visiting all instructions in a bundle. The
1351// argument points to the bundle header.
1352// Normal stand-alone instructions are also considered 'bundles', and this
1353// function is called for all of them.
1354void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001355 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1356 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001357 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001358 // Kill any masked registers.
1359 while (!regMasks.empty()) {
1360 const uint32_t *Mask = regMasks.pop_back_val();
1361 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1362 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1363 MachineOperand::clobbersPhysReg(Mask, *I))
1364 regsDead.push_back(*I);
1365 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001366 set_subtract(regsLive, regsDead); regsDead.clear();
1367 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001368}
1369
1370void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001371MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001372 MBBInfoMap[MBB].regsLiveOut = regsLive;
1373 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001374
1375 if (Indexes) {
1376 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1377 if (!(stop > lastIndex)) {
1378 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001379 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001380 << " last instruction was at " << lastIndex << '\n';
1381 }
1382 lastIndex = stop;
1383 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001384}
1385
1386// Calculate the largest possible vregsPassed sets. These are the registers that
1387// can pass through an MBB live, but may not be live every time. It is assumed
1388// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001389void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001390 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1391 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001392 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001393 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001394 BBInfo &MInfo = MBBInfoMap[&MBB];
1395 if (!MInfo.reachable)
1396 continue;
1397 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1398 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1399 BBInfo &SInfo = MBBInfoMap[*SuI];
1400 if (SInfo.addPassed(MInfo.regsLiveOut))
1401 todo.insert(*SuI);
1402 }
1403 }
1404
1405 // Iteratively push vregsPassed to successors. This will converge to the same
1406 // final state regardless of DenseSet iteration order.
1407 while (!todo.empty()) {
1408 const MachineBasicBlock *MBB = *todo.begin();
1409 todo.erase(MBB);
1410 BBInfo &MInfo = MBBInfoMap[MBB];
1411 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1412 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1413 if (*SuI == MBB)
1414 continue;
1415 BBInfo &SInfo = MBBInfoMap[*SuI];
1416 if (SInfo.addPassed(MInfo.vregsPassed))
1417 todo.insert(*SuI);
1418 }
1419 }
1420}
1421
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001422// Calculate the set of virtual registers that must be passed through each basic
1423// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001424// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001425void MachineVerifier::calcRegsRequired() {
1426 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001427 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001428 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001429 BBInfo &MInfo = MBBInfoMap[&MBB];
1430 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1431 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1432 BBInfo &PInfo = MBBInfoMap[*PrI];
1433 if (PInfo.addRequired(MInfo.vregsLiveIn))
1434 todo.insert(*PrI);
1435 }
1436 }
1437
1438 // Iteratively push vregsRequired to predecessors. This will converge to the
1439 // same final state regardless of DenseSet iteration order.
1440 while (!todo.empty()) {
1441 const MachineBasicBlock *MBB = *todo.begin();
1442 todo.erase(MBB);
1443 BBInfo &MInfo = MBBInfoMap[MBB];
1444 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1445 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1446 if (*PrI == MBB)
1447 continue;
1448 BBInfo &SInfo = MBBInfoMap[*PrI];
1449 if (SInfo.addRequired(MInfo.vregsRequired))
1450 todo.insert(*PrI);
1451 }
1452 }
1453}
1454
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001455// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001456// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001457void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001458 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001459 for (const auto &BBI : *MBB) {
1460 if (!BBI.isPHI())
1461 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001462 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001463
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001464 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1465 unsigned Reg = BBI.getOperand(i).getReg();
1466 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001467 if (!Pre->isSuccessor(MBB))
1468 continue;
1469 seen.insert(Pre);
1470 BBInfo &PrInfo = MBBInfoMap[Pre];
1471 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1472 report("PHI operand is not live-out from predecessor",
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001473 &BBI.getOperand(i), i);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001474 }
1475
1476 // Did we see all predecessors?
1477 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1478 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1479 if (!seen.count(*PrI)) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001480 report("Missing PHI operand", &BBI);
Owen Anderson21b17882015-02-04 00:02:59 +00001481 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001482 << " is a predecessor according to the CFG.\n";
1483 }
1484 }
1485 }
1486}
1487
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001488void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001489 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001490
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001491 for (const auto &MBB : *MF) {
1492 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001493
1494 // Skip unreachable MBBs.
1495 if (!MInfo.reachable)
1496 continue;
1497
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001498 checkPHIOps(&MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001499 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001500
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001501 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001502 calcRegsRequired();
1503
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001504 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001505 for (const auto &MBB : *MF) {
1506 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001507 for (RegSet::iterator
1508 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1509 ++I)
1510 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001511 report("Virtual register killed in block, but needed live out.", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001512 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001513 << " is used after the block.\n";
1514 }
1515 }
1516
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001517 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001518 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1519 for (RegSet::iterator
1520 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Matthias Braun30668dd2016-05-11 21:31:39 +00001521 ++I) {
1522 report("Virtual register defs don't dominate all uses.", MF);
1523 report_context_vreg(*I);
1524 }
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001525 }
1526
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001527 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001528 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001529 if (LiveInts)
1530 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001531}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001532
1533void MachineVerifier::verifyLiveVariables() {
1534 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001535 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1536 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001537 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001538 for (const auto &MBB : *MF) {
1539 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001540
1541 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1542 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001543 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1544 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001545 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001546 << " must be live through the block.\n";
1547 }
1548 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001549 if (VI.AliveBlocks.test(MBB.getNumber())) {
1550 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001551 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001552 << " is not needed live through the block.\n";
1553 }
1554 }
1555 }
1556 }
1557}
1558
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001559void MachineVerifier::verifyLiveIntervals() {
1560 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001561 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1562 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001563
1564 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001565 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001566 continue;
1567
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001568 if (!LiveInts->hasInterval(Reg)) {
1569 report("Missing live interval for virtual register", MF);
Owen Anderson21b17882015-02-04 00:02:59 +00001570 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001571 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001572 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001573
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001574 const LiveInterval &LI = LiveInts->getInterval(Reg);
1575 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001576 verifyLiveInterval(LI);
1577 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001578
1579 // Verify all the cached regunit intervals.
1580 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001581 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1582 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001583}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001584
Matthias Braun364e6e92013-10-10 21:28:54 +00001585void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001586 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001587 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001588 if (VNI->isUnused())
1589 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001590
Matthias Braun364e6e92013-10-10 21:28:54 +00001591 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001592
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001593 if (!DefVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001594 report("Value not live at VNInfo def and not marked unused", MF);
1595 report_context(LR, Reg, LaneMask);
1596 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001597 return;
1598 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001599
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001600 if (DefVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001601 report("Live segment at def has different VNInfo", MF);
1602 report_context(LR, Reg, LaneMask);
1603 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001604 return;
1605 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001606
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001607 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1608 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001609 report("Invalid VNInfo definition index", MF);
1610 report_context(LR, Reg, LaneMask);
1611 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001612 return;
1613 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001614
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001615 if (VNI->isPHIDef()) {
1616 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001617 report("PHIDef VNInfo is not defined at MBB start", MBB);
1618 report_context(LR, Reg, LaneMask);
1619 report_context(*VNI);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001620 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001621 return;
1622 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001623
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001624 // Non-PHI def.
1625 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1626 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001627 report("No instruction at VNInfo def index", MBB);
1628 report_context(LR, Reg, LaneMask);
1629 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001630 return;
1631 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001632
Matthias Braun364e6e92013-10-10 21:28:54 +00001633 if (Reg != 0) {
1634 bool hasDef = false;
1635 bool isEarlyClobber = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001636 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001637 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001638 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001639 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1640 if (MOI->getReg() != Reg)
1641 continue;
1642 } else {
1643 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1644 !TRI->hasRegUnit(MOI->getReg(), Reg))
1645 continue;
1646 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001647 if (LaneMask != 0 &&
1648 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask) == 0)
1649 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001650 hasDef = true;
1651 if (MOI->isEarlyClobber())
1652 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001653 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001654
Matthias Braun364e6e92013-10-10 21:28:54 +00001655 if (!hasDef) {
1656 report("Defining instruction does not modify register", MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001657 report_context(LR, Reg, LaneMask);
1658 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001659 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001660
Matthias Braun364e6e92013-10-10 21:28:54 +00001661 // Early clobber defs begin at USE slots, but other defs must begin at
1662 // DEF slots.
1663 if (isEarlyClobber) {
1664 if (!VNI->def.isEarlyClobber()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001665 report("Early clobber def must be at an early-clobber slot", MBB);
1666 report_context(LR, Reg, LaneMask);
1667 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001668 }
1669 } else if (!VNI->def.isRegister()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001670 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
1671 report_context(LR, Reg, LaneMask);
1672 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001673 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001674 }
1675}
1676
Matthias Braun364e6e92013-10-10 21:28:54 +00001677void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1678 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001679 unsigned Reg, LaneBitmask LaneMask)
1680{
Matthias Braun364e6e92013-10-10 21:28:54 +00001681 const LiveRange::Segment &S = *I;
1682 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001683 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001684
Matthias Braun364e6e92013-10-10 21:28:54 +00001685 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001686 report("Foreign valno in live segment", MF);
1687 report_context(LR, Reg, LaneMask);
1688 report_context(S);
1689 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001690 }
1691
1692 if (VNI->isUnused()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001693 report("Live segment valno is marked unused", MF);
1694 report_context(LR, Reg, LaneMask);
1695 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001696 }
1697
Matthias Braun364e6e92013-10-10 21:28:54 +00001698 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001699 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001700 report("Bad start of live segment, no basic block", MF);
1701 report_context(LR, Reg, LaneMask);
1702 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001703 return;
1704 }
1705 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001706 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001707 report("Live segment must begin at MBB entry or valno def", MBB);
1708 report_context(LR, Reg, LaneMask);
1709 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001710 }
1711
1712 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001713 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001714 if (!EndMBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001715 report("Bad end of live segment, no basic block", MF);
1716 report_context(LR, Reg, LaneMask);
1717 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001718 return;
1719 }
1720
1721 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001722 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001723 return;
1724
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001725 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001726 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1727 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001728 return;
1729
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001730 // The live segment is ending inside EndMBB
1731 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001732 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001733 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001734 report("Live segment doesn't end at a valid instruction", EndMBB);
1735 report_context(LR, Reg, LaneMask);
1736 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001737 return;
1738 }
1739
1740 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001741 if (S.end.isBlock()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001742 report("Live segment ends at B slot of an instruction", EndMBB);
1743 report_context(LR, Reg, LaneMask);
1744 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001745 }
1746
Matthias Braun364e6e92013-10-10 21:28:54 +00001747 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001748 // Segment ends on the dead slot.
1749 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001750 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001751 report("Live segment ending at dead slot spans instructions", EndMBB);
1752 report_context(LR, Reg, LaneMask);
1753 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001754 }
1755 }
1756
1757 // A live segment can only end at an early-clobber slot if it is being
1758 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001759 if (S.end.isEarlyClobber()) {
1760 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001761 report("Live segment ending at early clobber slot must be "
Matthias Braun7e624d52015-11-09 23:59:33 +00001762 "redefined by an EC def in the same instruction", EndMBB);
1763 report_context(LR, Reg, LaneMask);
1764 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001765 }
1766 }
1767
1768 // The following checks only apply to virtual registers. Physreg liveness
1769 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001770 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001771 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001772 // use, or a dead flag on a def.
1773 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001774 bool hasSubRegDef = false;
Matthias Braun72a58c32016-03-29 19:07:43 +00001775 bool hasDeadDef = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001776 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001777 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001778 continue;
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001779 if (LaneMask != 0 &&
1780 (LaneMask & TRI->getSubRegIndexLaneMask(MOI->getSubReg())) == 0)
1781 continue;
Matthias Braun72a58c32016-03-29 19:07:43 +00001782 if (MOI->isDef()) {
1783 if (MOI->getSubReg() != 0)
1784 hasSubRegDef = true;
1785 if (MOI->isDead())
1786 hasDeadDef = true;
1787 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001788 if (MOI->readsReg())
1789 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001790 }
Matthias Braun72a58c32016-03-29 19:07:43 +00001791 if (S.end.isDead()) {
1792 // Make sure that the corresponding machine operand for a "dead" live
1793 // range has the dead flag. We cannot perform this check for subregister
1794 // liveranges as partially dead values are allowed.
1795 if (LaneMask == 0 && !hasDeadDef) {
1796 report("Instruction ending live segment on dead slot has no dead flag",
1797 MI);
1798 report_context(LR, Reg, LaneMask);
1799 report_context(S);
1800 }
1801 } else {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001802 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001803 // When tracking subregister liveness, the main range must start new
1804 // values on partial register writes, even if there is no read.
Matthias Brauna25e13a2015-03-19 00:21:58 +00001805 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask != 0 ||
1806 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001807 report("Instruction ending live segment doesn't read the register",
1808 MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001809 report_context(LR, Reg, LaneMask);
1810 report_context(S);
Matthias Braun21554d92014-12-10 01:13:11 +00001811 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001812 }
1813 }
1814 }
1815
1816 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001817 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001818 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00001819 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001820 // Not live-in to any blocks.
1821 if (MBB == EndMBB)
1822 return;
1823 // Skip this block.
1824 ++MFI;
1825 }
1826 for (;;) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001827 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001828 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00001829 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00001830 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001831 if (&*MFI == EndMBB)
1832 break;
1833 ++MFI;
1834 continue;
1835 }
1836
1837 // Is VNI a PHI-def in the current block?
1838 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001839 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001840
1841 // Check that VNI is live-out of all predecessors.
1842 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1843 PE = MFI->pred_end(); PI != PE; ++PI) {
1844 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001845 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001846
1847 // All predecessors must have a live-out value.
1848 if (!PVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001849 report("Register not marked live out of predecessor", *PI);
1850 report_context(LR, Reg, LaneMask);
1851 report_context(*VNI);
1852 errs() << " live into BB#" << MFI->getNumber()
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001853 << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
1854 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001855 continue;
1856 }
1857
1858 // Only PHI-defs can take different predecessor values.
1859 if (!IsPHI && PVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001860 report("Different value live out of predecessor", *PI);
1861 report_context(LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001862 errs() << "Valno #" << PVNI->id << " live out of BB#"
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001863 << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
1864 << " live into BB#" << MFI->getNumber() << '@'
1865 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001866 }
1867 }
1868 if (&*MFI == EndMBB)
1869 break;
1870 ++MFI;
1871 }
1872}
1873
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001874void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001875 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00001876 for (const VNInfo *VNI : LR.valnos)
1877 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001878
Matthias Braun364e6e92013-10-10 21:28:54 +00001879 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001880 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00001881}
1882
1883void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001884 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00001885 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1886 verifyLiveRange(LI, Reg);
1887
Matthias Braune6a24852015-09-25 21:51:14 +00001888 LaneBitmask Mask = 0;
1889 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00001890 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001891 if ((Mask & SR.LaneMask) != 0) {
1892 report("Lane masks of sub ranges overlap in live interval", MF);
1893 report_context(LI);
1894 }
1895 if ((SR.LaneMask & ~MaxMask) != 0) {
1896 report("Subrange lanemask is invalid", MF);
1897 report_context(LI);
1898 }
1899 if (SR.empty()) {
1900 report("Subrange must not be empty", MF);
1901 report_context(SR, LI.reg, SR.LaneMask);
1902 }
Matthias Braune962e522015-03-25 21:18:22 +00001903 Mask |= SR.LaneMask;
1904 verifyLiveRange(SR, LI.reg, SR.LaneMask);
Matthias Braun7e624d52015-11-09 23:59:33 +00001905 if (!LI.covers(SR)) {
1906 report("A Subrange is not covered by the main range", MF);
1907 report_context(LI);
1908 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001909 }
1910
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001911 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00001912 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
Matthias Braunbf47f632016-01-08 01:16:35 +00001913 unsigned NumComp = ConEQ.Classify(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001914 if (NumComp > 1) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001915 report("Multiple connected components in live interval", MF);
1916 report_context(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001917 for (unsigned comp = 0; comp != NumComp; ++comp) {
1918 errs() << comp << ": valnos";
1919 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1920 E = LI.vni_end(); I!=E; ++I)
1921 if (comp == ConEQ.getEqClass(*I))
1922 errs() << ' ' << (*I)->id;
1923 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00001924 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001925 }
1926}
Manman Renaa6875b2013-07-15 21:26:31 +00001927
1928namespace {
1929 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1930 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1931 // value is zero.
1932 // We use a bool plus an integer to capture the stack state.
1933 struct StackStateOfBB {
1934 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1935 ExitIsSetup(false) { }
1936 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1937 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1938 ExitIsSetup(ExitSetup) { }
1939 // Can be negative, which means we are setting up a frame.
1940 int EntryValue;
1941 int ExitValue;
1942 bool EntryIsSetup;
1943 bool ExitIsSetup;
1944 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001945}
Manman Renaa6875b2013-07-15 21:26:31 +00001946
1947/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1948/// by a FrameDestroy <n>, stack adjustments are identical on all
1949/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1950void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001951 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1952 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Manman Renaa6875b2013-07-15 21:26:31 +00001953
1954 SmallVector<StackStateOfBB, 8> SPState;
1955 SPState.resize(MF->getNumBlockIDs());
1956 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1957
1958 // Visit the MBBs in DFS order.
1959 for (df_ext_iterator<const MachineFunction*,
1960 SmallPtrSet<const MachineBasicBlock*, 8> >
1961 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1962 DFI != DFE; ++DFI) {
1963 const MachineBasicBlock *MBB = *DFI;
1964
1965 StackStateOfBB BBState;
1966 // Check the exit state of the DFS stack predecessor.
1967 if (DFI.getPathLength() >= 2) {
1968 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1969 assert(Reachable.count(StackPred) &&
1970 "DFS stack predecessor is already visited.\n");
1971 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1972 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1973 BBState.ExitValue = BBState.EntryValue;
1974 BBState.ExitIsSetup = BBState.EntryIsSetup;
1975 }
1976
1977 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001978 for (const auto &I : *MBB) {
1979 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001980 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001981 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001982 assert(Size >= 0 &&
1983 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1984
1985 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001986 report("FrameSetup is after another FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001987 BBState.ExitValue -= Size;
1988 BBState.ExitIsSetup = true;
1989 }
1990
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001991 if (I.getOpcode() == FrameDestroyOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001992 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001993 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001994 assert(Size >= 0 &&
1995 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1996
1997 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001998 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001999 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
2000 BBState.ExitValue;
2001 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002002 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00002003 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00002004 << AbsSPAdj << ">.\n";
2005 }
2006 BBState.ExitValue += Size;
2007 BBState.ExitIsSetup = false;
2008 }
2009 }
2010 SPState[MBB->getNumber()] = BBState;
2011
2012 // Make sure the exit state of any predecessor is consistent with the entry
2013 // state.
2014 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
2015 E = MBB->pred_end(); I != E; ++I) {
2016 if (Reachable.count(*I) &&
2017 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
2018 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
2019 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002020 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002021 << SPState[(*I)->getNumber()].ExitValue << ", "
2022 << SPState[(*I)->getNumber()].ExitIsSetup
2023 << "), while BB#" << MBB->getNumber() << " has entry state ("
2024 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
2025 }
2026 }
2027
2028 // Make sure the entry state of any successor is consistent with the exit
2029 // state.
2030 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
2031 E = MBB->succ_end(); I != E; ++I) {
2032 if (Reachable.count(*I) &&
2033 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
2034 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
2035 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002036 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002037 << SPState[(*I)->getNumber()].EntryValue << ", "
2038 << SPState[(*I)->getNumber()].EntryIsSetup
2039 << "), while BB#" << MBB->getNumber() << " has exit state ("
2040 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
2041 }
2042 }
2043
2044 // Make sure a basic block with return ends with zero stack adjustment.
2045 if (!MBB->empty() && MBB->back().isReturn()) {
2046 if (BBState.ExitIsSetup)
2047 report("A return block ends with a FrameSetup.", MBB);
2048 if (BBState.ExitValue)
2049 report("A return block ends with a nonzero stack adjustment.", MBB);
2050 }
2051 }
2052}