blob: 2d138298a943c1b48ee579e72bc7450c60762714 [file] [log] [blame]
Eugene Zelenko32a40562017-09-11 23:00:48 +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//
Matthias Braunbb8507e2017-10-12 22:57:28 +000020// 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.
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000024//===----------------------------------------------------------------------===//
25
Eugene Zelenko32a40562017-09-11 23:00:48 +000026#include "llvm/ADT/BitVector.h"
27#include "llvm/ADT/DenseMap.h"
Chris Lattner565449d2009-08-23 03:13:20 +000028#include "llvm/ADT/DenseSet.h"
Manman Renaa6875b2013-07-15 21:26:31 +000029#include "llvm/ADT/DepthFirstIterator.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000030#include "llvm/ADT/STLExtras.h"
Chris Lattner565449d2009-08-23 03:13:20 +000031#include "llvm/ADT/SetOperations.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000032#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner565449d2009-08-23 03:13:20 +000033#include "llvm/ADT/SmallVector.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000034#include "llvm/ADT/StringRef.h"
35#include "llvm/ADT/Twine.h"
David Majnemer70497c62015-12-02 23:06:39 +000036#include "llvm/Analysis/EHPersonalities.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000037#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
38#include "llvm/CodeGen/LiveInterval.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000039#include "llvm/CodeGen/LiveIntervalAnalysis.h"
40#include "llvm/CodeGen/LiveStackAnalysis.h"
41#include "llvm/CodeGen/LiveVariables.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000042#include "llvm/CodeGen/MachineBasicBlock.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000043#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000044#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000045#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000046#include "llvm/CodeGen/MachineInstr.h"
47#include "llvm/CodeGen/MachineInstrBundle.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000048#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000049#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000050#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000051#include "llvm/CodeGen/PseudoSourceValue.h"
52#include "llvm/CodeGen/SlotIndexes.h"
Philip Reames94cc4a22017-06-02 16:36:37 +000053#include "llvm/CodeGen/StackMaps.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000054#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000055#include "llvm/CodeGen/TargetOpcodes.h"
56#include "llvm/CodeGen/TargetRegisterInfo.h"
57#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000058#include "llvm/IR/BasicBlock.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000059#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000060#include "llvm/IR/InlineAsm.h"
61#include "llvm/IR/Instructions.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000062#include "llvm/MC/LaneBitmask.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000063#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000064#include "llvm/MC/MCInstrDesc.h"
65#include "llvm/MC/MCRegisterInfo.h"
66#include "llvm/MC/MCTargetOptions.h"
67#include "llvm/Pass.h"
68#include "llvm/Support/Casting.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000069#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000070#include "llvm/Support/LowLevelTypeImpl.h"
71#include "llvm/Support/MathExtras.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000072#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000073#include "llvm/Target/TargetMachine.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000074#include <algorithm>
75#include <cassert>
76#include <cstddef>
77#include <cstdint>
78#include <iterator>
79#include <string>
80#include <utility>
81
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000082using namespace llvm;
83
84namespace {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000085
Eugene Zelenko32a40562017-09-11 23:00:48 +000086 struct MachineVerifier {
87 MachineVerifier(Pass *pass, const char *b) : PASS(pass), Banner(b) {}
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000088
Matthias Braunb3aefc32016-02-15 19:25:31 +000089 unsigned verify(MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000090
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +000091 Pass *const PASS;
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +000092 const char *Banner;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000093 const MachineFunction *MF;
94 const TargetMachine *TM;
Evan Cheng8d71a752011-06-27 21:26:13 +000095 const TargetInstrInfo *TII;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000096 const TargetRegisterInfo *TRI;
97 const MachineRegisterInfo *MRI;
98
99 unsigned foundErrors;
100
Ahmed Bougacha3681c772016-08-02 16:17:15 +0000101 // Avoid querying the MachineFunctionProperties for each operand.
102 bool isFunctionRegBankSelected;
Ahmed Bougachab14e9442016-08-02 16:49:22 +0000103 bool isFunctionSelected;
Ahmed Bougacha3681c772016-08-02 16:17:15 +0000104
Eugene Zelenko32a40562017-09-11 23:00:48 +0000105 using RegVector = SmallVector<unsigned, 16>;
106 using RegMaskVector = SmallVector<const uint32_t *, 4>;
107 using RegSet = DenseSet<unsigned>;
108 using RegMap = DenseMap<unsigned, const MachineInstr *>;
109 using BlockSet = SmallPtrSet<const MachineBasicBlock *, 8>;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000110
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000111 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000112 BlockSet FunctionBlocks;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000113
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000114 BitVector regsReserved;
115 RegSet regsLive;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +0000116 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000117 RegMaskVector regMasks;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000118
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000119 SlotIndex lastIndex;
120
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000121 // Add Reg and any sub-registers to RV
122 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
123 RV.push_back(Reg);
124 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000125 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
126 RV.push_back(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000127 }
128
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000129 struct BBInfo {
130 // Is this MBB reachable from the MF entry point?
Eugene Zelenko32a40562017-09-11 23:00:48 +0000131 bool reachable = false;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000132
133 // Vregs that must be live in because they are used without being
134 // defined. Map value is the user.
135 RegMap vregsLiveIn;
136
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000137 // Regs killed in MBB. They may be defined again, and will then be in both
138 // regsKilled and regsLiveOut.
139 RegSet regsKilled;
140
141 // Regs defined in MBB and live out. Note that vregs passing through may
142 // be live out without being mentioned here.
143 RegSet regsLiveOut;
144
145 // Vregs that pass through MBB untouched. This set is disjoint from
146 // regsKilled and regsLiveOut.
147 RegSet vregsPassed;
148
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000149 // Vregs that must pass through MBB because they are needed by a successor
150 // block. This set is disjoint from regsLiveOut.
151 RegSet vregsRequired;
152
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000153 // Set versions of block's predecessor and successor lists.
154 BlockSet Preds, Succs;
155
Eugene Zelenko32a40562017-09-11 23:00:48 +0000156 BBInfo() = default;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000157
158 // Add register to vregsPassed if it belongs there. Return true if
159 // anything changed.
160 bool addPassed(unsigned Reg) {
161 if (!TargetRegisterInfo::isVirtualRegister(Reg))
162 return false;
163 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
164 return false;
165 return vregsPassed.insert(Reg).second;
166 }
167
168 // Same for a full set.
169 bool addPassed(const RegSet &RS) {
170 bool changed = false;
171 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
172 if (addPassed(*I))
173 changed = true;
174 return changed;
175 }
176
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000177 // Add register to vregsRequired if it belongs there. Return true if
178 // anything changed.
179 bool addRequired(unsigned Reg) {
180 if (!TargetRegisterInfo::isVirtualRegister(Reg))
181 return false;
182 if (regsLiveOut.count(Reg))
183 return false;
184 return vregsRequired.insert(Reg).second;
185 }
186
187 // Same for a full set.
188 bool addRequired(const RegSet &RS) {
189 bool changed = false;
190 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
191 if (addRequired(*I))
192 changed = true;
193 return changed;
194 }
195
196 // Same for a full map.
197 bool addRequired(const RegMap &RM) {
198 bool changed = false;
199 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
200 if (addRequired(I->first))
201 changed = true;
202 return changed;
203 }
204
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000205 // Live-out registers are either in regsLiveOut or vregsPassed.
206 bool isLiveOut(unsigned Reg) const {
207 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
208 }
209 };
210
211 // Extra register info per MBB.
212 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
213
214 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000215 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000216 }
217
Matthias Braun4682ac62017-05-05 22:04:05 +0000218 bool isAllocatable(unsigned Reg) const {
219 return Reg < TRI->getNumRegs() && TRI->isInAllocatableClass(Reg) &&
220 !regsReserved.test(Reg);
Lang Hames1ce837a2012-02-14 19:17:48 +0000221 }
222
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000223 // Analysis information if available
224 LiveVariables *LiveVars;
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +0000225 LiveIntervals *LiveInts;
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000226 LiveStacks *LiveStks;
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000227 SlotIndexes *Indexes;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000228
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000229 void visitMachineFunctionBefore();
230 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000231 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000232 void visitMachineInstrBefore(const MachineInstr *MI);
233 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
234 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000235 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000236 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
237 void visitMachineFunctionAfter();
238
239 void report(const char *msg, const MachineFunction *MF);
240 void report(const char *msg, const MachineBasicBlock *MBB);
241 void report(const char *msg, const MachineInstr *MI);
242 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Matthias Braun7e624d52015-11-09 23:59:33 +0000243
244 void report_context(const LiveInterval &LI) const;
Matt Arsenault892fcd02016-07-25 19:39:01 +0000245 void report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000246 LaneBitmask LaneMask) const;
247 void report_context(const LiveRange::Segment &S) const;
248 void report_context(const VNInfo &VNI) const;
Matthias Braun579c9cd2016-02-02 02:44:25 +0000249 void report_context(SlotIndex Pos) const;
250 void report_context_liverange(const LiveRange &LR) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000251 void report_context_lanemask(LaneBitmask LaneMask) const;
Matthias Braun30668dd2016-05-11 21:31:39 +0000252 void report_context_vreg(unsigned VReg) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000253 void report_context_vreg_regunit(unsigned VRegOrRegUnit) const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000254
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000255 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000256
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000257 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Matthias Braun1377fd62016-02-02 20:04:51 +0000258 void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
259 SlotIndex UseIdx, const LiveRange &LR, unsigned Reg,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000260 LaneBitmask LaneMask = LaneBitmask::getNone());
Matthias Braun1377fd62016-02-02 20:04:51 +0000261 void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
262 SlotIndex DefIdx, const LiveRange &LR, unsigned Reg,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000263 LaneBitmask LaneMask = LaneBitmask::getNone());
Matthias Braun1377fd62016-02-02 20:04:51 +0000264
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000265 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000266 void calcRegsPassed();
Matthias Brauna6d53742017-11-28 03:54:19 +0000267 void checkPHIOps(const MachineBasicBlock &MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000268
269 void calcRegsRequired();
270 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000271 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000272 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000273 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000274 LaneBitmask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000275 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000276 const LiveRange::const_iterator I, unsigned,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000277 LaneBitmask);
278 void verifyLiveRange(const LiveRange&, unsigned,
279 LaneBitmask LaneMask = LaneBitmask::getNone());
Manman Renaa6875b2013-07-15 21:26:31 +0000280
281 void verifyStackFrame();
Matthias Braun80595462015-09-09 17:49:46 +0000282
283 void verifySlotIndexes() const;
Derek Schuff42666ee2016-03-29 17:40:22 +0000284 void verifyProperties(const MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000285 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000286
287 struct MachineVerifierPass : public MachineFunctionPass {
288 static char ID; // Pass ID, replacement for typeid
Eugene Zelenko32a40562017-09-11 23:00:48 +0000289
Matthias Brauna4e932d2014-12-11 19:41:51 +0000290 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000291
Sven van Haastregt04bfa872017-03-29 15:25:06 +0000292 MachineVerifierPass(std::string banner = std::string())
Sven van Haastregt039a6d92017-03-29 09:08:25 +0000293 : MachineFunctionPass(ID), Banner(std::move(banner)) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000294 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
295 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000296
Craig Topper4584cd52014-03-07 09:26:03 +0000297 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000298 AU.setPreservesAll();
299 MachineFunctionPass::getAnalysisUsage(AU);
300 }
301
Craig Topper4584cd52014-03-07 09:26:03 +0000302 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Braunb3aefc32016-02-15 19:25:31 +0000303 unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
304 if (FoundErrors)
305 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000306 return false;
307 }
308 };
309
Eugene Zelenko32a40562017-09-11 23:00:48 +0000310} // end anonymous namespace
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000311
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000312char MachineVerifierPass::ID = 0;
Eugene Zelenko32a40562017-09-11 23:00:48 +0000313
Owen Andersond31d82d2010-08-23 17:52:01 +0000314INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000315 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000316
Matthias Brauna4e932d2014-12-11 19:41:51 +0000317FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000318 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000319}
320
Matthias Braunb3aefc32016-02-15 19:25:31 +0000321bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
322 const {
323 MachineFunction &MF = const_cast<MachineFunction&>(*this);
324 unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
325 if (AbortOnErrors && FoundErrors)
326 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
327 return FoundErrors == 0;
Jakob Stoklund Olesen27440e72009-11-13 21:56:09 +0000328}
329
Matthias Braun80595462015-09-09 17:49:46 +0000330void MachineVerifier::verifySlotIndexes() const {
331 if (Indexes == nullptr)
332 return;
333
334 // Ensure the IdxMBB list is sorted by slot indexes.
335 SlotIndex Last;
336 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
337 E = Indexes->MBBIndexEnd(); I != E; ++I) {
338 assert(!Last.isValid() || I->first > Last);
339 Last = I->first;
340 }
341}
342
Derek Schuff42666ee2016-03-29 17:40:22 +0000343void MachineVerifier::verifyProperties(const MachineFunction &MF) {
344 // If a pass has introduced virtual registers without clearing the
Matthias Braun1eb47362016-08-25 01:27:13 +0000345 // NoVRegs property (or set it without allocating the vregs)
Derek Schuff42666ee2016-03-29 17:40:22 +0000346 // then report an error.
347 if (MF.getProperties().hasProperty(
Matthias Braun1eb47362016-08-25 01:27:13 +0000348 MachineFunctionProperties::Property::NoVRegs) &&
349 MRI->getNumVirtRegs())
350 report("Function has NoVRegs property but there are VReg operands", &MF);
Derek Schuff42666ee2016-03-29 17:40:22 +0000351}
352
Matthias Braunb3aefc32016-02-15 19:25:31 +0000353unsigned MachineVerifier::verify(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000354 foundErrors = 0;
355
356 this->MF = &MF;
357 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000358 TII = MF.getSubtarget().getInstrInfo();
359 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000360 MRI = &MF.getRegInfo();
361
Ahmed Bougacha3681c772016-08-02 16:17:15 +0000362 isFunctionRegBankSelected = MF.getProperties().hasProperty(
363 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab14e9442016-08-02 16:49:22 +0000364 isFunctionSelected = MF.getProperties().hasProperty(
365 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha3681c772016-08-02 16:17:15 +0000366
Craig Topperc0196b12014-04-14 00:51:57 +0000367 LiveVars = nullptr;
368 LiveInts = nullptr;
369 LiveStks = nullptr;
370 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000371 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000372 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000373 // We don't want to verify LiveVariables if LiveIntervals is available.
374 if (!LiveInts)
375 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000376 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000377 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000378 }
379
Matthias Braun80595462015-09-09 17:49:46 +0000380 verifySlotIndexes();
381
Derek Schuff42666ee2016-03-29 17:40:22 +0000382 verifyProperties(MF);
383
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000384 visitMachineFunctionBefore();
385 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
386 MFI!=MFE; ++MFI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000387 visitMachineBasicBlockBefore(&*MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000388 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000389 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000390 // Do we expect the next instruction to be part of the same bundle?
391 bool InBundle = false;
392
Evan Cheng7fae11b2011-12-14 02:11:42 +0000393 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
394 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000395 if (MBBI->getParent() != &*MFI) {
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +0000396 report("Bad instruction parent pointer", &*MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000397 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000398 continue;
399 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000400
401 // Check for consistent bundle flags.
402 if (InBundle && !MBBI->isBundledWithPred())
403 report("Missing BundledPred flag, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000404 "BundledSucc was set on predecessor",
405 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000406 if (!InBundle && MBBI->isBundledWithPred())
407 report("BundledPred flag is set, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000408 "but BundledSucc not set on predecessor",
409 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000410
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000411 // Is this a bundle header?
412 if (!MBBI->isInsideBundle()) {
413 if (CurBundle)
414 visitMachineBundleAfter(CurBundle);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000415 CurBundle = &*MBBI;
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000416 visitMachineBundleBefore(CurBundle);
417 } else if (!CurBundle)
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +0000418 report("No bundle header", &*MBBI);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000419 visitMachineInstrBefore(&*MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000420 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
421 const MachineInstr &MI = *MBBI;
422 const MachineOperand &Op = MI.getOperand(I);
423 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000424 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000425 // functions when replacing operands of a MachineInstr.
426 report("Instruction has operand with wrong parent set", &MI);
427 }
428
429 visitMachineOperand(&Op, I);
430 }
431
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000432 visitMachineInstrAfter(&*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000433
434 // Was this the last bundled instruction?
435 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000436 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000437 if (CurBundle)
438 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000439 if (InBundle)
440 report("BundledSucc flag set on last instruction in block", &MFI->back());
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000441 visitMachineBasicBlockAfter(&*MFI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000442 }
443 visitMachineFunctionAfter();
444
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000445 // Clean up.
446 regsLive.clear();
447 regsDefined.clear();
448 regsDead.clear();
449 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000450 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000451 MBBInfoMap.clear();
452
Matthias Braunb3aefc32016-02-15 19:25:31 +0000453 return foundErrors;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000454}
455
Chris Lattner75f40452009-08-23 01:03:30 +0000456void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000457 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000458 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000459 if (!foundErrors++) {
460 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000461 errs() << "# " << Banner << '\n';
Matthias Braun42b4b632015-11-09 23:59:23 +0000462 if (LiveInts != nullptr)
463 LiveInts->print(errs());
464 else
465 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000466 }
Owen Anderson21b17882015-02-04 00:02:59 +0000467 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000468 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000469}
470
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000471void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000472 assert(MBB);
473 report(msg, MBB->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000474 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000475 << ' ' << MBB->getName()
Roman Divackyad06cee2012-09-05 22:26:57 +0000476 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000477 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000478 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000479 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000480 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000481}
482
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000483void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000484 assert(MI);
485 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000486 errs() << "- instruction: ";
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000487 if (Indexes && Indexes->hasIndex(*MI))
488 errs() << Indexes->getInstructionIndex(*MI) << '\t';
Matthias Braun45718db2015-11-09 23:59:25 +0000489 MI->print(errs(), /*SkipOpers=*/true);
Matthias Braun716b4332015-11-09 23:59:29 +0000490 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000491}
492
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000493void MachineVerifier::report(const char *msg,
494 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000495 assert(MO);
496 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000497 errs() << "- operand " << MONum << ": ";
Eric Christopher1cdefae2015-02-27 00:11:34 +0000498 MO->print(errs(), TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000499 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000500}
501
Matthias Braun579c9cd2016-02-02 02:44:25 +0000502void MachineVerifier::report_context(SlotIndex Pos) const {
503 errs() << "- at: " << Pos << '\n';
504}
505
Matthias Braun7e624d52015-11-09 23:59:33 +0000506void MachineVerifier::report_context(const LiveInterval &LI) const {
Owen Anderson21b17882015-02-04 00:02:59 +0000507 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000508}
509
Matt Arsenault892fcd02016-07-25 19:39:01 +0000510void MachineVerifier::report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000511 LaneBitmask LaneMask) const {
Matthias Braun579c9cd2016-02-02 02:44:25 +0000512 report_context_liverange(LR);
Matt Arsenault892fcd02016-07-25 19:39:01 +0000513 report_context_vreg_regunit(VRegUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000514 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +0000515 report_context_lanemask(LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000516}
517
Matthias Braun7e624d52015-11-09 23:59:33 +0000518void MachineVerifier::report_context(const LiveRange::Segment &S) const {
519 errs() << "- segment: " << S << '\n';
520}
521
522void MachineVerifier::report_context(const VNInfo &VNI) const {
523 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
Matthias Braun364e6e92013-10-10 21:28:54 +0000524}
525
Matthias Braun579c9cd2016-02-02 02:44:25 +0000526void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
527 errs() << "- liverange: " << LR << '\n';
528}
529
Matthias Braun30668dd2016-05-11 21:31:39 +0000530void MachineVerifier::report_context_vreg(unsigned VReg) const {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000531 errs() << "- v. register: " << printReg(VReg, TRI) << '\n';
Matthias Braun30668dd2016-05-11 21:31:39 +0000532}
533
Matthias Braun1377fd62016-02-02 20:04:51 +0000534void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
535 if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
Matthias Braun30668dd2016-05-11 21:31:39 +0000536 report_context_vreg(VRegOrUnit);
Matthias Braun1377fd62016-02-02 20:04:51 +0000537 } else {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000538 errs() << "- regunit: " << printRegUnit(VRegOrUnit, TRI) << '\n';
Matthias Braun1377fd62016-02-02 20:04:51 +0000539 }
540}
541
542void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
543 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
544}
545
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000546void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000547 BBInfo &MInfo = MBBInfoMap[MBB];
548 if (!MInfo.reachable) {
549 MInfo.reachable = true;
550 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
551 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
552 markReachable(*SuI);
553 }
554}
555
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000556void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000557 lastIndex = SlotIndex();
Matthias Braun4682ac62017-05-05 22:04:05 +0000558 regsReserved = MRI->reservedRegsFrozen() ? MRI->getReservedRegs()
559 : TRI->getReservedRegs(*MF);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000560
Justin Bogner20dd36a2017-04-11 19:32:41 +0000561 if (!MF->empty())
562 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000563
564 // Build a set of the basic blocks in the function.
565 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000566 for (const auto &MBB : *MF) {
567 FunctionBlocks.insert(&MBB);
568 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000569
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000570 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
571 if (MInfo.Preds.size() != MBB.pred_size())
572 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000573
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000574 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
575 if (MInfo.Succs.size() != MBB.succ_size())
576 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000577 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000578
579 // Check that the register use lists are sane.
580 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000581
Justin Bogner20dd36a2017-04-11 19:32:41 +0000582 if (!MF->empty())
583 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000584}
585
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000586// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000587static bool matchPair(MachineBasicBlock::const_succ_iterator i,
588 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000589 if (*i == a)
590 return *++i == b;
591 if (*i == b)
592 return *++i == a;
593 return false;
594}
595
596void
597MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000598 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000599
Matthias Braun79f85b32016-08-24 01:32:41 +0000600 if (!MF->getProperties().hasProperty(
Matthias Braun11723322017-01-05 20:01:19 +0000601 MachineFunctionProperties::Property::NoPHIs) && MRI->tracksLiveness()) {
Lang Hames1ce837a2012-02-14 19:17:48 +0000602 // If this block has allocatable physical registers live-in, check that
603 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000604 for (const auto &LI : MBB->liveins()) {
605 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +0000606 MBB->getIterator() != MBB->getParent()->begin()) {
Matt Arsenault900b21c2017-02-15 22:19:06 +0000607 report("MBB has allocatable live-in, but isn't entry or landing-pad.", MBB);
Lang Hames1ce837a2012-02-14 19:17:48 +0000608 }
609 }
610 }
611
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000612 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000613 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000614 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000615 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000616 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000617 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000618 if (!FunctionBlocks.count(*I))
619 report("MBB has successor that isn't part of the function.", MBB);
620 if (!MBBInfoMap[*I].Preds.count(MBB)) {
621 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000622 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000623 << (*I)->getNumber() << ".\n";
624 }
625 }
626
627 // Check the predecessor list.
628 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
629 E = MBB->pred_end(); I != E; ++I) {
630 if (!FunctionBlocks.count(*I))
631 report("MBB has predecessor that isn't part of the function.", MBB);
632 if (!MBBInfoMap[*I].Succs.count(MBB)) {
633 report("Inconsistent CFG", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +0000634 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000635 << (*I)->getNumber() << ".\n";
636 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000637 }
Bill Wendling2a401312011-05-04 22:54:05 +0000638
639 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
640 const BasicBlock *BB = MBB->getBasicBlock();
Reid Kleckner64b003f2015-11-09 21:04:00 +0000641 const Function *Fn = MF->getFunction();
Bill Wendling2a401312011-05-04 22:54:05 +0000642 if (LandingPadSuccs.size() > 1 &&
643 !(AsmInfo &&
644 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
Reid Kleckner64b003f2015-11-09 21:04:00 +0000645 BB && isa<SwitchInst>(BB->getTerminator())) &&
646 !isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000647 report("MBB has more than one landing pad successor", MBB);
648
Dan Gohman352a4952009-08-27 02:43:49 +0000649 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000650 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000651 SmallVector<MachineOperand, 4> Cond;
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000652 if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
653 Cond)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000654 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
655 // check whether its answers match up with reality.
656 if (!TBB && !FBB) {
657 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000658 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000659 ++MBBI;
660 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000661 // It's possible that the block legitimately ends with a noreturn
662 // call or an unreachable, in which case it won't actually fall
663 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000664 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000665 // It's possible that the block legitimately ends with a noreturn
666 // call or an unreachable, in which case it won't actuall fall
667 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000668 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000669 report("MBB exits via unconditional fall-through but doesn't have "
670 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000671 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000672 report("MBB exits via unconditional fall-through but its successor "
673 "differs from its CFG successor!", MBB);
674 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000675 if (!MBB->empty() && MBB->back().isBarrier() &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000676 !TII->isPredicated(MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000677 report("MBB exits via unconditional fall-through but ends with a "
678 "barrier instruction!", MBB);
679 }
680 if (!Cond.empty()) {
681 report("MBB exits via unconditional fall-through but has a condition!",
682 MBB);
683 }
684 } else if (TBB && !FBB && Cond.empty()) {
685 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000686 // If the block has exactly one successor, that happens to be a
687 // landingpad, accept it as valid control flow.
688 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
689 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
690 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000691 report("MBB exits via unconditional branch but doesn't have "
692 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000693 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000694 report("MBB exits via unconditional branch but the CFG "
695 "successor doesn't match the actual successor!", MBB);
696 }
697 if (MBB->empty()) {
698 report("MBB exits via unconditional branch but doesn't contain "
699 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000700 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000701 report("MBB exits via unconditional branch but doesn't end with a "
702 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000703 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000704 report("MBB exits via unconditional branch but the branch isn't a "
705 "terminator instruction!", MBB);
706 }
707 } else if (TBB && !FBB && !Cond.empty()) {
708 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000709 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000710 ++MBBI;
711 if (MBBI == MF->end()) {
712 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000713 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000714 // A conditional branch with only one successor is weird, but allowed.
715 if (&*MBBI != TBB)
716 report("MBB exits via conditional branch/fall-through but only has "
717 "one CFG successor!", MBB);
718 else if (TBB != *MBB->succ_begin())
719 report("MBB exits via conditional branch/fall-through but the CFG "
720 "successor don't match the actual successor!", MBB);
721 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000722 report("MBB exits via conditional branch/fall-through but doesn't have "
723 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000724 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000725 report("MBB exits via conditional branch/fall-through but the CFG "
726 "successors don't match the actual successors!", MBB);
727 }
728 if (MBB->empty()) {
729 report("MBB exits via conditional branch/fall-through but doesn't "
730 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000731 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000732 report("MBB exits via conditional branch/fall-through but ends with a "
733 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000734 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000735 report("MBB exits via conditional branch/fall-through but the branch "
736 "isn't a terminator instruction!", MBB);
737 }
738 } else if (TBB && FBB) {
739 // Block conditionally branches somewhere, otherwise branches
740 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000741 if (MBB->succ_size() == 1) {
742 // A conditional branch with only one successor is weird, but allowed.
743 if (FBB != TBB)
744 report("MBB exits via conditional branch/branch through but only has "
745 "one CFG successor!", MBB);
746 else if (TBB != *MBB->succ_begin())
747 report("MBB exits via conditional branch/branch through but the CFG "
748 "successor don't match the actual successor!", MBB);
749 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000750 report("MBB exits via conditional branch/branch but doesn't have "
751 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000752 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000753 report("MBB exits via conditional branch/branch but the CFG "
754 "successors don't match the actual successors!", MBB);
755 }
756 if (MBB->empty()) {
757 report("MBB exits via conditional branch/branch but doesn't "
758 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000759 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000760 report("MBB exits via conditional branch/branch but doesn't end with a "
761 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000762 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000763 report("MBB exits via conditional branch/branch but the branch "
764 "isn't a terminator instruction!", MBB);
765 }
766 if (Cond.empty()) {
767 report("MBB exits via conditinal branch/branch but there's no "
768 "condition!", MBB);
769 }
770 } else {
771 report("AnalyzeBranch returned invalid data!", MBB);
772 }
773 }
774
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000775 regsLive.clear();
Matthias Braun11723322017-01-05 20:01:19 +0000776 if (MRI->tracksLiveness()) {
777 for (const auto &LI : MBB->liveins()) {
778 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
779 report("MBB live-in list contains non-physical register", MBB);
780 continue;
781 }
782 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
783 SubRegs.isValid(); ++SubRegs)
784 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000785 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000786 }
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000787
Matthias Braun941a7052016-07-28 18:40:00 +0000788 const MachineFrameInfo &MFI = MF->getFrameInfo();
789 BitVector PR = MFI.getPristineRegs(*MF);
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +0000790 for (unsigned I : PR.set_bits()) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000791 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
792 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000793 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000794 }
795
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000796 regsKilled.clear();
797 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000798
799 if (Indexes)
800 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000801}
802
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000803// This function gets called for all bundle headers, including normal
804// stand-alone unbundled instructions.
805void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000806 if (Indexes && Indexes->hasIndex(*MI)) {
807 SlotIndex idx = Indexes->getInstructionIndex(*MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000808 if (!(idx > lastIndex)) {
809 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000810 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000811 }
812 lastIndex = idx;
813 }
Pete Coopercd720162012-06-07 17:41:39 +0000814
815 // Ensure non-terminators don't follow terminators.
816 // Ignore predicated terminators formed by if conversion.
817 // FIXME: If conversion shouldn't need to violate this rule.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000818 if (MI->isTerminator() && !TII->isPredicated(*MI)) {
Pete Coopercd720162012-06-07 17:41:39 +0000819 if (!FirstTerminator)
820 FirstTerminator = MI;
821 } else if (FirstTerminator) {
822 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000823 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000824 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000825}
826
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000827// The operands on an INLINEASM instruction must follow a template.
828// Verify that the flag operands make sense.
829void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
830 // The first two operands on INLINEASM are the asm string and global flags.
831 if (MI->getNumOperands() < 2) {
832 report("Too few operands on inline asm", MI);
833 return;
834 }
835 if (!MI->getOperand(0).isSymbol())
836 report("Asm string must be an external symbol", MI);
837 if (!MI->getOperand(1).isImm())
838 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000839 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
Wei Ding0526e7f2016-06-22 18:51:08 +0000840 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
841 // and Extra_IsConvergent = 32.
842 if (!isUInt<6>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000843 report("Unknown asm flags", &MI->getOperand(1), 1);
844
Gabor Horvathfee04342015-03-16 09:53:42 +0000845 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000846
847 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
848 unsigned NumOps;
849 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
850 const MachineOperand &MO = MI->getOperand(OpNo);
851 // There may be implicit ops after the fixed operands.
852 if (!MO.isImm())
853 break;
854 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
855 }
856
857 if (OpNo > MI->getNumOperands())
858 report("Missing operands in last group", MI);
859
860 // An optional MDNode follows the groups.
861 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
862 ++OpNo;
863
864 // All trailing operands must be implicit registers.
865 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
866 const MachineOperand &MO = MI->getOperand(OpNo);
867 if (!MO.isReg() || !MO.isImplicit())
868 report("Expected implicit register after groups", &MO, OpNo);
869 }
870}
871
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000872void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000873 const MCInstrDesc &MCID = MI->getDesc();
874 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000875 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000876 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault23c92742013-11-15 22:18:19 +0000877 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000878 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000879
Matthias Braun90799ce2016-08-23 21:19:49 +0000880 if (MI->isPHI() && MF->getProperties().hasProperty(
881 MachineFunctionProperties::Property::NoPHIs))
882 report("Found PHI instruction with NoPHIs property set", MI);
883
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000884 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000885 if (MI->isInlineAsm())
886 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000887
Dan Gohmandb9493c2009-10-07 17:36:00 +0000888 // Check the MachineMemOperands for basic consistency.
889 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
890 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000891 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000892 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000893 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000894 report("Missing mayStore flag", MI);
895 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000896
897 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000898 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000899 if (LiveInts) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000900 bool mapped = !LiveInts->isNotInMIMap(*MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000901 if (MI->isDebugValue()) {
902 if (mapped)
903 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000904 } else if (MI->isInsideBundle()) {
905 if (mapped)
906 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000907 } else {
908 if (!mapped)
909 report("Missing slot index", MI);
910 }
911 }
912
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000913 // Check types.
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000914 if (isPreISelGenericOpcode(MCID.getOpcode())) {
Ahmed Bougachab14e9442016-08-02 16:49:22 +0000915 if (isFunctionSelected)
916 report("Unexpected generic instruction in a Selected function", MI);
917
Tim Northover0f140c72016-09-09 11:46:34 +0000918 // Generic instructions specify equality constraints between some
919 // of their operands. Make sure these are consistent.
920 SmallVector<LLT, 4> Types;
921 for (unsigned i = 0; i < MCID.getNumOperands(); ++i) {
922 if (!MCID.OpInfo[i].isGenericType())
923 continue;
924 size_t TypeIdx = MCID.OpInfo[i].getGenericTypeIndex();
925 Types.resize(std::max(TypeIdx + 1, Types.size()));
926
927 LLT OpTy = MRI->getType(MI->getOperand(i).getReg());
928 if (Types[TypeIdx].isValid() && Types[TypeIdx] != OpTy)
929 report("type mismatch in generic instruction", MI);
930 Types[TypeIdx] = OpTy;
931 }
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000932 }
933
Tim Northovere5102de2016-08-30 18:52:46 +0000934 // Generic opcodes must not have physical register operands.
Tim Northover25d12862016-09-09 11:47:31 +0000935 if (isPreISelGenericOpcode(MCID.getOpcode())) {
Tim Northovere5102de2016-08-30 18:52:46 +0000936 for (auto &Op : MI->operands()) {
937 if (Op.isReg() && TargetRegisterInfo::isPhysicalRegister(Op.getReg()))
938 report("Generic instruction cannot have physical register", MI);
939 }
940 }
941
Andrew Trick924123a2011-09-21 02:20:46 +0000942 StringRef ErrorInfo;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000943 if (!TII->verifyInstruction(*MI, ErrorInfo))
Andrew Trick924123a2011-09-21 02:20:46 +0000944 report(ErrorInfo.data(), MI);
Philip Reames94cc4a22017-06-02 16:36:37 +0000945
946 // Verify properties of various specific instruction types
947 switch(MI->getOpcode()) {
948 default:
949 break;
950 case TargetOpcode::G_LOAD:
951 case TargetOpcode::G_STORE:
952 // Generic loads and stores must have a single MachineMemOperand
953 // describing that access.
954 if (!MI->hasOneMemOperand())
955 report("Generic instruction accessing memory must have one mem operand",
956 MI);
957 break;
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000958 case TargetOpcode::G_PHI: {
959 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
960 if (!DstTy.isValid() ||
961 !std::all_of(MI->operands_begin() + 1, MI->operands_end(),
962 [this, &DstTy](const MachineOperand &MO) {
963 if (!MO.isReg())
964 return true;
965 LLT Ty = MRI->getType(MO.getReg());
966 if (!Ty.isValid() || (Ty != DstTy))
967 return false;
968 return true;
969 }))
970 report("Generic Instruction G_PHI has operands with incompatible/missing "
971 "types",
972 MI);
973 break;
974 }
Philip Reames94cc4a22017-06-02 16:36:37 +0000975 case TargetOpcode::STATEPOINT:
976 if (!MI->getOperand(StatepointOpers::IDPos).isImm() ||
977 !MI->getOperand(StatepointOpers::NBytesPos).isImm() ||
978 !MI->getOperand(StatepointOpers::NCallArgsPos).isImm())
979 report("meta operands to STATEPOINT not constant!", MI);
980 break;
Philip Reames0f02bbc2017-06-02 17:02:33 +0000981
982 auto VerifyStackMapConstant = [&](unsigned Offset) {
983 if (!MI->getOperand(Offset).isImm() ||
984 MI->getOperand(Offset).getImm() != StackMaps::ConstantOp ||
985 !MI->getOperand(Offset + 1).isImm())
986 report("stack map constant to STATEPOINT not well formed!", MI);
987 };
988 const unsigned VarStart = StatepointOpers(MI).getVarIdx();
989 VerifyStackMapConstant(VarStart + StatepointOpers::CCOffset);
990 VerifyStackMapConstant(VarStart + StatepointOpers::FlagsOffset);
991 VerifyStackMapConstant(VarStart + StatepointOpers::NumDeoptOperandsOffset);
992
993 // TODO: verify we have properly encoded deopt arguments
Philip Reames94cc4a22017-06-02 16:36:37 +0000994 };
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000995}
996
997void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000998MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000999 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001000 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +00001001 unsigned NumDefs = MCID.getNumDefs();
1002 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
1003 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +00001004
Evan Cheng6cc775f2011-06-28 19:10:37 +00001005 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +00001006 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +00001007 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +00001008 if (!MO->isReg())
1009 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +00001010 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +00001011 report("Explicit definition marked as use", MO, MONum);
1012 else if (MO->isImplicit())
1013 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001014 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +00001015 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +00001016 // Don't check if it's the last operand in a variadic instruction. See,
1017 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001018 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +00001019 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001020 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +00001021 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +00001022 if (MO->isImplicit())
1023 report("Explicit operand marked as implicit", MO, MONum);
1024 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +00001025
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +00001026 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
1027 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +00001028 if (!MO->isReg())
1029 report("Tied use must be a register", MO, MONum);
1030 else if (!MO->isTied())
1031 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +00001032 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
1033 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Mikael Holmen9c3e2ea2017-07-06 13:18:21 +00001034 else if (TargetRegisterInfo::isPhysicalRegister(MO->getReg())) {
1035 const MachineOperand &MOTied = MI->getOperand(TiedTo);
1036 if (!MOTied.isReg())
1037 report("Tied counterpart must be a register", &MOTied, TiedTo);
1038 else if (TargetRegisterInfo::isPhysicalRegister(MOTied.getReg()) &&
1039 MO->getReg() != MOTied.getReg())
1040 report("Tied physical registers must match.", &MOTied, TiedTo);
1041 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +00001042 } else if (MO->isReg() && MO->isTied())
1043 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +00001044 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +00001045 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001046 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +00001047 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +00001048 }
1049
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001050 switch (MO->getType()) {
1051 case MachineOperand::MO_Register: {
1052 const unsigned Reg = MO->getReg();
1053 if (!Reg)
1054 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001055 if (MRI->tracksLiveness() && !MI->isDebugValue())
1056 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001057
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +00001058 // Verify the consistency of tied operands.
1059 if (MO->isTied()) {
1060 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
1061 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
1062 if (!OtherMO.isReg())
1063 report("Must be tied to a register", MO, MONum);
1064 if (!OtherMO.isTied())
1065 report("Missing tie flags on tied operand", MO, MONum);
1066 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
1067 report("Inconsistent tie links", MO, MONum);
1068 if (MONum < MCID.getNumDefs()) {
1069 if (OtherIdx < MCID.getNumOperands()) {
1070 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
1071 report("Explicit def tied to explicit use without tie constraint",
1072 MO, MONum);
1073 } else {
1074 if (!OtherMO.isImplicit())
1075 report("Explicit def should be tied to implicit use", MO, MONum);
1076 }
1077 }
1078 }
1079
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001080 // Verify two-address constraints after leaving SSA form.
1081 unsigned DefIdx;
1082 if (!MRI->isSSA() && MO->isUse() &&
1083 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
1084 Reg != MI->getOperand(DefIdx).getReg())
1085 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001086
1087 // Check register classes.
Matthias Brauneca98582017-11-28 03:54:20 +00001088 unsigned SubIdx = MO->getSubReg();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001089
Matthias Brauneca98582017-11-28 03:54:20 +00001090 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1091 if (SubIdx) {
1092 report("Illegal subregister index for physical register", MO, MONum);
1093 return;
1094 }
1095 if (MONum < MCID.getNumOperands()) {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001096 if (const TargetRegisterClass *DRC =
1097 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001098 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001099 report("Illegal physical register for instruction", MO, MONum);
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001100 errs() << printReg(Reg, TRI) << " is not a "
1101 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001102 }
1103 }
Matthias Brauneca98582017-11-28 03:54:20 +00001104 }
1105 } else {
1106 // Virtual register.
1107 const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
1108 if (!RC) {
1109 // This is a generic virtual register.
Ahmed Bougachab14e9442016-08-02 16:49:22 +00001110
Matthias Brauneca98582017-11-28 03:54:20 +00001111 // If we're post-Select, we can't have gvregs anymore.
1112 if (isFunctionSelected) {
1113 report("Generic virtual register invalid in a Selected function",
1114 MO, MONum);
1115 return;
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001116 }
Matthias Brauneca98582017-11-28 03:54:20 +00001117
1118 // The gvreg must have a type and it must not have a SubIdx.
1119 LLT Ty = MRI->getType(Reg);
1120 if (!Ty.isValid()) {
1121 report("Generic virtual register must have a valid type", MO,
1122 MONum);
1123 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001124 }
Matthias Brauneca98582017-11-28 03:54:20 +00001125
1126 const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
1127
1128 // If we're post-RegBankSelect, the gvreg must have a bank.
1129 if (!RegBank && isFunctionRegBankSelected) {
1130 report("Generic virtual register must have a bank in a "
1131 "RegBankSelected function",
1132 MO, MONum);
1133 return;
1134 }
1135
1136 // Make sure the register fits into its register bank if any.
1137 if (RegBank && Ty.isValid() &&
1138 RegBank->getSize() < Ty.getSizeInBits()) {
1139 report("Register bank is too small for virtual register", MO,
1140 MONum);
1141 errs() << "Register bank " << RegBank->getName() << " too small("
1142 << RegBank->getSize() << ") to fit " << Ty.getSizeInBits()
1143 << "-bits\n";
1144 return;
1145 }
1146 if (SubIdx) {
1147 report("Generic virtual register does not subregister index", MO,
1148 MONum);
1149 return;
1150 }
1151
1152 // If this is a target specific instruction and this operand
1153 // has register class constraint, the virtual register must
1154 // comply to it.
1155 if (!isPreISelGenericOpcode(MCID.getOpcode()) &&
1156 MONum < MCID.getNumOperands() &&
1157 TII->getRegClass(MCID, MONum, TRI, *MF)) {
1158 report("Virtual register does not match instruction constraint", MO,
1159 MONum);
1160 errs() << "Expect register class "
1161 << TRI->getRegClassName(
1162 TII->getRegClass(MCID, MONum, TRI, *MF))
1163 << " but got nothing\n";
1164 return;
1165 }
1166
1167 break;
1168 }
1169 if (SubIdx) {
1170 const TargetRegisterClass *SRC =
1171 TRI->getSubClassWithSubReg(RC, SubIdx);
1172 if (!SRC) {
1173 report("Invalid subregister index for virtual register", MO, MONum);
1174 errs() << "Register class " << TRI->getRegClassName(RC)
1175 << " does not support subreg index " << SubIdx << "\n";
1176 return;
1177 }
1178 if (RC != SRC) {
1179 report("Invalid register class for subregister index", MO, MONum);
1180 errs() << "Register class " << TRI->getRegClassName(RC)
1181 << " does not fully support subreg index " << SubIdx << "\n";
1182 return;
1183 }
1184 }
1185 if (MONum < MCID.getNumOperands()) {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001186 if (const TargetRegisterClass *DRC =
1187 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001188 if (SubIdx) {
1189 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +00001190 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001191 if (!SuperRC) {
1192 report("No largest legal super class exists.", MO, MONum);
1193 return;
1194 }
1195 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
1196 if (!DRC) {
1197 report("No matching super-reg register class.", MO, MONum);
1198 return;
1199 }
1200 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +00001201 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001202 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001203 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +00001204 << " register, but got a " << TRI->getRegClassName(RC)
1205 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001206 }
1207 }
1208 }
1209 }
1210 break;
1211 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001212
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001213 case MachineOperand::MO_RegisterMask:
1214 regMasks.push_back(MO->getRegMask());
1215 break;
1216
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001217 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +00001218 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
1219 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001220 break;
1221
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001222 case MachineOperand::MO_FrameIndex:
1223 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001224 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
Jonas Paulsson72640f12015-10-29 08:28:35 +00001225 int FI = MO->getIndex();
1226 LiveInterval &LI = LiveStks->getInterval(FI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001227 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001228
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001229 bool stores = MI->mayStore();
Jonas Paulsson72640f12015-10-29 08:28:35 +00001230 bool loads = MI->mayLoad();
1231 // For a memory-to-memory move, we need to check if the frame
1232 // index is used for storing or loading, by inspecting the
1233 // memory operands.
1234 if (stores && loads) {
1235 for (auto *MMO : MI->memoperands()) {
1236 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1237 if (PSV == nullptr) continue;
1238 const FixedStackPseudoSourceValue *Value =
1239 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1240 if (Value == nullptr) continue;
1241 if (Value->getFrameIndex() != FI) continue;
1242
1243 if (MMO->isStore())
1244 loads = false;
1245 else
1246 stores = false;
1247 break;
1248 }
1249 if (loads == stores)
1250 report("Missing fixed stack memoperand.", MI);
1251 }
1252 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001253 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001254 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001255 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001256 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001257 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001258 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001259 }
1260 }
1261 break;
1262
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001263 default:
1264 break;
1265 }
1266}
1267
Matthias Braun1377fd62016-02-02 20:04:51 +00001268void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
1269 unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
1270 LaneBitmask LaneMask) {
1271 LiveQueryResult LRQ = LR.Query(UseIdx);
1272 // Check if we have a segment at the use, note however that we only need one
1273 // live subregister range, the others may be dead.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001274 if (!LRQ.valueIn() && LaneMask.none()) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001275 report("No live segment at use", MO, MONum);
1276 report_context_liverange(LR);
1277 report_context_vreg_regunit(VRegOrUnit);
1278 report_context(UseIdx);
1279 }
1280 if (MO->isKill() && !LRQ.isKill()) {
1281 report("Live range continues after kill flag", MO, MONum);
1282 report_context_liverange(LR);
1283 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001284 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001285 report_context_lanemask(LaneMask);
1286 report_context(UseIdx);
1287 }
1288}
1289
1290void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
1291 unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
1292 LaneBitmask LaneMask) {
1293 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
1294 assert(VNI && "NULL valno is not allowed");
1295 if (VNI->def != DefIdx) {
1296 report("Inconsistent valno->def", MO, MONum);
1297 report_context_liverange(LR);
1298 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001299 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001300 report_context_lanemask(LaneMask);
1301 report_context(*VNI);
1302 report_context(DefIdx);
1303 }
1304 } else {
1305 report("No live segment at def", MO, MONum);
1306 report_context_liverange(LR);
1307 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001308 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001309 report_context_lanemask(LaneMask);
1310 report_context(DefIdx);
1311 }
1312 // Check that, if the dead def flag is present, LiveInts agree.
1313 if (MO->isDead()) {
1314 LiveQueryResult LRQ = LR.Query(DefIdx);
1315 if (!LRQ.isDeadDef()) {
1316 // In case of physregs we can have a non-dead definition on another
1317 // operand.
1318 bool otherDef = false;
1319 if (!TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
1320 const MachineInstr &MI = *MO->getParent();
1321 for (const MachineOperand &MO : MI.operands()) {
1322 if (!MO.isReg() || !MO.isDef() || MO.isDead())
1323 continue;
1324 unsigned Reg = MO.getReg();
1325 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1326 if (*Units == VRegOrUnit) {
1327 otherDef = true;
1328 break;
1329 }
1330 }
1331 }
1332 }
1333
1334 if (!otherDef) {
1335 report("Live range continues after dead def flag", MO, MONum);
1336 report_context_liverange(LR);
1337 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001338 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001339 report_context_lanemask(LaneMask);
1340 }
1341 }
1342 }
1343}
1344
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001345void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1346 const MachineInstr *MI = MO->getParent();
1347 const unsigned Reg = MO->getReg();
1348
1349 // Both use and def operands can read a register.
1350 if (MO->readsReg()) {
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001351 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001352 addRegWithSubRegs(regsKilled, Reg);
1353
1354 // Check that LiveVars knows this kill.
1355 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1356 MO->isKill()) {
1357 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
David Majnemer0d955d02016-08-11 22:21:41 +00001358 if (!is_contained(VI.Kills, MI))
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001359 report("Kill missing from LiveVariables", MO, MONum);
1360 }
1361
1362 // Check LiveInts liveness and kill.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001363 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1364 SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001365 // Check the cached regunit intervals.
1366 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1367 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Brauncebdb172017-09-01 18:36:26 +00001368 if (MRI->isReservedRegUnit(*Units))
1369 continue;
Matthias Braun1377fd62016-02-02 20:04:51 +00001370 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
1371 checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001372 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001373 }
1374
1375 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1376 if (LiveInts->hasInterval(Reg)) {
1377 // This is a virtual register interval.
1378 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun1377fd62016-02-02 20:04:51 +00001379 checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
1380
1381 if (LI.hasSubRanges() && !MO->isDef()) {
1382 unsigned SubRegIdx = MO->getSubReg();
1383 LaneBitmask MOMask = SubRegIdx != 0
1384 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1385 : MRI->getMaxLaneMaskForVReg(Reg);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001386 LaneBitmask LiveInMask;
Matthias Braun1377fd62016-02-02 20:04:51 +00001387 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001388 if ((MOMask & SR.LaneMask).none())
Matthias Braun1377fd62016-02-02 20:04:51 +00001389 continue;
1390 checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
1391 LiveQueryResult LRQ = SR.Query(UseIdx);
1392 if (LRQ.valueIn())
1393 LiveInMask |= SR.LaneMask;
1394 }
1395 // At least parts of the register has to be live at the use.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001396 if ((LiveInMask & MOMask).none()) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001397 report("No live subrange at use", MO, MONum);
1398 report_context(LI);
1399 report_context(UseIdx);
1400 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001401 }
1402 } else {
1403 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001404 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001405 }
1406 }
1407
1408 // Use of a dead register.
1409 if (!regsLive.count(Reg)) {
1410 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1411 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001412 bool Bad = !isReserved(Reg);
1413 // We are fine if just any subregister has a defined value.
1414 if (Bad) {
1415 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1416 ++SubRegs) {
1417 if (regsLive.count(*SubRegs)) {
1418 Bad = false;
1419 break;
1420 }
1421 }
1422 }
Matthias Braun96a31952015-01-14 22:25:14 +00001423 // If there is an additional implicit-use of a super register we stop
1424 // here. By definition we are fine if the super register is not
1425 // (completely) dead, if the complete super register is dead we will
1426 // get a report for its operand.
1427 if (Bad) {
1428 for (const MachineOperand &MOP : MI->uses()) {
1429 if (!MOP.isReg())
1430 continue;
1431 if (!MOP.isImplicit())
1432 continue;
1433 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1434 ++SubRegs) {
1435 if (*SubRegs == Reg) {
1436 Bad = false;
1437 break;
1438 }
1439 }
1440 }
1441 }
Matthias Braun96d77322014-12-10 01:13:13 +00001442 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001443 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001444 } else if (MRI->def_empty(Reg)) {
1445 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001446 } else {
1447 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1448 // We don't know which virtual registers are live in, so only complain
1449 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1450 // must be live in. PHI instructions are handled separately.
1451 if (MInfo.regsKilled.count(Reg))
1452 report("Using a killed virtual register", MO, MONum);
1453 else if (!MI->isPHI())
1454 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1455 }
1456 }
1457 }
1458
1459 if (MO->isDef()) {
1460 // Register defined.
1461 // TODO: verify that earlyclobber ops are not used.
1462 if (MO->isDead())
1463 addRegWithSubRegs(regsDead, Reg);
1464 else
1465 addRegWithSubRegs(regsDefined, Reg);
1466
1467 // Verify SSA form.
1468 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001469 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001470 report("Multiple virtual register defs in SSA form", MO, MONum);
1471
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001472 // Check LiveInts for a live segment, but only for virtual registers.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001473 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1474 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001475 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Matthias Braun1377fd62016-02-02 20:04:51 +00001476
1477 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1478 if (LiveInts->hasInterval(Reg)) {
1479 const LiveInterval &LI = LiveInts->getInterval(Reg);
1480 checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
1481
1482 if (LI.hasSubRanges()) {
1483 unsigned SubRegIdx = MO->getSubReg();
1484 LaneBitmask MOMask = SubRegIdx != 0
1485 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1486 : MRI->getMaxLaneMaskForVReg(Reg);
1487 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001488 if ((SR.LaneMask & MOMask).none())
Matthias Braun1377fd62016-02-02 20:04:51 +00001489 continue;
1490 checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, SR.LaneMask);
1491 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001492 }
1493 } else {
Matthias Braun1377fd62016-02-02 20:04:51 +00001494 report("Virtual register has no Live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001495 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001496 }
1497 }
1498 }
1499}
1500
Eugene Zelenko32a40562017-09-11 23:00:48 +00001501void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {}
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001502
1503// This function gets called after visiting all instructions in a bundle. The
1504// argument points to the bundle header.
1505// Normal stand-alone instructions are also considered 'bundles', and this
1506// function is called for all of them.
1507void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001508 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1509 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001510 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001511 // Kill any masked registers.
1512 while (!regMasks.empty()) {
1513 const uint32_t *Mask = regMasks.pop_back_val();
1514 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1515 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1516 MachineOperand::clobbersPhysReg(Mask, *I))
1517 regsDead.push_back(*I);
1518 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001519 set_subtract(regsLive, regsDead); regsDead.clear();
1520 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001521}
1522
1523void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001524MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001525 MBBInfoMap[MBB].regsLiveOut = regsLive;
1526 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001527
1528 if (Indexes) {
1529 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1530 if (!(stop > lastIndex)) {
1531 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001532 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001533 << " last instruction was at " << lastIndex << '\n';
1534 }
1535 lastIndex = stop;
1536 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001537}
1538
1539// Calculate the largest possible vregsPassed sets. These are the registers that
1540// can pass through an MBB live, but may not be live every time. It is assumed
1541// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001542void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001543 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1544 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001545 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001546 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001547 BBInfo &MInfo = MBBInfoMap[&MBB];
1548 if (!MInfo.reachable)
1549 continue;
1550 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1551 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1552 BBInfo &SInfo = MBBInfoMap[*SuI];
1553 if (SInfo.addPassed(MInfo.regsLiveOut))
1554 todo.insert(*SuI);
1555 }
1556 }
1557
1558 // Iteratively push vregsPassed to successors. This will converge to the same
1559 // final state regardless of DenseSet iteration order.
1560 while (!todo.empty()) {
1561 const MachineBasicBlock *MBB = *todo.begin();
1562 todo.erase(MBB);
1563 BBInfo &MInfo = MBBInfoMap[MBB];
1564 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1565 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1566 if (*SuI == MBB)
1567 continue;
1568 BBInfo &SInfo = MBBInfoMap[*SuI];
1569 if (SInfo.addPassed(MInfo.vregsPassed))
1570 todo.insert(*SuI);
1571 }
1572 }
1573}
1574
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001575// Calculate the set of virtual registers that must be passed through each basic
1576// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001577// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001578void MachineVerifier::calcRegsRequired() {
1579 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001580 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001581 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001582 BBInfo &MInfo = MBBInfoMap[&MBB];
1583 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1584 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1585 BBInfo &PInfo = MBBInfoMap[*PrI];
1586 if (PInfo.addRequired(MInfo.vregsLiveIn))
1587 todo.insert(*PrI);
1588 }
1589 }
1590
1591 // Iteratively push vregsRequired to predecessors. This will converge to the
1592 // same final state regardless of DenseSet iteration order.
1593 while (!todo.empty()) {
1594 const MachineBasicBlock *MBB = *todo.begin();
1595 todo.erase(MBB);
1596 BBInfo &MInfo = MBBInfoMap[MBB];
1597 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1598 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1599 if (*PrI == MBB)
1600 continue;
1601 BBInfo &SInfo = MBBInfoMap[*PrI];
1602 if (SInfo.addRequired(MInfo.vregsRequired))
1603 todo.insert(*PrI);
1604 }
1605 }
1606}
1607
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001608// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001609// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Matthias Brauna6d53742017-11-28 03:54:19 +00001610void MachineVerifier::checkPHIOps(const MachineBasicBlock &MBB) {
1611 BBInfo &MInfo = MBBInfoMap[&MBB];
1612
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001613 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Matthias Brauna6d53742017-11-28 03:54:19 +00001614 for (const MachineInstr &Phi : MBB) {
1615 if (!Phi.isPHI())
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001616 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001617 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001618
Matthias Brauna6d53742017-11-28 03:54:19 +00001619 const MachineOperand &MODef = Phi.getOperand(0);
1620 if (!MODef.isReg() || !MODef.isDef()) {
1621 report("Expected first PHI operand to be a register def", &MODef, 0);
1622 continue;
1623 }
1624 if (MODef.isTied() || MODef.isImplicit() || MODef.isInternalRead() ||
1625 MODef.isEarlyClobber() || MODef.isDebug())
1626 report("Unexpected flag on PHI operand", &MODef, 0);
1627 unsigned DefReg = MODef.getReg();
1628 if (!TargetRegisterInfo::isVirtualRegister(DefReg))
1629 report("Expected first PHI operand to be a virtual register", &MODef, 0);
1630
1631 for (unsigned I = 1, E = Phi.getNumOperands(); I != E; I += 2) {
1632 const MachineOperand &MO0 = Phi.getOperand(I);
1633 if (!MO0.isReg()) {
1634 report("Expected PHI operand to be a register", &MO0, I);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001635 continue;
Matthias Brauna6d53742017-11-28 03:54:19 +00001636 }
1637 if (MO0.isImplicit() || MO0.isInternalRead() || MO0.isEarlyClobber() ||
1638 MO0.isDebug() || MO0.isTied())
1639 report("Unexpected flag on PHI operand", &MO0, I);
1640
1641 const MachineOperand &MO1 = Phi.getOperand(I + 1);
1642 if (!MO1.isMBB()) {
1643 report("Expected PHI operand to be a basic block", &MO1, I + 1);
1644 continue;
1645 }
1646
1647 const MachineBasicBlock &Pre = *MO1.getMBB();
1648 if (!Pre.isSuccessor(&MBB)) {
1649 report("PHI input is not a predecessor block", &MO1, I + 1);
1650 continue;
1651 }
1652
1653 if (MInfo.reachable) {
1654 seen.insert(&Pre);
1655 BBInfo &PrInfo = MBBInfoMap[&Pre];
1656 if (PrInfo.reachable && !PrInfo.isLiveOut(MO0.getReg()))
1657 report("PHI operand is not live-out from predecessor", &MO0, I);
1658 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001659 }
1660
1661 // Did we see all predecessors?
Matthias Brauna6d53742017-11-28 03:54:19 +00001662 if (MInfo.reachable) {
1663 for (MachineBasicBlock *Pred : MBB.predecessors()) {
1664 if (!seen.count(Pred)) {
1665 report("Missing PHI operand", &Phi);
1666 errs() << "BB#" << Pred->getNumber()
1667 << " is a predecessor according to the CFG.\n";
1668 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001669 }
1670 }
1671 }
1672}
1673
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001674void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001675 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001676
Matthias Brauna6d53742017-11-28 03:54:19 +00001677 for (const MachineBasicBlock &MBB : *MF)
1678 checkPHIOps(MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001679
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001680 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001681 calcRegsRequired();
1682
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001683 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001684 for (const auto &MBB : *MF) {
1685 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001686 for (RegSet::iterator
1687 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1688 ++I)
1689 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001690 report("Virtual register killed in block, but needed live out.", &MBB);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001691 errs() << "Virtual register " << printReg(*I)
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001692 << " is used after the block.\n";
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001693 }
1694 }
1695
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001696 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001697 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1698 for (RegSet::iterator
1699 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Matthias Braun30668dd2016-05-11 21:31:39 +00001700 ++I) {
1701 report("Virtual register defs don't dominate all uses.", MF);
1702 report_context_vreg(*I);
1703 }
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001704 }
1705
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001706 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001707 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001708 if (LiveInts)
1709 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001710}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001711
1712void MachineVerifier::verifyLiveVariables() {
1713 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001714 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1715 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001716 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001717 for (const auto &MBB : *MF) {
1718 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001719
1720 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1721 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001722 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1723 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001724 errs() << "Virtual register " << printReg(Reg)
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001725 << " must be live through the block.\n";
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001726 }
1727 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001728 if (VI.AliveBlocks.test(MBB.getNumber())) {
1729 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001730 errs() << "Virtual register " << printReg(Reg)
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001731 << " is not needed live through the block.\n";
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001732 }
1733 }
1734 }
1735 }
1736}
1737
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001738void MachineVerifier::verifyLiveIntervals() {
1739 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001740 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1741 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001742
1743 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001744 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001745 continue;
1746
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001747 if (!LiveInts->hasInterval(Reg)) {
1748 report("Missing live interval for virtual register", MF);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001749 errs() << printReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001750 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001751 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001752
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001753 const LiveInterval &LI = LiveInts->getInterval(Reg);
1754 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001755 verifyLiveInterval(LI);
1756 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001757
1758 // Verify all the cached regunit intervals.
1759 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001760 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1761 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001762}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001763
Matthias Braun364e6e92013-10-10 21:28:54 +00001764void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001765 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001766 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001767 if (VNI->isUnused())
1768 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001769
Matthias Braun364e6e92013-10-10 21:28:54 +00001770 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001771
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001772 if (!DefVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001773 report("Value not live at VNInfo def and not marked unused", MF);
1774 report_context(LR, Reg, LaneMask);
1775 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001776 return;
1777 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001778
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001779 if (DefVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001780 report("Live segment at def has different VNInfo", MF);
1781 report_context(LR, Reg, LaneMask);
1782 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001783 return;
1784 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001785
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001786 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1787 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001788 report("Invalid VNInfo definition index", MF);
1789 report_context(LR, Reg, LaneMask);
1790 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001791 return;
1792 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001793
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001794 if (VNI->isPHIDef()) {
1795 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001796 report("PHIDef VNInfo is not defined at MBB start", MBB);
1797 report_context(LR, Reg, LaneMask);
1798 report_context(*VNI);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001799 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001800 return;
1801 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001802
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001803 // Non-PHI def.
1804 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1805 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001806 report("No instruction at VNInfo def index", MBB);
1807 report_context(LR, Reg, LaneMask);
1808 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001809 return;
1810 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001811
Matthias Braun364e6e92013-10-10 21:28:54 +00001812 if (Reg != 0) {
1813 bool hasDef = false;
1814 bool isEarlyClobber = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001815 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001816 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001817 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001818 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1819 if (MOI->getReg() != Reg)
1820 continue;
1821 } else {
1822 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1823 !TRI->hasRegUnit(MOI->getReg(), Reg))
1824 continue;
1825 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001826 if (LaneMask.any() &&
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001827 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask).none())
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001828 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001829 hasDef = true;
1830 if (MOI->isEarlyClobber())
1831 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001832 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001833
Matthias Braun364e6e92013-10-10 21:28:54 +00001834 if (!hasDef) {
1835 report("Defining instruction does not modify register", MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001836 report_context(LR, Reg, LaneMask);
1837 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001838 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001839
Matthias Braun364e6e92013-10-10 21:28:54 +00001840 // Early clobber defs begin at USE slots, but other defs must begin at
1841 // DEF slots.
1842 if (isEarlyClobber) {
1843 if (!VNI->def.isEarlyClobber()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001844 report("Early clobber def must be at an early-clobber slot", MBB);
1845 report_context(LR, Reg, LaneMask);
1846 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001847 }
1848 } else if (!VNI->def.isRegister()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001849 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
1850 report_context(LR, Reg, LaneMask);
1851 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001852 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001853 }
1854}
1855
Matthias Braun364e6e92013-10-10 21:28:54 +00001856void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1857 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001858 unsigned Reg, LaneBitmask LaneMask)
1859{
Matthias Braun364e6e92013-10-10 21:28:54 +00001860 const LiveRange::Segment &S = *I;
1861 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001862 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001863
Matthias Braun364e6e92013-10-10 21:28:54 +00001864 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001865 report("Foreign valno in live segment", MF);
1866 report_context(LR, Reg, LaneMask);
1867 report_context(S);
1868 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001869 }
1870
1871 if (VNI->isUnused()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001872 report("Live segment valno is marked unused", MF);
1873 report_context(LR, Reg, LaneMask);
1874 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001875 }
1876
Matthias Braun364e6e92013-10-10 21:28:54 +00001877 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001878 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001879 report("Bad start of live segment, no basic block", MF);
1880 report_context(LR, Reg, LaneMask);
1881 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001882 return;
1883 }
1884 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001885 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001886 report("Live segment must begin at MBB entry or valno def", MBB);
1887 report_context(LR, Reg, LaneMask);
1888 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001889 }
1890
1891 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001892 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001893 if (!EndMBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001894 report("Bad end of live segment, no basic block", MF);
1895 report_context(LR, Reg, LaneMask);
1896 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001897 return;
1898 }
1899
1900 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00001901 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001902 return;
1903
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001904 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00001905 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1906 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001907 return;
1908
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001909 // The live segment is ending inside EndMBB
1910 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00001911 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001912 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001913 report("Live segment doesn't end at a valid instruction", EndMBB);
1914 report_context(LR, Reg, LaneMask);
1915 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001916 return;
1917 }
1918
1919 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00001920 if (S.end.isBlock()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001921 report("Live segment ends at B slot of an instruction", EndMBB);
1922 report_context(LR, Reg, LaneMask);
1923 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001924 }
1925
Matthias Braun364e6e92013-10-10 21:28:54 +00001926 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001927 // Segment ends on the dead slot.
1928 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001929 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001930 report("Live segment ending at dead slot spans instructions", EndMBB);
1931 report_context(LR, Reg, LaneMask);
1932 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001933 }
1934 }
1935
1936 // A live segment can only end at an early-clobber slot if it is being
1937 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00001938 if (S.end.isEarlyClobber()) {
1939 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001940 report("Live segment ending at early clobber slot must be "
Matthias Braun7e624d52015-11-09 23:59:33 +00001941 "redefined by an EC def in the same instruction", EndMBB);
1942 report_context(LR, Reg, LaneMask);
1943 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001944 }
1945 }
1946
1947 // The following checks only apply to virtual registers. Physreg liveness
1948 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00001949 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001950 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001951 // use, or a dead flag on a def.
1952 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00001953 bool hasSubRegDef = false;
Matthias Braun72a58c32016-03-29 19:07:43 +00001954 bool hasDeadDef = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001955 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001956 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001957 continue;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001958 unsigned Sub = MOI->getSubReg();
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001959 LaneBitmask SLM = Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub)
1960 : LaneBitmask::getAll();
Matthias Braun72a58c32016-03-29 19:07:43 +00001961 if (MOI->isDef()) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001962 if (Sub != 0) {
Matthias Braun72a58c32016-03-29 19:07:43 +00001963 hasSubRegDef = true;
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +00001964 // An operand %0:sub0<def> reads %0:sub1..n. Invert the lane
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001965 // mask for subregister defs. Read-undef defs will be handled by
1966 // readsReg below.
Krzysztof Parzyszek0a955d62016-08-29 13:15:35 +00001967 SLM = ~SLM;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001968 }
Matthias Braun72a58c32016-03-29 19:07:43 +00001969 if (MOI->isDead())
1970 hasDeadDef = true;
1971 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001972 if (LaneMask.any() && (LaneMask & SLM).none())
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001973 continue;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001974 if (MOI->readsReg())
1975 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001976 }
Matthias Braun72a58c32016-03-29 19:07:43 +00001977 if (S.end.isDead()) {
1978 // Make sure that the corresponding machine operand for a "dead" live
1979 // range has the dead flag. We cannot perform this check for subregister
1980 // liveranges as partially dead values are allowed.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001981 if (LaneMask.none() && !hasDeadDef) {
Matthias Braun72a58c32016-03-29 19:07:43 +00001982 report("Instruction ending live segment on dead slot has no dead flag",
1983 MI);
1984 report_context(LR, Reg, LaneMask);
1985 report_context(S);
1986 }
1987 } else {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001988 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00001989 // When tracking subregister liveness, the main range must start new
1990 // values on partial register writes, even if there is no read.
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001991 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask.any() ||
Matthias Brauna25e13a2015-03-19 00:21:58 +00001992 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00001993 report("Instruction ending live segment doesn't read the register",
1994 MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001995 report_context(LR, Reg, LaneMask);
1996 report_context(S);
Matthias Braun21554d92014-12-10 01:13:11 +00001997 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001998 }
1999 }
2000 }
2001
2002 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002003 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00002004 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00002005 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002006 // Not live-in to any blocks.
2007 if (MBB == EndMBB)
2008 return;
2009 // Skip this block.
2010 ++MFI;
2011 }
Eugene Zelenko32a40562017-09-11 23:00:48 +00002012 while (true) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002013 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002014 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00002015 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00002016 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002017 if (&*MFI == EndMBB)
2018 break;
2019 ++MFI;
2020 continue;
2021 }
2022
2023 // Is VNI a PHI-def in the current block?
2024 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002025 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002026
2027 // Check that VNI is live-out of all predecessors.
2028 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
2029 PE = MFI->pred_end(); PI != PE; ++PI) {
2030 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00002031 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002032
Matthias Braun1ee25e02017-06-08 21:30:54 +00002033 // All predecessors must have a live-out value. However for a phi
2034 // instruction with subregister intervals
2035 // only one of the subregisters (not necessarily the current one) needs to
2036 // be defined.
2037 if (!PVNI && (LaneMask.none() || !IsPHI) ) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002038 report("Register not marked live out of predecessor", *PI);
2039 report_context(LR, Reg, LaneMask);
2040 report_context(*VNI);
2041 errs() << " live into BB#" << MFI->getNumber()
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002042 << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
2043 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002044 continue;
2045 }
2046
2047 // Only PHI-defs can take different predecessor values.
2048 if (!IsPHI && PVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002049 report("Different value live out of predecessor", *PI);
2050 report_context(LR, Reg, LaneMask);
Owen Anderson21b17882015-02-04 00:02:59 +00002051 errs() << "Valno #" << PVNI->id << " live out of BB#"
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002052 << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
2053 << " live into BB#" << MFI->getNumber() << '@'
2054 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002055 }
2056 }
2057 if (&*MFI == EndMBB)
2058 break;
2059 ++MFI;
2060 }
2061}
2062
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002063void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00002064 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00002065 for (const VNInfo *VNI : LR.valnos)
2066 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002067
Matthias Braun364e6e92013-10-10 21:28:54 +00002068 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002069 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00002070}
2071
2072void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002073 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00002074 assert(TargetRegisterInfo::isVirtualRegister(Reg));
2075 verifyLiveRange(LI, Reg);
2076
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00002077 LaneBitmask Mask;
Matthias Braune6a24852015-09-25 21:51:14 +00002078 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00002079 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00002080 if ((Mask & SR.LaneMask).any()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002081 report("Lane masks of sub ranges overlap in live interval", MF);
2082 report_context(LI);
2083 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00002084 if ((SR.LaneMask & ~MaxMask).any()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002085 report("Subrange lanemask is invalid", MF);
2086 report_context(LI);
2087 }
2088 if (SR.empty()) {
2089 report("Subrange must not be empty", MF);
2090 report_context(SR, LI.reg, SR.LaneMask);
2091 }
Matthias Braune962e522015-03-25 21:18:22 +00002092 Mask |= SR.LaneMask;
2093 verifyLiveRange(SR, LI.reg, SR.LaneMask);
Matthias Braun7e624d52015-11-09 23:59:33 +00002094 if (!LI.covers(SR)) {
2095 report("A Subrange is not covered by the main range", MF);
2096 report_context(LI);
2097 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002098 }
2099
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002100 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00002101 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
Matthias Braunbf47f632016-01-08 01:16:35 +00002102 unsigned NumComp = ConEQ.Classify(LI);
Matthias Braune962e522015-03-25 21:18:22 +00002103 if (NumComp > 1) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002104 report("Multiple connected components in live interval", MF);
2105 report_context(LI);
Matthias Braune962e522015-03-25 21:18:22 +00002106 for (unsigned comp = 0; comp != NumComp; ++comp) {
2107 errs() << comp << ": valnos";
2108 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
2109 E = LI.vni_end(); I!=E; ++I)
2110 if (comp == ConEQ.getEqClass(*I))
2111 errs() << ' ' << (*I)->id;
2112 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00002113 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002114 }
2115}
Manman Renaa6875b2013-07-15 21:26:31 +00002116
2117namespace {
Eugene Zelenko32a40562017-09-11 23:00:48 +00002118
Manman Renaa6875b2013-07-15 21:26:31 +00002119 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
2120 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
2121 // value is zero.
2122 // We use a bool plus an integer to capture the stack state.
2123 struct StackStateOfBB {
Eugene Zelenko32a40562017-09-11 23:00:48 +00002124 StackStateOfBB() = default;
Manman Renaa6875b2013-07-15 21:26:31 +00002125 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
2126 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
Eugene Zelenko32a40562017-09-11 23:00:48 +00002127 ExitIsSetup(ExitSetup) {}
2128
Manman Renaa6875b2013-07-15 21:26:31 +00002129 // Can be negative, which means we are setting up a frame.
Eugene Zelenko32a40562017-09-11 23:00:48 +00002130 int EntryValue = 0;
2131 int ExitValue = 0;
2132 bool EntryIsSetup = false;
2133 bool ExitIsSetup = false;
Manman Renaa6875b2013-07-15 21:26:31 +00002134 };
Eugene Zelenko32a40562017-09-11 23:00:48 +00002135
2136} // end anonymous namespace
Manman Renaa6875b2013-07-15 21:26:31 +00002137
2138/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
2139/// by a FrameDestroy <n>, stack adjustments are identical on all
2140/// CFG edges to a merge point, and frame is destroyed at end of a return block.
2141void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00002142 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
2143 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Serge Pavlov802aa662017-04-20 01:34:04 +00002144 if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
2145 return;
Manman Renaa6875b2013-07-15 21:26:31 +00002146
2147 SmallVector<StackStateOfBB, 8> SPState;
2148 SPState.resize(MF->getNumBlockIDs());
David Callahanc1051ab2016-10-05 21:36:16 +00002149 df_iterator_default_set<const MachineBasicBlock*> Reachable;
Manman Renaa6875b2013-07-15 21:26:31 +00002150
2151 // Visit the MBBs in DFS order.
Eugene Zelenko32a40562017-09-11 23:00:48 +00002152 for (df_ext_iterator<const MachineFunction *,
2153 df_iterator_default_set<const MachineBasicBlock *>>
Manman Renaa6875b2013-07-15 21:26:31 +00002154 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
2155 DFI != DFE; ++DFI) {
2156 const MachineBasicBlock *MBB = *DFI;
2157
2158 StackStateOfBB BBState;
2159 // Check the exit state of the DFS stack predecessor.
2160 if (DFI.getPathLength() >= 2) {
2161 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
2162 assert(Reachable.count(StackPred) &&
2163 "DFS stack predecessor is already visited.\n");
2164 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
2165 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
2166 BBState.ExitValue = BBState.EntryValue;
2167 BBState.ExitIsSetup = BBState.EntryIsSetup;
2168 }
2169
2170 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002171 for (const auto &I : *MBB) {
2172 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00002173 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002174 report("FrameSetup is after another FrameSetup", &I);
Serge Pavlovd526b132017-05-09 13:35:13 +00002175 BBState.ExitValue -= TII->getFrameTotalSize(I);
Manman Renaa6875b2013-07-15 21:26:31 +00002176 BBState.ExitIsSetup = true;
2177 }
2178
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002179 if (I.getOpcode() == FrameDestroyOpcode) {
Serge Pavlovd526b132017-05-09 13:35:13 +00002180 int Size = TII->getFrameTotalSize(I);
Manman Renaa6875b2013-07-15 21:26:31 +00002181 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002182 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00002183 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
2184 BBState.ExitValue;
2185 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002186 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00002187 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00002188 << AbsSPAdj << ">.\n";
2189 }
2190 BBState.ExitValue += Size;
2191 BBState.ExitIsSetup = false;
2192 }
2193 }
2194 SPState[MBB->getNumber()] = BBState;
2195
2196 // Make sure the exit state of any predecessor is consistent with the entry
2197 // state.
2198 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
2199 E = MBB->pred_end(); I != E; ++I) {
2200 if (Reachable.count(*I) &&
2201 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
2202 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
2203 report("The exit stack state of a predecessor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002204 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002205 << SPState[(*I)->getNumber()].ExitValue << ", "
2206 << SPState[(*I)->getNumber()].ExitIsSetup
2207 << "), while BB#" << MBB->getNumber() << " has entry state ("
2208 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
2209 }
2210 }
2211
2212 // Make sure the entry state of any successor is consistent with the exit
2213 // state.
2214 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
2215 E = MBB->succ_end(); I != E; ++I) {
2216 if (Reachable.count(*I) &&
2217 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
2218 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
2219 report("The entry stack state of a successor is inconsistent.", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002220 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Renaa6875b2013-07-15 21:26:31 +00002221 << SPState[(*I)->getNumber()].EntryValue << ", "
2222 << SPState[(*I)->getNumber()].EntryIsSetup
2223 << "), while BB#" << MBB->getNumber() << " has exit state ("
2224 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
2225 }
2226 }
2227
2228 // Make sure a basic block with return ends with zero stack adjustment.
2229 if (!MBB->empty() && MBB->back().isReturn()) {
2230 if (BBState.ExitIsSetup)
2231 report("A return block ends with a FrameSetup.", MBB);
2232 if (BBState.ExitValue)
2233 report("A return block ends with a nonzero stack adjustment.", MBB);
2234 }
2235 }
2236}