blob: 8c45ce2648aff37ebe7df4cf3ffa9c3258af5e1f [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;
Matt Arsenault892fcd02016-07-25 19:39:01 +0000216 void report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000217 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;
Matthias Braun1377fd62016-02-02 20:04:51 +0000222 void report_context_lanemask(LaneBitmask LaneMask) const;
Matthias Braun30668dd2016-05-11 21:31:39 +0000223 void report_context_vreg(unsigned VReg) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000224 void report_context_vreg_regunit(unsigned VRegOrRegUnit) const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000225
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000226 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000227
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000228 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Matthias Braun1377fd62016-02-02 20:04:51 +0000229 void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
230 SlotIndex UseIdx, const LiveRange &LR, unsigned Reg,
231 LaneBitmask LaneMask = 0);
232 void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
233 SlotIndex DefIdx, const LiveRange &LR, unsigned Reg,
234 LaneBitmask LaneMask = 0);
235
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000236 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000237 void calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000238 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000239
240 void calcRegsRequired();
241 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000242 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000243 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000244 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
245 unsigned);
Matthias Braun364e6e92013-10-10 21:28:54 +0000246 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000247 const LiveRange::const_iterator I, unsigned,
248 unsigned);
Matthias Braune6a24852015-09-25 21:51:14 +0000249 void verifyLiveRange(const LiveRange&, unsigned, LaneBitmask LaneMask = 0);
Manman Renaa6875b2013-07-15 21:26:31 +0000250
251 void verifyStackFrame();
Matthias Braun80595462015-09-09 17:49:46 +0000252
253 void verifySlotIndexes() const;
Derek Schuff42666ee2016-03-29 17:40:22 +0000254 void verifyProperties(const MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000255 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000256
257 struct MachineVerifierPass : public MachineFunctionPass {
258 static char ID; // Pass ID, replacement for typeid
Matthias Brauna4e932d2014-12-11 19:41:51 +0000259 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000260
Matthias Brauna4e932d2014-12-11 19:41:51 +0000261 MachineVerifierPass(const std::string &banner = nullptr)
262 : MachineFunctionPass(ID), Banner(banner) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000263 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
264 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000265
Craig Topper4584cd52014-03-07 09:26:03 +0000266 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000267 AU.setPreservesAll();
268 MachineFunctionPass::getAnalysisUsage(AU);
269 }
270
Craig Topper4584cd52014-03-07 09:26:03 +0000271 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Braunb3aefc32016-02-15 19:25:31 +0000272 unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
273 if (FoundErrors)
274 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000275 return false;
276 }
277 };
278
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000279}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000280
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000281char MachineVerifierPass::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +0000282INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000283 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000284
Matthias Brauna4e932d2014-12-11 19:41:51 +0000285FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000286 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000287}
288
Matthias Braunb3aefc32016-02-15 19:25:31 +0000289bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
290 const {
291 MachineFunction &MF = const_cast<MachineFunction&>(*this);
292 unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
293 if (AbortOnErrors && FoundErrors)
294 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
295 return FoundErrors == 0;
Jakob Stoklund Olesen27440e72009-11-13 21:56:09 +0000296}
297
Matthias Braun80595462015-09-09 17:49:46 +0000298void MachineVerifier::verifySlotIndexes() const {
299 if (Indexes == nullptr)
300 return;
301
302 // Ensure the IdxMBB list is sorted by slot indexes.
303 SlotIndex Last;
304 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
305 E = Indexes->MBBIndexEnd(); I != E; ++I) {
306 assert(!Last.isValid() || I->first > Last);
307 Last = I->first;
308 }
309}
310
Derek Schuff42666ee2016-03-29 17:40:22 +0000311void MachineVerifier::verifyProperties(const MachineFunction &MF) {
312 // If a pass has introduced virtual registers without clearing the
313 // AllVRegsAllocated property (or set it without allocating the vregs)
314 // then report an error.
315 if (MF.getProperties().hasProperty(
316 MachineFunctionProperties::Property::AllVRegsAllocated) &&
317 MRI->getNumVirtRegs()) {
318 report(
319 "Function has AllVRegsAllocated property but there are VReg operands",
320 &MF);
321 }
322}
323
Matthias Braunb3aefc32016-02-15 19:25:31 +0000324unsigned MachineVerifier::verify(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000325 foundErrors = 0;
326
327 this->MF = &MF;
328 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000329 TII = MF.getSubtarget().getInstrInfo();
330 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000331 MRI = &MF.getRegInfo();
332
Craig Topperc0196b12014-04-14 00:51:57 +0000333 LiveVars = nullptr;
334 LiveInts = nullptr;
335 LiveStks = nullptr;
336 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000337 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000338 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000339 // We don't want to verify LiveVariables if LiveIntervals is available.
340 if (!LiveInts)
341 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000342 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000343 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000344 }
345
Matthias Braun80595462015-09-09 17:49:46 +0000346 verifySlotIndexes();
347
Derek Schuff42666ee2016-03-29 17:40:22 +0000348 verifyProperties(MF);
349
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000350 visitMachineFunctionBefore();
351 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
352 MFI!=MFE; ++MFI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000353 visitMachineBasicBlockBefore(&*MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000354 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000355 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000356 // Do we expect the next instruction to be part of the same bundle?
357 bool InBundle = false;
358
Evan Cheng7fae11b2011-12-14 02:11:42 +0000359 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
360 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000361 if (MBBI->getParent() != &*MFI) {
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000362 report("Bad instruction parent pointer", MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000363 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000364 continue;
365 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000366
367 // Check for consistent bundle flags.
368 if (InBundle && !MBBI->isBundledWithPred())
369 report("Missing BundledPred flag, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000370 "BundledSucc was set on predecessor",
371 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000372 if (!InBundle && MBBI->isBundledWithPred())
373 report("BundledPred flag is set, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000374 "but BundledSucc not set on predecessor",
375 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000376
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000377 // Is this a bundle header?
378 if (!MBBI->isInsideBundle()) {
379 if (CurBundle)
380 visitMachineBundleAfter(CurBundle);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000381 CurBundle = &*MBBI;
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000382 visitMachineBundleBefore(CurBundle);
383 } else if (!CurBundle)
384 report("No bundle header", MBBI);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000385 visitMachineInstrBefore(&*MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000386 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
387 const MachineInstr &MI = *MBBI;
388 const MachineOperand &Op = MI.getOperand(I);
389 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000390 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000391 // functions when replacing operands of a MachineInstr.
392 report("Instruction has operand with wrong parent set", &MI);
393 }
394
395 visitMachineOperand(&Op, I);
396 }
397
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000398 visitMachineInstrAfter(&*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000399
400 // Was this the last bundled instruction?
401 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000402 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000403 if (CurBundle)
404 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000405 if (InBundle)
406 report("BundledSucc flag set on last instruction in block", &MFI->back());
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000407 visitMachineBasicBlockAfter(&*MFI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000408 }
409 visitMachineFunctionAfter();
410
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000411 // Clean up.
412 regsLive.clear();
413 regsDefined.clear();
414 regsDead.clear();
415 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000416 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000417 regsLiveInButUnused.clear();
418 MBBInfoMap.clear();
419
Matthias Braunb3aefc32016-02-15 19:25:31 +0000420 return foundErrors;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000421}
422
Chris Lattner75f40452009-08-23 01:03:30 +0000423void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000424 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000425 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000426 if (!foundErrors++) {
427 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000428 errs() << "# " << Banner << '\n';
Matthias Braun42b4b632015-11-09 23:59:23 +0000429 if (LiveInts != nullptr)
430 LiveInts->print(errs());
431 else
432 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000433 }
Owen Anderson21b17882015-02-04 00:02:59 +0000434 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000435 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000436}
437
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000438void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000439 assert(MBB);
440 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000441 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000442 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000443 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000444 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000445 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000446 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000447 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000448}
449
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000450void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000451 assert(MI);
452 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000453 errs() << "- instruction: ";
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000454 if (Indexes && Indexes->hasIndex(*MI))
455 errs() << Indexes->getInstructionIndex(*MI) << '\t';
Matthias Braun45718db2015-11-09 23:59:25 +0000456 MI->print(errs(), /*SkipOpers=*/true);
Matthias Braun716b4332015-11-09 23:59:29 +0000457 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000458}
459
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000460void MachineVerifier::report(const char *msg,
461 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000462 assert(MO);
463 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000464 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000465 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000466 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000467}
468
Matthias Braun579c9cd2016-02-02 02:44:25 +0000469void MachineVerifier::report_context(SlotIndex Pos) const {
470 errs() << "- at: " << Pos << '\n';
471}
472
Matthias Braun7e624d52015-11-09 23:59:33 +0000473void MachineVerifier::report_context(const LiveInterval &LI) const {
Owen Anderson21b17882015-02-04 00:02:59 +0000474 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000475}
476
Matt Arsenault892fcd02016-07-25 19:39:01 +0000477void MachineVerifier::report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000478 LaneBitmask LaneMask) const {
Matthias Braun579c9cd2016-02-02 02:44:25 +0000479 report_context_liverange(LR);
Matt Arsenault892fcd02016-07-25 19:39:01 +0000480 report_context_vreg_regunit(VRegUnit);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000481 if (LaneMask != 0)
Matthias Braun1377fd62016-02-02 20:04:51 +0000482 report_context_lanemask(LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000483}
484
Matthias Braun7e624d52015-11-09 23:59:33 +0000485void MachineVerifier::report_context(const LiveRange::Segment &S) const {
486 errs() << "- segment: " << S << '\n';
487}
488
489void MachineVerifier::report_context(const VNInfo &VNI) const {
490 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
Matthias Braun364e6e92013-10-10 21:28:54 +0000491}
492
Matthias Braun579c9cd2016-02-02 02:44:25 +0000493void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
494 errs() << "- liverange: " << LR << '\n';
495}
496
Matthias Braun30668dd2016-05-11 21:31:39 +0000497void MachineVerifier::report_context_vreg(unsigned VReg) const {
498 errs() << "- v. register: " << PrintReg(VReg, TRI) << '\n';
499}
500
Matthias Braun1377fd62016-02-02 20:04:51 +0000501void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
502 if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
Matthias Braun30668dd2016-05-11 21:31:39 +0000503 report_context_vreg(VRegOrUnit);
Matthias Braun1377fd62016-02-02 20:04:51 +0000504 } else {
505 errs() << "- regunit: " << PrintRegUnit(VRegOrUnit, TRI) << '\n';
506 }
507}
508
509void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
510 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
511}
512
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000513void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000514 BBInfo &MInfo = MBBInfoMap[MBB];
515 if (!MInfo.reachable) {
516 MInfo.reachable = true;
517 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
518 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
519 markReachable(*SuI);
520 }
521}
522
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000523void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000524 lastIndex = SlotIndex();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000525 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000526
527 // A sub-register of a reserved register is also reserved
528 for (int Reg = regsReserved.find_first(); Reg>=0;
529 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000530 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000531 // FIXME: This should probably be:
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000532 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
533 regsReserved.set(*SubRegs);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000534 }
535 }
Lang Hames1ce837a2012-02-14 19:17:48 +0000536
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000537 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000538
539 // Build a set of the basic blocks in the function.
540 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000541 for (const auto &MBB : *MF) {
542 FunctionBlocks.insert(&MBB);
543 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000544
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000545 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
546 if (MInfo.Preds.size() != MBB.pred_size())
547 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000548
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000549 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
550 if (MInfo.Succs.size() != MBB.succ_size())
551 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000552 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000553
554 // Check that the register use lists are sane.
555 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000556
557 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000558}
559
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000560// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000561static bool matchPair(MachineBasicBlock::const_succ_iterator i,
562 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000563 if (*i == a)
564 return *++i == b;
565 if (*i == b)
566 return *++i == a;
567 return false;
568}
569
570void
571MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000572 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000573
Lang Hames1ce837a2012-02-14 19:17:48 +0000574 if (MRI->isSSA()) {
575 // If this block has allocatable physical registers live-in, check that
576 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000577 for (const auto &LI : MBB->liveins()) {
578 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +0000579 MBB->getIterator() != MBB->getParent()->begin()) {
Lang Hames1ce837a2012-02-14 19:17:48 +0000580 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
581 }
582 }
583 }
584
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000585 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000586 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000587 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000588 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000589 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000590 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000591 if (!FunctionBlocks.count(*I))
592 report("MBB has successor that isn't part of the function.", MBB);
593 if (!MBBInfoMap[*I].Preds.count(MBB)) {
594 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000595 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000596 << (*I)->getNumber() << ".\n";
597 }
598 }
599
600 // Check the predecessor list.
601 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
602 E = MBB->pred_end(); I != E; ++I) {
603 if (!FunctionBlocks.count(*I))
604 report("MBB has predecessor that isn't part of the function.", MBB);
605 if (!MBBInfoMap[*I].Succs.count(MBB)) {
606 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000607 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000608 << (*I)->getNumber() << ".\n";
609 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000610 }
Bill Wendling2a401312011-05-04 22:54:05 +0000611
612 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
613 const BasicBlock *BB = MBB->getBasicBlock();
Reid Kleckner64b003f2015-11-09 21:04:00 +0000614 const Function *Fn = MF->getFunction();
Bill Wendling2a401312011-05-04 22:54:05 +0000615 if (LandingPadSuccs.size() > 1 &&
616 !(AsmInfo &&
617 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
Reid Kleckner64b003f2015-11-09 21:04:00 +0000618 BB && isa<SwitchInst>(BB->getTerminator())) &&
619 !isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000620 report("MBB has more than one landing pad successor", MBB);
621
Dan Gohman352a4952009-08-27 02:43:49 +0000622 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000623 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000624 SmallVector<MachineOperand, 4> Cond;
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000625 if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
626 Cond)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000627 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
628 // check whether its answers match up with reality.
629 if (!TBB && !FBB) {
630 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000631 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000632 ++MBBI;
633 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000634 // It's possible that the block legitimately ends with a noreturn
635 // call or an unreachable, in which case it won't actually fall
636 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000637 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000638 // It's possible that the block legitimately ends with a noreturn
639 // call or an unreachable, in which case it won't actuall fall
640 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000641 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000642 report("MBB exits via unconditional fall-through but doesn't have "
643 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000644 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000645 report("MBB exits via unconditional fall-through but its successor "
646 "differs from its CFG successor!", MBB);
647 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000648 if (!MBB->empty() && MBB->back().isBarrier() &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000649 !TII->isPredicated(MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000650 report("MBB exits via unconditional fall-through but ends with a "
651 "barrier instruction!", MBB);
652 }
653 if (!Cond.empty()) {
654 report("MBB exits via unconditional fall-through but has a condition!",
655 MBB);
656 }
657 } else if (TBB && !FBB && Cond.empty()) {
658 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000659 // If the block has exactly one successor, that happens to be a
660 // landingpad, accept it as valid control flow.
661 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
662 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
663 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000664 report("MBB exits via unconditional branch but doesn't have "
665 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000666 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000667 report("MBB exits via unconditional branch but the CFG "
668 "successor doesn't match the actual successor!", MBB);
669 }
670 if (MBB->empty()) {
671 report("MBB exits via unconditional branch but doesn't contain "
672 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000673 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000674 report("MBB exits via unconditional branch but doesn't end with a "
675 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000676 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000677 report("MBB exits via unconditional branch but the branch isn't a "
678 "terminator instruction!", MBB);
679 }
680 } else if (TBB && !FBB && !Cond.empty()) {
681 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000682 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000683 ++MBBI;
684 if (MBBI == MF->end()) {
685 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000686 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000687 // A conditional branch with only one successor is weird, but allowed.
688 if (&*MBBI != TBB)
689 report("MBB exits via conditional branch/fall-through but only has "
690 "one CFG successor!", MBB);
691 else if (TBB != *MBB->succ_begin())
692 report("MBB exits via conditional branch/fall-through but the CFG "
693 "successor don't match the actual successor!", MBB);
694 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000695 report("MBB exits via conditional branch/fall-through but doesn't have "
696 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000697 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000698 report("MBB exits via conditional branch/fall-through but the CFG "
699 "successors don't match the actual successors!", MBB);
700 }
701 if (MBB->empty()) {
702 report("MBB exits via conditional branch/fall-through but doesn't "
703 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000704 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000705 report("MBB exits via conditional branch/fall-through but ends with a "
706 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000707 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000708 report("MBB exits via conditional branch/fall-through but the branch "
709 "isn't a terminator instruction!", MBB);
710 }
711 } else if (TBB && FBB) {
712 // Block conditionally branches somewhere, otherwise branches
713 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000714 if (MBB->succ_size() == 1) {
715 // A conditional branch with only one successor is weird, but allowed.
716 if (FBB != TBB)
717 report("MBB exits via conditional branch/branch through but only has "
718 "one CFG successor!", MBB);
719 else if (TBB != *MBB->succ_begin())
720 report("MBB exits via conditional branch/branch through but the CFG "
721 "successor don't match the actual successor!", MBB);
722 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000723 report("MBB exits via conditional branch/branch but doesn't have "
724 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000725 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000726 report("MBB exits via conditional branch/branch but the CFG "
727 "successors don't match the actual successors!", MBB);
728 }
729 if (MBB->empty()) {
730 report("MBB exits via conditional branch/branch but doesn't "
731 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000732 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000733 report("MBB exits via conditional branch/branch but doesn't end with a "
734 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000735 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000736 report("MBB exits via conditional branch/branch but the branch "
737 "isn't a terminator instruction!", MBB);
738 }
739 if (Cond.empty()) {
740 report("MBB exits via conditinal branch/branch but there's no "
741 "condition!", MBB);
742 }
743 } else {
744 report("AnalyzeBranch returned invalid data!", MBB);
745 }
746 }
747
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000748 regsLive.clear();
Matthias Braund9da1622015-09-09 18:08:03 +0000749 for (const auto &LI : MBB->liveins()) {
750 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000751 report("MBB live-in list contains non-physical register", MBB);
752 continue;
753 }
Matthias Braund9da1622015-09-09 18:08:03 +0000754 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
Chad Rosierabdb1d62013-05-22 23:17:36 +0000755 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000756 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000757 }
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +0000758 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000759
Matthias Braun941a7052016-07-28 18:40:00 +0000760 const MachineFrameInfo &MFI = MF->getFrameInfo();
761 BitVector PR = MFI.getPristineRegs(*MF);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000762 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000763 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
764 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000765 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000766 }
767
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000768 regsKilled.clear();
769 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000770
771 if (Indexes)
772 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000773}
774
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000775// This function gets called for all bundle headers, including normal
776// stand-alone unbundled instructions.
777void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000778 if (Indexes && Indexes->hasIndex(*MI)) {
779 SlotIndex idx = Indexes->getInstructionIndex(*MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000780 if (!(idx > lastIndex)) {
781 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000782 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000783 }
784 lastIndex = idx;
785 }
Pete Coopercd720162012-06-07 17:41:39 +0000786
787 // Ensure non-terminators don't follow terminators.
788 // Ignore predicated terminators formed by if conversion.
789 // FIXME: If conversion shouldn't need to violate this rule.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000790 if (MI->isTerminator() && !TII->isPredicated(*MI)) {
Pete Coopercd720162012-06-07 17:41:39 +0000791 if (!FirstTerminator)
792 FirstTerminator = MI;
793 } else if (FirstTerminator) {
794 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000795 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000796 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000797}
798
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000799// The operands on an INLINEASM instruction must follow a template.
800// Verify that the flag operands make sense.
801void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
802 // The first two operands on INLINEASM are the asm string and global flags.
803 if (MI->getNumOperands() < 2) {
804 report("Too few operands on inline asm", MI);
805 return;
806 }
807 if (!MI->getOperand(0).isSymbol())
808 report("Asm string must be an external symbol", MI);
809 if (!MI->getOperand(1).isImm())
810 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000811 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
Wei Ding0526e7f2016-06-22 18:51:08 +0000812 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
813 // and Extra_IsConvergent = 32.
814 if (!isUInt<6>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000815 report("Unknown asm flags", &MI->getOperand(1), 1);
816
Gabor Horvathfee04342015-03-16 09:53:42 +0000817 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000818
819 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
820 unsigned NumOps;
821 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
822 const MachineOperand &MO = MI->getOperand(OpNo);
823 // There may be implicit ops after the fixed operands.
824 if (!MO.isImm())
825 break;
826 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
827 }
828
829 if (OpNo > MI->getNumOperands())
830 report("Missing operands in last group", MI);
831
832 // An optional MDNode follows the groups.
833 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
834 ++OpNo;
835
836 // All trailing operands must be implicit registers.
837 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
838 const MachineOperand &MO = MI->getOperand(OpNo);
839 if (!MO.isReg() || !MO.isImplicit())
840 report("Expected implicit register after groups", &MO, OpNo);
841 }
842}
843
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000844void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000845 const MCInstrDesc &MCID = MI->getDesc();
846 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000847 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000848 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000849 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000850 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000851
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000852 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000853 if (MI->isInlineAsm())
854 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000855
Dan Gohmandb9493c2009-10-07 17:36:00 +0000856 // Check the MachineMemOperands for basic consistency.
857 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
858 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000859 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000860 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000861 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000862 report("Missing mayStore flag", MI);
863 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000864
865 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000866 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000867 if (LiveInts) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000868 bool mapped = !LiveInts->isNotInMIMap(*MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000869 if (MI->isDebugValue()) {
870 if (mapped)
871 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000872 } else if (MI->isInsideBundle()) {
873 if (mapped)
874 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000875 } else {
876 if (!mapped)
877 report("Missing slot index", MI);
878 }
879 }
880
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000881 // Check types.
882 const unsigned NumTypes = MI->getNumTypes();
883 if (isPreISelGenericOpcode(MCID.getOpcode())) {
884 if (NumTypes == 0)
885 report("Generic instruction must have a type", MI);
886 } else {
887 if (NumTypes != 0)
888 report("Non-generic instruction cannot have a type", MI);
889 }
890
Andrew Trick924123a2011-09-21 02:20:46 +0000891 StringRef ErrorInfo;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000892 if (!TII->verifyInstruction(*MI, ErrorInfo))
Andrew Trick924123a2011-09-21 02:20:46 +0000893 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000894}
895
896void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000897MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000898 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000899 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +0000900 unsigned NumDefs = MCID.getNumDefs();
901 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
902 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000903
Evan Cheng6cc775f2011-06-28 19:10:37 +0000904 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +0000905 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000906 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000907 if (!MO->isReg())
908 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +0000909 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000910 report("Explicit definition marked as use", MO, MONum);
911 else if (MO->isImplicit())
912 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000913 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000914 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +0000915 // Don't check if it's the last operand in a variadic instruction. See,
916 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000917 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000918 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000919 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +0000920 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000921 if (MO->isImplicit())
922 report("Explicit operand marked as implicit", MO, MONum);
923 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000924
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000925 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
926 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000927 if (!MO->isReg())
928 report("Tied use must be a register", MO, MONum);
929 else if (!MO->isTied())
930 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000931 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
932 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000933 } else if (MO->isReg() && MO->isTied())
934 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000935 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +0000936 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000937 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000938 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000939 }
940
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000941 switch (MO->getType()) {
942 case MachineOperand::MO_Register: {
943 const unsigned Reg = MO->getReg();
944 if (!Reg)
945 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000946 if (MRI->tracksLiveness() && !MI->isDebugValue())
947 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000948
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000949 // Verify the consistency of tied operands.
950 if (MO->isTied()) {
951 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
952 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
953 if (!OtherMO.isReg())
954 report("Must be tied to a register", MO, MONum);
955 if (!OtherMO.isTied())
956 report("Missing tie flags on tied operand", MO, MONum);
957 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
958 report("Inconsistent tie links", MO, MONum);
959 if (MONum < MCID.getNumDefs()) {
960 if (OtherIdx < MCID.getNumOperands()) {
961 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
962 report("Explicit def tied to explicit use without tie constraint",
963 MO, MONum);
964 } else {
965 if (!OtherMO.isImplicit())
966 report("Explicit def should be tied to implicit use", MO, MONum);
967 }
968 }
969 }
970
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +0000971 // Verify two-address constraints after leaving SSA form.
972 unsigned DefIdx;
973 if (!MRI->isSSA() && MO->isUse() &&
974 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
975 Reg != MI->getOperand(DefIdx).getReg())
976 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000977
978 // Check register classes.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000979 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000980 unsigned SubIdx = MO->getSubReg();
981
982 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000983 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000984 report("Illegal subregister index for physical register", MO, MONum);
985 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000986 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000987 if (const TargetRegisterClass *DRC =
988 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000989 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000990 report("Illegal physical register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000991 errs() << TRI->getName(Reg) << " is not a "
Craig Toppercf0444b2014-11-17 05:50:14 +0000992 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000993 }
994 }
995 } else {
996 // Virtual register.
Quentin Colombetc1c94bc2016-04-08 16:35:22 +0000997 const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
998 if (!RC) {
999 // This is a generic virtual register.
1000 // It must have a size and it must not have a SubIdx.
1001 unsigned Size = MRI->getSize(Reg);
1002 if (!Size) {
1003 report("Generic virtual register must have a size", MO, MONum);
1004 return;
1005 }
1006 // Make sure the register fits into its register bank if any.
1007 const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
1008 if (RegBank && RegBank->getSize() < Size) {
1009 report("Register bank is too small for virtual register", MO,
1010 MONum);
1011 errs() << "Register bank " << RegBank->getName() << " too small("
1012 << RegBank->getSize() << ") to fit " << Size << "-bits\n";
1013 return;
1014 }
1015 if (SubIdx) {
1016 report("Generic virtual register does not subregister index", MO, MONum);
1017 return;
1018 }
1019 break;
1020 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001021 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001022 const TargetRegisterClass *SRC =
1023 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001024 if (!SRC) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001025 report("Invalid subregister index for virtual register", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001026 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +00001027 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001028 return;
1029 }
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001030 if (RC != SRC) {
1031 report("Invalid register class for subregister index", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001032 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001033 << " does not fully support subreg index " << SubIdx << "\n";
1034 return;
1035 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001036 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001037 if (const TargetRegisterClass *DRC =
1038 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001039 if (SubIdx) {
1040 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +00001041 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001042 if (!SuperRC) {
1043 report("No largest legal super class exists.", MO, MONum);
1044 return;
1045 }
1046 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
1047 if (!DRC) {
1048 report("No matching super-reg register class.", MO, MONum);
1049 return;
1050 }
1051 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +00001052 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001053 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001054 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +00001055 << " register, but got a " << TRI->getRegClassName(RC)
1056 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001057 }
1058 }
1059 }
1060 }
1061 break;
1062 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001063
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001064 case MachineOperand::MO_RegisterMask:
1065 regMasks.push_back(MO->getRegMask());
1066 break;
1067
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001068 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +00001069 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
1070 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001071 break;
1072
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001073 case MachineOperand::MO_FrameIndex:
1074 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001075 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
Jonas Paulsson72640f12015-10-29 08:28:35 +00001076 int FI = MO->getIndex();
1077 LiveInterval &LI = LiveStks->getInterval(FI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001078 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001079
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001080 bool stores = MI->mayStore();
Jonas Paulsson72640f12015-10-29 08:28:35 +00001081 bool loads = MI->mayLoad();
1082 // For a memory-to-memory move, we need to check if the frame
1083 // index is used for storing or loading, by inspecting the
1084 // memory operands.
1085 if (stores && loads) {
1086 for (auto *MMO : MI->memoperands()) {
1087 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1088 if (PSV == nullptr) continue;
1089 const FixedStackPseudoSourceValue *Value =
1090 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1091 if (Value == nullptr) continue;
1092 if (Value->getFrameIndex() != FI) continue;
1093
1094 if (MMO->isStore())
1095 loads = false;
1096 else
1097 stores = false;
1098 break;
1099 }
1100 if (loads == stores)
1101 report("Missing fixed stack memoperand.", MI);
1102 }
1103 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001104 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001105 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001106 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001107 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001108 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001109 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001110 }
1111 }
1112 break;
1113
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001114 default:
1115 break;
1116 }
1117}
1118
Matthias Braun1377fd62016-02-02 20:04:51 +00001119void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
1120 unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
1121 LaneBitmask LaneMask) {
1122 LiveQueryResult LRQ = LR.Query(UseIdx);
1123 // Check if we have a segment at the use, note however that we only need one
1124 // live subregister range, the others may be dead.
1125 if (!LRQ.valueIn() && LaneMask == 0) {
1126 report("No live segment at use", MO, MONum);
1127 report_context_liverange(LR);
1128 report_context_vreg_regunit(VRegOrUnit);
1129 report_context(UseIdx);
1130 }
1131 if (MO->isKill() && !LRQ.isKill()) {
1132 report("Live range continues after kill flag", MO, MONum);
1133 report_context_liverange(LR);
1134 report_context_vreg_regunit(VRegOrUnit);
1135 if (LaneMask != 0)
1136 report_context_lanemask(LaneMask);
1137 report_context(UseIdx);
1138 }
1139}
1140
1141void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
1142 unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
1143 LaneBitmask LaneMask) {
1144 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
1145 assert(VNI && "NULL valno is not allowed");
1146 if (VNI->def != DefIdx) {
1147 report("Inconsistent valno->def", MO, MONum);
1148 report_context_liverange(LR);
1149 report_context_vreg_regunit(VRegOrUnit);
1150 if (LaneMask != 0)
1151 report_context_lanemask(LaneMask);
1152 report_context(*VNI);
1153 report_context(DefIdx);
1154 }
1155 } else {
1156 report("No live segment at def", MO, MONum);
1157 report_context_liverange(LR);
1158 report_context_vreg_regunit(VRegOrUnit);
1159 if (LaneMask != 0)
1160 report_context_lanemask(LaneMask);
1161 report_context(DefIdx);
1162 }
1163 // Check that, if the dead def flag is present, LiveInts agree.
1164 if (MO->isDead()) {
1165 LiveQueryResult LRQ = LR.Query(DefIdx);
1166 if (!LRQ.isDeadDef()) {
1167 // In case of physregs we can have a non-dead definition on another
1168 // operand.
1169 bool otherDef = false;
1170 if (!TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
1171 const MachineInstr &MI = *MO->getParent();
1172 for (const MachineOperand &MO : MI.operands()) {
1173 if (!MO.isReg() || !MO.isDef() || MO.isDead())
1174 continue;
1175 unsigned Reg = MO.getReg();
1176 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1177 if (*Units == VRegOrUnit) {
1178 otherDef = true;
1179 break;
1180 }
1181 }
1182 }
1183 }
1184
1185 if (!otherDef) {
1186 report("Live range continues after dead def flag", MO, MONum);
1187 report_context_liverange(LR);
1188 report_context_vreg_regunit(VRegOrUnit);
1189 if (LaneMask != 0)
1190 report_context_lanemask(LaneMask);
1191 }
1192 }
1193 }
1194}
1195
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001196void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1197 const MachineInstr *MI = MO->getParent();
1198 const unsigned Reg = MO->getReg();
1199
1200 // Both use and def operands can read a register.
1201 if (MO->readsReg()) {
1202 regsLiveInButUnused.erase(Reg);
1203
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001204 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001205 addRegWithSubRegs(regsKilled, Reg);
1206
1207 // Check that LiveVars knows this kill.
1208 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1209 MO->isKill()) {
1210 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1211 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
1212 report("Kill missing from LiveVariables", MO, MONum);
1213 }
1214
1215 // Check LiveInts liveness and kill.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001216 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1217 SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001218 // Check the cached regunit intervals.
1219 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1220 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001221 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
1222 checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001223 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001224 }
1225
1226 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1227 if (LiveInts->hasInterval(Reg)) {
1228 // This is a virtual register interval.
1229 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun1377fd62016-02-02 20:04:51 +00001230 checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
1231
1232 if (LI.hasSubRanges() && !MO->isDef()) {
1233 unsigned SubRegIdx = MO->getSubReg();
1234 LaneBitmask MOMask = SubRegIdx != 0
1235 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1236 : MRI->getMaxLaneMaskForVReg(Reg);
1237 LaneBitmask LiveInMask = 0;
1238 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1239 if ((MOMask & SR.LaneMask) == 0)
1240 continue;
1241 checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
1242 LiveQueryResult LRQ = SR.Query(UseIdx);
1243 if (LRQ.valueIn())
1244 LiveInMask |= SR.LaneMask;
1245 }
1246 // At least parts of the register has to be live at the use.
1247 if ((LiveInMask & MOMask) == 0) {
1248 report("No live subrange at use", MO, MONum);
1249 report_context(LI);
1250 report_context(UseIdx);
1251 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001252 }
1253 } else {
1254 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001255 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001256 }
1257 }
1258
1259 // Use of a dead register.
1260 if (!regsLive.count(Reg)) {
1261 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1262 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001263 bool Bad = !isReserved(Reg);
1264 // We are fine if just any subregister has a defined value.
1265 if (Bad) {
1266 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1267 ++SubRegs) {
1268 if (regsLive.count(*SubRegs)) {
1269 Bad = false;
1270 break;
1271 }
1272 }
1273 }
Matthias Braun96a31952015-01-14 22:25:14 +00001274 // If there is an additional implicit-use of a super register we stop
1275 // here. By definition we are fine if the super register is not
1276 // (completely) dead, if the complete super register is dead we will
1277 // get a report for its operand.
1278 if (Bad) {
1279 for (const MachineOperand &MOP : MI->uses()) {
1280 if (!MOP.isReg())
1281 continue;
1282 if (!MOP.isImplicit())
1283 continue;
1284 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1285 ++SubRegs) {
1286 if (*SubRegs == Reg) {
1287 Bad = false;
1288 break;
1289 }
1290 }
1291 }
1292 }
Matthias Braun96d77322014-12-10 01:13:13 +00001293 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001294 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001295 } else if (MRI->def_empty(Reg)) {
1296 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001297 } else {
1298 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1299 // We don't know which virtual registers are live in, so only complain
1300 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1301 // must be live in. PHI instructions are handled separately.
1302 if (MInfo.regsKilled.count(Reg))
1303 report("Using a killed virtual register", MO, MONum);
1304 else if (!MI->isPHI())
1305 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1306 }
1307 }
1308 }
1309
1310 if (MO->isDef()) {
1311 // Register defined.
1312 // TODO: verify that earlyclobber ops are not used.
1313 if (MO->isDead())
1314 addRegWithSubRegs(regsDead, Reg);
1315 else
1316 addRegWithSubRegs(regsDefined, Reg);
1317
1318 // Verify SSA form.
1319 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001320 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001321 report("Multiple virtual register defs in SSA form", MO, MONum);
1322
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001323 // Check LiveInts for a live segment, but only for virtual registers.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001324 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1325 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001326 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Matthias Braun1377fd62016-02-02 20:04:51 +00001327
1328 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1329 if (LiveInts->hasInterval(Reg)) {
1330 const LiveInterval &LI = LiveInts->getInterval(Reg);
1331 checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
1332
1333 if (LI.hasSubRanges()) {
1334 unsigned SubRegIdx = MO->getSubReg();
1335 LaneBitmask MOMask = SubRegIdx != 0
1336 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1337 : MRI->getMaxLaneMaskForVReg(Reg);
1338 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1339 if ((SR.LaneMask & MOMask) == 0)
1340 continue;
1341 checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, SR.LaneMask);
1342 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001343 }
1344 } else {
Matthias Braun1377fd62016-02-02 20:04:51 +00001345 report("Virtual register has no Live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001346 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001347 }
1348 }
1349 }
1350}
1351
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001352void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001353}
1354
1355// This function gets called after visiting all instructions in a bundle. The
1356// argument points to the bundle header.
1357// Normal stand-alone instructions are also considered 'bundles', and this
1358// function is called for all of them.
1359void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001360 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1361 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001362 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001363 // Kill any masked registers.
1364 while (!regMasks.empty()) {
1365 const uint32_t *Mask = regMasks.pop_back_val();
1366 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1367 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1368 MachineOperand::clobbersPhysReg(Mask, *I))
1369 regsDead.push_back(*I);
1370 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001371 set_subtract(regsLive, regsDead); regsDead.clear();
1372 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001373}
1374
1375void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001376MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001377 MBBInfoMap[MBB].regsLiveOut = regsLive;
1378 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001379
1380 if (Indexes) {
1381 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1382 if (!(stop > lastIndex)) {
1383 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001384 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001385 << " last instruction was at " << lastIndex << '\n';
1386 }
1387 lastIndex = stop;
1388 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001389}
1390
1391// Calculate the largest possible vregsPassed sets. These are the registers that
1392// can pass through an MBB live, but may not be live every time. It is assumed
1393// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001394void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001395 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1396 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001397 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001398 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001399 BBInfo &MInfo = MBBInfoMap[&MBB];
1400 if (!MInfo.reachable)
1401 continue;
1402 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1403 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1404 BBInfo &SInfo = MBBInfoMap[*SuI];
1405 if (SInfo.addPassed(MInfo.regsLiveOut))
1406 todo.insert(*SuI);
1407 }
1408 }
1409
1410 // Iteratively push vregsPassed to successors. This will converge to the same
1411 // final state regardless of DenseSet iteration order.
1412 while (!todo.empty()) {
1413 const MachineBasicBlock *MBB = *todo.begin();
1414 todo.erase(MBB);
1415 BBInfo &MInfo = MBBInfoMap[MBB];
1416 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1417 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1418 if (*SuI == MBB)
1419 continue;
1420 BBInfo &SInfo = MBBInfoMap[*SuI];
1421 if (SInfo.addPassed(MInfo.vregsPassed))
1422 todo.insert(*SuI);
1423 }
1424 }
1425}
1426
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001427// Calculate the set of virtual registers that must be passed through each basic
1428// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001429// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001430void MachineVerifier::calcRegsRequired() {
1431 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001432 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001433 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001434 BBInfo &MInfo = MBBInfoMap[&MBB];
1435 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1436 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1437 BBInfo &PInfo = MBBInfoMap[*PrI];
1438 if (PInfo.addRequired(MInfo.vregsLiveIn))
1439 todo.insert(*PrI);
1440 }
1441 }
1442
1443 // Iteratively push vregsRequired to predecessors. This will converge to the
1444 // same final state regardless of DenseSet iteration order.
1445 while (!todo.empty()) {
1446 const MachineBasicBlock *MBB = *todo.begin();
1447 todo.erase(MBB);
1448 BBInfo &MInfo = MBBInfoMap[MBB];
1449 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1450 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1451 if (*PrI == MBB)
1452 continue;
1453 BBInfo &SInfo = MBBInfoMap[*PrI];
1454 if (SInfo.addRequired(MInfo.vregsRequired))
1455 todo.insert(*PrI);
1456 }
1457 }
1458}
1459
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001460// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001461// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001462void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001463 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001464 for (const auto &BBI : *MBB) {
1465 if (!BBI.isPHI())
1466 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001467 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001468
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001469 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1470 unsigned Reg = BBI.getOperand(i).getReg();
1471 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001472 if (!Pre->isSuccessor(MBB))
1473 continue;
1474 seen.insert(Pre);
1475 BBInfo &PrInfo = MBBInfoMap[Pre];
1476 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1477 report("PHI operand is not live-out from predecessor",
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001478 &BBI.getOperand(i), i);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001479 }
1480
1481 // Did we see all predecessors?
1482 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1483 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1484 if (!seen.count(*PrI)) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001485 report("Missing PHI operand", &BBI);
Owen Anderson21b17882015-02-04 00:02:59 +00001486 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001487 << " is a predecessor according to the CFG.\n";
1488 }
1489 }
1490 }
1491}
1492
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001493void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001494 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001495
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001496 for (const auto &MBB : *MF) {
1497 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001498
1499 // Skip unreachable MBBs.
1500 if (!MInfo.reachable)
1501 continue;
1502
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001503 checkPHIOps(&MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001504 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001505
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001506 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001507 calcRegsRequired();
1508
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001509 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001510 for (const auto &MBB : *MF) {
1511 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001512 for (RegSet::iterator
1513 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1514 ++I)
1515 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001516 report("Virtual register killed in block, but needed live out.", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001517 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001518 << " is used after the block.\n";
1519 }
1520 }
1521
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001522 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001523 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1524 for (RegSet::iterator
1525 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Matthias Braun30668dd2016-05-11 21:31:39 +00001526 ++I) {
1527 report("Virtual register defs don't dominate all uses.", MF);
1528 report_context_vreg(*I);
1529 }
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001530 }
1531
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001532 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001533 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001534 if (LiveInts)
1535 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001536}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001537
1538void MachineVerifier::verifyLiveVariables() {
1539 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001540 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1541 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001542 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001543 for (const auto &MBB : *MF) {
1544 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001545
1546 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1547 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001548 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1549 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001550 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001551 << " must be live through the block.\n";
1552 }
1553 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001554 if (VI.AliveBlocks.test(MBB.getNumber())) {
1555 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001556 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001557 << " is not needed live through the block.\n";
1558 }
1559 }
1560 }
1561 }
1562}
1563
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001564void MachineVerifier::verifyLiveIntervals() {
1565 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001566 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1567 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001568
1569 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001570 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001571 continue;
1572
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001573 if (!LiveInts->hasInterval(Reg)) {
1574 report("Missing live interval for virtual register", MF);
Owen Anderson21b17882015-02-04 00:02:59 +00001575 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001576 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001577 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001578
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001579 const LiveInterval &LI = LiveInts->getInterval(Reg);
1580 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001581 verifyLiveInterval(LI);
1582 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001583
1584 // Verify all the cached regunit intervals.
1585 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001586 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1587 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001588}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001589
Matthias Braun364e6e92013-10-10 21:28:54 +00001590void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001591 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001592 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001593 if (VNI->isUnused())
1594 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001595
Matthias Braun364e6e92013-10-10 21:28:54 +00001596 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001597
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001598 if (!DefVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001599 report("Value not live at VNInfo def and not marked unused", MF);
1600 report_context(LR, Reg, LaneMask);
1601 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001602 return;
1603 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001604
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001605 if (DefVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001606 report("Live segment at def has different VNInfo", MF);
1607 report_context(LR, Reg, LaneMask);
1608 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001609 return;
1610 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001611
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001612 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1613 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001614 report("Invalid VNInfo definition index", MF);
1615 report_context(LR, Reg, LaneMask);
1616 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001617 return;
1618 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001619
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001620 if (VNI->isPHIDef()) {
1621 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001622 report("PHIDef VNInfo is not defined at MBB start", MBB);
1623 report_context(LR, Reg, LaneMask);
1624 report_context(*VNI);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001625 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001626 return;
1627 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001628
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001629 // Non-PHI def.
1630 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1631 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001632 report("No instruction at VNInfo def index", MBB);
1633 report_context(LR, Reg, LaneMask);
1634 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001635 return;
1636 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001637
Matthias Braun364e6e92013-10-10 21:28:54 +00001638 if (Reg != 0) {
1639 bool hasDef = false;
1640 bool isEarlyClobber = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001641 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001642 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001643 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001644 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1645 if (MOI->getReg() != Reg)
1646 continue;
1647 } else {
1648 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1649 !TRI->hasRegUnit(MOI->getReg(), Reg))
1650 continue;
1651 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001652 if (LaneMask != 0 &&
1653 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask) == 0)
1654 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001655 hasDef = true;
1656 if (MOI->isEarlyClobber())
1657 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001658 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001659
Matthias Braun364e6e92013-10-10 21:28:54 +00001660 if (!hasDef) {
1661 report("Defining instruction does not modify register", MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001662 report_context(LR, Reg, LaneMask);
1663 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001664 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001665
Matthias Braun364e6e92013-10-10 21:28:54 +00001666 // Early clobber defs begin at USE slots, but other defs must begin at
1667 // DEF slots.
1668 if (isEarlyClobber) {
1669 if (!VNI->def.isEarlyClobber()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001670 report("Early clobber def must be at an early-clobber slot", MBB);
1671 report_context(LR, Reg, LaneMask);
1672 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001673 }
1674 } else if (!VNI->def.isRegister()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001675 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
1676 report_context(LR, Reg, LaneMask);
1677 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001678 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001679 }
1680}
1681
Matthias Braun364e6e92013-10-10 21:28:54 +00001682void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1683 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001684 unsigned Reg, LaneBitmask LaneMask)
1685{
Matthias Braun364e6e92013-10-10 21:28:54 +00001686 const LiveRange::Segment &S = *I;
1687 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001688 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001689
Matthias Braun364e6e92013-10-10 21:28:54 +00001690 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001691 report("Foreign valno in live segment", MF);
1692 report_context(LR, Reg, LaneMask);
1693 report_context(S);
1694 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001695 }
1696
1697 if (VNI->isUnused()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001698 report("Live segment valno is marked unused", MF);
1699 report_context(LR, Reg, LaneMask);
1700 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001701 }
1702
Matthias Braun364e6e92013-10-10 21:28:54 +00001703 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001704 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001705 report("Bad start of live segment, no basic block", MF);
1706 report_context(LR, Reg, LaneMask);
1707 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001708 return;
1709 }
1710 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001711 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001712 report("Live segment must begin at MBB entry or valno def", MBB);
1713 report_context(LR, Reg, LaneMask);
1714 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001715 }
1716
1717 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001718 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001719 if (!EndMBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001720 report("Bad end of live segment, no basic block", MF);
1721 report_context(LR, Reg, LaneMask);
1722 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001723 return;
1724 }
1725
1726 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001727 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001728 return;
1729
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001730 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001731 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1732 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001733 return;
1734
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001735 // The live segment is ending inside EndMBB
1736 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001737 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001738 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001739 report("Live segment doesn't end at a valid instruction", EndMBB);
1740 report_context(LR, Reg, LaneMask);
1741 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001742 return;
1743 }
1744
1745 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001746 if (S.end.isBlock()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001747 report("Live segment ends at B slot of an instruction", EndMBB);
1748 report_context(LR, Reg, LaneMask);
1749 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001750 }
1751
Matthias Braun364e6e92013-10-10 21:28:54 +00001752 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001753 // Segment ends on the dead slot.
1754 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001755 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001756 report("Live segment ending at dead slot spans instructions", EndMBB);
1757 report_context(LR, Reg, LaneMask);
1758 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001759 }
1760 }
1761
1762 // A live segment can only end at an early-clobber slot if it is being
1763 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001764 if (S.end.isEarlyClobber()) {
1765 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001766 report("Live segment ending at early clobber slot must be "
Matthias Braun7e624d52015-11-09 23:59:33 +00001767 "redefined by an EC def in the same instruction", EndMBB);
1768 report_context(LR, Reg, LaneMask);
1769 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001770 }
1771 }
1772
1773 // The following checks only apply to virtual registers. Physreg liveness
1774 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001775 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001776 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001777 // use, or a dead flag on a def.
1778 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001779 bool hasSubRegDef = false;
Matthias Braun72a58c32016-03-29 19:07:43 +00001780 bool hasDeadDef = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001781 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001782 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001783 continue;
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001784 if (LaneMask != 0 &&
1785 (LaneMask & TRI->getSubRegIndexLaneMask(MOI->getSubReg())) == 0)
1786 continue;
Matthias Braun72a58c32016-03-29 19:07:43 +00001787 if (MOI->isDef()) {
1788 if (MOI->getSubReg() != 0)
1789 hasSubRegDef = true;
1790 if (MOI->isDead())
1791 hasDeadDef = true;
1792 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001793 if (MOI->readsReg())
1794 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001795 }
Matthias Braun72a58c32016-03-29 19:07:43 +00001796 if (S.end.isDead()) {
1797 // Make sure that the corresponding machine operand for a "dead" live
1798 // range has the dead flag. We cannot perform this check for subregister
1799 // liveranges as partially dead values are allowed.
1800 if (LaneMask == 0 && !hasDeadDef) {
1801 report("Instruction ending live segment on dead slot has no dead flag",
1802 MI);
1803 report_context(LR, Reg, LaneMask);
1804 report_context(S);
1805 }
1806 } else {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001807 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001808 // When tracking subregister liveness, the main range must start new
1809 // values on partial register writes, even if there is no read.
Matthias Brauna25e13a2015-03-19 00:21:58 +00001810 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask != 0 ||
1811 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001812 report("Instruction ending live segment doesn't read the register",
1813 MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001814 report_context(LR, Reg, LaneMask);
1815 report_context(S);
Matthias Braun21554d92014-12-10 01:13:11 +00001816 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001817 }
1818 }
1819 }
1820
1821 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001822 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001823 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00001824 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001825 // Not live-in to any blocks.
1826 if (MBB == EndMBB)
1827 return;
1828 // Skip this block.
1829 ++MFI;
1830 }
1831 for (;;) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001832 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001833 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00001834 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00001835 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001836 if (&*MFI == EndMBB)
1837 break;
1838 ++MFI;
1839 continue;
1840 }
1841
1842 // Is VNI a PHI-def in the current block?
1843 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001844 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001845
1846 // Check that VNI is live-out of all predecessors.
1847 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1848 PE = MFI->pred_end(); PI != PE; ++PI) {
1849 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001850 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001851
Matthias Braune29b7682016-05-20 23:02:13 +00001852 // All predecessors must have a live-out value if this is not a
1853 // subregister liverange.
1854 if (!PVNI && LaneMask == 0) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001855 report("Register not marked live out of predecessor", *PI);
1856 report_context(LR, Reg, LaneMask);
1857 report_context(*VNI);
1858 errs() << " live into BB#" << MFI->getNumber()
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001859 << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
1860 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001861 continue;
1862 }
1863
1864 // Only PHI-defs can take different predecessor values.
1865 if (!IsPHI && PVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001866 report("Different value live out of predecessor", *PI);
1867 report_context(LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001868 errs() << "Valno #" << PVNI->id << " live out of BB#"
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001869 << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
1870 << " live into BB#" << MFI->getNumber() << '@'
1871 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001872 }
1873 }
1874 if (&*MFI == EndMBB)
1875 break;
1876 ++MFI;
1877 }
1878}
1879
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001880void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001881 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00001882 for (const VNInfo *VNI : LR.valnos)
1883 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001884
Matthias Braun364e6e92013-10-10 21:28:54 +00001885 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001886 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00001887}
1888
1889void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001890 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00001891 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1892 verifyLiveRange(LI, Reg);
1893
Matthias Braune6a24852015-09-25 21:51:14 +00001894 LaneBitmask Mask = 0;
1895 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00001896 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001897 if ((Mask & SR.LaneMask) != 0) {
1898 report("Lane masks of sub ranges overlap in live interval", MF);
1899 report_context(LI);
1900 }
1901 if ((SR.LaneMask & ~MaxMask) != 0) {
1902 report("Subrange lanemask is invalid", MF);
1903 report_context(LI);
1904 }
1905 if (SR.empty()) {
1906 report("Subrange must not be empty", MF);
1907 report_context(SR, LI.reg, SR.LaneMask);
1908 }
Matthias Braune962e522015-03-25 21:18:22 +00001909 Mask |= SR.LaneMask;
1910 verifyLiveRange(SR, LI.reg, SR.LaneMask);
Matthias Braun7e624d52015-11-09 23:59:33 +00001911 if (!LI.covers(SR)) {
1912 report("A Subrange is not covered by the main range", MF);
1913 report_context(LI);
1914 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001915 }
1916
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001917 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00001918 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
Matthias Braunbf47f632016-01-08 01:16:35 +00001919 unsigned NumComp = ConEQ.Classify(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001920 if (NumComp > 1) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001921 report("Multiple connected components in live interval", MF);
1922 report_context(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001923 for (unsigned comp = 0; comp != NumComp; ++comp) {
1924 errs() << comp << ": valnos";
1925 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1926 E = LI.vni_end(); I!=E; ++I)
1927 if (comp == ConEQ.getEqClass(*I))
1928 errs() << ' ' << (*I)->id;
1929 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00001930 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001931 }
1932}
Manman Renaa6875b2013-07-15 21:26:31 +00001933
1934namespace {
1935 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1936 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1937 // value is zero.
1938 // We use a bool plus an integer to capture the stack state.
1939 struct StackStateOfBB {
1940 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1941 ExitIsSetup(false) { }
1942 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1943 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1944 ExitIsSetup(ExitSetup) { }
1945 // Can be negative, which means we are setting up a frame.
1946 int EntryValue;
1947 int ExitValue;
1948 bool EntryIsSetup;
1949 bool ExitIsSetup;
1950 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001951}
Manman Renaa6875b2013-07-15 21:26:31 +00001952
1953/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1954/// by a FrameDestroy <n>, stack adjustments are identical on all
1955/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1956void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001957 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1958 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Manman Renaa6875b2013-07-15 21:26:31 +00001959
1960 SmallVector<StackStateOfBB, 8> SPState;
1961 SPState.resize(MF->getNumBlockIDs());
1962 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1963
1964 // Visit the MBBs in DFS order.
1965 for (df_ext_iterator<const MachineFunction*,
1966 SmallPtrSet<const MachineBasicBlock*, 8> >
1967 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1968 DFI != DFE; ++DFI) {
1969 const MachineBasicBlock *MBB = *DFI;
1970
1971 StackStateOfBB BBState;
1972 // Check the exit state of the DFS stack predecessor.
1973 if (DFI.getPathLength() >= 2) {
1974 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1975 assert(Reachable.count(StackPred) &&
1976 "DFS stack predecessor is already visited.\n");
1977 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1978 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1979 BBState.ExitValue = BBState.EntryValue;
1980 BBState.ExitIsSetup = BBState.EntryIsSetup;
1981 }
1982
1983 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001984 for (const auto &I : *MBB) {
1985 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001986 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001987 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001988 assert(Size >= 0 &&
1989 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1990
1991 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001992 report("FrameSetup is after another FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001993 BBState.ExitValue -= Size;
1994 BBState.ExitIsSetup = true;
1995 }
1996
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001997 if (I.getOpcode() == FrameDestroyOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001998 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001999 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00002000 assert(Size >= 0 &&
2001 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
2002
2003 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002004 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00002005 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
2006 BBState.ExitValue;
2007 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002008 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00002009 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00002010 << AbsSPAdj << ">.\n";
2011 }
2012 BBState.ExitValue += Size;
2013 BBState.ExitIsSetup = false;
2014 }
2015 }
2016 SPState[MBB->getNumber()] = BBState;
2017
2018 // Make sure the exit state of any predecessor is consistent with the entry
2019 // state.
2020 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
2021 E = MBB->pred_end(); I != E; ++I) {
2022 if (Reachable.count(*I) &&
2023 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
2024 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
2025 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002026 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002027 << SPState[(*I)->getNumber()].ExitValue << ", "
2028 << SPState[(*I)->getNumber()].ExitIsSetup
2029 << "), while BB#" << MBB->getNumber() << " has entry state ("
2030 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
2031 }
2032 }
2033
2034 // Make sure the entry state of any successor is consistent with the exit
2035 // state.
2036 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
2037 E = MBB->succ_end(); I != E; ++I) {
2038 if (Reachable.count(*I) &&
2039 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
2040 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
2041 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002042 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002043 << SPState[(*I)->getNumber()].EntryValue << ", "
2044 << SPState[(*I)->getNumber()].EntryIsSetup
2045 << "), while BB#" << MBB->getNumber() << " has exit state ("
2046 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
2047 }
2048 }
2049
2050 // Make sure a basic block with return ends with zero stack adjustment.
2051 if (!MBB->empty() && MBB->back().isReturn()) {
2052 if (BBState.ExitIsSetup)
2053 report("A return block ends with a FrameSetup.", MBB);
2054 if (BBState.ExitValue)
2055 report("A return block ends with a nonzero stack adjustment.", MBB);
2056 }
2057 }
2058}