blob: cdcd8eb4fbdfcc533c6b37c9e2679dd1e3331f45 [file] [log] [blame]
Bill Wendling68caaaf2010-08-19 18:52:17 +00001//===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Pass to verify generated machine code. The following is checked:
11//
12// Operand counts: All explicit operands must be present.
13//
14// Register classes: All physical and virtual register operands must be
15// compatible with the register class required by the instruction descriptor.
16//
17// Register live intervals: Registers must be defined only once, and must be
18// defined before use.
19//
20// The machine code verifier is enabled from LLVMTargetMachine.cpp with the
21// command-line option -verify-machineinstrs, or by defining the environment
22// variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
23// the verifier errors.
24//===----------------------------------------------------------------------===//
25
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000026#include "llvm/CodeGen/Passes.h"
Chris Lattner565449d2009-08-23 03:13:20 +000027#include "llvm/ADT/DenseSet.h"
Manman Renaa6875b2013-07-15 21:26:31 +000028#include "llvm/ADT/DepthFirstIterator.h"
Chris Lattner565449d2009-08-23 03:13:20 +000029#include "llvm/ADT/SetOperations.h"
30#include "llvm/ADT/SmallVector.h"
David Majnemer70497c62015-12-02 23:06:39 +000031#include "llvm/Analysis/EHPersonalities.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/CodeGen/LiveIntervalAnalysis.h"
33#include "llvm/CodeGen/LiveStackAnalysis.h"
34#include "llvm/CodeGen/LiveVariables.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/CodeGen/MachineMemOperand.h"
38#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/InlineAsm.h"
41#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000043#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000044#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000045#include "llvm/Support/FileSystem.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000046#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Target/TargetInstrInfo.h"
48#include "llvm/Target/TargetMachine.h"
49#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000050#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000051using namespace llvm;
52
53namespace {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000054 struct MachineVerifier {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000055
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000056 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000057 PASS(pass),
Owen Anderson21b17882015-02-04 00:02:59 +000058 Banner(b)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000059 {}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000060
61 bool runOnMachineFunction(MachineFunction &MF);
62
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000063 Pass *const PASS;
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000064 const char *Banner;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000065 const MachineFunction *MF;
66 const TargetMachine *TM;
Evan Cheng8d71a752011-06-27 21:26:13 +000067 const TargetInstrInfo *TII;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000068 const TargetRegisterInfo *TRI;
69 const MachineRegisterInfo *MRI;
70
71 unsigned foundErrors;
72
73 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000074 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000075 typedef DenseSet<unsigned> RegSet;
76 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000077 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000078
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000079 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000080 BlockSet FunctionBlocks;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000081
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000082 BitVector regsReserved;
83 RegSet regsLive;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000084 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000085 RegMaskVector regMasks;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000086 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000087
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +000088 SlotIndex lastIndex;
89
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000090 // Add Reg and any sub-registers to RV
91 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
92 RV.push_back(Reg);
93 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +000094 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
95 RV.push_back(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000096 }
97
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000098 struct BBInfo {
99 // Is this MBB reachable from the MF entry point?
100 bool reachable;
101
102 // Vregs that must be live in because they are used without being
103 // defined. Map value is the user.
104 RegMap vregsLiveIn;
105
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000106 // Regs killed in MBB. They may be defined again, and will then be in both
107 // regsKilled and regsLiveOut.
108 RegSet regsKilled;
109
110 // Regs defined in MBB and live out. Note that vregs passing through may
111 // be live out without being mentioned here.
112 RegSet regsLiveOut;
113
114 // Vregs that pass through MBB untouched. This set is disjoint from
115 // regsKilled and regsLiveOut.
116 RegSet vregsPassed;
117
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000118 // Vregs that must pass through MBB because they are needed by a successor
119 // block. This set is disjoint from regsLiveOut.
120 RegSet vregsRequired;
121
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000122 // Set versions of block's predecessor and successor lists.
123 BlockSet Preds, Succs;
124
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000125 BBInfo() : reachable(false) {}
126
127 // Add register to vregsPassed if it belongs there. Return true if
128 // anything changed.
129 bool addPassed(unsigned Reg) {
130 if (!TargetRegisterInfo::isVirtualRegister(Reg))
131 return false;
132 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
133 return false;
134 return vregsPassed.insert(Reg).second;
135 }
136
137 // Same for a full set.
138 bool addPassed(const RegSet &RS) {
139 bool changed = false;
140 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
141 if (addPassed(*I))
142 changed = true;
143 return changed;
144 }
145
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000146 // Add register to vregsRequired if it belongs there. Return true if
147 // anything changed.
148 bool addRequired(unsigned Reg) {
149 if (!TargetRegisterInfo::isVirtualRegister(Reg))
150 return false;
151 if (regsLiveOut.count(Reg))
152 return false;
153 return vregsRequired.insert(Reg).second;
154 }
155
156 // Same for a full set.
157 bool addRequired(const RegSet &RS) {
158 bool changed = false;
159 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
160 if (addRequired(*I))
161 changed = true;
162 return changed;
163 }
164
165 // Same for a full map.
166 bool addRequired(const RegMap &RM) {
167 bool changed = false;
168 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
169 if (addRequired(I->first))
170 changed = true;
171 return changed;
172 }
173
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000174 // Live-out registers are either in regsLiveOut or vregsPassed.
175 bool isLiveOut(unsigned Reg) const {
176 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
177 }
178 };
179
180 // Extra register info per MBB.
181 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
182
183 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000184 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000185 }
186
Lang Hames1ce837a2012-02-14 19:17:48 +0000187 bool isAllocatable(unsigned Reg) {
Jakob Stoklund Olesen244beb42012-10-16 00:05:06 +0000188 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
Lang Hames1ce837a2012-02-14 19:17:48 +0000189 }
190
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000191 // Analysis information if available
192 LiveVariables *LiveVars;
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +0000193 LiveIntervals *LiveInts;
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000194 LiveStacks *LiveStks;
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000195 SlotIndexes *Indexes;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000196
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000197 void visitMachineFunctionBefore();
198 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000199 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000200 void visitMachineInstrBefore(const MachineInstr *MI);
201 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
202 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000203 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000204 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
205 void visitMachineFunctionAfter();
206
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000207 template <typename T> void report(const char *msg, ilist_iterator<T> I) {
208 report(msg, &*I);
209 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000210 void report(const char *msg, const MachineFunction *MF);
211 void report(const char *msg, const MachineBasicBlock *MBB);
212 void report(const char *msg, const MachineInstr *MI);
213 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Matthias Braun7e624d52015-11-09 23:59:33 +0000214
215 void report_context(const LiveInterval &LI) const;
216 void report_context(const LiveRange &LR, unsigned Reg,
217 LaneBitmask LaneMask) const;
218 void report_context(const LiveRange::Segment &S) const;
219 void report_context(const VNInfo &VNI) const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000220
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000221 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000222
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000223 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000224 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000225 void calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000226 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000227
228 void calcRegsRequired();
229 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000230 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000231 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000232 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
233 unsigned);
Matthias Braun364e6e92013-10-10 21:28:54 +0000234 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000235 const LiveRange::const_iterator I, unsigned,
236 unsigned);
Matthias Braune6a24852015-09-25 21:51:14 +0000237 void verifyLiveRange(const LiveRange&, unsigned, LaneBitmask LaneMask = 0);
Manman Renaa6875b2013-07-15 21:26:31 +0000238
239 void verifyStackFrame();
Matthias Braun80595462015-09-09 17:49:46 +0000240
241 void verifySlotIndexes() const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000242 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000243
244 struct MachineVerifierPass : public MachineFunctionPass {
245 static char ID; // Pass ID, replacement for typeid
Matthias Brauna4e932d2014-12-11 19:41:51 +0000246 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000247
Matthias Brauna4e932d2014-12-11 19:41:51 +0000248 MachineVerifierPass(const std::string &banner = nullptr)
249 : MachineFunctionPass(ID), Banner(banner) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000250 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
251 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000252
Craig Topper4584cd52014-03-07 09:26:03 +0000253 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000254 AU.setPreservesAll();
255 MachineFunctionPass::getAnalysisUsage(AU);
256 }
257
Craig Topper4584cd52014-03-07 09:26:03 +0000258 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Brauna4e932d2014-12-11 19:41:51 +0000259 MF.verify(this, Banner.c_str());
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000260 return false;
261 }
262 };
263
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000264}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000265
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000266char MachineVerifierPass::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +0000267INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000268 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000269
Matthias Brauna4e932d2014-12-11 19:41:51 +0000270FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000271 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000272}
273
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000274void MachineFunction::verify(Pass *p, const char *Banner) const {
275 MachineVerifier(p, Banner)
276 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesen27440e72009-11-13 21:56:09 +0000277}
278
Matthias Braun80595462015-09-09 17:49:46 +0000279void MachineVerifier::verifySlotIndexes() const {
280 if (Indexes == nullptr)
281 return;
282
283 // Ensure the IdxMBB list is sorted by slot indexes.
284 SlotIndex Last;
285 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
286 E = Indexes->MBBIndexEnd(); I != E; ++I) {
287 assert(!Last.isValid() || I->first > Last);
288 Last = I->first;
289 }
290}
291
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000292bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000293 foundErrors = 0;
294
295 this->MF = &MF;
296 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000297 TII = MF.getSubtarget().getInstrInfo();
298 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000299 MRI = &MF.getRegInfo();
300
Craig Topperc0196b12014-04-14 00:51:57 +0000301 LiveVars = nullptr;
302 LiveInts = nullptr;
303 LiveStks = nullptr;
304 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000305 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000306 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000307 // We don't want to verify LiveVariables if LiveIntervals is available.
308 if (!LiveInts)
309 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000310 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000311 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000312 }
313
Matthias Braun80595462015-09-09 17:49:46 +0000314 verifySlotIndexes();
315
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000316 visitMachineFunctionBefore();
317 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
318 MFI!=MFE; ++MFI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000319 visitMachineBasicBlockBefore(&*MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000320 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000321 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000322 // Do we expect the next instruction to be part of the same bundle?
323 bool InBundle = false;
324
Evan Cheng7fae11b2011-12-14 02:11:42 +0000325 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
326 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000327 if (MBBI->getParent() != &*MFI) {
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000328 report("Bad instruction parent pointer", MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000329 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000330 continue;
331 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000332
333 // Check for consistent bundle flags.
334 if (InBundle && !MBBI->isBundledWithPred())
335 report("Missing BundledPred flag, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000336 "BundledSucc was set on predecessor",
337 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000338 if (!InBundle && MBBI->isBundledWithPred())
339 report("BundledPred flag is set, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000340 "but BundledSucc not set on predecessor",
341 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000342
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000343 // Is this a bundle header?
344 if (!MBBI->isInsideBundle()) {
345 if (CurBundle)
346 visitMachineBundleAfter(CurBundle);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000347 CurBundle = &*MBBI;
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000348 visitMachineBundleBefore(CurBundle);
349 } else if (!CurBundle)
350 report("No bundle header", MBBI);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000351 visitMachineInstrBefore(&*MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000352 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
353 const MachineInstr &MI = *MBBI;
354 const MachineOperand &Op = MI.getOperand(I);
355 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000356 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000357 // functions when replacing operands of a MachineInstr.
358 report("Instruction has operand with wrong parent set", &MI);
359 }
360
361 visitMachineOperand(&Op, I);
362 }
363
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000364 visitMachineInstrAfter(&*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000365
366 // Was this the last bundled instruction?
367 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000368 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000369 if (CurBundle)
370 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000371 if (InBundle)
372 report("BundledSucc flag set on last instruction in block", &MFI->back());
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000373 visitMachineBasicBlockAfter(&*MFI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000374 }
375 visitMachineFunctionAfter();
376
Owen Anderson21b17882015-02-04 00:02:59 +0000377 if (foundErrors)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000378 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000379
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000380 // Clean up.
381 regsLive.clear();
382 regsDefined.clear();
383 regsDead.clear();
384 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000385 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000386 regsLiveInButUnused.clear();
387 MBBInfoMap.clear();
388
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000389 return false; // no changes
390}
391
Chris Lattner75f40452009-08-23 01:03:30 +0000392void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000393 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000394 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000395 if (!foundErrors++) {
396 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000397 errs() << "# " << Banner << '\n';
Matthias Braun42b4b632015-11-09 23:59:23 +0000398 if (LiveInts != nullptr)
399 LiveInts->print(errs());
400 else
401 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000402 }
Owen Anderson21b17882015-02-04 00:02:59 +0000403 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000404 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000405}
406
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000407void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000408 assert(MBB);
409 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000410 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000411 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000412 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000413 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000414 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000415 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000416 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000417}
418
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000419void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000420 assert(MI);
421 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000422 errs() << "- instruction: ";
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000423 if (Indexes && Indexes->hasIndex(MI))
Owen Anderson21b17882015-02-04 00:02:59 +0000424 errs() << Indexes->getInstructionIndex(MI) << '\t';
Matthias Braun45718db2015-11-09 23:59:25 +0000425 MI->print(errs(), /*SkipOpers=*/true);
Matthias Braun716b4332015-11-09 23:59:29 +0000426 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000427}
428
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000429void MachineVerifier::report(const char *msg,
430 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000431 assert(MO);
432 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000433 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000434 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000435 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000436}
437
Matthias Braun7e624d52015-11-09 23:59:33 +0000438void MachineVerifier::report_context(const LiveInterval &LI) const {
Owen Anderson21b17882015-02-04 00:02:59 +0000439 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000440}
441
Matthias Braun7e624d52015-11-09 23:59:33 +0000442void MachineVerifier::report_context(const LiveRange &LR, unsigned Reg,
443 LaneBitmask LaneMask) const {
Owen Anderson21b17882015-02-04 00:02:59 +0000444 errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000445 if (LaneMask != 0)
Matthias Braunc804cdb2015-09-25 21:51:24 +0000446 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
Matthias Braun7e624d52015-11-09 23:59:33 +0000447 errs() << "- liverange: " << LR << '\n';
Matthias Braun364e6e92013-10-10 21:28:54 +0000448}
449
Matthias Braun7e624d52015-11-09 23:59:33 +0000450void MachineVerifier::report_context(const LiveRange::Segment &S) const {
451 errs() << "- segment: " << S << '\n';
452}
453
454void MachineVerifier::report_context(const VNInfo &VNI) const {
455 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
Matthias Braun364e6e92013-10-10 21:28:54 +0000456}
457
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000458void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000459 BBInfo &MInfo = MBBInfoMap[MBB];
460 if (!MInfo.reachable) {
461 MInfo.reachable = true;
462 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
463 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
464 markReachable(*SuI);
465 }
466}
467
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000468void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000469 lastIndex = SlotIndex();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000470 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000471
472 // A sub-register of a reserved register is also reserved
473 for (int Reg = regsReserved.find_first(); Reg>=0;
474 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000475 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000476 // FIXME: This should probably be:
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000477 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
478 regsReserved.set(*SubRegs);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000479 }
480 }
Lang Hames1ce837a2012-02-14 19:17:48 +0000481
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000482 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000483
484 // Build a set of the basic blocks in the function.
485 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000486 for (const auto &MBB : *MF) {
487 FunctionBlocks.insert(&MBB);
488 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000489
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000490 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
491 if (MInfo.Preds.size() != MBB.pred_size())
492 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000493
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000494 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
495 if (MInfo.Succs.size() != MBB.succ_size())
496 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000497 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000498
499 // Check that the register use lists are sane.
500 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000501
502 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000503}
504
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000505// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000506static bool matchPair(MachineBasicBlock::const_succ_iterator i,
507 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000508 if (*i == a)
509 return *++i == b;
510 if (*i == b)
511 return *++i == a;
512 return false;
513}
514
515void
516MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000517 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000518
Lang Hames1ce837a2012-02-14 19:17:48 +0000519 if (MRI->isSSA()) {
520 // If this block has allocatable physical registers live-in, check that
521 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000522 for (const auto &LI : MBB->liveins()) {
523 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Lang Hames1ce837a2012-02-14 19:17:48 +0000524 MBB != MBB->getParent()->begin()) {
525 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
526 }
527 }
528 }
529
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000530 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000531 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000532 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000533 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000534 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000535 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000536 if (!FunctionBlocks.count(*I))
537 report("MBB has successor that isn't part of the function.", MBB);
538 if (!MBBInfoMap[*I].Preds.count(MBB)) {
539 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000540 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000541 << (*I)->getNumber() << ".\n";
542 }
543 }
544
545 // Check the predecessor list.
546 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
547 E = MBB->pred_end(); I != E; ++I) {
548 if (!FunctionBlocks.count(*I))
549 report("MBB has predecessor that isn't part of the function.", MBB);
550 if (!MBBInfoMap[*I].Succs.count(MBB)) {
551 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000552 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000553 << (*I)->getNumber() << ".\n";
554 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000555 }
Bill Wendling2a401312011-05-04 22:54:05 +0000556
557 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
558 const BasicBlock *BB = MBB->getBasicBlock();
Reid Kleckner64b003f2015-11-09 21:04:00 +0000559 const Function *Fn = MF->getFunction();
Bill Wendling2a401312011-05-04 22:54:05 +0000560 if (LandingPadSuccs.size() > 1 &&
561 !(AsmInfo &&
562 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
Reid Kleckner64b003f2015-11-09 21:04:00 +0000563 BB && isa<SwitchInst>(BB->getTerminator())) &&
564 !isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000565 report("MBB has more than one landing pad successor", MBB);
566
Dan Gohman352a4952009-08-27 02:43:49 +0000567 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000568 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000569 SmallVector<MachineOperand, 4> Cond;
570 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
571 TBB, FBB, Cond)) {
572 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
573 // check whether its answers match up with reality.
574 if (!TBB && !FBB) {
575 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000576 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000577 ++MBBI;
578 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000579 // It's possible that the block legitimately ends with a noreturn
580 // call or an unreachable, in which case it won't actually fall
581 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000582 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000583 // It's possible that the block legitimately ends with a noreturn
584 // call or an unreachable, in which case it won't actuall fall
585 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000586 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000587 report("MBB exits via unconditional fall-through but doesn't have "
588 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000589 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000590 report("MBB exits via unconditional fall-through but its successor "
591 "differs from its CFG successor!", MBB);
592 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000593 if (!MBB->empty() && MBB->back().isBarrier() &&
594 !TII->isPredicated(&MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000595 report("MBB exits via unconditional fall-through but ends with a "
596 "barrier instruction!", MBB);
597 }
598 if (!Cond.empty()) {
599 report("MBB exits via unconditional fall-through but has a condition!",
600 MBB);
601 }
602 } else if (TBB && !FBB && Cond.empty()) {
603 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000604 // If the block has exactly one successor, that happens to be a
605 // landingpad, accept it as valid control flow.
606 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
607 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
608 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000609 report("MBB exits via unconditional branch but doesn't have "
610 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000611 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000612 report("MBB exits via unconditional branch but the CFG "
613 "successor doesn't match the actual successor!", MBB);
614 }
615 if (MBB->empty()) {
616 report("MBB exits via unconditional branch but doesn't contain "
617 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000618 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000619 report("MBB exits via unconditional branch but doesn't end with a "
620 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000621 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000622 report("MBB exits via unconditional branch but the branch isn't a "
623 "terminator instruction!", MBB);
624 }
625 } else if (TBB && !FBB && !Cond.empty()) {
626 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000627 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000628 ++MBBI;
629 if (MBBI == MF->end()) {
630 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000631 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000632 // A conditional branch with only one successor is weird, but allowed.
633 if (&*MBBI != TBB)
634 report("MBB exits via conditional branch/fall-through but only has "
635 "one CFG successor!", MBB);
636 else if (TBB != *MBB->succ_begin())
637 report("MBB exits via conditional branch/fall-through but the CFG "
638 "successor don't match the actual successor!", MBB);
639 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000640 report("MBB exits via conditional branch/fall-through but doesn't have "
641 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000642 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000643 report("MBB exits via conditional branch/fall-through but the CFG "
644 "successors don't match the actual successors!", MBB);
645 }
646 if (MBB->empty()) {
647 report("MBB exits via conditional branch/fall-through but doesn't "
648 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000649 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000650 report("MBB exits via conditional branch/fall-through but ends with a "
651 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000652 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000653 report("MBB exits via conditional branch/fall-through but the branch "
654 "isn't a terminator instruction!", MBB);
655 }
656 } else if (TBB && FBB) {
657 // Block conditionally branches somewhere, otherwise branches
658 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000659 if (MBB->succ_size() == 1) {
660 // A conditional branch with only one successor is weird, but allowed.
661 if (FBB != TBB)
662 report("MBB exits via conditional branch/branch through but only has "
663 "one CFG successor!", MBB);
664 else if (TBB != *MBB->succ_begin())
665 report("MBB exits via conditional branch/branch through but the CFG "
666 "successor don't match the actual successor!", MBB);
667 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000668 report("MBB exits via conditional branch/branch but doesn't have "
669 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000670 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000671 report("MBB exits via conditional branch/branch but the CFG "
672 "successors don't match the actual successors!", MBB);
673 }
674 if (MBB->empty()) {
675 report("MBB exits via conditional branch/branch but doesn't "
676 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000677 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000678 report("MBB exits via conditional branch/branch but doesn't end with a "
679 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000680 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000681 report("MBB exits via conditional branch/branch but the branch "
682 "isn't a terminator instruction!", MBB);
683 }
684 if (Cond.empty()) {
685 report("MBB exits via conditinal branch/branch but there's no "
686 "condition!", MBB);
687 }
688 } else {
689 report("AnalyzeBranch returned invalid data!", MBB);
690 }
691 }
692
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000693 regsLive.clear();
Matthias Braund9da1622015-09-09 18:08:03 +0000694 for (const auto &LI : MBB->liveins()) {
695 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000696 report("MBB live-in list contains non-physical register", MBB);
697 continue;
698 }
Matthias Braund9da1622015-09-09 18:08:03 +0000699 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
Chad Rosierabdb1d62013-05-22 23:17:36 +0000700 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000701 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000702 }
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +0000703 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000704
705 const MachineFrameInfo *MFI = MF->getFrameInfo();
706 assert(MFI && "Function has no frame info");
Matthias Braun111f5d82015-05-28 23:20:35 +0000707 BitVector PR = MFI->getPristineRegs(*MF);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000708 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000709 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
710 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000711 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000712 }
713
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000714 regsKilled.clear();
715 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000716
717 if (Indexes)
718 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000719}
720
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000721// This function gets called for all bundle headers, including normal
722// stand-alone unbundled instructions.
723void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
724 if (Indexes && Indexes->hasIndex(MI)) {
725 SlotIndex idx = Indexes->getInstructionIndex(MI);
726 if (!(idx > lastIndex)) {
727 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000728 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000729 }
730 lastIndex = idx;
731 }
Pete Coopercd720162012-06-07 17:41:39 +0000732
733 // Ensure non-terminators don't follow terminators.
734 // Ignore predicated terminators formed by if conversion.
735 // FIXME: If conversion shouldn't need to violate this rule.
736 if (MI->isTerminator() && !TII->isPredicated(MI)) {
737 if (!FirstTerminator)
738 FirstTerminator = MI;
739 } else if (FirstTerminator) {
740 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000741 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000742 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000743}
744
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000745// The operands on an INLINEASM instruction must follow a template.
746// Verify that the flag operands make sense.
747void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
748 // The first two operands on INLINEASM are the asm string and global flags.
749 if (MI->getNumOperands() < 2) {
750 report("Too few operands on inline asm", MI);
751 return;
752 }
753 if (!MI->getOperand(0).isSymbol())
754 report("Asm string must be an external symbol", MI);
755 if (!MI->getOperand(1).isImm())
756 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000757 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
758 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
759 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000760 report("Unknown asm flags", &MI->getOperand(1), 1);
761
Gabor Horvathfee04342015-03-16 09:53:42 +0000762 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000763
764 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
765 unsigned NumOps;
766 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
767 const MachineOperand &MO = MI->getOperand(OpNo);
768 // There may be implicit ops after the fixed operands.
769 if (!MO.isImm())
770 break;
771 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
772 }
773
774 if (OpNo > MI->getNumOperands())
775 report("Missing operands in last group", MI);
776
777 // An optional MDNode follows the groups.
778 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
779 ++OpNo;
780
781 // All trailing operands must be implicit registers.
782 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
783 const MachineOperand &MO = MI->getOperand(OpNo);
784 if (!MO.isReg() || !MO.isImplicit())
785 report("Expected implicit register after groups", &MO, OpNo);
786 }
787}
788
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000789void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000790 const MCInstrDesc &MCID = MI->getDesc();
791 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000792 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000793 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000794 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000795 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000796
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000797 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000798 if (MI->isInlineAsm())
799 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000800
Dan Gohmandb9493c2009-10-07 17:36:00 +0000801 // Check the MachineMemOperands for basic consistency.
802 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
803 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000804 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000805 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000806 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000807 report("Missing mayStore flag", MI);
808 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000809
810 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000811 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000812 if (LiveInts) {
813 bool mapped = !LiveInts->isNotInMIMap(MI);
814 if (MI->isDebugValue()) {
815 if (mapped)
816 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000817 } else if (MI->isInsideBundle()) {
818 if (mapped)
819 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000820 } else {
821 if (!mapped)
822 report("Missing slot index", MI);
823 }
824 }
825
Andrew Trick924123a2011-09-21 02:20:46 +0000826 StringRef ErrorInfo;
827 if (!TII->verifyInstruction(MI, ErrorInfo))
828 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000829}
830
831void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000832MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000833 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000834 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +0000835 unsigned NumDefs = MCID.getNumDefs();
836 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
837 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000838
Evan Cheng6cc775f2011-06-28 19:10:37 +0000839 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +0000840 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000841 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000842 if (!MO->isReg())
843 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +0000844 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000845 report("Explicit definition marked as use", MO, MONum);
846 else if (MO->isImplicit())
847 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000848 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000849 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +0000850 // Don't check if it's the last operand in a variadic instruction. See,
851 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000852 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000853 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000854 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +0000855 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000856 if (MO->isImplicit())
857 report("Explicit operand marked as implicit", MO, MONum);
858 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000859
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000860 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
861 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000862 if (!MO->isReg())
863 report("Tied use must be a register", MO, MONum);
864 else if (!MO->isTied())
865 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000866 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
867 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000868 } else if (MO->isReg() && MO->isTied())
869 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000870 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +0000871 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000872 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000873 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000874 }
875
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000876 switch (MO->getType()) {
877 case MachineOperand::MO_Register: {
878 const unsigned Reg = MO->getReg();
879 if (!Reg)
880 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000881 if (MRI->tracksLiveness() && !MI->isDebugValue())
882 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000883
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000884 // Verify the consistency of tied operands.
885 if (MO->isTied()) {
886 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
887 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
888 if (!OtherMO.isReg())
889 report("Must be tied to a register", MO, MONum);
890 if (!OtherMO.isTied())
891 report("Missing tie flags on tied operand", MO, MONum);
892 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
893 report("Inconsistent tie links", MO, MONum);
894 if (MONum < MCID.getNumDefs()) {
895 if (OtherIdx < MCID.getNumOperands()) {
896 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
897 report("Explicit def tied to explicit use without tie constraint",
898 MO, MONum);
899 } else {
900 if (!OtherMO.isImplicit())
901 report("Explicit def should be tied to implicit use", MO, MONum);
902 }
903 }
904 }
905
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +0000906 // Verify two-address constraints after leaving SSA form.
907 unsigned DefIdx;
908 if (!MRI->isSSA() && MO->isUse() &&
909 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
910 Reg != MI->getOperand(DefIdx).getReg())
911 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000912
913 // Check register classes.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000914 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000915 unsigned SubIdx = MO->getSubReg();
916
917 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000918 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000919 report("Illegal subregister index for physical register", MO, MONum);
920 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000921 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000922 if (const TargetRegisterClass *DRC =
923 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000924 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000925 report("Illegal physical register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000926 errs() << TRI->getName(Reg) << " is not a "
Craig Toppercf0444b2014-11-17 05:50:14 +0000927 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000928 }
929 }
930 } else {
931 // Virtual register.
932 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
933 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000934 const TargetRegisterClass *SRC =
935 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +0000936 if (!SRC) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000937 report("Invalid subregister index for virtual register", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000938 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +0000939 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000940 return;
941 }
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000942 if (RC != SRC) {
943 report("Invalid register class for subregister index", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000944 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000945 << " does not fully support subreg index " << SubIdx << "\n";
946 return;
947 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000948 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000949 if (const TargetRegisterClass *DRC =
950 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000951 if (SubIdx) {
952 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +0000953 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000954 if (!SuperRC) {
955 report("No largest legal super class exists.", MO, MONum);
956 return;
957 }
958 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
959 if (!DRC) {
960 report("No matching super-reg register class.", MO, MONum);
961 return;
962 }
963 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +0000964 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000965 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000966 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +0000967 << " register, but got a " << TRI->getRegClassName(RC)
968 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000969 }
970 }
971 }
972 }
973 break;
974 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000975
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000976 case MachineOperand::MO_RegisterMask:
977 regMasks.push_back(MO->getRegMask());
978 break;
979
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000980 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +0000981 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
982 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +0000983 break;
984
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000985 case MachineOperand::MO_FrameIndex:
986 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
987 LiveInts && !LiveInts->isNotInMIMap(MI)) {
Jonas Paulsson72640f12015-10-29 08:28:35 +0000988 int FI = MO->getIndex();
989 LiveInterval &LI = LiveStks->getInterval(FI);
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000990 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +0000991
Jonas Paulsson17ad0452015-10-21 07:39:47 +0000992 bool stores = MI->mayStore();
Jonas Paulsson72640f12015-10-29 08:28:35 +0000993 bool loads = MI->mayLoad();
994 // For a memory-to-memory move, we need to check if the frame
995 // index is used for storing or loading, by inspecting the
996 // memory operands.
997 if (stores && loads) {
998 for (auto *MMO : MI->memoperands()) {
999 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1000 if (PSV == nullptr) continue;
1001 const FixedStackPseudoSourceValue *Value =
1002 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1003 if (Value == nullptr) continue;
1004 if (Value->getFrameIndex() != FI) continue;
1005
1006 if (MMO->isStore())
1007 loads = false;
1008 else
1009 stores = false;
1010 break;
1011 }
1012 if (loads == stores)
1013 report("Missing fixed stack memoperand.", MI);
1014 }
1015 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001016 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001017 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001018 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001019 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001020 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001021 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001022 }
1023 }
1024 break;
1025
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001026 default:
1027 break;
1028 }
1029}
1030
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001031void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1032 const MachineInstr *MI = MO->getParent();
1033 const unsigned Reg = MO->getReg();
1034
1035 // Both use and def operands can read a register.
1036 if (MO->readsReg()) {
1037 regsLiveInButUnused.erase(Reg);
1038
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001039 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001040 addRegWithSubRegs(regsKilled, Reg);
1041
1042 // Check that LiveVars knows this kill.
1043 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1044 MO->isKill()) {
1045 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1046 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
1047 report("Kill missing from LiveVariables", MO, MONum);
1048 }
1049
1050 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001051 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
1052 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
1053 // Check the cached regunit intervals.
1054 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1055 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun34e1be92013-10-10 21:29:02 +00001056 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units)) {
1057 LiveQueryResult LRQ = LR->Query(UseIdx);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001058 if (!LRQ.valueIn()) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001059 report("No live segment at use", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001060 errs() << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
Matthias Braun34e1be92013-10-10 21:29:02 +00001061 << ' ' << *LR << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001062 }
1063 if (MO->isKill() && !LRQ.isKill()) {
1064 report("Live range continues after kill flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001065 errs() << PrintRegUnit(*Units, TRI) << ' ' << *LR << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001066 }
1067 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001068 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001069 }
1070
1071 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1072 if (LiveInts->hasInterval(Reg)) {
1073 // This is a virtual register interval.
1074 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun88dd0ab2013-10-10 21:28:52 +00001075 LiveQueryResult LRQ = LI.Query(UseIdx);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001076 if (!LRQ.valueIn()) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001077 report("No live segment at use", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001078 errs() << UseIdx << " is not live in " << LI << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001079 }
1080 // Check for extra kill flags.
1081 // Note that we allow missing kill flags for now.
1082 if (MO->isKill() && !LRQ.isKill()) {
1083 report("Live range continues after kill flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001084 errs() << "Live range: " << LI << '\n';
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001085 }
1086 } else {
1087 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001088 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001089 }
1090 }
1091
1092 // Use of a dead register.
1093 if (!regsLive.count(Reg)) {
1094 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1095 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001096 bool Bad = !isReserved(Reg);
1097 // We are fine if just any subregister has a defined value.
1098 if (Bad) {
1099 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1100 ++SubRegs) {
1101 if (regsLive.count(*SubRegs)) {
1102 Bad = false;
1103 break;
1104 }
1105 }
1106 }
Matthias Braun96a31952015-01-14 22:25:14 +00001107 // If there is an additional implicit-use of a super register we stop
1108 // here. By definition we are fine if the super register is not
1109 // (completely) dead, if the complete super register is dead we will
1110 // get a report for its operand.
1111 if (Bad) {
1112 for (const MachineOperand &MOP : MI->uses()) {
1113 if (!MOP.isReg())
1114 continue;
1115 if (!MOP.isImplicit())
1116 continue;
1117 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1118 ++SubRegs) {
1119 if (*SubRegs == Reg) {
1120 Bad = false;
1121 break;
1122 }
1123 }
1124 }
1125 }
Matthias Braun96d77322014-12-10 01:13:13 +00001126 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001127 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001128 } else if (MRI->def_empty(Reg)) {
1129 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001130 } else {
1131 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1132 // We don't know which virtual registers are live in, so only complain
1133 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1134 // must be live in. PHI instructions are handled separately.
1135 if (MInfo.regsKilled.count(Reg))
1136 report("Using a killed virtual register", MO, MONum);
1137 else if (!MI->isPHI())
1138 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1139 }
1140 }
1141 }
1142
1143 if (MO->isDef()) {
1144 // Register defined.
1145 // TODO: verify that earlyclobber ops are not used.
1146 if (MO->isDead())
1147 addRegWithSubRegs(regsDead, Reg);
1148 else
1149 addRegWithSubRegs(regsDefined, Reg);
1150
1151 // Verify SSA form.
1152 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001153 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001154 report("Multiple virtual register defs in SSA form", MO, MONum);
1155
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001156 // Check LiveInts for a live segment, but only for virtual registers.
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001157 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1158 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001159 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1160 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001161 if (LiveInts->hasInterval(Reg)) {
1162 const LiveInterval &LI = LiveInts->getInterval(Reg);
1163 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1164 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001165 if (VNI->def != DefIdx) {
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001166 report("Inconsistent valno->def", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001167 errs() << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001168 << DefIdx << " in " << LI << '\n';
1169 }
1170 } else {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001171 report("No live segment at def", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001172 errs() << DefIdx << " is not live in " << LI << '\n';
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001173 }
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001174 // Check that, if the dead def flag is present, LiveInts agree.
1175 if (MO->isDead()) {
1176 LiveQueryResult LRQ = LI.Query(DefIdx);
1177 if (!LRQ.isDeadDef()) {
1178 report("Live range continues after dead def flag", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001179 errs() << "Live range: " << LI << '\n';
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001180 }
1181 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001182 } else {
1183 report("Virtual register has no Live interval", MO, MONum);
1184 }
1185 }
1186 }
1187}
1188
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001189void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001190}
1191
1192// This function gets called after visiting all instructions in a bundle. The
1193// argument points to the bundle header.
1194// Normal stand-alone instructions are also considered 'bundles', and this
1195// function is called for all of them.
1196void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001197 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1198 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001199 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001200 // Kill any masked registers.
1201 while (!regMasks.empty()) {
1202 const uint32_t *Mask = regMasks.pop_back_val();
1203 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1204 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1205 MachineOperand::clobbersPhysReg(Mask, *I))
1206 regsDead.push_back(*I);
1207 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001208 set_subtract(regsLive, regsDead); regsDead.clear();
1209 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001210}
1211
1212void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001213MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001214 MBBInfoMap[MBB].regsLiveOut = regsLive;
1215 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001216
1217 if (Indexes) {
1218 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1219 if (!(stop > lastIndex)) {
1220 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001221 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001222 << " last instruction was at " << lastIndex << '\n';
1223 }
1224 lastIndex = stop;
1225 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001226}
1227
1228// Calculate the largest possible vregsPassed sets. These are the registers that
1229// can pass through an MBB live, but may not be live every time. It is assumed
1230// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001231void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001232 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1233 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001234 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001235 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001236 BBInfo &MInfo = MBBInfoMap[&MBB];
1237 if (!MInfo.reachable)
1238 continue;
1239 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1240 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1241 BBInfo &SInfo = MBBInfoMap[*SuI];
1242 if (SInfo.addPassed(MInfo.regsLiveOut))
1243 todo.insert(*SuI);
1244 }
1245 }
1246
1247 // Iteratively push vregsPassed to successors. This will converge to the same
1248 // final state regardless of DenseSet iteration order.
1249 while (!todo.empty()) {
1250 const MachineBasicBlock *MBB = *todo.begin();
1251 todo.erase(MBB);
1252 BBInfo &MInfo = MBBInfoMap[MBB];
1253 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1254 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1255 if (*SuI == MBB)
1256 continue;
1257 BBInfo &SInfo = MBBInfoMap[*SuI];
1258 if (SInfo.addPassed(MInfo.vregsPassed))
1259 todo.insert(*SuI);
1260 }
1261 }
1262}
1263
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001264// Calculate the set of virtual registers that must be passed through each basic
1265// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001266// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001267void MachineVerifier::calcRegsRequired() {
1268 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001269 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001270 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001271 BBInfo &MInfo = MBBInfoMap[&MBB];
1272 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1273 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1274 BBInfo &PInfo = MBBInfoMap[*PrI];
1275 if (PInfo.addRequired(MInfo.vregsLiveIn))
1276 todo.insert(*PrI);
1277 }
1278 }
1279
1280 // Iteratively push vregsRequired to predecessors. This will converge to the
1281 // same final state regardless of DenseSet iteration order.
1282 while (!todo.empty()) {
1283 const MachineBasicBlock *MBB = *todo.begin();
1284 todo.erase(MBB);
1285 BBInfo &MInfo = MBBInfoMap[MBB];
1286 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1287 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1288 if (*PrI == MBB)
1289 continue;
1290 BBInfo &SInfo = MBBInfoMap[*PrI];
1291 if (SInfo.addRequired(MInfo.vregsRequired))
1292 todo.insert(*PrI);
1293 }
1294 }
1295}
1296
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001297// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001298// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001299void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001300 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001301 for (const auto &BBI : *MBB) {
1302 if (!BBI.isPHI())
1303 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001304 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001305
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001306 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1307 unsigned Reg = BBI.getOperand(i).getReg();
1308 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001309 if (!Pre->isSuccessor(MBB))
1310 continue;
1311 seen.insert(Pre);
1312 BBInfo &PrInfo = MBBInfoMap[Pre];
1313 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1314 report("PHI operand is not live-out from predecessor",
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001315 &BBI.getOperand(i), i);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001316 }
1317
1318 // Did we see all predecessors?
1319 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1320 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1321 if (!seen.count(*PrI)) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001322 report("Missing PHI operand", &BBI);
Owen Anderson21b17882015-02-04 00:02:59 +00001323 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001324 << " is a predecessor according to the CFG.\n";
1325 }
1326 }
1327 }
1328}
1329
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001330void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001331 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001332
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001333 for (const auto &MBB : *MF) {
1334 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001335
1336 // Skip unreachable MBBs.
1337 if (!MInfo.reachable)
1338 continue;
1339
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001340 checkPHIOps(&MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001341 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001342
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001343 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001344 calcRegsRequired();
1345
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001346 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001347 for (const auto &MBB : *MF) {
1348 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001349 for (RegSet::iterator
1350 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1351 ++I)
1352 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001353 report("Virtual register killed in block, but needed live out.", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001354 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001355 << " is used after the block.\n";
1356 }
1357 }
1358
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001359 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001360 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1361 for (RegSet::iterator
1362 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesen99014ff2012-03-10 00:44:11 +00001363 ++I)
1364 report("Virtual register def doesn't dominate all uses.",
1365 MRI->getVRegDef(*I));
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001366 }
1367
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001368 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001369 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001370 if (LiveInts)
1371 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001372}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001373
1374void MachineVerifier::verifyLiveVariables() {
1375 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001376 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1377 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001378 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001379 for (const auto &MBB : *MF) {
1380 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001381
1382 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1383 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001384 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1385 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001386 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001387 << " must be live through the block.\n";
1388 }
1389 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001390 if (VI.AliveBlocks.test(MBB.getNumber())) {
1391 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001392 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001393 << " is not needed live through the block.\n";
1394 }
1395 }
1396 }
1397 }
1398}
1399
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001400void MachineVerifier::verifyLiveIntervals() {
1401 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001402 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1403 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001404
1405 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001406 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001407 continue;
1408
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001409 if (!LiveInts->hasInterval(Reg)) {
1410 report("Missing live interval for virtual register", MF);
Owen Anderson21b17882015-02-04 00:02:59 +00001411 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001412 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001413 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001414
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001415 const LiveInterval &LI = LiveInts->getInterval(Reg);
1416 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001417 verifyLiveInterval(LI);
1418 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001419
1420 // Verify all the cached regunit intervals.
1421 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001422 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1423 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001424}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001425
Matthias Braun364e6e92013-10-10 21:28:54 +00001426void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001427 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001428 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001429 if (VNI->isUnused())
1430 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001431
Matthias Braun364e6e92013-10-10 21:28:54 +00001432 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001433
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001434 if (!DefVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001435 report("Value not live at VNInfo def and not marked unused", MF);
1436 report_context(LR, Reg, LaneMask);
1437 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001438 return;
1439 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001440
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001441 if (DefVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001442 report("Live segment at def has different VNInfo", MF);
1443 report_context(LR, Reg, LaneMask);
1444 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001445 return;
1446 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001447
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001448 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1449 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001450 report("Invalid VNInfo definition index", MF);
1451 report_context(LR, Reg, LaneMask);
1452 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001453 return;
1454 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001455
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001456 if (VNI->isPHIDef()) {
1457 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001458 report("PHIDef VNInfo is not defined at MBB start", MBB);
1459 report_context(LR, Reg, LaneMask);
1460 report_context(*VNI);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001461 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001462 return;
1463 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001464
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001465 // Non-PHI def.
1466 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1467 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001468 report("No instruction at VNInfo def index", MBB);
1469 report_context(LR, Reg, LaneMask);
1470 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001471 return;
1472 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001473
Matthias Braun364e6e92013-10-10 21:28:54 +00001474 if (Reg != 0) {
1475 bool hasDef = false;
1476 bool isEarlyClobber = false;
1477 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1478 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001479 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001480 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1481 if (MOI->getReg() != Reg)
1482 continue;
1483 } else {
1484 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1485 !TRI->hasRegUnit(MOI->getReg(), Reg))
1486 continue;
1487 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001488 if (LaneMask != 0 &&
1489 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask) == 0)
1490 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001491 hasDef = true;
1492 if (MOI->isEarlyClobber())
1493 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001494 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001495
Matthias Braun364e6e92013-10-10 21:28:54 +00001496 if (!hasDef) {
1497 report("Defining instruction does not modify register", MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001498 report_context(LR, Reg, LaneMask);
1499 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001500 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001501
Matthias Braun364e6e92013-10-10 21:28:54 +00001502 // Early clobber defs begin at USE slots, but other defs must begin at
1503 // DEF slots.
1504 if (isEarlyClobber) {
1505 if (!VNI->def.isEarlyClobber()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001506 report("Early clobber def must be at an early-clobber slot", MBB);
1507 report_context(LR, Reg, LaneMask);
1508 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001509 }
1510 } else if (!VNI->def.isRegister()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001511 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
1512 report_context(LR, Reg, LaneMask);
1513 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001514 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001515 }
1516}
1517
Matthias Braun364e6e92013-10-10 21:28:54 +00001518void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1519 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001520 unsigned Reg, LaneBitmask LaneMask)
1521{
Matthias Braun364e6e92013-10-10 21:28:54 +00001522 const LiveRange::Segment &S = *I;
1523 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001524 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001525
Matthias Braun364e6e92013-10-10 21:28:54 +00001526 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001527 report("Foreign valno in live segment", MF);
1528 report_context(LR, Reg, LaneMask);
1529 report_context(S);
1530 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001531 }
1532
1533 if (VNI->isUnused()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001534 report("Live segment valno is marked unused", MF);
1535 report_context(LR, Reg, LaneMask);
1536 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001537 }
1538
Matthias Braun364e6e92013-10-10 21:28:54 +00001539 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001540 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001541 report("Bad start of live segment, no basic block", MF);
1542 report_context(LR, Reg, LaneMask);
1543 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001544 return;
1545 }
1546 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001547 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001548 report("Live segment must begin at MBB entry or valno def", MBB);
1549 report_context(LR, Reg, LaneMask);
1550 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001551 }
1552
1553 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001554 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001555 if (!EndMBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001556 report("Bad end of live segment, no basic block", MF);
1557 report_context(LR, Reg, LaneMask);
1558 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001559 return;
1560 }
1561
1562 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001563 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001564 return;
1565
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001566 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001567 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1568 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001569 return;
1570
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001571 // The live segment is ending inside EndMBB
1572 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001573 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001574 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001575 report("Live segment doesn't end at a valid instruction", EndMBB);
1576 report_context(LR, Reg, LaneMask);
1577 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001578 return;
1579 }
1580
1581 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001582 if (S.end.isBlock()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001583 report("Live segment ends at B slot of an instruction", EndMBB);
1584 report_context(LR, Reg, LaneMask);
1585 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001586 }
1587
Matthias Braun364e6e92013-10-10 21:28:54 +00001588 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001589 // Segment ends on the dead slot.
1590 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001591 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001592 report("Live segment ending at dead slot spans instructions", EndMBB);
1593 report_context(LR, Reg, LaneMask);
1594 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001595 }
1596 }
1597
1598 // A live segment can only end at an early-clobber slot if it is being
1599 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001600 if (S.end.isEarlyClobber()) {
1601 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001602 report("Live segment ending at early clobber slot must be "
Matthias Braun7e624d52015-11-09 23:59:33 +00001603 "redefined by an EC def in the same instruction", EndMBB);
1604 report_context(LR, Reg, LaneMask);
1605 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001606 }
1607 }
1608
1609 // The following checks only apply to virtual registers. Physreg liveness
1610 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001611 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001612 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001613 // use, or a dead flag on a def.
1614 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001615 bool hasSubRegDef = false;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001616 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001617 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001618 continue;
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001619 if (LaneMask != 0 &&
1620 (LaneMask & TRI->getSubRegIndexLaneMask(MOI->getSubReg())) == 0)
1621 continue;
Matthias Braun21554d92014-12-10 01:13:11 +00001622 if (MOI->isDef() && MOI->getSubReg() != 0)
1623 hasSubRegDef = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001624 if (MOI->readsReg())
1625 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001626 }
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001627 if (!S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001628 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001629 // When tracking subregister liveness, the main range must start new
1630 // values on partial register writes, even if there is no read.
Matthias Brauna25e13a2015-03-19 00:21:58 +00001631 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask != 0 ||
1632 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001633 report("Instruction ending live segment doesn't read the register",
1634 MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001635 report_context(LR, Reg, LaneMask);
1636 report_context(S);
Matthias Braun21554d92014-12-10 01:13:11 +00001637 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001638 }
1639 }
1640 }
1641
1642 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001643 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001644 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00001645 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001646 // Not live-in to any blocks.
1647 if (MBB == EndMBB)
1648 return;
1649 // Skip this block.
1650 ++MFI;
1651 }
1652 for (;;) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001653 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001654 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00001655 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00001656 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001657 if (&*MFI == EndMBB)
1658 break;
1659 ++MFI;
1660 continue;
1661 }
1662
1663 // Is VNI a PHI-def in the current block?
1664 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001665 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001666
1667 // Check that VNI is live-out of all predecessors.
1668 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1669 PE = MFI->pred_end(); PI != PE; ++PI) {
1670 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001671 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001672
1673 // All predecessors must have a live-out value.
1674 if (!PVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001675 report("Register not marked live out of predecessor", *PI);
1676 report_context(LR, Reg, LaneMask);
1677 report_context(*VNI);
1678 errs() << " live into BB#" << MFI->getNumber()
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001679 << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
1680 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001681 continue;
1682 }
1683
1684 // Only PHI-defs can take different predecessor values.
1685 if (!IsPHI && PVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001686 report("Different value live out of predecessor", *PI);
1687 report_context(LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001688 errs() << "Valno #" << PVNI->id << " live out of BB#"
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001689 << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
1690 << " live into BB#" << MFI->getNumber() << '@'
1691 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001692 }
1693 }
1694 if (&*MFI == EndMBB)
1695 break;
1696 ++MFI;
1697 }
1698}
1699
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001700void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001701 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00001702 for (const VNInfo *VNI : LR.valnos)
1703 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001704
Matthias Braun364e6e92013-10-10 21:28:54 +00001705 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001706 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00001707}
1708
1709void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001710 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00001711 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1712 verifyLiveRange(LI, Reg);
1713
Matthias Braune6a24852015-09-25 21:51:14 +00001714 LaneBitmask Mask = 0;
1715 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00001716 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001717 if ((Mask & SR.LaneMask) != 0) {
1718 report("Lane masks of sub ranges overlap in live interval", MF);
1719 report_context(LI);
1720 }
1721 if ((SR.LaneMask & ~MaxMask) != 0) {
1722 report("Subrange lanemask is invalid", MF);
1723 report_context(LI);
1724 }
1725 if (SR.empty()) {
1726 report("Subrange must not be empty", MF);
1727 report_context(SR, LI.reg, SR.LaneMask);
1728 }
Matthias Braune962e522015-03-25 21:18:22 +00001729 Mask |= SR.LaneMask;
1730 verifyLiveRange(SR, LI.reg, SR.LaneMask);
Matthias Braun7e624d52015-11-09 23:59:33 +00001731 if (!LI.covers(SR)) {
1732 report("A Subrange is not covered by the main range", MF);
1733 report_context(LI);
1734 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001735 }
1736
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001737 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00001738 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1739 unsigned NumComp = ConEQ.Classify(&LI);
1740 if (NumComp > 1) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001741 report("Multiple connected components in live interval", MF);
1742 report_context(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001743 for (unsigned comp = 0; comp != NumComp; ++comp) {
1744 errs() << comp << ": valnos";
1745 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1746 E = LI.vni_end(); I!=E; ++I)
1747 if (comp == ConEQ.getEqClass(*I))
1748 errs() << ' ' << (*I)->id;
1749 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00001750 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001751 }
1752}
Manman Renaa6875b2013-07-15 21:26:31 +00001753
1754namespace {
1755 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1756 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1757 // value is zero.
1758 // We use a bool plus an integer to capture the stack state.
1759 struct StackStateOfBB {
1760 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1761 ExitIsSetup(false) { }
1762 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1763 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1764 ExitIsSetup(ExitSetup) { }
1765 // Can be negative, which means we are setting up a frame.
1766 int EntryValue;
1767 int ExitValue;
1768 bool EntryIsSetup;
1769 bool ExitIsSetup;
1770 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001771}
Manman Renaa6875b2013-07-15 21:26:31 +00001772
1773/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1774/// by a FrameDestroy <n>, stack adjustments are identical on all
1775/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1776void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001777 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1778 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Manman Renaa6875b2013-07-15 21:26:31 +00001779
1780 SmallVector<StackStateOfBB, 8> SPState;
1781 SPState.resize(MF->getNumBlockIDs());
1782 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1783
1784 // Visit the MBBs in DFS order.
1785 for (df_ext_iterator<const MachineFunction*,
1786 SmallPtrSet<const MachineBasicBlock*, 8> >
1787 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1788 DFI != DFE; ++DFI) {
1789 const MachineBasicBlock *MBB = *DFI;
1790
1791 StackStateOfBB BBState;
1792 // Check the exit state of the DFS stack predecessor.
1793 if (DFI.getPathLength() >= 2) {
1794 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1795 assert(Reachable.count(StackPred) &&
1796 "DFS stack predecessor is already visited.\n");
1797 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1798 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1799 BBState.ExitValue = BBState.EntryValue;
1800 BBState.ExitIsSetup = BBState.EntryIsSetup;
1801 }
1802
1803 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001804 for (const auto &I : *MBB) {
1805 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001806 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001807 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001808 assert(Size >= 0 &&
1809 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1810
1811 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001812 report("FrameSetup is after another FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001813 BBState.ExitValue -= Size;
1814 BBState.ExitIsSetup = true;
1815 }
1816
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001817 if (I.getOpcode() == FrameDestroyOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001818 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001819 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001820 assert(Size >= 0 &&
1821 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1822
1823 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001824 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001825 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1826 BBState.ExitValue;
1827 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001828 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00001829 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00001830 << AbsSPAdj << ">.\n";
1831 }
1832 BBState.ExitValue += Size;
1833 BBState.ExitIsSetup = false;
1834 }
1835 }
1836 SPState[MBB->getNumber()] = BBState;
1837
1838 // Make sure the exit state of any predecessor is consistent with the entry
1839 // state.
1840 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
1841 E = MBB->pred_end(); I != E; ++I) {
1842 if (Reachable.count(*I) &&
1843 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
1844 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
1845 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001846 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00001847 << SPState[(*I)->getNumber()].ExitValue << ", "
1848 << SPState[(*I)->getNumber()].ExitIsSetup
1849 << "), while BB#" << MBB->getNumber() << " has entry state ("
1850 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
1851 }
1852 }
1853
1854 // Make sure the entry state of any successor is consistent with the exit
1855 // state.
1856 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
1857 E = MBB->succ_end(); I != E; ++I) {
1858 if (Reachable.count(*I) &&
1859 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
1860 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
1861 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001862 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00001863 << SPState[(*I)->getNumber()].EntryValue << ", "
1864 << SPState[(*I)->getNumber()].EntryIsSetup
1865 << "), while BB#" << MBB->getNumber() << " has exit state ("
1866 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
1867 }
1868 }
1869
1870 // Make sure a basic block with return ends with zero stack adjustment.
1871 if (!MBB->empty() && MBB->back().isReturn()) {
1872 if (BBState.ExitIsSetup)
1873 report("A return block ends with a FrameSetup.", MBB);
1874 if (BBState.ExitValue)
1875 report("A return block ends with a nonzero stack adjustment.", MBB);
1876 }
1877 }
1878}