blob: 310316c491bf1da290aeddfc00cf72f2812d291e [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"
Matthias Braunf8422972017-12-13 02:51:04 +000039#include "llvm/CodeGen/LiveIntervals.h"
Matthias Braunef959692017-12-18 23:19:44 +000040#include "llvm/CodeGen/LiveStacks.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#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);
Roman Tereshinf487eda2018-05-07 22:31:12 +0000242 void report(const char *msg, const MachineOperand *MO, unsigned MONum,
243 LLT MOVRegType = LLT{});
Matthias Braun7e624d52015-11-09 23:59:33 +0000244
245 void report_context(const LiveInterval &LI) const;
Matt Arsenault892fcd02016-07-25 19:39:01 +0000246 void report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000247 LaneBitmask LaneMask) const;
248 void report_context(const LiveRange::Segment &S) const;
249 void report_context(const VNInfo &VNI) const;
Matthias Braun579c9cd2016-02-02 02:44:25 +0000250 void report_context(SlotIndex Pos) const;
251 void report_context_liverange(const LiveRange &LR) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000252 void report_context_lanemask(LaneBitmask LaneMask) const;
Matthias Braun30668dd2016-05-11 21:31:39 +0000253 void report_context_vreg(unsigned VReg) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000254 void report_context_vreg_regunit(unsigned VRegOrRegUnit) const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000255
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000256 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000257
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000258 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Matthias Braun1377fd62016-02-02 20:04:51 +0000259 void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
260 SlotIndex UseIdx, const LiveRange &LR, unsigned Reg,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000261 LaneBitmask LaneMask = LaneBitmask::getNone());
Matthias Braun1377fd62016-02-02 20:04:51 +0000262 void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
263 SlotIndex DefIdx, const LiveRange &LR, unsigned Reg,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000264 LaneBitmask LaneMask = LaneBitmask::getNone());
Matthias Braun1377fd62016-02-02 20:04:51 +0000265
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000266 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000267 void calcRegsPassed();
Matthias Brauna6d53742017-11-28 03:54:19 +0000268 void checkPHIOps(const MachineBasicBlock &MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000269
270 void calcRegsRequired();
271 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000272 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000273 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000274 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000275 LaneBitmask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000276 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000277 const LiveRange::const_iterator I, unsigned,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000278 LaneBitmask);
279 void verifyLiveRange(const LiveRange&, unsigned,
280 LaneBitmask LaneMask = LaneBitmask::getNone());
Manman Renaa6875b2013-07-15 21:26:31 +0000281
282 void verifyStackFrame();
Matthias Braun80595462015-09-09 17:49:46 +0000283
284 void verifySlotIndexes() const;
Derek Schuff42666ee2016-03-29 17:40:22 +0000285 void verifyProperties(const MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000286 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000287
288 struct MachineVerifierPass : public MachineFunctionPass {
289 static char ID; // Pass ID, replacement for typeid
Eugene Zelenko32a40562017-09-11 23:00:48 +0000290
Matthias Brauna4e932d2014-12-11 19:41:51 +0000291 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000292
Sven van Haastregt04bfa872017-03-29 15:25:06 +0000293 MachineVerifierPass(std::string banner = std::string())
Sven van Haastregt039a6d92017-03-29 09:08:25 +0000294 : MachineFunctionPass(ID), Banner(std::move(banner)) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000295 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
296 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000297
Craig Topper4584cd52014-03-07 09:26:03 +0000298 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000299 AU.setPreservesAll();
300 MachineFunctionPass::getAnalysisUsage(AU);
301 }
302
Craig Topper4584cd52014-03-07 09:26:03 +0000303 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Braunb3aefc32016-02-15 19:25:31 +0000304 unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
305 if (FoundErrors)
306 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000307 return false;
308 }
309 };
310
Eugene Zelenko32a40562017-09-11 23:00:48 +0000311} // end anonymous namespace
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000312
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000313char MachineVerifierPass::ID = 0;
Eugene Zelenko32a40562017-09-11 23:00:48 +0000314
Owen Andersond31d82d2010-08-23 17:52:01 +0000315INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000316 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000317
Matthias Brauna4e932d2014-12-11 19:41:51 +0000318FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000319 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000320}
321
Matthias Braunb3aefc32016-02-15 19:25:31 +0000322bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
323 const {
324 MachineFunction &MF = const_cast<MachineFunction&>(*this);
325 unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
326 if (AbortOnErrors && FoundErrors)
327 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
328 return FoundErrors == 0;
Jakob Stoklund Olesen27440e72009-11-13 21:56:09 +0000329}
330
Matthias Braun80595462015-09-09 17:49:46 +0000331void MachineVerifier::verifySlotIndexes() const {
332 if (Indexes == nullptr)
333 return;
334
335 // Ensure the IdxMBB list is sorted by slot indexes.
336 SlotIndex Last;
337 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
338 E = Indexes->MBBIndexEnd(); I != E; ++I) {
339 assert(!Last.isValid() || I->first > Last);
340 Last = I->first;
341 }
342}
343
Derek Schuff42666ee2016-03-29 17:40:22 +0000344void MachineVerifier::verifyProperties(const MachineFunction &MF) {
345 // If a pass has introduced virtual registers without clearing the
Matthias Braun1eb47362016-08-25 01:27:13 +0000346 // NoVRegs property (or set it without allocating the vregs)
Derek Schuff42666ee2016-03-29 17:40:22 +0000347 // then report an error.
348 if (MF.getProperties().hasProperty(
Matthias Braun1eb47362016-08-25 01:27:13 +0000349 MachineFunctionProperties::Property::NoVRegs) &&
350 MRI->getNumVirtRegs())
351 report("Function has NoVRegs property but there are VReg operands", &MF);
Derek Schuff42666ee2016-03-29 17:40:22 +0000352}
353
Matthias Braunb3aefc32016-02-15 19:25:31 +0000354unsigned MachineVerifier::verify(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000355 foundErrors = 0;
356
357 this->MF = &MF;
358 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000359 TII = MF.getSubtarget().getInstrInfo();
360 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000361 MRI = &MF.getRegInfo();
362
Roman Tereshin3054ece2018-02-28 17:55:45 +0000363 const bool isFunctionFailedISel = MF.getProperties().hasProperty(
364 MachineFunctionProperties::Property::FailedISel);
365 isFunctionRegBankSelected =
366 !isFunctionFailedISel &&
367 MF.getProperties().hasProperty(
368 MachineFunctionProperties::Property::RegBankSelected);
369 isFunctionSelected = !isFunctionFailedISel &&
370 MF.getProperties().hasProperty(
371 MachineFunctionProperties::Property::Selected);
Craig Topperc0196b12014-04-14 00:51:57 +0000372 LiveVars = nullptr;
373 LiveInts = nullptr;
374 LiveStks = nullptr;
375 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000376 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000377 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000378 // We don't want to verify LiveVariables if LiveIntervals is available.
379 if (!LiveInts)
380 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000381 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000382 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000383 }
384
Matthias Braun80595462015-09-09 17:49:46 +0000385 verifySlotIndexes();
386
Derek Schuff42666ee2016-03-29 17:40:22 +0000387 verifyProperties(MF);
388
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000389 visitMachineFunctionBefore();
390 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
391 MFI!=MFE; ++MFI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000392 visitMachineBasicBlockBefore(&*MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000393 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000394 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000395 // Do we expect the next instruction to be part of the same bundle?
396 bool InBundle = false;
397
Evan Cheng7fae11b2011-12-14 02:11:42 +0000398 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
399 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000400 if (MBBI->getParent() != &*MFI) {
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +0000401 report("Bad instruction parent pointer", &*MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000402 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000403 continue;
404 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000405
406 // Check for consistent bundle flags.
407 if (InBundle && !MBBI->isBundledWithPred())
408 report("Missing BundledPred flag, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000409 "BundledSucc was set on predecessor",
410 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000411 if (!InBundle && MBBI->isBundledWithPred())
412 report("BundledPred flag is set, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000413 "but BundledSucc not set on predecessor",
414 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000415
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000416 // Is this a bundle header?
417 if (!MBBI->isInsideBundle()) {
418 if (CurBundle)
419 visitMachineBundleAfter(CurBundle);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000420 CurBundle = &*MBBI;
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000421 visitMachineBundleBefore(CurBundle);
422 } else if (!CurBundle)
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +0000423 report("No bundle header", &*MBBI);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000424 visitMachineInstrBefore(&*MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000425 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
426 const MachineInstr &MI = *MBBI;
427 const MachineOperand &Op = MI.getOperand(I);
428 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000429 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000430 // functions when replacing operands of a MachineInstr.
431 report("Instruction has operand with wrong parent set", &MI);
432 }
433
434 visitMachineOperand(&Op, I);
435 }
436
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000437 visitMachineInstrAfter(&*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000438
439 // Was this the last bundled instruction?
440 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000441 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000442 if (CurBundle)
443 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000444 if (InBundle)
445 report("BundledSucc flag set on last instruction in block", &MFI->back());
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000446 visitMachineBasicBlockAfter(&*MFI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000447 }
448 visitMachineFunctionAfter();
449
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000450 // Clean up.
451 regsLive.clear();
452 regsDefined.clear();
453 regsDead.clear();
454 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000455 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000456 MBBInfoMap.clear();
457
Matthias Braunb3aefc32016-02-15 19:25:31 +0000458 return foundErrors;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000459}
460
Chris Lattner75f40452009-08-23 01:03:30 +0000461void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000462 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000463 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000464 if (!foundErrors++) {
465 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000466 errs() << "# " << Banner << '\n';
Matthias Braun42b4b632015-11-09 23:59:23 +0000467 if (LiveInts != nullptr)
468 LiveInts->print(errs());
469 else
470 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000471 }
Owen Anderson21b17882015-02-04 00:02:59 +0000472 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000473 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000474}
475
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000476void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000477 assert(MBB);
478 report(msg, MBB->getParent());
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000479 errs() << "- basic block: " << printMBBReference(*MBB) << ' '
480 << MBB->getName() << " (" << (const void *)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000481 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000482 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000483 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000484 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000485}
486
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000487void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000488 assert(MI);
489 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000490 errs() << "- instruction: ";
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000491 if (Indexes && Indexes->hasIndex(*MI))
492 errs() << Indexes->getInstructionIndex(*MI) << '\t';
Matthias Braun45718db2015-11-09 23:59:25 +0000493 MI->print(errs(), /*SkipOpers=*/true);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000494}
495
Roman Tereshinf487eda2018-05-07 22:31:12 +0000496void MachineVerifier::report(const char *msg, const MachineOperand *MO,
497 unsigned MONum, LLT MOVRegType) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000498 assert(MO);
499 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000500 errs() << "- operand " << MONum << ": ";
Roman Tereshinf487eda2018-05-07 22:31:12 +0000501 MO->print(errs(), MOVRegType, TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000502 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000503}
504
Matthias Braun579c9cd2016-02-02 02:44:25 +0000505void MachineVerifier::report_context(SlotIndex Pos) const {
506 errs() << "- at: " << Pos << '\n';
507}
508
Matthias Braun7e624d52015-11-09 23:59:33 +0000509void MachineVerifier::report_context(const LiveInterval &LI) const {
Owen Anderson21b17882015-02-04 00:02:59 +0000510 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000511}
512
Matt Arsenault892fcd02016-07-25 19:39:01 +0000513void MachineVerifier::report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000514 LaneBitmask LaneMask) const {
Matthias Braun579c9cd2016-02-02 02:44:25 +0000515 report_context_liverange(LR);
Matt Arsenault892fcd02016-07-25 19:39:01 +0000516 report_context_vreg_regunit(VRegUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000517 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +0000518 report_context_lanemask(LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000519}
520
Matthias Braun7e624d52015-11-09 23:59:33 +0000521void MachineVerifier::report_context(const LiveRange::Segment &S) const {
522 errs() << "- segment: " << S << '\n';
523}
524
525void MachineVerifier::report_context(const VNInfo &VNI) const {
526 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
Matthias Braun364e6e92013-10-10 21:28:54 +0000527}
528
Matthias Braun579c9cd2016-02-02 02:44:25 +0000529void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
530 errs() << "- liverange: " << LR << '\n';
531}
532
Matthias Braun30668dd2016-05-11 21:31:39 +0000533void MachineVerifier::report_context_vreg(unsigned VReg) const {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000534 errs() << "- v. register: " << printReg(VReg, TRI) << '\n';
Matthias Braun30668dd2016-05-11 21:31:39 +0000535}
536
Matthias Braun1377fd62016-02-02 20:04:51 +0000537void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
538 if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
Matthias Braun30668dd2016-05-11 21:31:39 +0000539 report_context_vreg(VRegOrUnit);
Matthias Braun1377fd62016-02-02 20:04:51 +0000540 } else {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000541 errs() << "- regunit: " << printRegUnit(VRegOrUnit, TRI) << '\n';
Matthias Braun1377fd62016-02-02 20:04:51 +0000542 }
543}
544
545void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
546 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
547}
548
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000549void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000550 BBInfo &MInfo = MBBInfoMap[MBB];
551 if (!MInfo.reachable) {
552 MInfo.reachable = true;
553 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
554 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
555 markReachable(*SuI);
556 }
557}
558
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000559void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000560 lastIndex = SlotIndex();
Matthias Braun4682ac62017-05-05 22:04:05 +0000561 regsReserved = MRI->reservedRegsFrozen() ? MRI->getReservedRegs()
562 : TRI->getReservedRegs(*MF);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000563
Justin Bogner20dd36a2017-04-11 19:32:41 +0000564 if (!MF->empty())
565 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000566
567 // Build a set of the basic blocks in the function.
568 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000569 for (const auto &MBB : *MF) {
570 FunctionBlocks.insert(&MBB);
571 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000572
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000573 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
574 if (MInfo.Preds.size() != MBB.pred_size())
575 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000576
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000577 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
578 if (MInfo.Succs.size() != MBB.succ_size())
579 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000580 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000581
582 // Check that the register use lists are sane.
583 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000584
Justin Bogner20dd36a2017-04-11 19:32:41 +0000585 if (!MF->empty())
586 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000587}
588
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000589// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000590static bool matchPair(MachineBasicBlock::const_succ_iterator i,
591 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000592 if (*i == a)
593 return *++i == b;
594 if (*i == b)
595 return *++i == a;
596 return false;
597}
598
599void
600MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000601 FirstTerminator = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000602
Matthias Braun79f85b32016-08-24 01:32:41 +0000603 if (!MF->getProperties().hasProperty(
Matthias Braun11723322017-01-05 20:01:19 +0000604 MachineFunctionProperties::Property::NoPHIs) && MRI->tracksLiveness()) {
Lang Hames1ce837a2012-02-14 19:17:48 +0000605 // If this block has allocatable physical registers live-in, check that
606 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000607 for (const auto &LI : MBB->liveins()) {
608 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +0000609 MBB->getIterator() != MBB->getParent()->begin()) {
Matt Arsenault900b21c2017-02-15 22:19:06 +0000610 report("MBB has allocatable live-in, but isn't entry or landing-pad.", MBB);
Lang Hames1ce837a2012-02-14 19:17:48 +0000611 }
612 }
613 }
614
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000615 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000616 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000617 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000618 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000619 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000620 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000621 if (!FunctionBlocks.count(*I))
622 report("MBB has successor that isn't part of the function.", MBB);
623 if (!MBBInfoMap[*I].Preds.count(MBB)) {
624 report("Inconsistent CFG", MBB);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000625 errs() << "MBB is not in the predecessor list of the successor "
626 << printMBBReference(*(*I)) << ".\n";
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000627 }
628 }
629
630 // Check the predecessor list.
631 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
632 E = MBB->pred_end(); I != E; ++I) {
633 if (!FunctionBlocks.count(*I))
634 report("MBB has predecessor that isn't part of the function.", MBB);
635 if (!MBBInfoMap[*I].Succs.count(MBB)) {
636 report("Inconsistent CFG", MBB);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000637 errs() << "MBB is not in the successor list of the predecessor "
638 << printMBBReference(*(*I)) << ".\n";
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000639 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000640 }
Bill Wendling2a401312011-05-04 22:54:05 +0000641
642 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
643 const BasicBlock *BB = MBB->getBasicBlock();
Matthias Braunf1caa282017-12-15 22:22:58 +0000644 const Function &F = MF->getFunction();
Bill Wendling2a401312011-05-04 22:54:05 +0000645 if (LandingPadSuccs.size() > 1 &&
646 !(AsmInfo &&
647 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
Reid Kleckner64b003f2015-11-09 21:04:00 +0000648 BB && isa<SwitchInst>(BB->getTerminator())) &&
Heejin Ahnb4be38f2018-05-17 20:52:03 +0000649 !isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000650 report("MBB has more than one landing pad successor", MBB);
651
Dan Gohman352a4952009-08-27 02:43:49 +0000652 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000653 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000654 SmallVector<MachineOperand, 4> Cond;
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000655 if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
656 Cond)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000657 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
658 // check whether its answers match up with reality.
659 if (!TBB && !FBB) {
660 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000661 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000662 ++MBBI;
663 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000664 // It's possible that the block legitimately ends with a noreturn
665 // call or an unreachable, in which case it won't actually fall
666 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000667 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000668 // It's possible that the block legitimately ends with a noreturn
669 // call or an unreachable, in which case it won't actuall fall
670 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000671 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000672 report("MBB exits via unconditional fall-through but doesn't have "
673 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000674 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000675 report("MBB exits via unconditional fall-through but its successor "
676 "differs from its CFG successor!", MBB);
677 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000678 if (!MBB->empty() && MBB->back().isBarrier() &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000679 !TII->isPredicated(MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000680 report("MBB exits via unconditional fall-through but ends with a "
681 "barrier instruction!", MBB);
682 }
683 if (!Cond.empty()) {
684 report("MBB exits via unconditional fall-through but has a condition!",
685 MBB);
686 }
687 } else if (TBB && !FBB && Cond.empty()) {
688 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000689 // If the block has exactly one successor, that happens to be a
690 // landingpad, accept it as valid control flow.
691 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
692 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
693 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000694 report("MBB exits via unconditional branch but doesn't have "
695 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000696 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000697 report("MBB exits via unconditional branch but the CFG "
698 "successor doesn't match the actual successor!", MBB);
699 }
700 if (MBB->empty()) {
701 report("MBB exits via unconditional branch but doesn't contain "
702 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000703 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000704 report("MBB exits via unconditional branch but doesn't end with a "
705 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000706 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000707 report("MBB exits via unconditional branch but the branch isn't a "
708 "terminator instruction!", MBB);
709 }
710 } else if (TBB && !FBB && !Cond.empty()) {
711 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000712 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000713 ++MBBI;
714 if (MBBI == MF->end()) {
715 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000716 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000717 // A conditional branch with only one successor is weird, but allowed.
718 if (&*MBBI != TBB)
719 report("MBB exits via conditional branch/fall-through but only has "
720 "one CFG successor!", MBB);
721 else if (TBB != *MBB->succ_begin())
722 report("MBB exits via conditional branch/fall-through but the CFG "
723 "successor don't match the actual successor!", MBB);
724 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000725 report("MBB exits via conditional branch/fall-through but doesn't have "
726 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000727 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000728 report("MBB exits via conditional branch/fall-through but the CFG "
729 "successors don't match the actual successors!", MBB);
730 }
731 if (MBB->empty()) {
732 report("MBB exits via conditional branch/fall-through but doesn't "
733 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000734 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000735 report("MBB exits via conditional branch/fall-through but ends with a "
736 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000737 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000738 report("MBB exits via conditional branch/fall-through but the branch "
739 "isn't a terminator instruction!", MBB);
740 }
741 } else if (TBB && FBB) {
742 // Block conditionally branches somewhere, otherwise branches
743 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000744 if (MBB->succ_size() == 1) {
745 // A conditional branch with only one successor is weird, but allowed.
746 if (FBB != TBB)
747 report("MBB exits via conditional branch/branch through but only has "
748 "one CFG successor!", MBB);
749 else if (TBB != *MBB->succ_begin())
750 report("MBB exits via conditional branch/branch through but the CFG "
751 "successor don't match the actual successor!", MBB);
752 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000753 report("MBB exits via conditional branch/branch but doesn't have "
754 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000755 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000756 report("MBB exits via conditional branch/branch but the CFG "
757 "successors don't match the actual successors!", MBB);
758 }
759 if (MBB->empty()) {
760 report("MBB exits via conditional branch/branch but doesn't "
761 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000762 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000763 report("MBB exits via conditional branch/branch but doesn't end with a "
764 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000765 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000766 report("MBB exits via conditional branch/branch but the branch "
767 "isn't a terminator instruction!", MBB);
768 }
769 if (Cond.empty()) {
770 report("MBB exits via conditinal branch/branch but there's no "
771 "condition!", MBB);
772 }
773 } else {
774 report("AnalyzeBranch returned invalid data!", MBB);
775 }
776 }
777
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000778 regsLive.clear();
Matthias Braun11723322017-01-05 20:01:19 +0000779 if (MRI->tracksLiveness()) {
780 for (const auto &LI : MBB->liveins()) {
781 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
782 report("MBB live-in list contains non-physical register", MBB);
783 continue;
784 }
785 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
786 SubRegs.isValid(); ++SubRegs)
787 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000788 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000789 }
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000790
Matthias Braun941a7052016-07-28 18:40:00 +0000791 const MachineFrameInfo &MFI = MF->getFrameInfo();
792 BitVector PR = MFI.getPristineRegs(*MF);
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +0000793 for (unsigned I : PR.set_bits()) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000794 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
795 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000796 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000797 }
798
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000799 regsKilled.clear();
800 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000801
802 if (Indexes)
803 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000804}
805
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000806// This function gets called for all bundle headers, including normal
807// stand-alone unbundled instructions.
808void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000809 if (Indexes && Indexes->hasIndex(*MI)) {
810 SlotIndex idx = Indexes->getInstructionIndex(*MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000811 if (!(idx > lastIndex)) {
812 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000813 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000814 }
815 lastIndex = idx;
816 }
Pete Coopercd720162012-06-07 17:41:39 +0000817
818 // Ensure non-terminators don't follow terminators.
819 // Ignore predicated terminators formed by if conversion.
820 // FIXME: If conversion shouldn't need to violate this rule.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000821 if (MI->isTerminator() && !TII->isPredicated(*MI)) {
Pete Coopercd720162012-06-07 17:41:39 +0000822 if (!FirstTerminator)
823 FirstTerminator = MI;
824 } else if (FirstTerminator) {
825 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000826 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000827 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000828}
829
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000830// The operands on an INLINEASM instruction must follow a template.
831// Verify that the flag operands make sense.
832void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
833 // The first two operands on INLINEASM are the asm string and global flags.
834 if (MI->getNumOperands() < 2) {
835 report("Too few operands on inline asm", MI);
836 return;
837 }
838 if (!MI->getOperand(0).isSymbol())
839 report("Asm string must be an external symbol", MI);
840 if (!MI->getOperand(1).isImm())
841 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000842 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
Wei Ding0526e7f2016-06-22 18:51:08 +0000843 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
844 // and Extra_IsConvergent = 32.
845 if (!isUInt<6>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000846 report("Unknown asm flags", &MI->getOperand(1), 1);
847
Gabor Horvathfee04342015-03-16 09:53:42 +0000848 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000849
850 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
851 unsigned NumOps;
852 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
853 const MachineOperand &MO = MI->getOperand(OpNo);
854 // There may be implicit ops after the fixed operands.
855 if (!MO.isImm())
856 break;
857 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
858 }
859
860 if (OpNo > MI->getNumOperands())
861 report("Missing operands in last group", MI);
862
863 // An optional MDNode follows the groups.
864 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
865 ++OpNo;
866
867 // All trailing operands must be implicit registers.
868 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
869 const MachineOperand &MO = MI->getOperand(OpNo);
870 if (!MO.isReg() || !MO.isImplicit())
871 report("Expected implicit register after groups", &MO, OpNo);
872 }
873}
874
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000875void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000876 const MCInstrDesc &MCID = MI->getDesc();
877 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000878 report("Too few operands", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000879 errs() << MCID.getNumOperands() << " operands expected, but "
Roman Tereshinf487eda2018-05-07 22:31:12 +0000880 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000881 }
Dan Gohmandb9493c2009-10-07 17:36:00 +0000882
Matthias Braun90799ce2016-08-23 21:19:49 +0000883 if (MI->isPHI() && MF->getProperties().hasProperty(
Roman Tereshinf487eda2018-05-07 22:31:12 +0000884 MachineFunctionProperties::Property::NoPHIs))
Matthias Braun90799ce2016-08-23 21:19:49 +0000885 report("Found PHI instruction with NoPHIs property set", MI);
886
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000887 // Check the tied operands.
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000888 if (MI->isInlineAsm())
889 verifyInlineAsm(MI);
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +0000890
Dan Gohmandb9493c2009-10-07 17:36:00 +0000891 // Check the MachineMemOperands for basic consistency.
892 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
Roman Tereshinf487eda2018-05-07 22:31:12 +0000893 E = MI->memoperands_end();
894 I != E; ++I) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000895 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000896 report("Missing mayLoad flag", MI);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000897 if ((*I)->isStore() && !MI->mayStore())
Dan Gohmandb9493c2009-10-07 17:36:00 +0000898 report("Missing mayStore flag", MI);
899 }
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000900
901 // Debug values must not have a slot index.
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000902 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000903 if (LiveInts) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000904 bool mapped = !LiveInts->isNotInMIMap(*MI);
Shiva Chen801bf7e2018-05-09 02:42:00 +0000905 if (MI->isDebugInstr()) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000906 if (mapped)
907 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen5aafb562012-02-27 18:24:30 +0000908 } else if (MI->isInsideBundle()) {
909 if (mapped)
910 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000911 } else {
912 if (!mapped)
913 report("Missing slot index", MI);
914 }
915 }
916
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000917 if (isPreISelGenericOpcode(MCID.getOpcode())) {
Ahmed Bougachab14e9442016-08-02 16:49:22 +0000918 if (isFunctionSelected)
919 report("Unexpected generic instruction in a Selected function", MI);
920
Roman Tereshinf487eda2018-05-07 22:31:12 +0000921 // Check types.
Tim Northover0f140c72016-09-09 11:46:34 +0000922 SmallVector<LLT, 4> Types;
Roman Tereshinf487eda2018-05-07 22:31:12 +0000923 for (unsigned I = 0; I < MCID.getNumOperands(); ++I) {
924 if (!MCID.OpInfo[I].isGenericType())
Tim Northover0f140c72016-09-09 11:46:34 +0000925 continue;
Roman Tereshinf487eda2018-05-07 22:31:12 +0000926 // Generic instructions specify type equality constraints between some of
927 // their operands. Make sure these are consistent.
928 size_t TypeIdx = MCID.OpInfo[I].getGenericTypeIndex();
Tim Northover0f140c72016-09-09 11:46:34 +0000929 Types.resize(std::max(TypeIdx + 1, Types.size()));
930
Roman Tereshinf487eda2018-05-07 22:31:12 +0000931 const MachineOperand *MO = &MI->getOperand(I);
932 LLT OpTy = MRI->getType(MO->getReg());
Roman Tereshind29fc892018-05-07 22:31:47 +0000933 // Don't report a type mismatch if there is no actual mismatch, only a
934 // type missing, to reduce noise:
935 if (OpTy.isValid()) {
936 // Only the first valid type for a type index will be printed: don't
937 // overwrite it later so it's always clear which type was expected:
938 if (!Types[TypeIdx].isValid())
939 Types[TypeIdx] = OpTy;
940 else if (Types[TypeIdx] != OpTy)
941 report("Type mismatch in generic instruction", MO, I, OpTy);
942 } else {
943 // Generic instructions must have types attached to their operands.
944 report("Generic instruction is missing a virtual register type", MO, I);
945 }
Tim Northover0f140c72016-09-09 11:46:34 +0000946 }
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000947
Roman Tereshinf487eda2018-05-07 22:31:12 +0000948 // Generic opcodes must not have physical register operands.
949 for (unsigned I = 0; I < MI->getNumOperands(); ++I) {
950 const MachineOperand *MO = &MI->getOperand(I);
951 if (MO->isReg() && TargetRegisterInfo::isPhysicalRegister(MO->getReg()))
Roman Tereshind29fc892018-05-07 22:31:47 +0000952 report("Generic instruction cannot have physical register", MO, I);
Tim Northovere5102de2016-08-30 18:52:46 +0000953 }
954 }
955
Andrew Trick924123a2011-09-21 02:20:46 +0000956 StringRef ErrorInfo;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000957 if (!TII->verifyInstruction(*MI, ErrorInfo))
Andrew Trick924123a2011-09-21 02:20:46 +0000958 report(ErrorInfo.data(), MI);
Philip Reames94cc4a22017-06-02 16:36:37 +0000959
960 // Verify properties of various specific instruction types
961 switch(MI->getOpcode()) {
962 default:
963 break;
964 case TargetOpcode::G_LOAD:
965 case TargetOpcode::G_STORE:
966 // Generic loads and stores must have a single MachineMemOperand
967 // describing that access.
968 if (!MI->hasOneMemOperand())
969 report("Generic instruction accessing memory must have one mem operand",
970 MI);
971 break;
Aditya Nandakumarefd8a842017-08-23 20:45:48 +0000972 case TargetOpcode::G_PHI: {
973 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
974 if (!DstTy.isValid() ||
975 !std::all_of(MI->operands_begin() + 1, MI->operands_end(),
976 [this, &DstTy](const MachineOperand &MO) {
977 if (!MO.isReg())
978 return true;
979 LLT Ty = MRI->getType(MO.getReg());
980 if (!Ty.isValid() || (Ty != DstTy))
981 return false;
982 return true;
983 }))
984 report("Generic Instruction G_PHI has operands with incompatible/missing "
985 "types",
986 MI);
987 break;
988 }
Roman Tereshind2421f92018-05-08 02:48:15 +0000989 case TargetOpcode::G_SEXT:
990 case TargetOpcode::G_ZEXT:
991 case TargetOpcode::G_ANYEXT:
992 case TargetOpcode::G_TRUNC:
993 case TargetOpcode::G_FPEXT:
994 case TargetOpcode::G_FPTRUNC: {
995 // Number of operands and presense of types is already checked (and
996 // reported in case of any issues), so no need to report them again. As
997 // we're trying to report as many issues as possible at once, however, the
998 // instructions aren't guaranteed to have the right number of operands or
999 // types attached to them at this point
1000 assert(MCID.getNumOperands() == 2 && "Expected 2 operands G_*{EXT,TRUNC}");
1001 if (MI->getNumOperands() < MCID.getNumOperands())
1002 break;
1003 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1004 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1005 if (!DstTy.isValid() || !SrcTy.isValid())
1006 break;
1007
1008 LLT DstElTy = DstTy.isVector() ? DstTy.getElementType() : DstTy;
1009 LLT SrcElTy = SrcTy.isVector() ? SrcTy.getElementType() : SrcTy;
1010 if (DstElTy.isPointer() || SrcElTy.isPointer())
1011 report("Generic extend/truncate can not operate on pointers", MI);
1012
1013 if (DstTy.isVector() != SrcTy.isVector()) {
1014 report("Generic extend/truncate must be all-vector or all-scalar", MI);
1015 // Generally we try to report as many issues as possible at once, but in
1016 // this case it's not clear what should we be comparing the size of the
1017 // scalar with: the size of the whole vector or its lane. Instead of
1018 // making an arbitrary choice and emitting not so helpful message, let's
1019 // avoid the extra noise and stop here.
1020 break;
1021 }
1022 if (DstTy.isVector() && DstTy.getNumElements() != SrcTy.getNumElements())
1023 report("Generic vector extend/truncate must preserve number of lanes",
1024 MI);
1025 unsigned DstSize = DstElTy.getSizeInBits();
1026 unsigned SrcSize = SrcElTy.getSizeInBits();
1027 switch (MI->getOpcode()) {
1028 default:
1029 if (DstSize <= SrcSize)
1030 report("Generic extend has destination type no larger than source", MI);
1031 break;
1032 case TargetOpcode::G_TRUNC:
1033 case TargetOpcode::G_FPTRUNC:
1034 if (DstSize >= SrcSize)
1035 report("Generic truncate has destination type no smaller than source",
1036 MI);
1037 break;
1038 }
1039 break;
1040 }
Aditya Nandakumarb14fd262018-02-09 01:27:23 +00001041 case TargetOpcode::COPY: {
1042 if (foundErrors)
1043 break;
1044 const MachineOperand &DstOp = MI->getOperand(0);
1045 const MachineOperand &SrcOp = MI->getOperand(1);
1046 LLT DstTy = MRI->getType(DstOp.getReg());
1047 LLT SrcTy = MRI->getType(SrcOp.getReg());
1048 if (SrcTy.isValid() && DstTy.isValid()) {
1049 // If both types are valid, check that the types are the same.
1050 if (SrcTy != DstTy) {
1051 report("Copy Instruction is illegal with mismatching types", MI);
1052 errs() << "Def = " << DstTy << ", Src = " << SrcTy << "\n";
1053 }
1054 }
1055 if (SrcTy.isValid() || DstTy.isValid()) {
1056 // If one of them have valid types, let's just check they have the same
1057 // size.
1058 unsigned SrcSize = TRI->getRegSizeInBits(SrcOp.getReg(), *MRI);
1059 unsigned DstSize = TRI->getRegSizeInBits(DstOp.getReg(), *MRI);
1060 assert(SrcSize && "Expecting size here");
1061 assert(DstSize && "Expecting size here");
1062 if (SrcSize != DstSize)
1063 if (!DstOp.getSubReg() && !SrcOp.getSubReg()) {
1064 report("Copy Instruction is illegal with mismatching sizes", MI);
1065 errs() << "Def Size = " << DstSize << ", Src Size = " << SrcSize
1066 << "\n";
1067 }
1068 }
1069 break;
1070 }
Philip Reames94cc4a22017-06-02 16:36:37 +00001071 case TargetOpcode::STATEPOINT:
1072 if (!MI->getOperand(StatepointOpers::IDPos).isImm() ||
1073 !MI->getOperand(StatepointOpers::NBytesPos).isImm() ||
1074 !MI->getOperand(StatepointOpers::NCallArgsPos).isImm())
1075 report("meta operands to STATEPOINT not constant!", MI);
1076 break;
Philip Reames0f02bbc2017-06-02 17:02:33 +00001077
1078 auto VerifyStackMapConstant = [&](unsigned Offset) {
1079 if (!MI->getOperand(Offset).isImm() ||
1080 MI->getOperand(Offset).getImm() != StackMaps::ConstantOp ||
1081 !MI->getOperand(Offset + 1).isImm())
1082 report("stack map constant to STATEPOINT not well formed!", MI);
1083 };
1084 const unsigned VarStart = StatepointOpers(MI).getVarIdx();
1085 VerifyStackMapConstant(VarStart + StatepointOpers::CCOffset);
1086 VerifyStackMapConstant(VarStart + StatepointOpers::FlagsOffset);
1087 VerifyStackMapConstant(VarStart + StatepointOpers::NumDeoptOperandsOffset);
1088
1089 // TODO: verify we have properly encoded deopt arguments
Philip Reames94cc4a22017-06-02 16:36:37 +00001090 };
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001091}
1092
1093void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001094MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001095 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001096 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +00001097 unsigned NumDefs = MCID.getNumDefs();
1098 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
1099 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +00001100
Evan Cheng6cc775f2011-06-28 19:10:37 +00001101 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +00001102 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +00001103 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +00001104 if (!MO->isReg())
1105 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +00001106 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +00001107 report("Explicit definition marked as use", MO, MONum);
1108 else if (MO->isImplicit())
1109 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001110 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +00001111 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +00001112 // Don't check if it's the last operand in a variadic instruction. See,
1113 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001114 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +00001115 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001116 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +00001117 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +00001118 if (MO->isImplicit())
1119 report("Explicit operand marked as implicit", MO, MONum);
1120 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +00001121
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +00001122 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
1123 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +00001124 if (!MO->isReg())
1125 report("Tied use must be a register", MO, MONum);
1126 else if (!MO->isTied())
1127 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +00001128 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
1129 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Mikael Holmen9c3e2ea2017-07-06 13:18:21 +00001130 else if (TargetRegisterInfo::isPhysicalRegister(MO->getReg())) {
1131 const MachineOperand &MOTied = MI->getOperand(TiedTo);
1132 if (!MOTied.isReg())
1133 report("Tied counterpart must be a register", &MOTied, TiedTo);
1134 else if (TargetRegisterInfo::isPhysicalRegister(MOTied.getReg()) &&
1135 MO->getReg() != MOTied.getReg())
1136 report("Tied physical registers must match.", &MOTied, TiedTo);
1137 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +00001138 } else if (MO->isReg() && MO->isTied())
1139 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +00001140 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +00001141 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001142 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +00001143 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +00001144 }
1145
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001146 switch (MO->getType()) {
1147 case MachineOperand::MO_Register: {
1148 const unsigned Reg = MO->getReg();
1149 if (!Reg)
1150 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001151 if (MRI->tracksLiveness() && !MI->isDebugValue())
1152 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001153
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +00001154 // Verify the consistency of tied operands.
1155 if (MO->isTied()) {
1156 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
1157 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
1158 if (!OtherMO.isReg())
1159 report("Must be tied to a register", MO, MONum);
1160 if (!OtherMO.isTied())
1161 report("Missing tie flags on tied operand", MO, MONum);
1162 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
1163 report("Inconsistent tie links", MO, MONum);
1164 if (MONum < MCID.getNumDefs()) {
1165 if (OtherIdx < MCID.getNumOperands()) {
1166 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
1167 report("Explicit def tied to explicit use without tie constraint",
1168 MO, MONum);
1169 } else {
1170 if (!OtherMO.isImplicit())
1171 report("Explicit def should be tied to implicit use", MO, MONum);
1172 }
1173 }
1174 }
1175
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001176 // Verify two-address constraints after leaving SSA form.
1177 unsigned DefIdx;
1178 if (!MRI->isSSA() && MO->isUse() &&
1179 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
1180 Reg != MI->getOperand(DefIdx).getReg())
1181 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001182
1183 // Check register classes.
Matthias Brauneca98582017-11-28 03:54:20 +00001184 unsigned SubIdx = MO->getSubReg();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001185
Matthias Brauneca98582017-11-28 03:54:20 +00001186 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1187 if (SubIdx) {
1188 report("Illegal subregister index for physical register", MO, MONum);
1189 return;
1190 }
1191 if (MONum < MCID.getNumOperands()) {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001192 if (const TargetRegisterClass *DRC =
1193 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001194 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001195 report("Illegal physical register for instruction", MO, MONum);
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001196 errs() << printReg(Reg, TRI) << " is not a "
1197 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001198 }
1199 }
Matthias Brauneca98582017-11-28 03:54:20 +00001200 }
Geoff Berryd1be9112018-01-29 18:57:07 +00001201 if (MO->isRenamable()) {
Geoff Berryf8bf2ec2018-02-23 18:25:08 +00001202 if (MRI->isReserved(Reg)) {
Geoff Berryd1be9112018-01-29 18:57:07 +00001203 report("isRenamable set on reserved register", MO, MONum);
Geoff Berryf8bf2ec2018-02-23 18:25:08 +00001204 return;
1205 }
Geoff Berry60c43102017-12-12 17:53:59 +00001206 }
Mikael Holmen42f7bc92018-06-21 10:03:34 +00001207 if (MI->isDebugValue() && MO->isUse() && !MO->isDebug()) {
1208 report("Use-reg is not IsDebug in a DBG_VALUE", MO, MONum);
1209 return;
1210 }
Matthias Brauneca98582017-11-28 03:54:20 +00001211 } else {
1212 // Virtual register.
1213 const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
1214 if (!RC) {
1215 // This is a generic virtual register.
Ahmed Bougachab14e9442016-08-02 16:49:22 +00001216
Matthias Brauneca98582017-11-28 03:54:20 +00001217 // If we're post-Select, we can't have gvregs anymore.
1218 if (isFunctionSelected) {
1219 report("Generic virtual register invalid in a Selected function",
1220 MO, MONum);
1221 return;
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001222 }
Matthias Brauneca98582017-11-28 03:54:20 +00001223
1224 // The gvreg must have a type and it must not have a SubIdx.
1225 LLT Ty = MRI->getType(Reg);
1226 if (!Ty.isValid()) {
1227 report("Generic virtual register must have a valid type", MO,
1228 MONum);
1229 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001230 }
Matthias Brauneca98582017-11-28 03:54:20 +00001231
1232 const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
1233
1234 // If we're post-RegBankSelect, the gvreg must have a bank.
1235 if (!RegBank && isFunctionRegBankSelected) {
1236 report("Generic virtual register must have a bank in a "
1237 "RegBankSelected function",
1238 MO, MONum);
1239 return;
1240 }
1241
1242 // Make sure the register fits into its register bank if any.
1243 if (RegBank && Ty.isValid() &&
1244 RegBank->getSize() < Ty.getSizeInBits()) {
1245 report("Register bank is too small for virtual register", MO,
1246 MONum);
1247 errs() << "Register bank " << RegBank->getName() << " too small("
1248 << RegBank->getSize() << ") to fit " << Ty.getSizeInBits()
1249 << "-bits\n";
1250 return;
1251 }
1252 if (SubIdx) {
1253 report("Generic virtual register does not subregister index", MO,
1254 MONum);
1255 return;
1256 }
1257
1258 // If this is a target specific instruction and this operand
1259 // has register class constraint, the virtual register must
1260 // comply to it.
1261 if (!isPreISelGenericOpcode(MCID.getOpcode()) &&
1262 MONum < MCID.getNumOperands() &&
1263 TII->getRegClass(MCID, MONum, TRI, *MF)) {
1264 report("Virtual register does not match instruction constraint", MO,
1265 MONum);
1266 errs() << "Expect register class "
1267 << TRI->getRegClassName(
1268 TII->getRegClass(MCID, MONum, TRI, *MF))
1269 << " but got nothing\n";
1270 return;
1271 }
1272
1273 break;
1274 }
1275 if (SubIdx) {
1276 const TargetRegisterClass *SRC =
1277 TRI->getSubClassWithSubReg(RC, SubIdx);
1278 if (!SRC) {
1279 report("Invalid subregister index for virtual register", MO, MONum);
1280 errs() << "Register class " << TRI->getRegClassName(RC)
1281 << " does not support subreg index " << SubIdx << "\n";
1282 return;
1283 }
1284 if (RC != SRC) {
1285 report("Invalid register class for subregister index", MO, MONum);
1286 errs() << "Register class " << TRI->getRegClassName(RC)
1287 << " does not fully support subreg index " << SubIdx << "\n";
1288 return;
1289 }
1290 }
1291 if (MONum < MCID.getNumOperands()) {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001292 if (const TargetRegisterClass *DRC =
1293 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001294 if (SubIdx) {
1295 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +00001296 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001297 if (!SuperRC) {
1298 report("No largest legal super class exists.", MO, MONum);
1299 return;
1300 }
1301 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
1302 if (!DRC) {
1303 report("No matching super-reg register class.", MO, MONum);
1304 return;
1305 }
1306 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +00001307 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001308 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001309 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +00001310 << " register, but got a " << TRI->getRegClassName(RC)
1311 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001312 }
1313 }
1314 }
1315 }
1316 break;
1317 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001318
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001319 case MachineOperand::MO_RegisterMask:
1320 regMasks.push_back(MO->getRegMask());
1321 break;
1322
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001323 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +00001324 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
1325 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001326 break;
1327
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001328 case MachineOperand::MO_FrameIndex:
1329 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001330 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
Jonas Paulsson72640f12015-10-29 08:28:35 +00001331 int FI = MO->getIndex();
1332 LiveInterval &LI = LiveStks->getInterval(FI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001333 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001334
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001335 bool stores = MI->mayStore();
Jonas Paulsson72640f12015-10-29 08:28:35 +00001336 bool loads = MI->mayLoad();
1337 // For a memory-to-memory move, we need to check if the frame
1338 // index is used for storing or loading, by inspecting the
1339 // memory operands.
1340 if (stores && loads) {
1341 for (auto *MMO : MI->memoperands()) {
1342 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1343 if (PSV == nullptr) continue;
1344 const FixedStackPseudoSourceValue *Value =
1345 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1346 if (Value == nullptr) continue;
1347 if (Value->getFrameIndex() != FI) continue;
1348
1349 if (MMO->isStore())
1350 loads = false;
1351 else
1352 stores = false;
1353 break;
1354 }
1355 if (loads == stores)
1356 report("Missing fixed stack memoperand.", MI);
1357 }
1358 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001359 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001360 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001361 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001362 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001363 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001364 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001365 }
1366 }
1367 break;
1368
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001369 default:
1370 break;
1371 }
1372}
1373
Matthias Braun1377fd62016-02-02 20:04:51 +00001374void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
1375 unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
1376 LaneBitmask LaneMask) {
1377 LiveQueryResult LRQ = LR.Query(UseIdx);
1378 // Check if we have a segment at the use, note however that we only need one
1379 // live subregister range, the others may be dead.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001380 if (!LRQ.valueIn() && LaneMask.none()) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001381 report("No live segment at use", MO, MONum);
1382 report_context_liverange(LR);
1383 report_context_vreg_regunit(VRegOrUnit);
1384 report_context(UseIdx);
1385 }
1386 if (MO->isKill() && !LRQ.isKill()) {
1387 report("Live range continues after kill flag", MO, MONum);
1388 report_context_liverange(LR);
1389 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001390 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001391 report_context_lanemask(LaneMask);
1392 report_context(UseIdx);
1393 }
1394}
1395
1396void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
1397 unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
1398 LaneBitmask LaneMask) {
1399 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
1400 assert(VNI && "NULL valno is not allowed");
1401 if (VNI->def != DefIdx) {
1402 report("Inconsistent valno->def", MO, MONum);
1403 report_context_liverange(LR);
1404 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001405 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001406 report_context_lanemask(LaneMask);
1407 report_context(*VNI);
1408 report_context(DefIdx);
1409 }
1410 } else {
1411 report("No live segment at def", MO, MONum);
1412 report_context_liverange(LR);
1413 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001414 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001415 report_context_lanemask(LaneMask);
1416 report_context(DefIdx);
1417 }
1418 // Check that, if the dead def flag is present, LiveInts agree.
1419 if (MO->isDead()) {
1420 LiveQueryResult LRQ = LR.Query(DefIdx);
1421 if (!LRQ.isDeadDef()) {
1422 // In case of physregs we can have a non-dead definition on another
1423 // operand.
1424 bool otherDef = false;
1425 if (!TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
1426 const MachineInstr &MI = *MO->getParent();
1427 for (const MachineOperand &MO : MI.operands()) {
1428 if (!MO.isReg() || !MO.isDef() || MO.isDead())
1429 continue;
1430 unsigned Reg = MO.getReg();
1431 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1432 if (*Units == VRegOrUnit) {
1433 otherDef = true;
1434 break;
1435 }
1436 }
1437 }
1438 }
1439
1440 if (!otherDef) {
1441 report("Live range continues after dead def flag", MO, MONum);
1442 report_context_liverange(LR);
1443 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001444 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001445 report_context_lanemask(LaneMask);
1446 }
1447 }
1448 }
1449}
1450
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001451void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1452 const MachineInstr *MI = MO->getParent();
1453 const unsigned Reg = MO->getReg();
1454
1455 // Both use and def operands can read a register.
1456 if (MO->readsReg()) {
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001457 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001458 addRegWithSubRegs(regsKilled, Reg);
1459
1460 // Check that LiveVars knows this kill.
1461 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1462 MO->isKill()) {
1463 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
David Majnemer0d955d02016-08-11 22:21:41 +00001464 if (!is_contained(VI.Kills, MI))
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001465 report("Kill missing from LiveVariables", MO, MONum);
1466 }
1467
1468 // Check LiveInts liveness and kill.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001469 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1470 SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001471 // Check the cached regunit intervals.
1472 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1473 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Brauncebdb172017-09-01 18:36:26 +00001474 if (MRI->isReservedRegUnit(*Units))
1475 continue;
Matthias Braun1377fd62016-02-02 20:04:51 +00001476 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
1477 checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001478 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001479 }
1480
1481 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1482 if (LiveInts->hasInterval(Reg)) {
1483 // This is a virtual register interval.
1484 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun1377fd62016-02-02 20:04:51 +00001485 checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
1486
1487 if (LI.hasSubRanges() && !MO->isDef()) {
1488 unsigned SubRegIdx = MO->getSubReg();
1489 LaneBitmask MOMask = SubRegIdx != 0
1490 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1491 : MRI->getMaxLaneMaskForVReg(Reg);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001492 LaneBitmask LiveInMask;
Matthias Braun1377fd62016-02-02 20:04:51 +00001493 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001494 if ((MOMask & SR.LaneMask).none())
Matthias Braun1377fd62016-02-02 20:04:51 +00001495 continue;
1496 checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
1497 LiveQueryResult LRQ = SR.Query(UseIdx);
1498 if (LRQ.valueIn())
1499 LiveInMask |= SR.LaneMask;
1500 }
1501 // At least parts of the register has to be live at the use.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001502 if ((LiveInMask & MOMask).none()) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001503 report("No live subrange at use", MO, MONum);
1504 report_context(LI);
1505 report_context(UseIdx);
1506 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001507 }
1508 } else {
1509 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001510 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001511 }
1512 }
1513
1514 // Use of a dead register.
1515 if (!regsLive.count(Reg)) {
1516 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1517 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001518 bool Bad = !isReserved(Reg);
1519 // We are fine if just any subregister has a defined value.
1520 if (Bad) {
1521 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1522 ++SubRegs) {
1523 if (regsLive.count(*SubRegs)) {
1524 Bad = false;
1525 break;
1526 }
1527 }
1528 }
Matthias Braun96a31952015-01-14 22:25:14 +00001529 // If there is an additional implicit-use of a super register we stop
1530 // here. By definition we are fine if the super register is not
1531 // (completely) dead, if the complete super register is dead we will
1532 // get a report for its operand.
1533 if (Bad) {
1534 for (const MachineOperand &MOP : MI->uses()) {
1535 if (!MOP.isReg())
1536 continue;
1537 if (!MOP.isImplicit())
1538 continue;
1539 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1540 ++SubRegs) {
1541 if (*SubRegs == Reg) {
1542 Bad = false;
1543 break;
1544 }
1545 }
1546 }
1547 }
Matthias Braun96d77322014-12-10 01:13:13 +00001548 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001549 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001550 } else if (MRI->def_empty(Reg)) {
1551 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001552 } else {
1553 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1554 // We don't know which virtual registers are live in, so only complain
1555 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1556 // must be live in. PHI instructions are handled separately.
1557 if (MInfo.regsKilled.count(Reg))
1558 report("Using a killed virtual register", MO, MONum);
1559 else if (!MI->isPHI())
1560 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1561 }
1562 }
1563 }
1564
1565 if (MO->isDef()) {
1566 // Register defined.
1567 // TODO: verify that earlyclobber ops are not used.
1568 if (MO->isDead())
1569 addRegWithSubRegs(regsDead, Reg);
1570 else
1571 addRegWithSubRegs(regsDefined, Reg);
1572
1573 // Verify SSA form.
1574 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001575 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001576 report("Multiple virtual register defs in SSA form", MO, MONum);
1577
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001578 // Check LiveInts for a live segment, but only for virtual registers.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001579 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1580 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001581 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Matthias Braun1377fd62016-02-02 20:04:51 +00001582
1583 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1584 if (LiveInts->hasInterval(Reg)) {
1585 const LiveInterval &LI = LiveInts->getInterval(Reg);
1586 checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
1587
1588 if (LI.hasSubRanges()) {
1589 unsigned SubRegIdx = MO->getSubReg();
1590 LaneBitmask MOMask = SubRegIdx != 0
1591 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1592 : MRI->getMaxLaneMaskForVReg(Reg);
1593 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001594 if ((SR.LaneMask & MOMask).none())
Matthias Braun1377fd62016-02-02 20:04:51 +00001595 continue;
1596 checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, SR.LaneMask);
1597 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001598 }
1599 } else {
Matthias Braun1377fd62016-02-02 20:04:51 +00001600 report("Virtual register has no Live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001601 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001602 }
1603 }
1604 }
1605}
1606
Eugene Zelenko32a40562017-09-11 23:00:48 +00001607void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {}
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001608
1609// This function gets called after visiting all instructions in a bundle. The
1610// argument points to the bundle header.
1611// Normal stand-alone instructions are also considered 'bundles', and this
1612// function is called for all of them.
1613void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001614 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1615 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001616 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001617 // Kill any masked registers.
1618 while (!regMasks.empty()) {
1619 const uint32_t *Mask = regMasks.pop_back_val();
1620 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1621 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1622 MachineOperand::clobbersPhysReg(Mask, *I))
1623 regsDead.push_back(*I);
1624 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00001625 set_subtract(regsLive, regsDead); regsDead.clear();
1626 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001627}
1628
1629void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001630MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001631 MBBInfoMap[MBB].regsLiveOut = regsLive;
1632 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001633
1634 if (Indexes) {
1635 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1636 if (!(stop > lastIndex)) {
1637 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00001638 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00001639 << " last instruction was at " << lastIndex << '\n';
1640 }
1641 lastIndex = stop;
1642 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001643}
1644
1645// Calculate the largest possible vregsPassed sets. These are the registers that
1646// can pass through an MBB live, but may not be live every time. It is assumed
1647// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001648void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001649 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1650 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001651 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001652 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001653 BBInfo &MInfo = MBBInfoMap[&MBB];
1654 if (!MInfo.reachable)
1655 continue;
1656 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1657 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1658 BBInfo &SInfo = MBBInfoMap[*SuI];
1659 if (SInfo.addPassed(MInfo.regsLiveOut))
1660 todo.insert(*SuI);
1661 }
1662 }
1663
1664 // Iteratively push vregsPassed to successors. This will converge to the same
1665 // final state regardless of DenseSet iteration order.
1666 while (!todo.empty()) {
1667 const MachineBasicBlock *MBB = *todo.begin();
1668 todo.erase(MBB);
1669 BBInfo &MInfo = MBBInfoMap[MBB];
1670 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1671 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1672 if (*SuI == MBB)
1673 continue;
1674 BBInfo &SInfo = MBBInfoMap[*SuI];
1675 if (SInfo.addPassed(MInfo.vregsPassed))
1676 todo.insert(*SuI);
1677 }
1678 }
1679}
1680
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001681// Calculate the set of virtual registers that must be passed through each basic
1682// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001683// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001684void MachineVerifier::calcRegsRequired() {
1685 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001686 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001687 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001688 BBInfo &MInfo = MBBInfoMap[&MBB];
1689 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1690 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1691 BBInfo &PInfo = MBBInfoMap[*PrI];
1692 if (PInfo.addRequired(MInfo.vregsLiveIn))
1693 todo.insert(*PrI);
1694 }
1695 }
1696
1697 // Iteratively push vregsRequired to predecessors. This will converge to the
1698 // same final state regardless of DenseSet iteration order.
1699 while (!todo.empty()) {
1700 const MachineBasicBlock *MBB = *todo.begin();
1701 todo.erase(MBB);
1702 BBInfo &MInfo = MBBInfoMap[MBB];
1703 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1704 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1705 if (*PrI == MBB)
1706 continue;
1707 BBInfo &SInfo = MBBInfoMap[*PrI];
1708 if (SInfo.addRequired(MInfo.vregsRequired))
1709 todo.insert(*PrI);
1710 }
1711 }
1712}
1713
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001714// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001715// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Matthias Brauna6d53742017-11-28 03:54:19 +00001716void MachineVerifier::checkPHIOps(const MachineBasicBlock &MBB) {
1717 BBInfo &MInfo = MBBInfoMap[&MBB];
1718
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001719 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Matthias Brauna6d53742017-11-28 03:54:19 +00001720 for (const MachineInstr &Phi : MBB) {
1721 if (!Phi.isPHI())
Alexey Samsonovf74bde62014-04-30 22:17:38 +00001722 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00001723 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001724
Matthias Brauna6d53742017-11-28 03:54:19 +00001725 const MachineOperand &MODef = Phi.getOperand(0);
1726 if (!MODef.isReg() || !MODef.isDef()) {
1727 report("Expected first PHI operand to be a register def", &MODef, 0);
1728 continue;
1729 }
1730 if (MODef.isTied() || MODef.isImplicit() || MODef.isInternalRead() ||
1731 MODef.isEarlyClobber() || MODef.isDebug())
1732 report("Unexpected flag on PHI operand", &MODef, 0);
1733 unsigned DefReg = MODef.getReg();
1734 if (!TargetRegisterInfo::isVirtualRegister(DefReg))
1735 report("Expected first PHI operand to be a virtual register", &MODef, 0);
1736
1737 for (unsigned I = 1, E = Phi.getNumOperands(); I != E; I += 2) {
1738 const MachineOperand &MO0 = Phi.getOperand(I);
1739 if (!MO0.isReg()) {
1740 report("Expected PHI operand to be a register", &MO0, I);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001741 continue;
Matthias Brauna6d53742017-11-28 03:54:19 +00001742 }
1743 if (MO0.isImplicit() || MO0.isInternalRead() || MO0.isEarlyClobber() ||
1744 MO0.isDebug() || MO0.isTied())
1745 report("Unexpected flag on PHI operand", &MO0, I);
1746
1747 const MachineOperand &MO1 = Phi.getOperand(I + 1);
1748 if (!MO1.isMBB()) {
1749 report("Expected PHI operand to be a basic block", &MO1, I + 1);
1750 continue;
1751 }
1752
1753 const MachineBasicBlock &Pre = *MO1.getMBB();
1754 if (!Pre.isSuccessor(&MBB)) {
1755 report("PHI input is not a predecessor block", &MO1, I + 1);
1756 continue;
1757 }
1758
1759 if (MInfo.reachable) {
1760 seen.insert(&Pre);
1761 BBInfo &PrInfo = MBBInfoMap[&Pre];
Matthias Braun7eae2512017-12-04 18:57:48 +00001762 if (!MO0.isUndef() && PrInfo.reachable &&
1763 !PrInfo.isLiveOut(MO0.getReg()))
Matthias Brauna6d53742017-11-28 03:54:19 +00001764 report("PHI operand is not live-out from predecessor", &MO0, I);
1765 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001766 }
1767
1768 // Did we see all predecessors?
Matthias Brauna6d53742017-11-28 03:54:19 +00001769 if (MInfo.reachable) {
1770 for (MachineBasicBlock *Pred : MBB.predecessors()) {
1771 if (!seen.count(Pred)) {
1772 report("Missing PHI operand", &Phi);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00001773 errs() << printMBBReference(*Pred)
1774 << " is a predecessor according to the CFG.\n";
Matthias Brauna6d53742017-11-28 03:54:19 +00001775 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001776 }
1777 }
1778 }
1779}
1780
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001781void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00001782 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001783
Matthias Brauna6d53742017-11-28 03:54:19 +00001784 for (const MachineBasicBlock &MBB : *MF)
1785 checkPHIOps(MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001786
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001787 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001788 calcRegsRequired();
1789
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001790 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001791 for (const auto &MBB : *MF) {
1792 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001793 for (RegSet::iterator
1794 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1795 ++I)
1796 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001797 report("Virtual register killed in block, but needed live out.", &MBB);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001798 errs() << "Virtual register " << printReg(*I)
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001799 << " is used after the block.\n";
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00001800 }
1801 }
1802
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00001803 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001804 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1805 for (RegSet::iterator
1806 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Matthias Braun30668dd2016-05-11 21:31:39 +00001807 ++I) {
1808 report("Virtual register defs don't dominate all uses.", MF);
1809 report_context_vreg(*I);
1810 }
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00001811 }
1812
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001813 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001814 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001815 if (LiveInts)
1816 verifyLiveIntervals();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001817}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001818
1819void MachineVerifier::verifyLiveVariables() {
1820 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00001821 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1822 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001823 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001824 for (const auto &MBB : *MF) {
1825 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001826
1827 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1828 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001829 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1830 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001831 errs() << "Virtual register " << printReg(Reg)
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001832 << " must be live through the block.\n";
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001833 }
1834 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00001835 if (VI.AliveBlocks.test(MBB.getNumber())) {
1836 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001837 errs() << "Virtual register " << printReg(Reg)
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001838 << " is not needed live through the block.\n";
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00001839 }
1840 }
1841 }
1842 }
1843}
1844
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001845void MachineVerifier::verifyLiveIntervals() {
1846 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001847 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1848 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001849
1850 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001851 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00001852 continue;
1853
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001854 if (!LiveInts->hasInterval(Reg)) {
1855 report("Missing live interval for virtual register", MF);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001856 errs() << printReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001857 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001858 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00001859
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00001860 const LiveInterval &LI = LiveInts->getInterval(Reg);
1861 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001862 verifyLiveInterval(LI);
1863 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00001864
1865 // Verify all the cached regunit intervals.
1866 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00001867 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1868 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001869}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001870
Matthias Braun364e6e92013-10-10 21:28:54 +00001871void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001872 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00001873 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001874 if (VNI->isUnused())
1875 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001876
Matthias Braun364e6e92013-10-10 21:28:54 +00001877 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001878
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001879 if (!DefVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001880 report("Value not live at VNInfo def and not marked unused", MF);
1881 report_context(LR, Reg, LaneMask);
1882 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001883 return;
1884 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001885
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001886 if (DefVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001887 report("Live segment at def has different VNInfo", MF);
1888 report_context(LR, Reg, LaneMask);
1889 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001890 return;
1891 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001892
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001893 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1894 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001895 report("Invalid VNInfo definition index", MF);
1896 report_context(LR, Reg, LaneMask);
1897 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001898 return;
1899 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00001900
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001901 if (VNI->isPHIDef()) {
1902 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001903 report("PHIDef VNInfo is not defined at MBB start", MBB);
1904 report_context(LR, Reg, LaneMask);
1905 report_context(*VNI);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001906 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001907 return;
1908 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001909
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001910 // Non-PHI def.
1911 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1912 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001913 report("No instruction at VNInfo def index", MBB);
1914 report_context(LR, Reg, LaneMask);
1915 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001916 return;
1917 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00001918
Matthias Braun364e6e92013-10-10 21:28:54 +00001919 if (Reg != 0) {
1920 bool hasDef = false;
1921 bool isEarlyClobber = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001922 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00001923 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001924 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001925 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1926 if (MOI->getReg() != Reg)
1927 continue;
1928 } else {
1929 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1930 !TRI->hasRegUnit(MOI->getReg(), Reg))
1931 continue;
1932 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001933 if (LaneMask.any() &&
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001934 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask).none())
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00001935 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00001936 hasDef = true;
1937 if (MOI->isEarlyClobber())
1938 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001939 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001940
Matthias Braun364e6e92013-10-10 21:28:54 +00001941 if (!hasDef) {
1942 report("Defining instruction does not modify register", MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00001943 report_context(LR, Reg, LaneMask);
1944 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001945 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001946
Matthias Braun364e6e92013-10-10 21:28:54 +00001947 // Early clobber defs begin at USE slots, but other defs must begin at
1948 // DEF slots.
1949 if (isEarlyClobber) {
1950 if (!VNI->def.isEarlyClobber()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001951 report("Early clobber def must be at an early-clobber slot", MBB);
1952 report_context(LR, Reg, LaneMask);
1953 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00001954 }
1955 } else if (!VNI->def.isRegister()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001956 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
1957 report_context(LR, Reg, LaneMask);
1958 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001959 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001960 }
1961}
1962
Matthias Braun364e6e92013-10-10 21:28:54 +00001963void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1964 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00001965 unsigned Reg, LaneBitmask LaneMask)
1966{
Matthias Braun364e6e92013-10-10 21:28:54 +00001967 const LiveRange::Segment &S = *I;
1968 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001969 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001970
Matthias Braun364e6e92013-10-10 21:28:54 +00001971 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001972 report("Foreign valno in live segment", MF);
1973 report_context(LR, Reg, LaneMask);
1974 report_context(S);
1975 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001976 }
1977
1978 if (VNI->isUnused()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001979 report("Live segment valno is marked unused", MF);
1980 report_context(LR, Reg, LaneMask);
1981 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001982 }
1983
Matthias Braun364e6e92013-10-10 21:28:54 +00001984 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001985 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001986 report("Bad start of live segment, no basic block", MF);
1987 report_context(LR, Reg, LaneMask);
1988 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001989 return;
1990 }
1991 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00001992 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun7e624d52015-11-09 23:59:33 +00001993 report("Live segment must begin at MBB entry or valno def", MBB);
1994 report_context(LR, Reg, LaneMask);
1995 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00001996 }
1997
1998 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00001999 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002000 if (!EndMBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002001 report("Bad end of live segment, no basic block", MF);
2002 report_context(LR, Reg, LaneMask);
2003 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002004 return;
2005 }
2006
2007 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00002008 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002009 return;
2010
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00002011 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00002012 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
2013 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00002014 return;
2015
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002016 // The live segment is ending inside EndMBB
2017 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00002018 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002019 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002020 report("Live segment doesn't end at a valid instruction", EndMBB);
2021 report_context(LR, Reg, LaneMask);
2022 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002023 return;
2024 }
2025
2026 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00002027 if (S.end.isBlock()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002028 report("Live segment ends at B slot of an instruction", EndMBB);
2029 report_context(LR, Reg, LaneMask);
2030 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002031 }
2032
Matthias Braun364e6e92013-10-10 21:28:54 +00002033 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002034 // Segment ends on the dead slot.
2035 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00002036 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002037 report("Live segment ending at dead slot spans instructions", EndMBB);
2038 report_context(LR, Reg, LaneMask);
2039 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002040 }
2041 }
2042
2043 // A live segment can only end at an early-clobber slot if it is being
2044 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00002045 if (S.end.isEarlyClobber()) {
2046 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002047 report("Live segment ending at early clobber slot must be "
Matthias Braun7e624d52015-11-09 23:59:33 +00002048 "redefined by an EC def in the same instruction", EndMBB);
2049 report_context(LR, Reg, LaneMask);
2050 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002051 }
2052 }
2053
2054 // The following checks only apply to virtual registers. Physreg liveness
2055 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00002056 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00002057 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002058 // use, or a dead flag on a def.
2059 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00002060 bool hasSubRegDef = false;
Matthias Braun72a58c32016-03-29 19:07:43 +00002061 bool hasDeadDef = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00002062 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00002063 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002064 continue;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00002065 unsigned Sub = MOI->getSubReg();
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00002066 LaneBitmask SLM = Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub)
2067 : LaneBitmask::getAll();
Matthias Braun72a58c32016-03-29 19:07:43 +00002068 if (MOI->isDef()) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00002069 if (Sub != 0) {
Matthias Braun72a58c32016-03-29 19:07:43 +00002070 hasSubRegDef = true;
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00002071 // An operand %0:sub0 reads %0:sub1..n. Invert the lane
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00002072 // mask for subregister defs. Read-undef defs will be handled by
2073 // readsReg below.
Krzysztof Parzyszek0a955d62016-08-29 13:15:35 +00002074 SLM = ~SLM;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00002075 }
Matthias Braun72a58c32016-03-29 19:07:43 +00002076 if (MOI->isDead())
2077 hasDeadDef = true;
2078 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00002079 if (LaneMask.any() && (LaneMask & SLM).none())
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00002080 continue;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002081 if (MOI->readsReg())
2082 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002083 }
Matthias Braun72a58c32016-03-29 19:07:43 +00002084 if (S.end.isDead()) {
2085 // Make sure that the corresponding machine operand for a "dead" live
2086 // range has the dead flag. We cannot perform this check for subregister
2087 // liveranges as partially dead values are allowed.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00002088 if (LaneMask.none() && !hasDeadDef) {
Matthias Braun72a58c32016-03-29 19:07:43 +00002089 report("Instruction ending live segment on dead slot has no dead flag",
2090 MI);
2091 report_context(LR, Reg, LaneMask);
2092 report_context(S);
2093 }
2094 } else {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002095 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00002096 // When tracking subregister liveness, the main range must start new
2097 // values on partial register writes, even if there is no read.
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00002098 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask.any() ||
Matthias Brauna25e13a2015-03-19 00:21:58 +00002099 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00002100 report("Instruction ending live segment doesn't read the register",
2101 MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00002102 report_context(LR, Reg, LaneMask);
2103 report_context(S);
Matthias Braun21554d92014-12-10 01:13:11 +00002104 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002105 }
2106 }
2107 }
2108
2109 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002110 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00002111 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00002112 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002113 // Not live-in to any blocks.
2114 if (MBB == EndMBB)
2115 return;
2116 // Skip this block.
2117 ++MFI;
2118 }
Eugene Zelenko32a40562017-09-11 23:00:48 +00002119 while (true) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002120 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002121 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00002122 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00002123 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002124 if (&*MFI == EndMBB)
2125 break;
2126 ++MFI;
2127 continue;
2128 }
2129
2130 // Is VNI a PHI-def in the current block?
2131 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002132 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002133
2134 // Check that VNI is live-out of all predecessors.
2135 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
2136 PE = MFI->pred_end(); PI != PE; ++PI) {
2137 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00002138 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002139
Matthias Braun1ee25e02017-06-08 21:30:54 +00002140 // All predecessors must have a live-out value. However for a phi
2141 // instruction with subregister intervals
2142 // only one of the subregisters (not necessarily the current one) needs to
2143 // be defined.
2144 if (!PVNI && (LaneMask.none() || !IsPHI) ) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002145 report("Register not marked live out of predecessor", *PI);
2146 report_context(LR, Reg, LaneMask);
2147 report_context(*VNI);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00002148 errs() << " live into " << printMBBReference(*MFI) << '@'
2149 << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002150 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002151 continue;
2152 }
2153
2154 // Only PHI-defs can take different predecessor values.
2155 if (!IsPHI && PVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002156 report("Different value live out of predecessor", *PI);
2157 report_context(LR, Reg, LaneMask);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00002158 errs() << "Valno #" << PVNI->id << " live out of "
2159 << printMBBReference(*(*PI)) << '@' << PEnd << "\nValno #"
2160 << VNI->id << " live into " << printMBBReference(*MFI) << '@'
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002161 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002162 }
2163 }
2164 if (&*MFI == EndMBB)
2165 break;
2166 ++MFI;
2167 }
2168}
2169
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002170void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00002171 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00002172 for (const VNInfo *VNI : LR.valnos)
2173 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002174
Matthias Braun364e6e92013-10-10 21:28:54 +00002175 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002176 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00002177}
2178
2179void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002180 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00002181 assert(TargetRegisterInfo::isVirtualRegister(Reg));
2182 verifyLiveRange(LI, Reg);
2183
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00002184 LaneBitmask Mask;
Matthias Braune6a24852015-09-25 21:51:14 +00002185 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00002186 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00002187 if ((Mask & SR.LaneMask).any()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002188 report("Lane masks of sub ranges overlap in live interval", MF);
2189 report_context(LI);
2190 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00002191 if ((SR.LaneMask & ~MaxMask).any()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002192 report("Subrange lanemask is invalid", MF);
2193 report_context(LI);
2194 }
2195 if (SR.empty()) {
2196 report("Subrange must not be empty", MF);
2197 report_context(SR, LI.reg, SR.LaneMask);
2198 }
Matthias Braune962e522015-03-25 21:18:22 +00002199 Mask |= SR.LaneMask;
2200 verifyLiveRange(SR, LI.reg, SR.LaneMask);
Matthias Braun7e624d52015-11-09 23:59:33 +00002201 if (!LI.covers(SR)) {
2202 report("A Subrange is not covered by the main range", MF);
2203 report_context(LI);
2204 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002205 }
2206
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002207 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00002208 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
Matthias Braunbf47f632016-01-08 01:16:35 +00002209 unsigned NumComp = ConEQ.Classify(LI);
Matthias Braune962e522015-03-25 21:18:22 +00002210 if (NumComp > 1) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002211 report("Multiple connected components in live interval", MF);
2212 report_context(LI);
Matthias Braune962e522015-03-25 21:18:22 +00002213 for (unsigned comp = 0; comp != NumComp; ++comp) {
2214 errs() << comp << ": valnos";
2215 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
2216 E = LI.vni_end(); I!=E; ++I)
2217 if (comp == ConEQ.getEqClass(*I))
2218 errs() << ' ' << (*I)->id;
2219 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00002220 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002221 }
2222}
Manman Renaa6875b2013-07-15 21:26:31 +00002223
2224namespace {
Eugene Zelenko32a40562017-09-11 23:00:48 +00002225
Manman Renaa6875b2013-07-15 21:26:31 +00002226 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
2227 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
2228 // value is zero.
2229 // We use a bool plus an integer to capture the stack state.
2230 struct StackStateOfBB {
Eugene Zelenko32a40562017-09-11 23:00:48 +00002231 StackStateOfBB() = default;
Manman Renaa6875b2013-07-15 21:26:31 +00002232 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
2233 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
Eugene Zelenko32a40562017-09-11 23:00:48 +00002234 ExitIsSetup(ExitSetup) {}
2235
Manman Renaa6875b2013-07-15 21:26:31 +00002236 // Can be negative, which means we are setting up a frame.
Eugene Zelenko32a40562017-09-11 23:00:48 +00002237 int EntryValue = 0;
2238 int ExitValue = 0;
2239 bool EntryIsSetup = false;
2240 bool ExitIsSetup = false;
Manman Renaa6875b2013-07-15 21:26:31 +00002241 };
Eugene Zelenko32a40562017-09-11 23:00:48 +00002242
2243} // end anonymous namespace
Manman Renaa6875b2013-07-15 21:26:31 +00002244
2245/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
2246/// by a FrameDestroy <n>, stack adjustments are identical on all
2247/// CFG edges to a merge point, and frame is destroyed at end of a return block.
2248void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00002249 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
2250 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Serge Pavlov802aa662017-04-20 01:34:04 +00002251 if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
2252 return;
Manman Renaa6875b2013-07-15 21:26:31 +00002253
2254 SmallVector<StackStateOfBB, 8> SPState;
2255 SPState.resize(MF->getNumBlockIDs());
David Callahanc1051ab2016-10-05 21:36:16 +00002256 df_iterator_default_set<const MachineBasicBlock*> Reachable;
Manman Renaa6875b2013-07-15 21:26:31 +00002257
2258 // Visit the MBBs in DFS order.
Eugene Zelenko32a40562017-09-11 23:00:48 +00002259 for (df_ext_iterator<const MachineFunction *,
2260 df_iterator_default_set<const MachineBasicBlock *>>
Manman Renaa6875b2013-07-15 21:26:31 +00002261 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
2262 DFI != DFE; ++DFI) {
2263 const MachineBasicBlock *MBB = *DFI;
2264
2265 StackStateOfBB BBState;
2266 // Check the exit state of the DFS stack predecessor.
2267 if (DFI.getPathLength() >= 2) {
2268 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
2269 assert(Reachable.count(StackPred) &&
2270 "DFS stack predecessor is already visited.\n");
2271 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
2272 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
2273 BBState.ExitValue = BBState.EntryValue;
2274 BBState.ExitIsSetup = BBState.EntryIsSetup;
2275 }
2276
2277 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002278 for (const auto &I : *MBB) {
2279 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00002280 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002281 report("FrameSetup is after another FrameSetup", &I);
Serge Pavlovd526b132017-05-09 13:35:13 +00002282 BBState.ExitValue -= TII->getFrameTotalSize(I);
Manman Renaa6875b2013-07-15 21:26:31 +00002283 BBState.ExitIsSetup = true;
2284 }
2285
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002286 if (I.getOpcode() == FrameDestroyOpcode) {
Serge Pavlovd526b132017-05-09 13:35:13 +00002287 int Size = TII->getFrameTotalSize(I);
Manman Renaa6875b2013-07-15 21:26:31 +00002288 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002289 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00002290 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
2291 BBState.ExitValue;
2292 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002293 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00002294 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00002295 << AbsSPAdj << ">.\n";
2296 }
2297 BBState.ExitValue += Size;
2298 BBState.ExitIsSetup = false;
2299 }
2300 }
2301 SPState[MBB->getNumber()] = BBState;
2302
2303 // Make sure the exit state of any predecessor is consistent with the entry
2304 // state.
2305 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
2306 E = MBB->pred_end(); I != E; ++I) {
2307 if (Reachable.count(*I) &&
2308 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
2309 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
2310 report("The exit stack state of a predecessor is inconsistent.", MBB);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00002311 errs() << "Predecessor " << printMBBReference(*(*I))
2312 << " has exit state (" << SPState[(*I)->getNumber()].ExitValue
2313 << ", " << SPState[(*I)->getNumber()].ExitIsSetup << "), while "
2314 << printMBBReference(*MBB) << " has entry state ("
2315 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
Manman Renaa6875b2013-07-15 21:26:31 +00002316 }
2317 }
2318
2319 // Make sure the entry state of any successor is consistent with the exit
2320 // state.
2321 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
2322 E = MBB->succ_end(); I != E; ++I) {
2323 if (Reachable.count(*I) &&
2324 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
2325 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
2326 report("The entry stack state of a successor is inconsistent.", MBB);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00002327 errs() << "Successor " << printMBBReference(*(*I))
2328 << " has entry state (" << SPState[(*I)->getNumber()].EntryValue
2329 << ", " << SPState[(*I)->getNumber()].EntryIsSetup << "), while "
2330 << printMBBReference(*MBB) << " has exit state ("
2331 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
Manman Renaa6875b2013-07-15 21:26:31 +00002332 }
2333 }
2334
2335 // Make sure a basic block with return ends with zero stack adjustment.
2336 if (!MBB->empty() && MBB->back().isReturn()) {
2337 if (BBState.ExitIsSetup)
2338 report("A return block ends with a FrameSetup.", MBB);
2339 if (BBState.ExitValue)
2340 report("A return block ends with a nonzero stack adjustment.", MBB);
2341 }
2342 }
2343}