blob: 0ad792ac62cf4f9f22d29394848e7216ed399e4f [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Pass to verify generated machine code. The following is checked:
10//
11// Operand counts: All explicit operands must be present.
12//
13// Register classes: All physical and virtual register operands must be
14// compatible with the register class required by the instruction descriptor.
15//
16// Register live intervals: Registers must be defined only once, and must be
17// defined before use.
18//
Matthias Braunbb8507e2017-10-12 22:57:28 +000019// The machine code verifier is enabled from LLVMTargetMachine.cpp with the
20// command-line option -verify-machineinstrs, or by defining the environment
21// variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
22// the verifier errors.
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000023//===----------------------------------------------------------------------===//
24
Krzysztof Parzyszek9af86a52018-08-16 19:13:28 +000025#include "LiveRangeCalc.h"
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
Daniel Sanders1b493732018-10-03 22:05:31 +0000111 const MachineInstr *FirstNonPHI;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000112 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000113 BlockSet FunctionBlocks;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000114
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000115 BitVector regsReserved;
116 RegSet regsLive;
Jakob Stoklund Olesen2d59cff2009-08-08 13:19:25 +0000117 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000118 RegMaskVector regMasks;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000119
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000120 SlotIndex lastIndex;
121
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000122 // Add Reg and any sub-registers to RV
123 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
124 RV.push_back(Reg);
125 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000126 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
127 RV.push_back(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000128 }
129
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000130 struct BBInfo {
131 // Is this MBB reachable from the MF entry point?
Eugene Zelenko32a40562017-09-11 23:00:48 +0000132 bool reachable = false;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000133
134 // Vregs that must be live in because they are used without being
135 // defined. Map value is the user.
136 RegMap vregsLiveIn;
137
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000138 // Regs killed in MBB. They may be defined again, and will then be in both
139 // regsKilled and regsLiveOut.
140 RegSet regsKilled;
141
142 // Regs defined in MBB and live out. Note that vregs passing through may
143 // be live out without being mentioned here.
144 RegSet regsLiveOut;
145
146 // Vregs that pass through MBB untouched. This set is disjoint from
147 // regsKilled and regsLiveOut.
148 RegSet vregsPassed;
149
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000150 // Vregs that must pass through MBB because they are needed by a successor
151 // block. This set is disjoint from regsLiveOut.
152 RegSet vregsRequired;
153
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000154 // Set versions of block's predecessor and successor lists.
155 BlockSet Preds, Succs;
156
Eugene Zelenko32a40562017-09-11 23:00:48 +0000157 BBInfo() = default;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000158
159 // Add register to vregsPassed if it belongs there. Return true if
160 // anything changed.
161 bool addPassed(unsigned Reg) {
162 if (!TargetRegisterInfo::isVirtualRegister(Reg))
163 return false;
164 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
165 return false;
166 return vregsPassed.insert(Reg).second;
167 }
168
169 // Same for a full set.
170 bool addPassed(const RegSet &RS) {
171 bool changed = false;
172 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
173 if (addPassed(*I))
174 changed = true;
175 return changed;
176 }
177
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000178 // Add register to vregsRequired if it belongs there. Return true if
179 // anything changed.
180 bool addRequired(unsigned Reg) {
181 if (!TargetRegisterInfo::isVirtualRegister(Reg))
182 return false;
183 if (regsLiveOut.count(Reg))
184 return false;
185 return vregsRequired.insert(Reg).second;
186 }
187
188 // Same for a full set.
189 bool addRequired(const RegSet &RS) {
190 bool changed = false;
191 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
192 if (addRequired(*I))
193 changed = true;
194 return changed;
195 }
196
197 // Same for a full map.
198 bool addRequired(const RegMap &RM) {
199 bool changed = false;
200 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
201 if (addRequired(I->first))
202 changed = true;
203 return changed;
204 }
205
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000206 // Live-out registers are either in regsLiveOut or vregsPassed.
207 bool isLiveOut(unsigned Reg) const {
208 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
209 }
210 };
211
212 // Extra register info per MBB.
213 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
214
215 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000216 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000217 }
218
Matthias Braun4682ac62017-05-05 22:04:05 +0000219 bool isAllocatable(unsigned Reg) const {
220 return Reg < TRI->getNumRegs() && TRI->isInAllocatableClass(Reg) &&
Matt Arsenault9cac4e62019-06-19 00:25:39 +0000221 !regsReserved.test(Reg);
Lang Hames1ce837a2012-02-14 19:17:48 +0000222 }
223
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000224 // Analysis information if available
225 LiveVariables *LiveVars;
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +0000226 LiveIntervals *LiveInts;
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000227 LiveStacks *LiveStks;
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000228 SlotIndexes *Indexes;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000229
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000230 void visitMachineFunctionBefore();
231 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000232 void visitMachineBundleBefore(const MachineInstr *MI);
Matt Arsenault46f9c6c2019-02-04 23:29:11 +0000233
Matt Arsenaultf2a26332019-02-04 23:29:16 +0000234 bool verifyVectorElementMatch(LLT Ty0, LLT Ty1, const MachineInstr *MI);
Matt Arsenault46f9c6c2019-02-04 23:29:11 +0000235 void verifyPreISelGenericInstruction(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000236 void visitMachineInstrBefore(const MachineInstr *MI);
237 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
238 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000239 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000240 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
241 void visitMachineFunctionAfter();
242
243 void report(const char *msg, const MachineFunction *MF);
244 void report(const char *msg, const MachineBasicBlock *MBB);
245 void report(const char *msg, const MachineInstr *MI);
Roman Tereshinf487eda2018-05-07 22:31:12 +0000246 void report(const char *msg, const MachineOperand *MO, unsigned MONum,
247 LLT MOVRegType = LLT{});
Matthias Braun7e624d52015-11-09 23:59:33 +0000248
249 void report_context(const LiveInterval &LI) const;
Matt Arsenault892fcd02016-07-25 19:39:01 +0000250 void report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000251 LaneBitmask LaneMask) const;
252 void report_context(const LiveRange::Segment &S) const;
253 void report_context(const VNInfo &VNI) const;
Matthias Braun579c9cd2016-02-02 02:44:25 +0000254 void report_context(SlotIndex Pos) const;
Florian Hahnc1ece1b2019-01-08 15:16:23 +0000255 void report_context(MCPhysReg PhysReg) const;
Matthias Braun579c9cd2016-02-02 02:44:25 +0000256 void report_context_liverange(const LiveRange &LR) const;
Matthias Braun1377fd62016-02-02 20:04:51 +0000257 void report_context_lanemask(LaneBitmask LaneMask) const;
Matthias Braun30668dd2016-05-11 21:31:39 +0000258 void report_context_vreg(unsigned VReg) const;
Fangrui Songcb0bab82018-07-16 18:51:40 +0000259 void report_context_vreg_regunit(unsigned VRegOrUnit) const;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000260
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000261 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000262
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +0000263 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Matthias Braun1377fd62016-02-02 20:04:51 +0000264 void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
Fangrui Songcb0bab82018-07-16 18:51:40 +0000265 SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000266 LaneBitmask LaneMask = LaneBitmask::getNone());
Matthias Braun1377fd62016-02-02 20:04:51 +0000267 void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
Fangrui Songcb0bab82018-07-16 18:51:40 +0000268 SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
Bjorn Petterssonb2154af2018-09-20 06:59:18 +0000269 bool SubRangeCheck = false,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000270 LaneBitmask LaneMask = LaneBitmask::getNone());
Matthias Braun1377fd62016-02-02 20:04:51 +0000271
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000272 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +0000273 void calcRegsPassed();
Matthias Brauna6d53742017-11-28 03:54:19 +0000274 void checkPHIOps(const MachineBasicBlock &MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000275
276 void calcRegsRequired();
277 void verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +0000278 void verifyLiveIntervals();
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +0000279 void verifyLiveInterval(const LiveInterval&);
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000280 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000281 LaneBitmask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000282 void verifyLiveRangeSegment(const LiveRange&,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +0000283 const LiveRange::const_iterator I, unsigned,
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000284 LaneBitmask);
285 void verifyLiveRange(const LiveRange&, unsigned,
286 LaneBitmask LaneMask = LaneBitmask::getNone());
Manman Renaa6875b2013-07-15 21:26:31 +0000287
288 void verifyStackFrame();
Matthias Braun80595462015-09-09 17:49:46 +0000289
290 void verifySlotIndexes() const;
Derek Schuff42666ee2016-03-29 17:40:22 +0000291 void verifyProperties(const MachineFunction &MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000292 };
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000293
294 struct MachineVerifierPass : public MachineFunctionPass {
295 static char ID; // Pass ID, replacement for typeid
Eugene Zelenko32a40562017-09-11 23:00:48 +0000296
Matthias Brauna4e932d2014-12-11 19:41:51 +0000297 const std::string Banner;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000298
Sven van Haastregt04bfa872017-03-29 15:25:06 +0000299 MachineVerifierPass(std::string banner = std::string())
Sven van Haastregt039a6d92017-03-29 09:08:25 +0000300 : MachineFunctionPass(ID), Banner(std::move(banner)) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000301 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
302 }
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000303
Craig Topper4584cd52014-03-07 09:26:03 +0000304 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000305 AU.setPreservesAll();
306 MachineFunctionPass::getAnalysisUsage(AU);
307 }
308
Craig Topper4584cd52014-03-07 09:26:03 +0000309 bool runOnMachineFunction(MachineFunction &MF) override {
Matthias Braunb3aefc32016-02-15 19:25:31 +0000310 unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
311 if (FoundErrors)
312 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000313 return false;
314 }
315 };
316
Eugene Zelenko32a40562017-09-11 23:00:48 +0000317} // end anonymous namespace
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000318
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000319char MachineVerifierPass::ID = 0;
Eugene Zelenko32a40562017-09-11 23:00:48 +0000320
Owen Andersond31d82d2010-08-23 17:52:01 +0000321INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000322 "Verify generated machine code", false, false)
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000323
Matthias Brauna4e932d2014-12-11 19:41:51 +0000324FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000325 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000326}
327
Matthias Braunb3aefc32016-02-15 19:25:31 +0000328bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
329 const {
330 MachineFunction &MF = const_cast<MachineFunction&>(*this);
331 unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
332 if (AbortOnErrors && FoundErrors)
333 report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
334 return FoundErrors == 0;
Jakob Stoklund Olesen27440e72009-11-13 21:56:09 +0000335}
336
Matthias Braun80595462015-09-09 17:49:46 +0000337void MachineVerifier::verifySlotIndexes() const {
338 if (Indexes == nullptr)
339 return;
340
341 // Ensure the IdxMBB list is sorted by slot indexes.
342 SlotIndex Last;
343 for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
344 E = Indexes->MBBIndexEnd(); I != E; ++I) {
345 assert(!Last.isValid() || I->first > Last);
346 Last = I->first;
347 }
348}
349
Derek Schuff42666ee2016-03-29 17:40:22 +0000350void MachineVerifier::verifyProperties(const MachineFunction &MF) {
351 // If a pass has introduced virtual registers without clearing the
Matthias Braun1eb47362016-08-25 01:27:13 +0000352 // NoVRegs property (or set it without allocating the vregs)
Derek Schuff42666ee2016-03-29 17:40:22 +0000353 // then report an error.
354 if (MF.getProperties().hasProperty(
Matthias Braun1eb47362016-08-25 01:27:13 +0000355 MachineFunctionProperties::Property::NoVRegs) &&
356 MRI->getNumVirtRegs())
357 report("Function has NoVRegs property but there are VReg operands", &MF);
Derek Schuff42666ee2016-03-29 17:40:22 +0000358}
359
Matthias Braunb3aefc32016-02-15 19:25:31 +0000360unsigned MachineVerifier::verify(MachineFunction &MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000361 foundErrors = 0;
362
363 this->MF = &MF;
364 TM = &MF.getTarget();
Eric Christophereb9e87f2014-10-14 07:00:33 +0000365 TII = MF.getSubtarget().getInstrInfo();
366 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000367 MRI = &MF.getRegInfo();
368
Roman Tereshin3054ece2018-02-28 17:55:45 +0000369 const bool isFunctionFailedISel = MF.getProperties().hasProperty(
370 MachineFunctionProperties::Property::FailedISel);
Daniel Sanders74de21d2018-10-02 17:56:58 +0000371
372 // If we're mid-GlobalISel and we already triggered the fallback path then
373 // it's expected that the MIR is somewhat broken but that's ok since we'll
374 // reset it and clear the FailedISel attribute in ResetMachineFunctions.
375 if (isFunctionFailedISel)
376 return foundErrors;
377
Roman Tereshin3054ece2018-02-28 17:55:45 +0000378 isFunctionRegBankSelected =
379 !isFunctionFailedISel &&
380 MF.getProperties().hasProperty(
381 MachineFunctionProperties::Property::RegBankSelected);
382 isFunctionSelected = !isFunctionFailedISel &&
383 MF.getProperties().hasProperty(
384 MachineFunctionProperties::Property::Selected);
Craig Topperc0196b12014-04-14 00:51:57 +0000385 LiveVars = nullptr;
386 LiveInts = nullptr;
387 LiveStks = nullptr;
388 Indexes = nullptr;
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000389 if (PASS) {
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000390 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenb4ef4a92010-08-05 23:51:26 +0000391 // We don't want to verify LiveVariables if LiveIntervals is available.
392 if (!LiveInts)
393 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +0000394 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000395 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +0000396 }
397
Matthias Braun80595462015-09-09 17:49:46 +0000398 verifySlotIndexes();
399
Derek Schuff42666ee2016-03-29 17:40:22 +0000400 verifyProperties(MF);
401
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000402 visitMachineFunctionBefore();
403 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
404 MFI!=MFE; ++MFI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000405 visitMachineBasicBlockBefore(&*MFI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000406 // Keep track of the current bundle header.
Craig Topperc0196b12014-04-14 00:51:57 +0000407 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000408 // Do we expect the next instruction to be part of the same bundle?
409 bool InBundle = false;
410
Evan Cheng7fae11b2011-12-14 02:11:42 +0000411 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
412 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000413 if (MBBI->getParent() != &*MFI) {
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +0000414 report("Bad instruction parent pointer", &*MFI);
Owen Anderson21b17882015-02-04 00:02:59 +0000415 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesenb5b4a5d2011-01-12 21:27:41 +0000416 continue;
417 }
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000418
419 // Check for consistent bundle flags.
420 if (InBundle && !MBBI->isBundledWithPred())
421 report("Missing BundledPred flag, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000422 "BundledSucc was set on predecessor",
423 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000424 if (!InBundle && MBBI->isBundledWithPred())
425 report("BundledPred flag is set, "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000426 "but BundledSucc not set on predecessor",
427 &*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000428
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000429 // Is this a bundle header?
430 if (!MBBI->isInsideBundle()) {
431 if (CurBundle)
432 visitMachineBundleAfter(CurBundle);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000433 CurBundle = &*MBBI;
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000434 visitMachineBundleBefore(CurBundle);
435 } else if (!CurBundle)
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +0000436 report("No bundle header", &*MBBI);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000437 visitMachineInstrBefore(&*MBBI);
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000438 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
439 const MachineInstr &MI = *MBBI;
440 const MachineOperand &Op = MI.getOperand(I);
441 if (Op.getParent() != &MI) {
Matt Arsenault59d2ca12015-04-30 23:20:56 +0000442 // Make sure to use correct addOperand / RemoveOperand / ChangeTo
Matt Arsenaultee5c2ab2015-04-30 19:35:41 +0000443 // functions when replacing operands of a MachineInstr.
444 report("Instruction has operand with wrong parent set", &MI);
445 }
446
447 visitMachineOperand(&Op, I);
448 }
449
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000450 visitMachineInstrAfter(&*MBBI);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000451
452 // Was this the last bundled instruction?
453 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000454 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000455 if (CurBundle)
456 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen29c27712012-12-18 22:55:07 +0000457 if (InBundle)
458 report("BundledSucc flag set on last instruction in block", &MFI->back());
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000459 visitMachineBasicBlockAfter(&*MFI);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000460 }
461 visitMachineFunctionAfter();
462
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000463 // Clean up.
464 regsLive.clear();
465 regsDefined.clear();
466 regsDead.clear();
467 regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +0000468 regMasks.clear();
Jakob Stoklund Olesendcf009c2009-08-08 15:34:50 +0000469 MBBInfoMap.clear();
470
Matthias Braunb3aefc32016-02-15 19:25:31 +0000471 return foundErrors;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000472}
473
Chris Lattner75f40452009-08-23 01:03:30 +0000474void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000475 assert(MF);
Owen Anderson21b17882015-02-04 00:02:59 +0000476 errs() << '\n';
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000477 if (!foundErrors++) {
478 if (Banner)
Owen Anderson21b17882015-02-04 00:02:59 +0000479 errs() << "# " << Banner << '\n';
Matthias Braun42b4b632015-11-09 23:59:23 +0000480 if (LiveInts != nullptr)
481 LiveInts->print(errs());
482 else
483 MF->print(errs(), Indexes);
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +0000484 }
Owen Anderson21b17882015-02-04 00:02:59 +0000485 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Toppera538d832012-08-22 06:07:19 +0000486 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000487}
488
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000489void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000490 assert(MBB);
491 report(msg, MBB->getParent());
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000492 errs() << "- basic block: " << printMBBReference(*MBB) << ' '
493 << MBB->getName() << " (" << (const void *)MBB << ')';
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000494 if (Indexes)
Owen Anderson21b17882015-02-04 00:02:59 +0000495 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000496 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Owen Anderson21b17882015-02-04 00:02:59 +0000497 errs() << '\n';
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000498}
499
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000500void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000501 assert(MI);
502 report(msg, MI->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000503 errs() << "- instruction: ";
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000504 if (Indexes && Indexes->hasIndex(*MI))
505 errs() << Indexes->getInstructionIndex(*MI) << '\t';
Matthias Braun45718db2015-11-09 23:59:25 +0000506 MI->print(errs(), /*SkipOpers=*/true);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000507}
508
Roman Tereshinf487eda2018-05-07 22:31:12 +0000509void MachineVerifier::report(const char *msg, const MachineOperand *MO,
510 unsigned MONum, LLT MOVRegType) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000511 assert(MO);
512 report(msg, MO->getParent());
Owen Anderson21b17882015-02-04 00:02:59 +0000513 errs() << "- operand " << MONum << ": ";
Roman Tereshinf487eda2018-05-07 22:31:12 +0000514 MO->print(errs(), MOVRegType, TRI);
Owen Anderson21b17882015-02-04 00:02:59 +0000515 errs() << "\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000516}
517
Matthias Braun579c9cd2016-02-02 02:44:25 +0000518void MachineVerifier::report_context(SlotIndex Pos) const {
519 errs() << "- at: " << Pos << '\n';
520}
521
Matthias Braun7e624d52015-11-09 23:59:33 +0000522void MachineVerifier::report_context(const LiveInterval &LI) const {
Owen Anderson21b17882015-02-04 00:02:59 +0000523 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesenbde5dc52012-08-02 14:31:49 +0000524}
525
Matt Arsenault892fcd02016-07-25 19:39:01 +0000526void MachineVerifier::report_context(const LiveRange &LR, unsigned VRegUnit,
Matthias Braun7e624d52015-11-09 23:59:33 +0000527 LaneBitmask LaneMask) const {
Matthias Braun579c9cd2016-02-02 02:44:25 +0000528 report_context_liverange(LR);
Matt Arsenault892fcd02016-07-25 19:39:01 +0000529 report_context_vreg_regunit(VRegUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000530 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +0000531 report_context_lanemask(LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +0000532}
533
Matthias Braun7e624d52015-11-09 23:59:33 +0000534void MachineVerifier::report_context(const LiveRange::Segment &S) const {
535 errs() << "- segment: " << S << '\n';
536}
537
538void MachineVerifier::report_context(const VNInfo &VNI) const {
539 errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
Matthias Braun364e6e92013-10-10 21:28:54 +0000540}
541
Matthias Braun579c9cd2016-02-02 02:44:25 +0000542void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
543 errs() << "- liverange: " << LR << '\n';
544}
545
Florian Hahnc1ece1b2019-01-08 15:16:23 +0000546void MachineVerifier::report_context(MCPhysReg PReg) const {
547 errs() << "- p. register: " << printReg(PReg, TRI) << '\n';
548}
549
Matthias Braun30668dd2016-05-11 21:31:39 +0000550void MachineVerifier::report_context_vreg(unsigned VReg) const {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000551 errs() << "- v. register: " << printReg(VReg, TRI) << '\n';
Matthias Braun30668dd2016-05-11 21:31:39 +0000552}
553
Matthias Braun1377fd62016-02-02 20:04:51 +0000554void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
555 if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
Matthias Braun30668dd2016-05-11 21:31:39 +0000556 report_context_vreg(VRegOrUnit);
Matthias Braun1377fd62016-02-02 20:04:51 +0000557 } else {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000558 errs() << "- regunit: " << printRegUnit(VRegOrUnit, TRI) << '\n';
Matthias Braun1377fd62016-02-02 20:04:51 +0000559 }
560}
561
562void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
563 errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
564}
565
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000566void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000567 BBInfo &MInfo = MBBInfoMap[MBB];
568 if (!MInfo.reachable) {
569 MInfo.reachable = true;
570 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
571 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
572 markReachable(*SuI);
573 }
574}
575
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +0000576void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000577 lastIndex = SlotIndex();
Matthias Braun4682ac62017-05-05 22:04:05 +0000578 regsReserved = MRI->reservedRegsFrozen() ? MRI->getReservedRegs()
579 : TRI->getReservedRegs(*MF);
Jakob Stoklund Olesen3c2a1de2009-08-04 19:18:01 +0000580
Justin Bogner20dd36a2017-04-11 19:32:41 +0000581 if (!MF->empty())
582 markReachable(&MF->front());
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000583
584 // Build a set of the basic blocks in the function.
585 FunctionBlocks.clear();
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000586 for (const auto &MBB : *MF) {
587 FunctionBlocks.insert(&MBB);
588 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000589
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000590 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
591 if (MInfo.Preds.size() != MBB.pred_size())
592 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000593
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000594 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
595 if (MInfo.Succs.size() != MBB.succ_size())
596 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000597 }
Jakob Stoklund Olesene17c3fd2013-04-19 21:40:57 +0000598
599 // Check that the register use lists are sane.
600 MRI->verifyUseLists();
Manman Renaa6875b2013-07-15 21:26:31 +0000601
Justin Bogner20dd36a2017-04-11 19:32:41 +0000602 if (!MF->empty())
603 verifyStackFrame();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000604}
605
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000606// Does iterator point to a and b as the first two elements?
Dan Gohmanb29cda92010-04-15 17:08:50 +0000607static bool matchPair(MachineBasicBlock::const_succ_iterator i,
608 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000609 if (*i == a)
610 return *++i == b;
611 if (*i == b)
612 return *++i == a;
613 return false;
614}
615
616void
617MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000618 FirstTerminator = nullptr;
Daniel Sanders1b493732018-10-03 22:05:31 +0000619 FirstNonPHI = nullptr;
Jakob Stoklund Olesen3bb99bc2011-09-23 22:45:39 +0000620
Matthias Braun79f85b32016-08-24 01:32:41 +0000621 if (!MF->getProperties().hasProperty(
Matthias Braun11723322017-01-05 20:01:19 +0000622 MachineFunctionProperties::Property::NoPHIs) && MRI->tracksLiveness()) {
Lang Hames1ce837a2012-02-14 19:17:48 +0000623 // If this block has allocatable physical registers live-in, check that
624 // it is an entry block or landing pad.
Matthias Braund9da1622015-09-09 18:08:03 +0000625 for (const auto &LI : MBB->liveins()) {
626 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +0000627 MBB->getIterator() != MBB->getParent()->begin()) {
Matt Arsenault900b21c2017-02-15 22:19:06 +0000628 report("MBB has allocatable live-in, but isn't entry or landing-pad.", MBB);
Florian Hahnc1ece1b2019-01-08 15:16:23 +0000629 report_context(LI.PhysReg);
Lang Hames1ce837a2012-02-14 19:17:48 +0000630 }
631 }
632 }
633
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000634 // Count the number of landing pad successors.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000635 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000636 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000637 E = MBB->succ_end(); I != E; ++I) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000638 if ((*I)->isEHPad())
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000639 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000640 if (!FunctionBlocks.count(*I))
641 report("MBB has successor that isn't part of the function.", MBB);
642 if (!MBBInfoMap[*I].Preds.count(MBB)) {
643 report("Inconsistent CFG", MBB);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000644 errs() << "MBB is not in the predecessor list of the successor "
645 << printMBBReference(*(*I)) << ".\n";
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000646 }
647 }
648
649 // Check the predecessor list.
650 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
651 E = MBB->pred_end(); I != E; ++I) {
652 if (!FunctionBlocks.count(*I))
653 report("MBB has predecessor that isn't part of the function.", MBB);
654 if (!MBBInfoMap[*I].Succs.count(MBB)) {
655 report("Inconsistent CFG", MBB);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000656 errs() << "MBB is not in the successor list of the predecessor "
657 << printMBBReference(*(*I)) << ".\n";
Jakob Stoklund Olesende31b522012-08-20 20:52:06 +0000658 }
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000659 }
Bill Wendling2a401312011-05-04 22:54:05 +0000660
661 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
662 const BasicBlock *BB = MBB->getBasicBlock();
Matthias Braunf1caa282017-12-15 22:22:58 +0000663 const Function &F = MF->getFunction();
Bill Wendling2a401312011-05-04 22:54:05 +0000664 if (LandingPadSuccs.size() > 1 &&
665 !(AsmInfo &&
666 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
Reid Kleckner64b003f2015-11-09 21:04:00 +0000667 BB && isa<SwitchInst>(BB->getTerminator())) &&
Heejin Ahnb4be38f2018-05-17 20:52:03 +0000668 !isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000669 report("MBB has more than one landing pad successor", MBB);
670
Dan Gohman352a4952009-08-27 02:43:49 +0000671 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Craig Topperc0196b12014-04-14 00:51:57 +0000672 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman352a4952009-08-27 02:43:49 +0000673 SmallVector<MachineOperand, 4> Cond;
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000674 if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
675 Cond)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000676 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
677 // check whether its answers match up with reality.
678 if (!TBB && !FBB) {
679 // Block falls through to its successor.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000680 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000681 ++MBBI;
682 if (MBBI == MF->end()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000683 // It's possible that the block legitimately ends with a noreturn
684 // call or an unreachable, in which case it won't actually fall
685 // out the bottom of the function.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000686 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000687 // It's possible that the block legitimately ends with a noreturn
Hiroshi Inouedad8c6a2019-01-09 05:11:10 +0000688 // call or an unreachable, in which case it won't actually fall
Dan Gohmaned10d7c2009-08-27 18:14:26 +0000689 // out of the block.
Cameron Zwarich4ffda702010-12-20 04:19:48 +0000690 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000691 report("MBB exits via unconditional fall-through but doesn't have "
692 "exactly one CFG successor!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000693 } else if (!MBB->isSuccessor(&*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000694 report("MBB exits via unconditional fall-through but its successor "
695 "differs from its CFG successor!", MBB);
696 }
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000697 if (!MBB->empty() && MBB->back().isBarrier() &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000698 !TII->isPredicated(MBB->back())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000699 report("MBB exits via unconditional fall-through but ends with a "
700 "barrier instruction!", MBB);
701 }
702 if (!Cond.empty()) {
703 report("MBB exits via unconditional fall-through but has a condition!",
704 MBB);
705 }
706 } else if (TBB && !FBB && Cond.empty()) {
707 // Block unconditionally branches somewhere.
Ahmed Bougachafb6eeb72014-12-01 18:43:53 +0000708 // If the block has exactly one successor, that happens to be a
709 // landingpad, accept it as valid control flow.
710 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
711 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
712 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman352a4952009-08-27 02:43:49 +0000713 report("MBB exits via unconditional branch but doesn't have "
714 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen7c9d5842010-10-21 18:47:06 +0000715 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000716 report("MBB exits via unconditional branch but the CFG "
717 "successor doesn't match the actual successor!", MBB);
718 }
719 if (MBB->empty()) {
720 report("MBB exits via unconditional branch but doesn't contain "
721 "any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000722 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000723 report("MBB exits via unconditional branch but doesn't end with a "
724 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000725 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000726 report("MBB exits via unconditional branch but the branch isn't a "
727 "terminator instruction!", MBB);
728 }
729 } else if (TBB && !FBB && !Cond.empty()) {
730 // Block conditionally branches somewhere, otherwise falls through.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000731 MachineFunction::const_iterator MBBI = MBB->getIterator();
Dan Gohman352a4952009-08-27 02:43:49 +0000732 ++MBBI;
733 if (MBBI == MF->end()) {
734 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko349d1a32012-12-19 22:13:01 +0000735 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000736 // A conditional branch with only one successor is weird, but allowed.
737 if (&*MBBI != TBB)
738 report("MBB exits via conditional branch/fall-through but only has "
739 "one CFG successor!", MBB);
740 else if (TBB != *MBB->succ_begin())
741 report("MBB exits via conditional branch/fall-through but the CFG "
742 "successor don't match the actual successor!", MBB);
743 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000744 report("MBB exits via conditional branch/fall-through but doesn't have "
745 "exactly two CFG successors!", MBB);
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000746 } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000747 report("MBB exits via conditional branch/fall-through but the CFG "
748 "successors don't match the actual successors!", MBB);
749 }
750 if (MBB->empty()) {
751 report("MBB exits via conditional branch/fall-through but doesn't "
752 "contain any instructions!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000753 } else if (MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000754 report("MBB exits via conditional branch/fall-through but ends with a "
755 "barrier instruction!", MBB);
Benjamin Kramer5256ce32014-05-24 13:31:10 +0000756 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000757 report("MBB exits via conditional branch/fall-through but the branch "
758 "isn't a terminator instruction!", MBB);
759 }
760 } else if (TBB && FBB) {
761 // Block conditionally branches somewhere, otherwise branches
762 // somewhere else.
Jakob Stoklund Olesen7d33c572012-08-20 21:39:52 +0000763 if (MBB->succ_size() == 1) {
764 // A conditional branch with only one successor is weird, but allowed.
765 if (FBB != TBB)
766 report("MBB exits via conditional branch/branch through but only has "
767 "one CFG successor!", MBB);
768 else if (TBB != *MBB->succ_begin())
769 report("MBB exits via conditional branch/branch through but the CFG "
770 "successor don't match the actual successor!", MBB);
771 } else if (MBB->succ_size() != 2) {
Dan Gohman352a4952009-08-27 02:43:49 +0000772 report("MBB exits via conditional branch/branch but doesn't have "
773 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1ecc8b22009-11-13 21:55:54 +0000774 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman352a4952009-08-27 02:43:49 +0000775 report("MBB exits via conditional branch/branch but the CFG "
776 "successors don't match the actual successors!", MBB);
777 }
778 if (MBB->empty()) {
779 report("MBB exits via conditional branch/branch but doesn't "
780 "contain any instructions!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000781 } else if (!MBB->back().isBarrier()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000782 report("MBB exits via conditional branch/branch but doesn't end with a "
783 "barrier instruction!", MBB);
Benjamin Kramer389cec02014-05-24 13:13:17 +0000784 } else if (!MBB->back().isTerminator()) {
Dan Gohman352a4952009-08-27 02:43:49 +0000785 report("MBB exits via conditional branch/branch but the branch "
786 "isn't a terminator instruction!", MBB);
787 }
788 if (Cond.empty()) {
Matt Arsenault9ef8e512018-10-23 21:23:52 +0000789 report("MBB exits via conditional branch/branch but there's no "
Dan Gohman352a4952009-08-27 02:43:49 +0000790 "condition!", MBB);
791 }
792 } else {
793 report("AnalyzeBranch returned invalid data!", MBB);
794 }
795 }
796
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000797 regsLive.clear();
Matthias Braun11723322017-01-05 20:01:19 +0000798 if (MRI->tracksLiveness()) {
799 for (const auto &LI : MBB->liveins()) {
800 if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
801 report("MBB live-in list contains non-physical register", MBB);
802 continue;
803 }
804 for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
805 SubRegs.isValid(); ++SubRegs)
806 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000807 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000808 }
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000809
Matthias Braun941a7052016-07-28 18:40:00 +0000810 const MachineFrameInfo &MFI = MF->getFrameInfo();
811 BitVector PR = MFI.getPristineRegs(*MF);
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +0000812 for (unsigned I : PR.set_bits()) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000813 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
814 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000815 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen0e73fdf2009-08-13 16:19:51 +0000816 }
817
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000818 regsKilled.clear();
819 regsDefined.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +0000820
821 if (Indexes)
822 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000823}
824
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000825// This function gets called for all bundle headers, including normal
826// stand-alone unbundled instructions.
827void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000828 if (Indexes && Indexes->hasIndex(*MI)) {
829 SlotIndex idx = Indexes->getInstructionIndex(*MI);
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000830 if (!(idx > lastIndex)) {
831 report("Instruction index out of order", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000832 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000833 }
834 lastIndex = idx;
835 }
Pete Coopercd720162012-06-07 17:41:39 +0000836
837 // Ensure non-terminators don't follow terminators.
838 // Ignore predicated terminators formed by if conversion.
839 // FIXME: If conversion shouldn't need to violate this rule.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000840 if (MI->isTerminator() && !TII->isPredicated(*MI)) {
Pete Coopercd720162012-06-07 17:41:39 +0000841 if (!FirstTerminator)
842 FirstTerminator = MI;
Djordje Todorovic12aca5d2019-07-09 08:36:34 +0000843 } else if (FirstTerminator && !MI->isDebugEntryValue()) {
Pete Coopercd720162012-06-07 17:41:39 +0000844 report("Non-terminator instruction after the first terminator", MI);
Owen Anderson21b17882015-02-04 00:02:59 +0000845 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Coopercd720162012-06-07 17:41:39 +0000846 }
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +0000847}
848
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000849// The operands on an INLINEASM instruction must follow a template.
850// Verify that the flag operands make sense.
851void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
852 // The first two operands on INLINEASM are the asm string and global flags.
853 if (MI->getNumOperands() < 2) {
854 report("Too few operands on inline asm", MI);
855 return;
856 }
857 if (!MI->getOperand(0).isSymbol())
858 report("Asm string must be an external symbol", MI);
859 if (!MI->getOperand(1).isImm())
860 report("Asm flags must be an immediate", MI);
Chad Rosier9e1274f2012-10-30 19:11:54 +0000861 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
Wei Ding0526e7f2016-06-22 18:51:08 +0000862 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
863 // and Extra_IsConvergent = 32.
864 if (!isUInt<6>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000865 report("Unknown asm flags", &MI->getOperand(1), 1);
866
Gabor Horvathfee04342015-03-16 09:53:42 +0000867 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
Jakob Stoklund Olesen7a837b92012-08-29 18:11:05 +0000868
869 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
870 unsigned NumOps;
871 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
872 const MachineOperand &MO = MI->getOperand(OpNo);
873 // There may be implicit ops after the fixed operands.
874 if (!MO.isImm())
875 break;
876 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
877 }
878
879 if (OpNo > MI->getNumOperands())
880 report("Missing operands in last group", MI);
881
882 // An optional MDNode follows the groups.
883 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
884 ++OpNo;
885
886 // All trailing operands must be implicit registers.
887 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
888 const MachineOperand &MO = MI->getOperand(OpNo);
889 if (!MO.isReg() || !MO.isImplicit())
890 report("Expected implicit register after groups", &MO, OpNo);
891 }
892}
893
Matt Arsenaultf2a26332019-02-04 23:29:16 +0000894/// Check that types are consistent when two operands need to have the same
895/// number of vector elements.
896/// \return true if the types are valid.
897bool MachineVerifier::verifyVectorElementMatch(LLT Ty0, LLT Ty1,
898 const MachineInstr *MI) {
899 if (Ty0.isVector() != Ty1.isVector()) {
900 report("operand types must be all-vector or all-scalar", MI);
901 // Generally we try to report as many issues as possible at once, but in
902 // this case it's not clear what should we be comparing the size of the
903 // scalar with: the size of the whole vector or its lane. Instead of
904 // making an arbitrary choice and emitting not so helpful message, let's
905 // avoid the extra noise and stop here.
906 return false;
907 }
908
909 if (Ty0.isVector() && Ty0.getNumElements() != Ty1.getNumElements()) {
910 report("operand types must preserve number of vector elements", MI);
911 return false;
912 }
913
914 return true;
915}
916
Matt Arsenault46f9c6c2019-02-04 23:29:11 +0000917void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) {
918 if (isFunctionSelected)
919 report("Unexpected generic instruction in a Selected function", MI);
920
Evan Cheng6cc775f2011-06-28 19:10:37 +0000921 const MCInstrDesc &MCID = MI->getDesc();
Matt Arsenault46f9c6c2019-02-04 23:29:11 +0000922 unsigned NumOps = MI->getNumOperands();
Dan Gohmandb9493c2009-10-07 17:36:00 +0000923
Matt Arsenault46f9c6c2019-02-04 23:29:11 +0000924 // Check types.
925 SmallVector<LLT, 4> Types;
926 for (unsigned I = 0, E = std::min(MCID.getNumOperands(), NumOps);
Roman Tereshinf487eda2018-05-07 22:31:12 +0000927 I != E; ++I) {
Matt Arsenault46f9c6c2019-02-04 23:29:11 +0000928 if (!MCID.OpInfo[I].isGenericType())
929 continue;
930 // Generic instructions specify type equality constraints between some of
931 // their operands. Make sure these are consistent.
932 size_t TypeIdx = MCID.OpInfo[I].getGenericTypeIndex();
933 Types.resize(std::max(TypeIdx + 1, Types.size()));
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000934
Matt Arsenault46f9c6c2019-02-04 23:29:11 +0000935 const MachineOperand *MO = &MI->getOperand(I);
Matt Arsenault2bf74ec2019-02-05 00:53:22 +0000936 if (!MO->isReg()) {
937 report("generic instruction must use register operands", MI);
938 continue;
939 }
940
Matt Arsenault46f9c6c2019-02-04 23:29:11 +0000941 LLT OpTy = MRI->getType(MO->getReg());
942 // Don't report a type mismatch if there is no actual mismatch, only a
943 // type missing, to reduce noise:
944 if (OpTy.isValid()) {
945 // Only the first valid type for a type index will be printed: don't
946 // overwrite it later so it's always clear which type was expected:
947 if (!Types[TypeIdx].isValid())
948 Types[TypeIdx] = OpTy;
949 else if (Types[TypeIdx] != OpTy)
950 report("Type mismatch in generic instruction", MO, I, OpTy);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000951 } else {
Matt Arsenault46f9c6c2019-02-04 23:29:11 +0000952 // Generic instructions must have types attached to their operands.
953 report("Generic instruction is missing a virtual register type", MO, I);
Jakob Stoklund Olesene7709eb2010-08-05 22:32:21 +0000954 }
955 }
956
Matt Arsenault46f9c6c2019-02-04 23:29:11 +0000957 // Generic opcodes must not have physical register operands.
958 for (unsigned I = 0; I < MI->getNumOperands(); ++I) {
959 const MachineOperand *MO = &MI->getOperand(I);
960 if (MO->isReg() && TargetRegisterInfo::isPhysicalRegister(MO->getReg()))
961 report("Generic instruction cannot have physical register", MO, I);
Tim Northovere5102de2016-08-30 18:52:46 +0000962 }
963
Matt Arsenault46f9c6c2019-02-04 23:29:11 +0000964 // Avoid out of bounds in checks below. This was already reported earlier.
965 if (MI->getNumOperands() < MCID.getNumOperands())
966 return;
967
Andrew Trick924123a2011-09-21 02:20:46 +0000968 StringRef ErrorInfo;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000969 if (!TII->verifyInstruction(*MI, ErrorInfo))
Andrew Trick924123a2011-09-21 02:20:46 +0000970 report(ErrorInfo.data(), MI);
Philip Reames94cc4a22017-06-02 16:36:37 +0000971
972 // Verify properties of various specific instruction types
Matt Arsenault46f9c6c2019-02-04 23:29:11 +0000973 switch (MI->getOpcode()) {
Matt Arsenaulta7cd83b2019-01-22 18:53:41 +0000974 case TargetOpcode::G_CONSTANT:
975 case TargetOpcode::G_FCONSTANT: {
976 if (MI->getNumOperands() < MCID.getNumOperands())
977 break;
978
979 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
980 if (DstTy.isVector())
981 report("Instruction cannot use a vector result type", MI);
Matt Arsenault1f795e22019-02-04 23:29:31 +0000982
983 if (MI->getOpcode() == TargetOpcode::G_CONSTANT) {
984 if (!MI->getOperand(1).isCImm()) {
985 report("G_CONSTANT operand must be cimm", MI);
986 break;
987 }
988
989 const ConstantInt *CI = MI->getOperand(1).getCImm();
990 if (CI->getBitWidth() != DstTy.getSizeInBits())
991 report("inconsistent constant size", MI);
992 } else {
993 if (!MI->getOperand(1).isFPImm()) {
994 report("G_FCONSTANT operand must be fpimm", MI);
995 break;
996 }
997 const ConstantFP *CF = MI->getOperand(1).getFPImm();
998
999 if (APFloat::getSizeInBits(CF->getValueAPF().getSemantics()) !=
1000 DstTy.getSizeInBits()) {
1001 report("inconsistent constant size", MI);
1002 }
1003 }
1004
Matt Arsenaulta7cd83b2019-01-22 18:53:41 +00001005 break;
1006 }
Philip Reames94cc4a22017-06-02 16:36:37 +00001007 case TargetOpcode::G_LOAD:
1008 case TargetOpcode::G_STORE:
Amara Emerson711bbdc2019-01-27 11:34:41 +00001009 case TargetOpcode::G_ZEXTLOAD:
Matt Arsenaultfdfb7d72019-01-27 15:57:23 +00001010 case TargetOpcode::G_SEXTLOAD: {
Matt Arsenaultccb810f2019-01-30 01:10:42 +00001011 LLT ValTy = MRI->getType(MI->getOperand(0).getReg());
Matt Arsenaultfdfb7d72019-01-27 15:57:23 +00001012 LLT PtrTy = MRI->getType(MI->getOperand(1).getReg());
1013 if (!PtrTy.isPointer())
1014 report("Generic memory instruction must access a pointer", MI);
1015
Philip Reames94cc4a22017-06-02 16:36:37 +00001016 // Generic loads and stores must have a single MachineMemOperand
1017 // describing that access.
Amara Emerson711bbdc2019-01-27 11:34:41 +00001018 if (!MI->hasOneMemOperand()) {
Philip Reames94cc4a22017-06-02 16:36:37 +00001019 report("Generic instruction accessing memory must have one mem operand",
1020 MI);
Amara Emerson711bbdc2019-01-27 11:34:41 +00001021 } else {
Matt Arsenaultccb810f2019-01-30 01:10:42 +00001022 const MachineMemOperand &MMO = **MI->memoperands_begin();
Amara Emerson711bbdc2019-01-27 11:34:41 +00001023 if (MI->getOpcode() == TargetOpcode::G_ZEXTLOAD ||
1024 MI->getOpcode() == TargetOpcode::G_SEXTLOAD) {
Amara Emersond51adf02019-04-17 22:21:05 +00001025 if (MMO.getSizeInBits() >= ValTy.getSizeInBits())
Amara Emerson711bbdc2019-01-27 11:34:41 +00001026 report("Generic extload must have a narrower memory type", MI);
Matt Arsenaultccb810f2019-01-30 01:10:42 +00001027 } else if (MI->getOpcode() == TargetOpcode::G_LOAD) {
Amara Emersond51adf02019-04-17 22:21:05 +00001028 if (MMO.getSize() > ValTy.getSizeInBytes())
Matt Arsenaultccb810f2019-01-30 01:10:42 +00001029 report("load memory size cannot exceed result size", MI);
1030 } else if (MI->getOpcode() == TargetOpcode::G_STORE) {
Amara Emersond51adf02019-04-17 22:21:05 +00001031 if (ValTy.getSizeInBytes() < MMO.getSize())
Matt Arsenaultccb810f2019-01-30 01:10:42 +00001032 report("store memory size cannot exceed value size", MI);
Amara Emerson711bbdc2019-01-27 11:34:41 +00001033 }
1034 }
1035
Philip Reames94cc4a22017-06-02 16:36:37 +00001036 break;
Matt Arsenaultfdfb7d72019-01-27 15:57:23 +00001037 }
Aditya Nandakumarefd8a842017-08-23 20:45:48 +00001038 case TargetOpcode::G_PHI: {
1039 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1040 if (!DstTy.isValid() ||
1041 !std::all_of(MI->operands_begin() + 1, MI->operands_end(),
1042 [this, &DstTy](const MachineOperand &MO) {
1043 if (!MO.isReg())
1044 return true;
1045 LLT Ty = MRI->getType(MO.getReg());
1046 if (!Ty.isValid() || (Ty != DstTy))
1047 return false;
1048 return true;
1049 }))
1050 report("Generic Instruction G_PHI has operands with incompatible/missing "
1051 "types",
1052 MI);
1053 break;
1054 }
Matt Arsenaultbd3a5b22019-01-18 21:04:59 +00001055 case TargetOpcode::G_BITCAST: {
1056 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1057 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1058 if (!DstTy.isValid() || !SrcTy.isValid())
1059 break;
1060
1061 if (SrcTy.isPointer() != DstTy.isPointer())
1062 report("bitcast cannot convert between pointers and other types", MI);
1063
1064 if (SrcTy.getSizeInBits() != DstTy.getSizeInBits())
1065 report("bitcast sizes must match", MI);
1066 break;
1067 }
Matt Arsenaultd45b03b2019-01-29 23:29:00 +00001068 case TargetOpcode::G_INTTOPTR:
1069 case TargetOpcode::G_PTRTOINT:
1070 case TargetOpcode::G_ADDRSPACE_CAST: {
1071 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1072 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1073 if (!DstTy.isValid() || !SrcTy.isValid())
1074 break;
1075
Matt Arsenaultf2a26332019-02-04 23:29:16 +00001076 verifyVectorElementMatch(DstTy, SrcTy, MI);
Matt Arsenaultd45b03b2019-01-29 23:29:00 +00001077
1078 DstTy = DstTy.getScalarType();
1079 SrcTy = SrcTy.getScalarType();
1080
1081 if (MI->getOpcode() == TargetOpcode::G_INTTOPTR) {
1082 if (!DstTy.isPointer())
1083 report("inttoptr result type must be a pointer", MI);
1084 if (SrcTy.isPointer())
1085 report("inttoptr source type must not be a pointer", MI);
1086 } else if (MI->getOpcode() == TargetOpcode::G_PTRTOINT) {
1087 if (!SrcTy.isPointer())
1088 report("ptrtoint source type must be a pointer", MI);
1089 if (DstTy.isPointer())
1090 report("ptrtoint result type must not be a pointer", MI);
1091 } else {
1092 assert(MI->getOpcode() == TargetOpcode::G_ADDRSPACE_CAST);
1093 if (!SrcTy.isPointer() || !DstTy.isPointer())
1094 report("addrspacecast types must be pointers", MI);
1095 else {
1096 if (SrcTy.getAddressSpace() == DstTy.getAddressSpace())
1097 report("addrspacecast must convert different address spaces", MI);
1098 }
1099 }
1100
1101 break;
1102 }
Matt Arsenaulta3d0c5a2019-02-05 20:04:12 +00001103 case TargetOpcode::G_GEP: {
1104 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1105 LLT PtrTy = MRI->getType(MI->getOperand(1).getReg());
1106 LLT OffsetTy = MRI->getType(MI->getOperand(2).getReg());
1107 if (!DstTy.isValid() || !PtrTy.isValid() || !OffsetTy.isValid())
1108 break;
1109
1110 if (!PtrTy.getScalarType().isPointer())
1111 report("gep first operand must be a pointer", MI);
1112
1113 if (OffsetTy.getScalarType().isPointer())
1114 report("gep offset operand must not be a pointer", MI);
1115
1116 // TODO: Is the offset allowed to be a scalar with a vector?
1117 break;
1118 }
Roman Tereshind2421f92018-05-08 02:48:15 +00001119 case TargetOpcode::G_SEXT:
1120 case TargetOpcode::G_ZEXT:
1121 case TargetOpcode::G_ANYEXT:
1122 case TargetOpcode::G_TRUNC:
1123 case TargetOpcode::G_FPEXT:
1124 case TargetOpcode::G_FPTRUNC: {
1125 // Number of operands and presense of types is already checked (and
1126 // reported in case of any issues), so no need to report them again. As
1127 // we're trying to report as many issues as possible at once, however, the
1128 // instructions aren't guaranteed to have the right number of operands or
1129 // types attached to them at this point
1130 assert(MCID.getNumOperands() == 2 && "Expected 2 operands G_*{EXT,TRUNC}");
Roman Tereshind2421f92018-05-08 02:48:15 +00001131 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1132 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1133 if (!DstTy.isValid() || !SrcTy.isValid())
1134 break;
1135
Matt Arsenaultf2a26332019-02-04 23:29:16 +00001136 LLT DstElTy = DstTy.getScalarType();
1137 LLT SrcElTy = SrcTy.getScalarType();
Roman Tereshind2421f92018-05-08 02:48:15 +00001138 if (DstElTy.isPointer() || SrcElTy.isPointer())
1139 report("Generic extend/truncate can not operate on pointers", MI);
1140
Matt Arsenaultf2a26332019-02-04 23:29:16 +00001141 verifyVectorElementMatch(DstTy, SrcTy, MI);
1142
Roman Tereshind2421f92018-05-08 02:48:15 +00001143 unsigned DstSize = DstElTy.getSizeInBits();
1144 unsigned SrcSize = SrcElTy.getSizeInBits();
1145 switch (MI->getOpcode()) {
1146 default:
1147 if (DstSize <= SrcSize)
1148 report("Generic extend has destination type no larger than source", MI);
1149 break;
1150 case TargetOpcode::G_TRUNC:
1151 case TargetOpcode::G_FPTRUNC:
1152 if (DstSize >= SrcSize)
1153 report("Generic truncate has destination type no smaller than source",
1154 MI);
1155 break;
1156 }
1157 break;
1158 }
Matt Arsenaultf2a26332019-02-04 23:29:16 +00001159 case TargetOpcode::G_SELECT: {
1160 LLT SelTy = MRI->getType(MI->getOperand(0).getReg());
1161 LLT CondTy = MRI->getType(MI->getOperand(1).getReg());
1162 if (!SelTy.isValid() || !CondTy.isValid())
1163 break;
1164
1165 // Scalar condition select on a vector is valid.
1166 if (CondTy.isVector())
1167 verifyVectorElementMatch(SelTy, CondTy, MI);
1168 break;
1169 }
Amara Emerson5ec14602018-12-10 18:44:58 +00001170 case TargetOpcode::G_MERGE_VALUES: {
1171 // G_MERGE_VALUES should only be used to merge scalars into a larger scalar,
1172 // e.g. s2N = MERGE sN, sN
1173 // Merging multiple scalars into a vector is not allowed, should use
1174 // G_BUILD_VECTOR for that.
1175 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1176 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1177 if (DstTy.isVector() || SrcTy.isVector())
1178 report("G_MERGE_VALUES cannot operate on vectors", MI);
Matt Arsenault03ca1762019-07-01 18:01:35 +00001179
1180 const unsigned NumOps = MI->getNumOperands();
1181 if (DstTy.getSizeInBits() != SrcTy.getSizeInBits() * (NumOps - 1))
1182 report("G_MERGE_VALUES result size is inconsistent", MI);
1183
1184 for (unsigned I = 2; I != NumOps; ++I) {
1185 if (MRI->getType(MI->getOperand(I).getReg()) != SrcTy)
1186 report("G_MERGE_VALUES source types do not match", MI);
1187 }
1188
Amara Emerson5ec14602018-12-10 18:44:58 +00001189 break;
1190 }
1191 case TargetOpcode::G_UNMERGE_VALUES: {
1192 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1193 LLT SrcTy = MRI->getType(MI->getOperand(MI->getNumOperands()-1).getReg());
1194 // For now G_UNMERGE can split vectors.
1195 for (unsigned i = 0; i < MI->getNumOperands()-1; ++i) {
1196 if (MRI->getType(MI->getOperand(i).getReg()) != DstTy)
1197 report("G_UNMERGE_VALUES destination types do not match", MI);
1198 }
1199 if (SrcTy.getSizeInBits() !=
1200 (DstTy.getSizeInBits() * (MI->getNumOperands() - 1))) {
1201 report("G_UNMERGE_VALUES source operand does not cover dest operands",
1202 MI);
1203 }
1204 break;
1205 }
Amara Emersona0b15d82018-12-05 23:53:30 +00001206 case TargetOpcode::G_BUILD_VECTOR: {
1207 // Source types must be scalars, dest type a vector. Total size of scalars
1208 // must match the dest vector size.
1209 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1210 LLT SrcEltTy = MRI->getType(MI->getOperand(1).getReg());
Matt Arsenault59ecdb02019-02-15 15:24:34 +00001211 if (!DstTy.isVector() || SrcEltTy.isVector()) {
Amara Emersona0b15d82018-12-05 23:53:30 +00001212 report("G_BUILD_VECTOR must produce a vector from scalar operands", MI);
Matt Arsenault59ecdb02019-02-15 15:24:34 +00001213 break;
1214 }
1215
1216 if (DstTy.getElementType() != SrcEltTy)
1217 report("G_BUILD_VECTOR result element type must match source type", MI);
1218
1219 if (DstTy.getNumElements() != MI->getNumOperands() - 1)
1220 report("G_BUILD_VECTOR must have an operand for each elemement", MI);
1221
Amara Emersona0b15d82018-12-05 23:53:30 +00001222 for (unsigned i = 2; i < MI->getNumOperands(); ++i) {
1223 if (MRI->getType(MI->getOperand(1).getReg()) !=
1224 MRI->getType(MI->getOperand(i).getReg()))
1225 report("G_BUILD_VECTOR source operand types are not homogeneous", MI);
1226 }
Matt Arsenault59ecdb02019-02-15 15:24:34 +00001227
Amara Emersona0b15d82018-12-05 23:53:30 +00001228 break;
1229 }
1230 case TargetOpcode::G_BUILD_VECTOR_TRUNC: {
1231 // Source types must be scalars, dest type a vector. Scalar types must be
1232 // larger than the dest vector elt type, as this is a truncating operation.
1233 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1234 LLT SrcEltTy = MRI->getType(MI->getOperand(1).getReg());
1235 if (!DstTy.isVector() || SrcEltTy.isVector())
1236 report("G_BUILD_VECTOR_TRUNC must produce a vector from scalar operands",
1237 MI);
1238 for (unsigned i = 2; i < MI->getNumOperands(); ++i) {
1239 if (MRI->getType(MI->getOperand(1).getReg()) !=
1240 MRI->getType(MI->getOperand(i).getReg()))
1241 report("G_BUILD_VECTOR_TRUNC source operand types are not homogeneous",
1242 MI);
1243 }
1244 if (SrcEltTy.getSizeInBits() <= DstTy.getElementType().getSizeInBits())
1245 report("G_BUILD_VECTOR_TRUNC source operand types are not larger than "
1246 "dest elt type",
1247 MI);
1248 break;
1249 }
1250 case TargetOpcode::G_CONCAT_VECTORS: {
1251 // Source types should be vectors, and total size should match the dest
1252 // vector size.
1253 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1254 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1255 if (!DstTy.isVector() || !SrcTy.isVector())
1256 report("G_CONCAT_VECTOR requires vector source and destination operands",
1257 MI);
1258 for (unsigned i = 2; i < MI->getNumOperands(); ++i) {
1259 if (MRI->getType(MI->getOperand(1).getReg()) !=
1260 MRI->getType(MI->getOperand(i).getReg()))
1261 report("G_CONCAT_VECTOR source operand types are not homogeneous", MI);
1262 }
1263 if (DstTy.getNumElements() !=
1264 SrcTy.getNumElements() * (MI->getNumOperands() - 1))
1265 report("G_CONCAT_VECTOR num dest and source elements should match", MI);
1266 break;
1267 }
Matt Arsenault46f9c6c2019-02-04 23:29:11 +00001268 case TargetOpcode::G_ICMP:
1269 case TargetOpcode::G_FCMP: {
1270 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1271 LLT SrcTy = MRI->getType(MI->getOperand(2).getReg());
1272
1273 if ((DstTy.isVector() != SrcTy.isVector()) ||
1274 (DstTy.isVector() && DstTy.getNumElements() != SrcTy.getNumElements()))
1275 report("Generic vector icmp/fcmp must preserve number of lanes", MI);
1276
1277 break;
1278 }
Matt Arsenaultb2d24572019-02-11 22:12:43 +00001279 case TargetOpcode::G_EXTRACT: {
1280 const MachineOperand &SrcOp = MI->getOperand(1);
1281 if (!SrcOp.isReg()) {
1282 report("extract source must be a register", MI);
1283 break;
1284 }
1285
1286 const MachineOperand &OffsetOp = MI->getOperand(2);
1287 if (!OffsetOp.isImm()) {
1288 report("extract offset must be a constant", MI);
1289 break;
1290 }
1291
1292 unsigned DstSize = MRI->getType(MI->getOperand(0).getReg()).getSizeInBits();
1293 unsigned SrcSize = MRI->getType(SrcOp.getReg()).getSizeInBits();
1294 if (SrcSize == DstSize)
1295 report("extract source must be larger than result", MI);
1296
1297 if (DstSize + OffsetOp.getImm() > SrcSize)
1298 report("extract reads past end of register", MI);
1299 break;
1300 }
Matt Arsenault26760142019-02-19 16:10:16 +00001301 case TargetOpcode::G_INSERT: {
1302 const MachineOperand &SrcOp = MI->getOperand(2);
1303 if (!SrcOp.isReg()) {
1304 report("insert source must be a register", MI);
1305 break;
1306 }
1307
1308 const MachineOperand &OffsetOp = MI->getOperand(3);
1309 if (!OffsetOp.isImm()) {
1310 report("insert offset must be a constant", MI);
1311 break;
1312 }
1313
1314 unsigned DstSize = MRI->getType(MI->getOperand(0).getReg()).getSizeInBits();
1315 unsigned SrcSize = MRI->getType(SrcOp.getReg()).getSizeInBits();
1316
1317 if (DstSize <= SrcSize)
1318 report("inserted size must be smaller than total register", MI);
1319
1320 if (SrcSize + OffsetOp.getImm() > DstSize)
1321 report("insert writes past end of register", MI);
1322
1323 break;
1324 }
Amara Emersond133c152019-06-11 19:58:06 +00001325 case TargetOpcode::G_JUMP_TABLE: {
1326 if (!MI->getOperand(1).isJTI())
1327 report("G_JUMP_TABLE source operand must be a jump table index", MI);
1328 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1329 if (!DstTy.isPointer())
1330 report("G_JUMP_TABLE dest operand must have a pointer type", MI);
1331 break;
1332 }
Amara Emersonf79d3bc2019-06-14 17:55:48 +00001333 case TargetOpcode::G_BRJT: {
1334 if (!MRI->getType(MI->getOperand(0).getReg()).isPointer())
1335 report("G_BRJT src operand 0 must be a pointer type", MI);
1336
1337 if (!MI->getOperand(1).isJTI())
1338 report("G_BRJT src operand 1 must be a jump table index", MI);
1339
1340 const auto &IdxOp = MI->getOperand(2);
1341 if (!IdxOp.isReg() || MRI->getType(IdxOp.getReg()).isPointer())
1342 report("G_BRJT src operand 2 must be a scalar reg type", MI);
1343 break;
1344 }
Matt Arsenaulta7f09f32019-06-17 17:01:32 +00001345 case TargetOpcode::G_INTRINSIC:
1346 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS: {
1347 // TODO: Should verify number of def and use operands, but the current
1348 // interface requires passing in IR types for mangling.
1349 const MachineOperand &IntrIDOp = MI->getOperand(MI->getNumExplicitDefs());
1350 if (!IntrIDOp.isIntrinsicID()) {
1351 report("G_INTRINSIC first src operand must be an intrinsic ID", MI);
1352 break;
1353 }
1354
1355 bool NoSideEffects = MI->getOpcode() == TargetOpcode::G_INTRINSIC;
1356 unsigned IntrID = IntrIDOp.getIntrinsicID();
1357 if (IntrID != 0 && IntrID < Intrinsic::num_intrinsics) {
1358 AttributeList Attrs
1359 = Intrinsic::getAttributes(MF->getFunction().getContext(),
1360 static_cast<Intrinsic::ID>(IntrID));
1361 bool DeclHasSideEffects = !Attrs.hasFnAttribute(Attribute::ReadNone);
1362 if (NoSideEffects && DeclHasSideEffects) {
1363 report("G_INTRINSIC used with intrinsic that accesses memory", MI);
1364 break;
1365 }
1366 if (!NoSideEffects && !DeclHasSideEffects) {
1367 report("G_INTRINSIC_W_SIDE_EFFECTS used with readnone intrinsic", MI);
1368 break;
1369 }
1370 }
1371
1372 break;
1373 }
Matt Arsenault46f9c6c2019-02-04 23:29:11 +00001374 default:
1375 break;
1376 }
1377}
1378
1379void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
1380 const MCInstrDesc &MCID = MI->getDesc();
1381 if (MI->getNumOperands() < MCID.getNumOperands()) {
1382 report("Too few operands", MI);
1383 errs() << MCID.getNumOperands() << " operands expected, but "
1384 << MI->getNumOperands() << " given.\n";
1385 }
1386
1387 if (MI->isPHI()) {
1388 if (MF->getProperties().hasProperty(
1389 MachineFunctionProperties::Property::NoPHIs))
1390 report("Found PHI instruction with NoPHIs property set", MI);
1391
1392 if (FirstNonPHI)
1393 report("Found PHI instruction after non-PHI", MI);
1394 } else if (FirstNonPHI == nullptr)
1395 FirstNonPHI = MI;
1396
1397 // Check the tied operands.
1398 if (MI->isInlineAsm())
1399 verifyInlineAsm(MI);
1400
1401 // Check the MachineMemOperands for basic consistency.
1402 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
1403 E = MI->memoperands_end();
1404 I != E; ++I) {
1405 if ((*I)->isLoad() && !MI->mayLoad())
1406 report("Missing mayLoad flag", MI);
1407 if ((*I)->isStore() && !MI->mayStore())
1408 report("Missing mayStore flag", MI);
1409 }
1410
1411 // Debug values must not have a slot index.
1412 // Other instructions must have one, unless they are inside a bundle.
1413 if (LiveInts) {
1414 bool mapped = !LiveInts->isNotInMIMap(*MI);
1415 if (MI->isDebugInstr()) {
1416 if (mapped)
1417 report("Debug instruction has a slot index", MI);
1418 } else if (MI->isInsideBundle()) {
1419 if (mapped)
1420 report("Instruction inside bundle has a slot index", MI);
1421 } else {
1422 if (!mapped)
1423 report("Missing slot index", MI);
1424 }
1425 }
1426
1427 if (isPreISelGenericOpcode(MCID.getOpcode())) {
1428 verifyPreISelGenericInstruction(MI);
1429 return;
1430 }
1431
1432 StringRef ErrorInfo;
1433 if (!TII->verifyInstruction(*MI, ErrorInfo))
1434 report(ErrorInfo.data(), MI);
1435
1436 // Verify properties of various specific instruction types
1437 switch (MI->getOpcode()) {
Aditya Nandakumarb14fd262018-02-09 01:27:23 +00001438 case TargetOpcode::COPY: {
1439 if (foundErrors)
1440 break;
1441 const MachineOperand &DstOp = MI->getOperand(0);
1442 const MachineOperand &SrcOp = MI->getOperand(1);
1443 LLT DstTy = MRI->getType(DstOp.getReg());
1444 LLT SrcTy = MRI->getType(SrcOp.getReg());
1445 if (SrcTy.isValid() && DstTy.isValid()) {
1446 // If both types are valid, check that the types are the same.
1447 if (SrcTy != DstTy) {
1448 report("Copy Instruction is illegal with mismatching types", MI);
1449 errs() << "Def = " << DstTy << ", Src = " << SrcTy << "\n";
1450 }
1451 }
1452 if (SrcTy.isValid() || DstTy.isValid()) {
1453 // If one of them have valid types, let's just check they have the same
1454 // size.
1455 unsigned SrcSize = TRI->getRegSizeInBits(SrcOp.getReg(), *MRI);
1456 unsigned DstSize = TRI->getRegSizeInBits(DstOp.getReg(), *MRI);
1457 assert(SrcSize && "Expecting size here");
1458 assert(DstSize && "Expecting size here");
1459 if (SrcSize != DstSize)
1460 if (!DstOp.getSubReg() && !SrcOp.getSubReg()) {
1461 report("Copy Instruction is illegal with mismatching sizes", MI);
1462 errs() << "Def Size = " << DstSize << ", Src Size = " << SrcSize
1463 << "\n";
1464 }
1465 }
1466 break;
1467 }
Philip Reames94cc4a22017-06-02 16:36:37 +00001468 case TargetOpcode::STATEPOINT:
1469 if (!MI->getOperand(StatepointOpers::IDPos).isImm() ||
1470 !MI->getOperand(StatepointOpers::NBytesPos).isImm() ||
1471 !MI->getOperand(StatepointOpers::NCallArgsPos).isImm())
1472 report("meta operands to STATEPOINT not constant!", MI);
1473 break;
Philip Reames0f02bbc2017-06-02 17:02:33 +00001474
1475 auto VerifyStackMapConstant = [&](unsigned Offset) {
1476 if (!MI->getOperand(Offset).isImm() ||
Fangrui Songf78650a2018-07-30 19:41:25 +00001477 MI->getOperand(Offset).getImm() != StackMaps::ConstantOp ||
1478 !MI->getOperand(Offset + 1).isImm())
Philip Reames0f02bbc2017-06-02 17:02:33 +00001479 report("stack map constant to STATEPOINT not well formed!", MI);
1480 };
1481 const unsigned VarStart = StatepointOpers(MI).getVarIdx();
1482 VerifyStackMapConstant(VarStart + StatepointOpers::CCOffset);
1483 VerifyStackMapConstant(VarStart + StatepointOpers::FlagsOffset);
1484 VerifyStackMapConstant(VarStart + StatepointOpers::NumDeoptOperandsOffset);
1485
1486 // TODO: verify we have properly encoded deopt arguments
Matt Arsenault46f9c6c2019-02-04 23:29:11 +00001487 break;
1488 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001489}
1490
1491void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00001492MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001493 const MachineInstr *MI = MO->getParent();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001494 const MCInstrDesc &MCID = MI->getDesc();
Alex Lorenze5101e22015-08-10 21:47:36 +00001495 unsigned NumDefs = MCID.getNumDefs();
1496 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
1497 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +00001498
Evan Cheng6cc775f2011-06-28 19:10:37 +00001499 // The first MCID.NumDefs operands must be explicit register defines
Alex Lorenze5101e22015-08-10 21:47:36 +00001500 if (MONum < NumDefs) {
Richard Smith8f3447c2012-08-15 01:39:31 +00001501 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +00001502 if (!MO->isReg())
1503 report("Explicit definition must be a register", MO, MONum);
Evan Cheng76f6e262012-05-29 19:40:44 +00001504 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +00001505 report("Explicit definition marked as use", MO, MONum);
1506 else if (MO->isImplicit())
1507 report("Explicit definition marked as implicit", MO, MONum);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001508 } else if (MONum < MCID.getNumOperands()) {
Richard Smith8f3447c2012-08-15 01:39:31 +00001509 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopherbcc230a72010-11-17 00:55:36 +00001510 // Don't check if it's the last operand in a variadic instruction. See,
1511 // e.g., LDM_RET in the arm back end.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001512 if (MO->isReg() &&
Evan Cheng7f8e5632011-12-07 07:15:52 +00001513 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001514 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braun6a57acf2013-10-04 16:53:00 +00001515 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +00001516 if (MO->isImplicit())
1517 report("Explicit operand marked as implicit", MO, MONum);
1518 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +00001519
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +00001520 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
1521 if (TiedTo != -1) {
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +00001522 if (!MO->isReg())
1523 report("Tied use must be a register", MO, MONum);
1524 else if (!MO->isTied())
1525 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +00001526 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
1527 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Mikael Holmen9c3e2ea2017-07-06 13:18:21 +00001528 else if (TargetRegisterInfo::isPhysicalRegister(MO->getReg())) {
1529 const MachineOperand &MOTied = MI->getOperand(TiedTo);
1530 if (!MOTied.isReg())
1531 report("Tied counterpart must be a register", &MOTied, TiedTo);
1532 else if (TargetRegisterInfo::isPhysicalRegister(MOTied.getReg()) &&
1533 MO->getReg() != MOTied.getReg())
1534 report("Tied physical registers must match.", &MOTied, TiedTo);
1535 }
Jakob Stoklund Olesendbbff782012-08-29 00:38:03 +00001536 } else if (MO->isReg() && MO->isTied())
1537 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +00001538 } else {
Jakob Stoklund Olesen3db495232009-12-22 21:48:20 +00001539 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001540 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen75b9c272009-09-23 20:57:55 +00001541 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesene61c7a32009-05-16 07:25:20 +00001542 }
1543
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001544 switch (MO->getType()) {
1545 case MachineOperand::MO_Register: {
1546 const unsigned Reg = MO->getReg();
1547 if (!Reg)
1548 return;
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001549 if (MRI->tracksLiveness() && !MI->isDebugValue())
1550 checkLiveness(MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001551
Jakob Stoklund Olesenc7579cd2012-09-04 18:38:28 +00001552 // Verify the consistency of tied operands.
1553 if (MO->isTied()) {
1554 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
1555 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
1556 if (!OtherMO.isReg())
1557 report("Must be tied to a register", MO, MONum);
1558 if (!OtherMO.isTied())
1559 report("Missing tie flags on tied operand", MO, MONum);
1560 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
1561 report("Inconsistent tie links", MO, MONum);
1562 if (MONum < MCID.getNumDefs()) {
1563 if (OtherIdx < MCID.getNumOperands()) {
1564 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
1565 report("Explicit def tied to explicit use without tie constraint",
1566 MO, MONum);
1567 } else {
1568 if (!OtherMO.isImplicit())
1569 report("Explicit def should be tied to implicit use", MO, MONum);
1570 }
1571 }
1572 }
1573
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001574 // Verify two-address constraints after leaving SSA form.
1575 unsigned DefIdx;
1576 if (!MRI->isSSA() && MO->isUse() &&
1577 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
1578 Reg != MI->getOperand(DefIdx).getReg())
1579 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001580
1581 // Check register classes.
Matthias Brauneca98582017-11-28 03:54:20 +00001582 unsigned SubIdx = MO->getSubReg();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001583
Matthias Brauneca98582017-11-28 03:54:20 +00001584 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1585 if (SubIdx) {
1586 report("Illegal subregister index for physical register", MO, MONum);
1587 return;
1588 }
1589 if (MONum < MCID.getNumOperands()) {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001590 if (const TargetRegisterClass *DRC =
1591 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001592 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001593 report("Illegal physical register for instruction", MO, MONum);
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001594 errs() << printReg(Reg, TRI) << " is not a "
1595 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001596 }
1597 }
Matthias Brauneca98582017-11-28 03:54:20 +00001598 }
Geoff Berryd1be9112018-01-29 18:57:07 +00001599 if (MO->isRenamable()) {
Geoff Berryf8bf2ec2018-02-23 18:25:08 +00001600 if (MRI->isReserved(Reg)) {
Geoff Berryd1be9112018-01-29 18:57:07 +00001601 report("isRenamable set on reserved register", MO, MONum);
Geoff Berryf8bf2ec2018-02-23 18:25:08 +00001602 return;
1603 }
Geoff Berry60c43102017-12-12 17:53:59 +00001604 }
Mikael Holmen42f7bc92018-06-21 10:03:34 +00001605 if (MI->isDebugValue() && MO->isUse() && !MO->isDebug()) {
1606 report("Use-reg is not IsDebug in a DBG_VALUE", MO, MONum);
1607 return;
1608 }
Matthias Brauneca98582017-11-28 03:54:20 +00001609 } else {
1610 // Virtual register.
1611 const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
1612 if (!RC) {
1613 // This is a generic virtual register.
Ahmed Bougachab14e9442016-08-02 16:49:22 +00001614
Matthias Brauneca98582017-11-28 03:54:20 +00001615 // If we're post-Select, we can't have gvregs anymore.
1616 if (isFunctionSelected) {
1617 report("Generic virtual register invalid in a Selected function",
1618 MO, MONum);
1619 return;
Quentin Colombetc1c94bc2016-04-08 16:35:22 +00001620 }
Matthias Brauneca98582017-11-28 03:54:20 +00001621
1622 // The gvreg must have a type and it must not have a SubIdx.
1623 LLT Ty = MRI->getType(Reg);
1624 if (!Ty.isValid()) {
1625 report("Generic virtual register must have a valid type", MO,
1626 MONum);
1627 return;
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001628 }
Matthias Brauneca98582017-11-28 03:54:20 +00001629
1630 const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
1631
1632 // If we're post-RegBankSelect, the gvreg must have a bank.
1633 if (!RegBank && isFunctionRegBankSelected) {
1634 report("Generic virtual register must have a bank in a "
1635 "RegBankSelected function",
1636 MO, MONum);
1637 return;
1638 }
1639
1640 // Make sure the register fits into its register bank if any.
1641 if (RegBank && Ty.isValid() &&
1642 RegBank->getSize() < Ty.getSizeInBits()) {
1643 report("Register bank is too small for virtual register", MO,
1644 MONum);
1645 errs() << "Register bank " << RegBank->getName() << " too small("
1646 << RegBank->getSize() << ") to fit " << Ty.getSizeInBits()
1647 << "-bits\n";
1648 return;
1649 }
1650 if (SubIdx) {
Matt Arsenault2bf74ec2019-02-05 00:53:22 +00001651 report("Generic virtual register does not allow subregister index", MO,
Matthias Brauneca98582017-11-28 03:54:20 +00001652 MONum);
1653 return;
1654 }
1655
1656 // If this is a target specific instruction and this operand
1657 // has register class constraint, the virtual register must
1658 // comply to it.
1659 if (!isPreISelGenericOpcode(MCID.getOpcode()) &&
1660 MONum < MCID.getNumOperands() &&
1661 TII->getRegClass(MCID, MONum, TRI, *MF)) {
1662 report("Virtual register does not match instruction constraint", MO,
1663 MONum);
1664 errs() << "Expect register class "
1665 << TRI->getRegClassName(
1666 TII->getRegClass(MCID, MONum, TRI, *MF))
1667 << " but got nothing\n";
1668 return;
1669 }
1670
1671 break;
1672 }
1673 if (SubIdx) {
1674 const TargetRegisterClass *SRC =
1675 TRI->getSubClassWithSubReg(RC, SubIdx);
1676 if (!SRC) {
1677 report("Invalid subregister index for virtual register", MO, MONum);
1678 errs() << "Register class " << TRI->getRegClassName(RC)
1679 << " does not support subreg index " << SubIdx << "\n";
1680 return;
1681 }
1682 if (RC != SRC) {
1683 report("Invalid register class for subregister index", MO, MONum);
1684 errs() << "Register class " << TRI->getRegClassName(RC)
1685 << " does not fully support subreg index " << SubIdx << "\n";
1686 return;
1687 }
1688 }
1689 if (MONum < MCID.getNumOperands()) {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001690 if (const TargetRegisterClass *DRC =
1691 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001692 if (SubIdx) {
1693 const TargetRegisterClass *SuperRC =
Eric Christopher433c4322015-03-10 23:46:01 +00001694 TRI->getLargestLegalSuperClass(RC, *MF);
Jakob Stoklund Oleseneb38bd8c2011-10-05 22:12:57 +00001695 if (!SuperRC) {
1696 report("No largest legal super class exists.", MO, MONum);
1697 return;
1698 }
1699 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
1700 if (!DRC) {
1701 report("No matching super-reg register class.", MO, MONum);
1702 return;
1703 }
1704 }
Jakob Stoklund Olesenaff10602011-06-02 05:43:46 +00001705 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001706 report("Illegal virtual register for instruction", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001707 errs() << "Expected a " << TRI->getRegClassName(DRC)
Craig Toppercf0444b2014-11-17 05:50:14 +00001708 << " register, but got a " << TRI->getRegClassName(RC)
1709 << " register\n";
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001710 }
1711 }
1712 }
1713 }
1714 break;
1715 }
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001716
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00001717 case MachineOperand::MO_RegisterMask:
1718 regMasks.push_back(MO->getRegMask());
1719 break;
1720
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001721 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerb06015a2010-02-09 19:54:29 +00001722 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
1723 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesenf6eb7d82009-09-21 07:19:08 +00001724 break;
1725
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001726 case MachineOperand::MO_FrameIndex:
1727 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001728 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
Jonas Paulsson72640f12015-10-29 08:28:35 +00001729 int FI = MO->getIndex();
1730 LiveInterval &LI = LiveStks->getInterval(FI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001731 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001732
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001733 bool stores = MI->mayStore();
Jonas Paulsson72640f12015-10-29 08:28:35 +00001734 bool loads = MI->mayLoad();
1735 // For a memory-to-memory move, we need to check if the frame
1736 // index is used for storing or loading, by inspecting the
1737 // memory operands.
1738 if (stores && loads) {
1739 for (auto *MMO : MI->memoperands()) {
1740 const PseudoSourceValue *PSV = MMO->getPseudoValue();
1741 if (PSV == nullptr) continue;
1742 const FixedStackPseudoSourceValue *Value =
1743 dyn_cast<FixedStackPseudoSourceValue>(PSV);
1744 if (Value == nullptr) continue;
1745 if (Value->getFrameIndex() != FI) continue;
1746
1747 if (MMO->isStore())
1748 loads = false;
1749 else
1750 stores = false;
1751 break;
1752 }
1753 if (loads == stores)
1754 report("Missing fixed stack memoperand.", MI);
1755 }
1756 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001757 report("Instruction loads from dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001758 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001759 }
Jonas Paulsson17ad0452015-10-21 07:39:47 +00001760 if (stores && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001761 report("Instruction stores to dead spill slot", MO, MONum);
Owen Anderson21b17882015-02-04 00:02:59 +00001762 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesen31fffb62010-11-01 19:49:52 +00001763 }
1764 }
1765 break;
1766
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00001767 default:
1768 break;
1769 }
1770}
1771
Matthias Braun1377fd62016-02-02 20:04:51 +00001772void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
1773 unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
1774 LaneBitmask LaneMask) {
1775 LiveQueryResult LRQ = LR.Query(UseIdx);
1776 // Check if we have a segment at the use, note however that we only need one
1777 // live subregister range, the others may be dead.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001778 if (!LRQ.valueIn() && LaneMask.none()) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001779 report("No live segment at use", MO, MONum);
1780 report_context_liverange(LR);
1781 report_context_vreg_regunit(VRegOrUnit);
1782 report_context(UseIdx);
1783 }
1784 if (MO->isKill() && !LRQ.isKill()) {
1785 report("Live range continues after kill flag", MO, MONum);
1786 report_context_liverange(LR);
1787 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001788 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001789 report_context_lanemask(LaneMask);
1790 report_context(UseIdx);
1791 }
1792}
1793
1794void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
1795 unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
Bjorn Petterssonb2154af2018-09-20 06:59:18 +00001796 bool SubRangeCheck, LaneBitmask LaneMask) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001797 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
1798 assert(VNI && "NULL valno is not allowed");
1799 if (VNI->def != DefIdx) {
1800 report("Inconsistent valno->def", MO, MONum);
1801 report_context_liverange(LR);
1802 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001803 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001804 report_context_lanemask(LaneMask);
1805 report_context(*VNI);
1806 report_context(DefIdx);
1807 }
1808 } else {
1809 report("No live segment at def", MO, MONum);
1810 report_context_liverange(LR);
1811 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001812 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001813 report_context_lanemask(LaneMask);
1814 report_context(DefIdx);
1815 }
1816 // Check that, if the dead def flag is present, LiveInts agree.
1817 if (MO->isDead()) {
1818 LiveQueryResult LRQ = LR.Query(DefIdx);
1819 if (!LRQ.isDeadDef()) {
Bjorn Petterssonb2154af2018-09-20 06:59:18 +00001820 assert(TargetRegisterInfo::isVirtualRegister(VRegOrUnit) &&
1821 "Expecting a virtual register.");
1822 // A dead subreg def only tells us that the specific subreg is dead. There
1823 // could be other non-dead defs of other subregs, or we could have other
1824 // parts of the register being live through the instruction. So unless we
1825 // are checking liveness for a subrange it is ok for the live range to
1826 // continue, given that we have a dead def of a subregister.
1827 if (SubRangeCheck || MO->getSubReg() == 0) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001828 report("Live range continues after dead def flag", MO, MONum);
1829 report_context_liverange(LR);
1830 report_context_vreg_regunit(VRegOrUnit);
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001831 if (LaneMask.any())
Matthias Braun1377fd62016-02-02 20:04:51 +00001832 report_context_lanemask(LaneMask);
1833 }
1834 }
1835 }
1836}
1837
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001838void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1839 const MachineInstr *MI = MO->getParent();
1840 const unsigned Reg = MO->getReg();
1841
1842 // Both use and def operands can read a register.
1843 if (MO->readsReg()) {
Jakob Stoklund Olesenc6fd3de2012-07-25 16:49:11 +00001844 if (MO->isKill())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001845 addRegWithSubRegs(regsKilled, Reg);
1846
1847 // Check that LiveVars knows this kill.
1848 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1849 MO->isKill()) {
1850 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
David Majnemer0d955d02016-08-11 22:21:41 +00001851 if (!is_contained(VI.Kills, MI))
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001852 report("Kill missing from LiveVariables", MO, MONum);
1853 }
1854
1855 // Check LiveInts liveness and kill.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001856 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1857 SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001858 // Check the cached regunit intervals.
1859 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1860 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Brauncebdb172017-09-01 18:36:26 +00001861 if (MRI->isReservedRegUnit(*Units))
1862 continue;
Matthias Braun1377fd62016-02-02 20:04:51 +00001863 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
1864 checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001865 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001866 }
1867
1868 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1869 if (LiveInts->hasInterval(Reg)) {
1870 // This is a virtual register interval.
1871 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun1377fd62016-02-02 20:04:51 +00001872 checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
1873
1874 if (LI.hasSubRanges() && !MO->isDef()) {
1875 unsigned SubRegIdx = MO->getSubReg();
1876 LaneBitmask MOMask = SubRegIdx != 0
1877 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1878 : MRI->getMaxLaneMaskForVReg(Reg);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001879 LaneBitmask LiveInMask;
Matthias Braun1377fd62016-02-02 20:04:51 +00001880 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001881 if ((MOMask & SR.LaneMask).none())
Matthias Braun1377fd62016-02-02 20:04:51 +00001882 continue;
1883 checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
1884 LiveQueryResult LRQ = SR.Query(UseIdx);
1885 if (LRQ.valueIn())
1886 LiveInMask |= SR.LaneMask;
1887 }
1888 // At least parts of the register has to be live at the use.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001889 if ((LiveInMask & MOMask).none()) {
Matthias Braun1377fd62016-02-02 20:04:51 +00001890 report("No live subrange at use", MO, MONum);
1891 report_context(LI);
1892 report_context(UseIdx);
1893 }
Jakob Stoklund Olesena766b472012-08-01 23:52:40 +00001894 }
1895 } else {
1896 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001897 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001898 }
1899 }
1900
1901 // Use of a dead register.
1902 if (!regsLive.count(Reg)) {
1903 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1904 // Reserved registers may be used even when 'dead'.
Matthias Braun96d77322014-12-10 01:13:13 +00001905 bool Bad = !isReserved(Reg);
1906 // We are fine if just any subregister has a defined value.
1907 if (Bad) {
1908 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1909 ++SubRegs) {
1910 if (regsLive.count(*SubRegs)) {
1911 Bad = false;
1912 break;
1913 }
1914 }
1915 }
Matthias Braun96a31952015-01-14 22:25:14 +00001916 // If there is an additional implicit-use of a super register we stop
1917 // here. By definition we are fine if the super register is not
1918 // (completely) dead, if the complete super register is dead we will
1919 // get a report for its operand.
1920 if (Bad) {
1921 for (const MachineOperand &MOP : MI->uses()) {
Matt Arsenault9eb3dda2018-08-27 17:40:09 +00001922 if (!MOP.isReg() || !MOP.isImplicit())
Matthias Braun96a31952015-01-14 22:25:14 +00001923 continue;
Matt Arsenault9eb3dda2018-08-27 17:40:09 +00001924
1925 if (!TargetRegisterInfo::isPhysicalRegister(MOP.getReg()))
Matthias Braun96a31952015-01-14 22:25:14 +00001926 continue;
Matt Arsenault9eb3dda2018-08-27 17:40:09 +00001927
Matthias Braun96a31952015-01-14 22:25:14 +00001928 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1929 ++SubRegs) {
1930 if (*SubRegs == Reg) {
1931 Bad = false;
1932 break;
1933 }
1934 }
1935 }
1936 }
Matthias Braun96d77322014-12-10 01:13:13 +00001937 if (Bad)
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001938 report("Using an undefined physical register", MO, MONum);
Pete Cooperdcf94db2012-07-19 23:40:38 +00001939 } else if (MRI->def_empty(Reg)) {
1940 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001941 } else {
1942 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1943 // We don't know which virtual registers are live in, so only complain
1944 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1945 // must be live in. PHI instructions are handled separately.
1946 if (MInfo.regsKilled.count(Reg))
1947 report("Using a killed virtual register", MO, MONum);
1948 else if (!MI->isPHI())
1949 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1950 }
1951 }
1952 }
1953
1954 if (MO->isDef()) {
1955 // Register defined.
1956 // TODO: verify that earlyclobber ops are not used.
1957 if (MO->isDead())
1958 addRegWithSubRegs(regsDead, Reg);
1959 else
1960 addRegWithSubRegs(regsDefined, Reg);
1961
1962 // Verify SSA form.
1963 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001964 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001965 report("Multiple virtual register defs in SSA form", MO, MONum);
1966
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001967 // Check LiveInts for a live segment, but only for virtual registers.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001968 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1969 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
Jakob Stoklund Olesenb033ded2012-06-22 22:23:58 +00001970 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Matthias Braun1377fd62016-02-02 20:04:51 +00001971
1972 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1973 if (LiveInts->hasInterval(Reg)) {
1974 const LiveInterval &LI = LiveInts->getInterval(Reg);
1975 checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
1976
1977 if (LI.hasSubRanges()) {
1978 unsigned SubRegIdx = MO->getSubReg();
1979 LaneBitmask MOMask = SubRegIdx != 0
1980 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1981 : MRI->getMaxLaneMaskForVReg(Reg);
1982 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001983 if ((SR.LaneMask & MOMask).none())
Matthias Braun1377fd62016-02-02 20:04:51 +00001984 continue;
Bjorn Petterssonb2154af2018-09-20 06:59:18 +00001985 checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, true, SR.LaneMask);
Matthias Braun1377fd62016-02-02 20:04:51 +00001986 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001987 }
1988 } else {
Matthias Braun1377fd62016-02-02 20:04:51 +00001989 report("Virtual register has no Live interval", MO, MONum);
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001990 }
Jakob Stoklund Olesenb21df322012-03-28 20:47:35 +00001991 }
1992 }
1993 }
1994}
1995
Eugene Zelenko32a40562017-09-11 23:00:48 +00001996void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {}
Jakob Stoklund Olesen00e7dff2012-06-06 22:34:30 +00001997
1998// This function gets called after visiting all instructions in a bundle. The
1999// argument points to the bundle header.
2000// Normal stand-alone instructions are also considered 'bundles', and this
2001// function is called for all of them.
2002void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002003 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
2004 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00002005 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen16c4a972012-02-28 01:42:41 +00002006 // Kill any masked registers.
2007 while (!regMasks.empty()) {
2008 const uint32_t *Mask = regMasks.pop_back_val();
2009 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
2010 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
2011 MachineOperand::clobbersPhysReg(Mask, *I))
2012 regsDead.push_back(*I);
2013 }
Jakob Stoklund Olesen45833552010-08-05 18:59:59 +00002014 set_subtract(regsLive, regsDead); regsDead.clear();
2015 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002016}
2017
2018void
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00002019MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002020 MBBInfoMap[MBB].regsLiveOut = regsLive;
2021 regsLive.clear();
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00002022
2023 if (Indexes) {
2024 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
2025 if (!(stop > lastIndex)) {
2026 report("Block ends before last instruction index", MBB);
Owen Anderson21b17882015-02-04 00:02:59 +00002027 errs() << "Block ends at " << stop
Jakob Stoklund Olesen58b6f4d2011-01-12 21:27:48 +00002028 << " last instruction was at " << lastIndex << '\n';
2029 }
2030 lastIndex = stop;
2031 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002032}
2033
2034// Calculate the largest possible vregsPassed sets. These are the registers that
2035// can pass through an MBB live, but may not be live every time. It is assumed
2036// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00002037void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002038 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
2039 // have any vregsPassed.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00002040 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00002041 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002042 BBInfo &MInfo = MBBInfoMap[&MBB];
2043 if (!MInfo.reachable)
2044 continue;
2045 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
2046 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
2047 BBInfo &SInfo = MBBInfoMap[*SuI];
2048 if (SInfo.addPassed(MInfo.regsLiveOut))
2049 todo.insert(*SuI);
2050 }
2051 }
2052
2053 // Iteratively push vregsPassed to successors. This will converge to the same
2054 // final state regardless of DenseSet iteration order.
2055 while (!todo.empty()) {
2056 const MachineBasicBlock *MBB = *todo.begin();
2057 todo.erase(MBB);
2058 BBInfo &MInfo = MBBInfoMap[MBB];
2059 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
2060 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
2061 if (*SuI == MBB)
2062 continue;
2063 BBInfo &SInfo = MBBInfoMap[*SuI];
2064 if (SInfo.addPassed(MInfo.vregsPassed))
2065 todo.insert(*SuI);
2066 }
2067 }
2068}
2069
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00002070// Calculate the set of virtual registers that must be passed through each basic
2071// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00002072// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00002073void MachineVerifier::calcRegsRequired() {
2074 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00002075 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Alexey Samsonov41b977d2014-04-30 18:29:51 +00002076 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00002077 BBInfo &MInfo = MBBInfoMap[&MBB];
2078 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
2079 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
2080 BBInfo &PInfo = MBBInfoMap[*PrI];
2081 if (PInfo.addRequired(MInfo.vregsLiveIn))
2082 todo.insert(*PrI);
2083 }
2084 }
2085
2086 // Iteratively push vregsRequired to predecessors. This will converge to the
2087 // same final state regardless of DenseSet iteration order.
2088 while (!todo.empty()) {
2089 const MachineBasicBlock *MBB = *todo.begin();
2090 todo.erase(MBB);
2091 BBInfo &MInfo = MBBInfoMap[MBB];
2092 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
2093 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
2094 if (*PrI == MBB)
2095 continue;
2096 BBInfo &SInfo = MBBInfoMap[*PrI];
2097 if (SInfo.addRequired(MInfo.vregsRequired))
2098 todo.insert(*PrI);
2099 }
2100 }
2101}
2102
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002103// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00002104// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Matthias Brauna6d53742017-11-28 03:54:19 +00002105void MachineVerifier::checkPHIOps(const MachineBasicBlock &MBB) {
2106 BBInfo &MInfo = MBBInfoMap[&MBB];
2107
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00002108 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Matthias Brauna6d53742017-11-28 03:54:19 +00002109 for (const MachineInstr &Phi : MBB) {
2110 if (!Phi.isPHI())
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002111 break;
Jakob Stoklund Olesen6ea6a1442012-03-10 00:36:04 +00002112 seen.clear();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002113
Matthias Brauna6d53742017-11-28 03:54:19 +00002114 const MachineOperand &MODef = Phi.getOperand(0);
2115 if (!MODef.isReg() || !MODef.isDef()) {
2116 report("Expected first PHI operand to be a register def", &MODef, 0);
2117 continue;
2118 }
2119 if (MODef.isTied() || MODef.isImplicit() || MODef.isInternalRead() ||
2120 MODef.isEarlyClobber() || MODef.isDebug())
2121 report("Unexpected flag on PHI operand", &MODef, 0);
2122 unsigned DefReg = MODef.getReg();
2123 if (!TargetRegisterInfo::isVirtualRegister(DefReg))
2124 report("Expected first PHI operand to be a virtual register", &MODef, 0);
2125
2126 for (unsigned I = 1, E = Phi.getNumOperands(); I != E; I += 2) {
2127 const MachineOperand &MO0 = Phi.getOperand(I);
2128 if (!MO0.isReg()) {
2129 report("Expected PHI operand to be a register", &MO0, I);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002130 continue;
Matthias Brauna6d53742017-11-28 03:54:19 +00002131 }
2132 if (MO0.isImplicit() || MO0.isInternalRead() || MO0.isEarlyClobber() ||
2133 MO0.isDebug() || MO0.isTied())
2134 report("Unexpected flag on PHI operand", &MO0, I);
2135
2136 const MachineOperand &MO1 = Phi.getOperand(I + 1);
2137 if (!MO1.isMBB()) {
2138 report("Expected PHI operand to be a basic block", &MO1, I + 1);
2139 continue;
2140 }
2141
2142 const MachineBasicBlock &Pre = *MO1.getMBB();
2143 if (!Pre.isSuccessor(&MBB)) {
2144 report("PHI input is not a predecessor block", &MO1, I + 1);
2145 continue;
2146 }
2147
2148 if (MInfo.reachable) {
2149 seen.insert(&Pre);
2150 BBInfo &PrInfo = MBBInfoMap[&Pre];
Matthias Braun7eae2512017-12-04 18:57:48 +00002151 if (!MO0.isUndef() && PrInfo.reachable &&
2152 !PrInfo.isLiveOut(MO0.getReg()))
Matthias Brauna6d53742017-11-28 03:54:19 +00002153 report("PHI operand is not live-out from predecessor", &MO0, I);
2154 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002155 }
2156
2157 // Did we see all predecessors?
Matthias Brauna6d53742017-11-28 03:54:19 +00002158 if (MInfo.reachable) {
2159 for (MachineBasicBlock *Pred : MBB.predecessors()) {
2160 if (!seen.count(Pred)) {
2161 report("Missing PHI operand", &Phi);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00002162 errs() << printMBBReference(*Pred)
2163 << " is a predecessor according to the CFG.\n";
Matthias Brauna6d53742017-11-28 03:54:19 +00002164 }
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002165 }
2166 }
2167 }
2168}
2169
Jakob Stoklund Olesen63c733f2009-10-04 18:18:39 +00002170void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesen4cb77022010-01-05 20:59:36 +00002171 calcRegsPassed();
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002172
Matthias Brauna6d53742017-11-28 03:54:19 +00002173 for (const MachineBasicBlock &MBB : *MF)
2174 checkPHIOps(MBB);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00002175
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002176 // Now check liveness info if available
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00002177 calcRegsRequired();
2178
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00002179 // Check for killed virtual registers that should be live out.
Alexey Samsonov41b977d2014-04-30 18:29:51 +00002180 for (const auto &MBB : *MF) {
2181 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00002182 for (RegSet::iterator
2183 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
2184 ++I)
2185 if (MInfo.regsKilled.count(*I)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00002186 report("Virtual register killed in block, but needed live out.", &MBB);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00002187 errs() << "Virtual register " << printReg(*I)
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00002188 << " is used after the block.\n";
Jakob Stoklund Olesenda9ea1d2012-06-29 21:00:00 +00002189 }
2190 }
2191
Jakob Stoklund Olesena57fc122012-06-25 18:18:27 +00002192 if (!MF->empty()) {
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00002193 BBInfo &MInfo = MBBInfoMap[&MF->front()];
2194 for (RegSet::iterator
2195 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Matthias Braun30668dd2016-05-11 21:31:39 +00002196 ++I) {
2197 report("Virtual register defs don't dominate all uses.", MF);
2198 report_context_vreg(*I);
2199 }
Jakob Stoklund Olesen9f3e5742012-03-10 00:36:06 +00002200 }
2201
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002202 if (LiveVars)
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00002203 verifyLiveVariables();
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002204 if (LiveInts)
2205 verifyLiveIntervals();
Djordje Todorovica7cde102019-06-27 07:48:06 +00002206
2207 for (auto CSInfo : MF->getCallSitesInfo())
2208 if (!CSInfo.first->isCall())
2209 report("Call site info referencing instruction that is not call", MF);
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +00002210}
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00002211
2212void MachineVerifier::verifyLiveVariables() {
2213 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen6ff70ad32011-01-08 23:11:02 +00002214 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
2215 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00002216 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Alexey Samsonov41b977d2014-04-30 18:29:51 +00002217 for (const auto &MBB : *MF) {
2218 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00002219
2220 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
2221 if (MInfo.vregsRequired.count(Reg)) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00002222 if (!VI.AliveBlocks.test(MBB.getNumber())) {
2223 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00002224 errs() << "Virtual register " << printReg(Reg)
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00002225 << " must be live through the block.\n";
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00002226 }
2227 } else {
Alexey Samsonov41b977d2014-04-30 18:29:51 +00002228 if (VI.AliveBlocks.test(MBB.getNumber())) {
2229 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00002230 errs() << "Virtual register " << printReg(Reg)
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00002231 << " is not needed live through the block.\n";
Jakob Stoklund Olesen9cbffd22009-11-18 20:36:57 +00002232 }
2233 }
2234 }
2235 }
2236}
2237
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002238void MachineVerifier::verifyLiveIntervals() {
2239 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00002240 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
2241 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00002242
2243 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00002244 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen1a065e42010-10-06 23:54:35 +00002245 continue;
2246
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00002247 if (!LiveInts->hasInterval(Reg)) {
2248 report("Missing live interval for virtual register", MF);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00002249 errs() << printReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00002250 continue;
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00002251 }
Jakob Stoklund Olesendc5e7062010-10-28 20:44:22 +00002252
Jakob Stoklund Olesen781e0b92012-06-20 23:23:59 +00002253 const LiveInterval &LI = LiveInts->getInterval(Reg);
2254 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002255 verifyLiveInterval(LI);
2256 }
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00002257
2258 // Verify all the cached regunit intervals.
2259 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun34e1be92013-10-10 21:29:02 +00002260 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
2261 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002262}
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002263
Matthias Braun364e6e92013-10-10 21:28:54 +00002264void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002265 const VNInfo *VNI, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00002266 LaneBitmask LaneMask) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002267 if (VNI->isUnused())
2268 return;
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002269
Matthias Braun364e6e92013-10-10 21:28:54 +00002270 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002271
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002272 if (!DefVNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002273 report("Value not live at VNInfo def and not marked unused", MF);
2274 report_context(LR, Reg, LaneMask);
2275 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002276 return;
2277 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002278
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002279 if (DefVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002280 report("Live segment at def has different VNInfo", MF);
2281 report_context(LR, Reg, LaneMask);
2282 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002283 return;
2284 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002285
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002286 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
2287 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002288 report("Invalid VNInfo definition index", MF);
2289 report_context(LR, Reg, LaneMask);
2290 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002291 return;
2292 }
Jakob Stoklund Olesen0fb303d2010-10-22 22:48:58 +00002293
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002294 if (VNI->isPHIDef()) {
2295 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002296 report("PHIDef VNInfo is not defined at MBB start", MBB);
2297 report_context(LR, Reg, LaneMask);
2298 report_context(*VNI);
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002299 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002300 return;
2301 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002302
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002303 // Non-PHI def.
2304 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
2305 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002306 report("No instruction at VNInfo def index", MBB);
2307 report_context(LR, Reg, LaneMask);
2308 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002309 return;
2310 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002311
Matthias Braun364e6e92013-10-10 21:28:54 +00002312 if (Reg != 0) {
2313 bool hasDef = false;
2314 bool isEarlyClobber = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00002315 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00002316 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002317 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00002318 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
2319 if (MOI->getReg() != Reg)
2320 continue;
2321 } else {
2322 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
2323 !TRI->hasRegUnit(MOI->getReg(), Reg))
2324 continue;
2325 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00002326 if (LaneMask.any() &&
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00002327 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask).none())
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002328 continue;
Matthias Braun364e6e92013-10-10 21:28:54 +00002329 hasDef = true;
2330 if (MOI->isEarlyClobber())
2331 isEarlyClobber = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002332 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002333
Matthias Braun364e6e92013-10-10 21:28:54 +00002334 if (!hasDef) {
2335 report("Defining instruction does not modify register", MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00002336 report_context(LR, Reg, LaneMask);
2337 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00002338 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002339
Matthias Braun364e6e92013-10-10 21:28:54 +00002340 // Early clobber defs begin at USE slots, but other defs must begin at
2341 // DEF slots.
2342 if (isEarlyClobber) {
2343 if (!VNI->def.isEarlyClobber()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002344 report("Early clobber def must be at an early-clobber slot", MBB);
2345 report_context(LR, Reg, LaneMask);
2346 report_context(*VNI);
Matthias Braun364e6e92013-10-10 21:28:54 +00002347 }
2348 } else if (!VNI->def.isRegister()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002349 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
2350 report_context(LR, Reg, LaneMask);
2351 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002352 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002353 }
2354}
2355
Matthias Braun364e6e92013-10-10 21:28:54 +00002356void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
2357 const LiveRange::const_iterator I,
Matthias Braune6a24852015-09-25 21:51:14 +00002358 unsigned Reg, LaneBitmask LaneMask)
2359{
Matthias Braun364e6e92013-10-10 21:28:54 +00002360 const LiveRange::Segment &S = *I;
2361 const VNInfo *VNI = S.valno;
Matthias Braun13ddb7c2013-10-10 21:28:43 +00002362 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002363
Matthias Braun364e6e92013-10-10 21:28:54 +00002364 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002365 report("Foreign valno in live segment", MF);
2366 report_context(LR, Reg, LaneMask);
2367 report_context(S);
2368 report_context(*VNI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002369 }
2370
2371 if (VNI->isUnused()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002372 report("Live segment valno is marked unused", MF);
2373 report_context(LR, Reg, LaneMask);
2374 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002375 }
2376
Matthias Braun364e6e92013-10-10 21:28:54 +00002377 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002378 if (!MBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002379 report("Bad start of live segment, no basic block", MF);
2380 report_context(LR, Reg, LaneMask);
2381 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002382 return;
2383 }
2384 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Braun364e6e92013-10-10 21:28:54 +00002385 if (S.start != MBBStartIdx && S.start != VNI->def) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002386 report("Live segment must begin at MBB entry or valno def", MBB);
2387 report_context(LR, Reg, LaneMask);
2388 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002389 }
2390
2391 const MachineBasicBlock *EndMBB =
Matthias Braun364e6e92013-10-10 21:28:54 +00002392 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002393 if (!EndMBB) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002394 report("Bad end of live segment, no basic block", MF);
2395 report_context(LR, Reg, LaneMask);
2396 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002397 return;
2398 }
2399
2400 // No more checks for live-out segments.
Matthias Braun364e6e92013-10-10 21:28:54 +00002401 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002402 return;
2403
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00002404 // RegUnit intervals are allowed dead phis.
Matthias Braun364e6e92013-10-10 21:28:54 +00002405 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
2406 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen637c4672012-08-02 16:36:50 +00002407 return;
2408
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002409 // The live segment is ending inside EndMBB
2410 const MachineInstr *MI =
Matthias Braun364e6e92013-10-10 21:28:54 +00002411 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002412 if (!MI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002413 report("Live segment doesn't end at a valid instruction", EndMBB);
2414 report_context(LR, Reg, LaneMask);
2415 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002416 return;
2417 }
2418
2419 // The block slot must refer to a basic block boundary.
Matthias Braun364e6e92013-10-10 21:28:54 +00002420 if (S.end.isBlock()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002421 report("Live segment ends at B slot of an instruction", EndMBB);
2422 report_context(LR, Reg, LaneMask);
2423 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002424 }
2425
Matthias Braun364e6e92013-10-10 21:28:54 +00002426 if (S.end.isDead()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002427 // Segment ends on the dead slot.
2428 // That means there must be a dead def.
Matthias Braun364e6e92013-10-10 21:28:54 +00002429 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002430 report("Live segment ending at dead slot spans instructions", EndMBB);
2431 report_context(LR, Reg, LaneMask);
2432 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002433 }
2434 }
2435
2436 // A live segment can only end at an early-clobber slot if it is being
2437 // redefined by an early-clobber def.
Matthias Braun364e6e92013-10-10 21:28:54 +00002438 if (S.end.isEarlyClobber()) {
2439 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002440 report("Live segment ending at early clobber slot must be "
Matthias Braun7e624d52015-11-09 23:59:33 +00002441 "redefined by an EC def in the same instruction", EndMBB);
2442 report_context(LR, Reg, LaneMask);
2443 report_context(S);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002444 }
2445 }
2446
2447 // The following checks only apply to virtual registers. Physreg liveness
2448 // is too weird to check.
Matthias Braun364e6e92013-10-10 21:28:54 +00002449 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +00002450 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002451 // use, or a dead flag on a def.
2452 bool hasRead = false;
Matthias Braun21554d92014-12-10 01:13:11 +00002453 bool hasSubRegDef = false;
Matthias Braun72a58c32016-03-29 19:07:43 +00002454 bool hasDeadDef = false;
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00002455 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
Matthias Braun364e6e92013-10-10 21:28:54 +00002456 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002457 continue;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00002458 unsigned Sub = MOI->getSubReg();
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00002459 LaneBitmask SLM = Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub)
2460 : LaneBitmask::getAll();
Matthias Braun72a58c32016-03-29 19:07:43 +00002461 if (MOI->isDef()) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00002462 if (Sub != 0) {
Matthias Braun72a58c32016-03-29 19:07:43 +00002463 hasSubRegDef = true;
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00002464 // An operand %0:sub0 reads %0:sub1..n. Invert the lane
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00002465 // mask for subregister defs. Read-undef defs will be handled by
2466 // readsReg below.
Krzysztof Parzyszek0a955d62016-08-29 13:15:35 +00002467 SLM = ~SLM;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00002468 }
Matthias Braun72a58c32016-03-29 19:07:43 +00002469 if (MOI->isDead())
2470 hasDeadDef = true;
2471 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00002472 if (LaneMask.any() && (LaneMask & SLM).none())
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00002473 continue;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002474 if (MOI->readsReg())
2475 hasRead = true;
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002476 }
Matthias Braun72a58c32016-03-29 19:07:43 +00002477 if (S.end.isDead()) {
2478 // Make sure that the corresponding machine operand for a "dead" live
2479 // range has the dead flag. We cannot perform this check for subregister
2480 // liveranges as partially dead values are allowed.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00002481 if (LaneMask.none() && !hasDeadDef) {
Matthias Braun72a58c32016-03-29 19:07:43 +00002482 report("Instruction ending live segment on dead slot has no dead flag",
2483 MI);
2484 report_context(LR, Reg, LaneMask);
2485 report_context(S);
2486 }
2487 } else {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002488 if (!hasRead) {
Matthias Braun21554d92014-12-10 01:13:11 +00002489 // When tracking subregister liveness, the main range must start new
2490 // values on partial register writes, even if there is no read.
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00002491 if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask.any() ||
Matthias Brauna25e13a2015-03-19 00:21:58 +00002492 !hasSubRegDef) {
Matthias Braun21554d92014-12-10 01:13:11 +00002493 report("Instruction ending live segment doesn't read the register",
2494 MI);
Matthias Braun7e624d52015-11-09 23:59:33 +00002495 report_context(LR, Reg, LaneMask);
2496 report_context(S);
Matthias Braun21554d92014-12-10 01:13:11 +00002497 }
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002498 }
2499 }
2500 }
2501
2502 // Now check all the basic blocks in this live segment.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002503 MachineFunction::const_iterator MFI = MBB->getIterator();
Matthias Braun13ddb7c2013-10-10 21:28:43 +00002504 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Braun364e6e92013-10-10 21:28:54 +00002505 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002506 // Not live-in to any blocks.
2507 if (MBB == EndMBB)
2508 return;
2509 // Skip this block.
2510 ++MFI;
2511 }
Krzysztof Parzyszek9af86a52018-08-16 19:13:28 +00002512
2513 SmallVector<SlotIndex, 4> Undefs;
2514 if (LaneMask.any()) {
2515 LiveInterval &OwnerLI = LiveInts->getInterval(Reg);
2516 OwnerLI.computeSubRangeUndefs(Undefs, LaneMask, *MRI, *Indexes);
2517 }
2518
Eugene Zelenko32a40562017-09-11 23:00:48 +00002519 while (true) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002520 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002521 // We don't know how to track physregs into a landing pad.
Matthias Braun364e6e92013-10-10 21:28:54 +00002522 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Reid Kleckner0e288232015-08-27 23:27:47 +00002523 MFI->isEHPad()) {
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002524 if (&*MFI == EndMBB)
2525 break;
2526 ++MFI;
2527 continue;
2528 }
2529
2530 // Is VNI a PHI-def in the current block?
2531 bool IsPHI = VNI->isPHIDef() &&
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002532 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002533
2534 // Check that VNI is live-out of all predecessors.
2535 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
2536 PE = MFI->pred_end(); PI != PE; ++PI) {
2537 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Braun364e6e92013-10-10 21:28:54 +00002538 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002539
Matthias Braun1ee25e02017-06-08 21:30:54 +00002540 // All predecessors must have a live-out value. However for a phi
2541 // instruction with subregister intervals
2542 // only one of the subregisters (not necessarily the current one) needs to
2543 // be defined.
Krzysztof Parzyszek9af86a52018-08-16 19:13:28 +00002544 if (!PVNI && (LaneMask.none() || !IsPHI)) {
2545 if (LiveRangeCalc::isJointlyDominated(*PI, Undefs, *Indexes))
2546 continue;
Matthias Braun7e624d52015-11-09 23:59:33 +00002547 report("Register not marked live out of predecessor", *PI);
2548 report_context(LR, Reg, LaneMask);
2549 report_context(*VNI);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00002550 errs() << " live into " << printMBBReference(*MFI) << '@'
2551 << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002552 << PEnd << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002553 continue;
2554 }
2555
2556 // Only PHI-defs can take different predecessor values.
2557 if (!IsPHI && PVNI != VNI) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002558 report("Different value live out of predecessor", *PI);
2559 report_context(LR, Reg, LaneMask);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00002560 errs() << "Valno #" << PVNI->id << " live out of "
2561 << printMBBReference(*(*PI)) << '@' << PEnd << "\nValno #"
2562 << VNI->id << " live into " << printMBBReference(*MFI) << '@'
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +00002563 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002564 }
2565 }
2566 if (&*MFI == EndMBB)
2567 break;
2568 ++MFI;
2569 }
2570}
2571
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002572void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
Matthias Braune6a24852015-09-25 21:51:14 +00002573 LaneBitmask LaneMask) {
Matthias Braun96761952014-12-10 23:07:54 +00002574 for (const VNInfo *VNI : LR.valnos)
2575 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002576
Matthias Braun364e6e92013-10-10 21:28:54 +00002577 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002578 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Braun364e6e92013-10-10 21:28:54 +00002579}
2580
2581void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002582 unsigned Reg = LI.reg;
Matthias Braune962e522015-03-25 21:18:22 +00002583 assert(TargetRegisterInfo::isVirtualRegister(Reg));
2584 verifyLiveRange(LI, Reg);
2585
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00002586 LaneBitmask Mask;
Matthias Braune6a24852015-09-25 21:51:14 +00002587 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braune962e522015-03-25 21:18:22 +00002588 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00002589 if ((Mask & SR.LaneMask).any()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002590 report("Lane masks of sub ranges overlap in live interval", MF);
2591 report_context(LI);
2592 }
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00002593 if ((SR.LaneMask & ~MaxMask).any()) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002594 report("Subrange lanemask is invalid", MF);
2595 report_context(LI);
2596 }
2597 if (SR.empty()) {
2598 report("Subrange must not be empty", MF);
2599 report_context(SR, LI.reg, SR.LaneMask);
2600 }
Matthias Braune962e522015-03-25 21:18:22 +00002601 Mask |= SR.LaneMask;
2602 verifyLiveRange(SR, LI.reg, SR.LaneMask);
Matthias Braun7e624d52015-11-09 23:59:33 +00002603 if (!LI.covers(SR)) {
2604 report("A Subrange is not covered by the main range", MF);
2605 report_context(LI);
2606 }
Matthias Braun3f1d8fd2014-12-10 01:12:10 +00002607 }
2608
Jakob Stoklund Olesene736b972012-08-02 00:20:20 +00002609 // Check the LI only has one connected component.
Matthias Braune962e522015-03-25 21:18:22 +00002610 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
Matthias Braunbf47f632016-01-08 01:16:35 +00002611 unsigned NumComp = ConEQ.Classify(LI);
Matthias Braune962e522015-03-25 21:18:22 +00002612 if (NumComp > 1) {
Matthias Braun7e624d52015-11-09 23:59:33 +00002613 report("Multiple connected components in live interval", MF);
2614 report_context(LI);
Matthias Braune962e522015-03-25 21:18:22 +00002615 for (unsigned comp = 0; comp != NumComp; ++comp) {
2616 errs() << comp << ": valnos";
2617 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
2618 E = LI.vni_end(); I!=E; ++I)
2619 if (comp == ConEQ.getEqClass(*I))
2620 errs() << ' ' << (*I)->id;
2621 errs() << '\n';
Jakob Stoklund Olesen260fa282010-10-26 22:36:07 +00002622 }
Jakob Stoklund Olesen8147d7a2010-08-06 18:04:19 +00002623 }
2624}
Manman Renaa6875b2013-07-15 21:26:31 +00002625
2626namespace {
Eugene Zelenko32a40562017-09-11 23:00:48 +00002627
Manman Renaa6875b2013-07-15 21:26:31 +00002628 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
2629 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
2630 // value is zero.
2631 // We use a bool plus an integer to capture the stack state.
2632 struct StackStateOfBB {
Eugene Zelenko32a40562017-09-11 23:00:48 +00002633 StackStateOfBB() = default;
Manman Renaa6875b2013-07-15 21:26:31 +00002634 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
2635 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
Eugene Zelenko32a40562017-09-11 23:00:48 +00002636 ExitIsSetup(ExitSetup) {}
2637
Manman Renaa6875b2013-07-15 21:26:31 +00002638 // Can be negative, which means we are setting up a frame.
Eugene Zelenko32a40562017-09-11 23:00:48 +00002639 int EntryValue = 0;
2640 int ExitValue = 0;
2641 bool EntryIsSetup = false;
2642 bool ExitIsSetup = false;
Manman Renaa6875b2013-07-15 21:26:31 +00002643 };
Eugene Zelenko32a40562017-09-11 23:00:48 +00002644
2645} // end anonymous namespace
Manman Renaa6875b2013-07-15 21:26:31 +00002646
2647/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
2648/// by a FrameDestroy <n>, stack adjustments are identical on all
2649/// CFG edges to a merge point, and frame is destroyed at end of a return block.
2650void MachineVerifier::verifyStackFrame() {
Matthias Braunfa3872e2015-05-18 20:27:55 +00002651 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
2652 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
Serge Pavlov802aa662017-04-20 01:34:04 +00002653 if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
2654 return;
Manman Renaa6875b2013-07-15 21:26:31 +00002655
2656 SmallVector<StackStateOfBB, 8> SPState;
2657 SPState.resize(MF->getNumBlockIDs());
David Callahanc1051ab2016-10-05 21:36:16 +00002658 df_iterator_default_set<const MachineBasicBlock*> Reachable;
Manman Renaa6875b2013-07-15 21:26:31 +00002659
2660 // Visit the MBBs in DFS order.
Eugene Zelenko32a40562017-09-11 23:00:48 +00002661 for (df_ext_iterator<const MachineFunction *,
2662 df_iterator_default_set<const MachineBasicBlock *>>
Manman Renaa6875b2013-07-15 21:26:31 +00002663 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
2664 DFI != DFE; ++DFI) {
2665 const MachineBasicBlock *MBB = *DFI;
2666
2667 StackStateOfBB BBState;
2668 // Check the exit state of the DFS stack predecessor.
2669 if (DFI.getPathLength() >= 2) {
2670 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
2671 assert(Reachable.count(StackPred) &&
2672 "DFS stack predecessor is already visited.\n");
2673 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
2674 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
2675 BBState.ExitValue = BBState.EntryValue;
2676 BBState.ExitIsSetup = BBState.EntryIsSetup;
2677 }
2678
2679 // Update stack state by checking contents of MBB.
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002680 for (const auto &I : *MBB) {
2681 if (I.getOpcode() == FrameSetupOpcode) {
Manman Renaa6875b2013-07-15 21:26:31 +00002682 if (BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002683 report("FrameSetup is after another FrameSetup", &I);
Serge Pavlovd526b132017-05-09 13:35:13 +00002684 BBState.ExitValue -= TII->getFrameTotalSize(I);
Manman Renaa6875b2013-07-15 21:26:31 +00002685 BBState.ExitIsSetup = true;
2686 }
2687
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002688 if (I.getOpcode() == FrameDestroyOpcode) {
Serge Pavlovd526b132017-05-09 13:35:13 +00002689 int Size = TII->getFrameTotalSize(I);
Manman Renaa6875b2013-07-15 21:26:31 +00002690 if (!BBState.ExitIsSetup)
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002691 report("FrameDestroy is not after a FrameSetup", &I);
Manman Renaa6875b2013-07-15 21:26:31 +00002692 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
2693 BBState.ExitValue;
2694 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +00002695 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Owen Anderson21b17882015-02-04 00:02:59 +00002696 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Renaa6875b2013-07-15 21:26:31 +00002697 << AbsSPAdj << ">.\n";
2698 }
2699 BBState.ExitValue += Size;
2700 BBState.ExitIsSetup = false;
2701 }
2702 }
2703 SPState[MBB->getNumber()] = BBState;
2704
2705 // Make sure the exit state of any predecessor is consistent with the entry
2706 // state.
2707 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
2708 E = MBB->pred_end(); I != E; ++I) {
2709 if (Reachable.count(*I) &&
2710 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
2711 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
2712 report("The exit stack state of a predecessor is inconsistent.", MBB);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00002713 errs() << "Predecessor " << printMBBReference(*(*I))
2714 << " has exit state (" << SPState[(*I)->getNumber()].ExitValue
2715 << ", " << SPState[(*I)->getNumber()].ExitIsSetup << "), while "
2716 << printMBBReference(*MBB) << " has entry state ("
2717 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
Manman Renaa6875b2013-07-15 21:26:31 +00002718 }
2719 }
2720
2721 // Make sure the entry state of any successor is consistent with the exit
2722 // state.
2723 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
2724 E = MBB->succ_end(); I != E; ++I) {
2725 if (Reachable.count(*I) &&
2726 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
2727 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
2728 report("The entry stack state of a successor is inconsistent.", MBB);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00002729 errs() << "Successor " << printMBBReference(*(*I))
2730 << " has entry state (" << SPState[(*I)->getNumber()].EntryValue
2731 << ", " << SPState[(*I)->getNumber()].EntryIsSetup << "), while "
2732 << printMBBReference(*MBB) << " has exit state ("
2733 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
Manman Renaa6875b2013-07-15 21:26:31 +00002734 }
2735 }
2736
2737 // Make sure a basic block with return ends with zero stack adjustment.
2738 if (!MBB->empty() && MBB->back().isReturn()) {
2739 if (BBState.ExitIsSetup)
2740 report("A return block ends with a FrameSetup.", MBB);
2741 if (BBState.ExitValue)
2742 report("A return block ends with a nonzero stack adjustment.", MBB);
2743 }
2744 }
2745}