blob: 50f7e69cf692db1f128f525ce7f441fe2d595df8 [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"
Reid Kleckner64b003f2015-11-09 21:04:00 +000031#include "llvm/Analysis/LibCallSemantics.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/CodeGen/LiveIntervalAnalysis.h"
33#include "llvm/CodeGen/LiveStackAnalysis.h"
34#include "llvm/CodeGen/LiveVariables.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/CodeGen/MachineMemOperand.h"
38#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/InlineAsm.h"
41#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000043#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000044#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000045#include "llvm/Support/FileSystem.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000046#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Target/TargetInstrInfo.h"
48#include "llvm/Target/TargetMachine.h"
49#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000050#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000051using namespace llvm;
52
53namespace {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000054 struct MachineVerifier {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000055
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000056 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000057 PASS(pass),
Owen Anderson21b17882015-02-04 00:02:59 +000058 Banner(b)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000059 {}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000060
61 bool runOnMachineFunction(MachineFunction &MF);
62
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000063 Pass *const PASS;
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000064 const char *Banner;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000065 const MachineFunction *MF;
66 const TargetMachine *TM;
Evan Cheng8d71a752011-06-27 21:26:13 +000067 const TargetInstrInfo *TII;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000068 const TargetRegisterInfo *TRI;
69 const MachineRegisterInfo *MRI;
70
71 unsigned foundErrors;
72
73 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000074 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000075 typedef DenseSet<unsigned> RegSet;
76 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000077 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000078
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000079 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000080 BlockSet FunctionBlocks;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000081
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000082 BitVector regsReserved;
83 RegSet regsLive;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000084 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000085 RegMaskVector regMasks;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000086 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000087
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +000088 SlotIndex lastIndex;
89
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000090 // Add Reg and any sub-registers to RV
91 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
92 RV.push_back(Reg);
93 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +000094 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
95 RV.push_back(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000096 }
97
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000098 struct BBInfo {
99 // Is this MBB reachable from the MF entry point?
100 bool reachable;
101
102 // Vregs that must be live in because they are used without being
103 // defined. Map value is the user.
104 RegMap vregsLiveIn;
105
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000106 // Regs killed in MBB. They may be defined again, and will then be in both
107 // regsKilled and regsLiveOut.
108 RegSet regsKilled;
109
110 // Regs defined in MBB and live out. Note that vregs passing through may
111 // be live out without being mentioned here.
112 RegSet regsLiveOut;
113
114 // Vregs that pass through MBB untouched. This set is disjoint from
115 // regsKilled and regsLiveOut.
116 RegSet vregsPassed;
117
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000118 // Vregs that must pass through MBB because they are needed by a successor
119 // block. This set is disjoint from regsLiveOut.
120 RegSet vregsRequired;
121
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000122 // Set versions of block's predecessor and successor lists.
123 BlockSet Preds, Succs;
124
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000125 BBInfo() : reachable(false) {}
126
127 // Add register to vregsPassed if it belongs there. Return true if
128 // anything changed.
129 bool addPassed(unsigned Reg) {
130 if (!TargetRegisterInfo::isVirtualRegister(Reg))
131 return false;
132 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
133 return false;
134 return vregsPassed.insert(Reg).second;
135 }
136
137 // Same for a full set.
138 bool addPassed(const RegSet &RS) {
139 bool changed = false;
140 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
141 if (addPassed(*I))
142 changed = true;
143 return changed;
144 }
145
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000146 // Add register to vregsRequired if it belongs there. Return true if
147 // anything changed.
148 bool addRequired(unsigned Reg) {
149 if (!TargetRegisterInfo::isVirtualRegister(Reg))
150 return false;
151 if (regsLiveOut.count(Reg))
152 return false;
153 return vregsRequired.insert(Reg).second;
154 }
155
156 // Same for a full set.
157 bool addRequired(const RegSet &RS) {
158 bool changed = false;
159 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
160 if (addRequired(*I))
161 changed = true;
162 return changed;
163 }
164
165 // Same for a full map.
166 bool addRequired(const RegMap &RM) {
167 bool changed = false;
168 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
169 if (addRequired(I->first))
170 changed = true;
171 return changed;
172 }
173
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000174 // Live-out registers are either in regsLiveOut or vregsPassed.
175 bool isLiveOut(unsigned Reg) const {
176 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
177 }
178 };
179
180 // Extra register info per MBB.
181 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
182
183 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000184 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000185 }
186
Lang Hames1ce837a2012-02-14 19:17:48 +0000187 bool isAllocatable(unsigned Reg) {
Jakob Stoklund Olesen244beb42012-10-16 00:05:06 +0000188 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
Lang Hames1ce837a2012-02-14 19:17:48 +0000189 }
190
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000191 // Analysis information if available
192 LiveVariables *LiveVars;
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +0000193 LiveIntervals *LiveInts;
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000194 LiveStacks *LiveStks;
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000195 SlotIndexes *Indexes;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000196
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000197 void visitMachineFunctionBefore();
198 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000199 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000200 void visitMachineInstrBefore(const MachineInstr *MI);
201 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
202 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000203 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000204 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
205 void visitMachineFunctionAfter();
206
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000207 template <typename T> void report(const char *msg, ilist_iterator<T> I) {
208 report(msg, &*I);
209 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000210 void report(const char *msg, const MachineFunction *MF);
211 void report(const char *msg, const MachineBasicBlock *MBB);
212 void report(const char *msg, const MachineInstr *MI);
213 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000214 void report(const char *msg, const MachineFunction *MF,
215 const LiveInterval &LI);
216 void report(const char *msg, const MachineBasicBlock *MBB,
217 const LiveInterval &LI);
Matthias Braun364e6e92013-10-10 21:28:54 +0000218 void report(const char *msg, const MachineFunction *MF,
Matthias Braune6a24852015-09-25 21:51:14 +0000219 const LiveRange &LR, unsigned Reg, LaneBitmask LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000220 void report(const char *msg, const MachineBasicBlock *MBB,
Matthias Braune6a24852015-09-25 21:51:14 +0000221 const LiveRange &LR, unsigned Reg, LaneBitmask LaneMask);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000222
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000223 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000224
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000225 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000226 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000227 void calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000228 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000229
230 void calcRegsRequired();
231 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000232 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000233 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000234 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
235 unsigned);
Matthias Braun364e6e92013-10-10 21:28:54 +0000236 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000237 const LiveRange::const_iterator I, unsigned,
238 unsigned);
Matthias Braune6a24852015-09-25 21:51:14 +0000239 void verifyLiveRange(const LiveRange&, unsigned, LaneBitmask LaneMask = 0);
Manman Renaa6875b2013-07-15 21:26:31 +0000240
241 void verifyStackFrame();
Matthias Braun80595462015-09-09 17:49:46 +0000242
243 void verifySlotIndexes() const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000244 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000245
246 struct MachineVerifierPass : public MachineFunctionPass {
247 static char ID; // Pass ID, replacement for typeid
Matthias Brauna4e932d2014-12-11 19:41:51 +0000248 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000249
Matthias Brauna4e932d2014-12-11 19:41:51 +0000250 MachineVerifierPass(const std::string &banner = nullptr)
251 : MachineFunctionPass(ID), Banner(banner) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000252 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
253 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000254
Craig Topper4584cd52014-03-07 09:26:03 +0000255 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000256 AU.setPreservesAll();
257 MachineFunctionPass::getAnalysisUsage(AU);
258 }
259
Craig Topper4584cd52014-03-07 09:26:03 +0000260 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Brauna4e932d2014-12-11 19:41:51 +0000261 MF.verify(this, Banner.c_str());
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000262 return false;
263 }
264 };
265
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000266}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000267
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000268char MachineVerifierPass::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +0000269INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000270 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000271
Matthias Brauna4e932d2014-12-11 19:41:51 +0000272FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000273 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000274}
275
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000276void MachineFunction::verify(Pass *p, const char *Banner) const {
277 MachineVerifier(p, Banner)
278 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesen27440e72009-11-13 21:56:09 +0000279}
280
Matthias Braun80595462015-09-09 17:49:46 +0000281void MachineVerifier::verifySlotIndexes() const {
282 if (Indexes == nullptr)
283 return;
284
285 // Ensure the IdxMBB list is sorted by slot indexes.
286 SlotIndex Last;
287 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
288 E = Indexes->MBBIndexEnd(); I != E; ++I) {
289 assert(!Last.isValid() || I->first > Last);
290 Last = I->first;
291 }
292}
293
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000294bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000295 foundErrors = 0;
296
297 this->MF = &MF;
298 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000299 TII = MF.getSubtarget().getInstrInfo();
300 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000301 MRI = &MF.getRegInfo();
302
Craig Topperc0196b12014-04-14 00:51:57 +0000303 LiveVars = nullptr;
304 LiveInts = nullptr;
305 LiveStks = nullptr;
306 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000307 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000308 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000309 // We don't want to verify LiveVariables if LiveIntervals is available.
310 if (!LiveInts)
311 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000312 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000313 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000314 }
315
Matthias Braun80595462015-09-09 17:49:46 +0000316 verifySlotIndexes();
317
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000318 visitMachineFunctionBefore();
319 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
320 MFI!=MFE; ++MFI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000321 visitMachineBasicBlockBefore(&*MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000322 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000323 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000324 // Do we expect the next instruction to be part of the same bundle?
325 bool InBundle = false;
326
Evan Cheng7fae11b2011-12-14 02:11:42 +0000327 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
328 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000329 if (MBBI->getParent() != &*MFI) {
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000330 report("Bad instruction parent pointer", MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000331 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000332 continue;
333 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000334
335 // Check for consistent bundle flags.
336 if (InBundle && !MBBI->isBundledWithPred())
337 report("Missing BundledPred flag, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000338 "BundledSucc was set on predecessor",
339 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000340 if (!InBundle && MBBI->isBundledWithPred())
341 report("BundledPred flag is set, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000342 "but BundledSucc not set on predecessor",
343 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000344
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000345 // Is this a bundle header?
346 if (!MBBI->isInsideBundle()) {
347 if (CurBundle)
348 visitMachineBundleAfter(CurBundle);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000349 CurBundle = &*MBBI;
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000350 visitMachineBundleBefore(CurBundle);
351 } else if (!CurBundle)
352 report("No bundle header", MBBI);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000353 visitMachineInstrBefore(&*MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000354 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
355 const MachineInstr &MI = *MBBI;
356 const MachineOperand &Op = MI.getOperand(I);
357 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000358 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000359 // functions when replacing operands of a MachineInstr.
360 report("Instruction has operand with wrong parent set", &MI);
361 }
362
363 visitMachineOperand(&Op, I);
364 }
365
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000366 visitMachineInstrAfter(&*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000367
368 // Was this the last bundled instruction?
369 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000370 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000371 if (CurBundle)
372 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000373 if (InBundle)
374 report("BundledSucc flag set on last instruction in block", &MFI->back());
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000375 visitMachineBasicBlockAfter(&*MFI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000376 }
377 visitMachineFunctionAfter();
378
Owen Anderson21b17882015-02-04 00:02:59 +0000379 if (foundErrors)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000380 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000381
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000382 // Clean up.
383 regsLive.clear();
384 regsDefined.clear();
385 regsDead.clear();
386 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000387 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000388 regsLiveInButUnused.clear();
389 MBBInfoMap.clear();
390
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000391 return false; // no changes
392}
393
Chris Lattner75f40452009-08-23 01:03:30 +0000394void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000395 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000396 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000397 if (!foundErrors++) {
398 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000399 errs() << "# " << Banner << '\n';
400 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000401 }
Owen Anderson21b17882015-02-04 00:02:59 +0000402 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000403 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000404}
405
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000406void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000407 assert(MBB);
408 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000409 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000410 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000411 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000412 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000413 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000414 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000415 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000416}
417
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000418void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000419 assert(MI);
420 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000421 errs() << "- instruction: ";
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000422 if (Indexes && Indexes->hasIndex(MI))
Owen Anderson21b17882015-02-04 00:02:59 +0000423 errs() << Indexes->getInstructionIndex(MI) << '\t';
424 MI->print(errs(), TM);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000425}
426
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000427void MachineVerifier::report(const char *msg,
428 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000429 assert(MO);
430 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000431 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000432 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000433 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000434}
435
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000436void MachineVerifier::report(const char *msg, const MachineFunction *MF,
437 const LiveInterval &LI) {
438 report(msg, MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000439 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000440}
441
442void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
443 const LiveInterval &LI) {
444 report(msg, MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000445 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000446}
447
Matthias Braun364e6e92013-10-10 21:28:54 +0000448void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000449 const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +0000450 LaneBitmask LaneMask) {
Matthias Braun364e6e92013-10-10 21:28:54 +0000451 report(msg, MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000452 errs() << "- liverange: " << LR << '\n';
453 errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000454 if (LaneMask != 0)
Matthias Braunc804cdb2015-09-25 21:51:24 +0000455 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
Matthias Braun364e6e92013-10-10 21:28:54 +0000456}
457
458void MachineVerifier::report(const char *msg, const MachineFunction *MF,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000459 const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +0000460 LaneBitmask LaneMask) {
Matthias Braun364e6e92013-10-10 21:28:54 +0000461 report(msg, MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000462 errs() << "- liverange: " << LR << '\n';
463 errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000464 if (LaneMask != 0)
Matthias Braunc804cdb2015-09-25 21:51:24 +0000465 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
Matthias Braun364e6e92013-10-10 21:28:54 +0000466}
467
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000468void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000469 BBInfo &MInfo = MBBInfoMap[MBB];
470 if (!MInfo.reachable) {
471 MInfo.reachable = true;
472 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
473 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
474 markReachable(*SuI);
475 }
476}
477
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000478void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000479 lastIndex = SlotIndex();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000480 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000481
482 // A sub-register of a reserved register is also reserved
483 for (int Reg = regsReserved.find_first(); Reg>=0;
484 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000485 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000486 // FIXME: This should probably be:
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000487 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
488 regsReserved.set(*SubRegs);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000489 }
490 }
Lang Hames1ce837a2012-02-14 19:17:48 +0000491
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000492 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000493
494 // Build a set of the basic blocks in the function.
495 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000496 for (const auto &MBB : *MF) {
497 FunctionBlocks.insert(&MBB);
498 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000499
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000500 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
501 if (MInfo.Preds.size() != MBB.pred_size())
502 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000503
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000504 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
505 if (MInfo.Succs.size() != MBB.succ_size())
506 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000507 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000508
509 // Check that the register use lists are sane.
510 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000511
512 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000513}
514
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000515// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000516static bool matchPair(MachineBasicBlock::const_succ_iterator i,
517 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000518 if (*i == a)
519 return *++i == b;
520 if (*i == b)
521 return *++i == a;
522 return false;
523}
524
525void
526MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000527 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000528
Lang Hames1ce837a2012-02-14 19:17:48 +0000529 if (MRI->isSSA()) {
530 // If this block has allocatable physical registers live-in, check that
531 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000532 for (const auto &LI : MBB->liveins()) {
533 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Lang Hames1ce837a2012-02-14 19:17:48 +0000534 MBB != MBB->getParent()->begin()) {
535 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
536 }
537 }
538 }
539
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000540 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000541 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000542 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000543 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000544 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000545 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000546 if (!FunctionBlocks.count(*I))
547 report("MBB has successor that isn't part of the function.", MBB);
548 if (!MBBInfoMap[*I].Preds.count(MBB)) {
549 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000550 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000551 << (*I)->getNumber() << ".\n";
552 }
553 }
554
555 // Check the predecessor list.
556 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
557 E = MBB->pred_end(); I != E; ++I) {
558 if (!FunctionBlocks.count(*I))
559 report("MBB has predecessor that isn't part of the function.", MBB);
560 if (!MBBInfoMap[*I].Succs.count(MBB)) {
561 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000562 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000563 << (*I)->getNumber() << ".\n";
564 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000565 }
Bill Wendling2a401312011-05-04 22:54:05 +0000566
567 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
568 const BasicBlock *BB = MBB->getBasicBlock();
Reid Kleckner64b003f2015-11-09 21:04:00 +0000569 const Function *Fn = MF->getFunction();
Bill Wendling2a401312011-05-04 22:54:05 +0000570 if (LandingPadSuccs.size() > 1 &&
571 !(AsmInfo &&
572 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
Reid Kleckner64b003f2015-11-09 21:04:00 +0000573 BB && isa<SwitchInst>(BB->getTerminator())) &&
574 !isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000575 report("MBB has more than one landing pad successor", MBB);
576
Dan Gohman352a4952009-08-27 02:43:49 +0000577 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000578 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000579 SmallVector<MachineOperand, 4> Cond;
580 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
581 TBB, FBB, Cond)) {
582 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
583 // check whether its answers match up with reality.
584 if (!TBB && !FBB) {
585 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000586 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000587 ++MBBI;
588 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000589 // It's possible that the block legitimately ends with a noreturn
590 // call or an unreachable, in which case it won't actually fall
591 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000592 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000593 // It's possible that the block legitimately ends with a noreturn
594 // call or an unreachable, in which case it won't actuall fall
595 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000596 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000597 report("MBB exits via unconditional fall-through but doesn't have "
598 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000599 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000600 report("MBB exits via unconditional fall-through but its successor "
601 "differs from its CFG successor!", MBB);
602 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000603 if (!MBB->empty() && MBB->back().isBarrier() &&
604 !TII->isPredicated(&MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000605 report("MBB exits via unconditional fall-through but ends with a "
606 "barrier instruction!", MBB);
607 }
608 if (!Cond.empty()) {
609 report("MBB exits via unconditional fall-through but has a condition!",
610 MBB);
611 }
612 } else if (TBB && !FBB && Cond.empty()) {
613 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000614 // If the block has exactly one successor, that happens to be a
615 // landingpad, accept it as valid control flow.
616 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
617 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
618 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000619 report("MBB exits via unconditional branch but doesn't have "
620 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000621 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000622 report("MBB exits via unconditional branch but the CFG "
623 "successor doesn't match the actual successor!", MBB);
624 }
625 if (MBB->empty()) {
626 report("MBB exits via unconditional branch but doesn't contain "
627 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000628 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000629 report("MBB exits via unconditional branch but doesn't end with a "
630 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000631 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000632 report("MBB exits via unconditional branch but the branch isn't a "
633 "terminator instruction!", MBB);
634 }
635 } else if (TBB && !FBB && !Cond.empty()) {
636 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000637 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000638 ++MBBI;
639 if (MBBI == MF->end()) {
640 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000641 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000642 // A conditional branch with only one successor is weird, but allowed.
643 if (&*MBBI != TBB)
644 report("MBB exits via conditional branch/fall-through but only has "
645 "one CFG successor!", MBB);
646 else if (TBB != *MBB->succ_begin())
647 report("MBB exits via conditional branch/fall-through but the CFG "
648 "successor don't match the actual successor!", MBB);
649 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000650 report("MBB exits via conditional branch/fall-through but doesn't have "
651 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000652 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000653 report("MBB exits via conditional branch/fall-through but the CFG "
654 "successors don't match the actual successors!", MBB);
655 }
656 if (MBB->empty()) {
657 report("MBB exits via conditional branch/fall-through but doesn't "
658 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000659 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000660 report("MBB exits via conditional branch/fall-through but ends with a "
661 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000662 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000663 report("MBB exits via conditional branch/fall-through but the branch "
664 "isn't a terminator instruction!", MBB);
665 }
666 } else if (TBB && FBB) {
667 // Block conditionally branches somewhere, otherwise branches
668 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000669 if (MBB->succ_size() == 1) {
670 // A conditional branch with only one successor is weird, but allowed.
671 if (FBB != TBB)
672 report("MBB exits via conditional branch/branch through but only has "
673 "one CFG successor!", MBB);
674 else if (TBB != *MBB->succ_begin())
675 report("MBB exits via conditional branch/branch through but the CFG "
676 "successor don't match the actual successor!", MBB);
677 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000678 report("MBB exits via conditional branch/branch but doesn't have "
679 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000680 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000681 report("MBB exits via conditional branch/branch but the CFG "
682 "successors don't match the actual successors!", MBB);
683 }
684 if (MBB->empty()) {
685 report("MBB exits via conditional branch/branch but doesn't "
686 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000687 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000688 report("MBB exits via conditional branch/branch but doesn't end with a "
689 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000690 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000691 report("MBB exits via conditional branch/branch but the branch "
692 "isn't a terminator instruction!", MBB);
693 }
694 if (Cond.empty()) {
695 report("MBB exits via conditinal branch/branch but there's no "
696 "condition!", MBB);
697 }
698 } else {
699 report("AnalyzeBranch returned invalid data!", MBB);
700 }
701 }
702
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000703 regsLive.clear();
Matthias Braund9da1622015-09-09 18:08:03 +0000704 for (const auto &LI : MBB->liveins()) {
705 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000706 report("MBB live-in list contains non-physical register", MBB);
707 continue;
708 }
Matthias Braund9da1622015-09-09 18:08:03 +0000709 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
Chad Rosierabdb1d62013-05-22 23:17:36 +0000710 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000711 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000712 }
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +0000713 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000714
715 const MachineFrameInfo *MFI = MF->getFrameInfo();
716 assert(MFI && "Function has no frame info");
Matthias Braun111f5d82015-05-28 23:20:35 +0000717 BitVector PR = MFI->getPristineRegs(*MF);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000718 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000719 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
720 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000721 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000722 }
723
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000724 regsKilled.clear();
725 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000726
727 if (Indexes)
728 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000729}
730
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000731// This function gets called for all bundle headers, including normal
732// stand-alone unbundled instructions.
733void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
734 if (Indexes && Indexes->hasIndex(MI)) {
735 SlotIndex idx = Indexes->getInstructionIndex(MI);
736 if (!(idx > lastIndex)) {
737 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000738 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000739 }
740 lastIndex = idx;
741 }
Pete Coopercd720162012-06-07 17:41:39 +0000742
743 // Ensure non-terminators don't follow terminators.
744 // Ignore predicated terminators formed by if conversion.
745 // FIXME: If conversion shouldn't need to violate this rule.
746 if (MI->isTerminator() && !TII->isPredicated(MI)) {
747 if (!FirstTerminator)
748 FirstTerminator = MI;
749 } else if (FirstTerminator) {
750 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000751 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000752 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000753}
754
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000755// The operands on an INLINEASM instruction must follow a template.
756// Verify that the flag operands make sense.
757void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
758 // The first two operands on INLINEASM are the asm string and global flags.
759 if (MI->getNumOperands() < 2) {
760 report("Too few operands on inline asm", MI);
761 return;
762 }
763 if (!MI->getOperand(0).isSymbol())
764 report("Asm string must be an external symbol", MI);
765 if (!MI->getOperand(1).isImm())
766 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000767 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
768 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
769 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000770 report("Unknown asm flags", &MI->getOperand(1), 1);
771
Gabor Horvathfee04342015-03-16 09:53:42 +0000772 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000773
774 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
775 unsigned NumOps;
776 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
777 const MachineOperand &MO = MI->getOperand(OpNo);
778 // There may be implicit ops after the fixed operands.
779 if (!MO.isImm())
780 break;
781 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
782 }
783
784 if (OpNo > MI->getNumOperands())
785 report("Missing operands in last group", MI);
786
787 // An optional MDNode follows the groups.
788 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
789 ++OpNo;
790
791 // All trailing operands must be implicit registers.
792 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
793 const MachineOperand &MO = MI->getOperand(OpNo);
794 if (!MO.isReg() || !MO.isImplicit())
795 report("Expected implicit register after groups", &MO, OpNo);
796 }
797}
798
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000799void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000800 const MCInstrDesc &MCID = MI->getDesc();
801 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000802 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000803 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000804 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000805 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000806
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000807 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000808 if (MI->isInlineAsm())
809 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000810
Dan Gohmandb9493c2009-10-07 17:36:00 +0000811 // Check the MachineMemOperands for basic consistency.
812 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
813 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000814 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000815 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000816 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000817 report("Missing mayStore flag", MI);
818 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000819
820 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000821 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000822 if (LiveInts) {
823 bool mapped = !LiveInts->isNotInMIMap(MI);
824 if (MI->isDebugValue()) {
825 if (mapped)
826 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000827 } else if (MI->isInsideBundle()) {
828 if (mapped)
829 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000830 } else {
831 if (!mapped)
832 report("Missing slot index", MI);
833 }
834 }
835
Andrew Trick924123a2011-09-21 02:20:46 +0000836 StringRef ErrorInfo;
837 if (!TII->verifyInstruction(MI, ErrorInfo))
838 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000839}
840
841void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000842MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000843 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000844 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +0000845 unsigned NumDefs = MCID.getNumDefs();
846 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
847 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000848
Evan Cheng6cc775f2011-06-28 19:10:37 +0000849 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +0000850 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000851 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000852 if (!MO->isReg())
853 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +0000854 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000855 report("Explicit definition marked as use", MO, MONum);
856 else if (MO->isImplicit())
857 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000858 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000859 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +0000860 // Don't check if it's the last operand in a variadic instruction. See,
861 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000862 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000863 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000864 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +0000865 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000866 if (MO->isImplicit())
867 report("Explicit operand marked as implicit", MO, MONum);
868 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000869
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000870 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
871 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000872 if (!MO->isReg())
873 report("Tied use must be a register", MO, MONum);
874 else if (!MO->isTied())
875 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000876 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
877 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000878 } else if (MO->isReg() && MO->isTied())
879 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000880 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +0000881 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000882 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000883 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000884 }
885
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000886 switch (MO->getType()) {
887 case MachineOperand::MO_Register: {
888 const unsigned Reg = MO->getReg();
889 if (!Reg)
890 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000891 if (MRI->tracksLiveness() && !MI->isDebugValue())
892 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000893
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000894 // Verify the consistency of tied operands.
895 if (MO->isTied()) {
896 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
897 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
898 if (!OtherMO.isReg())
899 report("Must be tied to a register", MO, MONum);
900 if (!OtherMO.isTied())
901 report("Missing tie flags on tied operand", MO, MONum);
902 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
903 report("Inconsistent tie links", MO, MONum);
904 if (MONum < MCID.getNumDefs()) {
905 if (OtherIdx < MCID.getNumOperands()) {
906 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
907 report("Explicit def tied to explicit use without tie constraint",
908 MO, MONum);
909 } else {
910 if (!OtherMO.isImplicit())
911 report("Explicit def should be tied to implicit use", MO, MONum);
912 }
913 }
914 }
915
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +0000916 // Verify two-address constraints after leaving SSA form.
917 unsigned DefIdx;
918 if (!MRI->isSSA() && MO->isUse() &&
919 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
920 Reg != MI->getOperand(DefIdx).getReg())
921 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000922
923 // Check register classes.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000924 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000925 unsigned SubIdx = MO->getSubReg();
926
927 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000928 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000929 report("Illegal subregister index for physical register", MO, MONum);
930 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000931 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000932 if (const TargetRegisterClass *DRC =
933 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000934 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000935 report("Illegal physical register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000936 errs() << TRI->getName(Reg) << " is not a "
Craig Toppercf0444b2014-11-17 05:50:14 +0000937 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000938 }
939 }
940 } else {
941 // Virtual register.
942 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
943 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000944 const TargetRegisterClass *SRC =
945 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +0000946 if (!SRC) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000947 report("Invalid subregister index for virtual register", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000948 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +0000949 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000950 return;
951 }
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000952 if (RC != SRC) {
953 report("Invalid register class for subregister index", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000954 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000955 << " does not fully support subreg index " << SubIdx << "\n";
956 return;
957 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000958 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000959 if (const TargetRegisterClass *DRC =
960 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000961 if (SubIdx) {
962 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +0000963 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000964 if (!SuperRC) {
965 report("No largest legal super class exists.", MO, MONum);
966 return;
967 }
968 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
969 if (!DRC) {
970 report("No matching super-reg register class.", MO, MONum);
971 return;
972 }
973 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +0000974 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000975 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000976 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +0000977 << " register, but got a " << TRI->getRegClassName(RC)
978 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000979 }
980 }
981 }
982 }
983 break;
984 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000985
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000986 case MachineOperand::MO_RegisterMask:
987 regMasks.push_back(MO->getRegMask());
988 break;
989
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000990 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +0000991 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
992 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000993 break;
994
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000995 case MachineOperand::MO_FrameIndex:
996 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
997 LiveInts && !LiveInts->isNotInMIMap(MI)) {
Jonas Paulsson72640f12015-10-29 08:28:35 +0000998 int FI = MO->getIndex();
999 LiveInterval &LI = LiveStks->getInterval(FI);
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001000 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001001
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001002 bool stores = MI->mayStore();
Jonas Paulsson72640f12015-10-29 08:28:35 +00001003 bool loads = MI->mayLoad();
1004 // For a memory-to-memory move, we need to check if the frame
1005 // index is used for storing or loading, by inspecting the
1006 // memory operands.
1007 if (stores && loads) {
1008 for (auto *MMO : MI->memoperands()) {
1009 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1010 if (PSV == nullptr) continue;
1011 const FixedStackPseudoSourceValue *Value =
1012 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1013 if (Value == nullptr) continue;
1014 if (Value->getFrameIndex() != FI) continue;
1015
1016 if (MMO->isStore())
1017 loads = false;
1018 else
1019 stores = false;
1020 break;
1021 }
1022 if (loads == stores)
1023 report("Missing fixed stack memoperand.", MI);
1024 }
1025 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001026 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001027 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001028 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001029 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001030 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001031 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001032 }
1033 }
1034 break;
1035
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001036 default:
1037 break;
1038 }
1039}
1040
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001041void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1042 const MachineInstr *MI = MO->getParent();
1043 const unsigned Reg = MO->getReg();
1044
1045 // Both use and def operands can read a register.
1046 if (MO->readsReg()) {
1047 regsLiveInButUnused.erase(Reg);
1048
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001049 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001050 addRegWithSubRegs(regsKilled, Reg);
1051
1052 // Check that LiveVars knows this kill.
1053 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1054 MO->isKill()) {
1055 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1056 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
1057 report("Kill missing from LiveVariables", MO, MONum);
1058 }
1059
1060 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001061 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
1062 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
1063 // Check the cached regunit intervals.
1064 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1065 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun34e1be92013-10-10 21:29:02 +00001066 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units)) {
1067 LiveQueryResult LRQ = LR->Query(UseIdx);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001068 if (!LRQ.valueIn()) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001069 report("No live segment at use", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001070 errs() << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
Matthias Braun34e1be92013-10-10 21:29:02 +00001071 << ' ' << *LR << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001072 }
1073 if (MO->isKill() && !LRQ.isKill()) {
1074 report("Live range continues after kill flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001075 errs() << PrintRegUnit(*Units, TRI) << ' ' << *LR << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001076 }
1077 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001078 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001079 }
1080
1081 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1082 if (LiveInts->hasInterval(Reg)) {
1083 // This is a virtual register interval.
1084 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun88dd0ab2013-10-10 21:28:52 +00001085 LiveQueryResult LRQ = LI.Query(UseIdx);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001086 if (!LRQ.valueIn()) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001087 report("No live segment at use", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001088 errs() << UseIdx << " is not live in " << LI << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001089 }
1090 // Check for extra kill flags.
1091 // Note that we allow missing kill flags for now.
1092 if (MO->isKill() && !LRQ.isKill()) {
1093 report("Live range continues after kill flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001094 errs() << "Live range: " << LI << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001095 }
1096 } else {
1097 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001098 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001099 }
1100 }
1101
1102 // Use of a dead register.
1103 if (!regsLive.count(Reg)) {
1104 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1105 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001106 bool Bad = !isReserved(Reg);
1107 // We are fine if just any subregister has a defined value.
1108 if (Bad) {
1109 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1110 ++SubRegs) {
1111 if (regsLive.count(*SubRegs)) {
1112 Bad = false;
1113 break;
1114 }
1115 }
1116 }
Matthias Braun96a31952015-01-14 22:25:14 +00001117 // If there is an additional implicit-use of a super register we stop
1118 // here. By definition we are fine if the super register is not
1119 // (completely) dead, if the complete super register is dead we will
1120 // get a report for its operand.
1121 if (Bad) {
1122 for (const MachineOperand &MOP : MI->uses()) {
1123 if (!MOP.isReg())
1124 continue;
1125 if (!MOP.isImplicit())
1126 continue;
1127 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1128 ++SubRegs) {
1129 if (*SubRegs == Reg) {
1130 Bad = false;
1131 break;
1132 }
1133 }
1134 }
1135 }
Matthias Braun96d77322014-12-10 01:13:13 +00001136 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001137 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001138 } else if (MRI->def_empty(Reg)) {
1139 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001140 } else {
1141 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1142 // We don't know which virtual registers are live in, so only complain
1143 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1144 // must be live in. PHI instructions are handled separately.
1145 if (MInfo.regsKilled.count(Reg))
1146 report("Using a killed virtual register", MO, MONum);
1147 else if (!MI->isPHI())
1148 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1149 }
1150 }
1151 }
1152
1153 if (MO->isDef()) {
1154 // Register defined.
1155 // TODO: verify that earlyclobber ops are not used.
1156 if (MO->isDead())
1157 addRegWithSubRegs(regsDead, Reg);
1158 else
1159 addRegWithSubRegs(regsDefined, Reg);
1160
1161 // Verify SSA form.
1162 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001163 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001164 report("Multiple virtual register defs in SSA form", MO, MONum);
1165
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001166 // Check LiveInts for a live segment, but only for virtual registers.
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001167 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1168 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001169 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1170 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001171 if (LiveInts->hasInterval(Reg)) {
1172 const LiveInterval &LI = LiveInts->getInterval(Reg);
1173 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1174 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001175 if (VNI->def != DefIdx) {
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001176 report("Inconsistent valno->def", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001177 errs() << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001178 << DefIdx << " in " << LI << '\n';
1179 }
1180 } else {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001181 report("No live segment at def", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001182 errs() << DefIdx << " is not live in " << LI << '\n';
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001183 }
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001184 // Check that, if the dead def flag is present, LiveInts agree.
1185 if (MO->isDead()) {
1186 LiveQueryResult LRQ = LI.Query(DefIdx);
1187 if (!LRQ.isDeadDef()) {
1188 report("Live range continues after dead def flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001189 errs() << "Live range: " << LI << '\n';
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001190 }
1191 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001192 } else {
1193 report("Virtual register has no Live interval", MO, MONum);
1194 }
1195 }
1196 }
1197}
1198
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001199void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001200}
1201
1202// This function gets called after visiting all instructions in a bundle. The
1203// argument points to the bundle header.
1204// Normal stand-alone instructions are also considered 'bundles', and this
1205// function is called for all of them.
1206void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001207 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1208 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001209 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001210 // Kill any masked registers.
1211 while (!regMasks.empty()) {
1212 const uint32_t *Mask = regMasks.pop_back_val();
1213 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1214 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1215 MachineOperand::clobbersPhysReg(Mask, *I))
1216 regsDead.push_back(*I);
1217 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001218 set_subtract(regsLive, regsDead); regsDead.clear();
1219 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001220}
1221
1222void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001223MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001224 MBBInfoMap[MBB].regsLiveOut = regsLive;
1225 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001226
1227 if (Indexes) {
1228 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1229 if (!(stop > lastIndex)) {
1230 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001231 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001232 << " last instruction was at " << lastIndex << '\n';
1233 }
1234 lastIndex = stop;
1235 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001236}
1237
1238// Calculate the largest possible vregsPassed sets. These are the registers that
1239// can pass through an MBB live, but may not be live every time. It is assumed
1240// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001241void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001242 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1243 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001244 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001245 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001246 BBInfo &MInfo = MBBInfoMap[&MBB];
1247 if (!MInfo.reachable)
1248 continue;
1249 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1250 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1251 BBInfo &SInfo = MBBInfoMap[*SuI];
1252 if (SInfo.addPassed(MInfo.regsLiveOut))
1253 todo.insert(*SuI);
1254 }
1255 }
1256
1257 // Iteratively push vregsPassed to successors. This will converge to the same
1258 // final state regardless of DenseSet iteration order.
1259 while (!todo.empty()) {
1260 const MachineBasicBlock *MBB = *todo.begin();
1261 todo.erase(MBB);
1262 BBInfo &MInfo = MBBInfoMap[MBB];
1263 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1264 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1265 if (*SuI == MBB)
1266 continue;
1267 BBInfo &SInfo = MBBInfoMap[*SuI];
1268 if (SInfo.addPassed(MInfo.vregsPassed))
1269 todo.insert(*SuI);
1270 }
1271 }
1272}
1273
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001274// Calculate the set of virtual registers that must be passed through each basic
1275// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001276// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001277void MachineVerifier::calcRegsRequired() {
1278 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001279 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001280 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001281 BBInfo &MInfo = MBBInfoMap[&MBB];
1282 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1283 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1284 BBInfo &PInfo = MBBInfoMap[*PrI];
1285 if (PInfo.addRequired(MInfo.vregsLiveIn))
1286 todo.insert(*PrI);
1287 }
1288 }
1289
1290 // Iteratively push vregsRequired to predecessors. This will converge to the
1291 // same final state regardless of DenseSet iteration order.
1292 while (!todo.empty()) {
1293 const MachineBasicBlock *MBB = *todo.begin();
1294 todo.erase(MBB);
1295 BBInfo &MInfo = MBBInfoMap[MBB];
1296 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1297 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1298 if (*PrI == MBB)
1299 continue;
1300 BBInfo &SInfo = MBBInfoMap[*PrI];
1301 if (SInfo.addRequired(MInfo.vregsRequired))
1302 todo.insert(*PrI);
1303 }
1304 }
1305}
1306
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001307// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001308// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001309void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001310 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001311 for (const auto &BBI : *MBB) {
1312 if (!BBI.isPHI())
1313 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001314 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001315
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001316 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1317 unsigned Reg = BBI.getOperand(i).getReg();
1318 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001319 if (!Pre->isSuccessor(MBB))
1320 continue;
1321 seen.insert(Pre);
1322 BBInfo &PrInfo = MBBInfoMap[Pre];
1323 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1324 report("PHI operand is not live-out from predecessor",
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001325 &BBI.getOperand(i), i);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001326 }
1327
1328 // Did we see all predecessors?
1329 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1330 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1331 if (!seen.count(*PrI)) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001332 report("Missing PHI operand", &BBI);
Owen Anderson21b17882015-02-04 00:02:59 +00001333 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001334 << " is a predecessor according to the CFG.\n";
1335 }
1336 }
1337 }
1338}
1339
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001340void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001341 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001342
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001343 for (const auto &MBB : *MF) {
1344 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001345
1346 // Skip unreachable MBBs.
1347 if (!MInfo.reachable)
1348 continue;
1349
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001350 checkPHIOps(&MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001351 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001352
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001353 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001354 calcRegsRequired();
1355
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001356 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001357 for (const auto &MBB : *MF) {
1358 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001359 for (RegSet::iterator
1360 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1361 ++I)
1362 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001363 report("Virtual register killed in block, but needed live out.", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001364 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001365 << " is used after the block.\n";
1366 }
1367 }
1368
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001369 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001370 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1371 for (RegSet::iterator
1372 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesen99014ff2012-03-10 00:44:11 +00001373 ++I)
1374 report("Virtual register def doesn't dominate all uses.",
1375 MRI->getVRegDef(*I));
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001376 }
1377
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001378 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001379 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001380 if (LiveInts)
1381 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001382}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001383
1384void MachineVerifier::verifyLiveVariables() {
1385 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001386 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1387 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001388 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001389 for (const auto &MBB : *MF) {
1390 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001391
1392 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1393 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001394 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1395 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001396 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001397 << " must be live through the block.\n";
1398 }
1399 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001400 if (VI.AliveBlocks.test(MBB.getNumber())) {
1401 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001402 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001403 << " is not needed live through the block.\n";
1404 }
1405 }
1406 }
1407 }
1408}
1409
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001410void MachineVerifier::verifyLiveIntervals() {
1411 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001412 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1413 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001414
1415 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001416 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001417 continue;
1418
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001419 if (!LiveInts->hasInterval(Reg)) {
1420 report("Missing live interval for virtual register", MF);
Owen Anderson21b17882015-02-04 00:02:59 +00001421 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001422 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001423 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001424
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001425 const LiveInterval &LI = LiveInts->getInterval(Reg);
1426 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001427 verifyLiveInterval(LI);
1428 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001429
1430 // Verify all the cached regunit intervals.
1431 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001432 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1433 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001434}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001435
Matthias Braun364e6e92013-10-10 21:28:54 +00001436void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001437 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001438 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001439 if (VNI->isUnused())
1440 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001441
Matthias Braun364e6e92013-10-10 21:28:54 +00001442 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001443
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001444 if (!DefVNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001445 report("Valno not live at def and not marked unused", MF, LR, Reg,
1446 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001447 errs() << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001448 return;
1449 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001450
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001451 if (DefVNI != VNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001452 report("Live segment at def has different valno", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001453 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +00001454 << " where valno #" << DefVNI->id << " is live\n";
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001455 return;
1456 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001457
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001458 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1459 if (!MBB) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001460 report("Invalid definition index", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001461 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Matthias Braun364e6e92013-10-10 21:28:54 +00001462 << " in " << LR << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001463 return;
1464 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001465
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001466 if (VNI->isPHIDef()) {
1467 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001468 report("PHIDef value is not defined at MBB start", MBB, LR, Reg,
1469 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001470 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +00001471 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001472 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001473 return;
1474 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001475
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001476 // Non-PHI def.
1477 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1478 if (!MI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001479 report("No instruction at def index", MBB, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001480 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001481 return;
1482 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001483
Matthias Braun364e6e92013-10-10 21:28:54 +00001484 if (Reg != 0) {
1485 bool hasDef = false;
1486 bool isEarlyClobber = false;
1487 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1488 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001489 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001490 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1491 if (MOI->getReg() != Reg)
1492 continue;
1493 } else {
1494 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1495 !TRI->hasRegUnit(MOI->getReg(), Reg))
1496 continue;
1497 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001498 if (LaneMask != 0 &&
1499 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask) == 0)
1500 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001501 hasDef = true;
1502 if (MOI->isEarlyClobber())
1503 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001504 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001505
Matthias Braun364e6e92013-10-10 21:28:54 +00001506 if (!hasDef) {
1507 report("Defining instruction does not modify register", MI);
Owen Anderson21b17882015-02-04 00:02:59 +00001508 errs() << "Valno #" << VNI->id << " in " << LR << '\n';
Matthias Braun364e6e92013-10-10 21:28:54 +00001509 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001510
Matthias Braun364e6e92013-10-10 21:28:54 +00001511 // Early clobber defs begin at USE slots, but other defs must begin at
1512 // DEF slots.
1513 if (isEarlyClobber) {
1514 if (!VNI->def.isEarlyClobber()) {
Matthias Braun47760d92014-11-19 19:46:13 +00001515 report("Early clobber def must be at an early-clobber slot", MBB, LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001516 Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001517 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Matthias Braun364e6e92013-10-10 21:28:54 +00001518 }
1519 } else if (!VNI->def.isRegister()) {
1520 report("Non-PHI, non-early clobber def must be at a register slot",
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001521 MBB, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001522 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001523 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001524 }
1525}
1526
Matthias Braun364e6e92013-10-10 21:28:54 +00001527void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1528 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001529 unsigned Reg, LaneBitmask LaneMask)
1530{
Matthias Braun364e6e92013-10-10 21:28:54 +00001531 const LiveRange::Segment &S = *I;
1532 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001533 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001534
Matthias Braun364e6e92013-10-10 21:28:54 +00001535 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001536 report("Foreign valno in live segment", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001537 errs() << S << " has a bad valno\n";
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001538 }
1539
1540 if (VNI->isUnused()) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001541 report("Live segment valno is marked unused", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001542 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001543 }
1544
Matthias Braun364e6e92013-10-10 21:28:54 +00001545 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001546 if (!MBB) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001547 report("Bad start of live segment, no basic block", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001548 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001549 return;
1550 }
1551 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001552 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001553 report("Live segment must begin at MBB entry or valno def", MBB, LR, Reg,
1554 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001555 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001556 }
1557
1558 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001559 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001560 if (!EndMBB) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001561 report("Bad end of live segment, no basic block", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001562 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001563 return;
1564 }
1565
1566 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001567 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001568 return;
1569
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001570 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001571 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1572 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001573 return;
1574
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001575 // The live segment is ending inside EndMBB
1576 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001577 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001578 if (!MI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001579 report("Live segment doesn't end at a valid instruction", EndMBB, LR, Reg,
1580 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001581 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001582 return;
1583 }
1584
1585 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001586 if (S.end.isBlock()) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001587 report("Live segment ends at B slot of an instruction", EndMBB, LR, Reg,
1588 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001589 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001590 }
1591
Matthias Braun364e6e92013-10-10 21:28:54 +00001592 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001593 // Segment ends on the dead slot.
1594 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001595 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun47760d92014-11-19 19:46:13 +00001596 report("Live segment ending at dead slot spans instructions", EndMBB, LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001597 Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001598 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001599 }
1600 }
1601
1602 // A live segment can only end at an early-clobber slot if it is being
1603 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001604 if (S.end.isEarlyClobber()) {
1605 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001606 report("Live segment ending at early clobber slot must be "
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001607 "redefined by an EC def in the same instruction", EndMBB, LR, Reg,
1608 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001609 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001610 }
1611 }
1612
1613 // The following checks only apply to virtual registers. Physreg liveness
1614 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001615 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001616 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001617 // use, or a dead flag on a def.
1618 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001619 bool hasSubRegDef = false;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001620 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001621 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001622 continue;
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001623 if (LaneMask != 0 &&
1624 (LaneMask & TRI->getSubRegIndexLaneMask(MOI->getSubReg())) == 0)
1625 continue;
Matthias Braun21554d92014-12-10 01:13:11 +00001626 if (MOI->isDef() && MOI->getSubReg() != 0)
1627 hasSubRegDef = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001628 if (MOI->readsReg())
1629 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001630 }
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001631 if (!S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001632 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001633 // When tracking subregister liveness, the main range must start new
1634 // values on partial register writes, even if there is no read.
Matthias Brauna25e13a2015-03-19 00:21:58 +00001635 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask != 0 ||
1636 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001637 report("Instruction ending live segment doesn't read the register",
1638 MI);
Owen Anderson21b17882015-02-04 00:02:59 +00001639 errs() << S << " in " << LR << '\n';
Matthias Braun21554d92014-12-10 01:13:11 +00001640 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001641 }
1642 }
1643 }
1644
1645 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001646 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001647 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00001648 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001649 // Not live-in to any blocks.
1650 if (MBB == EndMBB)
1651 return;
1652 // Skip this block.
1653 ++MFI;
1654 }
1655 for (;;) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001656 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001657 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00001658 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00001659 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001660 if (&*MFI == EndMBB)
1661 break;
1662 ++MFI;
1663 continue;
1664 }
1665
1666 // Is VNI a PHI-def in the current block?
1667 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001668 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001669
1670 // Check that VNI is live-out of all predecessors.
1671 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1672 PE = MFI->pred_end(); PI != PE; ++PI) {
1673 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001674 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001675
1676 // All predecessors must have a live-out value.
1677 if (!PVNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001678 report("Register not marked live out of predecessor", *PI, LR, Reg,
1679 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001680 errs() << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001681 << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
1682 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001683 continue;
1684 }
1685
1686 // Only PHI-defs can take different predecessor values.
1687 if (!IsPHI && PVNI != VNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001688 report("Different value live out of predecessor", *PI, LR, Reg,
1689 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001690 errs() << "Valno #" << PVNI->id << " live out of BB#"
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001691 << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
1692 << " live into BB#" << MFI->getNumber() << '@'
1693 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001694 }
1695 }
1696 if (&*MFI == EndMBB)
1697 break;
1698 ++MFI;
1699 }
1700}
1701
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001702void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001703 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00001704 for (const VNInfo *VNI : LR.valnos)
1705 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001706
Matthias Braun364e6e92013-10-10 21:28:54 +00001707 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001708 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00001709}
1710
1711void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001712 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00001713 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1714 verifyLiveRange(LI, Reg);
1715
Matthias Braune6a24852015-09-25 21:51:14 +00001716 LaneBitmask Mask = 0;
1717 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00001718 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1719 if ((Mask & SR.LaneMask) != 0)
1720 report("Lane masks of sub ranges overlap in live interval", MF, LI);
1721 if ((SR.LaneMask & ~MaxMask) != 0)
1722 report("Subrange lanemask is invalid", MF, LI);
Matthias Braun0d4cebd2015-07-16 18:55:35 +00001723 if (SR.empty())
1724 report("Subrange must not be empty", MF, SR, LI.reg, SR.LaneMask);
Matthias Braune962e522015-03-25 21:18:22 +00001725 Mask |= SR.LaneMask;
1726 verifyLiveRange(SR, LI.reg, SR.LaneMask);
1727 if (!LI.covers(SR))
1728 report("A Subrange is not covered by the main range", MF, LI);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001729 }
1730
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001731 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00001732 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1733 unsigned NumComp = ConEQ.Classify(&LI);
1734 if (NumComp > 1) {
1735 report("Multiple connected components in live interval", MF, LI);
1736 for (unsigned comp = 0; comp != NumComp; ++comp) {
1737 errs() << comp << ": valnos";
1738 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1739 E = LI.vni_end(); I!=E; ++I)
1740 if (comp == ConEQ.getEqClass(*I))
1741 errs() << ' ' << (*I)->id;
1742 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00001743 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001744 }
1745}
Manman Renaa6875b2013-07-15 21:26:31 +00001746
1747namespace {
1748 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1749 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1750 // value is zero.
1751 // We use a bool plus an integer to capture the stack state.
1752 struct StackStateOfBB {
1753 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1754 ExitIsSetup(false) { }
1755 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1756 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1757 ExitIsSetup(ExitSetup) { }
1758 // Can be negative, which means we are setting up a frame.
1759 int EntryValue;
1760 int ExitValue;
1761 bool EntryIsSetup;
1762 bool ExitIsSetup;
1763 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001764}
Manman Renaa6875b2013-07-15 21:26:31 +00001765
1766/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1767/// by a FrameDestroy <n>, stack adjustments are identical on all
1768/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1769void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001770 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1771 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Manman Renaa6875b2013-07-15 21:26:31 +00001772
1773 SmallVector<StackStateOfBB, 8> SPState;
1774 SPState.resize(MF->getNumBlockIDs());
1775 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1776
1777 // Visit the MBBs in DFS order.
1778 for (df_ext_iterator<const MachineFunction*,
1779 SmallPtrSet<const MachineBasicBlock*, 8> >
1780 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1781 DFI != DFE; ++DFI) {
1782 const MachineBasicBlock *MBB = *DFI;
1783
1784 StackStateOfBB BBState;
1785 // Check the exit state of the DFS stack predecessor.
1786 if (DFI.getPathLength() >= 2) {
1787 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1788 assert(Reachable.count(StackPred) &&
1789 "DFS stack predecessor is already visited.\n");
1790 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1791 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1792 BBState.ExitValue = BBState.EntryValue;
1793 BBState.ExitIsSetup = BBState.EntryIsSetup;
1794 }
1795
1796 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001797 for (const auto &I : *MBB) {
1798 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001799 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001800 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001801 assert(Size >= 0 &&
1802 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1803
1804 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001805 report("FrameSetup is after another FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001806 BBState.ExitValue -= Size;
1807 BBState.ExitIsSetup = true;
1808 }
1809
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001810 if (I.getOpcode() == FrameDestroyOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001811 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001812 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001813 assert(Size >= 0 &&
1814 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1815
1816 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001817 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001818 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1819 BBState.ExitValue;
1820 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001821 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00001822 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00001823 << AbsSPAdj << ">.\n";
1824 }
1825 BBState.ExitValue += Size;
1826 BBState.ExitIsSetup = false;
1827 }
1828 }
1829 SPState[MBB->getNumber()] = BBState;
1830
1831 // Make sure the exit state of any predecessor is consistent with the entry
1832 // state.
1833 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
1834 E = MBB->pred_end(); I != E; ++I) {
1835 if (Reachable.count(*I) &&
1836 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
1837 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
1838 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001839 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00001840 << SPState[(*I)->getNumber()].ExitValue << ", "
1841 << SPState[(*I)->getNumber()].ExitIsSetup
1842 << "), while BB#" << MBB->getNumber() << " has entry state ("
1843 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
1844 }
1845 }
1846
1847 // Make sure the entry state of any successor is consistent with the exit
1848 // state.
1849 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
1850 E = MBB->succ_end(); I != E; ++I) {
1851 if (Reachable.count(*I) &&
1852 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
1853 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
1854 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001855 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00001856 << SPState[(*I)->getNumber()].EntryValue << ", "
1857 << SPState[(*I)->getNumber()].EntryIsSetup
1858 << "), while BB#" << MBB->getNumber() << " has exit state ("
1859 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
1860 }
1861 }
1862
1863 // Make sure a basic block with return ends with zero stack adjustment.
1864 if (!MBB->empty() && MBB->back().isReturn()) {
1865 if (BBState.ExitIsSetup)
1866 report("A return block ends with a FrameSetup.", MBB);
1867 if (BBState.ExitValue)
1868 report("A return block ends with a nonzero stack adjustment.", MBB);
1869 }
1870 }
1871}