blob: 709437801f98302ba0080c2770fa54dd36595c6c [file] [log] [blame]
Bill Wendling68caaaf2010-08-19 18:52:17 +00001//===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Pass to verify generated machine code. The following is checked:
11//
12// Operand counts: All explicit operands must be present.
13//
14// Register classes: All physical and virtual register operands must be
15// compatible with the register class required by the instruction descriptor.
16//
17// Register live intervals: Registers must be defined only once, and must be
18// defined before use.
19//
20// The machine code verifier is enabled from LLVMTargetMachine.cpp with the
21// command-line option -verify-machineinstrs, or by defining the environment
22// variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
23// the verifier errors.
24//===----------------------------------------------------------------------===//
25
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000026#include "llvm/CodeGen/Passes.h"
Chris Lattner565449d2009-08-23 03:13:20 +000027#include "llvm/ADT/DenseSet.h"
Manman Renaa6875b2013-07-15 21:26:31 +000028#include "llvm/ADT/DepthFirstIterator.h"
Chris Lattner565449d2009-08-23 03:13:20 +000029#include "llvm/ADT/SetOperations.h"
30#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/CodeGen/LiveIntervalAnalysis.h"
32#include "llvm/CodeGen/LiveStackAnalysis.h"
33#include "llvm/CodeGen/LiveVariables.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/CodeGen/MachineMemOperand.h"
37#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000038#include "llvm/IR/BasicBlock.h"
39#include "llvm/IR/InlineAsm.h"
40#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000042#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000043#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000044#include "llvm/Support/FileSystem.h"
Matthias Braun3f1d8fd2014-12-10 01:12:10 +000045#include "llvm/Support/Format.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
207 void report(const char *msg, const MachineFunction *MF);
208 void report(const char *msg, const MachineBasicBlock *MBB);
209 void report(const char *msg, const MachineInstr *MI);
210 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000211 void report(const char *msg, const MachineFunction *MF,
212 const LiveInterval &LI);
213 void report(const char *msg, const MachineBasicBlock *MBB,
214 const LiveInterval &LI);
Matthias Braun364e6e92013-10-10 21:28:54 +0000215 void report(const char *msg, const MachineFunction *MF,
Matthias Braune6a24852015-09-25 21:51:14 +0000216 const LiveRange &LR, unsigned Reg, LaneBitmask LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000217 void report(const char *msg, const MachineBasicBlock *MBB,
Matthias Braune6a24852015-09-25 21:51:14 +0000218 const LiveRange &LR, unsigned Reg, LaneBitmask LaneMask);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000219
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000220 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000221
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000222 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000223 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000224 void calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000225 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000226
227 void calcRegsRequired();
228 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000229 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000230 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000231 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
232 unsigned);
Matthias Braun364e6e92013-10-10 21:28:54 +0000233 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000234 const LiveRange::const_iterator I, unsigned,
235 unsigned);
Matthias Braune6a24852015-09-25 21:51:14 +0000236 void verifyLiveRange(const LiveRange&, unsigned, LaneBitmask LaneMask = 0);
Manman Renaa6875b2013-07-15 21:26:31 +0000237
238 void verifyStackFrame();
Matthias Braun80595462015-09-09 17:49:46 +0000239
240 void verifySlotIndexes() const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000241 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000242
243 struct MachineVerifierPass : public MachineFunctionPass {
244 static char ID; // Pass ID, replacement for typeid
Matthias Brauna4e932d2014-12-11 19:41:51 +0000245 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000246
Matthias Brauna4e932d2014-12-11 19:41:51 +0000247 MachineVerifierPass(const std::string &banner = nullptr)
248 : MachineFunctionPass(ID), Banner(banner) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000249 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
250 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000251
Craig Topper4584cd52014-03-07 09:26:03 +0000252 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000253 AU.setPreservesAll();
254 MachineFunctionPass::getAnalysisUsage(AU);
255 }
256
Craig Topper4584cd52014-03-07 09:26:03 +0000257 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Brauna4e932d2014-12-11 19:41:51 +0000258 MF.verify(this, Banner.c_str());
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000259 return false;
260 }
261 };
262
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000263}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000264
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000265char MachineVerifierPass::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +0000266INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000267 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000268
Matthias Brauna4e932d2014-12-11 19:41:51 +0000269FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000270 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000271}
272
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000273void MachineFunction::verify(Pass *p, const char *Banner) const {
274 MachineVerifier(p, Banner)
275 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesen27440e72009-11-13 21:56:09 +0000276}
277
Matthias Braun80595462015-09-09 17:49:46 +0000278void MachineVerifier::verifySlotIndexes() const {
279 if (Indexes == nullptr)
280 return;
281
282 // Ensure the IdxMBB list is sorted by slot indexes.
283 SlotIndex Last;
284 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
285 E = Indexes->MBBIndexEnd(); I != E; ++I) {
286 assert(!Last.isValid() || I->first > Last);
287 Last = I->first;
288 }
289}
290
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000291bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000292 foundErrors = 0;
293
294 this->MF = &MF;
295 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000296 TII = MF.getSubtarget().getInstrInfo();
297 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000298 MRI = &MF.getRegInfo();
299
Craig Topperc0196b12014-04-14 00:51:57 +0000300 LiveVars = nullptr;
301 LiveInts = nullptr;
302 LiveStks = nullptr;
303 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000304 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000305 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000306 // We don't want to verify LiveVariables if LiveIntervals is available.
307 if (!LiveInts)
308 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000309 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000310 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000311 }
312
Matthias Braun80595462015-09-09 17:49:46 +0000313 verifySlotIndexes();
314
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000315 visitMachineFunctionBefore();
316 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
317 MFI!=MFE; ++MFI) {
318 visitMachineBasicBlockBefore(MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000319 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000320 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000321 // Do we expect the next instruction to be part of the same bundle?
322 bool InBundle = false;
323
Evan Cheng7fae11b2011-12-14 02:11:42 +0000324 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
325 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000326 if (MBBI->getParent() != MFI) {
327 report("Bad instruction parent pointer", MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000328 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000329 continue;
330 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000331
332 // Check for consistent bundle flags.
333 if (InBundle && !MBBI->isBundledWithPred())
334 report("Missing BundledPred flag, "
335 "BundledSucc was set on predecessor", MBBI);
336 if (!InBundle && MBBI->isBundledWithPred())
337 report("BundledPred flag is set, "
338 "but BundledSucc not set on predecessor", MBBI);
339
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000340 // Is this a bundle header?
341 if (!MBBI->isInsideBundle()) {
342 if (CurBundle)
343 visitMachineBundleAfter(CurBundle);
344 CurBundle = MBBI;
345 visitMachineBundleBefore(CurBundle);
346 } else if (!CurBundle)
347 report("No bundle header", MBBI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000348 visitMachineInstrBefore(MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000349 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
350 const MachineInstr &MI = *MBBI;
351 const MachineOperand &Op = MI.getOperand(I);
352 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000353 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000354 // functions when replacing operands of a MachineInstr.
355 report("Instruction has operand with wrong parent set", &MI);
356 }
357
358 visitMachineOperand(&Op, I);
359 }
360
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000361 visitMachineInstrAfter(MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000362
363 // Was this the last bundled instruction?
364 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000365 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000366 if (CurBundle)
367 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000368 if (InBundle)
369 report("BundledSucc flag set on last instruction in block", &MFI->back());
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000370 visitMachineBasicBlockAfter(MFI);
371 }
372 visitMachineFunctionAfter();
373
Owen Anderson21b17882015-02-04 00:02:59 +0000374 if (foundErrors)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000375 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000376
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000377 // Clean up.
378 regsLive.clear();
379 regsDefined.clear();
380 regsDead.clear();
381 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000382 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000383 regsLiveInButUnused.clear();
384 MBBInfoMap.clear();
385
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000386 return false; // no changes
387}
388
Chris Lattner75f40452009-08-23 01:03:30 +0000389void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000390 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000391 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000392 if (!foundErrors++) {
393 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000394 errs() << "# " << Banner << '\n';
395 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000396 }
Owen Anderson21b17882015-02-04 00:02:59 +0000397 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000398 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000399}
400
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000401void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000402 assert(MBB);
403 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000404 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000405 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000406 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000407 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000408 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000409 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000410 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000411}
412
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000413void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000414 assert(MI);
415 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000416 errs() << "- instruction: ";
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000417 if (Indexes && Indexes->hasIndex(MI))
Owen Anderson21b17882015-02-04 00:02:59 +0000418 errs() << Indexes->getInstructionIndex(MI) << '\t';
419 MI->print(errs(), TM);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000420}
421
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000422void MachineVerifier::report(const char *msg,
423 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000424 assert(MO);
425 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000426 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000427 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000428 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000429}
430
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000431void MachineVerifier::report(const char *msg, const MachineFunction *MF,
432 const LiveInterval &LI) {
433 report(msg, MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000434 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000435}
436
437void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
438 const LiveInterval &LI) {
439 report(msg, MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000440 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000441}
442
Matthias Braun364e6e92013-10-10 21:28:54 +0000443void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000444 const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +0000445 LaneBitmask LaneMask) {
Matthias Braun364e6e92013-10-10 21:28:54 +0000446 report(msg, MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000447 errs() << "- liverange: " << LR << '\n';
448 errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000449 if (LaneMask != 0)
Owen Anderson21b17882015-02-04 00:02:59 +0000450 errs() << "- lanemask: " << format("%04X\n", LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000451}
452
453void MachineVerifier::report(const char *msg, const MachineFunction *MF,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000454 const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +0000455 LaneBitmask LaneMask) {
Matthias Braun364e6e92013-10-10 21:28:54 +0000456 report(msg, MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000457 errs() << "- liverange: " << LR << '\n';
458 errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000459 if (LaneMask != 0)
Owen Anderson21b17882015-02-04 00:02:59 +0000460 errs() << "- lanemask: " << format("%04X\n", LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000461}
462
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000463void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000464 BBInfo &MInfo = MBBInfoMap[MBB];
465 if (!MInfo.reachable) {
466 MInfo.reachable = true;
467 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
468 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
469 markReachable(*SuI);
470 }
471}
472
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000473void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000474 lastIndex = SlotIndex();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000475 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000476
477 // A sub-register of a reserved register is also reserved
478 for (int Reg = regsReserved.find_first(); Reg>=0;
479 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000480 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000481 // FIXME: This should probably be:
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000482 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
483 regsReserved.set(*SubRegs);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000484 }
485 }
Lang Hames1ce837a2012-02-14 19:17:48 +0000486
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000487 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000488
489 // Build a set of the basic blocks in the function.
490 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000491 for (const auto &MBB : *MF) {
492 FunctionBlocks.insert(&MBB);
493 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000494
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000495 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
496 if (MInfo.Preds.size() != MBB.pred_size())
497 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000498
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000499 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
500 if (MInfo.Succs.size() != MBB.succ_size())
501 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000502 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000503
504 // Check that the register use lists are sane.
505 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000506
507 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000508}
509
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000510// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000511static bool matchPair(MachineBasicBlock::const_succ_iterator i,
512 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000513 if (*i == a)
514 return *++i == b;
515 if (*i == b)
516 return *++i == a;
517 return false;
518}
519
520void
521MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000522 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000523
Lang Hames1ce837a2012-02-14 19:17:48 +0000524 if (MRI->isSSA()) {
525 // If this block has allocatable physical registers live-in, check that
526 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000527 for (const auto &LI : MBB->liveins()) {
528 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Lang Hames1ce837a2012-02-14 19:17:48 +0000529 MBB != MBB->getParent()->begin()) {
530 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
531 }
532 }
533 }
534
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000535 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000536 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000537 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000538 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000539 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000540 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000541 if (!FunctionBlocks.count(*I))
542 report("MBB has successor that isn't part of the function.", MBB);
543 if (!MBBInfoMap[*I].Preds.count(MBB)) {
544 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000545 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000546 << (*I)->getNumber() << ".\n";
547 }
548 }
549
550 // Check the predecessor list.
551 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
552 E = MBB->pred_end(); I != E; ++I) {
553 if (!FunctionBlocks.count(*I))
554 report("MBB has predecessor that isn't part of the function.", MBB);
555 if (!MBBInfoMap[*I].Succs.count(MBB)) {
556 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000557 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000558 << (*I)->getNumber() << ".\n";
559 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000560 }
Bill Wendling2a401312011-05-04 22:54:05 +0000561
562 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
563 const BasicBlock *BB = MBB->getBasicBlock();
564 if (LandingPadSuccs.size() > 1 &&
565 !(AsmInfo &&
566 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
567 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000568 report("MBB has more than one landing pad successor", MBB);
569
Dan Gohman352a4952009-08-27 02:43:49 +0000570 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000571 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000572 SmallVector<MachineOperand, 4> Cond;
573 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
574 TBB, FBB, Cond)) {
575 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
576 // check whether its answers match up with reality.
577 if (!TBB && !FBB) {
578 // Block falls through to its successor.
579 MachineFunction::const_iterator MBBI = MBB;
580 ++MBBI;
581 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000582 // It's possible that the block legitimately ends with a noreturn
583 // call or an unreachable, in which case it won't actually fall
584 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000585 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000586 // It's possible that the block legitimately ends with a noreturn
587 // call or an unreachable, in which case it won't actuall fall
588 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000589 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000590 report("MBB exits via unconditional fall-through but doesn't have "
591 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000592 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000593 report("MBB exits via unconditional fall-through but its successor "
594 "differs from its CFG successor!", MBB);
595 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000596 if (!MBB->empty() && MBB->back().isBarrier() &&
597 !TII->isPredicated(&MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000598 report("MBB exits via unconditional fall-through but ends with a "
599 "barrier instruction!", MBB);
600 }
601 if (!Cond.empty()) {
602 report("MBB exits via unconditional fall-through but has a condition!",
603 MBB);
604 }
605 } else if (TBB && !FBB && Cond.empty()) {
606 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000607 // If the block has exactly one successor, that happens to be a
608 // landingpad, accept it as valid control flow.
609 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
610 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
611 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000612 report("MBB exits via unconditional branch but doesn't have "
613 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000614 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000615 report("MBB exits via unconditional branch but the CFG "
616 "successor doesn't match the actual successor!", MBB);
617 }
618 if (MBB->empty()) {
619 report("MBB exits via unconditional branch but doesn't contain "
620 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000621 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000622 report("MBB exits via unconditional branch but doesn't end with a "
623 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000624 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000625 report("MBB exits via unconditional branch but the branch isn't a "
626 "terminator instruction!", MBB);
627 }
628 } else if (TBB && !FBB && !Cond.empty()) {
629 // Block conditionally branches somewhere, otherwise falls through.
630 MachineFunction::const_iterator MBBI = MBB;
631 ++MBBI;
632 if (MBBI == MF->end()) {
633 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000634 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000635 // A conditional branch with only one successor is weird, but allowed.
636 if (&*MBBI != TBB)
637 report("MBB exits via conditional branch/fall-through but only has "
638 "one CFG successor!", MBB);
639 else if (TBB != *MBB->succ_begin())
640 report("MBB exits via conditional branch/fall-through but the CFG "
641 "successor don't match the actual successor!", MBB);
642 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000643 report("MBB exits via conditional branch/fall-through but doesn't have "
644 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000645 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000646 report("MBB exits via conditional branch/fall-through but the CFG "
647 "successors don't match the actual successors!", MBB);
648 }
649 if (MBB->empty()) {
650 report("MBB exits via conditional branch/fall-through but doesn't "
651 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000652 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000653 report("MBB exits via conditional branch/fall-through but ends with a "
654 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000655 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000656 report("MBB exits via conditional branch/fall-through but the branch "
657 "isn't a terminator instruction!", MBB);
658 }
659 } else if (TBB && FBB) {
660 // Block conditionally branches somewhere, otherwise branches
661 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000662 if (MBB->succ_size() == 1) {
663 // A conditional branch with only one successor is weird, but allowed.
664 if (FBB != TBB)
665 report("MBB exits via conditional branch/branch through but only has "
666 "one CFG successor!", MBB);
667 else if (TBB != *MBB->succ_begin())
668 report("MBB exits via conditional branch/branch through but the CFG "
669 "successor don't match the actual successor!", MBB);
670 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000671 report("MBB exits via conditional branch/branch but doesn't have "
672 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000673 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000674 report("MBB exits via conditional branch/branch but the CFG "
675 "successors don't match the actual successors!", MBB);
676 }
677 if (MBB->empty()) {
678 report("MBB exits via conditional branch/branch but doesn't "
679 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000680 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000681 report("MBB exits via conditional branch/branch but doesn't end with a "
682 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000683 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000684 report("MBB exits via conditional branch/branch but the branch "
685 "isn't a terminator instruction!", MBB);
686 }
687 if (Cond.empty()) {
688 report("MBB exits via conditinal branch/branch but there's no "
689 "condition!", MBB);
690 }
691 } else {
692 report("AnalyzeBranch returned invalid data!", MBB);
693 }
694 }
695
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000696 regsLive.clear();
Matthias Braund9da1622015-09-09 18:08:03 +0000697 for (const auto &LI : MBB->liveins()) {
698 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000699 report("MBB live-in list contains non-physical register", MBB);
700 continue;
701 }
Matthias Braund9da1622015-09-09 18:08:03 +0000702 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
Chad Rosierabdb1d62013-05-22 23:17:36 +0000703 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000704 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000705 }
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +0000706 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000707
708 const MachineFrameInfo *MFI = MF->getFrameInfo();
709 assert(MFI && "Function has no frame info");
Matthias Braun111f5d82015-05-28 23:20:35 +0000710 BitVector PR = MFI->getPristineRegs(*MF);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000711 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000712 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
713 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000714 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000715 }
716
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000717 regsKilled.clear();
718 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000719
720 if (Indexes)
721 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000722}
723
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000724// This function gets called for all bundle headers, including normal
725// stand-alone unbundled instructions.
726void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
727 if (Indexes && Indexes->hasIndex(MI)) {
728 SlotIndex idx = Indexes->getInstructionIndex(MI);
729 if (!(idx > lastIndex)) {
730 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000731 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000732 }
733 lastIndex = idx;
734 }
Pete Coopercd720162012-06-07 17:41:39 +0000735
736 // Ensure non-terminators don't follow terminators.
737 // Ignore predicated terminators formed by if conversion.
738 // FIXME: If conversion shouldn't need to violate this rule.
739 if (MI->isTerminator() && !TII->isPredicated(MI)) {
740 if (!FirstTerminator)
741 FirstTerminator = MI;
742 } else if (FirstTerminator) {
743 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000744 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000745 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000746}
747
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000748// The operands on an INLINEASM instruction must follow a template.
749// Verify that the flag operands make sense.
750void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
751 // The first two operands on INLINEASM are the asm string and global flags.
752 if (MI->getNumOperands() < 2) {
753 report("Too few operands on inline asm", MI);
754 return;
755 }
756 if (!MI->getOperand(0).isSymbol())
757 report("Asm string must be an external symbol", MI);
758 if (!MI->getOperand(1).isImm())
759 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000760 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
761 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
762 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000763 report("Unknown asm flags", &MI->getOperand(1), 1);
764
Gabor Horvathfee04342015-03-16 09:53:42 +0000765 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000766
767 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
768 unsigned NumOps;
769 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
770 const MachineOperand &MO = MI->getOperand(OpNo);
771 // There may be implicit ops after the fixed operands.
772 if (!MO.isImm())
773 break;
774 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
775 }
776
777 if (OpNo > MI->getNumOperands())
778 report("Missing operands in last group", MI);
779
780 // An optional MDNode follows the groups.
781 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
782 ++OpNo;
783
784 // All trailing operands must be implicit registers.
785 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
786 const MachineOperand &MO = MI->getOperand(OpNo);
787 if (!MO.isReg() || !MO.isImplicit())
788 report("Expected implicit register after groups", &MO, OpNo);
789 }
790}
791
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000792void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000793 const MCInstrDesc &MCID = MI->getDesc();
794 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000795 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000796 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000797 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000798 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000799
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000800 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000801 if (MI->isInlineAsm())
802 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000803
Dan Gohmandb9493c2009-10-07 17:36:00 +0000804 // Check the MachineMemOperands for basic consistency.
805 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
806 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000807 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000808 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000809 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000810 report("Missing mayStore flag", MI);
811 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000812
813 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000814 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000815 if (LiveInts) {
816 bool mapped = !LiveInts->isNotInMIMap(MI);
817 if (MI->isDebugValue()) {
818 if (mapped)
819 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000820 } else if (MI->isInsideBundle()) {
821 if (mapped)
822 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000823 } else {
824 if (!mapped)
825 report("Missing slot index", MI);
826 }
827 }
828
Andrew Trick924123a2011-09-21 02:20:46 +0000829 StringRef ErrorInfo;
830 if (!TII->verifyInstruction(MI, ErrorInfo))
831 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000832}
833
834void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000835MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000836 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000837 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +0000838 unsigned NumDefs = MCID.getNumDefs();
839 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
840 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000841
Evan Cheng6cc775f2011-06-28 19:10:37 +0000842 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +0000843 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000844 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000845 if (!MO->isReg())
846 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +0000847 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000848 report("Explicit definition marked as use", MO, MONum);
849 else if (MO->isImplicit())
850 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000851 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000852 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +0000853 // Don't check if it's the last operand in a variadic instruction. See,
854 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000855 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000856 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000857 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +0000858 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000859 if (MO->isImplicit())
860 report("Explicit operand marked as implicit", MO, MONum);
861 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000862
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000863 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
864 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000865 if (!MO->isReg())
866 report("Tied use must be a register", MO, MONum);
867 else if (!MO->isTied())
868 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000869 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
870 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000871 } else if (MO->isReg() && MO->isTied())
872 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000873 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +0000874 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000875 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000876 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000877 }
878
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000879 switch (MO->getType()) {
880 case MachineOperand::MO_Register: {
881 const unsigned Reg = MO->getReg();
882 if (!Reg)
883 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000884 if (MRI->tracksLiveness() && !MI->isDebugValue())
885 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000886
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000887 // Verify the consistency of tied operands.
888 if (MO->isTied()) {
889 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
890 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
891 if (!OtherMO.isReg())
892 report("Must be tied to a register", MO, MONum);
893 if (!OtherMO.isTied())
894 report("Missing tie flags on tied operand", MO, MONum);
895 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
896 report("Inconsistent tie links", MO, MONum);
897 if (MONum < MCID.getNumDefs()) {
898 if (OtherIdx < MCID.getNumOperands()) {
899 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
900 report("Explicit def tied to explicit use without tie constraint",
901 MO, MONum);
902 } else {
903 if (!OtherMO.isImplicit())
904 report("Explicit def should be tied to implicit use", MO, MONum);
905 }
906 }
907 }
908
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +0000909 // Verify two-address constraints after leaving SSA form.
910 unsigned DefIdx;
911 if (!MRI->isSSA() && MO->isUse() &&
912 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
913 Reg != MI->getOperand(DefIdx).getReg())
914 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000915
916 // Check register classes.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000917 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000918 unsigned SubIdx = MO->getSubReg();
919
920 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000921 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000922 report("Illegal subregister index for physical register", MO, MONum);
923 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000924 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000925 if (const TargetRegisterClass *DRC =
926 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000927 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000928 report("Illegal physical register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000929 errs() << TRI->getName(Reg) << " is not a "
Craig Toppercf0444b2014-11-17 05:50:14 +0000930 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000931 }
932 }
933 } else {
934 // Virtual register.
935 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
936 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000937 const TargetRegisterClass *SRC =
938 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +0000939 if (!SRC) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000940 report("Invalid subregister index for virtual register", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000941 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +0000942 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000943 return;
944 }
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000945 if (RC != SRC) {
946 report("Invalid register class for subregister index", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000947 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000948 << " does not fully support subreg index " << SubIdx << "\n";
949 return;
950 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000951 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000952 if (const TargetRegisterClass *DRC =
953 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000954 if (SubIdx) {
955 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +0000956 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000957 if (!SuperRC) {
958 report("No largest legal super class exists.", MO, MONum);
959 return;
960 }
961 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
962 if (!DRC) {
963 report("No matching super-reg register class.", MO, MONum);
964 return;
965 }
966 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +0000967 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000968 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000969 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +0000970 << " register, but got a " << TRI->getRegClassName(RC)
971 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000972 }
973 }
974 }
975 }
976 break;
977 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000978
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000979 case MachineOperand::MO_RegisterMask:
980 regMasks.push_back(MO->getRegMask());
981 break;
982
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000983 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +0000984 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
985 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000986 break;
987
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000988 case MachineOperand::MO_FrameIndex:
989 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
990 LiveInts && !LiveInts->isNotInMIMap(MI)) {
991 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
992 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000993 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000994 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000995 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000996 }
Evan Cheng7f8e5632011-12-07 07:15:52 +0000997 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000998 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000999 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001000 }
1001 }
1002 break;
1003
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001004 default:
1005 break;
1006 }
1007}
1008
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001009void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1010 const MachineInstr *MI = MO->getParent();
1011 const unsigned Reg = MO->getReg();
1012
1013 // Both use and def operands can read a register.
1014 if (MO->readsReg()) {
1015 regsLiveInButUnused.erase(Reg);
1016
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001017 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001018 addRegWithSubRegs(regsKilled, Reg);
1019
1020 // Check that LiveVars knows this kill.
1021 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1022 MO->isKill()) {
1023 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1024 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
1025 report("Kill missing from LiveVariables", MO, MONum);
1026 }
1027
1028 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001029 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
1030 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
1031 // Check the cached regunit intervals.
1032 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1033 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun34e1be92013-10-10 21:29:02 +00001034 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units)) {
1035 LiveQueryResult LRQ = LR->Query(UseIdx);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001036 if (!LRQ.valueIn()) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001037 report("No live segment at use", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001038 errs() << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
Matthias Braun34e1be92013-10-10 21:29:02 +00001039 << ' ' << *LR << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001040 }
1041 if (MO->isKill() && !LRQ.isKill()) {
1042 report("Live range continues after kill flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001043 errs() << PrintRegUnit(*Units, TRI) << ' ' << *LR << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001044 }
1045 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001046 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001047 }
1048
1049 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1050 if (LiveInts->hasInterval(Reg)) {
1051 // This is a virtual register interval.
1052 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun88dd0ab2013-10-10 21:28:52 +00001053 LiveQueryResult LRQ = LI.Query(UseIdx);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001054 if (!LRQ.valueIn()) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001055 report("No live segment at use", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001056 errs() << UseIdx << " is not live in " << LI << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001057 }
1058 // Check for extra kill flags.
1059 // Note that we allow missing kill flags for now.
1060 if (MO->isKill() && !LRQ.isKill()) {
1061 report("Live range continues after kill flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001062 errs() << "Live range: " << LI << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001063 }
1064 } else {
1065 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001066 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001067 }
1068 }
1069
1070 // Use of a dead register.
1071 if (!regsLive.count(Reg)) {
1072 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1073 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001074 bool Bad = !isReserved(Reg);
1075 // We are fine if just any subregister has a defined value.
1076 if (Bad) {
1077 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1078 ++SubRegs) {
1079 if (regsLive.count(*SubRegs)) {
1080 Bad = false;
1081 break;
1082 }
1083 }
1084 }
Matthias Braun96a31952015-01-14 22:25:14 +00001085 // If there is an additional implicit-use of a super register we stop
1086 // here. By definition we are fine if the super register is not
1087 // (completely) dead, if the complete super register is dead we will
1088 // get a report for its operand.
1089 if (Bad) {
1090 for (const MachineOperand &MOP : MI->uses()) {
1091 if (!MOP.isReg())
1092 continue;
1093 if (!MOP.isImplicit())
1094 continue;
1095 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1096 ++SubRegs) {
1097 if (*SubRegs == Reg) {
1098 Bad = false;
1099 break;
1100 }
1101 }
1102 }
1103 }
Matthias Braun96d77322014-12-10 01:13:13 +00001104 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001105 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001106 } else if (MRI->def_empty(Reg)) {
1107 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001108 } else {
1109 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1110 // We don't know which virtual registers are live in, so only complain
1111 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1112 // must be live in. PHI instructions are handled separately.
1113 if (MInfo.regsKilled.count(Reg))
1114 report("Using a killed virtual register", MO, MONum);
1115 else if (!MI->isPHI())
1116 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1117 }
1118 }
1119 }
1120
1121 if (MO->isDef()) {
1122 // Register defined.
1123 // TODO: verify that earlyclobber ops are not used.
1124 if (MO->isDead())
1125 addRegWithSubRegs(regsDead, Reg);
1126 else
1127 addRegWithSubRegs(regsDefined, Reg);
1128
1129 // Verify SSA form.
1130 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001131 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001132 report("Multiple virtual register defs in SSA form", MO, MONum);
1133
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001134 // Check LiveInts for a live segment, but only for virtual registers.
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001135 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1136 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001137 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1138 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001139 if (LiveInts->hasInterval(Reg)) {
1140 const LiveInterval &LI = LiveInts->getInterval(Reg);
1141 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1142 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001143 if (VNI->def != DefIdx) {
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001144 report("Inconsistent valno->def", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001145 errs() << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001146 << DefIdx << " in " << LI << '\n';
1147 }
1148 } else {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001149 report("No live segment at def", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001150 errs() << DefIdx << " is not live in " << LI << '\n';
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001151 }
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001152 // Check that, if the dead def flag is present, LiveInts agree.
1153 if (MO->isDead()) {
1154 LiveQueryResult LRQ = LI.Query(DefIdx);
1155 if (!LRQ.isDeadDef()) {
1156 report("Live range continues after dead def flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001157 errs() << "Live range: " << LI << '\n';
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001158 }
1159 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001160 } else {
1161 report("Virtual register has no Live interval", MO, MONum);
1162 }
1163 }
1164 }
1165}
1166
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001167void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001168}
1169
1170// This function gets called after visiting all instructions in a bundle. The
1171// argument points to the bundle header.
1172// Normal stand-alone instructions are also considered 'bundles', and this
1173// function is called for all of them.
1174void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001175 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1176 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001177 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001178 // Kill any masked registers.
1179 while (!regMasks.empty()) {
1180 const uint32_t *Mask = regMasks.pop_back_val();
1181 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1182 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1183 MachineOperand::clobbersPhysReg(Mask, *I))
1184 regsDead.push_back(*I);
1185 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001186 set_subtract(regsLive, regsDead); regsDead.clear();
1187 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001188}
1189
1190void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001191MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001192 MBBInfoMap[MBB].regsLiveOut = regsLive;
1193 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001194
1195 if (Indexes) {
1196 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1197 if (!(stop > lastIndex)) {
1198 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001199 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001200 << " last instruction was at " << lastIndex << '\n';
1201 }
1202 lastIndex = stop;
1203 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001204}
1205
1206// Calculate the largest possible vregsPassed sets. These are the registers that
1207// can pass through an MBB live, but may not be live every time. It is assumed
1208// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001209void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001210 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1211 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001212 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001213 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001214 BBInfo &MInfo = MBBInfoMap[&MBB];
1215 if (!MInfo.reachable)
1216 continue;
1217 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1218 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1219 BBInfo &SInfo = MBBInfoMap[*SuI];
1220 if (SInfo.addPassed(MInfo.regsLiveOut))
1221 todo.insert(*SuI);
1222 }
1223 }
1224
1225 // Iteratively push vregsPassed to successors. This will converge to the same
1226 // final state regardless of DenseSet iteration order.
1227 while (!todo.empty()) {
1228 const MachineBasicBlock *MBB = *todo.begin();
1229 todo.erase(MBB);
1230 BBInfo &MInfo = MBBInfoMap[MBB];
1231 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1232 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1233 if (*SuI == MBB)
1234 continue;
1235 BBInfo &SInfo = MBBInfoMap[*SuI];
1236 if (SInfo.addPassed(MInfo.vregsPassed))
1237 todo.insert(*SuI);
1238 }
1239 }
1240}
1241
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001242// Calculate the set of virtual registers that must be passed through each basic
1243// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001244// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001245void MachineVerifier::calcRegsRequired() {
1246 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001247 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001248 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001249 BBInfo &MInfo = MBBInfoMap[&MBB];
1250 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1251 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1252 BBInfo &PInfo = MBBInfoMap[*PrI];
1253 if (PInfo.addRequired(MInfo.vregsLiveIn))
1254 todo.insert(*PrI);
1255 }
1256 }
1257
1258 // Iteratively push vregsRequired to predecessors. This will converge to the
1259 // same final state regardless of DenseSet iteration order.
1260 while (!todo.empty()) {
1261 const MachineBasicBlock *MBB = *todo.begin();
1262 todo.erase(MBB);
1263 BBInfo &MInfo = MBBInfoMap[MBB];
1264 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1265 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1266 if (*PrI == MBB)
1267 continue;
1268 BBInfo &SInfo = MBBInfoMap[*PrI];
1269 if (SInfo.addRequired(MInfo.vregsRequired))
1270 todo.insert(*PrI);
1271 }
1272 }
1273}
1274
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001275// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001276// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001277void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001278 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001279 for (const auto &BBI : *MBB) {
1280 if (!BBI.isPHI())
1281 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001282 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001283
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001284 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1285 unsigned Reg = BBI.getOperand(i).getReg();
1286 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001287 if (!Pre->isSuccessor(MBB))
1288 continue;
1289 seen.insert(Pre);
1290 BBInfo &PrInfo = MBBInfoMap[Pre];
1291 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1292 report("PHI operand is not live-out from predecessor",
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001293 &BBI.getOperand(i), i);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001294 }
1295
1296 // Did we see all predecessors?
1297 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1298 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1299 if (!seen.count(*PrI)) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001300 report("Missing PHI operand", &BBI);
Owen Anderson21b17882015-02-04 00:02:59 +00001301 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001302 << " is a predecessor according to the CFG.\n";
1303 }
1304 }
1305 }
1306}
1307
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001308void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001309 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001310
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001311 for (const auto &MBB : *MF) {
1312 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001313
1314 // Skip unreachable MBBs.
1315 if (!MInfo.reachable)
1316 continue;
1317
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001318 checkPHIOps(&MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001319 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001320
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001321 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001322 calcRegsRequired();
1323
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001324 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001325 for (const auto &MBB : *MF) {
1326 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001327 for (RegSet::iterator
1328 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1329 ++I)
1330 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001331 report("Virtual register killed in block, but needed live out.", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001332 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001333 << " is used after the block.\n";
1334 }
1335 }
1336
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001337 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001338 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1339 for (RegSet::iterator
1340 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesen99014ff2012-03-10 00:44:11 +00001341 ++I)
1342 report("Virtual register def doesn't dominate all uses.",
1343 MRI->getVRegDef(*I));
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001344 }
1345
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001346 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001347 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001348 if (LiveInts)
1349 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001350}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001351
1352void MachineVerifier::verifyLiveVariables() {
1353 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001354 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1355 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001356 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001357 for (const auto &MBB : *MF) {
1358 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001359
1360 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1361 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001362 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1363 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001364 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001365 << " must be live through the block.\n";
1366 }
1367 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001368 if (VI.AliveBlocks.test(MBB.getNumber())) {
1369 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001370 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001371 << " is not needed live through the block.\n";
1372 }
1373 }
1374 }
1375 }
1376}
1377
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001378void MachineVerifier::verifyLiveIntervals() {
1379 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001380 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1381 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001382
1383 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001384 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001385 continue;
1386
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001387 if (!LiveInts->hasInterval(Reg)) {
1388 report("Missing live interval for virtual register", MF);
Owen Anderson21b17882015-02-04 00:02:59 +00001389 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001390 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001391 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001392
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001393 const LiveInterval &LI = LiveInts->getInterval(Reg);
1394 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001395 verifyLiveInterval(LI);
1396 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001397
1398 // Verify all the cached regunit intervals.
1399 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001400 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1401 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001402}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001403
Matthias Braun364e6e92013-10-10 21:28:54 +00001404void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001405 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001406 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001407 if (VNI->isUnused())
1408 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001409
Matthias Braun364e6e92013-10-10 21:28:54 +00001410 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001411
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001412 if (!DefVNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001413 report("Valno not live at def and not marked unused", MF, LR, Reg,
1414 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001415 errs() << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001416 return;
1417 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001418
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001419 if (DefVNI != VNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001420 report("Live segment at def has different valno", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001421 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +00001422 << " where valno #" << DefVNI->id << " is live\n";
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001423 return;
1424 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001425
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001426 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1427 if (!MBB) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001428 report("Invalid definition index", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001429 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Matthias Braun364e6e92013-10-10 21:28:54 +00001430 << " in " << LR << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001431 return;
1432 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001433
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001434 if (VNI->isPHIDef()) {
1435 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001436 report("PHIDef value is not defined at MBB start", MBB, LR, Reg,
1437 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001438 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +00001439 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001440 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001441 return;
1442 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001443
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001444 // Non-PHI def.
1445 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1446 if (!MI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001447 report("No instruction at def index", MBB, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001448 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001449 return;
1450 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001451
Matthias Braun364e6e92013-10-10 21:28:54 +00001452 if (Reg != 0) {
1453 bool hasDef = false;
1454 bool isEarlyClobber = false;
1455 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1456 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001457 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001458 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1459 if (MOI->getReg() != Reg)
1460 continue;
1461 } else {
1462 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1463 !TRI->hasRegUnit(MOI->getReg(), Reg))
1464 continue;
1465 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001466 if (LaneMask != 0 &&
1467 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask) == 0)
1468 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001469 hasDef = true;
1470 if (MOI->isEarlyClobber())
1471 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001472 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001473
Matthias Braun364e6e92013-10-10 21:28:54 +00001474 if (!hasDef) {
1475 report("Defining instruction does not modify register", MI);
Owen Anderson21b17882015-02-04 00:02:59 +00001476 errs() << "Valno #" << VNI->id << " in " << LR << '\n';
Matthias Braun364e6e92013-10-10 21:28:54 +00001477 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001478
Matthias Braun364e6e92013-10-10 21:28:54 +00001479 // Early clobber defs begin at USE slots, but other defs must begin at
1480 // DEF slots.
1481 if (isEarlyClobber) {
1482 if (!VNI->def.isEarlyClobber()) {
Matthias Braun47760d92014-11-19 19:46:13 +00001483 report("Early clobber def must be at an early-clobber slot", MBB, LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001484 Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001485 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Matthias Braun364e6e92013-10-10 21:28:54 +00001486 }
1487 } else if (!VNI->def.isRegister()) {
1488 report("Non-PHI, non-early clobber def must be at a register slot",
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001489 MBB, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001490 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001491 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001492 }
1493}
1494
Matthias Braun364e6e92013-10-10 21:28:54 +00001495void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1496 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001497 unsigned Reg, LaneBitmask LaneMask)
1498{
Matthias Braun364e6e92013-10-10 21:28:54 +00001499 const LiveRange::Segment &S = *I;
1500 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001501 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001502
Matthias Braun364e6e92013-10-10 21:28:54 +00001503 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001504 report("Foreign valno in live segment", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001505 errs() << S << " has a bad valno\n";
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001506 }
1507
1508 if (VNI->isUnused()) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001509 report("Live segment valno is marked unused", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001510 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001511 }
1512
Matthias Braun364e6e92013-10-10 21:28:54 +00001513 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001514 if (!MBB) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001515 report("Bad start of live segment, no basic block", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001516 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001517 return;
1518 }
1519 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001520 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001521 report("Live segment must begin at MBB entry or valno def", MBB, LR, Reg,
1522 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001523 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001524 }
1525
1526 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001527 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001528 if (!EndMBB) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001529 report("Bad end of live segment, no basic block", MF, LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001530 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001531 return;
1532 }
1533
1534 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001535 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001536 return;
1537
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001538 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001539 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1540 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001541 return;
1542
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001543 // The live segment is ending inside EndMBB
1544 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001545 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001546 if (!MI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001547 report("Live segment doesn't end at a valid instruction", EndMBB, LR, Reg,
1548 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001549 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001550 return;
1551 }
1552
1553 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001554 if (S.end.isBlock()) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001555 report("Live segment ends at B slot of an instruction", EndMBB, LR, Reg,
1556 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001557 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001558 }
1559
Matthias Braun364e6e92013-10-10 21:28:54 +00001560 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001561 // Segment ends on the dead slot.
1562 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001563 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun47760d92014-11-19 19:46:13 +00001564 report("Live segment ending at dead slot spans instructions", EndMBB, LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001565 Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001566 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001567 }
1568 }
1569
1570 // A live segment can only end at an early-clobber slot if it is being
1571 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001572 if (S.end.isEarlyClobber()) {
1573 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001574 report("Live segment ending at early clobber slot must be "
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001575 "redefined by an EC def in the same instruction", EndMBB, LR, Reg,
1576 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001577 errs() << S << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001578 }
1579 }
1580
1581 // The following checks only apply to virtual registers. Physreg liveness
1582 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001583 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001584 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001585 // use, or a dead flag on a def.
1586 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001587 bool hasSubRegDef = false;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001588 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001589 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001590 continue;
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001591 if (LaneMask != 0 &&
1592 (LaneMask & TRI->getSubRegIndexLaneMask(MOI->getSubReg())) == 0)
1593 continue;
Matthias Braun21554d92014-12-10 01:13:11 +00001594 if (MOI->isDef() && MOI->getSubReg() != 0)
1595 hasSubRegDef = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001596 if (MOI->readsReg())
1597 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001598 }
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001599 if (!S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001600 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001601 // When tracking subregister liveness, the main range must start new
1602 // values on partial register writes, even if there is no read.
Matthias Brauna25e13a2015-03-19 00:21:58 +00001603 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask != 0 ||
1604 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001605 report("Instruction ending live segment doesn't read the register",
1606 MI);
Owen Anderson21b17882015-02-04 00:02:59 +00001607 errs() << S << " in " << LR << '\n';
Matthias Braun21554d92014-12-10 01:13:11 +00001608 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001609 }
1610 }
1611 }
1612
1613 // Now check all the basic blocks in this live segment.
1614 MachineFunction::const_iterator MFI = MBB;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001615 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00001616 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001617 // Not live-in to any blocks.
1618 if (MBB == EndMBB)
1619 return;
1620 // Skip this block.
1621 ++MFI;
1622 }
1623 for (;;) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001624 assert(LiveInts->isLiveInToMBB(LR, MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001625 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00001626 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00001627 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001628 if (&*MFI == EndMBB)
1629 break;
1630 ++MFI;
1631 continue;
1632 }
1633
1634 // Is VNI a PHI-def in the current block?
1635 bool IsPHI = VNI->isPHIDef() &&
1636 VNI->def == LiveInts->getMBBStartIdx(MFI);
1637
1638 // Check that VNI is live-out of all predecessors.
1639 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1640 PE = MFI->pred_end(); PI != PE; ++PI) {
1641 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001642 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001643
1644 // All predecessors must have a live-out value.
1645 if (!PVNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001646 report("Register not marked live out of predecessor", *PI, LR, Reg,
1647 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001648 errs() << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001649 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +00001650 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001651 continue;
1652 }
1653
1654 // Only PHI-defs can take different predecessor values.
1655 if (!IsPHI && PVNI != VNI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001656 report("Different value live out of predecessor", *PI, LR, Reg,
1657 LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001658 errs() << "Valno #" << PVNI->id << " live out of BB#"
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001659 << (*PI)->getNumber() << '@' << PEnd
1660 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +00001661 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001662 }
1663 }
1664 if (&*MFI == EndMBB)
1665 break;
1666 ++MFI;
1667 }
1668}
1669
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001670void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001671 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00001672 for (const VNInfo *VNI : LR.valnos)
1673 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001674
Matthias Braun364e6e92013-10-10 21:28:54 +00001675 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001676 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00001677}
1678
1679void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001680 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00001681 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1682 verifyLiveRange(LI, Reg);
1683
Matthias Braune6a24852015-09-25 21:51:14 +00001684 LaneBitmask Mask = 0;
1685 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00001686 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1687 if ((Mask & SR.LaneMask) != 0)
1688 report("Lane masks of sub ranges overlap in live interval", MF, LI);
1689 if ((SR.LaneMask & ~MaxMask) != 0)
1690 report("Subrange lanemask is invalid", MF, LI);
Matthias Braun0d4cebd2015-07-16 18:55:35 +00001691 if (SR.empty())
1692 report("Subrange must not be empty", MF, SR, LI.reg, SR.LaneMask);
Matthias Braune962e522015-03-25 21:18:22 +00001693 Mask |= SR.LaneMask;
1694 verifyLiveRange(SR, LI.reg, SR.LaneMask);
1695 if (!LI.covers(SR))
1696 report("A Subrange is not covered by the main range", MF, LI);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001697 }
1698
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001699 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00001700 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1701 unsigned NumComp = ConEQ.Classify(&LI);
1702 if (NumComp > 1) {
1703 report("Multiple connected components in live interval", MF, LI);
1704 for (unsigned comp = 0; comp != NumComp; ++comp) {
1705 errs() << comp << ": valnos";
1706 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1707 E = LI.vni_end(); I!=E; ++I)
1708 if (comp == ConEQ.getEqClass(*I))
1709 errs() << ' ' << (*I)->id;
1710 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00001711 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001712 }
1713}
Manman Renaa6875b2013-07-15 21:26:31 +00001714
1715namespace {
1716 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1717 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1718 // value is zero.
1719 // We use a bool plus an integer to capture the stack state.
1720 struct StackStateOfBB {
1721 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1722 ExitIsSetup(false) { }
1723 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1724 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1725 ExitIsSetup(ExitSetup) { }
1726 // Can be negative, which means we are setting up a frame.
1727 int EntryValue;
1728 int ExitValue;
1729 bool EntryIsSetup;
1730 bool ExitIsSetup;
1731 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001732}
Manman Renaa6875b2013-07-15 21:26:31 +00001733
1734/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1735/// by a FrameDestroy <n>, stack adjustments are identical on all
1736/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1737void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001738 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1739 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Manman Renaa6875b2013-07-15 21:26:31 +00001740
1741 SmallVector<StackStateOfBB, 8> SPState;
1742 SPState.resize(MF->getNumBlockIDs());
1743 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1744
1745 // Visit the MBBs in DFS order.
1746 for (df_ext_iterator<const MachineFunction*,
1747 SmallPtrSet<const MachineBasicBlock*, 8> >
1748 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1749 DFI != DFE; ++DFI) {
1750 const MachineBasicBlock *MBB = *DFI;
1751
1752 StackStateOfBB BBState;
1753 // Check the exit state of the DFS stack predecessor.
1754 if (DFI.getPathLength() >= 2) {
1755 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1756 assert(Reachable.count(StackPred) &&
1757 "DFS stack predecessor is already visited.\n");
1758 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1759 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1760 BBState.ExitValue = BBState.EntryValue;
1761 BBState.ExitIsSetup = BBState.EntryIsSetup;
1762 }
1763
1764 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001765 for (const auto &I : *MBB) {
1766 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001767 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001768 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001769 assert(Size >= 0 &&
1770 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1771
1772 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001773 report("FrameSetup is after another FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001774 BBState.ExitValue -= Size;
1775 BBState.ExitIsSetup = true;
1776 }
1777
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001778 if (I.getOpcode() == FrameDestroyOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001779 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001780 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001781 assert(Size >= 0 &&
1782 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1783
1784 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001785 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001786 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1787 BBState.ExitValue;
1788 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001789 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00001790 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00001791 << AbsSPAdj << ">.\n";
1792 }
1793 BBState.ExitValue += Size;
1794 BBState.ExitIsSetup = false;
1795 }
1796 }
1797 SPState[MBB->getNumber()] = BBState;
1798
1799 // Make sure the exit state of any predecessor is consistent with the entry
1800 // state.
1801 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
1802 E = MBB->pred_end(); I != E; ++I) {
1803 if (Reachable.count(*I) &&
1804 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
1805 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
1806 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001807 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00001808 << SPState[(*I)->getNumber()].ExitValue << ", "
1809 << SPState[(*I)->getNumber()].ExitIsSetup
1810 << "), while BB#" << MBB->getNumber() << " has entry state ("
1811 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
1812 }
1813 }
1814
1815 // Make sure the entry state of any successor is consistent with the exit
1816 // state.
1817 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
1818 E = MBB->succ_end(); I != E; ++I) {
1819 if (Reachable.count(*I) &&
1820 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
1821 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
1822 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001823 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00001824 << SPState[(*I)->getNumber()].EntryValue << ", "
1825 << SPState[(*I)->getNumber()].EntryIsSetup
1826 << "), while BB#" << MBB->getNumber() << " has exit state ("
1827 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
1828 }
1829 }
1830
1831 // Make sure a basic block with return ends with zero stack adjustment.
1832 if (!MBB->empty() && MBB->back().isReturn()) {
1833 if (BBState.ExitIsSetup)
1834 report("A return block ends with a FrameSetup.", MBB);
1835 if (BBState.ExitValue)
1836 report("A return block ends with a nonzero stack adjustment.", MBB);
1837 }
1838 }
1839}