blob: b9ca0c51d76feb5d813bf4444e562ec2731be0e1 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/CodeGen/LiveIntervalAnalysis.h"
32#include "llvm/CodeGen/LiveStackAnalysis.h"
33#include "llvm/CodeGen/LiveVariables.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/CodeGen/MachineMemOperand.h"
37#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000038#include "llvm/IR/BasicBlock.h"
39#include "llvm/IR/InlineAsm.h"
40#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000042#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000043#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000044#include "llvm/Support/FileSystem.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000045#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000046#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetMachine.h"
48#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000049#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000050using namespace llvm;
51
52namespace {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000053 struct MachineVerifier {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000054
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000055 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000056 PASS(pass),
Owen Anderson21b17882015-02-04 00:02:59 +000057 Banner(b)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000058 {}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000059
60 bool runOnMachineFunction(MachineFunction &MF);
61
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000062 Pass *const PASS;
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000063 const char *Banner;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000064 const MachineFunction *MF;
65 const TargetMachine *TM;
Evan Cheng8d71a752011-06-27 21:26:13 +000066 const TargetInstrInfo *TII;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000067 const TargetRegisterInfo *TRI;
68 const MachineRegisterInfo *MRI;
69
70 unsigned foundErrors;
71
72 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000073 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000074 typedef DenseSet<unsigned> RegSet;
75 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000076 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000077
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000078 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000079 BlockSet FunctionBlocks;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000080
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000081 BitVector regsReserved;
82 RegSet regsLive;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000083 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000084 RegMaskVector regMasks;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000085 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000086
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +000087 SlotIndex lastIndex;
88
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000089 // Add Reg and any sub-registers to RV
90 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
91 RV.push_back(Reg);
92 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +000093 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
94 RV.push_back(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000095 }
96
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000097 struct BBInfo {
98 // Is this MBB reachable from the MF entry point?
99 bool reachable;
100
101 // Vregs that must be live in because they are used without being
102 // defined. Map value is the user.
103 RegMap vregsLiveIn;
104
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000105 // Regs killed in MBB. They may be defined again, and will then be in both
106 // regsKilled and regsLiveOut.
107 RegSet regsKilled;
108
109 // Regs defined in MBB and live out. Note that vregs passing through may
110 // be live out without being mentioned here.
111 RegSet regsLiveOut;
112
113 // Vregs that pass through MBB untouched. This set is disjoint from
114 // regsKilled and regsLiveOut.
115 RegSet vregsPassed;
116
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000117 // Vregs that must pass through MBB because they are needed by a successor
118 // block. This set is disjoint from regsLiveOut.
119 RegSet vregsRequired;
120
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000121 // Set versions of block's predecessor and successor lists.
122 BlockSet Preds, Succs;
123
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000124 BBInfo() : reachable(false) {}
125
126 // Add register to vregsPassed if it belongs there. Return true if
127 // anything changed.
128 bool addPassed(unsigned Reg) {
129 if (!TargetRegisterInfo::isVirtualRegister(Reg))
130 return false;
131 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
132 return false;
133 return vregsPassed.insert(Reg).second;
134 }
135
136 // Same for a full set.
137 bool addPassed(const RegSet &RS) {
138 bool changed = false;
139 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
140 if (addPassed(*I))
141 changed = true;
142 return changed;
143 }
144
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000145 // Add register to vregsRequired if it belongs there. Return true if
146 // anything changed.
147 bool addRequired(unsigned Reg) {
148 if (!TargetRegisterInfo::isVirtualRegister(Reg))
149 return false;
150 if (regsLiveOut.count(Reg))
151 return false;
152 return vregsRequired.insert(Reg).second;
153 }
154
155 // Same for a full set.
156 bool addRequired(const RegSet &RS) {
157 bool changed = false;
158 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
159 if (addRequired(*I))
160 changed = true;
161 return changed;
162 }
163
164 // Same for a full map.
165 bool addRequired(const RegMap &RM) {
166 bool changed = false;
167 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
168 if (addRequired(I->first))
169 changed = true;
170 return changed;
171 }
172
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000173 // Live-out registers are either in regsLiveOut or vregsPassed.
174 bool isLiveOut(unsigned Reg) const {
175 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
176 }
177 };
178
179 // Extra register info per MBB.
180 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
181
182 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000183 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000184 }
185
Lang Hames1ce837a2012-02-14 19:17:48 +0000186 bool isAllocatable(unsigned Reg) {
Jakob Stoklund Olesen244beb42012-10-16 00:05:06 +0000187 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
Lang Hames1ce837a2012-02-14 19:17:48 +0000188 }
189
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000190 // Analysis information if available
191 LiveVariables *LiveVars;
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +0000192 LiveIntervals *LiveInts;
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000193 LiveStacks *LiveStks;
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000194 SlotIndexes *Indexes;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000195
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000196 void visitMachineFunctionBefore();
197 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000198 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000199 void visitMachineInstrBefore(const MachineInstr *MI);
200 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
201 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000202 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000203 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
204 void visitMachineFunctionAfter();
205
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000206 template <typename T> void report(const char *msg, ilist_iterator<T> I) {
207 report(msg, &*I);
208 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000209 void report(const char *msg, const MachineFunction *MF);
210 void report(const char *msg, const MachineBasicBlock *MBB);
211 void report(const char *msg, const MachineInstr *MI);
212 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000213 void report(const char *msg, const MachineFunction *MF,
214 const LiveInterval &LI);
215 void report(const char *msg, const MachineBasicBlock *MBB,
216 const LiveInterval &LI);
Matthias Braun364e6e92013-10-10 21:28:54 +0000217 void report(const char *msg, const MachineFunction *MF,
Matthias Braune6a24852015-09-25 21:51:14 +0000218 const LiveRange &LR, unsigned Reg, LaneBitmask LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000219 void report(const char *msg, const MachineBasicBlock *MBB,
Matthias Braune6a24852015-09-25 21:51:14 +0000220 const LiveRange &LR, unsigned Reg, LaneBitmask LaneMask);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000221
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000222 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000223
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000224 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000225 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000226 void calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000227 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000228
229 void calcRegsRequired();
230 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000231 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000232 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000233 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
234 unsigned);
Matthias Braun364e6e92013-10-10 21:28:54 +0000235 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000236 const LiveRange::const_iterator I, unsigned,
237 unsigned);
Matthias Braune6a24852015-09-25 21:51:14 +0000238 void verifyLiveRange(const LiveRange&, unsigned, LaneBitmask LaneMask = 0);
Manman Renaa6875b2013-07-15 21:26:31 +0000239
240 void verifyStackFrame();
Matthias Braun80595462015-09-09 17:49:46 +0000241
242 void verifySlotIndexes() const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000243 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000244
245 struct MachineVerifierPass : public MachineFunctionPass {
246 static char ID; // Pass ID, replacement for typeid
Matthias Brauna4e932d2014-12-11 19:41:51 +0000247 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000248
Matthias Brauna4e932d2014-12-11 19:41:51 +0000249 MachineVerifierPass(const std::string &banner = nullptr)
250 : MachineFunctionPass(ID), Banner(banner) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000251 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
252 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000253
Craig Topper4584cd52014-03-07 09:26:03 +0000254 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000255 AU.setPreservesAll();
256 MachineFunctionPass::getAnalysisUsage(AU);
257 }
258
Craig Topper4584cd52014-03-07 09:26:03 +0000259 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Brauna4e932d2014-12-11 19:41:51 +0000260 MF.verify(this, Banner.c_str());
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000261 return false;
262 }
263 };
264
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000265}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000266
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000267char MachineVerifierPass::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +0000268INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000269 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000270
Matthias Brauna4e932d2014-12-11 19:41:51 +0000271FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000272 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000273}
274
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000275void MachineFunction::verify(Pass *p, const char *Banner) const {
276 MachineVerifier(p, Banner)
277 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesen27440e72009-11-13 21:56:09 +0000278}
279
Matthias Braun80595462015-09-09 17:49:46 +0000280void MachineVerifier::verifySlotIndexes() const {
281 if (Indexes == nullptr)
282 return;
283
284 // Ensure the IdxMBB list is sorted by slot indexes.
285 SlotIndex Last;
286 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
287 E = Indexes->MBBIndexEnd(); I != E; ++I) {
288 assert(!Last.isValid() || I->first > Last);
289 Last = I->first;
290 }
291}
292
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000293bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000294 foundErrors = 0;
295
296 this->MF = &MF;
297 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000298 TII = MF.getSubtarget().getInstrInfo();
299 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000300 MRI = &MF.getRegInfo();
301
Craig Topperc0196b12014-04-14 00:51:57 +0000302 LiveVars = nullptr;
303 LiveInts = nullptr;
304 LiveStks = nullptr;
305 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000306 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000307 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000308 // We don't want to verify LiveVariables if LiveIntervals is available.
309 if (!LiveInts)
310 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000311 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000312 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000313 }
314
Matthias Braun80595462015-09-09 17:49:46 +0000315 verifySlotIndexes();
316
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000317 visitMachineFunctionBefore();
318 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
319 MFI!=MFE; ++MFI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000320 visitMachineBasicBlockBefore(&*MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000321 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000322 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000323 // Do we expect the next instruction to be part of the same bundle?
324 bool InBundle = false;
325
Evan Cheng7fae11b2011-12-14 02:11:42 +0000326 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
327 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000328 if (MBBI->getParent() != &*MFI) {
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000329 report("Bad instruction parent pointer", MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000330 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000331 continue;
332 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000333
334 // Check for consistent bundle flags.
335 if (InBundle && !MBBI->isBundledWithPred())
336 report("Missing BundledPred flag, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000337 "BundledSucc was set on predecessor",
338 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000339 if (!InBundle && MBBI->isBundledWithPred())
340 report("BundledPred flag is set, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000341 "but BundledSucc not set on predecessor",
342 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000343
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000344 // Is this a bundle header?
345 if (!MBBI->isInsideBundle()) {
346 if (CurBundle)
347 visitMachineBundleAfter(CurBundle);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000348 CurBundle = &*MBBI;
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000349 visitMachineBundleBefore(CurBundle);
350 } else if (!CurBundle)
351 report("No bundle header", MBBI);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000352 visitMachineInstrBefore(&*MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000353 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
354 const MachineInstr &MI = *MBBI;
355 const MachineOperand &Op = MI.getOperand(I);
356 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000357 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000358 // functions when replacing operands of a MachineInstr.
359 report("Instruction has operand with wrong parent set", &MI);
360 }
361
362 visitMachineOperand(&Op, I);
363 }
364
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000365 visitMachineInstrAfter(&*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000366
367 // Was this the last bundled instruction?
368 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000369 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000370 if (CurBundle)
371 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000372 if (InBundle)
373 report("BundledSucc flag set on last instruction in block", &MFI->back());
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000374 visitMachineBasicBlockAfter(&*MFI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000375 }
376 visitMachineFunctionAfter();
377
Owen Anderson21b17882015-02-04 00:02:59 +0000378 if (foundErrors)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000379 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000380
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000381 // Clean up.
382 regsLive.clear();
383 regsDefined.clear();
384 regsDead.clear();
385 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000386 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000387 regsLiveInButUnused.clear();
388 MBBInfoMap.clear();
389
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000390 return false; // no changes
391}
392
Chris Lattner75f40452009-08-23 01:03:30 +0000393void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000394 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000395 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000396 if (!foundErrors++) {
397 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000398 errs() << "# " << Banner << '\n';
399 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000400 }
Owen Anderson21b17882015-02-04 00:02:59 +0000401 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000402 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000403}
404
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000405void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000406 assert(MBB);
407 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000408 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000409 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000410 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000411 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000412 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000413 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000414 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000415}
416
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000417void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000418 assert(MI);
419 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000420 errs() << "- instruction: ";
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000421 if (Indexes && Indexes->hasIndex(MI))
Owen Anderson21b17882015-02-04 00:02:59 +0000422 errs() << Indexes->getInstructionIndex(MI) << '\t';
423 MI->print(errs(), TM);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000424}
425
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000426void MachineVerifier::report(const char *msg,
427 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000428 assert(MO);
429 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000430 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000431 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000432 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000433}
434
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000435void MachineVerifier::report(const char *msg, const MachineFunction *MF,
436 const LiveInterval &LI) {
437 report(msg, MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000438 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000439}
440
441void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
442 const LiveInterval &LI) {
443 report(msg, MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000444 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000445}
446
Matthias Braun364e6e92013-10-10 21:28:54 +0000447void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000448 const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +0000449 LaneBitmask LaneMask) {
Matthias Braun364e6e92013-10-10 21:28:54 +0000450 report(msg, MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000451 errs() << "- liverange: " << LR << '\n';
452 errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000453 if (LaneMask != 0)
Matthias Braunc804cdb2015-09-25 21:51:24 +0000454 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
Matthias Braun364e6e92013-10-10 21:28:54 +0000455}
456
457void MachineVerifier::report(const char *msg, const MachineFunction *MF,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000458 const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +0000459 LaneBitmask LaneMask) {
Matthias Braun364e6e92013-10-10 21:28:54 +0000460 report(msg, MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000461 errs() << "- liverange: " << LR << '\n';
462 errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000463 if (LaneMask != 0)
Matthias Braunc804cdb2015-09-25 21:51:24 +0000464 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
Matthias Braun364e6e92013-10-10 21:28:54 +0000465}
466
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000467void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000468 BBInfo &MInfo = MBBInfoMap[MBB];
469 if (!MInfo.reachable) {
470 MInfo.reachable = true;
471 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
472 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
473 markReachable(*SuI);
474 }
475}
476
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000477void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000478 lastIndex = SlotIndex();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000479 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000480
481 // A sub-register of a reserved register is also reserved
482 for (int Reg = regsReserved.find_first(); Reg>=0;
483 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000484 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000485 // FIXME: This should probably be:
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000486 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
487 regsReserved.set(*SubRegs);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000488 }
489 }
Lang Hames1ce837a2012-02-14 19:17:48 +0000490
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000491 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000492
493 // Build a set of the basic blocks in the function.
494 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000495 for (const auto &MBB : *MF) {
496 FunctionBlocks.insert(&MBB);
497 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000498
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000499 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
500 if (MInfo.Preds.size() != MBB.pred_size())
501 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000502
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000503 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
504 if (MInfo.Succs.size() != MBB.succ_size())
505 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000506 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000507
508 // Check that the register use lists are sane.
509 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000510
511 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000512}
513
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000514// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000515static bool matchPair(MachineBasicBlock::const_succ_iterator i,
516 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000517 if (*i == a)
518 return *++i == b;
519 if (*i == b)
520 return *++i == a;
521 return false;
522}
523
524void
525MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000526 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000527
Lang Hames1ce837a2012-02-14 19:17:48 +0000528 if (MRI->isSSA()) {
529 // If this block has allocatable physical registers live-in, check that
530 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000531 for (const auto &LI : MBB->liveins()) {
532 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Lang Hames1ce837a2012-02-14 19:17:48 +0000533 MBB != MBB->getParent()->begin()) {
534 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
535 }
536 }
537 }
538
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000539 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000540 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000541 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000542 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000543 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000544 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000545 if (!FunctionBlocks.count(*I))
546 report("MBB has successor that isn't part of the function.", MBB);
547 if (!MBBInfoMap[*I].Preds.count(MBB)) {
548 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000549 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000550 << (*I)->getNumber() << ".\n";
551 }
552 }
553
554 // Check the predecessor list.
555 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
556 E = MBB->pred_end(); I != E; ++I) {
557 if (!FunctionBlocks.count(*I))
558 report("MBB has predecessor that isn't part of the function.", MBB);
559 if (!MBBInfoMap[*I].Succs.count(MBB)) {
560 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000561 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000562 << (*I)->getNumber() << ".\n";
563 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000564 }
Bill Wendling2a401312011-05-04 22:54:05 +0000565
566 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
567 const BasicBlock *BB = MBB->getBasicBlock();
568 if (LandingPadSuccs.size() > 1 &&
569 !(AsmInfo &&
570 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
571 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000572 report("MBB has more than one landing pad successor", MBB);
573
Dan Gohman352a4952009-08-27 02:43:49 +0000574 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000575 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000576 SmallVector<MachineOperand, 4> Cond;
577 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
578 TBB, FBB, Cond)) {
579 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
580 // check whether its answers match up with reality.
581 if (!TBB && !FBB) {
582 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000583 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000584 ++MBBI;
585 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000586 // It's possible that the block legitimately ends with a noreturn
587 // call or an unreachable, in which case it won't actually fall
588 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000589 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000590 // It's possible that the block legitimately ends with a noreturn
591 // call or an unreachable, in which case it won't actuall fall
592 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000593 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000594 report("MBB exits via unconditional fall-through but doesn't have "
595 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000596 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000597 report("MBB exits via unconditional fall-through but its successor "
598 "differs from its CFG successor!", MBB);
599 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000600 if (!MBB->empty() && MBB->back().isBarrier() &&
601 !TII->isPredicated(&MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000602 report("MBB exits via unconditional fall-through but ends with a "
603 "barrier instruction!", MBB);
604 }
605 if (!Cond.empty()) {
606 report("MBB exits via unconditional fall-through but has a condition!",
607 MBB);
608 }
609 } else if (TBB && !FBB && Cond.empty()) {
610 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000611 // If the block has exactly one successor, that happens to be a
612 // landingpad, accept it as valid control flow.
613 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
614 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
615 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000616 report("MBB exits via unconditional branch but doesn't have "
617 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000618 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000619 report("MBB exits via unconditional branch but the CFG "
620 "successor doesn't match the actual successor!", MBB);
621 }
622 if (MBB->empty()) {
623 report("MBB exits via unconditional branch but doesn't contain "
624 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000625 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000626 report("MBB exits via unconditional branch but doesn't end with a "
627 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000628 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000629 report("MBB exits via unconditional branch but the branch isn't a "
630 "terminator instruction!", MBB);
631 }
632 } else if (TBB && !FBB && !Cond.empty()) {
633 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000634 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000635 ++MBBI;
636 if (MBBI == MF->end()) {
637 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000638 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000639 // A conditional branch with only one successor is weird, but allowed.
640 if (&*MBBI != TBB)
641 report("MBB exits via conditional branch/fall-through but only has "
642 "one CFG successor!", MBB);
643 else if (TBB != *MBB->succ_begin())
644 report("MBB exits via conditional branch/fall-through but the CFG "
645 "successor don't match the actual successor!", MBB);
646 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000647 report("MBB exits via conditional branch/fall-through but doesn't have "
648 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000649 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000650 report("MBB exits via conditional branch/fall-through but the CFG "
651 "successors don't match the actual successors!", MBB);
652 }
653 if (MBB->empty()) {
654 report("MBB exits via conditional branch/fall-through but doesn't "
655 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000656 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000657 report("MBB exits via conditional branch/fall-through but ends with a "
658 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000659 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000660 report("MBB exits via conditional branch/fall-through but the branch "
661 "isn't a terminator instruction!", MBB);
662 }
663 } else if (TBB && FBB) {
664 // Block conditionally branches somewhere, otherwise branches
665 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000666 if (MBB->succ_size() == 1) {
667 // A conditional branch with only one successor is weird, but allowed.
668 if (FBB != TBB)
669 report("MBB exits via conditional branch/branch through but only has "
670 "one CFG successor!", MBB);
671 else if (TBB != *MBB->succ_begin())
672 report("MBB exits via conditional branch/branch through but the CFG "
673 "successor don't match the actual successor!", MBB);
674 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000675 report("MBB exits via conditional branch/branch but doesn't have "
676 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000677 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000678 report("MBB exits via conditional branch/branch but the CFG "
679 "successors don't match the actual successors!", MBB);
680 }
681 if (MBB->empty()) {
682 report("MBB exits via conditional branch/branch but doesn't "
683 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000684 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000685 report("MBB exits via conditional branch/branch but doesn't end with a "
686 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000687 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000688 report("MBB exits via conditional branch/branch but the branch "
689 "isn't a terminator instruction!", MBB);
690 }
691 if (Cond.empty()) {
692 report("MBB exits via conditinal branch/branch but there's no "
693 "condition!", MBB);
694 }
695 } else {
696 report("AnalyzeBranch returned invalid data!", MBB);
697 }
698 }
699
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000700 regsLive.clear();
Matthias Braund9da1622015-09-09 18:08:03 +0000701 for (const auto &LI : MBB->liveins()) {
702 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000703 report("MBB live-in list contains non-physical register", MBB);
704 continue;
705 }
Matthias Braund9da1622015-09-09 18:08:03 +0000706 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
Chad Rosierabdb1d62013-05-22 23:17:36 +0000707 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000708 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000709 }
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +0000710 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000711
712 const MachineFrameInfo *MFI = MF->getFrameInfo();
713 assert(MFI && "Function has no frame info");
Matthias Braun111f5d82015-05-28 23:20:35 +0000714 BitVector PR = MFI->getPristineRegs(*MF);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000715 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000716 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
717 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000718 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000719 }
720
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000721 regsKilled.clear();
722 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000723
724 if (Indexes)
725 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000726}
727
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000728// This function gets called for all bundle headers, including normal
729// stand-alone unbundled instructions.
730void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
731 if (Indexes && Indexes->hasIndex(MI)) {
732 SlotIndex idx = Indexes->getInstructionIndex(MI);
733 if (!(idx > lastIndex)) {
734 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000735 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000736 }
737 lastIndex = idx;
738 }
Pete Coopercd720162012-06-07 17:41:39 +0000739
740 // Ensure non-terminators don't follow terminators.
741 // Ignore predicated terminators formed by if conversion.
742 // FIXME: If conversion shouldn't need to violate this rule.
743 if (MI->isTerminator() && !TII->isPredicated(MI)) {
744 if (!FirstTerminator)
745 FirstTerminator = MI;
746 } else if (FirstTerminator) {
747 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000748 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000749 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000750}
751
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000752// The operands on an INLINEASM instruction must follow a template.
753// Verify that the flag operands make sense.
754void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
755 // The first two operands on INLINEASM are the asm string and global flags.
756 if (MI->getNumOperands() < 2) {
757 report("Too few operands on inline asm", MI);
758 return;
759 }
760 if (!MI->getOperand(0).isSymbol())
761 report("Asm string must be an external symbol", MI);
762 if (!MI->getOperand(1).isImm())
763 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000764 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
765 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
766 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000767 report("Unknown asm flags", &MI->getOperand(1), 1);
768
Gabor Horvathfee04342015-03-16 09:53:42 +0000769 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000770
771 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
772 unsigned NumOps;
773 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
774 const MachineOperand &MO = MI->getOperand(OpNo);
775 // There may be implicit ops after the fixed operands.
776 if (!MO.isImm())
777 break;
778 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
779 }
780
781 if (OpNo > MI->getNumOperands())
782 report("Missing operands in last group", MI);
783
784 // An optional MDNode follows the groups.
785 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
786 ++OpNo;
787
788 // All trailing operands must be implicit registers.
789 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
790 const MachineOperand &MO = MI->getOperand(OpNo);
791 if (!MO.isReg() || !MO.isImplicit())
792 report("Expected implicit register after groups", &MO, OpNo);
793 }
794}
795
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000796void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000797 const MCInstrDesc &MCID = MI->getDesc();
798 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000799 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000800 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000801 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000802 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000803
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000804 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000805 if (MI->isInlineAsm())
806 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000807
Dan Gohmandb9493c2009-10-07 17:36:00 +0000808 // Check the MachineMemOperands for basic consistency.
809 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
810 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000811 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000812 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000813 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000814 report("Missing mayStore flag", MI);
815 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000816
817 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000818 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000819 if (LiveInts) {
820 bool mapped = !LiveInts->isNotInMIMap(MI);
821 if (MI->isDebugValue()) {
822 if (mapped)
823 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000824 } else if (MI->isInsideBundle()) {
825 if (mapped)
826 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000827 } else {
828 if (!mapped)
829 report("Missing slot index", MI);
830 }
831 }
832
Andrew Trick924123a2011-09-21 02:20:46 +0000833 StringRef ErrorInfo;
834 if (!TII->verifyInstruction(MI, ErrorInfo))
835 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000836}
837
838void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000839MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000840 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000841 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +0000842 unsigned NumDefs = MCID.getNumDefs();
843 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
844 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000845
Evan Cheng6cc775f2011-06-28 19:10:37 +0000846 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +0000847 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000848 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000849 if (!MO->isReg())
850 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +0000851 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000852 report("Explicit definition marked as use", MO, MONum);
853 else if (MO->isImplicit())
854 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000855 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000856 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +0000857 // Don't check if it's the last operand in a variadic instruction. See,
858 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000859 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000860 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000861 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +0000862 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000863 if (MO->isImplicit())
864 report("Explicit operand marked as implicit", MO, MONum);
865 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000866
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000867 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
868 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000869 if (!MO->isReg())
870 report("Tied use must be a register", MO, MONum);
871 else if (!MO->isTied())
872 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000873 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
874 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000875 } else if (MO->isReg() && MO->isTied())
876 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000877 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +0000878 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000879 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000880 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000881 }
882
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000883 switch (MO->getType()) {
884 case MachineOperand::MO_Register: {
885 const unsigned Reg = MO->getReg();
886 if (!Reg)
887 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000888 if (MRI->tracksLiveness() && !MI->isDebugValue())
889 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000890
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000891 // Verify the consistency of tied operands.
892 if (MO->isTied()) {
893 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
894 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
895 if (!OtherMO.isReg())
896 report("Must be tied to a register", MO, MONum);
897 if (!OtherMO.isTied())
898 report("Missing tie flags on tied operand", MO, MONum);
899 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
900 report("Inconsistent tie links", MO, MONum);
901 if (MONum < MCID.getNumDefs()) {
902 if (OtherIdx < MCID.getNumOperands()) {
903 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
904 report("Explicit def tied to explicit use without tie constraint",
905 MO, MONum);
906 } else {
907 if (!OtherMO.isImplicit())
908 report("Explicit def should be tied to implicit use", MO, MONum);
909 }
910 }
911 }
912
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +0000913 // Verify two-address constraints after leaving SSA form.
914 unsigned DefIdx;
915 if (!MRI->isSSA() && MO->isUse() &&
916 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
917 Reg != MI->getOperand(DefIdx).getReg())
918 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000919
920 // Check register classes.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000921 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000922 unsigned SubIdx = MO->getSubReg();
923
924 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000925 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000926 report("Illegal subregister index for physical register", MO, MONum);
927 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000928 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000929 if (const TargetRegisterClass *DRC =
930 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000931 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000932 report("Illegal physical register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000933 errs() << TRI->getName(Reg) << " is not a "
Craig Toppercf0444b2014-11-17 05:50:14 +0000934 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000935 }
936 }
937 } else {
938 // Virtual register.
939 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
940 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000941 const TargetRegisterClass *SRC =
942 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +0000943 if (!SRC) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000944 report("Invalid subregister index for virtual register", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000945 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +0000946 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000947 return;
948 }
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000949 if (RC != SRC) {
950 report("Invalid register class for subregister index", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000951 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000952 << " does not fully support subreg index " << SubIdx << "\n";
953 return;
954 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000955 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000956 if (const TargetRegisterClass *DRC =
957 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000958 if (SubIdx) {
959 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +0000960 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000961 if (!SuperRC) {
962 report("No largest legal super class exists.", MO, MONum);
963 return;
964 }
965 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
966 if (!DRC) {
967 report("No matching super-reg register class.", MO, MONum);
968 return;
969 }
970 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +0000971 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000972 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000973 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +0000974 << " register, but got a " << TRI->getRegClassName(RC)
975 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000976 }
977 }
978 }
979 }
980 break;
981 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000982
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000983 case MachineOperand::MO_RegisterMask:
984 regMasks.push_back(MO->getRegMask());
985 break;
986
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000987 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +0000988 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
989 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000990 break;
991
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000992 case MachineOperand::MO_FrameIndex:
993 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
994 LiveInts && !LiveInts->isNotInMIMap(MI)) {
995 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
996 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +0000997
998 // For a memory-to-memory move, we don't know if MI is using
999 // this frame index for loading or storing, so check for
1000 // liveness at reg-slot only in the simple load case.
1001 bool stores = MI->mayStore();
1002 bool simpleLoad = (MI->mayLoad() && !stores);
1003 if (simpleLoad && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001004 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001005 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001006 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001007 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001008 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001009 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001010 }
1011 }
1012 break;
1013
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001014 default:
1015 break;
1016 }
1017}
1018
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001019void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1020 const MachineInstr *MI = MO->getParent();
1021 const unsigned Reg = MO->getReg();
1022
1023 // Both use and def operands can read a register.
1024 if (MO->readsReg()) {
1025 regsLiveInButUnused.erase(Reg);
1026
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001027 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001028 addRegWithSubRegs(regsKilled, Reg);
1029
1030 // Check that LiveVars knows this kill.
1031 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1032 MO->isKill()) {
1033 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1034 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
1035 report("Kill missing from LiveVariables", MO, MONum);
1036 }
1037
1038 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001039 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
1040 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
1041 // Check the cached regunit intervals.
1042 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1043 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun34e1be92013-10-10 21:29:02 +00001044 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units)) {
1045 LiveQueryResult LRQ = LR->Query(UseIdx);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001046 if (!LRQ.valueIn()) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001047 report("No live segment at use", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001048 errs() << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
Matthias Braun34e1be92013-10-10 21:29:02 +00001049 << ' ' << *LR << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001050 }
1051 if (MO->isKill() && !LRQ.isKill()) {
1052 report("Live range continues after kill flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001053 errs() << PrintRegUnit(*Units, TRI) << ' ' << *LR << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001054 }
1055 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001056 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001057 }
1058
1059 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1060 if (LiveInts->hasInterval(Reg)) {
1061 // This is a virtual register interval.
1062 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun88dd0ab2013-10-10 21:28:52 +00001063 LiveQueryResult LRQ = LI.Query(UseIdx);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001064 if (!LRQ.valueIn()) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001065 report("No live segment at use", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001066 errs() << UseIdx << " is not live in " << LI << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001067 }
1068 // Check for extra kill flags.
1069 // Note that we allow missing kill flags for now.
1070 if (MO->isKill() && !LRQ.isKill()) {
1071 report("Live range continues after kill flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001072 errs() << "Live range: " << LI << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001073 }
1074 } else {
1075 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001076 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001077 }
1078 }
1079
1080 // Use of a dead register.
1081 if (!regsLive.count(Reg)) {
1082 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1083 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001084 bool Bad = !isReserved(Reg);
1085 // We are fine if just any subregister has a defined value.
1086 if (Bad) {
1087 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1088 ++SubRegs) {
1089 if (regsLive.count(*SubRegs)) {
1090 Bad = false;
1091 break;
1092 }
1093 }
1094 }
Matthias Braun96a31952015-01-14 22:25:14 +00001095 // If there is an additional implicit-use of a super register we stop
1096 // here. By definition we are fine if the super register is not
1097 // (completely) dead, if the complete super register is dead we will
1098 // get a report for its operand.
1099 if (Bad) {
1100 for (const MachineOperand &MOP : MI->uses()) {
1101 if (!MOP.isReg())
1102 continue;
1103 if (!MOP.isImplicit())
1104 continue;
1105 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1106 ++SubRegs) {
1107 if (*SubRegs == Reg) {
1108 Bad = false;
1109 break;
1110 }
1111 }
1112 }
1113 }
Matthias Braun96d77322014-12-10 01:13:13 +00001114 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001115 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001116 } else if (MRI->def_empty(Reg)) {
1117 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001118 } else {
1119 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1120 // We don't know which virtual registers are live in, so only complain
1121 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1122 // must be live in. PHI instructions are handled separately.
1123 if (MInfo.regsKilled.count(Reg))
1124 report("Using a killed virtual register", MO, MONum);
1125 else if (!MI->isPHI())
1126 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1127 }
1128 }
1129 }
1130
1131 if (MO->isDef()) {
1132 // Register defined.
1133 // TODO: verify that earlyclobber ops are not used.
1134 if (MO->isDead())
1135 addRegWithSubRegs(regsDead, Reg);
1136 else
1137 addRegWithSubRegs(regsDefined, Reg);
1138
1139 // Verify SSA form.
1140 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001141 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001142 report("Multiple virtual register defs in SSA form", MO, MONum);
1143
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001144 // Check LiveInts for a live segment, but only for virtual registers.
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001145 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1146 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001147 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1148 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001149 if (LiveInts->hasInterval(Reg)) {
1150 const LiveInterval &LI = LiveInts->getInterval(Reg);
1151 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1152 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001153 if (VNI->def != DefIdx) {
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001154 report("Inconsistent valno->def", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001155 errs() << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001156 << DefIdx << " in " << LI << '\n';
1157 }
1158 } else {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001159 report("No live segment at def", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001160 errs() << DefIdx << " is not live in " << LI << '\n';
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001161 }
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001162 // Check that, if the dead def flag is present, LiveInts agree.
1163 if (MO->isDead()) {
1164 LiveQueryResult LRQ = LI.Query(DefIdx);
1165 if (!LRQ.isDeadDef()) {
1166 report("Live range continues after dead def flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001167 errs() << "Live range: " << LI << '\n';
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001168 }
1169 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001170 } else {
1171 report("Virtual register has no Live interval", MO, MONum);
1172 }
1173 }
1174 }
1175}
1176
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001177void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001178}
1179
1180// This function gets called after visiting all instructions in a bundle. The
1181// argument points to the bundle header.
1182// Normal stand-alone instructions are also considered 'bundles', and this
1183// function is called for all of them.
1184void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001185 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1186 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001187 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001188 // Kill any masked registers.
1189 while (!regMasks.empty()) {
1190 const uint32_t *Mask = regMasks.pop_back_val();
1191 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1192 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1193 MachineOperand::clobbersPhysReg(Mask, *I))
1194 regsDead.push_back(*I);
1195 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001196 set_subtract(regsLive, regsDead); regsDead.clear();
1197 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001198}
1199
1200void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001201MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001202 MBBInfoMap[MBB].regsLiveOut = regsLive;
1203 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001204
1205 if (Indexes) {
1206 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1207 if (!(stop > lastIndex)) {
1208 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001209 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001210 << " last instruction was at " << lastIndex << '\n';
1211 }
1212 lastIndex = stop;
1213 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001214}
1215
1216// Calculate the largest possible vregsPassed sets. These are the registers that
1217// can pass through an MBB live, but may not be live every time. It is assumed
1218// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001219void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001220 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1221 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001222 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001223 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001224 BBInfo &MInfo = MBBInfoMap[&MBB];
1225 if (!MInfo.reachable)
1226 continue;
1227 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1228 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1229 BBInfo &SInfo = MBBInfoMap[*SuI];
1230 if (SInfo.addPassed(MInfo.regsLiveOut))
1231 todo.insert(*SuI);
1232 }
1233 }
1234
1235 // Iteratively push vregsPassed to successors. This will converge to the same
1236 // final state regardless of DenseSet iteration order.
1237 while (!todo.empty()) {
1238 const MachineBasicBlock *MBB = *todo.begin();
1239 todo.erase(MBB);
1240 BBInfo &MInfo = MBBInfoMap[MBB];
1241 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1242 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1243 if (*SuI == MBB)
1244 continue;
1245 BBInfo &SInfo = MBBInfoMap[*SuI];
1246 if (SInfo.addPassed(MInfo.vregsPassed))
1247 todo.insert(*SuI);
1248 }
1249 }
1250}
1251
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001252// Calculate the set of virtual registers that must be passed through each basic
1253// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001254// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001255void MachineVerifier::calcRegsRequired() {
1256 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001257 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001258 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001259 BBInfo &MInfo = MBBInfoMap[&MBB];
1260 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1261 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1262 BBInfo &PInfo = MBBInfoMap[*PrI];
1263 if (PInfo.addRequired(MInfo.vregsLiveIn))
1264 todo.insert(*PrI);
1265 }
1266 }
1267
1268 // Iteratively push vregsRequired to predecessors. This will converge to the
1269 // same final state regardless of DenseSet iteration order.
1270 while (!todo.empty()) {
1271 const MachineBasicBlock *MBB = *todo.begin();
1272 todo.erase(MBB);
1273 BBInfo &MInfo = MBBInfoMap[MBB];
1274 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1275 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1276 if (*PrI == MBB)
1277 continue;
1278 BBInfo &SInfo = MBBInfoMap[*PrI];
1279 if (SInfo.addRequired(MInfo.vregsRequired))
1280 todo.insert(*PrI);
1281 }
1282 }
1283}
1284
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001285// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001286// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001287void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001288 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001289 for (const auto &BBI : *MBB) {
1290 if (!BBI.isPHI())
1291 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001292 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001293
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001294 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1295 unsigned Reg = BBI.getOperand(i).getReg();
1296 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001297 if (!Pre->isSuccessor(MBB))
1298 continue;
1299 seen.insert(Pre);
1300 BBInfo &PrInfo = MBBInfoMap[Pre];
1301 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1302 report("PHI operand is not live-out from predecessor",
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001303 &BBI.getOperand(i), i);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001304 }
1305
1306 // Did we see all predecessors?
1307 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1308 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1309 if (!seen.count(*PrI)) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001310 report("Missing PHI operand", &BBI);
Owen Anderson21b17882015-02-04 00:02:59 +00001311 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001312 << " is a predecessor according to the CFG.\n";
1313 }
1314 }
1315 }
1316}
1317
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001318void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001319 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001320
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001321 for (const auto &MBB : *MF) {
1322 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001323
1324 // Skip unreachable MBBs.
1325 if (!MInfo.reachable)
1326 continue;
1327
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001328 checkPHIOps(&MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001329 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001330
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001331 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001332 calcRegsRequired();
1333
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001334 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001335 for (const auto &MBB : *MF) {
1336 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001337 for (RegSet::iterator
1338 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1339 ++I)
1340 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001341 report("Virtual register killed in block, but needed live out.", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001342 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001343 << " is used after the block.\n";
1344 }
1345 }
1346
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001347 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001348 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1349 for (RegSet::iterator
1350 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesen99014ff2012-03-10 00:44:11 +00001351 ++I)
1352 report("Virtual register def doesn't dominate all uses.",
1353 MRI->getVRegDef(*I));
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001354 }
1355
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001356 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001357 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001358 if (LiveInts)
1359 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001360}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001361
1362void MachineVerifier::verifyLiveVariables() {
1363 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001364 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1365 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001366 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001367 for (const auto &MBB : *MF) {
1368 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001369
1370 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1371 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001372 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1373 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001374 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001375 << " must be live through the block.\n";
1376 }
1377 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001378 if (VI.AliveBlocks.test(MBB.getNumber())) {
1379 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001380 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001381 << " is not needed live through the block.\n";
1382 }
1383 }
1384 }
1385 }
1386}
1387
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001388void MachineVerifier::verifyLiveIntervals() {
1389 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001390 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1391 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001392
1393 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001394 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001395 continue;
1396
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001397 if (!LiveInts->hasInterval(Reg)) {
1398 report("Missing live interval for virtual register", MF);
Owen Anderson21b17882015-02-04 00:02:59 +00001399 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001400 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001401 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001402
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001403 const LiveInterval &LI = LiveInts->getInterval(Reg);
1404 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001405 verifyLiveInterval(LI);
1406 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001407
1408 // Verify all the cached regunit intervals.
1409 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001410 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1411 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001412}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001413
Matthias Braun364e6e92013-10-10 21:28:54 +00001414void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001415 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001416 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001417 if (VNI->isUnused())
1418 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001419
Matthias Braun364e6e92013-10-10 21:28:54 +00001420 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001421
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001422 if (!DefVNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001423 report("Valno not live at def and not marked unused", MF, LR, Reg,
1424 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001425 errs() << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001426 return;
1427 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001428
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001429 if (DefVNI != VNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001430 report("Live segment at def has different valno", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001431 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +00001432 << " where valno #" << DefVNI->id << " is live\n";
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001433 return;
1434 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001435
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001436 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1437 if (!MBB) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001438 report("Invalid definition index", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001439 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Matthias Braun364e6e92013-10-10 21:28:54 +00001440 << " in " << LR << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001441 return;
1442 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001443
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001444 if (VNI->isPHIDef()) {
1445 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001446 report("PHIDef value is not defined at MBB start", MBB, LR, Reg,
1447 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001448 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +00001449 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001450 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001451 return;
1452 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001453
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001454 // Non-PHI def.
1455 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1456 if (!MI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001457 report("No instruction at def index", MBB, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001458 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001459 return;
1460 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001461
Matthias Braun364e6e92013-10-10 21:28:54 +00001462 if (Reg != 0) {
1463 bool hasDef = false;
1464 bool isEarlyClobber = false;
1465 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1466 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001467 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001468 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1469 if (MOI->getReg() != Reg)
1470 continue;
1471 } else {
1472 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1473 !TRI->hasRegUnit(MOI->getReg(), Reg))
1474 continue;
1475 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001476 if (LaneMask != 0 &&
1477 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask) == 0)
1478 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001479 hasDef = true;
1480 if (MOI->isEarlyClobber())
1481 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001482 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001483
Matthias Braun364e6e92013-10-10 21:28:54 +00001484 if (!hasDef) {
1485 report("Defining instruction does not modify register", MI);
Owen Anderson21b17882015-02-04 00:02:59 +00001486 errs() << "Valno #" << VNI->id << " in " << LR << '\n';
Matthias Braun364e6e92013-10-10 21:28:54 +00001487 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001488
Matthias Braun364e6e92013-10-10 21:28:54 +00001489 // Early clobber defs begin at USE slots, but other defs must begin at
1490 // DEF slots.
1491 if (isEarlyClobber) {
1492 if (!VNI->def.isEarlyClobber()) {
Matthias Braun47760d92014-11-19 19:46:13 +00001493 report("Early clobber def must be at an early-clobber slot", MBB, LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001494 Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001495 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Matthias Braun364e6e92013-10-10 21:28:54 +00001496 }
1497 } else if (!VNI->def.isRegister()) {
1498 report("Non-PHI, non-early clobber def must be at a register slot",
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001499 MBB, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001500 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001501 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001502 }
1503}
1504
Matthias Braun364e6e92013-10-10 21:28:54 +00001505void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1506 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001507 unsigned Reg, LaneBitmask LaneMask)
1508{
Matthias Braun364e6e92013-10-10 21:28:54 +00001509 const LiveRange::Segment &S = *I;
1510 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001511 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001512
Matthias Braun364e6e92013-10-10 21:28:54 +00001513 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001514 report("Foreign valno in live segment", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001515 errs() << S << " has a bad valno\n";
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001516 }
1517
1518 if (VNI->isUnused()) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001519 report("Live segment valno is marked unused", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001520 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001521 }
1522
Matthias Braun364e6e92013-10-10 21:28:54 +00001523 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001524 if (!MBB) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001525 report("Bad start of live segment, no basic block", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001526 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001527 return;
1528 }
1529 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001530 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001531 report("Live segment must begin at MBB entry or valno def", MBB, LR, Reg,
1532 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001533 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001534 }
1535
1536 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001537 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001538 if (!EndMBB) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001539 report("Bad end of live segment, no basic block", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001540 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001541 return;
1542 }
1543
1544 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001545 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001546 return;
1547
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001548 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001549 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1550 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001551 return;
1552
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001553 // The live segment is ending inside EndMBB
1554 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001555 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001556 if (!MI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001557 report("Live segment doesn't end at a valid instruction", EndMBB, LR, Reg,
1558 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001559 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001560 return;
1561 }
1562
1563 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001564 if (S.end.isBlock()) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001565 report("Live segment ends at B slot of an instruction", EndMBB, LR, Reg,
1566 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001567 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001568 }
1569
Matthias Braun364e6e92013-10-10 21:28:54 +00001570 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001571 // Segment ends on the dead slot.
1572 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001573 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun47760d92014-11-19 19:46:13 +00001574 report("Live segment ending at dead slot spans instructions", EndMBB, LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001575 Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001576 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001577 }
1578 }
1579
1580 // A live segment can only end at an early-clobber slot if it is being
1581 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001582 if (S.end.isEarlyClobber()) {
1583 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001584 report("Live segment ending at early clobber slot must be "
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001585 "redefined by an EC def in the same instruction", EndMBB, LR, Reg,
1586 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001587 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001588 }
1589 }
1590
1591 // The following checks only apply to virtual registers. Physreg liveness
1592 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001593 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001594 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001595 // use, or a dead flag on a def.
1596 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001597 bool hasSubRegDef = false;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001598 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001599 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001600 continue;
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001601 if (LaneMask != 0 &&
1602 (LaneMask & TRI->getSubRegIndexLaneMask(MOI->getSubReg())) == 0)
1603 continue;
Matthias Braun21554d92014-12-10 01:13:11 +00001604 if (MOI->isDef() && MOI->getSubReg() != 0)
1605 hasSubRegDef = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001606 if (MOI->readsReg())
1607 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001608 }
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001609 if (!S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001610 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001611 // When tracking subregister liveness, the main range must start new
1612 // values on partial register writes, even if there is no read.
Matthias Brauna25e13a2015-03-19 00:21:58 +00001613 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask != 0 ||
1614 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001615 report("Instruction ending live segment doesn't read the register",
1616 MI);
Owen Anderson21b17882015-02-04 00:02:59 +00001617 errs() << S << " in " << LR << '\n';
Matthias Braun21554d92014-12-10 01:13:11 +00001618 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001619 }
1620 }
1621 }
1622
1623 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001624 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001625 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00001626 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001627 // Not live-in to any blocks.
1628 if (MBB == EndMBB)
1629 return;
1630 // Skip this block.
1631 ++MFI;
1632 }
1633 for (;;) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001634 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001635 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00001636 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00001637 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001638 if (&*MFI == EndMBB)
1639 break;
1640 ++MFI;
1641 continue;
1642 }
1643
1644 // Is VNI a PHI-def in the current block?
1645 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001646 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001647
1648 // Check that VNI is live-out of all predecessors.
1649 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1650 PE = MFI->pred_end(); PI != PE; ++PI) {
1651 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001652 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001653
1654 // All predecessors must have a live-out value.
1655 if (!PVNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001656 report("Register not marked live out of predecessor", *PI, LR, Reg,
1657 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001658 errs() << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001659 << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
1660 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001661 continue;
1662 }
1663
1664 // Only PHI-defs can take different predecessor values.
1665 if (!IsPHI && PVNI != VNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001666 report("Different value live out of predecessor", *PI, LR, Reg,
1667 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001668 errs() << "Valno #" << PVNI->id << " live out of BB#"
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001669 << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
1670 << " live into BB#" << MFI->getNumber() << '@'
1671 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001672 }
1673 }
1674 if (&*MFI == EndMBB)
1675 break;
1676 ++MFI;
1677 }
1678}
1679
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001680void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001681 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00001682 for (const VNInfo *VNI : LR.valnos)
1683 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001684
Matthias Braun364e6e92013-10-10 21:28:54 +00001685 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001686 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00001687}
1688
1689void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001690 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00001691 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1692 verifyLiveRange(LI, Reg);
1693
Matthias Braune6a24852015-09-25 21:51:14 +00001694 LaneBitmask Mask = 0;
1695 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00001696 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1697 if ((Mask & SR.LaneMask) != 0)
1698 report("Lane masks of sub ranges overlap in live interval", MF, LI);
1699 if ((SR.LaneMask & ~MaxMask) != 0)
1700 report("Subrange lanemask is invalid", MF, LI);
Matthias Braun0d4cebd2015-07-16 18:55:35 +00001701 if (SR.empty())
1702 report("Subrange must not be empty", MF, SR, LI.reg, SR.LaneMask);
Matthias Braune962e522015-03-25 21:18:22 +00001703 Mask |= SR.LaneMask;
1704 verifyLiveRange(SR, LI.reg, SR.LaneMask);
1705 if (!LI.covers(SR))
1706 report("A Subrange is not covered by the main range", MF, LI);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001707 }
1708
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001709 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00001710 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1711 unsigned NumComp = ConEQ.Classify(&LI);
1712 if (NumComp > 1) {
1713 report("Multiple connected components in live interval", MF, LI);
1714 for (unsigned comp = 0; comp != NumComp; ++comp) {
1715 errs() << comp << ": valnos";
1716 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1717 E = LI.vni_end(); I!=E; ++I)
1718 if (comp == ConEQ.getEqClass(*I))
1719 errs() << ' ' << (*I)->id;
1720 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00001721 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001722 }
1723}
Manman Renaa6875b2013-07-15 21:26:31 +00001724
1725namespace {
1726 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1727 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1728 // value is zero.
1729 // We use a bool plus an integer to capture the stack state.
1730 struct StackStateOfBB {
1731 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1732 ExitIsSetup(false) { }
1733 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1734 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1735 ExitIsSetup(ExitSetup) { }
1736 // Can be negative, which means we are setting up a frame.
1737 int EntryValue;
1738 int ExitValue;
1739 bool EntryIsSetup;
1740 bool ExitIsSetup;
1741 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001742}
Manman Renaa6875b2013-07-15 21:26:31 +00001743
1744/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1745/// by a FrameDestroy <n>, stack adjustments are identical on all
1746/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1747void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001748 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1749 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Manman Renaa6875b2013-07-15 21:26:31 +00001750
1751 SmallVector<StackStateOfBB, 8> SPState;
1752 SPState.resize(MF->getNumBlockIDs());
1753 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1754
1755 // Visit the MBBs in DFS order.
1756 for (df_ext_iterator<const MachineFunction*,
1757 SmallPtrSet<const MachineBasicBlock*, 8> >
1758 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1759 DFI != DFE; ++DFI) {
1760 const MachineBasicBlock *MBB = *DFI;
1761
1762 StackStateOfBB BBState;
1763 // Check the exit state of the DFS stack predecessor.
1764 if (DFI.getPathLength() >= 2) {
1765 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1766 assert(Reachable.count(StackPred) &&
1767 "DFS stack predecessor is already visited.\n");
1768 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1769 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1770 BBState.ExitValue = BBState.EntryValue;
1771 BBState.ExitIsSetup = BBState.EntryIsSetup;
1772 }
1773
1774 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001775 for (const auto &I : *MBB) {
1776 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001777 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001778 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001779 assert(Size >= 0 &&
1780 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1781
1782 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001783 report("FrameSetup is after another FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001784 BBState.ExitValue -= Size;
1785 BBState.ExitIsSetup = true;
1786 }
1787
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001788 if (I.getOpcode() == FrameDestroyOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001789 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001790 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001791 assert(Size >= 0 &&
1792 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1793
1794 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001795 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001796 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1797 BBState.ExitValue;
1798 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001799 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00001800 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00001801 << AbsSPAdj << ">.\n";
1802 }
1803 BBState.ExitValue += Size;
1804 BBState.ExitIsSetup = false;
1805 }
1806 }
1807 SPState[MBB->getNumber()] = BBState;
1808
1809 // Make sure the exit state of any predecessor is consistent with the entry
1810 // state.
1811 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
1812 E = MBB->pred_end(); I != E; ++I) {
1813 if (Reachable.count(*I) &&
1814 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
1815 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
1816 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001817 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00001818 << SPState[(*I)->getNumber()].ExitValue << ", "
1819 << SPState[(*I)->getNumber()].ExitIsSetup
1820 << "), while BB#" << MBB->getNumber() << " has entry state ("
1821 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
1822 }
1823 }
1824
1825 // Make sure the entry state of any successor is consistent with the exit
1826 // state.
1827 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
1828 E = MBB->succ_end(); I != E; ++I) {
1829 if (Reachable.count(*I) &&
1830 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
1831 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
1832 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001833 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00001834 << SPState[(*I)->getNumber()].EntryValue << ", "
1835 << SPState[(*I)->getNumber()].EntryIsSetup
1836 << "), while BB#" << MBB->getNumber() << " has exit state ("
1837 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
1838 }
1839 }
1840
1841 // Make sure a basic block with return ends with zero stack adjustment.
1842 if (!MBB->empty() && MBB->back().isReturn()) {
1843 if (BBState.ExitIsSetup)
1844 report("A return block ends with a FrameSetup.", MBB);
1845 if (BBState.ExitValue)
1846 report("A return block ends with a nonzero stack adjustment.", MBB);
1847 }
1848 }
1849}