blob: bdb094f342d34545d5db0b88d5d5936db49e74b4 [file] [log] [blame]
Bill Wendling5567bb02010-08-19 18:52:17 +00001//===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Pass to verify generated machine code. The following is checked:
11//
12// Operand counts: All explicit operands must be present.
13//
14// Register classes: All physical and virtual register operands must be
15// compatible with the register class required by the instruction descriptor.
16//
17// Register live intervals: Registers must be defined only once, and must be
18// defined before use.
19//
20// The machine code verifier is enabled from LLVMTargetMachine.cpp with the
21// command-line option -verify-machineinstrs, or by defining the environment
22// variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
23// the verifier errors.
24//===----------------------------------------------------------------------===//
25
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000026#include "llvm/CodeGen/Passes.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000027#include "llvm/ADT/DenseSet.h"
Manman Ren7310b752013-07-15 21:26:31 +000028#include "llvm/ADT/DepthFirstIterator.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000029#include "llvm/ADT/SetOperations.h"
30#include "llvm/ADT/SmallVector.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/CodeGen/LiveIntervalAnalysis.h"
32#include "llvm/CodeGen/LiveStackAnalysis.h"
33#include "llvm/CodeGen/LiveVariables.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000036#include "llvm/CodeGen/MachineMemOperand.h"
37#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000038#include "llvm/IR/BasicBlock.h"
39#include "llvm/IR/InlineAsm.h"
40#include "llvm/IR/Instructions.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000041#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000042#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000043#include "llvm/Support/ErrorHandling.h"
Stephen Hinesdce4a402014-05-29 02:49:00 -070044#include "llvm/Support/FileSystem.h"
Stephen Hinesebe69fe2015-03-23 12:10:34 -070045#include "llvm/Support/Format.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000046#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000047#include "llvm/Target/TargetInstrInfo.h"
48#include "llvm/Target/TargetMachine.h"
49#include "llvm/Target/TargetRegisterInfo.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080050#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000051using namespace llvm;
52
53namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000054 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000055
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000056 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000057 PASS(pass),
Stephen Hinesebe69fe2015-03-23 12:10:34 -070058 Banner(b)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000059 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000060
61 bool runOnMachineFunction(MachineFunction &MF);
62
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000063 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000064 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000065 const MachineFunction *MF;
66 const TargetMachine *TM;
Evan Cheng15993f82011-06-27 21:26:13 +000067 const TargetInstrInfo *TII;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000068 const TargetRegisterInfo *TRI;
69 const MachineRegisterInfo *MRI;
70
71 unsigned foundErrors;
72
73 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000074 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000075 typedef DenseSet<unsigned> RegSet;
76 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +000077 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000078
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000079 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +000080 BlockSet FunctionBlocks;
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000081
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000082 BitVector regsReserved;
83 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000084 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000085 RegMaskVector regMasks;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000086 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000087
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000088 SlotIndex lastIndex;
89
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000090 // Add Reg and any sub-registers to RV
91 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
92 RV.push_back(Reg);
93 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +000094 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
95 RV.push_back(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000096 }
97
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000098 struct BBInfo {
99 // Is this MBB reachable from the MF entry point?
100 bool reachable;
101
102 // Vregs that must be live in because they are used without being
103 // defined. Map value is the user.
104 RegMap vregsLiveIn;
105
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000106 // Regs killed in MBB. They may be defined again, and will then be in both
107 // regsKilled and regsLiveOut.
108 RegSet regsKilled;
109
110 // Regs defined in MBB and live out. Note that vregs passing through may
111 // be live out without being mentioned here.
112 RegSet regsLiveOut;
113
114 // Vregs that pass through MBB untouched. This set is disjoint from
115 // regsKilled and regsLiveOut.
116 RegSet vregsPassed;
117
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000118 // Vregs that must pass through MBB because they are needed by a successor
119 // block. This set is disjoint from regsLiveOut.
120 RegSet vregsRequired;
121
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000122 // Set versions of block's predecessor and successor lists.
123 BlockSet Preds, Succs;
124
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000125 BBInfo() : reachable(false) {}
126
127 // Add register to vregsPassed if it belongs there. Return true if
128 // anything changed.
129 bool addPassed(unsigned Reg) {
130 if (!TargetRegisterInfo::isVirtualRegister(Reg))
131 return false;
132 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
133 return false;
134 return vregsPassed.insert(Reg).second;
135 }
136
137 // Same for a full set.
138 bool addPassed(const RegSet &RS) {
139 bool changed = false;
140 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
141 if (addPassed(*I))
142 changed = true;
143 return changed;
144 }
145
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000146 // Add register to vregsRequired if it belongs there. Return true if
147 // anything changed.
148 bool addRequired(unsigned Reg) {
149 if (!TargetRegisterInfo::isVirtualRegister(Reg))
150 return false;
151 if (regsLiveOut.count(Reg))
152 return false;
153 return vregsRequired.insert(Reg).second;
154 }
155
156 // Same for a full set.
157 bool addRequired(const RegSet &RS) {
158 bool changed = false;
159 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
160 if (addRequired(*I))
161 changed = true;
162 return changed;
163 }
164
165 // Same for a full map.
166 bool addRequired(const RegMap &RM) {
167 bool changed = false;
168 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
169 if (addRequired(I->first))
170 changed = true;
171 return changed;
172 }
173
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000174 // Live-out registers are either in regsLiveOut or vregsPassed.
175 bool isLiveOut(unsigned Reg) const {
176 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
177 }
178 };
179
180 // Extra register info per MBB.
181 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
182
183 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000184 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000185 }
186
Lang Hames03698de2012-02-14 19:17:48 +0000187 bool isAllocatable(unsigned Reg) {
Jakob Stoklund Olesenfeab72c2012-10-16 00:05:06 +0000188 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
Lang Hames03698de2012-02-14 19:17:48 +0000189 }
190
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000191 // Analysis information if available
192 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000193 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000194 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000195 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000196
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000197 void visitMachineFunctionBefore();
198 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000199 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000200 void visitMachineInstrBefore(const MachineInstr *MI);
201 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
202 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000203 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000204 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
205 void visitMachineFunctionAfter();
206
207 void report(const char *msg, const MachineFunction *MF);
208 void report(const char *msg, const MachineBasicBlock *MBB);
209 void report(const char *msg, const MachineInstr *MI);
210 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000211 void report(const char *msg, const MachineFunction *MF,
212 const LiveInterval &LI);
213 void report(const char *msg, const MachineBasicBlock *MBB,
214 const LiveInterval &LI);
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000215 void report(const char *msg, const MachineFunction *MF,
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700216 const LiveRange &LR, unsigned Reg, unsigned LaneMask);
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000217 void report(const char *msg, const MachineBasicBlock *MBB,
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700218 const LiveRange &LR, unsigned Reg, unsigned LaneMask);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000219
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000220 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000221
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000222 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000223 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000224 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000225 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000226
227 void calcRegsRequired();
228 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000229 void verifyLiveIntervals();
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +0000230 void verifyLiveInterval(const LiveInterval&);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700231 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
232 unsigned);
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000233 void verifyLiveRangeSegment(const LiveRange&,
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700234 const LiveRange::const_iterator I, unsigned,
235 unsigned);
236 void verifyLiveRange(const LiveRange&, unsigned, unsigned LaneMask = 0);
Manman Ren7310b752013-07-15 21:26:31 +0000237
238 void verifyStackFrame();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000239 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000240
241 struct MachineVerifierPass : public MachineFunctionPass {
242 static char ID; // Pass ID, replacement for typeid
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700243 const std::string Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000244
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700245 MachineVerifierPass(const std::string &banner = nullptr)
246 : MachineFunctionPass(ID), Banner(banner) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000247 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
248 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000249
Stephen Hines36b56882014-04-23 16:57:46 -0700250 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000251 AU.setPreservesAll();
252 MachineFunctionPass::getAnalysisUsage(AU);
253 }
254
Stephen Hines36b56882014-04-23 16:57:46 -0700255 bool runOnMachineFunction(MachineFunction &MF) override {
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700256 MF.verify(this, Banner.c_str());
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000257 return false;
258 }
259 };
260
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000261}
262
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000263char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000264INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000265 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000266
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700267FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000268 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000269}
270
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000271void MachineFunction::verify(Pass *p, const char *Banner) const {
272 MachineVerifier(p, Banner)
273 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000274}
275
Chris Lattner17e9edc2009-08-23 02:51:22 +0000276bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000277 foundErrors = 0;
278
279 this->MF = &MF;
280 TM = &MF.getTarget();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800281 TII = MF.getSubtarget().getInstrInfo();
282 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000283 MRI = &MF.getRegInfo();
284
Stephen Hinesdce4a402014-05-29 02:49:00 -0700285 LiveVars = nullptr;
286 LiveInts = nullptr;
287 LiveStks = nullptr;
288 Indexes = nullptr;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000289 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000290 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000291 // We don't want to verify LiveVariables if LiveIntervals is available.
292 if (!LiveInts)
293 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000294 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000295 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000296 }
297
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000298 visitMachineFunctionBefore();
299 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
300 MFI!=MFE; ++MFI) {
301 visitMachineBasicBlockBefore(MFI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000302 // Keep track of the current bundle header.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700303 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000304 // Do we expect the next instruction to be part of the same bundle?
305 bool InBundle = false;
306
Evan Chengddfd1372011-12-14 02:11:42 +0000307 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
308 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000309 if (MBBI->getParent() != MFI) {
310 report("Bad instruction parent pointer", MFI);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700311 errs() << "Instruction: " << *MBBI;
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000312 continue;
313 }
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000314
315 // Check for consistent bundle flags.
316 if (InBundle && !MBBI->isBundledWithPred())
317 report("Missing BundledPred flag, "
318 "BundledSucc was set on predecessor", MBBI);
319 if (!InBundle && MBBI->isBundledWithPred())
320 report("BundledPred flag is set, "
321 "but BundledSucc not set on predecessor", MBBI);
322
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000323 // Is this a bundle header?
324 if (!MBBI->isInsideBundle()) {
325 if (CurBundle)
326 visitMachineBundleAfter(CurBundle);
327 CurBundle = MBBI;
328 visitMachineBundleBefore(CurBundle);
329 } else if (!CurBundle)
330 report("No bundle header", MBBI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000331 visitMachineInstrBefore(MBBI);
332 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
333 visitMachineOperand(&MBBI->getOperand(I), I);
334 visitMachineInstrAfter(MBBI);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000335
336 // Was this the last bundled instruction?
337 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000338 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000339 if (CurBundle)
340 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000341 if (InBundle)
342 report("BundledSucc flag set on last instruction in block", &MFI->back());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000343 visitMachineBasicBlockAfter(MFI);
344 }
345 visitMachineFunctionAfter();
346
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700347 if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000348 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000349
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000350 // Clean up.
351 regsLive.clear();
352 regsDefined.clear();
353 regsDead.clear();
354 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000355 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000356 regsLiveInButUnused.clear();
357 MBBInfoMap.clear();
358
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000359 return false; // no changes
360}
361
Chris Lattner372fefe2009-08-23 01:03:30 +0000362void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000363 assert(MF);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700364 errs() << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000365 if (!foundErrors++) {
366 if (Banner)
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700367 errs() << "# " << Banner << '\n';
368 MF->print(errs(), Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000369 }
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700370 errs() << "*** Bad machine code: " << msg << " ***\n"
Craig Topper96601ca2012-08-22 06:07:19 +0000371 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000372}
373
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000374void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000375 assert(MBB);
376 report(msg, MBB->getParent());
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700377 errs() << "- basic block: BB#" << MBB->getNumber()
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000378 << ' ' << MBB->getName()
Roman Divacky59324292012-09-05 22:26:57 +0000379 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000380 if (Indexes)
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700381 errs() << " [" << Indexes->getMBBStartIdx(MBB)
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000382 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700383 errs() << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000384}
385
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000386void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000387 assert(MI);
388 report(msg, MI->getParent());
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700389 errs() << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000390 if (Indexes && Indexes->hasIndex(MI))
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700391 errs() << Indexes->getInstructionIndex(MI) << '\t';
392 MI->print(errs(), TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000393}
394
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000395void MachineVerifier::report(const char *msg,
396 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000397 assert(MO);
398 report(msg, MO->getParent());
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700399 errs() << "- operand " << MONum << ": ";
400 MO->print(errs(), TM);
401 errs() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000402}
403
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000404void MachineVerifier::report(const char *msg, const MachineFunction *MF,
405 const LiveInterval &LI) {
406 report(msg, MF);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700407 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000408}
409
410void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
411 const LiveInterval &LI) {
412 report(msg, MBB);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700413 errs() << "- interval: " << LI << '\n';
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000414}
415
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000416void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700417 const LiveRange &LR, unsigned Reg,
418 unsigned LaneMask) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000419 report(msg, MBB);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700420 errs() << "- liverange: " << LR << '\n';
421 errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
422 if (LaneMask != 0)
423 errs() << "- lanemask: " << format("%04X\n", LaneMask);
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000424}
425
426void MachineVerifier::report(const char *msg, const MachineFunction *MF,
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700427 const LiveRange &LR, unsigned Reg,
428 unsigned LaneMask) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000429 report(msg, MF);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700430 errs() << "- liverange: " << LR << '\n';
431 errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
432 if (LaneMask != 0)
433 errs() << "- lanemask: " << format("%04X\n", LaneMask);
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000434}
435
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000436void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000437 BBInfo &MInfo = MBBInfoMap[MBB];
438 if (!MInfo.reachable) {
439 MInfo.reachable = true;
440 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
441 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
442 markReachable(*SuI);
443 }
444}
445
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000446void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000447 lastIndex = SlotIndex();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000448 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000449
450 // A sub-register of a reserved register is also reserved
451 for (int Reg = regsReserved.find_first(); Reg>=0;
452 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000453 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000454 // FIXME: This should probably be:
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000455 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
456 regsReserved.set(*SubRegs);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000457 }
458 }
Lang Hames03698de2012-02-14 19:17:48 +0000459
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000460 markReachable(&MF->front());
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000461
462 // Build a set of the basic blocks in the function.
463 FunctionBlocks.clear();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700464 for (const auto &MBB : *MF) {
465 FunctionBlocks.insert(&MBB);
466 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000467
Stephen Hinesdce4a402014-05-29 02:49:00 -0700468 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
469 if (MInfo.Preds.size() != MBB.pred_size())
470 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000471
Stephen Hinesdce4a402014-05-29 02:49:00 -0700472 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
473 if (MInfo.Succs.size() != MBB.succ_size())
474 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000475 }
Jakob Stoklund Olesena58d67a2013-04-19 21:40:57 +0000476
477 // Check that the register use lists are sane.
478 MRI->verifyUseLists();
Manman Ren7310b752013-07-15 21:26:31 +0000479
480 verifyStackFrame();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000481}
482
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000483// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000484static bool matchPair(MachineBasicBlock::const_succ_iterator i,
485 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000486 if (*i == a)
487 return *++i == b;
488 if (*i == b)
489 return *++i == a;
490 return false;
491}
492
493void
494MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700495 FirstTerminator = nullptr;
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000496
Lang Hames03698de2012-02-14 19:17:48 +0000497 if (MRI->isSSA()) {
498 // If this block has allocatable physical registers live-in, check that
499 // it is an entry block or landing pad.
500 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
501 LE = MBB->livein_end();
502 LI != LE; ++LI) {
503 unsigned reg = *LI;
504 if (isAllocatable(reg) && !MBB->isLandingPad() &&
505 MBB != MBB->getParent()->begin()) {
506 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
507 }
508 }
509 }
510
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000511 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000512 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000513 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000514 E = MBB->succ_end(); I != E; ++I) {
515 if ((*I)->isLandingPad())
516 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000517 if (!FunctionBlocks.count(*I))
518 report("MBB has successor that isn't part of the function.", MBB);
519 if (!MBBInfoMap[*I].Preds.count(MBB)) {
520 report("Inconsistent CFG", MBB);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700521 errs() << "MBB is not in the predecessor list of the successor BB#"
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000522 << (*I)->getNumber() << ".\n";
523 }
524 }
525
526 // Check the predecessor list.
527 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
528 E = MBB->pred_end(); I != E; ++I) {
529 if (!FunctionBlocks.count(*I))
530 report("MBB has predecessor that isn't part of the function.", MBB);
531 if (!MBBInfoMap[*I].Succs.count(MBB)) {
532 report("Inconsistent CFG", MBB);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700533 errs() << "MBB is not in the successor list of the predecessor BB#"
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000534 << (*I)->getNumber() << ".\n";
535 }
Cameron Zwarich2100d212010-12-20 04:19:48 +0000536 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000537
538 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
539 const BasicBlock *BB = MBB->getBasicBlock();
540 if (LandingPadSuccs.size() > 1 &&
541 !(AsmInfo &&
542 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
543 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000544 report("MBB has more than one landing pad successor", MBB);
545
Dan Gohman27920592009-08-27 02:43:49 +0000546 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700547 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman27920592009-08-27 02:43:49 +0000548 SmallVector<MachineOperand, 4> Cond;
549 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
550 TBB, FBB, Cond)) {
551 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
552 // check whether its answers match up with reality.
553 if (!TBB && !FBB) {
554 // Block falls through to its successor.
555 MachineFunction::const_iterator MBBI = MBB;
556 ++MBBI;
557 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000558 // It's possible that the block legitimately ends with a noreturn
559 // call or an unreachable, in which case it won't actually fall
560 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000561 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000562 // It's possible that the block legitimately ends with a noreturn
563 // call or an unreachable, in which case it won't actuall fall
564 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000565 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000566 report("MBB exits via unconditional fall-through but doesn't have "
567 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000568 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000569 report("MBB exits via unconditional fall-through but its successor "
570 "differs from its CFG successor!", MBB);
571 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700572 if (!MBB->empty() && MBB->back().isBarrier() &&
573 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000574 report("MBB exits via unconditional fall-through but ends with a "
575 "barrier instruction!", MBB);
576 }
577 if (!Cond.empty()) {
578 report("MBB exits via unconditional fall-through but has a condition!",
579 MBB);
580 }
581 } else if (TBB && !FBB && Cond.empty()) {
582 // Block unconditionally branches somewhere.
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700583 // If the block has exactly one successor, that happens to be a
584 // landingpad, accept it as valid control flow.
585 if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
586 (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
587 *MBB->succ_begin() != *LandingPadSuccs.begin())) {
Dan Gohman27920592009-08-27 02:43:49 +0000588 report("MBB exits via unconditional branch but doesn't have "
589 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000590 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000591 report("MBB exits via unconditional branch but the CFG "
592 "successor doesn't match the actual successor!", MBB);
593 }
594 if (MBB->empty()) {
595 report("MBB exits via unconditional branch but doesn't contain "
596 "any instructions!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700597 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000598 report("MBB exits via unconditional branch but doesn't end with a "
599 "barrier instruction!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700600 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000601 report("MBB exits via unconditional branch but the branch isn't a "
602 "terminator instruction!", MBB);
603 }
604 } else if (TBB && !FBB && !Cond.empty()) {
605 // Block conditionally branches somewhere, otherwise falls through.
606 MachineFunction::const_iterator MBBI = MBB;
607 ++MBBI;
608 if (MBBI == MF->end()) {
609 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko344df792012-12-19 22:13:01 +0000610 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000611 // A conditional branch with only one successor is weird, but allowed.
612 if (&*MBBI != TBB)
613 report("MBB exits via conditional branch/fall-through but only has "
614 "one CFG successor!", MBB);
615 else if (TBB != *MBB->succ_begin())
616 report("MBB exits via conditional branch/fall-through but the CFG "
617 "successor don't match the actual successor!", MBB);
618 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000619 report("MBB exits via conditional branch/fall-through but doesn't have "
620 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000621 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000622 report("MBB exits via conditional branch/fall-through but the CFG "
623 "successors don't match the actual successors!", MBB);
624 }
625 if (MBB->empty()) {
626 report("MBB exits via conditional branch/fall-through but doesn't "
627 "contain any instructions!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700628 } else if (MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000629 report("MBB exits via conditional branch/fall-through but ends with a "
630 "barrier instruction!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700631 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000632 report("MBB exits via conditional branch/fall-through but the branch "
633 "isn't a terminator instruction!", MBB);
634 }
635 } else if (TBB && FBB) {
636 // Block conditionally branches somewhere, otherwise branches
637 // somewhere else.
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000638 if (MBB->succ_size() == 1) {
639 // A conditional branch with only one successor is weird, but allowed.
640 if (FBB != TBB)
641 report("MBB exits via conditional branch/branch through but only has "
642 "one CFG successor!", MBB);
643 else if (TBB != *MBB->succ_begin())
644 report("MBB exits via conditional branch/branch through but the CFG "
645 "successor don't match the actual successor!", MBB);
646 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000647 report("MBB exits via conditional branch/branch but doesn't have "
648 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000649 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000650 report("MBB exits via conditional branch/branch but the CFG "
651 "successors don't match the actual successors!", MBB);
652 }
653 if (MBB->empty()) {
654 report("MBB exits via conditional branch/branch but doesn't "
655 "contain any instructions!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700656 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000657 report("MBB exits via conditional branch/branch but doesn't end with a "
658 "barrier instruction!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700659 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000660 report("MBB exits via conditional branch/branch but the branch "
661 "isn't a terminator instruction!", MBB);
662 }
663 if (Cond.empty()) {
664 report("MBB exits via conditinal branch/branch but there's no "
665 "condition!", MBB);
666 }
667 } else {
668 report("AnalyzeBranch returned invalid data!", MBB);
669 }
670 }
671
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000672 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000673 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000674 E = MBB->livein_end(); I != E; ++I) {
675 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
676 report("MBB live-in list contains non-physical register", MBB);
677 continue;
678 }
Chad Rosier62c320a2013-05-22 23:17:36 +0000679 for (MCSubRegIterator SubRegs(*I, TRI, /*IncludeSelf=*/true);
680 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000681 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000682 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000683 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000684
685 const MachineFrameInfo *MFI = MF->getFrameInfo();
686 assert(MFI && "Function has no frame info");
687 BitVector PR = MFI->getPristineRegs(MBB);
688 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosier62c320a2013-05-22 23:17:36 +0000689 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
690 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000691 regsLive.insert(*SubRegs);
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000692 }
693
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000694 regsKilled.clear();
695 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000696
697 if (Indexes)
698 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000699}
700
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000701// This function gets called for all bundle headers, including normal
702// stand-alone unbundled instructions.
703void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
704 if (Indexes && Indexes->hasIndex(MI)) {
705 SlotIndex idx = Indexes->getInstructionIndex(MI);
706 if (!(idx > lastIndex)) {
707 report("Instruction index out of order", MI);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700708 errs() << "Last instruction was at " << lastIndex << '\n';
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000709 }
710 lastIndex = idx;
711 }
Pete Cooper83569cb2012-06-07 17:41:39 +0000712
713 // Ensure non-terminators don't follow terminators.
714 // Ignore predicated terminators formed by if conversion.
715 // FIXME: If conversion shouldn't need to violate this rule.
716 if (MI->isTerminator() && !TII->isPredicated(MI)) {
717 if (!FirstTerminator)
718 FirstTerminator = MI;
719 } else if (FirstTerminator) {
720 report("Non-terminator instruction after the first terminator", MI);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700721 errs() << "First terminator was:\t" << *FirstTerminator;
Pete Cooper83569cb2012-06-07 17:41:39 +0000722 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000723}
724
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000725// The operands on an INLINEASM instruction must follow a template.
726// Verify that the flag operands make sense.
727void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
728 // The first two operands on INLINEASM are the asm string and global flags.
729 if (MI->getNumOperands() < 2) {
730 report("Too few operands on inline asm", MI);
731 return;
732 }
733 if (!MI->getOperand(0).isSymbol())
734 report("Asm string must be an external symbol", MI);
735 if (!MI->getOperand(1).isImm())
736 report("Asm flags must be an immediate", MI);
Chad Rosier3d716882012-10-30 19:11:54 +0000737 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
738 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
739 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000740 report("Unknown asm flags", &MI->getOperand(1), 1);
741
742 assert(InlineAsm::MIOp_FirstOperand == 2 && "Asm format changed");
743
744 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
745 unsigned NumOps;
746 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
747 const MachineOperand &MO = MI->getOperand(OpNo);
748 // There may be implicit ops after the fixed operands.
749 if (!MO.isImm())
750 break;
751 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
752 }
753
754 if (OpNo > MI->getNumOperands())
755 report("Missing operands in last group", MI);
756
757 // An optional MDNode follows the groups.
758 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
759 ++OpNo;
760
761 // All trailing operands must be implicit registers.
762 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
763 const MachineOperand &MO = MI->getOperand(OpNo);
764 if (!MO.isReg() || !MO.isImplicit())
765 report("Expected implicit register after groups", &MO, OpNo);
766 }
767}
768
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000769void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000770 const MCInstrDesc &MCID = MI->getDesc();
771 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000772 report("Too few operands", MI);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700773 errs() << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault17d4ac82013-11-15 22:18:19 +0000774 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000775 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000776
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000777 // Check the tied operands.
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000778 if (MI->isInlineAsm())
779 verifyInlineAsm(MI);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000780
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000781 // Check the MachineMemOperands for basic consistency.
782 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
783 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000784 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000785 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000786 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000787 report("Missing mayStore flag", MI);
788 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000789
790 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000791 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000792 if (LiveInts) {
793 bool mapped = !LiveInts->isNotInMIMap(MI);
794 if (MI->isDebugValue()) {
795 if (mapped)
796 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000797 } else if (MI->isInsideBundle()) {
798 if (mapped)
799 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000800 } else {
801 if (!mapped)
802 report("Missing slot index", MI);
803 }
804 }
805
Andrew Trick3be654f2011-09-21 02:20:46 +0000806 StringRef ErrorInfo;
807 if (!TII->verifyInstruction(MI, ErrorInfo))
808 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000809}
810
811void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000812MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000813 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000814 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000815
Evan Chenge837dea2011-06-28 19:10:37 +0000816 // The first MCID.NumDefs operands must be explicit register defines
817 if (MONum < MCID.getNumDefs()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000818 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000819 if (!MO->isReg())
820 report("Explicit definition must be a register", MO, MONum);
Evan Chengcac58aa2012-05-29 19:40:44 +0000821 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000822 report("Explicit definition marked as use", MO, MONum);
823 else if (MO->isImplicit())
824 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000825 } else if (MONum < MCID.getNumOperands()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000826 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopher113a06c2010-11-17 00:55:36 +0000827 // Don't check if it's the last operand in a variadic instruction. See,
828 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000829 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000830 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000831 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braunb38d9872013-10-04 16:53:00 +0000832 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000833 if (MO->isImplicit())
834 report("Explicit operand marked as implicit", MO, MONum);
835 }
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000836
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000837 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
838 if (TiedTo != -1) {
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000839 if (!MO->isReg())
840 report("Tied use must be a register", MO, MONum);
841 else if (!MO->isTied())
842 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000843 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
844 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000845 } else if (MO->isReg() && MO->isTied())
846 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000847 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000848 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000849 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000850 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000851 }
852
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000853 switch (MO->getType()) {
854 case MachineOperand::MO_Register: {
855 const unsigned Reg = MO->getReg();
856 if (!Reg)
857 return;
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000858 if (MRI->tracksLiveness() && !MI->isDebugValue())
859 checkLiveness(MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000860
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000861 // Verify the consistency of tied operands.
862 if (MO->isTied()) {
863 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
864 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
865 if (!OtherMO.isReg())
866 report("Must be tied to a register", MO, MONum);
867 if (!OtherMO.isTied())
868 report("Missing tie flags on tied operand", MO, MONum);
869 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
870 report("Inconsistent tie links", MO, MONum);
871 if (MONum < MCID.getNumDefs()) {
872 if (OtherIdx < MCID.getNumOperands()) {
873 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
874 report("Explicit def tied to explicit use without tie constraint",
875 MO, MONum);
876 } else {
877 if (!OtherMO.isImplicit())
878 report("Explicit def should be tied to implicit use", MO, MONum);
879 }
880 }
881 }
882
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000883 // Verify two-address constraints after leaving SSA form.
884 unsigned DefIdx;
885 if (!MRI->isSSA() && MO->isUse() &&
886 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
887 Reg != MI->getOperand(DefIdx).getReg())
888 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000889
890 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000891 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000892 unsigned SubIdx = MO->getSubReg();
893
894 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000895 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000896 report("Illegal subregister index for physical register", MO, MONum);
897 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000898 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000899 if (const TargetRegisterClass *DRC =
900 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000901 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000902 report("Illegal physical register for instruction", MO, MONum);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700903 errs() << TRI->getName(Reg) << " is not a "
Stephen Hines37ed9c12014-12-01 14:51:49 -0800904 << TRI->getRegClassName(DRC) << " register.\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000905 }
906 }
907 } else {
908 // Virtual register.
909 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
910 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000911 const TargetRegisterClass *SRC =
912 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000913 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000914 report("Invalid subregister index for virtual register", MO, MONum);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700915 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000916 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000917 return;
918 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000919 if (RC != SRC) {
920 report("Invalid register class for subregister index", MO, MONum);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700921 errs() << "Register class " << TRI->getRegClassName(RC)
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000922 << " does not fully support subreg index " << SubIdx << "\n";
923 return;
924 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000925 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000926 if (const TargetRegisterClass *DRC =
927 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000928 if (SubIdx) {
929 const TargetRegisterClass *SuperRC =
930 TRI->getLargestLegalSuperClass(RC);
931 if (!SuperRC) {
932 report("No largest legal super class exists.", MO, MONum);
933 return;
934 }
935 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
936 if (!DRC) {
937 report("No matching super-reg register class.", MO, MONum);
938 return;
939 }
940 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000941 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000942 report("Illegal virtual register for instruction", MO, MONum);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700943 errs() << "Expected a " << TRI->getRegClassName(DRC)
Stephen Hines37ed9c12014-12-01 14:51:49 -0800944 << " register, but got a " << TRI->getRegClassName(RC)
945 << " register\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000946 }
947 }
948 }
949 }
950 break;
951 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000952
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000953 case MachineOperand::MO_RegisterMask:
954 regMasks.push_back(MO->getRegMask());
955 break;
956
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000957 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000958 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
959 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000960 break;
961
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000962 case MachineOperand::MO_FrameIndex:
963 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
964 LiveInts && !LiveInts->isNotInMIMap(MI)) {
965 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
966 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000967 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000968 report("Instruction loads from dead spill slot", MO, MONum);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700969 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000970 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000971 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000972 report("Instruction stores to dead spill slot", MO, MONum);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700973 errs() << "Live stack: " << LI << '\n';
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000974 }
975 }
976 break;
977
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000978 default:
979 break;
980 }
981}
982
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000983void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
984 const MachineInstr *MI = MO->getParent();
985 const unsigned Reg = MO->getReg();
986
987 // Both use and def operands can read a register.
988 if (MO->readsReg()) {
989 regsLiveInButUnused.erase(Reg);
990
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000991 if (MO->isKill())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000992 addRegWithSubRegs(regsKilled, Reg);
993
994 // Check that LiveVars knows this kill.
995 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
996 MO->isKill()) {
997 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
998 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
999 report("Kill missing from LiveVariables", MO, MONum);
1000 }
1001
1002 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001003 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
1004 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
1005 // Check the cached regunit intervals.
1006 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1007 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001008 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units)) {
1009 LiveQueryResult LRQ = LR->Query(UseIdx);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001010 if (!LRQ.valueIn()) {
Matthias Braun331de112013-10-10 21:28:43 +00001011 report("No live segment at use", MO, MONum);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001012 errs() << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001013 << ' ' << *LR << '\n';
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001014 }
1015 if (MO->isKill() && !LRQ.isKill()) {
1016 report("Live range continues after kill flag", MO, MONum);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001017 errs() << PrintRegUnit(*Units, TRI) << ' ' << *LR << '\n';
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001018 }
1019 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001020 }
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001021 }
1022
1023 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1024 if (LiveInts->hasInterval(Reg)) {
1025 // This is a virtual register interval.
1026 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun5649e252013-10-10 21:28:52 +00001027 LiveQueryResult LRQ = LI.Query(UseIdx);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001028 if (!LRQ.valueIn()) {
Matthias Braun331de112013-10-10 21:28:43 +00001029 report("No live segment at use", MO, MONum);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001030 errs() << UseIdx << " is not live in " << LI << '\n';
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001031 }
1032 // Check for extra kill flags.
1033 // Note that we allow missing kill flags for now.
1034 if (MO->isKill() && !LRQ.isKill()) {
1035 report("Live range continues after kill flag", MO, MONum);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001036 errs() << "Live range: " << LI << '\n';
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001037 }
1038 } else {
1039 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001040 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001041 }
1042 }
1043
1044 // Use of a dead register.
1045 if (!regsLive.count(Reg)) {
1046 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1047 // Reserved registers may be used even when 'dead'.
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001048 bool Bad = !isReserved(Reg);
1049 // We are fine if just any subregister has a defined value.
1050 if (Bad) {
1051 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1052 ++SubRegs) {
1053 if (regsLive.count(*SubRegs)) {
1054 Bad = false;
1055 break;
1056 }
1057 }
1058 }
1059 // If there is an additional implicit-use of a super register we stop
1060 // here. By definition we are fine if the super register is not
1061 // (completely) dead, if the complete super register is dead we will
1062 // get a report for its operand.
1063 if (Bad) {
1064 for (const MachineOperand &MOP : MI->uses()) {
1065 if (!MOP.isReg())
1066 continue;
1067 if (!MOP.isImplicit())
1068 continue;
1069 for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1070 ++SubRegs) {
1071 if (*SubRegs == Reg) {
1072 Bad = false;
1073 break;
1074 }
1075 }
1076 }
1077 }
1078 if (Bad)
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001079 report("Using an undefined physical register", MO, MONum);
Pete Cooperb97c57a2012-07-19 23:40:38 +00001080 } else if (MRI->def_empty(Reg)) {
1081 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001082 } else {
1083 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1084 // We don't know which virtual registers are live in, so only complain
1085 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1086 // must be live in. PHI instructions are handled separately.
1087 if (MInfo.regsKilled.count(Reg))
1088 report("Using a killed virtual register", MO, MONum);
1089 else if (!MI->isPHI())
1090 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1091 }
1092 }
1093 }
1094
1095 if (MO->isDef()) {
1096 // Register defined.
1097 // TODO: verify that earlyclobber ops are not used.
1098 if (MO->isDead())
1099 addRegWithSubRegs(regsDead, Reg);
1100 else
1101 addRegWithSubRegs(regsDefined, Reg);
1102
1103 // Verify SSA form.
1104 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Stephen Hines36b56882014-04-23 16:57:46 -07001105 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001106 report("Multiple virtual register defs in SSA form", MO, MONum);
1107
Matthias Braun331de112013-10-10 21:28:43 +00001108 // Check LiveInts for a live segment, but only for virtual registers.
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001109 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1110 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001111 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1112 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001113 if (LiveInts->hasInterval(Reg)) {
1114 const LiveInterval &LI = LiveInts->getInterval(Reg);
1115 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1116 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001117 if (VNI->def != DefIdx) {
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001118 report("Inconsistent valno->def", MO, MONum);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001119 errs() << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001120 << DefIdx << " in " << LI << '\n';
1121 }
1122 } else {
Matthias Braun331de112013-10-10 21:28:43 +00001123 report("No live segment at def", MO, MONum);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001124 errs() << DefIdx << " is not live in " << LI << '\n';
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001125 }
Pedro Artigasd900b112013-11-08 22:46:28 +00001126 // Check that, if the dead def flag is present, LiveInts agree.
1127 if (MO->isDead()) {
1128 LiveQueryResult LRQ = LI.Query(DefIdx);
1129 if (!LRQ.isDeadDef()) {
1130 report("Live range continues after dead def flag", MO, MONum);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001131 errs() << "Live range: " << LI << '\n';
Pedro Artigasd900b112013-11-08 22:46:28 +00001132 }
1133 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001134 } else {
1135 report("Virtual register has no Live interval", MO, MONum);
1136 }
1137 }
1138 }
1139}
1140
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001141void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +00001142}
1143
1144// This function gets called after visiting all instructions in a bundle. The
1145// argument points to the bundle header.
1146// Normal stand-alone instructions are also considered 'bundles', and this
1147// function is called for all of them.
1148void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001149 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1150 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001151 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +00001152 // Kill any masked registers.
1153 while (!regMasks.empty()) {
1154 const uint32_t *Mask = regMasks.pop_back_val();
1155 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1156 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1157 MachineOperand::clobbersPhysReg(Mask, *I))
1158 regsDead.push_back(*I);
1159 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001160 set_subtract(regsLive, regsDead); regsDead.clear();
1161 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001162}
1163
1164void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001165MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001166 MBBInfoMap[MBB].regsLiveOut = regsLive;
1167 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +00001168
1169 if (Indexes) {
1170 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1171 if (!(stop > lastIndex)) {
1172 report("Block ends before last instruction index", MBB);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001173 errs() << "Block ends at " << stop
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +00001174 << " last instruction was at " << lastIndex << '\n';
1175 }
1176 lastIndex = stop;
1177 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001178}
1179
1180// Calculate the largest possible vregsPassed sets. These are the registers that
1181// can pass through an MBB live, but may not be live every time. It is assumed
1182// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001183void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001184 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1185 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001186 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001187 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001188 BBInfo &MInfo = MBBInfoMap[&MBB];
1189 if (!MInfo.reachable)
1190 continue;
1191 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1192 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1193 BBInfo &SInfo = MBBInfoMap[*SuI];
1194 if (SInfo.addPassed(MInfo.regsLiveOut))
1195 todo.insert(*SuI);
1196 }
1197 }
1198
1199 // Iteratively push vregsPassed to successors. This will converge to the same
1200 // final state regardless of DenseSet iteration order.
1201 while (!todo.empty()) {
1202 const MachineBasicBlock *MBB = *todo.begin();
1203 todo.erase(MBB);
1204 BBInfo &MInfo = MBBInfoMap[MBB];
1205 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1206 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1207 if (*SuI == MBB)
1208 continue;
1209 BBInfo &SInfo = MBBInfoMap[*SuI];
1210 if (SInfo.addPassed(MInfo.vregsPassed))
1211 todo.insert(*SuI);
1212 }
1213 }
1214}
1215
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001216// Calculate the set of virtual registers that must be passed through each basic
1217// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001218// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001219void MachineVerifier::calcRegsRequired() {
1220 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001221 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001222 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001223 BBInfo &MInfo = MBBInfoMap[&MBB];
1224 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1225 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1226 BBInfo &PInfo = MBBInfoMap[*PrI];
1227 if (PInfo.addRequired(MInfo.vregsLiveIn))
1228 todo.insert(*PrI);
1229 }
1230 }
1231
1232 // Iteratively push vregsRequired to predecessors. This will converge to the
1233 // same final state regardless of DenseSet iteration order.
1234 while (!todo.empty()) {
1235 const MachineBasicBlock *MBB = *todo.begin();
1236 todo.erase(MBB);
1237 BBInfo &MInfo = MBBInfoMap[MBB];
1238 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1239 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1240 if (*PrI == MBB)
1241 continue;
1242 BBInfo &SInfo = MBBInfoMap[*PrI];
1243 if (SInfo.addRequired(MInfo.vregsRequired))
1244 todo.insert(*PrI);
1245 }
1246 }
1247}
1248
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001249// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001250// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001251void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001252 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001253 for (const auto &BBI : *MBB) {
1254 if (!BBI.isPHI())
1255 break;
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001256 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001257
Stephen Hinesdce4a402014-05-29 02:49:00 -07001258 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1259 unsigned Reg = BBI.getOperand(i).getReg();
1260 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001261 if (!Pre->isSuccessor(MBB))
1262 continue;
1263 seen.insert(Pre);
1264 BBInfo &PrInfo = MBBInfoMap[Pre];
1265 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1266 report("PHI operand is not live-out from predecessor",
Stephen Hinesdce4a402014-05-29 02:49:00 -07001267 &BBI.getOperand(i), i);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001268 }
1269
1270 // Did we see all predecessors?
1271 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1272 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1273 if (!seen.count(*PrI)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001274 report("Missing PHI operand", &BBI);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001275 errs() << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001276 << " is a predecessor according to the CFG.\n";
1277 }
1278 }
1279 }
1280}
1281
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001282void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001283 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001284
Stephen Hinesdce4a402014-05-29 02:49:00 -07001285 for (const auto &MBB : *MF) {
1286 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001287
1288 // Skip unreachable MBBs.
1289 if (!MInfo.reachable)
1290 continue;
1291
Stephen Hinesdce4a402014-05-29 02:49:00 -07001292 checkPHIOps(&MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001293 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001294
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001295 // Now check liveness info if available
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001296 calcRegsRequired();
1297
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001298 // Check for killed virtual registers that should be live out.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001299 for (const auto &MBB : *MF) {
1300 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001301 for (RegSet::iterator
1302 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1303 ++I)
1304 if (MInfo.regsKilled.count(*I)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001305 report("Virtual register killed in block, but needed live out.", &MBB);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001306 errs() << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001307 << " is used after the block.\n";
1308 }
1309 }
1310
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001311 if (!MF->empty()) {
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001312 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1313 for (RegSet::iterator
1314 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesenff0275e2012-03-10 00:44:11 +00001315 ++I)
1316 report("Virtual register def doesn't dominate all uses.",
1317 MRI->getVRegDef(*I));
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001318 }
1319
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001320 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001321 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001322 if (LiveInts)
1323 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001324}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001325
1326void MachineVerifier::verifyLiveVariables() {
1327 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001328 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1329 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001330 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001331 for (const auto &MBB : *MF) {
1332 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001333
1334 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1335 if (MInfo.vregsRequired.count(Reg)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001336 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1337 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001338 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001339 << " must be live through the block.\n";
1340 }
1341 } else {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001342 if (VI.AliveBlocks.test(MBB.getNumber())) {
1343 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001344 errs() << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001345 << " is not needed live through the block.\n";
1346 }
1347 }
1348 }
1349 }
1350}
1351
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001352void MachineVerifier::verifyLiveIntervals() {
1353 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001354 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1355 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001356
1357 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001358 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001359 continue;
1360
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001361 if (!LiveInts->hasInterval(Reg)) {
1362 report("Missing live interval for virtual register", MF);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001363 errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001364 continue;
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001365 }
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001366
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001367 const LiveInterval &LI = LiveInts->getInterval(Reg);
1368 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001369 verifyLiveInterval(LI);
1370 }
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001371
1372 // Verify all the cached regunit intervals.
1373 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001374 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1375 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001376}
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001377
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001378void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001379 const VNInfo *VNI, unsigned Reg,
1380 unsigned LaneMask) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001381 if (VNI->isUnused())
1382 return;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001383
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001384 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001385
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001386 if (!DefVNI) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001387 report("Valno not live at def and not marked unused", MF, LR, Reg,
1388 LaneMask);
1389 errs() << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001390 return;
1391 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001392
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001393 if (DefVNI != VNI) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001394 report("Live segment at def has different valno", MF, LR, Reg, LaneMask);
1395 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001396 << " where valno #" << DefVNI->id << " is live\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001397 return;
1398 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001399
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001400 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1401 if (!MBB) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001402 report("Invalid definition index", MF, LR, Reg, LaneMask);
1403 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001404 << " in " << LR << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001405 return;
1406 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001407
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001408 if (VNI->isPHIDef()) {
1409 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001410 report("PHIDef value is not defined at MBB start", MBB, LR, Reg,
1411 LaneMask);
1412 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001413 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001414 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001415 return;
1416 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001417
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001418 // Non-PHI def.
1419 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1420 if (!MI) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001421 report("No instruction at def index", MBB, LR, Reg, LaneMask);
1422 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001423 return;
1424 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001425
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001426 if (Reg != 0) {
1427 bool hasDef = false;
1428 bool isEarlyClobber = false;
1429 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1430 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001431 continue;
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001432 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1433 if (MOI->getReg() != Reg)
1434 continue;
1435 } else {
1436 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1437 !TRI->hasRegUnit(MOI->getReg(), Reg))
1438 continue;
1439 }
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001440 if (LaneMask != 0 &&
1441 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask) == 0)
1442 continue;
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001443 hasDef = true;
1444 if (MOI->isEarlyClobber())
1445 isEarlyClobber = true;
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001446 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001447
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001448 if (!hasDef) {
1449 report("Defining instruction does not modify register", MI);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001450 errs() << "Valno #" << VNI->id << " in " << LR << '\n';
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001451 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001452
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001453 // Early clobber defs begin at USE slots, but other defs must begin at
1454 // DEF slots.
1455 if (isEarlyClobber) {
1456 if (!VNI->def.isEarlyClobber()) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001457 report("Early clobber def must be at an early-clobber slot", MBB, LR,
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001458 Reg, LaneMask);
1459 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001460 }
1461 } else if (!VNI->def.isRegister()) {
1462 report("Non-PHI, non-early clobber def must be at a register slot",
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001463 MBB, LR, Reg, LaneMask);
1464 errs() << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001465 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001466 }
1467}
1468
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001469void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1470 const LiveRange::const_iterator I,
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001471 unsigned Reg, unsigned LaneMask) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001472 const LiveRange::Segment &S = *I;
1473 const VNInfo *VNI = S.valno;
Matthias Braun331de112013-10-10 21:28:43 +00001474 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001475
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001476 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001477 report("Foreign valno in live segment", MF, LR, Reg, LaneMask);
1478 errs() << S << " has a bad valno\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001479 }
1480
1481 if (VNI->isUnused()) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001482 report("Live segment valno is marked unused", MF, LR, Reg, LaneMask);
1483 errs() << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001484 }
1485
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001486 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001487 if (!MBB) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001488 report("Bad start of live segment, no basic block", MF, LR, Reg, LaneMask);
1489 errs() << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001490 return;
1491 }
1492 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001493 if (S.start != MBBStartIdx && S.start != VNI->def) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001494 report("Live segment must begin at MBB entry or valno def", MBB, LR, Reg,
1495 LaneMask);
1496 errs() << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001497 }
1498
1499 const MachineBasicBlock *EndMBB =
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001500 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001501 if (!EndMBB) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001502 report("Bad end of live segment, no basic block", MF, LR, Reg, LaneMask);
1503 errs() << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001504 return;
1505 }
1506
1507 // No more checks for live-out segments.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001508 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001509 return;
1510
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001511 // RegUnit intervals are allowed dead phis.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001512 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1513 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001514 return;
1515
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001516 // The live segment is ending inside EndMBB
1517 const MachineInstr *MI =
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001518 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001519 if (!MI) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001520 report("Live segment doesn't end at a valid instruction", EndMBB, LR, Reg,
1521 LaneMask);
1522 errs() << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001523 return;
1524 }
1525
1526 // The block slot must refer to a basic block boundary.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001527 if (S.end.isBlock()) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001528 report("Live segment ends at B slot of an instruction", EndMBB, LR, Reg,
1529 LaneMask);
1530 errs() << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001531 }
1532
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001533 if (S.end.isDead()) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001534 // Segment ends on the dead slot.
1535 // That means there must be a dead def.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001536 if (!SlotIndex::isSameInstr(S.start, S.end)) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001537 report("Live segment ending at dead slot spans instructions", EndMBB, LR,
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001538 Reg, LaneMask);
1539 errs() << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001540 }
1541 }
1542
1543 // A live segment can only end at an early-clobber slot if it is being
1544 // redefined by an early-clobber def.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001545 if (S.end.isEarlyClobber()) {
1546 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001547 report("Live segment ending at early clobber slot must be "
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001548 "redefined by an EC def in the same instruction", EndMBB, LR, Reg,
1549 LaneMask);
1550 errs() << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001551 }
1552 }
1553
1554 // The following checks only apply to virtual registers. Physreg liveness
1555 // is too weird to check.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001556 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun331de112013-10-10 21:28:43 +00001557 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001558 // use, or a dead flag on a def.
1559 bool hasRead = false;
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001560 bool hasSubRegDef = false;
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001561 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001562 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001563 continue;
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001564 if (LaneMask != 0 &&
1565 (LaneMask & TRI->getSubRegIndexLaneMask(MOI->getSubReg())) == 0)
1566 continue;
1567 if (MOI->isDef() && MOI->getSubReg() != 0)
1568 hasSubRegDef = true;
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001569 if (MOI->readsReg())
1570 hasRead = true;
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001571 }
Pedro Artigasd900b112013-11-08 22:46:28 +00001572 if (!S.end.isDead()) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001573 if (!hasRead) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001574 // When tracking subregister liveness, the main range must start new
1575 // values on partial register writes, even if there is no read.
1576 if (!MRI->tracksSubRegLiveness() || LaneMask != 0 || !hasSubRegDef) {
1577 report("Instruction ending live segment doesn't read the register",
1578 MI);
1579 errs() << S << " in " << LR << '\n';
1580 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001581 }
1582 }
1583 }
1584
1585 // Now check all the basic blocks in this live segment.
1586 MachineFunction::const_iterator MFI = MBB;
Matthias Braun331de112013-10-10 21:28:43 +00001587 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001588 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001589 // Not live-in to any blocks.
1590 if (MBB == EndMBB)
1591 return;
1592 // Skip this block.
1593 ++MFI;
1594 }
1595 for (;;) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001596 assert(LiveInts->isLiveInToMBB(LR, MFI));
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001597 // We don't know how to track physregs into a landing pad.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001598 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001599 MFI->isLandingPad()) {
1600 if (&*MFI == EndMBB)
1601 break;
1602 ++MFI;
1603 continue;
1604 }
1605
1606 // Is VNI a PHI-def in the current block?
1607 bool IsPHI = VNI->isPHIDef() &&
1608 VNI->def == LiveInts->getMBBStartIdx(MFI);
1609
1610 // Check that VNI is live-out of all predecessors.
1611 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1612 PE = MFI->pred_end(); PI != PE; ++PI) {
1613 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001614 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001615
1616 // All predecessors must have a live-out value.
1617 if (!PVNI) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001618 report("Register not marked live out of predecessor", *PI, LR, Reg,
1619 LaneMask);
1620 errs() << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001621 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001622 << PEnd << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001623 continue;
1624 }
1625
1626 // Only PHI-defs can take different predecessor values.
1627 if (!IsPHI && PVNI != VNI) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001628 report("Different value live out of predecessor", *PI, LR, Reg,
1629 LaneMask);
1630 errs() << "Valno #" << PVNI->id << " live out of BB#"
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001631 << (*PI)->getNumber() << '@' << PEnd
1632 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001633 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001634 }
1635 }
1636 if (&*MFI == EndMBB)
1637 break;
1638 ++MFI;
1639 }
1640}
1641
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001642void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
1643 unsigned LaneMask) {
1644 for (const VNInfo *VNI : LR.valnos)
1645 verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001646
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001647 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001648 verifyLiveRangeSegment(LR, I, Reg, LaneMask);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001649}
1650
1651void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1652 verifyLiveRange(LI, LI.reg);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001653
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001654 unsigned Reg = LI.reg;
1655 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1656 unsigned Mask = 0;
1657 unsigned MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
1658 for (const LiveInterval::SubRange &SR : LI.subranges()) {
1659 if ((Mask & SR.LaneMask) != 0)
1660 report("Lane masks of sub ranges overlap in live interval", MF, LI);
1661 if ((SR.LaneMask & ~MaxMask) != 0)
1662 report("Subrange lanemask is invalid", MF, LI);
1663 Mask |= SR.LaneMask;
1664 verifyLiveRange(SR, LI.reg, SR.LaneMask);
1665 if (!LI.covers(SR))
1666 report("A Subrange is not covered by the main range", MF, LI);
1667 }
1668 } else if (LI.hasSubRanges()) {
1669 report("subregister liveness only allowed for virtual registers", MF, LI);
1670 }
1671
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001672 // Check the LI only has one connected component.
1673 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1674 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1675 unsigned NumComp = ConEQ.Classify(&LI);
1676 if (NumComp > 1) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001677 report("Multiple connected components in live interval", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001678 for (unsigned comp = 0; comp != NumComp; ++comp) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001679 errs() << comp << ": valnos";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001680 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1681 E = LI.vni_end(); I!=E; ++I)
1682 if (comp == ConEQ.getEqClass(*I))
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001683 errs() << ' ' << (*I)->id;
1684 errs() << '\n';
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001685 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001686 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001687 }
1688}
Manman Ren7310b752013-07-15 21:26:31 +00001689
1690namespace {
1691 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1692 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1693 // value is zero.
1694 // We use a bool plus an integer to capture the stack state.
1695 struct StackStateOfBB {
1696 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1697 ExitIsSetup(false) { }
1698 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1699 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1700 ExitIsSetup(ExitSetup) { }
1701 // Can be negative, which means we are setting up a frame.
1702 int EntryValue;
1703 int ExitValue;
1704 bool EntryIsSetup;
1705 bool ExitIsSetup;
1706 };
1707}
1708
1709/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1710/// by a FrameDestroy <n>, stack adjustments are identical on all
1711/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1712void MachineVerifier::verifyStackFrame() {
1713 int FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1714 int FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
1715
1716 SmallVector<StackStateOfBB, 8> SPState;
1717 SPState.resize(MF->getNumBlockIDs());
1718 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1719
1720 // Visit the MBBs in DFS order.
1721 for (df_ext_iterator<const MachineFunction*,
1722 SmallPtrSet<const MachineBasicBlock*, 8> >
1723 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1724 DFI != DFE; ++DFI) {
1725 const MachineBasicBlock *MBB = *DFI;
1726
1727 StackStateOfBB BBState;
1728 // Check the exit state of the DFS stack predecessor.
1729 if (DFI.getPathLength() >= 2) {
1730 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1731 assert(Reachable.count(StackPred) &&
1732 "DFS stack predecessor is already visited.\n");
1733 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1734 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1735 BBState.ExitValue = BBState.EntryValue;
1736 BBState.ExitIsSetup = BBState.EntryIsSetup;
1737 }
1738
1739 // Update stack state by checking contents of MBB.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001740 for (const auto &I : *MBB) {
1741 if (I.getOpcode() == FrameSetupOpcode) {
Manman Ren7310b752013-07-15 21:26:31 +00001742 // The first operand of a FrameOpcode should be i32.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001743 int Size = I.getOperand(0).getImm();
Manman Ren7310b752013-07-15 21:26:31 +00001744 assert(Size >= 0 &&
1745 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1746
1747 if (BBState.ExitIsSetup)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001748 report("FrameSetup is after another FrameSetup", &I);
Manman Ren7310b752013-07-15 21:26:31 +00001749 BBState.ExitValue -= Size;
1750 BBState.ExitIsSetup = true;
1751 }
1752
Stephen Hinesdce4a402014-05-29 02:49:00 -07001753 if (I.getOpcode() == FrameDestroyOpcode) {
Manman Ren7310b752013-07-15 21:26:31 +00001754 // The first operand of a FrameOpcode should be i32.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001755 int Size = I.getOperand(0).getImm();
Manman Ren7310b752013-07-15 21:26:31 +00001756 assert(Size >= 0 &&
1757 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1758
1759 if (!BBState.ExitIsSetup)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001760 report("FrameDestroy is not after a FrameSetup", &I);
Manman Ren7310b752013-07-15 21:26:31 +00001761 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1762 BBState.ExitValue;
1763 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001764 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001765 errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
Manman Ren7310b752013-07-15 21:26:31 +00001766 << AbsSPAdj << ">.\n";
1767 }
1768 BBState.ExitValue += Size;
1769 BBState.ExitIsSetup = false;
1770 }
1771 }
1772 SPState[MBB->getNumber()] = BBState;
1773
1774 // Make sure the exit state of any predecessor is consistent with the entry
1775 // state.
1776 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
1777 E = MBB->pred_end(); I != E; ++I) {
1778 if (Reachable.count(*I) &&
1779 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
1780 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
1781 report("The exit stack state of a predecessor is inconsistent.", MBB);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001782 errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
Manman Ren7310b752013-07-15 21:26:31 +00001783 << SPState[(*I)->getNumber()].ExitValue << ", "
1784 << SPState[(*I)->getNumber()].ExitIsSetup
1785 << "), while BB#" << MBB->getNumber() << " has entry state ("
1786 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
1787 }
1788 }
1789
1790 // Make sure the entry state of any successor is consistent with the exit
1791 // state.
1792 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
1793 E = MBB->succ_end(); I != E; ++I) {
1794 if (Reachable.count(*I) &&
1795 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
1796 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
1797 report("The entry stack state of a successor is inconsistent.", MBB);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001798 errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
Manman Ren7310b752013-07-15 21:26:31 +00001799 << SPState[(*I)->getNumber()].EntryValue << ", "
1800 << SPState[(*I)->getNumber()].EntryIsSetup
1801 << "), while BB#" << MBB->getNumber() << " has exit state ("
1802 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
1803 }
1804 }
1805
1806 // Make sure a basic block with return ends with zero stack adjustment.
1807 if (!MBB->empty() && MBB->back().isReturn()) {
1808 if (BBState.ExitIsSetup)
1809 report("A return block ends with a FrameSetup.", MBB);
1810 if (BBState.ExitValue)
1811 report("A return block ends with a nonzero stack adjustment.", MBB);
1812 }
1813 }
1814}