blob: 15180b52eb1c28ace5db04517f8d4e6d684c0cfe [file] [log] [blame]
Bill Wendling68caaaf2010-08-19 18:52:17 +00001//===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Pass to verify generated machine code. The following is checked:
11//
12// Operand counts: All explicit operands must be present.
13//
14// Register classes: All physical and virtual register operands must be
15// compatible with the register class required by the instruction descriptor.
16//
17// Register live intervals: Registers must be defined only once, and must be
18// defined before use.
19//
20// The machine code verifier is enabled from LLVMTargetMachine.cpp with the
21// command-line option -verify-machineinstrs, or by defining the environment
22// variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
23// the verifier errors.
24//===----------------------------------------------------------------------===//
25
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000026#include "llvm/CodeGen/Passes.h"
Chris Lattner565449d2009-08-23 03:13:20 +000027#include "llvm/ADT/DenseSet.h"
Manman Renaa6875b2013-07-15 21:26:31 +000028#include "llvm/ADT/DepthFirstIterator.h"
Chris Lattner565449d2009-08-23 03:13:20 +000029#include "llvm/ADT/SetOperations.h"
30#include "llvm/ADT/SmallVector.h"
David Majnemer70497c62015-12-02 23:06:39 +000031#include "llvm/Analysis/EHPersonalities.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/CodeGen/LiveIntervalAnalysis.h"
33#include "llvm/CodeGen/LiveStackAnalysis.h"
34#include "llvm/CodeGen/LiveVariables.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/CodeGen/MachineMemOperand.h"
38#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/InlineAsm.h"
41#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000043#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000044#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000045#include "llvm/Support/FileSystem.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000046#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Target/TargetInstrInfo.h"
48#include "llvm/Target/TargetMachine.h"
49#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000050#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000051using namespace llvm;
52
53namespace {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000054 struct MachineVerifier {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000055
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000056 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000057 PASS(pass),
Owen Anderson21b17882015-02-04 00:02:59 +000058 Banner(b)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000059 {}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000060
Matthias Braunb3aefc32016-02-15 19:25:31 +000061 unsigned verify(MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000062
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000063 Pass *const PASS;
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000064 const char *Banner;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000065 const MachineFunction *MF;
66 const TargetMachine *TM;
Evan Cheng8d71a752011-06-27 21:26:13 +000067 const TargetInstrInfo *TII;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000068 const TargetRegisterInfo *TRI;
69 const MachineRegisterInfo *MRI;
70
71 unsigned foundErrors;
72
73 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000074 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000075 typedef DenseSet<unsigned> RegSet;
76 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000077 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000078
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000079 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +000080 BlockSet FunctionBlocks;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +000081
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000082 BitVector regsReserved;
83 RegSet regsLive;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000084 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +000085 RegMaskVector regMasks;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +000086 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000087
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +000088 SlotIndex lastIndex;
89
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000090 // Add Reg and any sub-registers to RV
91 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
92 RV.push_back(Reg);
93 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +000094 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
95 RV.push_back(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000096 }
97
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000098 struct BBInfo {
99 // Is this MBB reachable from the MF entry point?
100 bool reachable;
101
102 // Vregs that must be live in because they are used without being
103 // defined. Map value is the user.
104 RegMap vregsLiveIn;
105
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000106 // Regs killed in MBB. They may be defined again, and will then be in both
107 // regsKilled and regsLiveOut.
108 RegSet regsKilled;
109
110 // Regs defined in MBB and live out. Note that vregs passing through may
111 // be live out without being mentioned here.
112 RegSet regsLiveOut;
113
114 // Vregs that pass through MBB untouched. This set is disjoint from
115 // regsKilled and regsLiveOut.
116 RegSet vregsPassed;
117
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000118 // Vregs that must pass through MBB because they are needed by a successor
119 // block. This set is disjoint from regsLiveOut.
120 RegSet vregsRequired;
121
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000122 // Set versions of block's predecessor and successor lists.
123 BlockSet Preds, Succs;
124
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000125 BBInfo() : reachable(false) {}
126
127 // Add register to vregsPassed if it belongs there. Return true if
128 // anything changed.
129 bool addPassed(unsigned Reg) {
130 if (!TargetRegisterInfo::isVirtualRegister(Reg))
131 return false;
132 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
133 return false;
134 return vregsPassed.insert(Reg).second;
135 }
136
137 // Same for a full set.
138 bool addPassed(const RegSet &RS) {
139 bool changed = false;
140 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
141 if (addPassed(*I))
142 changed = true;
143 return changed;
144 }
145
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000146 // Add register to vregsRequired if it belongs there. Return true if
147 // anything changed.
148 bool addRequired(unsigned Reg) {
149 if (!TargetRegisterInfo::isVirtualRegister(Reg))
150 return false;
151 if (regsLiveOut.count(Reg))
152 return false;
153 return vregsRequired.insert(Reg).second;
154 }
155
156 // Same for a full set.
157 bool addRequired(const RegSet &RS) {
158 bool changed = false;
159 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
160 if (addRequired(*I))
161 changed = true;
162 return changed;
163 }
164
165 // Same for a full map.
166 bool addRequired(const RegMap &RM) {
167 bool changed = false;
168 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
169 if (addRequired(I->first))
170 changed = true;
171 return changed;
172 }
173
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000174 // Live-out registers are either in regsLiveOut or vregsPassed.
175 bool isLiveOut(unsigned Reg) const {
176 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
177 }
178 };
179
180 // Extra register info per MBB.
181 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
182
183 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000184 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000185 }
186
Lang Hames1ce837a2012-02-14 19:17:48 +0000187 bool isAllocatable(unsigned Reg) {
Jakob Stoklund Olesen244beb42012-10-16 00:05:06 +0000188 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
Lang Hames1ce837a2012-02-14 19:17:48 +0000189 }
190
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000191 // Analysis information if available
192 LiveVariables *LiveVars;
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +0000193 LiveIntervals *LiveInts;
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000194 LiveStacks *LiveStks;
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000195 SlotIndexes *Indexes;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000196
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000197 void visitMachineFunctionBefore();
198 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000199 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000200 void visitMachineInstrBefore(const MachineInstr *MI);
201 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
202 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000203 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000204 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
205 void visitMachineFunctionAfter();
206
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000207 template <typename T> void report(const char *msg, ilist_iterator<T> I) {
208 report(msg, &*I);
209 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000210 void report(const char *msg, const MachineFunction *MF);
211 void report(const char *msg, const MachineBasicBlock *MBB);
212 void report(const char *msg, const MachineInstr *MI);
213 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Matthias Braun7e624d52015-11-09 23:59:33 +0000214
215 void report_context(const LiveInterval &LI) const;
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;
Matthias Braun579c9cd2016-02-02 02:44:25 +0000220 void report_context(SlotIndex Pos) const;
221 void report_context_liverange(const LiveRange &LR) const;
222 void report_context_regunit(unsigned RegUnit) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000223 void report_context_lanemask(LaneBitmask LaneMask) const;
224 void report_context_vreg_regunit(unsigned VRegOrRegUnit) const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000225
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000226 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000227
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000228 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Matthias Braun1377fd62016-02-02 20:04:51 +0000229 void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
230 SlotIndex UseIdx, const LiveRange &LR, unsigned Reg,
231 LaneBitmask LaneMask = 0);
232 void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
233 SlotIndex DefIdx, const LiveRange &LR, unsigned Reg,
234 LaneBitmask LaneMask = 0);
235
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000236 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000237 void calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000238 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000239
240 void calcRegsRequired();
241 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000242 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000243 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000244 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
245 unsigned);
Matthias Braun364e6e92013-10-10 21:28:54 +0000246 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000247 const LiveRange::const_iterator I, unsigned,
248 unsigned);
Matthias Braune6a24852015-09-25 21:51:14 +0000249 void verifyLiveRange(const LiveRange&, unsigned, LaneBitmask LaneMask = 0);
Manman Renaa6875b2013-07-15 21:26:31 +0000250
251 void verifyStackFrame();
Matthias Braun80595462015-09-09 17:49:46 +0000252
253 void verifySlotIndexes() const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000254 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000255
256 struct MachineVerifierPass : public MachineFunctionPass {
257 static char ID; // Pass ID, replacement for typeid
Matthias Brauna4e932d2014-12-11 19:41:51 +0000258 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000259
Matthias Brauna4e932d2014-12-11 19:41:51 +0000260 MachineVerifierPass(const std::string &banner = nullptr)
261 : MachineFunctionPass(ID), Banner(banner) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000262 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
263 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000264
Craig Topper4584cd52014-03-07 09:26:03 +0000265 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000266 AU.setPreservesAll();
267 MachineFunctionPass::getAnalysisUsage(AU);
268 }
269
Craig Topper4584cd52014-03-07 09:26:03 +0000270 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Braunb3aefc32016-02-15 19:25:31 +0000271 unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
272 if (FoundErrors)
273 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000274 return false;
275 }
276 };
277
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000278}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000279
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000280char MachineVerifierPass::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +0000281INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000282 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000283
Matthias Brauna4e932d2014-12-11 19:41:51 +0000284FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000285 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000286}
287
Matthias Braunb3aefc32016-02-15 19:25:31 +0000288bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
289 const {
290 MachineFunction &MF = const_cast<MachineFunction&>(*this);
291 unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
292 if (AbortOnErrors && FoundErrors)
293 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
294 return FoundErrors == 0;
Jakob Stoklund Olesen27440e72009-11-13 21:56:09 +0000295}
296
Matthias Braun80595462015-09-09 17:49:46 +0000297void MachineVerifier::verifySlotIndexes() const {
298 if (Indexes == nullptr)
299 return;
300
301 // Ensure the IdxMBB list is sorted by slot indexes.
302 SlotIndex Last;
303 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
304 E = Indexes->MBBIndexEnd(); I != E; ++I) {
305 assert(!Last.isValid() || I->first > Last);
306 Last = I->first;
307 }
308}
309
Matthias Braunb3aefc32016-02-15 19:25:31 +0000310unsigned MachineVerifier::verify(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000311 foundErrors = 0;
312
313 this->MF = &MF;
314 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000315 TII = MF.getSubtarget().getInstrInfo();
316 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000317 MRI = &MF.getRegInfo();
318
Craig Topperc0196b12014-04-14 00:51:57 +0000319 LiveVars = nullptr;
320 LiveInts = nullptr;
321 LiveStks = nullptr;
322 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000323 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000324 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000325 // We don't want to verify LiveVariables if LiveIntervals is available.
326 if (!LiveInts)
327 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000328 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000329 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000330 }
331
Matthias Braun80595462015-09-09 17:49:46 +0000332 verifySlotIndexes();
333
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000334 visitMachineFunctionBefore();
335 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
336 MFI!=MFE; ++MFI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000337 visitMachineBasicBlockBefore(&*MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000338 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000339 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000340 // Do we expect the next instruction to be part of the same bundle?
341 bool InBundle = false;
342
Evan Cheng7fae11b2011-12-14 02:11:42 +0000343 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
344 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000345 if (MBBI->getParent() != &*MFI) {
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000346 report("Bad instruction parent pointer", MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000347 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000348 continue;
349 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000350
351 // Check for consistent bundle flags.
352 if (InBundle && !MBBI->isBundledWithPred())
353 report("Missing BundledPred flag, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000354 "BundledSucc was set on predecessor",
355 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000356 if (!InBundle && MBBI->isBundledWithPred())
357 report("BundledPred flag is set, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000358 "but BundledSucc not set on predecessor",
359 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000360
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000361 // Is this a bundle header?
362 if (!MBBI->isInsideBundle()) {
363 if (CurBundle)
364 visitMachineBundleAfter(CurBundle);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000365 CurBundle = &*MBBI;
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000366 visitMachineBundleBefore(CurBundle);
367 } else if (!CurBundle)
368 report("No bundle header", MBBI);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000369 visitMachineInstrBefore(&*MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000370 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
371 const MachineInstr &MI = *MBBI;
372 const MachineOperand &Op = MI.getOperand(I);
373 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000374 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000375 // functions when replacing operands of a MachineInstr.
376 report("Instruction has operand with wrong parent set", &MI);
377 }
378
379 visitMachineOperand(&Op, I);
380 }
381
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000382 visitMachineInstrAfter(&*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000383
384 // Was this the last bundled instruction?
385 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000386 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000387 if (CurBundle)
388 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000389 if (InBundle)
390 report("BundledSucc flag set on last instruction in block", &MFI->back());
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000391 visitMachineBasicBlockAfter(&*MFI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000392 }
393 visitMachineFunctionAfter();
394
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000395 // Clean up.
396 regsLive.clear();
397 regsDefined.clear();
398 regsDead.clear();
399 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000400 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000401 regsLiveInButUnused.clear();
402 MBBInfoMap.clear();
403
Matthias Braunb3aefc32016-02-15 19:25:31 +0000404 return foundErrors;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000405}
406
Chris Lattner75f40452009-08-23 01:03:30 +0000407void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000408 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000409 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000410 if (!foundErrors++) {
411 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000412 errs() << "# " << Banner << '\n';
Matthias Braun42b4b632015-11-09 23:59:23 +0000413 if (LiveInts != nullptr)
414 LiveInts->print(errs());
415 else
416 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000417 }
Owen Anderson21b17882015-02-04 00:02:59 +0000418 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000419 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000420}
421
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000422void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000423 assert(MBB);
424 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000425 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000426 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000427 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000428 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000429 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000430 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000431 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000432}
433
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000434void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000435 assert(MI);
436 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000437 errs() << "- instruction: ";
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000438 if (Indexes && Indexes->hasIndex(*MI))
439 errs() << Indexes->getInstructionIndex(*MI) << '\t';
Matthias Braun45718db2015-11-09 23:59:25 +0000440 MI->print(errs(), /*SkipOpers=*/true);
Matthias Braun716b4332015-11-09 23:59:29 +0000441 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000442}
443
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000444void MachineVerifier::report(const char *msg,
445 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000446 assert(MO);
447 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000448 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000449 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000450 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000451}
452
Matthias Braun579c9cd2016-02-02 02:44:25 +0000453void MachineVerifier::report_context(SlotIndex Pos) const {
454 errs() << "- at: " << Pos << '\n';
455}
456
Matthias Braun7e624d52015-11-09 23:59:33 +0000457void MachineVerifier::report_context(const LiveInterval &LI) const {
Owen Anderson21b17882015-02-04 00:02:59 +0000458 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000459}
460
Matthias Braun7e624d52015-11-09 23:59:33 +0000461void MachineVerifier::report_context(const LiveRange &LR, unsigned Reg,
462 LaneBitmask LaneMask) const {
Matthias Braun579c9cd2016-02-02 02:44:25 +0000463 report_context_liverange(LR);
Owen Anderson21b17882015-02-04 00:02:59 +0000464 errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000465 if (LaneMask != 0)
Matthias Braun1377fd62016-02-02 20:04:51 +0000466 report_context_lanemask(LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000467}
468
Matthias Braun7e624d52015-11-09 23:59:33 +0000469void MachineVerifier::report_context(const LiveRange::Segment &S) const {
470 errs() << "- segment: " << S << '\n';
471}
472
473void MachineVerifier::report_context(const VNInfo &VNI) const {
474 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
Matthias Braun364e6e92013-10-10 21:28:54 +0000475}
476
Matthias Braun579c9cd2016-02-02 02:44:25 +0000477void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
478 errs() << "- liverange: " << LR << '\n';
479}
480
481void MachineVerifier::report_context_regunit(unsigned RegUnit) const {
482 errs() << "- regunit: " << PrintRegUnit(RegUnit, TRI) << '\n';
483}
484
Matthias Braun1377fd62016-02-02 20:04:51 +0000485void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
486 if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
487 errs() << "- v. register: " << PrintReg(VRegOrUnit, TRI) << '\n';
488 } else {
489 errs() << "- regunit: " << PrintRegUnit(VRegOrUnit, TRI) << '\n';
490 }
491}
492
493void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
494 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
495}
496
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000497void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000498 BBInfo &MInfo = MBBInfoMap[MBB];
499 if (!MInfo.reachable) {
500 MInfo.reachable = true;
501 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
502 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
503 markReachable(*SuI);
504 }
505}
506
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000507void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000508 lastIndex = SlotIndex();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000509 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000510
511 // A sub-register of a reserved register is also reserved
512 for (int Reg = regsReserved.find_first(); Reg>=0;
513 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000514 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000515 // FIXME: This should probably be:
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000516 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
517 regsReserved.set(*SubRegs);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000518 }
519 }
Lang Hames1ce837a2012-02-14 19:17:48 +0000520
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000521 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000522
523 // Build a set of the basic blocks in the function.
524 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000525 for (const auto &MBB : *MF) {
526 FunctionBlocks.insert(&MBB);
527 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000528
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000529 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
530 if (MInfo.Preds.size() != MBB.pred_size())
531 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000532
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000533 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
534 if (MInfo.Succs.size() != MBB.succ_size())
535 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000536 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000537
538 // Check that the register use lists are sane.
539 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000540
541 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000542}
543
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000544// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000545static bool matchPair(MachineBasicBlock::const_succ_iterator i,
546 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000547 if (*i == a)
548 return *++i == b;
549 if (*i == b)
550 return *++i == a;
551 return false;
552}
553
554void
555MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000556 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000557
Lang Hames1ce837a2012-02-14 19:17:48 +0000558 if (MRI->isSSA()) {
559 // If this block has allocatable physical registers live-in, check that
560 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000561 for (const auto &LI : MBB->liveins()) {
562 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +0000563 MBB->getIterator() != MBB->getParent()->begin()) {
Lang Hames1ce837a2012-02-14 19:17:48 +0000564 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
565 }
566 }
567 }
568
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000569 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000570 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000571 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000572 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000573 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000574 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000575 if (!FunctionBlocks.count(*I))
576 report("MBB has successor that isn't part of the function.", MBB);
577 if (!MBBInfoMap[*I].Preds.count(MBB)) {
578 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000579 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000580 << (*I)->getNumber() << ".\n";
581 }
582 }
583
584 // Check the predecessor list.
585 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
586 E = MBB->pred_end(); I != E; ++I) {
587 if (!FunctionBlocks.count(*I))
588 report("MBB has predecessor that isn't part of the function.", MBB);
589 if (!MBBInfoMap[*I].Succs.count(MBB)) {
590 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000591 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000592 << (*I)->getNumber() << ".\n";
593 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000594 }
Bill Wendling2a401312011-05-04 22:54:05 +0000595
596 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
597 const BasicBlock *BB = MBB->getBasicBlock();
Reid Kleckner64b003f2015-11-09 21:04:00 +0000598 const Function *Fn = MF->getFunction();
Bill Wendling2a401312011-05-04 22:54:05 +0000599 if (LandingPadSuccs.size() > 1 &&
600 !(AsmInfo &&
601 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
Reid Kleckner64b003f2015-11-09 21:04:00 +0000602 BB && isa<SwitchInst>(BB->getTerminator())) &&
603 !isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000604 report("MBB has more than one landing pad successor", MBB);
605
Dan Gohman352a4952009-08-27 02:43:49 +0000606 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000607 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000608 SmallVector<MachineOperand, 4> Cond;
609 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
610 TBB, FBB, Cond)) {
611 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
612 // check whether its answers match up with reality.
613 if (!TBB && !FBB) {
614 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000615 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000616 ++MBBI;
617 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000618 // It's possible that the block legitimately ends with a noreturn
619 // call or an unreachable, in which case it won't actually fall
620 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000621 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000622 // It's possible that the block legitimately ends with a noreturn
623 // call or an unreachable, in which case it won't actuall fall
624 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000625 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000626 report("MBB exits via unconditional fall-through but doesn't have "
627 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000628 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000629 report("MBB exits via unconditional fall-through but its successor "
630 "differs from its CFG successor!", MBB);
631 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000632 if (!MBB->empty() && MBB->back().isBarrier() &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000633 !TII->isPredicated(MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000634 report("MBB exits via unconditional fall-through but ends with a "
635 "barrier instruction!", MBB);
636 }
637 if (!Cond.empty()) {
638 report("MBB exits via unconditional fall-through but has a condition!",
639 MBB);
640 }
641 } else if (TBB && !FBB && Cond.empty()) {
642 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000643 // If the block has exactly one successor, that happens to be a
644 // landingpad, accept it as valid control flow.
645 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
646 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
647 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000648 report("MBB exits via unconditional branch but doesn't have "
649 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000650 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000651 report("MBB exits via unconditional branch but the CFG "
652 "successor doesn't match the actual successor!", MBB);
653 }
654 if (MBB->empty()) {
655 report("MBB exits via unconditional branch but doesn't contain "
656 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000657 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000658 report("MBB exits via unconditional branch but doesn't end with a "
659 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000660 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000661 report("MBB exits via unconditional branch but the branch isn't a "
662 "terminator instruction!", MBB);
663 }
664 } else if (TBB && !FBB && !Cond.empty()) {
665 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000666 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000667 ++MBBI;
668 if (MBBI == MF->end()) {
669 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000670 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000671 // A conditional branch with only one successor is weird, but allowed.
672 if (&*MBBI != TBB)
673 report("MBB exits via conditional branch/fall-through but only has "
674 "one CFG successor!", MBB);
675 else if (TBB != *MBB->succ_begin())
676 report("MBB exits via conditional branch/fall-through but the CFG "
677 "successor don't match the actual successor!", MBB);
678 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000679 report("MBB exits via conditional branch/fall-through but doesn't have "
680 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000681 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000682 report("MBB exits via conditional branch/fall-through but the CFG "
683 "successors don't match the actual successors!", MBB);
684 }
685 if (MBB->empty()) {
686 report("MBB exits via conditional branch/fall-through but doesn't "
687 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000688 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000689 report("MBB exits via conditional branch/fall-through but ends with a "
690 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000691 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000692 report("MBB exits via conditional branch/fall-through but the branch "
693 "isn't a terminator instruction!", MBB);
694 }
695 } else if (TBB && FBB) {
696 // Block conditionally branches somewhere, otherwise branches
697 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000698 if (MBB->succ_size() == 1) {
699 // A conditional branch with only one successor is weird, but allowed.
700 if (FBB != TBB)
701 report("MBB exits via conditional branch/branch through but only has "
702 "one CFG successor!", MBB);
703 else if (TBB != *MBB->succ_begin())
704 report("MBB exits via conditional branch/branch through but the CFG "
705 "successor don't match the actual successor!", MBB);
706 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000707 report("MBB exits via conditional branch/branch but doesn't have "
708 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000709 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000710 report("MBB exits via conditional branch/branch but the CFG "
711 "successors don't match the actual successors!", MBB);
712 }
713 if (MBB->empty()) {
714 report("MBB exits via conditional branch/branch but doesn't "
715 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000716 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000717 report("MBB exits via conditional branch/branch but doesn't end with a "
718 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000719 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000720 report("MBB exits via conditional branch/branch but the branch "
721 "isn't a terminator instruction!", MBB);
722 }
723 if (Cond.empty()) {
724 report("MBB exits via conditinal branch/branch but there's no "
725 "condition!", MBB);
726 }
727 } else {
728 report("AnalyzeBranch returned invalid data!", MBB);
729 }
730 }
731
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000732 regsLive.clear();
Matthias Braund9da1622015-09-09 18:08:03 +0000733 for (const auto &LI : MBB->liveins()) {
734 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000735 report("MBB live-in list contains non-physical register", MBB);
736 continue;
737 }
Matthias Braund9da1622015-09-09 18:08:03 +0000738 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
Chad Rosierabdb1d62013-05-22 23:17:36 +0000739 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000740 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000741 }
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +0000742 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000743
744 const MachineFrameInfo *MFI = MF->getFrameInfo();
745 assert(MFI && "Function has no frame info");
Matthias Braun111f5d82015-05-28 23:20:35 +0000746 BitVector PR = MFI->getPristineRegs(*MF);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000747 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000748 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
749 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000750 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000751 }
752
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000753 regsKilled.clear();
754 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000755
756 if (Indexes)
757 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000758}
759
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000760// This function gets called for all bundle headers, including normal
761// stand-alone unbundled instructions.
762void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000763 if (Indexes && Indexes->hasIndex(*MI)) {
764 SlotIndex idx = Indexes->getInstructionIndex(*MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000765 if (!(idx > lastIndex)) {
766 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000767 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000768 }
769 lastIndex = idx;
770 }
Pete Coopercd720162012-06-07 17:41:39 +0000771
772 // Ensure non-terminators don't follow terminators.
773 // Ignore predicated terminators formed by if conversion.
774 // FIXME: If conversion shouldn't need to violate this rule.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000775 if (MI->isTerminator() && !TII->isPredicated(*MI)) {
Pete Coopercd720162012-06-07 17:41:39 +0000776 if (!FirstTerminator)
777 FirstTerminator = MI;
778 } else if (FirstTerminator) {
779 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000780 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000781 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000782}
783
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000784// The operands on an INLINEASM instruction must follow a template.
785// Verify that the flag operands make sense.
786void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
787 // The first two operands on INLINEASM are the asm string and global flags.
788 if (MI->getNumOperands() < 2) {
789 report("Too few operands on inline asm", MI);
790 return;
791 }
792 if (!MI->getOperand(0).isSymbol())
793 report("Asm string must be an external symbol", MI);
794 if (!MI->getOperand(1).isImm())
795 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000796 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
797 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
798 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000799 report("Unknown asm flags", &MI->getOperand(1), 1);
800
Gabor Horvathfee04342015-03-16 09:53:42 +0000801 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000802
803 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
804 unsigned NumOps;
805 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
806 const MachineOperand &MO = MI->getOperand(OpNo);
807 // There may be implicit ops after the fixed operands.
808 if (!MO.isImm())
809 break;
810 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
811 }
812
813 if (OpNo > MI->getNumOperands())
814 report("Missing operands in last group", MI);
815
816 // An optional MDNode follows the groups.
817 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
818 ++OpNo;
819
820 // All trailing operands must be implicit registers.
821 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
822 const MachineOperand &MO = MI->getOperand(OpNo);
823 if (!MO.isReg() || !MO.isImplicit())
824 report("Expected implicit register after groups", &MO, OpNo);
825 }
826}
827
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000828void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000829 const MCInstrDesc &MCID = MI->getDesc();
830 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000831 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000832 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000833 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000834 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000835
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000836 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000837 if (MI->isInlineAsm())
838 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000839
Dan Gohmandb9493c2009-10-07 17:36:00 +0000840 // Check the MachineMemOperands for basic consistency.
841 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
842 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000843 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000844 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000845 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000846 report("Missing mayStore flag", MI);
847 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000848
849 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000850 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000851 if (LiveInts) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000852 bool mapped = !LiveInts->isNotInMIMap(*MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000853 if (MI->isDebugValue()) {
854 if (mapped)
855 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000856 } else if (MI->isInsideBundle()) {
857 if (mapped)
858 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000859 } else {
860 if (!mapped)
861 report("Missing slot index", MI);
862 }
863 }
864
Andrew Trick924123a2011-09-21 02:20:46 +0000865 StringRef ErrorInfo;
866 if (!TII->verifyInstruction(MI, ErrorInfo))
867 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000868}
869
870void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000871MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000872 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000873 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +0000874 unsigned NumDefs = MCID.getNumDefs();
875 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
876 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000877
Evan Cheng6cc775f2011-06-28 19:10:37 +0000878 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +0000879 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000880 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000881 if (!MO->isReg())
882 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +0000883 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000884 report("Explicit definition marked as use", MO, MONum);
885 else if (MO->isImplicit())
886 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000887 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +0000888 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +0000889 // Don't check if it's the last operand in a variadic instruction. See,
890 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000891 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000892 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000893 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +0000894 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000895 if (MO->isImplicit())
896 report("Explicit operand marked as implicit", MO, MONum);
897 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000898
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000899 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
900 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000901 if (!MO->isReg())
902 report("Tied use must be a register", MO, MONum);
903 else if (!MO->isTied())
904 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000905 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
906 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000907 } else if (MO->isReg() && MO->isTied())
908 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000909 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +0000910 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000911 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +0000912 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +0000913 }
914
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000915 switch (MO->getType()) {
916 case MachineOperand::MO_Register: {
917 const unsigned Reg = MO->getReg();
918 if (!Reg)
919 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000920 if (MRI->tracksLiveness() && !MI->isDebugValue())
921 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000922
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +0000923 // Verify the consistency of tied operands.
924 if (MO->isTied()) {
925 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
926 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
927 if (!OtherMO.isReg())
928 report("Must be tied to a register", MO, MONum);
929 if (!OtherMO.isTied())
930 report("Missing tie flags on tied operand", MO, MONum);
931 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
932 report("Inconsistent tie links", MO, MONum);
933 if (MONum < MCID.getNumDefs()) {
934 if (OtherIdx < MCID.getNumOperands()) {
935 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
936 report("Explicit def tied to explicit use without tie constraint",
937 MO, MONum);
938 } else {
939 if (!OtherMO.isImplicit())
940 report("Explicit def should be tied to implicit use", MO, MONum);
941 }
942 }
943 }
944
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +0000945 // Verify two-address constraints after leaving SSA form.
946 unsigned DefIdx;
947 if (!MRI->isSSA() && MO->isUse() &&
948 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
949 Reg != MI->getOperand(DefIdx).getReg())
950 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000951
952 // Check register classes.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000953 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000954 unsigned SubIdx = MO->getSubReg();
955
956 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000957 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000958 report("Illegal subregister index for physical register", MO, MONum);
959 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000960 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000961 if (const TargetRegisterClass *DRC =
962 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000963 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000964 report("Illegal physical register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000965 errs() << TRI->getName(Reg) << " is not a "
Craig Toppercf0444b2014-11-17 05:50:14 +0000966 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000967 }
968 }
969 } else {
970 // Virtual register.
971 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
972 if (SubIdx) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000973 const TargetRegisterClass *SRC =
974 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +0000975 if (!SRC) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000976 report("Invalid subregister index for virtual register", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000977 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen48431782010-05-18 17:31:12 +0000978 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000979 return;
980 }
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000981 if (RC != SRC) {
982 report("Invalid register class for subregister index", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +0000983 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000984 << " does not fully support subreg index " << SubIdx << "\n";
985 return;
986 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000987 }
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000988 if (const TargetRegisterClass *DRC =
989 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000990 if (SubIdx) {
991 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +0000992 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +0000993 if (!SuperRC) {
994 report("No largest legal super class exists.", MO, MONum);
995 return;
996 }
997 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
998 if (!DRC) {
999 report("No matching super-reg register class.", MO, MONum);
1000 return;
1001 }
1002 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +00001003 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001004 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001005 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +00001006 << " register, but got a " << TRI->getRegClassName(RC)
1007 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001008 }
1009 }
1010 }
1011 }
1012 break;
1013 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001014
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001015 case MachineOperand::MO_RegisterMask:
1016 regMasks.push_back(MO->getRegMask());
1017 break;
1018
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001019 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +00001020 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
1021 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001022 break;
1023
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001024 case MachineOperand::MO_FrameIndex:
1025 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001026 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
Jonas Paulsson72640f12015-10-29 08:28:35 +00001027 int FI = MO->getIndex();
1028 LiveInterval &LI = LiveStks->getInterval(FI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001029 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001030
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001031 bool stores = MI->mayStore();
Jonas Paulsson72640f12015-10-29 08:28:35 +00001032 bool loads = MI->mayLoad();
1033 // For a memory-to-memory move, we need to check if the frame
1034 // index is used for storing or loading, by inspecting the
1035 // memory operands.
1036 if (stores && loads) {
1037 for (auto *MMO : MI->memoperands()) {
1038 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1039 if (PSV == nullptr) continue;
1040 const FixedStackPseudoSourceValue *Value =
1041 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1042 if (Value == nullptr) continue;
1043 if (Value->getFrameIndex() != FI) continue;
1044
1045 if (MMO->isStore())
1046 loads = false;
1047 else
1048 stores = false;
1049 break;
1050 }
1051 if (loads == stores)
1052 report("Missing fixed stack memoperand.", MI);
1053 }
1054 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001055 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001056 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001057 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001058 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001059 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001060 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001061 }
1062 }
1063 break;
1064
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001065 default:
1066 break;
1067 }
1068}
1069
Matthias Braun1377fd62016-02-02 20:04:51 +00001070void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
1071 unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
1072 LaneBitmask LaneMask) {
1073 LiveQueryResult LRQ = LR.Query(UseIdx);
1074 // Check if we have a segment at the use, note however that we only need one
1075 // live subregister range, the others may be dead.
1076 if (!LRQ.valueIn() && LaneMask == 0) {
1077 report("No live segment at use", MO, MONum);
1078 report_context_liverange(LR);
1079 report_context_vreg_regunit(VRegOrUnit);
1080 report_context(UseIdx);
1081 }
1082 if (MO->isKill() && !LRQ.isKill()) {
1083 report("Live range continues after kill flag", MO, MONum);
1084 report_context_liverange(LR);
1085 report_context_vreg_regunit(VRegOrUnit);
1086 if (LaneMask != 0)
1087 report_context_lanemask(LaneMask);
1088 report_context(UseIdx);
1089 }
1090}
1091
1092void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
1093 unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
1094 LaneBitmask LaneMask) {
1095 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
1096 assert(VNI && "NULL valno is not allowed");
1097 if (VNI->def != DefIdx) {
1098 report("Inconsistent valno->def", MO, MONum);
1099 report_context_liverange(LR);
1100 report_context_vreg_regunit(VRegOrUnit);
1101 if (LaneMask != 0)
1102 report_context_lanemask(LaneMask);
1103 report_context(*VNI);
1104 report_context(DefIdx);
1105 }
1106 } else {
1107 report("No live segment at def", MO, MONum);
1108 report_context_liverange(LR);
1109 report_context_vreg_regunit(VRegOrUnit);
1110 if (LaneMask != 0)
1111 report_context_lanemask(LaneMask);
1112 report_context(DefIdx);
1113 }
1114 // Check that, if the dead def flag is present, LiveInts agree.
1115 if (MO->isDead()) {
1116 LiveQueryResult LRQ = LR.Query(DefIdx);
1117 if (!LRQ.isDeadDef()) {
1118 // In case of physregs we can have a non-dead definition on another
1119 // operand.
1120 bool otherDef = false;
1121 if (!TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
1122 const MachineInstr &MI = *MO->getParent();
1123 for (const MachineOperand &MO : MI.operands()) {
1124 if (!MO.isReg() || !MO.isDef() || MO.isDead())
1125 continue;
1126 unsigned Reg = MO.getReg();
1127 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1128 if (*Units == VRegOrUnit) {
1129 otherDef = true;
1130 break;
1131 }
1132 }
1133 }
1134 }
1135
1136 if (!otherDef) {
1137 report("Live range continues after dead def flag", MO, MONum);
1138 report_context_liverange(LR);
1139 report_context_vreg_regunit(VRegOrUnit);
1140 if (LaneMask != 0)
1141 report_context_lanemask(LaneMask);
1142 }
1143 }
1144 }
1145}
1146
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001147void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1148 const MachineInstr *MI = MO->getParent();
1149 const unsigned Reg = MO->getReg();
1150
1151 // Both use and def operands can read a register.
1152 if (MO->readsReg()) {
1153 regsLiveInButUnused.erase(Reg);
1154
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001155 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001156 addRegWithSubRegs(regsKilled, Reg);
1157
1158 // Check that LiveVars knows this kill.
1159 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1160 MO->isKill()) {
1161 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1162 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
1163 report("Kill missing from LiveVariables", MO, MONum);
1164 }
1165
1166 // Check LiveInts liveness and kill.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001167 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1168 SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001169 // Check the cached regunit intervals.
1170 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1171 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001172 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
1173 checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001174 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001175 }
1176
1177 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1178 if (LiveInts->hasInterval(Reg)) {
1179 // This is a virtual register interval.
1180 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun1377fd62016-02-02 20:04:51 +00001181 checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
1182
1183 if (LI.hasSubRanges() && !MO->isDef()) {
1184 unsigned SubRegIdx = MO->getSubReg();
1185 LaneBitmask MOMask = SubRegIdx != 0
1186 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1187 : MRI->getMaxLaneMaskForVReg(Reg);
1188 LaneBitmask LiveInMask = 0;
1189 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1190 if ((MOMask & SR.LaneMask) == 0)
1191 continue;
1192 checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
1193 LiveQueryResult LRQ = SR.Query(UseIdx);
1194 if (LRQ.valueIn())
1195 LiveInMask |= SR.LaneMask;
1196 }
1197 // At least parts of the register has to be live at the use.
1198 if ((LiveInMask & MOMask) == 0) {
1199 report("No live subrange at use", MO, MONum);
1200 report_context(LI);
1201 report_context(UseIdx);
1202 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001203 }
1204 } else {
1205 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001206 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001207 }
1208 }
1209
1210 // Use of a dead register.
1211 if (!regsLive.count(Reg)) {
1212 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1213 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001214 bool Bad = !isReserved(Reg);
1215 // We are fine if just any subregister has a defined value.
1216 if (Bad) {
1217 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1218 ++SubRegs) {
1219 if (regsLive.count(*SubRegs)) {
1220 Bad = false;
1221 break;
1222 }
1223 }
1224 }
Matthias Braun96a31952015-01-14 22:25:14 +00001225 // If there is an additional implicit-use of a super register we stop
1226 // here. By definition we are fine if the super register is not
1227 // (completely) dead, if the complete super register is dead we will
1228 // get a report for its operand.
1229 if (Bad) {
1230 for (const MachineOperand &MOP : MI->uses()) {
1231 if (!MOP.isReg())
1232 continue;
1233 if (!MOP.isImplicit())
1234 continue;
1235 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1236 ++SubRegs) {
1237 if (*SubRegs == Reg) {
1238 Bad = false;
1239 break;
1240 }
1241 }
1242 }
1243 }
Matthias Braun96d77322014-12-10 01:13:13 +00001244 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001245 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001246 } else if (MRI->def_empty(Reg)) {
1247 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001248 } else {
1249 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1250 // We don't know which virtual registers are live in, so only complain
1251 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1252 // must be live in. PHI instructions are handled separately.
1253 if (MInfo.regsKilled.count(Reg))
1254 report("Using a killed virtual register", MO, MONum);
1255 else if (!MI->isPHI())
1256 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1257 }
1258 }
1259 }
1260
1261 if (MO->isDef()) {
1262 // Register defined.
1263 // TODO: verify that earlyclobber ops are not used.
1264 if (MO->isDead())
1265 addRegWithSubRegs(regsDead, Reg);
1266 else
1267 addRegWithSubRegs(regsDefined, Reg);
1268
1269 // Verify SSA form.
1270 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001271 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001272 report("Multiple virtual register defs in SSA form", MO, MONum);
1273
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001274 // Check LiveInts for a live segment, but only for virtual registers.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001275 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1276 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001277 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Matthias Braun1377fd62016-02-02 20:04:51 +00001278
1279 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1280 if (LiveInts->hasInterval(Reg)) {
1281 const LiveInterval &LI = LiveInts->getInterval(Reg);
1282 checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
1283
1284 if (LI.hasSubRanges()) {
1285 unsigned SubRegIdx = MO->getSubReg();
1286 LaneBitmask MOMask = SubRegIdx != 0
1287 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1288 : MRI->getMaxLaneMaskForVReg(Reg);
1289 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1290 if ((SR.LaneMask & MOMask) == 0)
1291 continue;
1292 checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, SR.LaneMask);
1293 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001294 }
1295 } else {
Matthias Braun1377fd62016-02-02 20:04:51 +00001296 report("Virtual register has no Live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001297 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001298 }
1299 }
1300 }
1301}
1302
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001303void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001304}
1305
1306// This function gets called after visiting all instructions in a bundle. The
1307// argument points to the bundle header.
1308// Normal stand-alone instructions are also considered 'bundles', and this
1309// function is called for all of them.
1310void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001311 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1312 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001313 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001314 // Kill any masked registers.
1315 while (!regMasks.empty()) {
1316 const uint32_t *Mask = regMasks.pop_back_val();
1317 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1318 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1319 MachineOperand::clobbersPhysReg(Mask, *I))
1320 regsDead.push_back(*I);
1321 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001322 set_subtract(regsLive, regsDead); regsDead.clear();
1323 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001324}
1325
1326void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001327MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001328 MBBInfoMap[MBB].regsLiveOut = regsLive;
1329 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001330
1331 if (Indexes) {
1332 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1333 if (!(stop > lastIndex)) {
1334 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001335 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001336 << " last instruction was at " << lastIndex << '\n';
1337 }
1338 lastIndex = stop;
1339 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001340}
1341
1342// Calculate the largest possible vregsPassed sets. These are the registers that
1343// can pass through an MBB live, but may not be live every time. It is assumed
1344// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001345void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001346 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1347 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001348 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001349 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001350 BBInfo &MInfo = MBBInfoMap[&MBB];
1351 if (!MInfo.reachable)
1352 continue;
1353 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1354 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1355 BBInfo &SInfo = MBBInfoMap[*SuI];
1356 if (SInfo.addPassed(MInfo.regsLiveOut))
1357 todo.insert(*SuI);
1358 }
1359 }
1360
1361 // Iteratively push vregsPassed to successors. This will converge to the same
1362 // final state regardless of DenseSet iteration order.
1363 while (!todo.empty()) {
1364 const MachineBasicBlock *MBB = *todo.begin();
1365 todo.erase(MBB);
1366 BBInfo &MInfo = MBBInfoMap[MBB];
1367 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1368 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1369 if (*SuI == MBB)
1370 continue;
1371 BBInfo &SInfo = MBBInfoMap[*SuI];
1372 if (SInfo.addPassed(MInfo.vregsPassed))
1373 todo.insert(*SuI);
1374 }
1375 }
1376}
1377
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001378// Calculate the set of virtual registers that must be passed through each basic
1379// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001380// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001381void MachineVerifier::calcRegsRequired() {
1382 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001383 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001384 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001385 BBInfo &MInfo = MBBInfoMap[&MBB];
1386 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1387 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1388 BBInfo &PInfo = MBBInfoMap[*PrI];
1389 if (PInfo.addRequired(MInfo.vregsLiveIn))
1390 todo.insert(*PrI);
1391 }
1392 }
1393
1394 // Iteratively push vregsRequired to predecessors. This will converge to the
1395 // same final state regardless of DenseSet iteration order.
1396 while (!todo.empty()) {
1397 const MachineBasicBlock *MBB = *todo.begin();
1398 todo.erase(MBB);
1399 BBInfo &MInfo = MBBInfoMap[MBB];
1400 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1401 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1402 if (*PrI == MBB)
1403 continue;
1404 BBInfo &SInfo = MBBInfoMap[*PrI];
1405 if (SInfo.addRequired(MInfo.vregsRequired))
1406 todo.insert(*PrI);
1407 }
1408 }
1409}
1410
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001411// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001412// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001413void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001414 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001415 for (const auto &BBI : *MBB) {
1416 if (!BBI.isPHI())
1417 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001418 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001419
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001420 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1421 unsigned Reg = BBI.getOperand(i).getReg();
1422 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001423 if (!Pre->isSuccessor(MBB))
1424 continue;
1425 seen.insert(Pre);
1426 BBInfo &PrInfo = MBBInfoMap[Pre];
1427 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1428 report("PHI operand is not live-out from predecessor",
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001429 &BBI.getOperand(i), i);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001430 }
1431
1432 // Did we see all predecessors?
1433 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1434 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1435 if (!seen.count(*PrI)) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001436 report("Missing PHI operand", &BBI);
Owen Anderson21b17882015-02-04 00:02:59 +00001437 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001438 << " is a predecessor according to the CFG.\n";
1439 }
1440 }
1441 }
1442}
1443
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001444void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001445 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001446
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001447 for (const auto &MBB : *MF) {
1448 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001449
1450 // Skip unreachable MBBs.
1451 if (!MInfo.reachable)
1452 continue;
1453
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001454 checkPHIOps(&MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001455 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001456
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001457 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001458 calcRegsRequired();
1459
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001460 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001461 for (const auto &MBB : *MF) {
1462 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001463 for (RegSet::iterator
1464 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1465 ++I)
1466 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001467 report("Virtual register killed in block, but needed live out.", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001468 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001469 << " is used after the block.\n";
1470 }
1471 }
1472
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001473 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001474 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1475 for (RegSet::iterator
1476 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesen99014ff2012-03-10 00:44:11 +00001477 ++I)
1478 report("Virtual register def doesn't dominate all uses.",
1479 MRI->getVRegDef(*I));
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001480 }
1481
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001482 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001483 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001484 if (LiveInts)
1485 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001486}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001487
1488void MachineVerifier::verifyLiveVariables() {
1489 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001490 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1491 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001492 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001493 for (const auto &MBB : *MF) {
1494 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001495
1496 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1497 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001498 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1499 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001500 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001501 << " must be live through the block.\n";
1502 }
1503 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001504 if (VI.AliveBlocks.test(MBB.getNumber())) {
1505 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001506 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001507 << " is not needed live through the block.\n";
1508 }
1509 }
1510 }
1511 }
1512}
1513
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001514void MachineVerifier::verifyLiveIntervals() {
1515 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001516 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1517 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001518
1519 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001520 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001521 continue;
1522
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001523 if (!LiveInts->hasInterval(Reg)) {
1524 report("Missing live interval for virtual register", MF);
Owen Anderson21b17882015-02-04 00:02:59 +00001525 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001526 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001527 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001528
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001529 const LiveInterval &LI = LiveInts->getInterval(Reg);
1530 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001531 verifyLiveInterval(LI);
1532 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001533
1534 // Verify all the cached regunit intervals.
1535 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001536 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1537 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001538}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001539
Matthias Braun364e6e92013-10-10 21:28:54 +00001540void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001541 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001542 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001543 if (VNI->isUnused())
1544 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001545
Matthias Braun364e6e92013-10-10 21:28:54 +00001546 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001547
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001548 if (!DefVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001549 report("Value not live at VNInfo def and not marked unused", MF);
1550 report_context(LR, Reg, LaneMask);
1551 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001552 return;
1553 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001554
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001555 if (DefVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001556 report("Live segment at def has different VNInfo", MF);
1557 report_context(LR, Reg, LaneMask);
1558 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001559 return;
1560 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001561
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001562 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1563 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001564 report("Invalid VNInfo definition index", MF);
1565 report_context(LR, Reg, LaneMask);
1566 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001567 return;
1568 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001569
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001570 if (VNI->isPHIDef()) {
1571 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001572 report("PHIDef VNInfo is not defined at MBB start", MBB);
1573 report_context(LR, Reg, LaneMask);
1574 report_context(*VNI);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001575 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001576 return;
1577 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001578
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001579 // Non-PHI def.
1580 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1581 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001582 report("No instruction at VNInfo def index", MBB);
1583 report_context(LR, Reg, LaneMask);
1584 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001585 return;
1586 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001587
Matthias Braun364e6e92013-10-10 21:28:54 +00001588 if (Reg != 0) {
1589 bool hasDef = false;
1590 bool isEarlyClobber = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001591 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001592 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001593 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001594 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1595 if (MOI->getReg() != Reg)
1596 continue;
1597 } else {
1598 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1599 !TRI->hasRegUnit(MOI->getReg(), Reg))
1600 continue;
1601 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001602 if (LaneMask != 0 &&
1603 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask) == 0)
1604 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001605 hasDef = true;
1606 if (MOI->isEarlyClobber())
1607 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001608 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001609
Matthias Braun364e6e92013-10-10 21:28:54 +00001610 if (!hasDef) {
1611 report("Defining instruction does not modify register", MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001612 report_context(LR, Reg, LaneMask);
1613 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001614 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001615
Matthias Braun364e6e92013-10-10 21:28:54 +00001616 // Early clobber defs begin at USE slots, but other defs must begin at
1617 // DEF slots.
1618 if (isEarlyClobber) {
1619 if (!VNI->def.isEarlyClobber()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001620 report("Early clobber def must be at an early-clobber slot", MBB);
1621 report_context(LR, Reg, LaneMask);
1622 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001623 }
1624 } else if (!VNI->def.isRegister()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001625 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
1626 report_context(LR, Reg, LaneMask);
1627 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001628 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001629 }
1630}
1631
Matthias Braun364e6e92013-10-10 21:28:54 +00001632void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1633 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001634 unsigned Reg, LaneBitmask LaneMask)
1635{
Matthias Braun364e6e92013-10-10 21:28:54 +00001636 const LiveRange::Segment &S = *I;
1637 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001638 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001639
Matthias Braun364e6e92013-10-10 21:28:54 +00001640 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001641 report("Foreign valno in live segment", MF);
1642 report_context(LR, Reg, LaneMask);
1643 report_context(S);
1644 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001645 }
1646
1647 if (VNI->isUnused()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001648 report("Live segment valno is marked unused", MF);
1649 report_context(LR, Reg, LaneMask);
1650 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001651 }
1652
Matthias Braun364e6e92013-10-10 21:28:54 +00001653 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001654 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001655 report("Bad start of live segment, no basic block", MF);
1656 report_context(LR, Reg, LaneMask);
1657 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001658 return;
1659 }
1660 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001661 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001662 report("Live segment must begin at MBB entry or valno def", MBB);
1663 report_context(LR, Reg, LaneMask);
1664 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001665 }
1666
1667 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001668 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001669 if (!EndMBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001670 report("Bad end of live segment, no basic block", MF);
1671 report_context(LR, Reg, LaneMask);
1672 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001673 return;
1674 }
1675
1676 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001677 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001678 return;
1679
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001680 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001681 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1682 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001683 return;
1684
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001685 // The live segment is ending inside EndMBB
1686 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001687 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001688 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001689 report("Live segment doesn't end at a valid instruction", EndMBB);
1690 report_context(LR, Reg, LaneMask);
1691 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001692 return;
1693 }
1694
1695 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001696 if (S.end.isBlock()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001697 report("Live segment ends at B slot of an instruction", EndMBB);
1698 report_context(LR, Reg, LaneMask);
1699 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001700 }
1701
Matthias Braun364e6e92013-10-10 21:28:54 +00001702 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001703 // Segment ends on the dead slot.
1704 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001705 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001706 report("Live segment ending at dead slot spans instructions", EndMBB);
1707 report_context(LR, Reg, LaneMask);
1708 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001709 }
1710 }
1711
1712 // A live segment can only end at an early-clobber slot if it is being
1713 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001714 if (S.end.isEarlyClobber()) {
1715 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001716 report("Live segment ending at early clobber slot must be "
Matthias Braun7e624d52015-11-09 23:59:33 +00001717 "redefined by an EC def in the same instruction", EndMBB);
1718 report_context(LR, Reg, LaneMask);
1719 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001720 }
1721 }
1722
1723 // The following checks only apply to virtual registers. Physreg liveness
1724 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001725 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001726 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001727 // use, or a dead flag on a def.
1728 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001729 bool hasSubRegDef = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001730 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001731 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001732 continue;
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001733 if (LaneMask != 0 &&
1734 (LaneMask & TRI->getSubRegIndexLaneMask(MOI->getSubReg())) == 0)
1735 continue;
Matthias Braun21554d92014-12-10 01:13:11 +00001736 if (MOI->isDef() && MOI->getSubReg() != 0)
1737 hasSubRegDef = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001738 if (MOI->readsReg())
1739 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001740 }
Pedro Artigas71f87cb2013-11-08 22:46:28 +00001741 if (!S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001742 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001743 // When tracking subregister liveness, the main range must start new
1744 // values on partial register writes, even if there is no read.
Matthias Brauna25e13a2015-03-19 00:21:58 +00001745 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask != 0 ||
1746 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001747 report("Instruction ending live segment doesn't read the register",
1748 MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001749 report_context(LR, Reg, LaneMask);
1750 report_context(S);
Matthias Braun21554d92014-12-10 01:13:11 +00001751 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001752 }
1753 }
1754 }
1755
1756 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001757 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001758 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00001759 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001760 // Not live-in to any blocks.
1761 if (MBB == EndMBB)
1762 return;
1763 // Skip this block.
1764 ++MFI;
1765 }
1766 for (;;) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001767 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001768 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00001769 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00001770 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001771 if (&*MFI == EndMBB)
1772 break;
1773 ++MFI;
1774 continue;
1775 }
1776
1777 // Is VNI a PHI-def in the current block?
1778 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001779 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001780
1781 // Check that VNI is live-out of all predecessors.
1782 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1783 PE = MFI->pred_end(); PI != PE; ++PI) {
1784 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001785 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001786
1787 // All predecessors must have a live-out value.
1788 if (!PVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001789 report("Register not marked live out of predecessor", *PI);
1790 report_context(LR, Reg, LaneMask);
1791 report_context(*VNI);
1792 errs() << " live into BB#" << MFI->getNumber()
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001793 << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
1794 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001795 continue;
1796 }
1797
1798 // Only PHI-defs can take different predecessor values.
1799 if (!IsPHI && PVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001800 report("Different value live out of predecessor", *PI);
1801 report_context(LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00001802 errs() << "Valno #" << PVNI->id << " live out of BB#"
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00001803 << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
1804 << " live into BB#" << MFI->getNumber() << '@'
1805 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001806 }
1807 }
1808 if (&*MFI == EndMBB)
1809 break;
1810 ++MFI;
1811 }
1812}
1813
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001814void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001815 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00001816 for (const VNInfo *VNI : LR.valnos)
1817 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001818
Matthias Braun364e6e92013-10-10 21:28:54 +00001819 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001820 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00001821}
1822
1823void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001824 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00001825 assert(TargetRegisterInfo::isVirtualRegister(Reg));
1826 verifyLiveRange(LI, Reg);
1827
Matthias Braune6a24852015-09-25 21:51:14 +00001828 LaneBitmask Mask = 0;
1829 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00001830 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001831 if ((Mask & SR.LaneMask) != 0) {
1832 report("Lane masks of sub ranges overlap in live interval", MF);
1833 report_context(LI);
1834 }
1835 if ((SR.LaneMask & ~MaxMask) != 0) {
1836 report("Subrange lanemask is invalid", MF);
1837 report_context(LI);
1838 }
1839 if (SR.empty()) {
1840 report("Subrange must not be empty", MF);
1841 report_context(SR, LI.reg, SR.LaneMask);
1842 }
Matthias Braune962e522015-03-25 21:18:22 +00001843 Mask |= SR.LaneMask;
1844 verifyLiveRange(SR, LI.reg, SR.LaneMask);
Matthias Braun7e624d52015-11-09 23:59:33 +00001845 if (!LI.covers(SR)) {
1846 report("A Subrange is not covered by the main range", MF);
1847 report_context(LI);
1848 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001849 }
1850
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001851 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00001852 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
Matthias Braunbf47f632016-01-08 01:16:35 +00001853 unsigned NumComp = ConEQ.Classify(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001854 if (NumComp > 1) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001855 report("Multiple connected components in live interval", MF);
1856 report_context(LI);
Matthias Braune962e522015-03-25 21:18:22 +00001857 for (unsigned comp = 0; comp != NumComp; ++comp) {
1858 errs() << comp << ": valnos";
1859 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1860 E = LI.vni_end(); I!=E; ++I)
1861 if (comp == ConEQ.getEqClass(*I))
1862 errs() << ' ' << (*I)->id;
1863 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00001864 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001865 }
1866}
Manman Renaa6875b2013-07-15 21:26:31 +00001867
1868namespace {
1869 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1870 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1871 // value is zero.
1872 // We use a bool plus an integer to capture the stack state.
1873 struct StackStateOfBB {
1874 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1875 ExitIsSetup(false) { }
1876 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1877 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1878 ExitIsSetup(ExitSetup) { }
1879 // Can be negative, which means we are setting up a frame.
1880 int EntryValue;
1881 int ExitValue;
1882 bool EntryIsSetup;
1883 bool ExitIsSetup;
1884 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001885}
Manman Renaa6875b2013-07-15 21:26:31 +00001886
1887/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1888/// by a FrameDestroy <n>, stack adjustments are identical on all
1889/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1890void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001891 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1892 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Manman Renaa6875b2013-07-15 21:26:31 +00001893
1894 SmallVector<StackStateOfBB, 8> SPState;
1895 SPState.resize(MF->getNumBlockIDs());
1896 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1897
1898 // Visit the MBBs in DFS order.
1899 for (df_ext_iterator<const MachineFunction*,
1900 SmallPtrSet<const MachineBasicBlock*, 8> >
1901 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1902 DFI != DFE; ++DFI) {
1903 const MachineBasicBlock *MBB = *DFI;
1904
1905 StackStateOfBB BBState;
1906 // Check the exit state of the DFS stack predecessor.
1907 if (DFI.getPathLength() >= 2) {
1908 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1909 assert(Reachable.count(StackPred) &&
1910 "DFS stack predecessor is already visited.\n");
1911 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1912 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1913 BBState.ExitValue = BBState.EntryValue;
1914 BBState.ExitIsSetup = BBState.EntryIsSetup;
1915 }
1916
1917 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001918 for (const auto &I : *MBB) {
1919 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001920 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001921 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001922 assert(Size >= 0 &&
1923 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1924
1925 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001926 report("FrameSetup is after another FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001927 BBState.ExitValue -= Size;
1928 BBState.ExitIsSetup = true;
1929 }
1930
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001931 if (I.getOpcode() == FrameDestroyOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00001932 // The first operand of a FrameOpcode should be i32.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001933 int Size = I.getOperand(0).getImm();
Manman Renaa6875b2013-07-15 21:26:31 +00001934 assert(Size >= 0 &&
1935 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1936
1937 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001938 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00001939 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1940 BBState.ExitValue;
1941 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001942 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00001943 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00001944 << AbsSPAdj << ">.\n";
1945 }
1946 BBState.ExitValue += Size;
1947 BBState.ExitIsSetup = false;
1948 }
1949 }
1950 SPState[MBB->getNumber()] = BBState;
1951
1952 // Make sure the exit state of any predecessor is consistent with the entry
1953 // state.
1954 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
1955 E = MBB->pred_end(); I != E; ++I) {
1956 if (Reachable.count(*I) &&
1957 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
1958 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
1959 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001960 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00001961 << SPState[(*I)->getNumber()].ExitValue << ", "
1962 << SPState[(*I)->getNumber()].ExitIsSetup
1963 << "), while BB#" << MBB->getNumber() << " has entry state ("
1964 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
1965 }
1966 }
1967
1968 // Make sure the entry state of any successor is consistent with the exit
1969 // state.
1970 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
1971 E = MBB->succ_end(); I != E; ++I) {
1972 if (Reachable.count(*I) &&
1973 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
1974 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
1975 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001976 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00001977 << SPState[(*I)->getNumber()].EntryValue << ", "
1978 << SPState[(*I)->getNumber()].EntryIsSetup
1979 << "), while BB#" << MBB->getNumber() << " has exit state ("
1980 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
1981 }
1982 }
1983
1984 // Make sure a basic block with return ends with zero stack adjustment.
1985 if (!MBB->empty() && MBB->back().isReturn()) {
1986 if (BBState.ExitIsSetup)
1987 report("A return block ends with a FrameSetup.", MBB);
1988 if (BBState.ExitValue)
1989 report("A return block ends with a nonzero stack adjustment.", MBB);
1990 }
1991 }
1992}